User.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 User extends Model
  9. {
  10. use HasFactory;
  11. // 与模型关联的表名
  12. protected $table = 'work_user';
  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. if( empty($data['insert_time']) ) $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. // 返回结果
  43. return $result;
  44. }
  45. /**
  46. * 删除跟进关系
  47. *
  48. */
  49. public function delByExtUser($extUserId,$userId)
  50. {
  51. // 写入数据表
  52. $result = $this->query()->where([['external_userid','=',$extUserId],['work_userid','=',$userId]])->delete();
  53. // 返回结果
  54. return $result;
  55. }
  56. /**
  57. * 通过内部客户ID查询用户
  58. *
  59. * @param string $customId 客户ID
  60. *
  61. */
  62. public function getListExtIds($extIds)
  63. {
  64. // 返回结果
  65. $data = $this->query()->whereIn('external_userid',$extIds)->get(['id','external_userid','work_userid','remark','description','remark_mobiles','remark_company','status'])->toArray();
  66. // 列表
  67. $list = [];
  68. // 获取结果
  69. foreach ($data as $value) {
  70. // 重组
  71. $list[$value['external_userid']][] = $value;
  72. }
  73. // 数据结构
  74. return $list;
  75. }
  76. /**
  77. * 通过外部联系人ID查询用户
  78. *
  79. * @param string $extUserId 外部联系人ID
  80. * @param string $userId 跟进人员ID
  81. *
  82. */
  83. public function getOneByExtUserId($extUserId,$userId)
  84. {
  85. // 返回结果
  86. $data = $this->query()->where([['external_userid','=',$extUserId],['work_userid','=',$userId]])->first(['id','work_userid','external_userid','remark','description','remark_mobiles','remark_company','status']);
  87. // 返回结果
  88. if( !$data ) return [];
  89. // 数据结构
  90. return $data->toArray();
  91. }
  92. /**
  93. * 通过外部联系人ID查询用户
  94. *
  95. * @param array $extUser 外部联系人
  96. * @param array $followUser 跟进人员
  97. *
  98. */
  99. public function upsertOne($extUser,$followUser)
  100. {
  101. // 组合数据
  102. $data['external_userid'] = $extUser['external_userid'];
  103. $data['work_userid'] = $followUser['userid'];
  104. $data['remark'] = $followUser['remark'];
  105. $data['description'] = $followUser['description'];
  106. $data['remark_mobiles'] = implode(',',$followUser['remark_mobiles']);
  107. $data['remark_company'] = empty($extUser['corp_name'])? (empty($followUser['remark_corp_name']) ? '' : $followUser['remark_corp_name']) : $extUser['corp_name'];
  108. $data['status'] = 0;
  109. // 查询用户是否已经存在此跟进人员关系
  110. $followData = $this->getOneByExtUserId($extUser['external_userid'],$followUser['userid']);
  111. // 添加时间
  112. if( !$followData ) $data['insert_time'] = $followUser['createtime'];
  113. // 如果不存在新增
  114. $result = $followData ? $this->edit($followData['id'],$data) : $this->add($data);
  115. // 返回结果
  116. return $result;
  117. }
  118. /**
  119. * 修改状态
  120. *
  121. * @param array $extUser 外部联系人
  122. * @param array $followUser 跟进人员
  123. *
  124. *
  125. */
  126. public function updateByExtUser($extUserId,$userId,$data)
  127. {
  128. // 更新时间
  129. $data['update_time'] = time();
  130. // 查询用户是否已经存在此跟进人员关系
  131. $result = $this->query()->where([['external_userid','=',$extUserId],['work_userid','=',$userId]])->update($data);
  132. // 返回结果
  133. return $result;
  134. }
  135. }