企微短剧业务系统

PortraitService.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\CustomerDetails;
  5. use App\Models\Portrait;
  6. use App\Models\PortraitConf;
  7. use App\Models\Customer;
  8. use App\Models\Tag;
  9. use App\Models\DjUser;
  10. class PortraitService
  11. {
  12. public static function confList($corpid)
  13. {
  14. $portraitConfData = PortraitConf::query()
  15. ->select(['fid', 'field', 'name', 'status', 'enable'])
  16. ->where('corpid', $corpid)
  17. ->orderBy('sort', 'ASC')
  18. ->get();
  19. $result = [];
  20. if ($portraitConfData->isEmpty()) {
  21. $result = Portrait::query()
  22. ->select(['fid', 'field', 'name', 'status', 'enable'])
  23. ->where('enable', 1)
  24. ->orderBy('sort', 'ASC')
  25. ->get()
  26. ->toArray();
  27. } else {
  28. foreach ($portraitConfData as $portraitConf) {
  29. if ($portraitConf->enable == 0) continue;
  30. $result[] = $portraitConf->toArray();
  31. }
  32. }
  33. return $result;
  34. }
  35. public static function confOperate($corpid, $confJson)
  36. {
  37. // 检测是否是json
  38. $confArr = json_decode($confJson, true);
  39. if (!is_array($confArr)) return [false, 4701];
  40. $isExist = PortraitConf::query()->where('corpid', $corpid)->exists();
  41. if (!$isExist) {
  42. $portraitData = Portrait::query()
  43. ->where('enable', 1)
  44. ->orderBy('id', 'ASC')
  45. ->get()
  46. ->keyBy('fid');
  47. } else {
  48. $portraitData = PortraitConf::query()
  49. ->where('corpid', $corpid)
  50. ->where('enable', 1)
  51. ->orderBy('sort', 'ASC')
  52. ->get()
  53. ->keyBy('fid');
  54. if (count($confArr) != $portraitData->count()) return [false, 4701];
  55. }
  56. foreach ($confArr as $sort => $conf) {
  57. $portraitInfo = $portraitData->get($conf['fid']);
  58. if (
  59. empty($portraitInfo) ||
  60. $portraitInfo->enable == 0 ||
  61. ($portraitInfo->name != $conf['name'])
  62. ) return [false, 4702];
  63. $portraitInfo->sort = $sort;
  64. $portraitInfo->status = $conf['status'];
  65. }
  66. \DB::begintransaction();
  67. try {
  68. self::intoConf($corpid, $portraitData);
  69. \DB::commit();
  70. return [true, 0];
  71. } catch (\Throwable $e) {
  72. Log::logError(
  73. '画像配置操作有误~'.$e->getMessage().PHP_EOL.$e->getTraceAsString(),
  74. [
  75. 'corpid' => $corpid,
  76. 'confJson' => $confJson,
  77. ],
  78. 'PortraitService'
  79. );
  80. \DB::rollBack();
  81. return [false, 3106];
  82. }
  83. }
  84. public static function confAdd($corpid, $name)
  85. {
  86. \DB::begintransaction();
  87. try {
  88. $isExist = PortraitConf::query()->where('corpid', $corpid)->exists();
  89. if (!$isExist) {
  90. $portraitData = Portrait::query()
  91. ->where('enable', 1)
  92. ->orderBy('id', 'ASC')
  93. ->get();
  94. self::intoConf($corpid, $portraitData);
  95. }
  96. $fid = self::getFieldId($name);
  97. $PortraitConfDetail = PortraitConf::query()
  98. ->where('corpid', $corpid)
  99. ->where('fid', $fid)
  100. ->where('name', $name)
  101. ->where('enable', 1)
  102. ->first();
  103. if (!empty($PortraitConfDetail)) return [false, 4706];
  104. $sort = PortraitConf::query()->where('corpid', $corpid)->max('sort');
  105. PortraitConf::query()->updateOrCreate([
  106. 'corpid' => $corpid,
  107. 'fid' => $fid,
  108. 'name' => $name
  109. ], [
  110. 'sort' => $sort + 1,
  111. 'status' => 0,
  112. 'enable' => 1
  113. ]);
  114. \DB::commit();
  115. return [true, 0];
  116. } catch (\Throwable $e) {
  117. Log::logError(
  118. '画像属性添加操作有误~'.$e->getMessage().PHP_EOL.$e->getTraceAsString(),
  119. [
  120. 'corpid' => $corpid,
  121. 'name' => $name,
  122. ],
  123. 'PortraitService'
  124. );
  125. \DB::rollBack();
  126. return [false, 3106];
  127. }
  128. }
  129. public static function confEdit($corpid, $fid, $name)
  130. {
  131. \DB::begintransaction();
  132. try {
  133. $isExist = PortraitConf::query()->where('corpid', $corpid)->exists();
  134. if (!$isExist) {
  135. $portraitData = Portrait::query()
  136. ->where('enable', 1)
  137. ->orderBy('id', 'ASC')
  138. ->get();
  139. self::intoConf($corpid, $portraitData);
  140. }
  141. $portraitConfInfo = PortraitConf::query()
  142. ->where('corpid', $corpid)
  143. ->where('fid', $fid)
  144. ->where('enable', 1)
  145. ->first();
  146. if (empty($portraitConfInfo)) return [false, 4707];
  147. $fid = self::getFieldId($name);
  148. $portraitConfInfo->update([
  149. 'fid' => $fid,
  150. 'name' => $name
  151. ]);
  152. \DB::commit();
  153. return [true, 0];
  154. } catch (\Throwable $e) {
  155. Log::logError(
  156. '画像属性编辑操作有误~'.$e->getMessage().PHP_EOL.$e->getTraceAsString(),
  157. [
  158. 'corpid' => $corpid,
  159. 'fid' => $fid,
  160. 'name' => $name,
  161. ],
  162. 'PortraitService'
  163. );
  164. \DB::rollBack();
  165. return [false, 3106];
  166. }
  167. }
  168. public static function edit($corpid, $userId, $externalUserid, $attrsJson)
  169. {
  170. $customerDetailInfo = CustomerDetails::suffix($corpid)
  171. ->where('corpid', $corpid)
  172. ->where('user_id', $userId)
  173. ->where('external_userid', $externalUserid)
  174. ->first();
  175. if (empty($customerDetailInfo)) return [false, 4703];
  176. // 检测json是否合法
  177. $attribute = json_decode($customerDetailInfo->attribute, true);
  178. if (!is_array($attribute)) return [false, 4704];
  179. $attrs = json_decode($attrsJson, true);
  180. if (!is_array($attrs)) return [false, 4701];
  181. // 检测conf属性是否存在
  182. $portraitData = PortraitConf::query()
  183. ->where('corpid', $corpid)
  184. ->where('enable', 1)
  185. ->pluck('name', 'fid');
  186. if ($portraitData->isEmpty()) $portraitData = Portrait::query()
  187. ->where('enable', 1)
  188. ->pluck('name', 'fid');
  189. // 检测detail数据
  190. foreach ($attribute as $key => &$item) {
  191. // 属性字段被修改过
  192. if (is_null($portraitData->get($item['fid']))) {
  193. unset($attribute[$key]);
  194. continue;
  195. }
  196. // 属性字段不匹配
  197. if (!isset($attrs[$item['fid']])) continue;
  198. $item['val'] = $attrs[$item['fid']];
  199. unset($attrs[$item['fid']]);
  200. }
  201. if (!empty($attrs)) {
  202. // 检测传递的属性字段参数是否存在
  203. foreach ($attrs as $fid => $val) {
  204. if (is_null($portraitData->get($fid))) return [false, 4705];
  205. $attribute[] = [
  206. 'fid' => $fid,
  207. 'val' => $val
  208. ];
  209. }
  210. }
  211. \DB::begintransaction();
  212. try {
  213. $customerDetailInfo->update(['attribute' => json_encode(array_values($attribute))]);
  214. \DB::commit();
  215. return [true, 0];
  216. } catch (\Throwable $e) {
  217. Log::logError(
  218. '客户添加属性操作有误~'.$e->getMessage().PHP_EOL.$e->getTraceAsString(),
  219. [
  220. 'corpid' => $corpid,
  221. 'userId' => $userId,
  222. 'externalUserid' => $externalUserid,
  223. 'fid' => $fid,
  224. 'val' => $val,
  225. ],
  226. 'PortraitService'
  227. );
  228. \DB::rollBack();
  229. return [false, 3106];
  230. }
  231. }
  232. protected static function intoConf($corpid, $portraitData)
  233. {
  234. foreach ($portraitData as $value) {
  235. PortraitConf::query()
  236. ->updateOrCreate([
  237. 'corpid' => $corpid,
  238. 'fid' => $value->fid,
  239. 'name' => $value->name
  240. ], [
  241. 'sort' => $value->sort,
  242. 'status' => $value->status,
  243. 'field' => $value->field
  244. ]);
  245. }
  246. }
  247. public static function h5CustomerDetail($corpid, $external_userid, $userId)
  248. {
  249. $cust = Customer::suffix($corpid)
  250. ->where('corpid', $corpid)
  251. ->where('enable', 1)
  252. ->where('external_userid', $external_userid)
  253. ->first();
  254. $detail = CustomerDetails::suffix($corpid)
  255. ->where('corpid', $corpid)
  256. ->where('enable', 1)
  257. ->where('external_userid', $external_userid)
  258. ->where('user_id', $userId)
  259. ->first();
  260. if(!isset($cust->id) || !isset($detail->id)){
  261. return [false, 4711];
  262. }
  263. $res = array();
  264. $res['external_userid'] = $external_userid;
  265. $res['type'] = $cust->type;
  266. $res['name'] = $cust->name;
  267. $res['avatar'] = $cust->avatar;
  268. $res['add_time'] = date('Y-m-d H:i:s', $detail->createtime);
  269. $staff_all = CustomerDetails::suffix($corpid)
  270. ->where('corpid', $corpid)
  271. ->where('enable', 1)
  272. ->where('external_userid', $external_userid)
  273. ->pluck('user_id')
  274. ->all();
  275. $staff_info = DjUser::select('user_id', 'name', 'avatar')
  276. ->where('corpid', $corpid)
  277. ->whereIn('user_id', $staff_all)
  278. ->get();
  279. $res['staff_count'] = count($staff_all);
  280. $res['staff_info'] = $staff_info;
  281. $customerTagList = empty($detail->tag_list) ? [] : explode(',', $detail->tag_list);
  282. $res['tagList'] = Tag::query()
  283. ->where('corpid', $corpid)
  284. ->whereIn('tag_md5', $customerTagList)
  285. ->where('enable', 1)
  286. ->select('tag_md5', 'tag_name')
  287. ->get();
  288. #客户信息配置 dj_portrait_conf
  289. $portraitConf = PortraitConf::where('corpid', $corpid)
  290. ->where('status', 1)
  291. ->where('enable', 1)
  292. ->select('fid', 'field', 'name')
  293. ->orderBy('sort', 'asc')
  294. ->get()
  295. ->toArray();
  296. if( empty($portraitConf) ){
  297. //判断是否还未写入conf
  298. $pCount = PortraitConf::where('corpid', $corpid)->count();
  299. if($pCount>0){
  300. //全部禁用,直接返回空
  301. return [null, 0];
  302. }
  303. //还未写入conf,直接读默认
  304. $portraitConf = []; //最终展示在页面上的字段
  305. $portraitConfAll = Portrait::where('enable', 1)
  306. ->select('fid', 'field', 'name', 'status')
  307. ->orderBy('sort', 'asc')
  308. ->get()
  309. ->toArray();
  310. foreach ($portraitConfAll as $k => $v) {
  311. if($v['status'] == 1) $portraitConf[] = $v;
  312. }
  313. }
  314. if( empty($detail->attribute) ){
  315. //还未写入json属性,直接写入
  316. if( !isset($portraitConfAll) ){
  317. $portraitConfAll = $portraitConf;
  318. }
  319. $info = self::addAttribute($cust, $detail, $portraitConfAll);
  320. } else {
  321. $info = json_decode($detail->attribute, true);
  322. }
  323. $info_data = array();
  324. foreach($info as $k=>$v){
  325. $info_data[$v['fid']] = $v;
  326. }
  327. $attr = array();
  328. foreach($portraitConf as $val){
  329. $fid = $val['fid'];
  330. $val['val'] = $info_data[$fid]['val'] ?? null;
  331. $attr[] = $val;
  332. }
  333. $res['attr'] = $attr;
  334. return [$res, 0];
  335. }
  336. public static function addAttribute($cust, $detail, $portraitConf)
  337. {
  338. $attr_ins = array();
  339. foreach($portraitConf as $val){
  340. if(in_array($val['field'], ['e_source', 'e_desc', 'e_phone', 'gender'])){
  341. #来源,描述,手机优先走详情
  342. if($val['field'] == 'e_source'){
  343. $attr_ins[] = [
  344. 'fid' => $val['fid'],
  345. 'val' => CustomerDetails::getAddWaySource($detail->add_way)
  346. ];
  347. }
  348. if($val['field'] == 'e_desc'){
  349. $attr_ins[] = [
  350. 'fid' => $val['fid'],
  351. 'val' => $detail->description
  352. ];
  353. }
  354. if($val['field'] == 'e_phone'){
  355. $attr_ins[] = [
  356. 'fid' => $val['fid'],
  357. 'val' => $detail->remark_mobiles
  358. ];
  359. }
  360. if($val['field'] == 'gender'){
  361. $gender_arr = [
  362. 0 => '未知',
  363. 1 => '男',
  364. 2 => '女'
  365. ];
  366. $sex = $gender_arr[$detail->gender] ?? '未知';
  367. $attr_ins[] = [
  368. 'fid' => $val['fid'],
  369. 'val' => $sex
  370. ];
  371. }
  372. } else {
  373. $field_name = $val['field'];
  374. if($val['field'] && isset($cust->$field_name)){
  375. $attr_ins[] = [
  376. 'fid' => $val['fid'],
  377. 'val' => $cust->$field_name
  378. ];
  379. } else {
  380. $attr_ins[] = [
  381. 'fid' => $val['fid'],
  382. 'val' => null
  383. ];
  384. }
  385. }
  386. }
  387. $res = CustomerDetails::suffix($cust->corpid)
  388. ->where('id', $detail->id)
  389. ->update([
  390. 'attribute' => json_encode($attr_ins)
  391. ]);
  392. if($res){
  393. return $attr_ins;
  394. } else {
  395. return [];
  396. }
  397. }
  398. public static function h5CustomerDynamicList($corpid, $external_userid)
  399. {
  400. $customerInfo = Customer::suffix($corpid)
  401. ->where('external_userid', $external_userid)
  402. ->first();
  403. if( !isset($customerInfo->id) ){
  404. return false;
  405. }
  406. $data = CustomerService::customerDynamicList($corpid, $customerInfo->id);
  407. return $data;
  408. }
  409. public static function h5CustomerOrderList($corpid, $external_userid, $page, $pagesize)
  410. {
  411. $customerInfo = Customer::suffix($corpid)
  412. ->where('external_userid', $external_userid)
  413. ->first();
  414. if( !isset($customerInfo->id) ){
  415. return [null, 0];
  416. }
  417. list($data, $count) = CustomerService::customerOrderList($corpid, $customerInfo->id, $page, $pagesize);
  418. return [$data, $count];
  419. }
  420. public static function h5CustomerDetailTagUpdate($corpid, $externalUserId, $userId, $selectedTagIdList)
  421. {
  422. list($res, $code) = CustomerService::customerDetailTagUpdate($corpid, $externalUserId, $userId, $selectedTagIdList);
  423. return [$res, $code];
  424. }
  425. protected static function getFieldId($name)
  426. {
  427. return hash('fnv164', $name, false);
  428. }
  429. }