123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- <?php
- namespace App\Service;
- use App\Log;
- use App\Models\CustomerDetails;
- use App\Models\Portrait;
- use App\Models\PortraitConf;
- use App\Models\Customer;
- use App\Models\Tag;
- use App\Models\DjUser;
- class PortraitService
- {
- public static function confList($corpid)
- {
- $portraitConfData = PortraitConf::query()
- ->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);
- }
- }
|