StoreServiceLogRepository.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace app\common\repositories\store\service;
  3. use app\common\dao\store\service\StoreServiceLogDao;
  4. use app\common\model\store\service\StoreServiceLog;
  5. use app\common\repositories\BaseRepository;
  6. use app\common\repositories\store\order\StoreOrderRepository;
  7. use app\common\repositories\store\order\StoreRefundOrderRepository;
  8. use app\common\repositories\store\product\ProductGroupRepository;
  9. use app\common\repositories\store\product\ProductPresellRepository;
  10. use app\common\repositories\store\product\ProductRepository;
  11. use think\exception\ValidateException;
  12. use think\facade\Cache;
  13. use think\facade\Db;
  14. use think\model\Relation;
  15. /**
  16. * Class StoreServiceLogRepository
  17. * @package app\common\repositories\store\service
  18. * @author zfy
  19. * @day 2020/5/29
  20. * @mixin StoreServiceLogDao
  21. */
  22. class StoreServiceLogRepository extends BaseRepository
  23. {
  24. /**
  25. * StoreServiceLogRepository constructor.
  26. * @param StoreServiceLogDao $dao
  27. */
  28. public function __construct(StoreServiceLogDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. public function tidyLogList($list, $sendType)
  33. {
  34. foreach ($list as $k => $log) {
  35. $list[$k]['last'] = $this->dao->getLastLog($log['mer_id'], $log['uid']);
  36. $list[$k]['num'] = ($list[$k]['last'] && $list[$k]['last']['send_type'] == $sendType && $list[$k]['last'][$sendType ? 'type' : 'service_type'] == 0) ? $this->getUnReadNum($log['mer_id'], $log['uid'], $sendType) : 0;
  37. }
  38. return $list;
  39. }
  40. /**
  41. * @param $uid
  42. * @param $page
  43. * @param $limit
  44. * @return array
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\DbException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. * @author zfy
  49. * @day 2020/6/16
  50. */
  51. public function userMerchantList($uid, $page, $limit)
  52. {
  53. $query = $this->dao->getMerchantListQuery($uid);
  54. $count = $query->count();
  55. $list = $query->setOption('field', [])->field('uid,mer_id,max(service_log_id) as service_log_id,type')->with(['merchant' => function ($query) {
  56. $query->field('mer_id,mer_avatar,mer_name');
  57. }])->page($page, $limit)->select()->toArray();
  58. $sort = array_column($list, 'service_log_id');
  59. array_multisort($sort, SORT_NUMERIC | SORT_DESC, $list);
  60. $list = $this->tidyLogList($list, 1);
  61. return compact('count', 'list');
  62. }
  63. /**
  64. * @param $uid
  65. * @param $page
  66. * @param $limit
  67. * @return array
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. * @author zfy
  72. * @day 2020/6/16
  73. */
  74. public function serviceUserList($merId, $uid, $page, $limit)
  75. {
  76. $service = app()->make(StoreServiceRepository::class)->getService($uid, $merId);
  77. if (!$service || !$service['status'])
  78. throw new ValidateException('没有权限');
  79. $query = $this->dao->getUserListQuery($service->service_id)->where('mer_id', $merId);
  80. $count = $query->count();
  81. $list = $query->setOption('field', [])->field('uid,mer_id,max(service_log_id) as service_log_id,type')->with(['user'])
  82. ->page($page, $limit)->select()->toArray();
  83. $sort = array_column($list, 'service_log_id');
  84. array_multisort($sort, SORT_NUMERIC | SORT_DESC, $list);
  85. $list = $this->tidyLogList($list, 0);
  86. return compact('count', 'list');
  87. }
  88. /**
  89. * @param $merId
  90. * @param $uid
  91. * @param $page
  92. * @param $limit
  93. * @return array
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\DbException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. * @author zfy
  98. * @day 2020/6/15
  99. */
  100. public function userList($merId, $uid, $page, $limit)
  101. {
  102. $query = $this->search(['mer_id' => $merId, 'uid' => $uid])->order('service_log_id DESC');
  103. $count = $query->count();
  104. $list = $query->page($page, $limit)->with(['user', 'service'])->select();
  105. if ($page == 1) $this->dao->userRead($merId, $uid);
  106. $list = array_reverse($this->getSendDataList($list)->toArray());
  107. return compact('count', 'list');
  108. }
  109. /**
  110. * @param $merId
  111. * @param $toUid
  112. * @param $uid
  113. * @param $page
  114. * @param $limit
  115. * @return array
  116. * @throws \think\db\exception\DataNotFoundException
  117. * @throws \think\db\exception\DbException
  118. * @throws \think\db\exception\ModelNotFoundException
  119. * @author zfy
  120. * @day 2020/6/15
  121. */
  122. public function merList($merId, $toUid, $uid, $page, $limit)
  123. {
  124. $service = app()->make(StoreServiceRepository::class)->getService($uid, $merId);
  125. if (!$service || !$service['status'])
  126. throw new ValidateException('没有权限');
  127. $query = $this->search(['mer_id' => $merId, 'uid' => $toUid])->order('service_log_id DESC');
  128. $count = $query->count();
  129. $list = $query->page($page, $limit)->with(['user', 'service'])->select();
  130. if ($page == 1) $this->dao->serviceRead($merId, $toUid, $service->service_id);
  131. $list = array_reverse($this->getSendDataList($list)->toArray());
  132. return compact('count', 'list');
  133. }
  134. /**
  135. * @param $merId
  136. * @param $uid
  137. * @param $type
  138. * @param $msn
  139. * @author zfy
  140. * @day 2020/6/13
  141. */
  142. public function checkMsn($merId, $uid, $type, $msn)
  143. {
  144. if ($type == 4 && !app()->make(ProductRepository::class)->merExists($merId, $msn))
  145. throw new ValidateException('商品不存在');
  146. else if ($type == 5 && !app()->make(StoreOrderRepository::class)->existsWhere(['uid' => $uid, 'mer_id' => $merId, 'order_id' => $msn]))
  147. throw new ValidateException('订单不存在');
  148. else if ($type == 6 && !app()->make(StoreRefundOrderRepository::class)->existsWhere(['uid' => $uid, 'mer_id' => $merId, 'refund_order_id' => $msn]))
  149. throw new ValidateException('退款单不存在');
  150. else if ($type == 7 && !app()->make(ProductPresellRepository::class)->existsWhere(['product_presell_id' => $msn, 'mer_id' => $merId]))
  151. throw new ValidateException('商品不存在');
  152. else if ($type == 8 && !app()->make(ProductGroupRepository::class)->existsWhere(['product_group_id' => $msn, 'mer_id' => $merId]))
  153. throw new ValidateException('商品不存在');
  154. }
  155. /**
  156. * @param StoreServiceLog $log
  157. * @return StoreServiceLog
  158. * @author zfy
  159. * @day 2020/6/15
  160. */
  161. public function getSendData(StoreServiceLog $log)
  162. {
  163. if ($log->msn_type == 4)
  164. $log->product;
  165. else if ($log->msn_type == 5)
  166. $log->orderInfo;
  167. else if ($log->msn_type == 6)
  168. $log->refundOrder;
  169. else if ($log->msn_type == 7)
  170. $log->presell;
  171. else if ($log->msn_type == 8)
  172. $log->productGroup;
  173. return $log;
  174. }
  175. public function getSendDataList($list)
  176. {
  177. $cache = [];
  178. foreach ($list as $log) {
  179. if (!in_array($log->msn_type, [4, 5, 6, 7,8])) continue;
  180. $key = $log->msn_type . $log->msn;
  181. if (isset($cache[$key])) {
  182. if ($log->msn_type == 4)
  183. $log->set('product', $cache[$key]);
  184. else if ($log->msn_type == 5)
  185. $log->set('orderInfo', $cache[$key]);
  186. else if ($log->msn_type == 6)
  187. $log->set('refundOrder', $cache[$key]);
  188. else if ($log->msn_type == 8)
  189. $log->set('productGroup', $cache[$key]);
  190. else
  191. $log->set('presell', $cache[$key]);
  192. } else {
  193. if ($log->msn_type == 4)
  194. $cache[$key] = $log->product;
  195. else if ($log->msn_type == 5)
  196. $cache[$key] = $log->orderInfo;
  197. else if ($log->msn_type == 6)
  198. $cache[$key] = $log->refundOrder;
  199. else if ($log->msn_type == 8)
  200. $cache[$key] = $log->productGroup;
  201. else
  202. $cache[$key] = $log->presell;
  203. }
  204. }
  205. return $list;
  206. }
  207. /**
  208. * @param $uid
  209. * @param $merId
  210. * @author zfy
  211. * @day 2020/6/15
  212. */
  213. public function userToChat($uid, $merId)
  214. {
  215. Cache::set('u_chat' . $uid, $merId, 3600);
  216. }
  217. /**
  218. * @param $uid
  219. * @param $toUid
  220. * @author zfy
  221. * @day 2020/6/15
  222. */
  223. public function serviceToChat($uid, $toUid)
  224. {
  225. Cache::set('s_chat' . $uid, $toUid, 3600);
  226. }
  227. /**
  228. * @param $uid
  229. * @param bool $isService
  230. * @author zfy
  231. * @day 2020/6/15
  232. */
  233. public function getChat($uid, $isService = false)
  234. {
  235. $key = ($isService ? 's_chat' : 'u_chat') . $uid;
  236. return Cache::get($key);
  237. }
  238. /**
  239. * @param $uid
  240. * @param bool $isService
  241. * @author zfy
  242. * @day 2020/6/15
  243. */
  244. public function unChat($uid, $isService = false)
  245. {
  246. $key = ($isService ? 's_chat' : 'u_chat') . $uid;
  247. Cache::delete($key);
  248. }
  249. /**
  250. * TODO 获取某个客服的用户列表
  251. * @param $service_id
  252. * @param $page
  253. * @param $limit
  254. * @return array
  255. * @author Qinii
  256. * @day 2020-06-18
  257. */
  258. public function getServiceUserList($service_id, $page, $limit)
  259. {
  260. $query = $this->dao->getUserListQuery($service_id);
  261. $count = $query->count();
  262. $list = $query->setOption('field', [])->field('uid,mer_id,create_time,type')->with(['user'])->page($page, $limit)->select();
  263. return compact('count', 'list');
  264. }
  265. /**
  266. * TODO 获取商户的聊天用户列表
  267. * @param $merId
  268. * @param $page
  269. * @param $limit
  270. * @return array
  271. * @author Qinii
  272. * @day 2020-06-19
  273. */
  274. public function getMerchantUserList($merId, $page, $limit)
  275. {
  276. $query = $this->dao->getMerchantUserList($merId);
  277. $count = $query->count();
  278. $list = $query->setOption('field', [])->field('uid,mer_id,create_time,type')->with(['user'])->page($page, $limit)->select();
  279. return compact('count', 'list');
  280. }
  281. /**
  282. * TODO
  283. * @param $merId
  284. * @param $uid
  285. * @param $page
  286. * @param $limit
  287. * @return array
  288. * @author Qinii
  289. * @day 2020-06-19
  290. */
  291. public function getUserMsn(int $uid, $page, $limit, ?int $merId = null, ?int $serviceId = null)
  292. {
  293. $where['uid'] = $uid;
  294. if ($merId) $where['mer_id'] = $merId;
  295. if ($serviceId) $where['service_id'] = $serviceId;
  296. $query = $this->search($where)->order('service_log_id DESC');
  297. $count = $query->count();
  298. $list = $query->page($page, $limit)->with(['user', 'service'])->select();
  299. return compact('count', 'list');
  300. }
  301. }