select(['fid', 'field', 'name', 'status', 'enable']) ->where('corpid', $corpid) ->orderBy('sort', 'ASC') ->get(); $result = []; if ($portraitConfData->isEmpty()) { $result = Portrait::query() ->select(['fid', 'field', 'name', 'status', 'enable']) ->where('enable', 1) ->orderBy('sort', 'ASC') ->get() ->toArray(); } else { foreach ($portraitConfData as $portraitConf) { if ($portraitConf->enable == 0) continue; $result[] = $portraitConf->toArray(); } } return $result; } public static function confOperate($corpid, $confJson) { // 检测是否是json $confArr = json_decode($confJson, true); if (!is_array($confArr)) return [false, 4701]; $isExist = PortraitConf::query()->where('corpid', $corpid)->exists(); if (!$isExist) { $portraitData = Portrait::query() ->where('enable', 1) ->orderBy('id', 'ASC') ->get() ->keyBy('fid'); } else { $portraitData = PortraitConf::query() ->where('corpid', $corpid) ->where('enable', 1) ->orderBy('sort', 'ASC') ->get() ->keyBy('fid'); if (count($confArr) != $portraitData->count()) return [false, 4701]; } foreach ($confArr as $sort => $conf) { $portraitInfo = $portraitData->get($conf['fid']); if ( empty($portraitInfo) || $portraitInfo->enable == 0 || ($portraitInfo->name != $conf['name']) ) return [false, 4702]; $portraitInfo->sort = $sort; $portraitInfo->status = $conf['status']; } \DB::begintransaction(); try { self::intoConf($corpid, $portraitData); \DB::commit(); return [true, 0]; } catch (\Throwable $e) { Log::logError( '画像配置操作有误~'.$e->getMessage().PHP_EOL.$e->getTraceAsString(), [ 'corpid' => $corpid, 'confJson' => $confJson, ], 'PortraitService' ); \DB::rollBack(); return [false, 3106]; } } public static function confAdd($corpid, $name) { \DB::begintransaction(); try { $isExist = PortraitConf::query()->where('corpid', $corpid)->exists(); if (!$isExist) { $portraitData = Portrait::query() ->where('enable', 1) ->orderBy('id', 'ASC') ->get(); self::intoConf($corpid, $portraitData); } $fid = self::getFieldId($name); $PortraitConfDetail = PortraitConf::query() ->where('corpid', $corpid) ->where('fid', $fid) ->where('name', $name) ->where('enable', 1) ->first(); if (!empty($PortraitConfDetail)) return [false, 4706]; $sort = PortraitConf::query()->where('corpid', $corpid)->max('sort'); PortraitConf::query()->updateOrCreate([ 'corpid' => $corpid, 'fid' => $fid, 'name' => $name ], [ 'sort' => $sort + 1, 'status' => 0, 'enable' => 1 ]); \DB::commit(); return [true, 0]; } catch (\Throwable $e) { Log::logError( '画像属性添加操作有误~'.$e->getMessage().PHP_EOL.$e->getTraceAsString(), [ 'corpid' => $corpid, 'name' => $name, ], 'PortraitService' ); \DB::rollBack(); return [false, 3106]; } } public static function confEdit($corpid, $fid, $name) { \DB::begintransaction(); try { $isExist = PortraitConf::query()->where('corpid', $corpid)->exists(); if (!$isExist) { $portraitData = Portrait::query() ->where('enable', 1) ->orderBy('id', 'ASC') ->get(); self::intoConf($corpid, $portraitData); } $portraitConfInfo = PortraitConf::query() ->where('corpid', $corpid) ->where('fid', $fid) ->where('enable', 1) ->first(); if (empty($portraitConfInfo)) return [false, 4707]; $fid = self::getFieldId($name); $portraitConfInfo->update([ 'fid' => $fid, 'name' => $name ]); \DB::commit(); return [true, 0]; } catch (\Throwable $e) { Log::logError( '画像属性编辑操作有误~'.$e->getMessage().PHP_EOL.$e->getTraceAsString(), [ 'corpid' => $corpid, 'fid' => $fid, 'name' => $name, ], 'PortraitService' ); \DB::rollBack(); return [false, 3106]; } } public static function edit($corpid, $userId, $externalUserid, $attrsJson) { $customerDetailInfo = CustomerDetails::suffix($corpid) ->where('corpid', $corpid) ->where('user_id', $userId) ->where('external_userid', $externalUserid) ->first(); if (empty($customerDetailInfo)) return [false, 4703]; // 检测json是否合法 $attribute = json_decode($customerDetailInfo->attribute, true); if (!is_array($attribute)) return [false, 4704]; $attrs = json_decode($attrsJson, true); if (!is_array($attrs)) return [false, 4701]; // 检测conf属性是否存在 $portraitData = PortraitConf::query() ->where('corpid', $corpid) ->where('enable', 1) ->pluck('name', 'fid'); if ($portraitData->isEmpty()) $portraitData = Portrait::query() ->where('enable', 1) ->pluck('name', 'fid'); // 检测detail数据 foreach ($attribute as $key => &$item) { // 属性字段被修改过 if (is_null($portraitData->get($item['fid']))) { unset($attribute[$key]); continue; } // 属性字段不匹配 if (!isset($attrs[$item['fid']])) continue; $item['val'] = $attrs[$item['fid']]; unset($attrs[$item['fid']]); } if (!empty($attrs)) { // 检测传递的属性字段参数是否存在 foreach ($attrs as $fid => $val) { if (is_null($portraitData->get($fid))) return [false, 4705]; $attribute[] = [ 'fid' => $fid, 'val' => $val ]; } } \DB::begintransaction(); try { $customerDetailInfo->update(['attribute' => json_encode(array_values($attribute))]); \DB::commit(); return [true, 0]; } catch (\Throwable $e) { Log::logError( '客户添加属性操作有误~'.$e->getMessage().PHP_EOL.$e->getTraceAsString(), [ 'corpid' => $corpid, 'userId' => $userId, 'externalUserid' => $externalUserid, 'fid' => $fid, 'val' => $val, ], 'PortraitService' ); \DB::rollBack(); return [false, 3106]; } } protected static function intoConf($corpid, $portraitData) { foreach ($portraitData as $value) { PortraitConf::query() ->updateOrCreate([ 'corpid' => $corpid, 'fid' => $value->fid, 'name' => $value->name ], [ 'sort' => $value->sort, 'status' => $value->status, 'field' => $value->field ]); } } public static function h5CustomerDetail($corpid, $external_userid, $userId) { $cust = Customer::suffix($corpid) ->where('corpid', $corpid) ->where('enable', 1) ->where('external_userid', $external_userid) ->first(); $detail = CustomerDetails::suffix($corpid) ->where('corpid', $corpid) ->where('enable', 1) ->where('external_userid', $external_userid) ->where('user_id', $userId) ->first(); if(!isset($cust->id) || !isset($detail->id)){ return [false, 4711]; } $res = array(); $res['external_userid'] = $external_userid; $res['type'] = $cust->type; $res['name'] = $cust->name; $res['avatar'] = $cust->avatar; $res['add_time'] = date('Y-m-d H:i:s', $detail->createtime); $staff_all = CustomerDetails::suffix($corpid) ->where('corpid', $corpid) ->where('enable', 1) ->where('external_userid', $external_userid) ->pluck('user_id') ->all(); $staff_info = DjUser::select('user_id', 'name', 'avatar') ->where('corpid', $corpid) ->whereIn('user_id', $staff_all) ->get(); $res['staff_count'] = count($staff_all); $res['staff_info'] = $staff_info; $customerTagList = empty($detail->tag_list) ? [] : explode(',', $detail->tag_list); $res['tagList'] = Tag::query() ->where('corpid', $corpid) ->whereIn('tag_md5', $customerTagList) ->where('enable', 1) ->select('tag_md5', 'tag_name') ->get(); #客户信息配置 dj_portrait_conf $portraitConf = PortraitConf::where('corpid', $corpid) ->where('status', 1) ->where('enable', 1) ->select('fid', 'field', 'name') ->orderBy('sort', 'asc') ->get() ->toArray(); if( empty($portraitConf) ){ //判断是否还未写入conf $pCount = PortraitConf::where('corpid', $corpid)->count(); if($pCount>0){ //全部禁用,直接返回空 return [null, 0]; } //还未写入conf,直接读默认 $portraitConf = []; //最终展示在页面上的字段 $portraitConfAll = Portrait::where('enable', 1) ->select('fid', 'field', 'name', 'status') ->orderBy('sort', 'asc') ->get() ->toArray(); foreach ($portraitConfAll as $k => $v) { if($v['status'] == 1) $portraitConf[] = $v; } } if( empty($detail->attribute) ){ //还未写入json属性,直接写入 if( !isset($portraitConfAll) ){ $portraitConfAll = $portraitConf; } $info = self::addAttribute($cust, $detail, $portraitConfAll); } else { $info = json_decode($detail->attribute, true); } $info_data = array(); foreach($info as $k=>$v){ $info_data[$v['fid']] = $v; } $attr = array(); foreach($portraitConf as $val){ $fid = $val['fid']; $val['val'] = $info_data[$fid]['val'] ?? null; $attr[] = $val; } $res['attr'] = $attr; return [$res, 0]; } public static function addAttribute($cust, $detail, $portraitConf) { $attr_ins = array(); foreach($portraitConf as $val){ if(in_array($val['field'], ['e_source', 'e_desc', 'e_phone', 'gender'])){ #来源,描述,手机优先走详情 if($val['field'] == 'e_source'){ $attr_ins[] = [ 'fid' => $val['fid'], 'val' => CustomerDetails::getAddWaySource($detail->add_way) ]; } if($val['field'] == 'e_desc'){ $attr_ins[] = [ 'fid' => $val['fid'], 'val' => $detail->description ]; } if($val['field'] == 'e_phone'){ $attr_ins[] = [ 'fid' => $val['fid'], 'val' => $detail->remark_mobiles ]; } if($val['field'] == 'gender'){ $gender_arr = [ 0 => '未知', 1 => '男', 2 => '女' ]; $sex = $gender_arr[$detail->gender] ?? '未知'; $attr_ins[] = [ 'fid' => $val['fid'], 'val' => $sex ]; } } else { $field_name = $val['field']; if($val['field'] && isset($cust->$field_name)){ $attr_ins[] = [ 'fid' => $val['fid'], 'val' => $cust->$field_name ]; } else { $attr_ins[] = [ 'fid' => $val['fid'], 'val' => null ]; } } } $res = CustomerDetails::suffix($cust->corpid) ->where('id', $detail->id) ->update([ 'attribute' => json_encode($attr_ins) ]); if($res){ return $attr_ins; } else { return []; } } public static function h5CustomerDynamicList($corpid, $external_userid) { $customerInfo = Customer::suffix($corpid) ->where('external_userid', $external_userid) ->first(); if( !isset($customerInfo->id) ){ return false; } $data = CustomerService::customerDynamicList($corpid, $customerInfo->id); return $data; } public static function h5CustomerOrderList($corpid, $external_userid, $page, $pagesize) { $customerInfo = Customer::suffix($corpid) ->where('external_userid', $external_userid) ->first(); if( !isset($customerInfo->id) ){ return [null, 0]; } list($data, $count) = CustomerService::customerOrderList($corpid, $customerInfo->id, $page, $pagesize); return [$data, $count]; } public static function h5CustomerDetailTagUpdate($corpid, $externalUserId, $userId, $selectedTagIdList) { list($res, $code) = CustomerService::customerDetailTagUpdate($corpid, $externalUserId, $userId, $selectedTagIdList); return [$res, $code]; } protected static function getFieldId($name) { return hash('fnv164', $name, false); } }