123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use App\Models\Traits\Custom\Login;
- /**
- * 客户模型
- *
- */
- class Custom extends Model
- {
- use HasFactory,Login;
- // 与模型关联的表名
- protected $table = 'custom';
- // 是否主动维护时间戳
- 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();
- // 写入数据表
- $uid = $this->query()->insertGetId($data);
- // 失败返回0
- if( !$uid ) return 0;
- // 返回结果
- return $uid;
- }
- /**
- * 添加数据
- *
- */
- public function edit($uid,$data)
- {
- // 更新时间
- $data['update_time'] = time();
- // 写入数据表
- $result = $this->query()->where(['uid'=>$uid])->update($data);
- // 失败返回0
- if( !$result ) return 0;
- // 返回结果
- return $uid;
- }
- /**
- * 编码转id
- *
- * @param string $code 编码
- *
- */
- public function codeToId($code){
- return intval(str_ireplace('klkh','',$code));
- }
-
- /**
- * id转编码
- *
- * @param int $uid 编码
- *
- */
- public function idToCode($uid){
- return 'klkh'. str_pad($uid, 9, '0', STR_PAD_LEFT);;
- }
- /**
- * 添加数据
- *
- */
- public function getOne($uid)
- {
- // 返回结果
- $custom = $this->query()->where([['uid','=',$uid]])->first(['uid','is_manager','external_userid','weiban_extid','phone','userpic','username','sex','city_id','status','insert_time']);
- // 返回结果
- if( !$custom ) return [];
- // 数据结构
- return $custom->toArray();
- }
- /**
- * 手机号获取
- *
- * @param string $phone 手机号
- *
- *
- */
- public function getOneByPhone($phone)
- {
- // 返回结果
- $custom = $this->query()->where([['phone','=',$phone]])->first(['uid','is_manager','external_userid','weiban_extid','phone','userpic','username','sex','city_id','status','insert_time']);
- // 返回结果
- if( !$custom ) return [];
- // 数据结构
- return $custom->toArray();
- }
- /**
- * 添加数据
- *
- * @param int $uid 客户ID
- * @param string $field 字段
- */
- public function getValue($uid,$field)
- {
- // 返回结果
- $result = $this->query()->where([['uid','=',$uid]])->value($field);
- // 数据结构
- return $result;
- }
- /**
- * 通过外部联系人ID查询用户
- *
- * @param string $extUserId 联系人ID
- *
- */
- public function getOneByExtUserId($extUserId)
- {
- // 返回结果
- $custom = $this->query()->where([['external_userid','=',$extUserId]])->first(['uid','external_userid','weiban_extid','phone','userpic','username','status','user_type','work_type']);
- // 返回结果
- if( !$custom ) return [];
- // 数据结构
- return $custom->toArray();
- }
- /**
- * 获取列表
- *
- * @param array $values 查询数据
- *
- */
- public function getPluckInFiled($filed,$values,$column,$key)
- {
- // 返回结果
- $custom = $this->query()->whereIn($filed,$values)->pluck($column,$key);
- // 返回结果
- if( !$custom ) return [];
- // 数据结构
- return $custom->toArray();
- }
- /**
- * 获取商业以及地区代表
- *
- */
- public function getListUserType()
- {
- $first = $this->query()->where([['user_type','=',1]])->select(['uid','userpic','username','user_type','status']);
- // 返回结果
- $result = $this->query()->where([['user_type','=',2]])->select(['uid','userpic','username','user_type','status'])->union($first)->get()->toArray();
- // 列表
- $list = [['user_group'=>'商业人员','user_list'=>[]],['user_group'=>'地区代表','user_list'=>[]]];
- // 循环处理
- foreach ( $result as $value ) {
- if( $value['user_type'] == 1 ) $list[0]['user_list'][] = $value;
- if( $value['user_type'] == 2 ) $list[1]['user_list'][] = $value;
- }
- // 数据结构
- return $list;
- }
- }
|