企微短剧业务系统

SourceQrcodeService.php 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. <?php
  2. namespace App\Service;
  3. use App\Models\AuthorizeCorp;
  4. use App\Models\SourceQrcodeDailyStatistics;
  5. use App\Models\SourceQrcodeGroups;
  6. use App\Models\SourceQrcodes;
  7. use App\Models\SourceQrcodeUsers;
  8. use App\Models\SourceQrcodeWelcomeMsg;
  9. use App\Models\CustomerDetails;
  10. use App\Models\Customer;
  11. use App\Models\DjUser;
  12. use App\RedisModel;
  13. use App\Log;
  14. use App\Models\System\Users;
  15. use App\Support\qyApi\QyCommon;
  16. use Illuminate\Support\Facades\DB;
  17. class SourceQrcodeService
  18. {
  19. /**
  20. * 添加活码分组
  21. */
  22. public static function addSqgroup($corpid, $name, $type)
  23. {
  24. $group = new SourceQrcodeGroups;
  25. $group->corpid = $corpid;
  26. $group->name = $name;
  27. $group->type = $type;
  28. $group->save();
  29. $group->sort_val = $group->id;
  30. $result = $group->save();
  31. return $result ? 0 : 4609;
  32. }
  33. /**
  34. * 修改活码分组
  35. */
  36. public static function editSqgroup($id, $name)
  37. {
  38. $group = SourceQrcodeGroups::where('id', $id)->first();
  39. if(empty($group)){
  40. return 4607;
  41. }
  42. $group->name = $name;
  43. $group->save();
  44. return 0;
  45. }
  46. /**
  47. * @param site: 1上移 2下移 3置顶 4置底
  48. */
  49. public static function editGroupSortOld($id, $site)
  50. {
  51. $group = SourceQrcodeGroups::where('id', $id)->first();
  52. $group_sort_val = $group->sort_val;
  53. if( $site==1 ){
  54. //上移 查上一个交换位置
  55. $target = SourceQrcodeGroups::where('sort_val', '<', $group_sort_val)->where('enable', 1)->orderBy('sort_val', 'desc')->first();
  56. if( empty($target) ){
  57. //已经是最高,不需操作
  58. return true;
  59. }
  60. $target_sort_val = $target->sort_val;
  61. $group->sort_val = $target_sort_val;
  62. $target->sort_val = $group_sort_val;
  63. $group->save();
  64. $target->save();
  65. } elseif( $site==2 ){
  66. //下移
  67. $target = SourceQrcodeGroups::where('sort_val', '>', $group_sort_val)->where('enable', 1)->orderBy('sort_val', 'asc')->first();
  68. if( empty($target) ){
  69. //已经是最低,不需操作
  70. return true;
  71. }
  72. $target_sort_val = $target->sort_val;
  73. $group->sort_val = $target_sort_val;
  74. $target->sort_val = $group_sort_val;
  75. $group->save();
  76. $target->save();
  77. } elseif( $site==3 ){
  78. //置顶
  79. $target = SourceQrcodeGroups::where('enable', 1)->orderBy('sort_val', 'asc')->first();
  80. $sort_val = $target->sort_val - 1;
  81. $group->sort_val = $sort_val;
  82. $group->save();
  83. } elseif( $site==4 ){
  84. //置底
  85. $target = SourceQrcodeGroups::where('enable', 1)->orderBy('sort_val', 'desc')->first();
  86. $sort_val = $target->sort_val + 1;
  87. $group->sort_val = $sort_val;
  88. $group->save();
  89. }
  90. return true;
  91. }
  92. public static function editGroupSort($ids)
  93. {
  94. try {
  95. foreach($ids as $k=>$v){
  96. SourceQrcodeGroups::where('id', $v)->update([
  97. 'sort_val' => $k
  98. ]);
  99. }
  100. } catch (\Exception $e) {
  101. return 4610;
  102. }
  103. return 0;
  104. }
  105. public static function delSqGroup($id, $del_sqs = 0)
  106. {
  107. DB::beginTransaction();
  108. try{
  109. $res = SourceQrcodeGroups::where('id', $id)->where('enable', 1)->update(['enable' => 0]);
  110. if($res && $del_sqs){
  111. $sqs = SourceQrcodes::where('group_id', $id)->where('enable', 1)->get();
  112. if( !$sqs->isEmpty() ){
  113. //删除数据
  114. SourceQrcodes::where('group_id', $id)->where('enable', 1)->update(['enable' => 0]);
  115. //企微删除接口
  116. foreach($sqs as $item){
  117. self::delSourceQrcode($item->$id);
  118. }
  119. }
  120. }
  121. DB::commit();
  122. } catch (\Exception $e) {
  123. DB::rollBack();
  124. Log::logError('删除渠道活码分组-过程发生异常', [
  125. 'line' => $e->getLine(),
  126. 'msg' => $e->getMessage(),
  127. 'data' => ['id'=>$id, 'del_sqs'=>$del_sqs]
  128. ], 'sourceQrcodes-Exception');
  129. return 4602;
  130. }
  131. return 0;
  132. }
  133. public static function sqGroupList($corpid, $type, $page, $pagesize)
  134. {
  135. $offset = ($page-1) * $pagesize;
  136. $listQuery = SourceQrcodeGroups::where('enable', 1)
  137. ->where('type', $type)
  138. ->where('corpid', $corpid);
  139. $total = (clone $listQuery)->count();
  140. $list = $listQuery->select(['id', 'name', 'corpid', 'sort_val', 'enable'])->orderBy('sort_val')
  141. ->offset($offset)
  142. ->limit($pagesize)
  143. ->get();
  144. return [$list, $total];
  145. }
  146. public static function addSourceQrcode($corpid, $name, $group_id, $user_json, $tag_list, $customer_prefix, $customer_prefix_type, $up_toplimit, $warn_user, $msgData)
  147. {
  148. if( !self::verifyUserJson($user_json) ){
  149. return [4600, '客服数据格式有误'];
  150. }
  151. # 验证欢迎语内容
  152. if(!is_array($msgData)) $msgData = json_decode($msgData, true);
  153. $errno = WelcomeMsgService::msgDataVerify($msgData);
  154. if($errno) return [$errno, '欢迎语格式有误'];
  155. $staffs = array();
  156. $user_info = json_decode($user_json, true);
  157. foreach($user_info as $staff){
  158. if( $staff['is_open'] ){
  159. $staffs[] = $staff['user_id']; //开启的客服
  160. }
  161. }
  162. #渠道活码请求
  163. $state = uniqid('sq-') . mt_rand(1000, 9999); //生成唯一state
  164. $params = [
  165. "type" => 2, //联系方式类型,1-单人, 2-多人
  166. "scene" => 2, //场景,1-在小程序中联系,2-通过二维码联系
  167. "remark" => "渠道活码", //联系方式的备注信息
  168. "skip_verify" => true, //外部客户添加时是否无需验证,默认为true
  169. "state" => $state, //企业自定义的state参数,用于区分不同的添加渠道,不超过30字符
  170. "user" => $staffs //客服列表
  171. ];
  172. DB::beginTransaction();
  173. try{
  174. $result = QyCommon::momentCommon('add_contact_way', $corpid, $params);
  175. if( !$result ){
  176. return [4601, '生成渠道二维码失败'];
  177. }
  178. $sqParams = array();
  179. $sqParams['corpid'] = $corpid;
  180. $sqParams['name'] = $name;
  181. $sqParams['group_id'] = $group_id;
  182. $sqParams['state'] = $state;
  183. $sqParams['tag_list'] = $tag_list;
  184. $sqParams['customer_prefix'] = $customer_prefix;
  185. $sqParams['customer_prefix_type'] = $customer_prefix_type;
  186. $sqParams['up_toplimit'] = (int)$up_toplimit;
  187. $sqParams['warn_user'] = $warn_user;
  188. $sqParams['user_id'] = \Auth::id();
  189. $sqParams['config_id'] = $result['config_id'];
  190. $sqParams['qrcode'] = $result['qr_code'];
  191. $sqModel = new SourceQrcodes($sqParams);
  192. if(!$sqModel->save()){
  193. DB::rollBack();
  194. return [4602, '系统错误'];
  195. }
  196. $sq_id = $sqModel->id;
  197. $sqUsers = array();
  198. foreach($user_info as $staff){
  199. $sqUsers[] = [
  200. 'corpid' => $corpid,
  201. 'sq_id' => $sq_id,
  202. 'user_id' => $staff['user_id'],
  203. 'is_open' => $staff['is_open'],
  204. 'toplimit' => $staff['toplimit']
  205. ];
  206. }
  207. $ures = SourceQrcodeUsers::insert($sqUsers);
  208. if(!$ures){
  209. DB::rollBack();
  210. return [4602, '系统错误'];
  211. }
  212. $sres = self::setWelcomeMsg($msgData, $corpid, $sq_id);
  213. if(!$sres){
  214. DB::rollBack();
  215. return [4602, '系统错误'];
  216. }
  217. DB::commit();
  218. } catch (\Exception $e) {
  219. Log::logError('创建渠道活码-过程发生异常', [
  220. 'line' => $e->getLine(),
  221. 'msg' => $e->getMessage(),
  222. 'data' => $msgData
  223. ], 'sourceQrcodes-Exception');
  224. return [4602, '系统错误'];
  225. }
  226. return [0, '添加成功'];
  227. }
  228. private static function verifyUserJson($user_json)
  229. {
  230. $user_info = json_decode($user_json, true);
  231. if( !is_array($user_info) ){
  232. return false;
  233. }
  234. $is_open = 0;
  235. foreach($user_info as $user){
  236. if( !isset($user['user_id']) || !isset($user['is_open']) || !isset($user['toplimit']) || $user['toplimit']<0 ){
  237. return false;
  238. }
  239. if($user['is_open']) $is_open = 1;
  240. }
  241. if($is_open == 0){
  242. return false;
  243. }
  244. return true;
  245. }
  246. public static function setWelcomeMsg($msgData, $corpid, $sq_id)
  247. {
  248. # 设置时间规则及消息内容到消息表
  249. foreach ($msgData as $msgRule) {
  250. $msgId = isset($msgRule['msg_id']) ? $msgRule['msg_id'] : 0;
  251. $weeks = $msgRule['weeks'];
  252. $isDayParting = $msgRule['is_day_parting'];
  253. $startTime = $msgRule['start_time'];
  254. $endTime = $msgRule['end_time'];
  255. $content = $msgRule['content'];
  256. $attachments = $msgRule['attachments'];
  257. $operate = isset($msgRule['operate']) ? $msgRule['operate'] : '';
  258. $key_val = isset($msgRule['key_val']) ? $msgRule['key_val'] : 0;
  259. if($msgId && $operate=='del') { // 删除子规则操作
  260. $result = SourceQrcodeWelcomeMsg::where('id', $msgId)->update(['enable' => 0]);
  261. if(!$result) {
  262. return false;
  263. }
  264. continue;
  265. }
  266. $result = SourceQrcodeWelcomeMsg::setMsg(
  267. $msgId, $weeks, $startTime, $endTime, $content, $attachments, $isDayParting, $corpid, $sq_id, $key_val
  268. );
  269. if(!$result) {
  270. return false;
  271. }
  272. }
  273. return true;
  274. }
  275. public static function editSourceQrcode($id, $name, $group_id, $user_json, $tag_list, $customer_prefix, $customer_prefix_type, $up_toplimit, $warn_user, $msgData)
  276. {
  277. $sqInfo = SourceQrcodes::where('id', $id)->first();
  278. if(!isset($sqInfo->id)){
  279. return [4603, '参数有误'];
  280. }
  281. if( !self::verifyUserJson($user_json) ){
  282. return [4600, '客服数据格式有误'];
  283. }
  284. $corpid = $sqInfo->corpid;
  285. # 验证欢迎语内容
  286. if(!is_array($msgData)) $msgData = json_decode($msgData, true);
  287. $errno = WelcomeMsgService::msgDataVerify($msgData);
  288. if($errno) return [$errno, '欢迎语格式有误'];
  289. $staffs = array();
  290. $user_info = json_decode($user_json, true);
  291. foreach($user_info as $staff){
  292. if( $staff['is_open'] ){
  293. $staffs[] = $staff['user_id']; //开启的客服
  294. }
  295. }
  296. DB::beginTransaction();
  297. try{
  298. #原有所有配置客服信息
  299. $old_all = SourceQrcodeUsers::where('sq_id', $sqInfo->id)->get()->keyBy('user_id')->toArray();
  300. $old_enable_staffs = array(); //原有未删除客服,用来判断本次删除了哪些
  301. //$old_up_staffs = array(); //原有企微配置客服
  302. foreach($old_all as $val){
  303. if($val['enable'] == 1){
  304. $old_enable_staffs[] = $val['user_id'];
  305. }
  306. /*if($val['enable'] == 1 && $val['is_open'] == 1 && $val['is_shut'] == 0){
  307. $old_up_staffs[] = $val['user_id'];
  308. }*/
  309. }
  310. #判断是否需要更新企微接口
  311. $final_up_staffs = array(); //获取最新二维码配置客服
  312. foreach($user_info as $staff){
  313. $staff_id = $staff['user_id'];
  314. if($staff['is_open'] == 0){
  315. continue; //未开启直接跳过,若最终qw客服为空,判断操作有误
  316. }
  317. #判断数据表是否已有
  318. if( isset($old_all[$staff_id]) ){
  319. #未设上限,直接开启
  320. if($staff['toplimit'] == 0){
  321. $final_up_staffs[] = $staff_id;
  322. } else {
  323. $user_add_count = CustomerDetails::suffix($corpid)
  324. ->where('corpid', $corpid)
  325. ->where('state', $sqInfo->state)
  326. ->where('user_id', $staff_id)
  327. ->where('scan_time', '>', date('Y-m-d'))
  328. ->count(); //已添加客户数
  329. if($user_add_count < $staff['toplimit']){
  330. //未达新限
  331. $final_up_staffs[] = $staff_id;
  332. }
  333. }
  334. } else {
  335. //全新 qw客服
  336. $final_up_staffs[] = $staff_id;
  337. }
  338. }
  339. if( empty($final_up_staffs)){
  340. return [4608, '没有配置符合条件的客服'];
  341. }
  342. //更新渠道二维码
  343. $params = [
  344. "config_id" => $sqInfo->config_id, //企业联系方式的配置id
  345. "user" => $final_up_staffs //客服列表
  346. ];
  347. $result = QyCommon::momentCommon('update_contact_way', $corpid, $params);
  348. if( !$result ){
  349. return [4604, '更新渠道二维码失败'];
  350. }
  351. $sqParams = array();
  352. $sqParams['name'] = $name;
  353. $sqParams['group_id'] = $group_id;
  354. $sqParams['tag_list'] = $tag_list;
  355. $sqParams['customer_prefix'] = $customer_prefix;
  356. $sqParams['customer_prefix_type'] = $customer_prefix_type;
  357. $sqParams['up_toplimit'] = (int)$up_toplimit;
  358. $sqParams['warn_user'] = $warn_user;
  359. $upRes = SourceQrcodes::where('id', $id)->update($sqParams);
  360. $sq_id = $sqInfo->id;
  361. $final_users = array(); //最终保存入库客服
  362. foreach($user_info as $staff){
  363. $con = [
  364. 'corpid' => $corpid,
  365. 'sq_id' => $sq_id,
  366. 'user_id' => $staff['user_id']
  367. ];
  368. $attr = [
  369. 'is_open' => $staff['is_open'],
  370. 'toplimit' => (int)$staff['toplimit'],
  371. 'enable' => 1
  372. ];
  373. if(in_array($staff['user_id'], $final_up_staffs)){
  374. $attr['is_shut'] = 0;
  375. }
  376. SourceQrcodeUsers::updateOrCreate($con, $attr);
  377. $final_users[] = $staff['user_id'];
  378. }
  379. #需要删除的客服
  380. $del_staffs = array_diff($old_enable_staffs, $final_users);
  381. SourceQrcodeUsers::whereIn('user_id', $del_staffs)->where('sq_id', $sq_id)->update(['enable' => 0]);
  382. $sres = self::setWelcomeMsg($msgData, $corpid, $sq_id);
  383. if(!$sres){
  384. DB::rollBack();
  385. return [4602, '系统错误'];
  386. }
  387. DB::commit();
  388. } catch (\Exception $e) {
  389. Log::logError('编辑渠道活码-过程发生异常', [
  390. 'line' => $e->getLine(),
  391. 'msg' => $e->getMessage(),
  392. 'data' => $msgData
  393. ], 'sourceQrcodes-Exception');
  394. return [4602, '系统错误'];
  395. }
  396. return [0, '修改成功'];
  397. }
  398. public static function sourceQrcodeList($corpid, $group_id, $name, $user_ids, $page, $pagesize)
  399. {
  400. #筛选客服
  401. $sq_ids = array();
  402. if(!empty($user_ids)){
  403. $sq_ids = SourceQrcodeUsers::where('corpid', $corpid)->whereIn('user_id', $user_ids)->where('enable', 1)->pluck('sq_id')->all();
  404. if( empty($sq_ids) ){
  405. return [[], 0];
  406. }
  407. }
  408. $listQuery = SourceQrcodes::where('enable', 1)
  409. ->where('corpid', $corpid)
  410. ->where(function($query) use($group_id, $name, $sq_ids){
  411. if($group_id) $query->where('group_id', $group_id);
  412. if($name) $query->where('name', 'like', '%'.$name.'%');
  413. if(!empty($sq_ids)) $query->whereIn('id', $sq_ids);
  414. });
  415. $total = (clone $listQuery)->count();
  416. if(!$total){
  417. return [[], 0];
  418. }
  419. $offset = ($page-1) * $pagesize;
  420. $list = $listQuery->orderBy('id', 'desc')
  421. ->offset($offset)
  422. ->limit($pagesize)
  423. ->get();
  424. $create_user_ids = $list->pluck('user_id')->all();
  425. $create_users = Users::whereIn('id', $create_user_ids)->pluck('name', 'id')->all();
  426. $group_ids = $list->pluck('group_id')->all();
  427. $groups = SourceQrcodeGroups::whereIn('id', $group_ids)->pluck('name', 'id')->all();
  428. foreach($list as $item){
  429. #创建人
  430. $item->creater = $create_users[$item->user_id] ?? '';
  431. #分组名称
  432. $item->group_name = $groups[$item->group_id] ?? '';
  433. #扫码总人数
  434. $item->user_add_count = CustomerDetails::suffix($corpid)
  435. ->where('corpid', $corpid)
  436. ->where('state', $item->state)
  437. ->count();
  438. }
  439. return [$list, $total];
  440. }
  441. //活码详情
  442. public static function sourceQrcodeDetail($id)
  443. {
  444. $sqInfo = SourceQrcodes::find($id);
  445. #客服信息
  446. $staffs = SourceQrcodeUsers::where('sq_id', $id)->where('enable', 1)->get();
  447. $user_ids = $staffs->pluck('user_id')->all();
  448. $user_names = DjUser::whereIn('user_id', $user_ids)->where('corpid', $sqInfo->corpid)->pluck('name', 'user_id')->all();
  449. foreach($staffs as $k=>$item){
  450. #客服名称
  451. $item->staff_name = $user_names[$item->user_id] ?? '';
  452. #今日添加客户数
  453. $item->user_add_count = CustomerDetails::suffix($sqInfo->corpid)
  454. ->where('corpid', $sqInfo->corpid)
  455. ->where('state', $sqInfo->state)
  456. ->where('user_id', $item->user_id)
  457. ->where('scan_time', '>', date('Y-m-d'))
  458. ->count();
  459. }
  460. #欢迎语信息
  461. $welcomeMsg = SourceQrcodeWelcomeMsg::where('sq_id', $id)->where('enable', 1)->orderBy('is_day_parting')->get();
  462. foreach ($welcomeMsg as $item) {
  463. $item->msg_id = $item->id;
  464. # 附件信息处理
  465. $attachments = json_decode($item->attachments, true);
  466. if(!empty($attachments)) {
  467. foreach ($attachments as $key=>&$attachment) {
  468. if(isset($attachment['msgtype']) && $attachment['msgtype'] == 'radar') { // 雷达附件信息回显
  469. $radarId = $attachment['radar']['radar_id'] ?? 0;
  470. $radarInfo = RadarService::getRadarContent($sqInfo->corpid, $radarId);
  471. if(empty($radarInfo)) {
  472. unset($attachment[$key]);
  473. continue;
  474. }
  475. $attachment['radar'] = $radarInfo;
  476. }
  477. }
  478. }
  479. $item->attachments = json_encode($attachments, 256);
  480. }
  481. $sqInfo->staff_users = $staffs;
  482. $sqInfo->welcomeMsg = $welcomeMsg;
  483. return $sqInfo;
  484. }
  485. //禁用渠道活码
  486. public static function delSourceQrcode($id)
  487. {
  488. DB::beginTransaction();
  489. try{
  490. $sqInfo = SourceQrcodes::find($id);
  491. $corpid = $sqInfo->corpid;
  492. $sqInfo->del_time = date('Y-m-d H:i:s');
  493. $sqInfo->status = 0;
  494. $sqInfo->save();
  495. $config_id = $sqInfo->config_id;
  496. # 企微删除接口
  497. $params = [
  498. "config_id" => $sqInfo->config_id, //企业联系方式的配置id
  499. ];
  500. $result = QyCommon::momentCommon('del_contact_way', $corpid, $params);
  501. if( !$result ){
  502. DB::rollBack();
  503. return [4605, '删除渠道二维码失败'];
  504. }
  505. DB::commit();
  506. } catch (\Exception $e) {
  507. DB::rollBack();
  508. Log::logError('删除渠道活码-过程发生异常', [
  509. 'line' => $e->getLine(),
  510. 'msg' => $e->getMessage(),
  511. 'data' => $id
  512. ], 'sourceQrcodes-Exception');
  513. return [4602, '系统错误'];
  514. }
  515. return [0, '禁用成功'];
  516. }
  517. public static function dataView($id)
  518. {
  519. $sqInfo = SourceQrcodes::find($id);
  520. $tTime = date('Y-m-d 00:00:00', time());
  521. $corpid = $sqInfo->corpid;
  522. $state = $sqInfo->state;
  523. $cusDetailTalInfo = CustomerDetails::suffix($corpid)
  524. ->selectRaw("COUNT(id) AS scan_num_total")
  525. ->selectRaw("SUM(IF(enable = 1, 1, 0)) AS keep_num_total")
  526. ->selectRaw("SUM(IF(scan_user_type = 2, 1, 0)) AS incr_num_total")
  527. ->where('state', $state)
  528. ->first();
  529. $total = [
  530. 'scan_num_total' => $cusDetailTalInfo->scan_num_total ?? 0,
  531. 'keep_num_total' => $cusDetailTalInfo->keep_num_total ?? 0,
  532. 'incr_num_total' => $cusDetailTalInfo->incr_num_total ?? 0,
  533. ];
  534. $total['loss_num_total'] = $total['scan_num_total'] - $total['keep_num_total'];
  535. $cusDetailTdInfo = CustomerDetails::suffix($corpid)
  536. ->selectRaw("COUNT(id) AS scan_num_td")
  537. ->selectRaw("SUM(IF(enable = 1, 1, 0)) AS keep_num_td")
  538. ->selectRaw("SUM(IF(scan_user_type = 2, 1, 0)) AS incr_num_td")
  539. ->where('state', $state)
  540. ->where('scan_time', '>=', $tTime)
  541. ->first();
  542. $today = [
  543. 'scan_num_td' => $cusDetailTdInfo->scan_num_td ?? 0,
  544. 'keep_num_td' => $cusDetailTdInfo->keep_num_td ?? 0,
  545. 'incr_num_td' => $cusDetailTdInfo->incr_num_td ?? 0,
  546. ];
  547. $today['loss_num_td'] = $today['scan_num_td'] - $today['keep_num_td'];
  548. return [
  549. 'tal' => $total,
  550. 'td' => $today
  551. ];
  552. }
  553. public static function dataTrend($id, $stDate, $enDate)
  554. {
  555. $list = self::getTrendFormatData($stDate, $enDate);
  556. $statisticList = SourceQrcodeDailyStatistics::query()
  557. ->select(['date'])
  558. ->selectRaw("SUM(scan_num) AS scan_num")
  559. ->selectRaw("SUM(keep_num) AS keep_num")
  560. ->where('sq_id', $id)
  561. ->whereBetween('date', [$stDate, $enDate])
  562. ->groupBy('date')
  563. ->get();
  564. if ($statisticList->isNotEmpty()) {
  565. foreach ($statisticList as $item) {
  566. $list[$item->date]['scan_num'] = intval($item->scan_num);
  567. $list[$item->date]['keep_num'] = intval($item->keep_num);
  568. }
  569. }
  570. return array_values($list);
  571. }
  572. protected static function getTrendFormatData($stDate, $enDate)
  573. {
  574. $dayArr = [];
  575. $stDateStr = strtotime($stDate);
  576. $enDateStr = strtotime($enDate);
  577. while ($stDateStr <= $enDateStr) {
  578. $date = date('Y-m-d', $stDateStr);
  579. $dayArr[$date] = [
  580. 'date' => $date,
  581. 'scan_num' => 0,
  582. 'keep_num' => 0
  583. ];
  584. $stDateStr += 86400;
  585. }
  586. return $dayArr;
  587. }
  588. public static function customerList($sq_id, $name, $page, $pagesize)
  589. {
  590. $sqInfo = SourceQrcodes::find($sq_id);
  591. $corpid = $sqInfo->corpid;
  592. $state = $sqInfo->state;
  593. $customerQuery = CustomerDetails::suffix($corpid)
  594. ->where('corpid', $corpid)
  595. ->where('state', $state);
  596. $total = (clone $customerQuery)->count();
  597. if($total == 0){
  598. return [[], 0];
  599. }
  600. $offset = ($page-1) * $pagesize;
  601. $list = $customerQuery->where(function($query) use($name){
  602. if($name) $query->where('name', 'like', '%'. $name .'%');
  603. })
  604. ->select('id', 'name', 'external_userid', 'remark', 'scan_time', 'scan_user_type', 'enable', 'user_id')
  605. ->orderBy('id', 'desc')
  606. ->offset($offset)
  607. ->limit($pagesize)
  608. ->get();
  609. //补充客服名称
  610. $user_ids = $list->pluck('user_id')->all();
  611. $user_names = DjUser::where('corpid', $corpid)->whereIn('user_id', $user_ids)->pluck('name', 'user_id')->all();
  612. //客户头像
  613. $external_userids = $list->pluck('external_userid')->all();
  614. $customers = Customer::suffix($corpid)
  615. ->where('corpid', $corpid)
  616. ->whereIn('external_userid', $external_userids)
  617. ->pluck('avatar', 'external_userid')
  618. ->all();
  619. foreach($list as $item){
  620. $item->user_name = $user_names[$item->user_id] ?? '';
  621. $item->avatar = $customers[$item->external_userid] ?? '';
  622. }
  623. return [$list, $total];
  624. }
  625. public static function staffList($sq_id)
  626. {
  627. $sqInfo = SourceQrcodes::find($sq_id);
  628. if(!isset($sqInfo->id)){
  629. return null;
  630. }
  631. $corpid = $sqInfo->corpid;
  632. $state = $sqInfo->state;
  633. $list = SourceQrcodeUsers::where('enable', 1)
  634. ->where('sq_id', $sq_id)
  635. ->get();
  636. if($list->isEmpty()){
  637. return null;
  638. }
  639. $user_ids = $list->pluck('user_id')->all();
  640. $user_names = DjUser::where('corpid', $corpid)->whereIn('user_id', $user_ids)->pluck('name', 'user_id')->all();
  641. foreach($list as $item){
  642. #客服名称
  643. $item->user_name = $user_names[$item->user_id] ?? '';
  644. #添加客户数
  645. $item->user_add_total = CustomerDetails::suffix($corpid)
  646. ->where('corpid', $corpid)
  647. ->where('state', $state)
  648. ->where('user_id', $item->user_id)
  649. ->count();
  650. #今日添加客户数
  651. $item->user_add_count = CustomerDetails::suffix($corpid)
  652. ->where('corpid', $corpid)
  653. ->where('state', $state)
  654. ->where('user_id', $item->user_id)
  655. ->where('scan_time', '>', date('Y-m-d'))
  656. ->count();
  657. }
  658. return $list;
  659. }
  660. }