123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\common\repositories\store\service;
- use app\common\dao\store\service\StoreServiceLogDao;
- use app\common\model\store\service\StoreServiceLog;
- use app\common\repositories\BaseRepository;
- use app\common\repositories\store\order\StoreOrderRepository;
- use app\common\repositories\store\order\StoreRefundOrderRepository;
- use app\common\repositories\store\product\ProductGroupRepository;
- use app\common\repositories\store\product\ProductPresellRepository;
- use app\common\repositories\store\product\ProductRepository;
- use think\exception\ValidateException;
- use think\facade\Cache;
- use think\facade\Db;
- use think\model\Relation;
- /**
- * Class StoreServiceLogRepository
- * @package app\common\repositories\store\service
- * @author xaboy
- * @day 2020/5/29
- * @mixin StoreServiceLogDao
- */
- class StoreServiceLogRepository extends BaseRepository
- {
- /**
- * StoreServiceLogRepository constructor.
- * @param StoreServiceLogDao $dao
- */
- public function __construct(StoreServiceLogDao $dao)
- {
- $this->dao = $dao;
- }
- public function tidyLogList($list, $sendType)
- {
- foreach ($list as $k => $log) {
- $list[$k]['last'] = $this->dao->getLastLog($log['mer_id'], $log['uid']);
- $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;
- }
- return $list;
- }
- /**
- * @param $uid
- * @param $page
- * @param $limit
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author xaboy
- * @day 2020/6/16
- */
- public function userMerchantList($uid, $page, $limit)
- {
- $query = $this->dao->getMerchantListQuery($uid);
- $count = $query->count();
- $list = $query->setOption('field', [])->field('uid,mer_id,max(service_log_id) as service_log_id,type')->with(['merchant' => function ($query) {
- $query->field('mer_id,mer_avatar,mer_name');
- }])->page($page, $limit)->select()->toArray();
- $sort = array_column($list, 'service_log_id');
- array_multisort($sort, SORT_NUMERIC | SORT_DESC, $list);
- $list = $this->tidyLogList($list, 1);
- return compact('count', 'list');
- }
- /**
- * @param $uid
- * @param $page
- * @param $limit
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author xaboy
- * @day 2020/6/16
- */
- public function serviceUserList($uid, $page, $limit)
- {
- $service = app()->make(StoreServiceRepository::class)->getService($uid);
- if (!$service || !$service['status'])
- throw new ValidateException('没有权限');
- $query = $this->dao->getUserListQuery($service->service_id)->where('mer_id', $service->mer_id);
- $count = $query->count();
- $list = $query->setOption('field', [])->field('uid,mer_id,max(service_log_id) as service_log_id,type')->with(['user'])
- ->page($page, $limit)->select()->toArray();
- $sort = array_column($list, 'service_log_id');
- array_multisort($sort, SORT_NUMERIC | SORT_DESC, $list);
- $list = $this->tidyLogList($list, 0);
- return compact('count', 'list');
- }
- /**
- * @param $merId
- * @param $uid
- * @param $page
- * @param $limit
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author xaboy
- * @day 2020/6/15
- */
- public function userList($merId, $uid, $page, $limit)
- {
- $query = $this->search(['mer_id' => $merId, 'uid' => $uid])->order('service_log_id DESC');
- $count = $query->count();
- $list = $query->page($page, $limit)->with(['user', 'service'])->select();
- if ($page == 1) $this->dao->userRead($merId, $uid);
- $list = array_reverse($this->getSendDataList($list)->toArray());
- return compact('count', 'list');
- }
- /**
- * @param $merId
- * @param $toUid
- * @param $uid
- * @param $page
- * @param $limit
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author xaboy
- * @day 2020/6/15
- */
- public function merList($merId, $toUid, $uid, $page, $limit)
- {
- $service = app()->make(StoreServiceRepository::class)->getService($uid, $merId);
- if (!$service || !$service['status'])
- throw new ValidateException('没有权限');
- $query = $this->search(['mer_id' => $merId, 'uid' => $toUid])->order('service_log_id DESC');
- $count = $query->count();
- $list = $query->page($page, $limit)->with(['user', 'service'])->select();
- if ($page == 1) $this->dao->serviceRead($merId, $toUid, $service->service_id);
- $list = array_reverse($this->getSendDataList($list)->toArray());
- return compact('count', 'list');
- }
- /**
- * @param $merId
- * @param $uid
- * @param $type
- * @param $msn
- * @author xaboy
- * @day 2020/6/13
- */
- public function checkMsn($merId, $uid, $type, $msn)
- {
- if ($type == 4 && !app()->make(ProductRepository::class)->merExists($merId, $msn))
- throw new ValidateException('商品不存在');
- else if ($type == 5 && !app()->make(StoreOrderRepository::class)->existsWhere(['uid' => $uid, 'mer_id' => $merId, 'order_id' => $msn]))
- throw new ValidateException('订单不存在');
- else if ($type == 6 && !app()->make(StoreRefundOrderRepository::class)->existsWhere(['uid' => $uid, 'mer_id' => $merId, 'refund_order_id' => $msn]))
- throw new ValidateException('退款单不存在');
- else if ($type == 7 && !app()->make(ProductPresellRepository::class)->existsWhere(['product_presell_id' => $msn, 'mer_id' => $merId]))
- throw new ValidateException('商品不存在');
- else if ($type == 8 && !app()->make(ProductGroupRepository::class)->existsWhere(['product_group_id' => $msn, 'mer_id' => $merId]))
- throw new ValidateException('商品不存在');
- }
- /**
- * @param StoreServiceLog $log
- * @return StoreServiceLog
- * @author xaboy
- * @day 2020/6/15
- */
- public function getSendData(StoreServiceLog $log)
- {
- if ($log->msn_type == 4)
- $log->product;
- else if ($log->msn_type == 5)
- $log->orderInfo;
- else if ($log->msn_type == 6)
- $log->refundOrder;
- else if ($log->msn_type == 7)
- $log->presell;
- else if ($log->msn_type == 8)
- $log->productGroup;
- return $log;
- }
- public function getSendDataList($list)
- {
- $cache = [];
- foreach ($list as $log) {
- if (!in_array($log->msn_type, [4, 5, 6, 7,8])) continue;
- $key = $log->msn_type . $log->msn;
- if (isset($cache[$key])) {
- if ($log->msn_type == 4)
- $log->set('product', $cache[$key]);
- else if ($log->msn_type == 5)
- $log->set('orderInfo', $cache[$key]);
- else if ($log->msn_type == 6)
- $log->set('refundOrder', $cache[$key]);
- else if ($log->msn_type == 8)
- $log->set('productGroup', $cache[$key]);
- else
- $log->set('presell', $cache[$key]);
- } else {
- if ($log->msn_type == 4)
- $cache[$key] = $log->product;
- else if ($log->msn_type == 5)
- $cache[$key] = $log->orderInfo;
- else if ($log->msn_type == 6)
- $cache[$key] = $log->refundOrder;
- else if ($log->msn_type == 8)
- $cache[$key] = $log->productGroup;
- else
- $cache[$key] = $log->presell;
- }
- }
- return $list;
- }
- /**
- * @param $uid
- * @param $merId
- * @author xaboy
- * @day 2020/6/15
- */
- public function userToChat($uid, $merId)
- {
- Cache::set('u_chat' . $uid, $merId, 3600);
- }
- /**
- * @param $uid
- * @param $toUid
- * @author xaboy
- * @day 2020/6/15
- */
- public function serviceToChat($uid, $toUid)
- {
- Cache::set('s_chat' . $uid, $toUid, 3600);
- }
- /**
- * @param $uid
- * @param bool $isService
- * @author xaboy
- * @day 2020/6/15
- */
- public function getChat($uid, $isService = false)
- {
- $key = ($isService ? 's_chat' : 'u_chat') . $uid;
- return Cache::get($key);
- }
- /**
- * @param $uid
- * @param bool $isService
- * @author xaboy
- * @day 2020/6/15
- */
- public function unChat($uid, $isService = false)
- {
- $key = ($isService ? 's_chat' : 'u_chat') . $uid;
- Cache::delete($key);
- }
- /**
- * TODO 获取某个客服的用户列表
- * @param $service_id
- * @param $page
- * @param $limit
- * @return array
- * @author Qinii
- * @day 2020-06-18
- */
- public function getServiceUserList($service_id, $page, $limit)
- {
- $query = $this->dao->getUserListQuery($service_id);
- $count = $query->count();
- $list = $query->setOption('field', [])->field('uid,mer_id,create_time,type')->with(['user'])->page($page, $limit)->select();
- return compact('count', 'list');
- }
- /**
- * TODO 获取商户的聊天用户列表
- * @param $merId
- * @param $page
- * @param $limit
- * @return array
- * @author Qinii
- * @day 2020-06-19
- */
- public function getMerchantUserList($merId, $page, $limit)
- {
- $query = $this->dao->getMerchantUserList($merId);
- $count = $query->count();
- $list = $query->setOption('field', [])->field('uid,mer_id,create_time,type')->with(['user'])->page($page, $limit)->select();
- return compact('count', 'list');
- }
- /**
- * TODO
- * @param $merId
- * @param $uid
- * @param $page
- * @param $limit
- * @return array
- * @author Qinii
- * @day 2020-06-19
- */
- public function getUserMsn(int $uid, $page, $limit, ?int $merId = null, ?int $serviceId = null)
- {
- $where['uid'] = $uid;
- if ($merId) $where['mer_id'] = $merId;
- if ($serviceId) $where['service_id'] = $serviceId;
- $query = $this->search($where)->order('service_log_id DESC');
- $count = $query->count();
- $list = $query->page($page, $limit)->with(['user', 'service'])->select();
- return compact('count', 'list');
- }
- }
|