Contacts.php 5.2 KB

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