123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace app\services\order;
- use app\dao\order\StoreOrderDao;
- use app\jobs\order\AutoCommentOrderJob;
- use app\services\BaseServices;
- use app\services\product\product\StoreProductReplyServices;
- use app\services\user\UserServices;
- use think\exception\ValidateException;
- class StoreOrderCommentServices extends BaseServices
- {
-
- protected $commentArr = [
- 'product_score' => 5,
- 'service_score' => 5,
- 'comment' => '',
- 'pics' => [],
- ];
-
- public function __construct(StoreOrderDao $dao)
- {
- $this->dao = $dao;
- }
-
- public function comment(int $oid, int $uid = 0, array $group = [], string $unique = '')
- {
- if (!$oid) throw new ValidateException('参数错误!');
- $order = $this->dao->get($oid);
- if (!$order) {
- throw new ValidateException('订单不存在!');
- }
- $where = ['oid' => $oid];
- if ($unique) {
- $where['unique'] = $unique;
- }
-
- $cartInfoServices = app()->make(StoreOrderCartInfoServices::class);
- $cartInfos = $cartInfoServices->getColumn($where, 'oid,product_id,cart_info,unique');
- if (!$cartInfos) {
- throw new ValidateException('评价商品不存在');
- }
- $group = array_merge($this->commentArr, array_intersect_key($group, $this->commentArr));
- $group['comment'] = htmlspecialchars(trim($group['comment']));
- if ($group['product_score'] < 1) return app('json')->fail('请为商品评分');
- else if ($group['service_score'] < 1) return app('json')->fail('请为商家服务评分');
- if ($group['pics']) $group['pics'] = json_encode(is_array($group['pics']) ? $group['pics'] : explode(',', $group['pics']));
- $user_info = [];
- if ($uid) {
-
- $userServices = app()->make(UserServices::class);
- $user_info = $userServices->getUserInfo($uid);
- }
- if ($order['store_id']) {
- $group['type'] = 1;
- $group['relation_id'] = $order['store_id'];
- } elseif ($order['supplier_id']) {
- $group['type'] = 2;
- $group['relation_id'] = $order['supplier_id'];
- } else {
- $group['type'] = $group['relation_id'] = 0;
- }
- $group = array_merge($group, [
- 'uid' => $uid,
- 'nickname' => $user_info['nickname'] ?? substr(md5($oid . time()), 0, 12),
- 'avatar' => $user_info['avatar'] ?? sys_config('h5_avatar'),
- 'oid' => $oid,
- 'add_time' => time(),
- 'reply_type' => 'product'
- ]);
-
- $replyServices = app()->make(StoreProductReplyServices::class);
- $data_all = [];
- foreach ($cartInfos as $cartInfo) {
- if ($replyServices->be(['oid' => $cartInfo['oid'], 'unique' => $cartInfo['unique']])) continue;
- $_info = is_string($cartInfo['cart_info']) ? json_decode($cartInfo['cart_info'], true) : $cartInfo['cart_info'];
- if (isset($_info['activity_id']) && $_info['activity_id']) $productId = $_info['product_id'];
- else $productId = $cartInfo['product_id'];
- $productId = $replyServices->checkReplyProductId((int)$productId);
- $group['unique'] = $cartInfo['unique'];
- $group['product_id'] = $productId;
- $group['sku_unique'] = $_info['attrInfo']['unique'] ?? '';
- $group['sku'] = $_info['attrInfo']['suk'] ?? '';
- $data_all[] = $group;
- }
- if ($data_all) {
- $res = $replyServices->saveAll($data_all);
- if (!$res) {
- throw new ValidateException('评价失败!');
- }
- }
- try {
-
- $orderServices = app()->make(StoreOrderServices::class);
- $orderServices->checkOrderOver($replyServices, $cartInfoServices->getCartColunm(['oid' => $oid, 'is_gift' => 0], 'unique', ''), $oid);
- } catch (\Exception $e) {
- throw new ValidateException($e->getMessage());
- }
-
- $replyServices->cacheTag()->clear();
-
- event('order.comment', [$order]);
- return true;
- }
-
- public function batchJoinJobs(array $where, int $count, int $maxLimit)
- {
- $page = ceil($count / $maxLimit);
- for ($i = 1; $i <= $page; $i++) {
- AutoCommentOrderJob::dispatch([$where, $i, $maxLimit]);
- }
- return true;
- }
-
- public function runAutoCommentOrder(array $where, int $page = 0, int $maxLimit = 0)
- {
-
- $service = app()->make(StoreOrderStoreOrderStatusServices::class);
- $orderList = $service->getOrderIds($where, $page, $maxLimit);
- foreach ($orderList as $order) {
- if ($order['status'] == 3) {
- continue;
- }
- $group = ['comment' => '此用户未作评价'];
- $this->comment((int)$order['id'], (int)$order['uid'], $group);
- }
- return true;
- }
-
- public function autoCommentOrder()
- {
-
- $systemCommentTime = (int)sys_config('system_comment_time', 0);
-
- if ($systemCommentTime == 0) {
- return true;
- }
- $sevenDay = strtotime(date('Y-m-d H:i:s', strtotime('-' . $systemCommentTime . ' day')));
-
- $service = app()->make(StoreOrderStoreOrderStatusServices::class);
- $where = [
- 'change_time' => $sevenDay,
- 'is_del' => 0,
- 'paid' => 1,
- 'status' => 2,
- 'change_type' => ['user_take_delivery', 'take_delivery']
- ];
- $maxLimit = 20;
- $count = $service->getOrderCount($where);
- if ($count > $maxLimit) {
- return $this->batchJoinJobs($where, $count, $maxLimit);
- }
- return $this->runAutoCommentOrder($where);
- }
- }
|