UserOrderRepository.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\user;
  12. use app\common\dao\user\LabelRuleDao;
  13. use app\common\dao\user\UserOrderDao;
  14. use app\common\repositories\BaseRepository;
  15. use app\common\repositories\store\order\StoreOrderRepository;
  16. use app\common\repositories\system\groupData\GroupDataRepository;
  17. use app\common\repositories\system\merchant\FinancialRecordRepository;
  18. use crmeb\jobs\SendSmsJob;
  19. use crmeb\services\PayService;
  20. use FormBuilder\Factory\Elm;
  21. use think\facade\Db;
  22. use think\facade\Log;
  23. use think\facade\Queue;
  24. /**
  25. * Class LabelRuleRepository
  26. * @package app\common\repositories\user
  27. * @author xaboy
  28. * @day 2020/10/20
  29. * @mixin UserOrderDao
  30. */
  31. class UserOrderRepository extends BaseRepository
  32. {
  33. //付费会员
  34. const TYPE_SVIP = 'S-';
  35. /**
  36. * LabelRuleRepository constructor.
  37. * @param UserOrderDao $dao
  38. */
  39. public function __construct(UserOrderDao $dao)
  40. {
  41. $this->dao = $dao;
  42. }
  43. /**
  44. * 根据条件获取列表数据
  45. *
  46. * 本函数用于根据给定的条件数组$where,从数据库中检索满足条件的数据列表。
  47. * 它支持分页查询,每页的数据数量由$limit参数指定,页码由$page参数指定。
  48. * 查询结果包括两部分:总数据量$count和当前页的数据列表$list。
  49. * 数据列表中的每个项目都包含了用户信息,这些信息是通过关联查询获取的,只包含指定的字段。
  50. *
  51. * @param array $where 查询条件数组
  52. * @param int $page 当前页码
  53. * @param int $limit 每页的数据数量
  54. * @return array 包含总数据量和当前页数据列表的数组
  55. */
  56. public function getList(array $where, $page, $limit)
  57. {
  58. // 根据条件查询数据
  59. $query = $this->dao->search($where);
  60. // 计算满足条件的总数据量
  61. $count = $query->count();
  62. // 查询数据列表,同时加载每个项目的用户信息,只包含指定的用户字段
  63. $list = $query->with([
  64. 'user' => function($query){
  65. $query->field('uid,nickname,avatar,phone,is_svip,svip_endtime');
  66. }
  67. ])
  68. ->order('create_time DESC') // 按照创建时间降序排序
  69. ->page($page, $limit) // 进行分页查询
  70. ->select()->toArray(); // 将查询结果转换为数组形式
  71. // 返回包含总数据量和数据列表的数组
  72. return compact('count', 'list');
  73. }
  74. /**
  75. * 添加会员订单
  76. *
  77. * @param object $res 订单资源信息
  78. * @param object $user 用户信息
  79. * @param array $params 额外参数,包括支付类型和是否为APP支付等
  80. * @return mixed 返回支付配置信息或订单ID
  81. */
  82. public function add($res, $user, $params)
  83. {
  84. $order_sn = app()->make(StoreOrderRepository::class)->getNewOrderId(StoreOrderRepository::TYPE_SN_USER_ORDER);
  85. $data = [
  86. 'title' => $res['value']['svip_name'],
  87. 'link_id' => $res->group_data_id,
  88. 'order_sn' => $order_sn,
  89. 'pay_price' => $res['value']['price'],
  90. 'order_info' => json_encode($res['value'],JSON_UNESCAPED_UNICODE),
  91. 'uid' => $user->uid,
  92. 'order_type' => self::TYPE_SVIP.$res['value']['svip_type'],
  93. 'pay_type' => $res['value']['price'] == 0 ? 'free' : $params['pay_type'],
  94. 'status' => 1,
  95. 'other' => $user->is_svip == -1 ? 'first' : '',
  96. ];
  97. $body = [
  98. 'order_sn' => $order_sn,
  99. 'pay_price' => $data['pay_price'],
  100. 'attach' => 'user_order',
  101. 'body' =>'付费会员'
  102. ];
  103. $type = $params['pay_type'];
  104. if (in_array($type, ['weixin', 'alipay'], true) && $params['is_app']) {
  105. $type .= 'App';
  106. }
  107. if ($params['return_url'] && $type === 'alipay') $body['return_url'] = $params['return_url'];
  108. $info = $this->dao->create($data);
  109. if ($data['pay_price']){
  110. try {
  111. $service = new PayService($type,$body, 'user_order');
  112. $config = $service->pay($user);
  113. return app('json')->status($type, $config + ['order_id' => $info->order_id]);
  114. } catch (\Exception $e) {
  115. return app('json')->status('error', $e->getMessage(), ['order_id' => $info->order_id]);
  116. }
  117. } else {
  118. $res = $this->paySuccess($data);
  119. return app('json')->status('success', ['order_id' => $info->order_id]);
  120. }
  121. }
  122. /**
  123. * 处理支付成功的逻辑。
  124. *
  125. * 当支付成功时,此函数将被调用以处理后续操作,例如更新订单状态,
  126. * 对于不同类型的订单,可能需要执行不同的操作,比如更新会员状态等。
  127. *
  128. * @param array $data 支付相关的数据,包含订单号等信息。
  129. * @return mixed 根据订单类型的不同,可能返回不同的结果。
  130. */
  131. public function paySuccess($data)
  132. {
  133. $res = $this->dao->getWhere(['order_sn' => $data['order_sn']]);
  134. $type = explode('-',$res['order_type'])[0].'-';
  135. Log::info('付费会员支付回调执行' . var_export([$data,$res->toArray(),$type,'----------------------------'],1));
  136. // 付费会员充值
  137. if ($res->pay_time && $res->paid) return true;
  138. if ($type == self::TYPE_SVIP) {
  139. return Db::transaction(function () use($data, $res) {
  140. $res->paid = 1;
  141. $res->pay_time = date('y_m-d H:i:s', time());
  142. $res->transaction_id = $data['data']['transaction_id'] ?? ($data['data']['trade_no'] ?? '');
  143. $res->save();
  144. return $this->payAfter($res, $res);
  145. });
  146. }
  147. }
  148. /**
  149. * 后付费处理函数
  150. * 该函数用于处理用户购买会员后的支付流程,包括更新用户会员状态、记录支付信息、生成财务记录和用户账单。
  151. *
  152. * @param array $data 包含订单信息的数据数组
  153. * @param object $ret 订单返回对象,包含订单相关数据
  154. */
  155. public function payAfter($data, $ret)
  156. {
  157. $financialRecordRepository = app()->make(FinancialRecordRepository::class);
  158. $userBillRepository = app()->make(UserBillRepository::class);
  159. $bill = $userBillRepository->getWhere(['type' => 'svip_pay','link_id' => $ret->order_id]);
  160. if ($bill) return true;
  161. $info = json_decode($data['order_info']);
  162. $user = app()->make(UserRepository::class)->get($ret['uid']);
  163. // 获取会员的增加天数
  164. $day = $info->svip_type == 3 ? 0 : $info->svip_number;
  165. //如果是过期了的会员,则更新结束时间以当前时间增加,否则是根据结束时间增加
  166. $endtime = ($user['svip_endtime'] && $user['is_svip'] != 0) ? $user['svip_endtime'] : date('Y-m-d H:i:s',time());
  167. $svip_endtime = date('Y-m-d H:i:s',strtotime("$endtime +$day day" ));
  168. $user->is_svip = $info->svip_type;
  169. $user->svip_endtime = $svip_endtime;
  170. $user->save();
  171. $ret->status = 1;
  172. $ret->pay_time = date('Y-m-d H:i:s',time());
  173. $ret->end_time = $svip_endtime;
  174. $ret->save();
  175. $title = '支付会员';
  176. if ($info->svip_type == 3) {
  177. $date = '终身会员';
  178. $mark = '终身会员';
  179. } else {
  180. $date = $svip_endtime;
  181. $mark = '到期时间'.$svip_endtime;
  182. }
  183. $financialRecordRepository->inc([
  184. 'order_id' => $ret->order_id,
  185. 'order_sn' => $ret->order_sn,
  186. 'user_info'=> $user->nickname,
  187. 'user_id' => $user->uid,
  188. 'financial_type' => $financialRecordRepository::FINANCIA_TYPE_SVIP,
  189. 'number' => $ret->pay_price,
  190. 'type' => 2,
  191. 'pay_type' => 0
  192. ],0);
  193. $userBillRepository->incBill($ret['uid'],UserBillRepository::CATEGORY_SVIP_PAY,'svip_pay',[
  194. 'link_id' => $ret->order_id,
  195. 'title' => $title,
  196. 'number'=> $ret->pay_price,
  197. 'status'=> 1,
  198. 'mark' => $mark,
  199. ]);
  200. if ($user->phone) Queue::push(SendSmsJob::class,['tempId' => 'SVIP_PAY_SUCCESS','id' => ['phone' => $user->phone, 'date' => $date]]);
  201. //小程序发货管理
  202. event('mini_order_shipping', ['member', $ret, 3, '', '']);
  203. return true;
  204. }
  205. /**
  206. * 统计会员信息
  207. * @return array
  208. */
  209. public function countMemberInfo(array $where = [])
  210. {
  211. return [
  212. [
  213. 'className' => 'el-icon-s-goods',
  214. 'count' => $this->dao->search(['paid' => 1] + $where)->group('uid')->count(),
  215. 'field' => 'member_nums',
  216. 'name' => '累计付费会员人数'
  217. ],
  218. [
  219. 'className' => 'el-icon-s-goods',
  220. 'count' => $this->dao->search(['paid' => 1, 'pay_price' => 0] + $where)->sum('UserOrder.pay_price'),
  221. 'field' => 'total_membership_fee',
  222. 'name' => '累计支付会员费'
  223. ],
  224. [
  225. 'className' => 'el-icon-s-goods',
  226. 'count' => app()->make(UserRepository::class)->search(['svip_type' => 0])->count(),
  227. 'field' => 'member_expire_nums',
  228. 'name' => '累计已过期人数'
  229. ],
  230. ];
  231. }
  232. }