Record.php 5.5 KB

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