External.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php namespace App\Models\Work;
  2. use Illuminate\Database\Eloquent\Factories\HasFactory;
  3. use Illuminate\Database\Eloquent\Model;
  4. /**
  5. * 客户模型
  6. *
  7. */
  8. class External extends Model
  9. {
  10. use HasFactory;
  11. // 与模型关联的表名
  12. protected $table = 'work_external';
  13. // 是否主动维护时间戳
  14. public $timestamps = false;
  15. // 定义时间戳字段名
  16. // const CREATED_AT = 'insert_time';
  17. // const UPDATED_AT = 'update_time';
  18. /**
  19. * 添加数据
  20. *
  21. */
  22. public function add($data)
  23. {
  24. // 时间
  25. $data['insert_time'] = time();
  26. $data['update_time'] = time();
  27. // 写入数据表
  28. $id = $this->query()->insertGetId($data);
  29. // 返回结果
  30. return $id;
  31. }
  32. /**
  33. * 添加数据
  34. *
  35. */
  36. public function edit($id,$data)
  37. {
  38. // 更新时间
  39. $data['update_time'] = time();
  40. // 写入数据表
  41. $result = $this->query()->where(['id'=>$id])->update($data);
  42. // 失败返回0
  43. if( !$result ) return 0;
  44. // 返回结果
  45. return $id;
  46. }
  47. /**
  48. * 编码转id
  49. *
  50. * @param string $code 编码
  51. *
  52. */
  53. public function codeToId($code){
  54. return intval(str_ireplace('klqw','',$code));
  55. }
  56. /**
  57. * id转编码
  58. *
  59. * @param int $id 编码
  60. *
  61. */
  62. public function idToCode($id){
  63. return 'klqw'. str_pad($id, 9, '0', STR_PAD_LEFT);;
  64. }
  65. /**
  66. * 添加数据
  67. *
  68. */
  69. public function getOne($id)
  70. {
  71. // 返回结果
  72. $custom = $this->query()->where([['id','=',$id]])->first(['id','external_userid','custom_uid','avatar','name','gender','status','work_type','user_type']);
  73. // 返回结果
  74. if( !$custom ) return [];
  75. // 数据结构
  76. return $custom->toArray();
  77. }
  78. /**
  79. * 添加数据
  80. *
  81. * @param int $id 客户ID
  82. * @param string $field 字段
  83. */
  84. public function getValue($id,$field)
  85. {
  86. // 返回结果
  87. $result = $this->query()->where([['id','=',$id]])->value($field);
  88. // 数据结构
  89. return $result;
  90. }
  91. /**
  92. * 通过外部联系人ID查询用户
  93. *
  94. * @param string $extUserId 联系人ID
  95. *
  96. */
  97. public function getOneByExtUserId($extUserId)
  98. {
  99. // 返回结果
  100. $custom = $this->query()->where([['external_userid','=',$extUserId]])->first(['id','external_userid','custom_uid','avatar','name','gender','status','work_type','user_type']);
  101. // 返回结果
  102. if( !$custom ) return [];
  103. // 数据结构
  104. return $custom->toArray();
  105. }
  106. /**
  107. * 获取列表
  108. *
  109. * @param array $extUserIds 联系人ID列表
  110. *
  111. */
  112. public function getPluckInFiled($filed,$extUserIds,$column,$key)
  113. {
  114. // 返回结果
  115. $custom = $this->query()->whereIn($filed,$extUserIds)->pluck($column,$key);
  116. // 返回结果
  117. if( !$custom ) return [];
  118. // 数据结构
  119. return $custom->toArray();
  120. }
  121. /**
  122. * 企微用户添加注册
  123. *
  124. * @param array $extUser 外部联系人信息
  125. *
  126. */
  127. public function workAdd($extUser){
  128. // 名称
  129. // 外部联系人ID
  130. $custom['external_userid'] = empty($extUser['external_userid'])?'':$extUser['external_userid'];
  131. // 联系人名称
  132. $custom['name'] = empty($extUser['name'])?'':$extUser['name'];
  133. // 头像
  134. $custom['avatar'] = empty($extUser['avatar'])?'':$extUser['avatar'];
  135. // 性别
  136. $custom['gender'] = empty($extUser['gender'])?'':$extUser['gender'];
  137. // 联系人类型
  138. $custom['work_type'] = empty($extUser['type'])? 0 :$extUser['type'];
  139. // 添加用户
  140. $id = $this->add($custom);
  141. // 返回结果
  142. return $id;
  143. }
  144. }