123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php namespace App\Models\Work;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- /**
- * 客户模型
- *
- */
- class External extends Model
- {
- use HasFactory;
- // 与模型关联的表名
- protected $table = 'work_external';
- // 是否主动维护时间戳
- public $timestamps = false;
- // 定义时间戳字段名
- // const CREATED_AT = 'insert_time';
- // const UPDATED_AT = 'update_time';
- /**
- * 添加数据
- *
- */
- public function add($data)
- {
- // 时间
- $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);
- // 失败返回0
- if( !$result ) return 0;
- // 返回结果
- return $id;
- }
- /**
- * 编码转id
- *
- * @param string $code 编码
- *
- */
- public function codeToId($code){
- return intval(str_ireplace('klqw','',$code));
- }
-
- /**
- * id转编码
- *
- * @param int $id 编码
- *
- */
- public function idToCode($id){
- return 'klqw'. str_pad($id, 9, '0', STR_PAD_LEFT);;
- }
- /**
- * 添加数据
- *
- */
- public function getOne($id)
- {
- // 返回结果
- $custom = $this->query()->where([['id','=',$id]])->first(['id','external_userid','custom_uid','avatar','name','gender','status','work_type','user_type']);
- // 返回结果
- if( !$custom ) return [];
- // 数据结构
- return $custom->toArray();
- }
- /**
- * 添加数据
- *
- * @param int $id 客户ID
- * @param string $field 字段
- */
- public function getValue($id,$field)
- {
- // 返回结果
- $result = $this->query()->where([['id','=',$id]])->value($field);
- // 数据结构
- return $result;
- }
- /**
- * 通过外部联系人ID查询用户
- *
- * @param string $extUserId 联系人ID
- *
- */
- public function getOneByExtUserId($extUserId)
- {
- // 返回结果
- $custom = $this->query()->where([['external_userid','=',$extUserId]])->first(['id','external_userid','custom_uid','avatar','name','gender','status','work_type','user_type']);
- // 返回结果
- if( !$custom ) return [];
- // 数据结构
- return $custom->toArray();
- }
- /**
- * 获取列表
- *
- * @param array $extUserIds 联系人ID列表
- *
- */
- public function getPluckInFiled($filed,$extUserIds,$column,$key)
- {
- // 返回结果
- $custom = $this->query()->whereIn($filed,$extUserIds)->pluck($column,$key);
- // 返回结果
- if( !$custom ) return [];
- // 数据结构
- return $custom->toArray();
- }
- /**
- * 企微用户添加注册
- *
- * @param array $extUser 外部联系人信息
- *
- */
- public function workAdd($extUser){
- // 名称
- // 外部联系人ID
- $custom['external_userid'] = empty($extUser['external_userid'])?'':$extUser['external_userid'];
- // 联系人名称
- $custom['name'] = empty($extUser['name'])?'':$extUser['name'];
- // 头像
- $custom['avatar'] = empty($extUser['avatar'])?'':$extUser['avatar'];
- // 性别
- $custom['gender'] = empty($extUser['gender'])?'':$extUser['gender'];
- // 联系人类型
- $custom['work_type'] = empty($extUser['type'])? 0 :$extUser['type'];
- // 添加用户
- $id = $this->add($custom);
- // 返回结果
- return $id;
- }
-
- }
|