StoreServiceLogRepository.php 11 KB

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