Record.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace addons\qingdongams\model;
  3. use addons\qingdongams\model\Record as RecordModel;
  4. use think\Exception;
  5. use think\Model;
  6. use think\Session;
  7. use traits\model\SoftDelete;
  8. /**
  9. *跟进记录
  10. */
  11. class Record Extends Model {
  12. use SoftDelete;
  13. // 表名,不含前缀
  14. protected $name = 'qingdongams_record';
  15. const CUSTOMER_TYPE = 1;//客户
  16. const CONTACTS_TYPE = 2;//联系人
  17. const CONTRACT_TYPE = 3;//合同
  18. const LEADS_TYPE = 4;//线索
  19. const BUSINESS_TYPE = 5;//商机
  20. // 开启自动写入时间戳字段
  21. protected $autoWriteTimestamp = 'int';
  22. // 定义时间戳字段名
  23. protected $createTime = 'createtime';
  24. protected $updateTime = 'updatetime';
  25. protected $deleteTime = 'deletetime';
  26. //获取跟进记录列表
  27. public static function getList($relation_type, $relation_id) {
  28. return self::where(['relation_type' => $relation_type, 'relation_id' => $relation_id])->with(['file', 'staff'])->order('id desc')->select();
  29. }
  30. //销售
  31. public function staff() {
  32. return $this->hasOne(Staff::class, 'id', 'create_staff_id')->field('id,img,name,post')->removeOption('soft_delete');
  33. }
  34. //获取附件记录
  35. public function file() {
  36. return $this->hasMany(RecordFile::class, 'record_id', 'id')->with('file')->field('record_id,file_id');
  37. }
  38. //创建人
  39. public function creatstaff() {
  40. return $this->hasOne(Staff::class, 'id', 'staff_id')->field('id,img,name,post')->removeOption('soft_delete');
  41. }
  42. //创建跟进记录
  43. public static function createRecord($params) {
  44. if(isset($params['files'])){
  45. $files = $params['files'];
  46. unset($params['files']);
  47. }else{
  48. $files='';
  49. }
  50. $staff = Staff::info();
  51. if ($params['relation_type'] == RecordModel::CUSTOMER_TYPE) {//客户
  52. $create_staff_id = Customer::where(['id' => $params['relation_id']])->value('owner_staff_id');
  53. } elseif ($params['relation_type'] == RecordModel::CONTACTS_TYPE) {//联系人
  54. $create_staff_id = Contacts::where(['id' => $params['relation_id']])->value('owner_staff_id');
  55. } elseif ($params['relation_type'] == RecordModel::CONTRACT_TYPE) {//合同
  56. $create_staff_id = Contract::where(['id' => $params['relation_id']])->value('owner_staff_id');
  57. }elseif ($params['relation_type'] == RecordModel::LEADS_TYPE) {//线索
  58. $create_staff_id = Leads::where(['id' => $params['relation_id']])->value('owner_staff_id');
  59. }elseif ($params['relation_type'] == RecordModel::BUSINESS_TYPE) {//
  60. $create_staff_id = Business::where(['id' => $params['relation_id']])->value('owner_staff_id');
  61. }
  62. if(empty($create_staff_id)){
  63. $create_staff_id = $staff->id;
  64. }
  65. if(isset($params['next_time'])){
  66. $params['next_time'] = $params['next_time'] ? : NULL;
  67. }
  68. $params['create_staff_id'] = $create_staff_id;
  69. $params['staff_id'] = $staff->id;
  70. $params['follow_time'] = date('Y-m-d H:i:s');
  71. $model = new self;
  72. $result = $model->allowField(true)->save($params);
  73. $lastId=$model->getLastInsID();
  74. if (false === $result) {
  75. // 验证失败 输出错误信息
  76. throw new Exception($model->getError());
  77. }
  78. if ($files) {
  79. RecordFile::addFiles($files, $lastId);
  80. }
  81. if ($params['relation_type'] == RecordModel::CUSTOMER_TYPE) {//客户
  82. if(isset($params['follow'])){
  83. $resCus['follow'] = $params['follow'];
  84. }
  85. if(isset($params['next_time'])){
  86. $resCus['next_time'] = $params['next_time'];
  87. }
  88. $resCus['last_time'] = date('Y-m-d H:i:s');
  89. //同步跟进状态
  90. Customer::where(['id' => $params['relation_id']])->update($resCus);
  91. if ($files) {
  92. CustomerFile::addFiles($files, $params['relation_id']);
  93. }
  94. } elseif ($params['relation_type'] == RecordModel::CONTACTS_TYPE && isset($params['next_time'])) {//联系人
  95. Contacts::where(['id' => $params['relation_id']])->update(['next_time' => $params['next_time']]);
  96. if ($files) {
  97. ContactsFile::addFiles($files, $params['relation_id']);
  98. }
  99. } elseif ($params['relation_type'] == RecordModel::CONTRACT_TYPE && isset($params['next_time'])) {//
  100. if ($files) {
  101. ContractFile::addFiles($files, $params['relation_id']);
  102. }
  103. }elseif ($params['relation_type'] == RecordModel::LEADS_TYPE && isset($params['next_time'])) {//
  104. Leads::where(['id' => $params['relation_id']])->update(['next_time' => $params['next_time']]);
  105. if ($files) {
  106. LeadsFile::addFiles($files, $params['relation_id']);
  107. }
  108. }elseif ($params['relation_type'] == RecordModel::BUSINESS_TYPE && isset($params['next_time'])) {//
  109. Business::where(['id' => $params['relation_id']])->update(['next_time' => $params['next_time']]);
  110. }
  111. if (isset($params['reminds_id'])) {//发送通知
  112. $staff_ids = explode(',', $params['reminds_id']);
  113. foreach ($staff_ids as $staff_id) {
  114. //发送通知
  115. Message::addMessage(Message::RECORD_TYPE, $lastId, $staff_id, $staff->id);
  116. }
  117. }
  118. return true;
  119. }
  120. //快速创建跟进记录
  121. public static function quickCreateRecord($type, $id, $content) {
  122. $follow_type = '其它';
  123. $staff=Staff::info();
  124. if(empty($staff)){
  125. return true;
  126. }
  127. $data = [
  128. 'relation_type' => $type,
  129. 'relation_id' => $id,
  130. 'content' => $content,
  131. 'follow_type' => $follow_type,
  132. 'follow_time' => date('Y-m-d H:i:s'),
  133. ];
  134. return self::createRecord($data);
  135. }
  136. public function read(){
  137. return $this->hasMany(RecordRead::class, 'record_id', 'id')->field('record_id,createtime');
  138. }
  139. //客户
  140. public function customer() {
  141. return $this->hasOne(Customer::class, 'id', 'relation_id')->field('id,name');
  142. }
  143. //线索
  144. public function leads() {
  145. return $this->hasOne(Leads::class, 'id', 'relation_id')->field('id,name');
  146. }
  147. //合同
  148. public function contract() {
  149. return $this->hasOne(Contract::class, 'id', 'relation_id')->field('id,name');
  150. }
  151. //商机
  152. public function business() {
  153. return $this->hasOne(Business::class, 'id', 'relation_id')->field('id,name');
  154. }
  155. }