Complete.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\common\service\order;
  3. use app\common\library\helper;
  4. use app\common\model\User as UserModel;
  5. use app\common\model\Setting as SettingModel;
  6. use app\common\model\dealer\Order as DealerOrderModel;
  7. use app\api\service\User as UserService;
  8. use app\common\model\user\PointsLog as PointsLogModel;
  9. use app\common\service\wechat\wow\Order as WowService;
  10. use app\common\enum\OrderType as OrderTypeEnum;
  11. /**
  12. * 已完成订单结算服务类
  13. * Class Complete
  14. * @package app\common\service\order
  15. */
  16. class Complete
  17. {
  18. /* @var int $orderType 订单类型 */
  19. private $orderType;
  20. /**
  21. * 订单模型类
  22. * @var array
  23. */
  24. private $orderModelClass = [
  25. OrderTypeEnum::MASTER => 'app\common\model\Order',
  26. OrderTypeEnum::SHARING => 'app\common\model\sharing\Order',
  27. ];
  28. /* @var \app\common\model\Order $model */
  29. private $model;
  30. /* @var UserModel $model */
  31. private $UserModel;
  32. /**
  33. * 构造方法
  34. * Complete constructor.
  35. * @param int $orderType
  36. */
  37. public function __construct($orderType = OrderTypeEnum::MASTER)
  38. {
  39. $this->orderType = $orderType;
  40. $this->model = $this->getOrderModel();
  41. $this->UserModel = new UserModel;
  42. }
  43. /**
  44. * 初始化订单模型类
  45. * @return \app\common\model\Order|mixed
  46. */
  47. private function getOrderModel()
  48. {
  49. $class = $this->orderModelClass[$this->orderType];
  50. return new $class;
  51. }
  52. /**
  53. * 执行订单完成后的操作
  54. * @param \think\Collection|array $orderList
  55. * @param int $wxappId
  56. * @return bool
  57. * @throws \app\common\exception\BaseException
  58. * @throws \think\Exception
  59. * @throws \think\exception\DbException
  60. * @throws \Exception
  61. */
  62. public function complete($orderList, $wxappId)
  63. {
  64. // 已完成订单结算
  65. // 条件:后台订单流程设置 - 已完成订单设置0天不允许申请售后
  66. if (SettingModel::getItem('trade', $wxappId)['order']['refund_days'] == 0) {
  67. $this->settled($orderList);
  68. }
  69. // 发放分销商佣金
  70. foreach ($orderList as $order) {
  71. // 结算分销佣金
  72. DealerOrderModel::grantOrderMoney($order, $this->orderType);
  73. // 用户升级
  74. UserService::upgrade($order['user_id'], $order['wxapp_id']);
  75. }
  76. // 更新好物圈订单状态
  77. if ($this->orderType == OrderTypeEnum::MASTER) {
  78. (new WowService($wxappId))->update($orderList);
  79. }
  80. return true;
  81. }
  82. /**
  83. * 执行订单结算
  84. * @param $orderList
  85. * @return bool
  86. * @throws \Exception
  87. */
  88. public function settled($orderList)
  89. {
  90. // 订单id集
  91. $orderIds = helper::getArrayColumn($orderList, 'order_id');
  92. // 累积用户实际消费金额
  93. $this->setIncUserExpend($orderList);
  94. // 处理订单赠送的积分
  95. $this->setGiftPointsBonus($orderList);
  96. // 将订单设置为已结算
  97. $this->model->onBatchUpdate($orderIds, ['is_settled' => 1]);
  98. return true;
  99. }
  100. /**
  101. * 处理订单赠送的积分
  102. * @param $orderList
  103. * @return bool
  104. * @throws \Exception
  105. */
  106. private function setGiftPointsBonus($orderList)
  107. {
  108. // 计算用户所得积分
  109. $userData = [];
  110. $logData = [];
  111. foreach ($orderList as $order) {
  112. // 计算用户所得积分
  113. $pointsBonus = $order['points_bonus'];
  114. if ($pointsBonus <= 0) continue;
  115. // 减去订单退款的积分
  116. foreach ($order['goods'] as $goods) {
  117. if (
  118. !empty($goods['refund'])
  119. && $goods['refund']['type']['value'] == 10 // 售后类型:退货退款
  120. && $goods['refund']['is_agree']['value'] == 10 // 商家审核:已同意
  121. ) {
  122. $pointsBonus -= $goods['points_bonus'];
  123. }
  124. }
  125. // 计算用户所得积分
  126. !isset($userData[$order['user_id']]) && $userData[$order['user_id']] = 0;
  127. $userData[$order['user_id']] += $pointsBonus;
  128. // 整理用户积分变动明细
  129. $logData[] = [
  130. 'user_id' => $order['user_id'],
  131. 'value' => $pointsBonus,
  132. 'describe' => "订单赠送:{$order['order_no']}",
  133. 'wxapp_id' => $order['wxapp_id'],
  134. ];
  135. }
  136. if (!empty($userData)) {
  137. // 累积到会员表记录
  138. $this->UserModel->onBatchIncPoints($userData);
  139. // 批量新增积分明细记录
  140. (new PointsLogModel)->onBatchAdd($logData);
  141. }
  142. return true;
  143. }
  144. /**
  145. * 累积用户实际消费金额
  146. * @param $orderList
  147. * @return bool
  148. * @throws \Exception
  149. */
  150. private function setIncUserExpend($orderList)
  151. {
  152. // 计算并累积实际消费金额(需减去售后退款的金额)
  153. $userData = [];
  154. $upgradeData = [];
  155. foreach ($orderList as $order) {
  156. // 订单实际支付金额
  157. $expendMoney = $order['pay_price'];
  158. $upgradeMoney = $order['is_upgrade']? $order['pay_price'] : 0;
  159. // 减去订单退款的金额
  160. foreach ($order['goods'] as $goods) {
  161. if (
  162. !empty($goods['refund'])
  163. && $goods['refund']['type']['value'] == 10 // 售后类型:退货退款
  164. && $goods['refund']['is_agree']['value'] == 10 // 商家审核:已同意
  165. ) {
  166. $expendMoney -= $goods['refund']['refund_money'];
  167. $upgradeMoney -= $goods['refund']['refund_money'];
  168. }
  169. }
  170. !isset($userData[$order['user_id']]) && $userData[$order['user_id']] = 0.00;
  171. $expendMoney > 0 && $userData[$order['user_id']] += $expendMoney;
  172. $upgradeMoney > 0 && $upgradeData[$order['user_id']] += $expendMoney;
  173. }
  174. // 累积到会员表记录
  175. $this->UserModel->onBatchIncExpendMoney($userData);
  176. $this->UserModel->onBatchIncUpgradeMoney($upgradeData);
  177. return true;
  178. }
  179. }