StoreOrderCommentServices.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\order;
  12. use app\dao\order\StoreOrderDao;
  13. use app\jobs\order\AutoCommentOrderJob;
  14. use app\services\BaseServices;
  15. use app\services\product\product\StoreProductReplyServices;
  16. use app\services\user\UserServices;
  17. use think\exception\ValidateException;
  18. /**
  19. * 订单评价
  20. * Class StoreOrderCommentServices
  21. * @package app\services\order
  22. * @mixin StoreOrderDao
  23. */
  24. class StoreOrderCommentServices extends BaseServices
  25. {
  26. /**
  27. * 评论默认数据
  28. * @var string[]
  29. */
  30. protected $commentArr = [
  31. 'product_score' => 5,
  32. 'service_score' => 5,
  33. 'comment' => '',
  34. 'pics' => [],
  35. ];
  36. /**
  37. * 构造方法
  38. * StoreOrderTakeServices constructor.
  39. * @param StoreOrderDao $dao
  40. */
  41. public function __construct(StoreOrderDao $dao)
  42. {
  43. $this->dao = $dao;
  44. }
  45. /**
  46. * 订单评价
  47. * @param int $oid
  48. * @param int $uid
  49. * @param array $group
  50. * @param string $unique
  51. * @return bool
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. */
  56. public function comment(int $oid, int $uid = 0, array $group = [], string $unique = '')
  57. {
  58. if (!$oid) throw new ValidateException('参数错误!');
  59. $order = $this->dao->get($oid);
  60. if (!$order) {
  61. throw new ValidateException('订单不存在!');
  62. }
  63. $where = ['oid' => $oid];
  64. if ($unique) {
  65. $where['unique'] = $unique;
  66. }
  67. /** @var StoreOrderCartInfoServices $cartInfoServices */
  68. $cartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  69. $cartInfos = $cartInfoServices->getColumn($where, 'oid,product_id,cart_info,unique');
  70. if (!$cartInfos) {
  71. throw new ValidateException('评价商品不存在');
  72. }
  73. $group = array_merge($this->commentArr, array_intersect_key($group, $this->commentArr));
  74. $group['comment'] = htmlspecialchars(trim($group['comment']));
  75. if ($group['product_score'] < 1) return app('json')->fail('请为商品评分');
  76. else if ($group['service_score'] < 1) return app('json')->fail('请为商家服务评分');
  77. if ($group['pics']) $group['pics'] = json_encode(is_array($group['pics']) ? $group['pics'] : explode(',', $group['pics']));
  78. $user_info = [];
  79. if ($uid) {
  80. /** @var UserServices $userServices */
  81. $userServices = app()->make(UserServices::class);
  82. $user_info = $userServices->getUserInfo($uid);
  83. }
  84. if ($order['store_id']) {
  85. $group['type'] = 1;
  86. $group['relation_id'] = $order['store_id'];
  87. } elseif ($order['supplier_id']) {
  88. $group['type'] = 2;
  89. $group['relation_id'] = $order['supplier_id'];
  90. } else {
  91. $group['type'] = $group['relation_id'] = 0;
  92. }
  93. $group = array_merge($group, [
  94. 'uid' => $uid,
  95. 'nickname' => $user_info['nickname'] ?? substr(md5($oid . time()), 0, 12),
  96. 'avatar' => $user_info['avatar'] ?? sys_config('h5_avatar'),
  97. 'oid' => $oid,
  98. 'add_time' => time(),
  99. 'reply_type' => 'product'
  100. ]);
  101. /** @var StoreProductReplyServices $replyServices */
  102. $replyServices = app()->make(StoreProductReplyServices::class);
  103. $data_all = [];
  104. foreach ($cartInfos as $cartInfo) {
  105. if ($replyServices->be(['oid' => $cartInfo['oid'], 'unique' => $cartInfo['unique']])) continue;
  106. $_info = is_string($cartInfo['cart_info']) ? json_decode($cartInfo['cart_info'], true) : $cartInfo['cart_info'];
  107. if (isset($_info['activity_id']) && $_info['activity_id']) $productId = $_info['product_id'];
  108. else $productId = $cartInfo['product_id'];
  109. $productId = $replyServices->checkReplyProductId((int)$productId);
  110. $group['unique'] = $cartInfo['unique'];
  111. $group['product_id'] = $productId;
  112. $group['sku_unique'] = $_info['attrInfo']['unique'] ?? '';
  113. $group['sku'] = $_info['attrInfo']['suk'] ?? '';
  114. $data_all[] = $group;
  115. }
  116. if ($data_all) {
  117. $res = $replyServices->saveAll($data_all);
  118. if (!$res) {
  119. throw new ValidateException('评价失败!');
  120. }
  121. }
  122. try {
  123. /** @var StoreOrderServices $orderServices */
  124. $orderServices = app()->make(StoreOrderServices::class);
  125. $orderServices->checkOrderOver($replyServices, $cartInfoServices->getCartColunm(['oid' => $oid, 'is_gift' => 0], 'unique', ''), $oid);
  126. } catch (\Exception $e) {
  127. throw new ValidateException($e->getMessage());
  128. }
  129. //清除评价缓存
  130. $replyServices->cacheTag()->clear();
  131. //订单评价成功事件
  132. event('order.comment', [$order]);
  133. return true;
  134. }
  135. /**
  136. * 加入队列
  137. * @param array $where
  138. * @param int $count
  139. * @param int $maxLimit
  140. * @return bool
  141. */
  142. public function batchJoinJobs(array $where, int $count, int $maxLimit)
  143. {
  144. $page = ceil($count / $maxLimit);
  145. for ($i = 1; $i <= $page; $i++) {
  146. AutoCommentOrderJob::dispatch([$where, $i, $maxLimit]);
  147. }
  148. return true;
  149. }
  150. /**
  151. * 执行自动默认好评
  152. * @param array $where
  153. * @param int $page
  154. * @param int $maxLimit
  155. * @return bool
  156. */
  157. public function runAutoCommentOrder(array $where, int $page = 0, int $maxLimit = 0)
  158. {
  159. /** @var StoreOrderStoreOrderStatusServices $service */
  160. $service = app()->make(StoreOrderStoreOrderStatusServices::class);
  161. $orderList = $service->getOrderIds($where, $page, $maxLimit);
  162. foreach ($orderList as $order) {
  163. if ($order['status'] == 3) {
  164. continue;
  165. }
  166. $group = ['comment' => '此用户未作评价'];
  167. $this->comment((int)$order['id'], (int)$order['uid'], $group);
  168. }
  169. return true;
  170. }
  171. /**
  172. * 自动默认好评
  173. * @return bool
  174. */
  175. public function autoCommentOrder()
  176. {
  177. //7天前时间戳
  178. $systemCommentTime = (int)sys_config('system_comment_time', 0);
  179. //0为取消自动默认好评功能
  180. if ($systemCommentTime == 0) {
  181. return true;
  182. }
  183. $sevenDay = strtotime(date('Y-m-d H:i:s', strtotime('-' . $systemCommentTime . ' day')));
  184. /** @var StoreOrderStoreOrderStatusServices $service */
  185. $service = app()->make(StoreOrderStoreOrderStatusServices::class);
  186. $where = [
  187. 'change_time' => $sevenDay,
  188. 'is_del' => 0,
  189. 'paid' => 1,
  190. 'status' => 2,
  191. 'change_type' => ['user_take_delivery', 'take_delivery']
  192. ];
  193. $maxLimit = 20;
  194. $count = $service->getOrderCount($where);
  195. if ($count > $maxLimit) {
  196. return $this->batchJoinJobs($where, $count, $maxLimit);
  197. }
  198. return $this->runAutoCommentOrder($where);
  199. }
  200. }