123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\common\service\order;
- use app\api\model\Comment as CommentModel;
- use app\common\library\helper;
- use app\common\model\User as UserModel;
- use app\common\model\Order as OrderModel;
- use app\common\model\store\Setting as SettingModel;
- use app\common\model\user\PointsLog as PointsLogModel;
- use app\common\enum\Setting as SettingEnum;
- use app\common\model\OrderGoods;
- use app\common\service\BaseService;
- /**
- * 已完成订单结算服务类
- * Class Complete
- * @package app\common\service\order
- */
- class Complete extends BaseService
- {
- // 订单模型
- /* @var OrderModel $model */
- private $model;
- // 用户模型
- /* @var UserModel $model */
- private $UserModel;
- /**
- * 构造方法
- * Complete constructor.
- */
- public function initialize()
- {
- $this->model = new OrderModel;
- $this->UserModel = new UserModel;
- }
- /**
- * 执行订单完成后的操作
- * @param iterable $orderList
- * @param int $storeId
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function complete(iterable $orderList, int $storeId)
- {
- // 已完成订单结算
- // 条件:后台订单流程设置 - 已完成订单设置0天不允许申请售后
- if (SettingModel::getItem(SettingEnum::TRADE, $storeId)['order']['refund_days'] == 0) {
- $this->settled($orderList);
- }
- return true;
- }
- /**
- * 执行订单结算
- * @param $orderList
- * @return bool
- */
- public function settled($orderList)
- {
- // 订单id集
- $orderIds = helper::getArrayColumn($orderList, 'order_id');
- // 累积用户实际消费金额
- $this->setIncUserExpend($orderList);
- // 处理订单赠送的积分
- $this->setGiftPointsBonus($orderList);
- // 将订单设置为已结算
- $this->model->onBatchUpdate($orderIds, ['is_settled' => 1]);
- return true;
- }
- public function comment($orderList)
- {
- foreach ($orderList as $order) {
- $commentData = [];
- foreach ($order['goods'] as $goods) {
- if ($goods['is_comment'] == 0) {
- // 待评价
- $commentData[] = [
- 'score' => 5,
- 'user_id' => $order['user_id'],
- 'nick_name' => $order['user']['nick_name'] ?? '',
- 'store_id' => 10001,
- 'sort' => 100,
- 'is_public' => 0, // 是否公开评论
- 'order_id' => $order['order_id'],
- 'goods_id' => $goods['goods_id'],
- 'order_goods_id' => $goods['order_goods_id'],
- 'status' => 1,
- 'create_time' => time(),
- 'update_time' => time(),
- 'content' => "此用户没有填写评价。"
- ];
- }
- }
- // 记录评价内容
- if (!empty($commentData)) {
- $commentModel = new CommentModel();
- $result = $commentModel->addAll($commentData);
- // 更新订单评价状态
- $this->updateOrderIsComment($order, true, $result);
- }
- }
- return true;
- }
- /**
- * 更新订单评价状态
- * @param OrderModel $order
- * @param $isComment
- * @param $commentList
- * @return array|false
- * @throws \Exception
- */
- private function updateOrderIsComment($order, $isComment, $commentList)
- {
- // 更新订单商品
- $orderGoodsData = [];
- foreach ($commentList as $comment) {
- $orderGoodsData[] = [
- 'where' => [
- 'order_goods_id' => $comment['order_goods_id'],
- ],
- 'data' => [
- 'is_comment' => 1
- ]
- ];
- }
- // 更新订单
- $isComment && $order->where('order_id', $order['order_id'])->update(['is_comment' => 1]);
- return (new OrderGoods())->updateAll($orderGoodsData);
- }
- /**
- * 处理订单赠送的积分
- * @param $orderList
- * @return bool
- */
- private function setGiftPointsBonus($orderList)
- {
- // 计算用户所得积分
- $userData = [];
- $logData = [];
- foreach ($orderList as $order) {
- // 计算用户所得积分
- $pointsBonus = $order['points_bonus'];
- if ($pointsBonus <= 0) continue;
- // 减去订单退款的积分
- foreach ($order['goods'] as $goods) {
- if (
- !empty($goods['refund'])
- && $goods['refund']['type'] == 10 // 售后类型:退货退款
- && $goods['refund']['audit_status'] == 10 // 商家审核:已同意
- ) {
- $pointsBonus -= $goods['points_bonus'];
- }
- }
- // 计算用户所得积分
- !isset($userData[$order['user_id']]) && $userData[$order['user_id']] = 0;
- $userData[$order['user_id']] += $pointsBonus;
- // 整理用户积分变动明细
- $logData[] = [
- 'user_id' => $order['user_id'],
- 'value' => $pointsBonus,
- 'describe' => "订单赠送:{$order['order_no']}",
- 'store_id' => $order['store_id'],
- ];
- }
- if (!empty($userData)) {
- // 累积到会员表记录
- $this->UserModel->onBatchIncPoints($userData);
- // 批量新增积分明细记录
- (new PointsLogModel)->onBatchAdd($logData);
- }
- return true;
- }
- /**
- * 累积用户实际消费金额
- * @param $orderList
- * @return bool
- */
- private function setIncUserExpend($orderList)
- {
- // 计算并累积实际消费金额(需减去售后退款的金额)
- $userData = [];
- foreach ($orderList as $order) {
- // 订单实际支付金额
- $expendMoney = $order['pay_price'];
- // 减去订单退款的金额
- foreach ($order['goods'] as $goods) {
- if (
- !empty($goods['refund'])
- && $goods['refund']['type'] == 10 // 售后类型:退货退款
- && $goods['refund']['audit_status'] == 10 // 商家审核:已同意
- ) {
- $expendMoney -= $goods['refund']['refund_money'];
- }
- }
- !isset($userData[$order['user_id']]) && $userData[$order['user_id']] = 0.00;
- $expendMoney > 0 && $userData[$order['user_id']] += $expendMoney;
- }
- // 累积到会员表记录
- $this->UserModel->onBatchIncExpendMoney($userData);
- return true;
- }
- }
|