Complete.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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\common\model\user\Grade;
  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. DealerOrderModel::grantMoney($order, $this->orderType);
  72. }
  73. // 更新好物圈订单状态
  74. if ($this->orderType == OrderTypeEnum::MASTER) {
  75. (new WowService($wxappId))->update($orderList);
  76. }
  77. return true;
  78. }
  79. /**
  80. * 执行订单结算
  81. * @param $orderList
  82. * @return bool
  83. * @throws \Exception
  84. */
  85. public function settled($orderList)
  86. {
  87. // 订单id集
  88. $orderIds = helper::getArrayColumn($orderList, 'order_id');
  89. // 累积用户实际消费金额
  90. $this->setIncUserExpend($orderList);
  91. // 处理订单赠送的积分
  92. $this->setGiftPointsBonus($orderList);
  93. // 将订单设置为已结算
  94. $this->model->onBatchUpdate($orderIds, ['is_settled' => 1]);
  95. return true;
  96. }
  97. /**
  98. * 处理订单赠送的积分
  99. * @param $orderList
  100. * @return bool
  101. * @throws \Exception
  102. */
  103. private function setGiftPointsBonus($orderList)
  104. {
  105. // 计算用户所得积分
  106. $userData = [];
  107. $logData = [];
  108. foreach ($orderList as $order) {
  109. // 计算用户所得积分
  110. $pointsBonus = $order['points_bonus'];
  111. if ($pointsBonus <= 0) continue;
  112. // 减去订单退款的积分
  113. foreach ($order['goods'] as $goods) {
  114. if (
  115. !empty($goods['refund'])
  116. && $goods['refund']['type']['value'] == 10 // 售后类型:退货退款
  117. && $goods['refund']['is_agree']['value'] == 10 // 商家审核:已同意
  118. ) {
  119. $pointsBonus -= $goods['points_bonus'];
  120. }
  121. }
  122. // 计算用户所得积分
  123. !isset($userData[$order['user_id']]) && $userData[$order['user_id']] = 0;
  124. $userData[$order['user_id']] += $pointsBonus;
  125. // 整理用户积分变动明细
  126. $logData[] = [
  127. 'user_id' => $order['user_id'],
  128. 'value' => $pointsBonus,
  129. 'describe' => "订单赠送:{$order['order_no']}",
  130. 'wxapp_id' => $order['wxapp_id'],
  131. ];
  132. }
  133. if (!empty($userData)) {
  134. // 累积到会员表记录
  135. $this->UserModel->onBatchIncPoints($userData);
  136. // 批量新增积分明细记录
  137. (new PointsLogModel)->onBatchAdd($logData);
  138. }
  139. return true;
  140. }
  141. /**
  142. * 累积用户实际消费金额
  143. * @param $orderList
  144. * @return bool
  145. * @throws \Exception
  146. */
  147. private function setIncUserExpend($orderList)
  148. {
  149. // 计算并累积实际消费金额(需减去售后退款的金额)
  150. $userData = [];
  151. $upgradeData = [];
  152. foreach ($orderList as $order) {
  153. // 订单实际支付金额
  154. $expendMoney = $order['pay_price'];
  155. $upgradeMoney = $order['is_upgrade']? $order['pay_price'] : 0;
  156. // 减去订单退款的金额
  157. foreach ($order['goods'] as $goods) {
  158. if (
  159. !empty($goods['refund'])
  160. && $goods['refund']['type']['value'] == 10 // 售后类型:退货退款
  161. && $goods['refund']['is_agree']['value'] == 10 // 商家审核:已同意
  162. ) {
  163. $expendMoney -= $goods['refund']['refund_money'];
  164. $upgradeMoney -= $goods['refund']['refund_money'];
  165. }
  166. }
  167. !isset($userData[$order['user_id']]) && $userData[$order['user_id']] = 0.00;
  168. $expendMoney > 0 && $userData[$order['user_id']] += $expendMoney;
  169. $upgradeMoney > 0 && $upgradeData[$order['user_id']] += $expendMoney;
  170. }
  171. // 累积到会员表记录
  172. $this->UserModel->onBatchIncExpendMoney($userData);
  173. $this->UserModel->onBatchIncUpgradeMoney($upgradeData);
  174. return true;
  175. }
  176. }