Contacts.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace addons\qingdong\model;
  3. use think\Exception;
  4. use think\Model;
  5. use traits\model\SoftDelete;
  6. use addons\qingdong\library\Ku;
  7. /**
  8. *联系人表
  9. */
  10. class Contacts Extends Model {
  11. use SoftDelete;
  12. // 表名,不含前缀
  13. protected $name = 'qingdong_contacts';
  14. // 开启自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'createtime';
  18. protected $updateTime = 'updatetime';
  19. protected $deleteTime = 'deletetime';
  20. protected $field = true;
  21. //客户
  22. public function customer() {
  23. return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,name,follow');
  24. }
  25. //负责人
  26. public function ownerStaff() {
  27. return $this->hasOne(Staff::class, 'id', 'owner_staff_id')->field('id,name');
  28. }
  29. /**
  30. *
  31. */
  32. public static function getList() {
  33. return self::where(['owner_staff_id'=>['in',Staff::getMyStaffIds()]])->field('id,name')->select();
  34. }
  35. //创建联系人
  36. public static function createContacts($params) {
  37. //自定义字段
  38. $other = [];
  39. foreach ($params as $name => $val) {
  40. if (strstr( $name,'other_') !== false) {
  41. if(is_array($val)){
  42. $other[$name] = implode(',',$val);
  43. }else{
  44. $other[$name] = $val;
  45. }
  46. unset($params[$name]);
  47. }else{
  48. if(empty($params[$name])){
  49. $params[$name]=NULL;
  50. }
  51. }
  52. }
  53. $staff = Staff::info();
  54. if($staff){
  55. $params['create_staff_id'] = $staff->id;
  56. $owner_staff_id = $staff->id;
  57. if($params['customer_id']){
  58. $owner_staff_id = Customer::where(array('id'=>$params['customer_id']))->value('owner_staff_id');
  59. }
  60. $params['owner_staff_id'] = $owner_staff_id;
  61. }
  62. $model = new self;
  63. $result = $model->allowField(true)->save($params);
  64. if (false === $result) {
  65. // 验证失败 输出错误信息
  66. throw new Exception($model->getError());
  67. }
  68. $lastId=$model->getLastInsID();
  69. $otherModel = new ContactsOther();
  70. if ($otherModel->save(['id' => $lastId, 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)]) === false) {
  71. // 验证失败 输出错误信息
  72. throw new Exception($otherModel->getError());
  73. }
  74. //创建日志
  75. OperationLog::createLog(OperationLog::CONTACTS_TYPE,$lastId, '创建联系人');
  76. //新增跟进记录
  77. Record::quickCreateRecord(Record::CUSTOMER_TYPE, $params['customer_id'], '新增联系人:' . $params['name']);
  78. OperationLog::createLog(OperationLog::CUSTOMER_TYPE, $params['customer_id'], '新增联系人:' . $params['name']);
  79. //同步数据
  80. $ku = new Ku();
  81. $params['contacts_id'] = $lastId;
  82. $ku->addCustomer($params);
  83. return $lastId;
  84. }
  85. //修改联系人
  86. public static function updateContacts($params) {
  87. //自定义字段
  88. $other = [];
  89. foreach ($params as $name => $val) {
  90. if (strstr($name,'other_') !== false) {
  91. if(is_array($val)){
  92. $other[$name] = implode(',',$val);
  93. }else{
  94. $other[$name] = $val;
  95. }
  96. unset($params[$name]);
  97. }else{
  98. if(empty($params[$name])){
  99. $params[$name]=NULL;
  100. }
  101. }
  102. }
  103. $model = new self;
  104. // 调用当前模型对应的User验证器类进行数据验证
  105. $result = $model->save($params, ['id' => $params['id']]);
  106. if (false === $result) {
  107. // 验证失败 输出错误信息
  108. throw new Exception($model->getError());
  109. }
  110. $otherModel = new ContactsOther();
  111. if ($otherModel->save(['otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)],['id' => $params['id']]) === false) {
  112. // 验证失败 输出错误信息
  113. throw new Exception($otherModel->getError());
  114. }
  115. return true;
  116. }
  117. //导入
  118. public static function importContacts($data) {
  119. $addContacts = [];
  120. $addOther = [];
  121. foreach ($data as $params) {
  122. //自定义字段
  123. $other = [];
  124. foreach ($params as $name => $val) {
  125. if (strstr($name, 'other_') !== false) {
  126. if(is_array($val)){
  127. $other[$name] = implode(',',$val);
  128. }else{
  129. $other[$name] = $val;
  130. }
  131. unset($params[$name]);
  132. }else{
  133. if(empty($params[$name])){
  134. $params[$name]=NULL;
  135. }
  136. }
  137. }
  138. $params['createtime'] = time();
  139. $params['next_time'] = date('Y-m-d H:i:s');
  140. $other['id'] = $params['id'];
  141. $addOther[] = ['id' => $params['id'], 'otherdata' => json_encode($other, JSON_UNESCAPED_UNICODE)];
  142. $addContacts[] = $params;
  143. }
  144. $model = new self;
  145. // 调用当前模型对应的User验证器类进行数据验证
  146. $result = $model->allowField(true)->insertAll($addContacts);
  147. $otherModel = new ContactsOther();
  148. $otherModel->allowField(true)->insertAll($addOther);
  149. return true;
  150. }
  151. //获取联系人相关信息
  152. public function contactsOther() {
  153. return $this->belongsTo(ContactsOther::class,'id','id');
  154. }
  155. public function getUpdatetimeAttr($value) {
  156. if ($value) {
  157. return date('Y-m-d H:i', $value);
  158. }
  159. return $value;
  160. }
  161. }