Complete.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\service\order;
  13. use app\api\model\Comment as CommentModel;
  14. use app\common\library\helper;
  15. use app\common\model\User as UserModel;
  16. use app\common\model\Order as OrderModel;
  17. use app\common\model\store\Setting as SettingModel;
  18. use app\common\model\user\PointsLog as PointsLogModel;
  19. use app\common\enum\Setting as SettingEnum;
  20. use app\common\model\OrderGoods;
  21. use app\common\service\BaseService;
  22. /**
  23. * 已完成订单结算服务类
  24. * Class Complete
  25. * @package app\common\service\order
  26. */
  27. class Complete extends BaseService
  28. {
  29. // 订单模型
  30. /* @var OrderModel $model */
  31. private $model;
  32. // 用户模型
  33. /* @var UserModel $model */
  34. private $UserModel;
  35. /**
  36. * 构造方法
  37. * Complete constructor.
  38. */
  39. public function initialize()
  40. {
  41. $this->model = new OrderModel;
  42. $this->UserModel = new UserModel;
  43. }
  44. /**
  45. * 执行订单完成后的操作
  46. * @param iterable $orderList
  47. * @param int $storeId
  48. * @return bool
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public function complete(iterable $orderList, int $storeId)
  54. {
  55. // 已完成订单结算
  56. // 条件:后台订单流程设置 - 已完成订单设置0天不允许申请售后
  57. if (SettingModel::getItem(SettingEnum::TRADE, $storeId)['order']['refund_days'] == 0) {
  58. $this->settled($orderList);
  59. }
  60. return true;
  61. }
  62. /**
  63. * 执行订单结算
  64. * @param $orderList
  65. * @return bool
  66. */
  67. public function settled($orderList)
  68. {
  69. // 订单id集
  70. $orderIds = helper::getArrayColumn($orderList, 'order_id');
  71. // 累积用户实际消费金额
  72. $this->setIncUserExpend($orderList);
  73. // 处理订单赠送的积分
  74. $this->setGiftPointsBonus($orderList);
  75. // 将订单设置为已结算
  76. $this->model->onBatchUpdate($orderIds, ['is_settled' => 1]);
  77. return true;
  78. }
  79. public function comment($orderList)
  80. {
  81. foreach ($orderList as $order) {
  82. $commentData = [];
  83. foreach ($order['goods'] as $goods) {
  84. if ($goods['is_comment'] == 0) {
  85. // 待评价
  86. $commentData[] = [
  87. 'score' => 5,
  88. 'user_id' => $order['user_id'],
  89. 'nick_name' => $order['user']['nick_name'] ?? '',
  90. 'store_id' => 10001,
  91. 'sort' => 100,
  92. 'is_public' => 0, // 是否公开评论
  93. 'order_id' => $order['order_id'],
  94. 'goods_id' => $goods['goods_id'],
  95. 'order_goods_id' => $goods['order_goods_id'],
  96. 'status' => 1,
  97. 'create_time' => time(),
  98. 'update_time' => time(),
  99. 'content' => "此用户没有填写评价。"
  100. ];
  101. }
  102. }
  103. // 记录评价内容
  104. if (!empty($commentData)) {
  105. $commentModel = new CommentModel();
  106. $result = $commentModel->addAll($commentData);
  107. // 更新订单评价状态
  108. $this->updateOrderIsComment($order, true, $result);
  109. }
  110. }
  111. return true;
  112. }
  113. /**
  114. * 更新订单评价状态
  115. * @param OrderModel $order
  116. * @param $isComment
  117. * @param $commentList
  118. * @return array|false
  119. * @throws \Exception
  120. */
  121. private function updateOrderIsComment($order, $isComment, $commentList)
  122. {
  123. // 更新订单商品
  124. $orderGoodsData = [];
  125. foreach ($commentList as $comment) {
  126. $orderGoodsData[] = [
  127. 'where' => [
  128. 'order_goods_id' => $comment['order_goods_id'],
  129. ],
  130. 'data' => [
  131. 'is_comment' => 1
  132. ]
  133. ];
  134. }
  135. // 更新订单
  136. $isComment && $order->where('order_id', $order['order_id'])->update(['is_comment' => 1]);
  137. return (new OrderGoods())->updateAll($orderGoodsData);
  138. }
  139. /**
  140. * 处理订单赠送的积分
  141. * @param $orderList
  142. * @return bool
  143. */
  144. private function setGiftPointsBonus($orderList)
  145. {
  146. // 计算用户所得积分
  147. $userData = [];
  148. $logData = [];
  149. foreach ($orderList as $order) {
  150. // 计算用户所得积分
  151. $pointsBonus = $order['points_bonus'];
  152. if ($pointsBonus <= 0) continue;
  153. // 减去订单退款的积分
  154. foreach ($order['goods'] as $goods) {
  155. if (
  156. !empty($goods['refund'])
  157. && $goods['refund']['type'] == 10 // 售后类型:退货退款
  158. && $goods['refund']['audit_status'] == 10 // 商家审核:已同意
  159. ) {
  160. $pointsBonus -= $goods['points_bonus'];
  161. }
  162. }
  163. // 计算用户所得积分
  164. !isset($userData[$order['user_id']]) && $userData[$order['user_id']] = 0;
  165. $userData[$order['user_id']] += $pointsBonus;
  166. // 整理用户积分变动明细
  167. $logData[] = [
  168. 'user_id' => $order['user_id'],
  169. 'value' => $pointsBonus,
  170. 'describe' => "订单赠送:{$order['order_no']}",
  171. 'store_id' => $order['store_id'],
  172. ];
  173. }
  174. if (!empty($userData)) {
  175. // 累积到会员表记录
  176. $this->UserModel->onBatchIncPoints($userData);
  177. // 批量新增积分明细记录
  178. (new PointsLogModel)->onBatchAdd($logData);
  179. }
  180. return true;
  181. }
  182. /**
  183. * 累积用户实际消费金额
  184. * @param $orderList
  185. * @return bool
  186. */
  187. private function setIncUserExpend($orderList)
  188. {
  189. // 计算并累积实际消费金额(需减去售后退款的金额)
  190. $userData = [];
  191. foreach ($orderList as $order) {
  192. // 订单实际支付金额
  193. $expendMoney = $order['pay_price'];
  194. // 减去订单退款的金额
  195. foreach ($order['goods'] as $goods) {
  196. if (
  197. !empty($goods['refund'])
  198. && $goods['refund']['type'] == 10 // 售后类型:退货退款
  199. && $goods['refund']['audit_status'] == 10 // 商家审核:已同意
  200. ) {
  201. $expendMoney -= $goods['refund']['refund_money'];
  202. }
  203. }
  204. !isset($userData[$order['user_id']]) && $userData[$order['user_id']] = 0.00;
  205. $expendMoney > 0 && $userData[$order['user_id']] += $expendMoney;
  206. }
  207. // 累积到会员表记录
  208. $this->UserModel->onBatchIncExpendMoney($userData);
  209. return true;
  210. }
  211. }