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; } }