Custom.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php namespace App\Models;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. use App\Models\Traits\Custom\Login;
  5. /**
  6. * 客户模型
  7. *
  8. */
  9. class Custom extends Model
  10. {
  11. use HasFactory,Login;
  12. // 与模型关联的表名
  13. protected $table = 'custom';
  14. // 是否主动维护时间戳
  15. public $timestamps = false;
  16. // 定义时间戳字段名
  17. // const CREATED_AT = 'insert_time';
  18. // const UPDATED_AT = 'update_time';
  19. /**
  20. * 添加数据
  21. *
  22. */
  23. public function add($data)
  24. {
  25. // 时间
  26. if( empty($data['insert_time']) ) $data['insert_time'] = time();
  27. $data['update_time'] = time();
  28. // 写入数据表
  29. $uid = $this->query()->insertGetId($data);
  30. // 失败返回0
  31. if( !$uid ) return 0;
  32. // 返回结果
  33. return $uid;
  34. }
  35. /**
  36. * 添加数据
  37. *
  38. */
  39. public function edit($uid,$data)
  40. {
  41. // 更新时间
  42. $data['update_time'] = time();
  43. // 写入数据表
  44. $result = $this->query()->where(['uid'=>$uid])->update($data);
  45. // 失败返回0
  46. if( !$result ) return 0;
  47. // 返回结果
  48. return $uid;
  49. }
  50. /**
  51. * 编码转id
  52. *
  53. * @param string $code 编码
  54. *
  55. */
  56. public function codeToId($code){
  57. return intval(str_ireplace('klkh','',$code));
  58. }
  59. /**
  60. * id转编码
  61. *
  62. * @param int $uid 编码
  63. *
  64. */
  65. public function idToCode($uid){
  66. return 'klkh'. str_pad($uid, 9, '0', STR_PAD_LEFT);;
  67. }
  68. /**
  69. * 添加数据
  70. *
  71. */
  72. public function getOne($uid)
  73. {
  74. // 返回结果
  75. $custom = $this->query()->where([['uid','=',$uid]])->first(['uid','is_manager','external_userid','weiban_extid','phone','userpic','username','sex','city_id','status','insert_time']);
  76. // 返回结果
  77. if( !$custom ) return [];
  78. // 数据结构
  79. return $custom->toArray();
  80. }
  81. /**
  82. * 手机号获取
  83. *
  84. * @param string $phone 手机号
  85. *
  86. *
  87. */
  88. public function getOneByPhone($phone)
  89. {
  90. // 返回结果
  91. $custom = $this->query()->where([['phone','=',$phone]])->first(['uid','is_manager','external_userid','weiban_extid','phone','userpic','username','sex','city_id','status','insert_time']);
  92. // 返回结果
  93. if( !$custom ) return [];
  94. // 数据结构
  95. return $custom->toArray();
  96. }
  97. /**
  98. * 添加数据
  99. *
  100. * @param int $uid 客户ID
  101. * @param string $field 字段
  102. */
  103. public function getValue($uid,$field)
  104. {
  105. // 返回结果
  106. $result = $this->query()->where([['uid','=',$uid]])->value($field);
  107. // 数据结构
  108. return $result;
  109. }
  110. /**
  111. * 通过外部联系人ID查询用户
  112. *
  113. * @param string $extUserId 联系人ID
  114. *
  115. */
  116. public function getOneByExtUserId($extUserId)
  117. {
  118. // 返回结果
  119. $custom = $this->query()->where([['external_userid','=',$extUserId]])->first(['uid','external_userid','weiban_extid','phone','userpic','username','status','user_type','work_type']);
  120. // 返回结果
  121. if( !$custom ) return [];
  122. // 数据结构
  123. return $custom->toArray();
  124. }
  125. /**
  126. * 获取列表
  127. *
  128. * @param array $values 查询数据
  129. *
  130. */
  131. public function getPluckInFiled($filed,$values,$column,$key)
  132. {
  133. // 返回结果
  134. $custom = $this->query()->whereIn($filed,$values)->pluck($column,$key);
  135. // 返回结果
  136. if( !$custom ) return [];
  137. // 数据结构
  138. return $custom->toArray();
  139. }
  140. /**
  141. * 获取商业以及地区代表
  142. *
  143. */
  144. public function getListUserType()
  145. {
  146. $first = $this->query()->where([['user_type','=',1]])->select(['uid','userpic','username','user_type','status']);
  147. // 返回结果
  148. $result = $this->query()->where([['user_type','=',2]])->select(['uid','userpic','username','user_type','status'])->union($first)->get()->toArray();
  149. // 列表
  150. $list = [['user_group'=>'商业人员','user_list'=>[]],['user_group'=>'地区代表','user_list'=>[]]];
  151. // 循环处理
  152. foreach ( $result as $value ) {
  153. if( $value['user_type'] == 1 ) $list[0]['user_list'][] = $value;
  154. if( $value['user_type'] == 2 ) $list[1]['user_list'][] = $value;
  155. }
  156. // 数据结构
  157. return $list;
  158. }
  159. }