123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php namespace App\Models\Work;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- /**
- * 客户-客服模型
- *
- */
- class User extends Model
- {
- use HasFactory;
- // 与模型关联的表名
- protected $table = 'work_user';
- // 是否主动维护时间戳
- public $timestamps = false;
- // 定义时间戳字段名
- // const CREATED_AT = 'insert_time';
- // const UPDATED_AT = 'update_time';
- /**
- * 添加数据
- *
- */
- public function add($data)
- {
- // 时间
- if( empty($data['insert_time']) ) $data['insert_time'] = time();
- $data['update_time'] = time();
- // 写入数据表
- $id = $this->query()->insertGetId($data);
- // 返回结果
- return $id;
- }
- /**
- * 添加数据
- *
- */
- public function edit($id,$data)
- {
- // 更新时间
- $data['update_time'] = time();
- // 写入数据表
- $result = $this->query()->where(['id'=>$id])->update($data);
- // 返回结果
- return $result;
- }
- /**
- * 删除跟进关系
- *
- */
- public function delByExtUser($extUserId,$userId)
- {
- // 写入数据表
- $result = $this->query()->where([['external_userid','=',$extUserId],['work_userid','=',$userId]])->delete();
- // 返回结果
- return $result;
- }
- /**
- * 通过内部客户ID查询用户
- *
- * @param string $customId 客户ID
- *
- */
- public function getListExtIds($extIds)
- {
- // 返回结果
- $data = $this->query()->whereIn('external_userid',$extIds)->get(['id','external_userid','work_userid','remark','description','remark_mobiles','remark_company','status'])->toArray();
- // 列表
- $list = [];
- // 获取结果
- foreach ($data as $value) {
- // 重组
- $list[$value['external_userid']][] = $value;
- }
- // 数据结构
- return $list;
- }
- /**
- * 通过外部联系人ID查询用户
- *
- * @param string $extUserId 外部联系人ID
- * @param string $userId 跟进人员ID
- *
- */
- public function getOneByExtUserId($extUserId,$userId)
- {
- // 返回结果
- $data = $this->query()->where([['external_userid','=',$extUserId],['work_userid','=',$userId]])->first(['id','work_userid','external_userid','remark','description','remark_mobiles','remark_company','status']);
- // 返回结果
- if( !$data ) return [];
- // 数据结构
- return $data->toArray();
- }
- /**
- * 通过外部联系人ID查询用户
- *
- * @param array $extUser 外部联系人
- * @param array $followUser 跟进人员
- *
- */
- public function upsertOne($extUser,$followUser)
- {
- // 组合数据
- $data['external_userid'] = $extUser['external_userid'];
- $data['work_userid'] = $followUser['userid'];
- $data['remark'] = $followUser['remark'];
- $data['description'] = $followUser['description'];
- $data['remark_mobiles'] = implode(',',$followUser['remark_mobiles']);
- $data['remark_company'] = empty($extUser['corp_name'])? (empty($followUser['remark_corp_name']) ? '' : $followUser['remark_corp_name']) : $extUser['corp_name'];
- $data['status'] = 0;
- // 查询用户是否已经存在此跟进人员关系
- $followData = $this->getOneByExtUserId($extUser['external_userid'],$followUser['userid']);
- // 添加时间
- if( !$followData ) $data['insert_time'] = $followUser['createtime'];
- // 如果不存在新增
- $result = $followData ? $this->edit($followData['id'],$data) : $this->add($data);
- // 返回结果
- return $result;
- }
- /**
- * 修改状态
- *
- * @param array $extUser 外部联系人
- * @param array $followUser 跟进人员
- *
- *
- */
- public function updateByExtUser($extUserId,$userId,$data)
- {
- // 更新时间
- $data['update_time'] = time();
- // 查询用户是否已经存在此跟进人员关系
- $result = $this->query()->where([['external_userid','=',$extUserId],['work_userid','=',$userId]])->update($data);
- // 返回结果
- return $result;
- }
- }
|