Record.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace addons\qingdong\model;
  3. use addons\qingdong\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 = 'qingdong_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 static function createRecord($params) {
  40. if(isset($params['files'])){
  41. $files = $params['files'];
  42. unset($params['files']);
  43. }else{
  44. $files='';
  45. }
  46. $staff = Staff::info();
  47. if ($params['relation_type'] == RecordModel::CUSTOMER_TYPE) {//客户
  48. // $create_staff_id = Customer::where(['id' => $params['relation_id']])->value('owner_staff_id');
  49. $create_staff_id = $staff->id;
  50. } elseif ($params['relation_type'] == RecordModel::CONTACTS_TYPE) {//联系人
  51. $create_staff_id = Contacts::where(['id' => $params['relation_id']])->value('owner_staff_id');
  52. } elseif ($params['relation_type'] == RecordModel::CONTRACT_TYPE) {//合同
  53. $create_staff_id = Contract::where(['id' => $params['relation_id']])->value('owner_staff_id');
  54. }elseif ($params['relation_type'] == RecordModel::LEADS_TYPE) {//线索
  55. $create_staff_id = Leads::where(['id' => $params['relation_id']])->value('owner_staff_id');
  56. }elseif ($params['relation_type'] == RecordModel::BUSINESS_TYPE) {//
  57. $create_staff_id = Business::where(['id' => $params['relation_id']])->value('owner_staff_id');
  58. }
  59. if(empty($create_staff_id)){
  60. $create_staff_id = $staff->id;
  61. }
  62. $params['create_staff_id'] = $create_staff_id;
  63. $params['staff_id'] = $staff->id;
  64. $params['follow_time'] = date('Y-m-d H:i:s');
  65. $model = new self;
  66. $result = $model->allowField(true)->save($params);
  67. $lastId=$model->getLastInsID();
  68. if (false === $result) {
  69. // 验证失败 输出错误信息
  70. throw new Exception($model->getError());
  71. }
  72. if ($files) {
  73. RecordFile::addFiles($files, $lastId);
  74. }
  75. if ($params['relation_type'] == RecordModel::CUSTOMER_TYPE && isset($params['follow'])) {//客户
  76. //同步跟进状态
  77. Customer::where(['id' => $params['relation_id']])->update(['follow' => $params['follow'],
  78. 'next_time' => $params['next_time'],'last_time'=>date('Y-m-d H:i:s')]);
  79. if ($files) {
  80. CustomerFile::addFiles($files, $params['relation_id']);
  81. }
  82. } elseif ($params['relation_type'] == RecordModel::CONTACTS_TYPE && isset($params['next_time'])) {//联系人
  83. Contacts::where(['id' => $params['relation_id']])->update(['next_time' => $params['next_time']]);
  84. if ($files) {
  85. ContactsFile::addFiles($files, $params['relation_id']);
  86. }
  87. } elseif ($params['relation_type'] == RecordModel::CONTRACT_TYPE && isset($params['next_time'])) {//
  88. if ($files) {
  89. ContractFile::addFiles($files, $params['relation_id']);
  90. }
  91. }elseif ($params['relation_type'] == RecordModel::LEADS_TYPE && isset($params['next_time'])) {//
  92. Leads::where(['id' => $params['relation_id']])->update(['next_time' => $params['next_time']]);
  93. if ($files) {
  94. LeadsFile::addFiles($files, $params['relation_id']);
  95. }
  96. }elseif ($params['relation_type'] == RecordModel::BUSINESS_TYPE && isset($params['next_time'])) {//
  97. Business::where(['id' => $params['relation_id']])->update(['next_time' => $params['next_time']]);
  98. }
  99. if (isset($params['reminds_id'])) {//发送通知
  100. $staff_ids = explode(',', $params['reminds_id']);
  101. foreach ($staff_ids as $staff_id) {
  102. //发送通知
  103. Message::addMessage(Message::RECORD_TYPE, $lastId, $staff_id, $staff->id);
  104. }
  105. }
  106. return true;
  107. }
  108. //快速创建跟进记录
  109. public static function quickCreateRecord($type, $id, $content) {
  110. $follow_type = '其它';
  111. $staff=Staff::info();
  112. if(empty($staff)){
  113. return true;
  114. }
  115. $data = [
  116. 'relation_type' => $type,
  117. 'relation_id' => $id,
  118. 'content' => $content,
  119. 'follow_type' => $follow_type,
  120. 'follow_time' => date('Y-m-d H:i:s'),
  121. ];
  122. return self::createRecord($data);
  123. }
  124. public function read(){
  125. return $this->hasMany(RecordRead::class, 'record_id', 'id')->field('record_id,createtime');
  126. }
  127. //客户
  128. public function customer() {
  129. return $this->hasOne(Customer::class, 'id', 'relation_id')->field('id,name');
  130. }
  131. //线索
  132. public function leads() {
  133. return $this->hasOne(Leads::class, 'id', 'relation_id')->field('id,name');
  134. }
  135. //合同
  136. public function contract() {
  137. return $this->hasOne(Contract::class, 'id', 'relation_id')->field('id,name');
  138. }
  139. }