Record.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace app\admin\controller\qingdongams\customer;
  3. use addons\qingdongams\model\Field;
  4. use addons\qingdongams\model\File;
  5. use addons\qingdongams\model\Message;
  6. use addons\qingdongams\model\RecordRead;
  7. use addons\qingdongams\model\Remind;
  8. use addons\qingdongams\model\Staff;
  9. use addons\qingdongams\model\Customer;
  10. use app\admin\controller\qingdongams\Base;
  11. use addons\qingdongams\model\Contacts;
  12. use addons\qingdongams\model\Comment;
  13. use addons\qingdongams\model\Record as RecordModel;
  14. use think\DB;
  15. use think\Exception;
  16. /**
  17. * 客户跟进记录
  18. */
  19. class Record extends Base
  20. {
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new RecordModel();
  25. // 获取跟进类型
  26. $record_type = Field::getField('跟进类型');
  27. $this->assignconfig('recordType', $record_type);
  28. }
  29. /**
  30. * 客户跟进记录
  31. */
  32. public function index()
  33. {
  34. $need = input('need', '', 'trim');
  35. $customer_id = input('customer_id', '', 'trim');
  36. $contacts_id = input('contacts_id', '', 'trim');
  37. $leads_id = input('leads_id', '', 'trim');
  38. $contract_id = input('contract_id', '', 'trim');
  39. $busniess_id = input('busniess_id', '', 'trim');
  40. //0:全部 1:我负责的 2:下属负责的 3:今日待跟进 4:今日已跟进
  41. $type = input('type', 0);
  42. $this->request->filter(['strip_tags']);
  43. if ($this->request->isAjax()) {
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. switch ($type) {
  46. case 1:
  47. $staff = Staff::info();
  48. $wheres['create_staff_id'] = $staff->id;
  49. break;
  50. case 2:
  51. $wheres['create_staff_id'] = array('in', Staff::getLowerStaffId());
  52. break;
  53. case 3:
  54. $start = date('Y-m-d 00:00:00');
  55. $end = date('Y-m-d 23:59:59');
  56. $record = collection(RecordModel::where(array('relation_type' => 1, 'next_time' => array(array('egt', $start), array('elt', $end))))->field("id,relation_id")->select())->toArray();
  57. $relationId = [];
  58. foreach ($record as $k => $v) {
  59. $whereRe['id'] = array('gt', $v['id']);
  60. $whereRe['relation_id'] = $v['relation_id'];
  61. $recordData = RecordModel::where($whereRe)->count();
  62. if ($recordData == 0) {
  63. $relationId[] = $v['id'];
  64. }
  65. }
  66. $wheres['id'] = array('in', $relationId);
  67. $staff = Staff::info();
  68. $wheres['create_staff_id'] = $staff->id;
  69. break;
  70. case 4:
  71. $start = date('Y-m-d 00:00:00');
  72. $end = date('Y-m-d 23:59:59');
  73. $record = collection(RecordModel::where(array('relation_type' => 1, 'next_time' => array(array('egt', $start), array('elt', $end))))->field("id,relation_id")->select())->toArray();
  74. $relationId = [];
  75. foreach ($record as $k => $v) {
  76. $whereRe['id'] = array('gt', $v['id']);
  77. $whereRe['relation_id'] = $v['relation_id'];
  78. $recordData = RecordModel::where($whereRe)->count();
  79. if ($recordData >= 1) {
  80. $relationId[] = $v['id'];
  81. }
  82. }
  83. $wheres['id'] = array('in', $relationId);
  84. $staff = Staff::info();
  85. $wheres['create_staff_id'] = $staff->id;
  86. break;
  87. default:
  88. $wheres['create_staff_id'] = array('in', Staff::getMyStaffIds());
  89. break;
  90. }
  91. $wheres['relation_type'] = 1;
  92. $wheres['follow_type'] = ['neq', '其它'];
  93. if (isset($need) && $need == 'customer') {
  94. $staff = Staff::info();
  95. $wheres['create_staff_id'] = $staff->id;
  96. $wheres['status'] = 0;
  97. $wheres['next_time'] = array(array('egt', date('Y-m-d 00:00:00')), array('lt', date('Y-m-d 23:59:59')));
  98. $customerlist = $this->model->where($wheres)->column('relation_id');
  99. if ($customerlist) {
  100. $whereExit['relation_id'] = array('in', $customerlist);
  101. $whereExit['next_time'] = array('gt', date('Y-m-d 23:59:59'));
  102. $recordIds = $this->model->where($whereExit)->column('id');
  103. if ($recordIds) {
  104. $wheres['id'] = array('in', $recordIds);
  105. }
  106. }
  107. }
  108. if (isset($need) && $need == 'contacts') {
  109. $staff = Staff::info();
  110. $wheres['create_staff_id'] = $staff->id;
  111. $wheres['status'] = 0;
  112. $wheres['relation_type'] = 2;
  113. $wheres['next_time'] = array(array('egt', date('Y-m-d 00:00:00')), array('lt', date('Y-m-d 23:59:59')));
  114. $customerlist = $this->model->where($wheres)->column('relation_id');
  115. if ($customerlist) {
  116. $whereExit['relation_id'] = array('in', $customerlist);
  117. $whereExit['next_time'] = array('gt', date('Y-m-d 23:59:59'));
  118. $recordIds = $this->model->where($whereExit)->column('id');
  119. if ($recordIds) {
  120. $wheres['id'] = array('in', $recordIds);
  121. }
  122. }
  123. }
  124. if (isset($customer_id) && $customer_id) {
  125. $wheres['relation_type'] = 1;
  126. $wheres['relation_id'] = $customer_id;
  127. unset($wheres['create_staff_id']);
  128. }
  129. if (isset($contacts_id) && $contacts_id) {
  130. $wheres['relation_type'] = 2;
  131. $wheres['relation_id'] = $contacts_id;
  132. unset($wheres['create_staff_id']);
  133. }
  134. if (isset($leads_id) && $leads_id) {
  135. $wheres['relation_type'] = 4;
  136. $wheres['relation_id'] = $leads_id;
  137. unset($wheres['create_staff_id']);
  138. }
  139. if (isset($contract_id) && $contract_id) {
  140. $wheres['relation_type'] = 3;
  141. $wheres['relation_id'] = $contract_id;
  142. unset($wheres['create_staff_id']);
  143. }
  144. if (isset($busniess_id) && $busniess_id) {
  145. $wheres['relation_type'] = 5;
  146. $wheres['relation_id'] = $busniess_id;
  147. unset($wheres['create_staff_id']);
  148. }
  149. $list = $this->model->where($where)->where($wheres)->with(['staff', 'customer'])->order($sort, $order)->paginate($limit);
  150. foreach ($list as $k => $v) {
  151. if (!isset($v['customer']) || !$v['customer']) {
  152. $v['customer'] = array('id' => '', 'name' => '');
  153. }
  154. }
  155. $row = $list->items();
  156. $result = array("total" => $list->total(), "rows" => $row);
  157. return json($result);
  158. }
  159. // 获取跟进类型
  160. $typeList = Field::where('name', '跟进类型')->value('data');
  161. $typeList = json_decode($typeList, true);
  162. $recordTypeList = [];
  163. foreach ($typeList as $value) {
  164. $recordTypeList[$value] = $value;
  165. }
  166. $this->assignconfig('recordTypeList', $recordTypeList);
  167. return $this->view->fetch();
  168. }
  169. /**
  170. * 添加跟进
  171. */
  172. public function add($ids = null)
  173. {
  174. $relation_type = input('relation_type', 1);
  175. if ($this->request->isPost()) {
  176. $params = $this->request->post('row/a');
  177. // 表单验证
  178. if (($result = $this->qingdongamsValidate($params, 'Record', 'create')) !== true) {
  179. $this->error($result);
  180. }
  181. if (!empty($params['files'])) {
  182. $params['files'] = File::getId($params['files']);
  183. }
  184. $reminds_id = $this->request->post('reminds_id/a');
  185. $params['reminds_id'] = '';
  186. if ($reminds_id) {
  187. $params['reminds_id'] = implode(',', $reminds_id);
  188. }
  189. Db::startTrans();
  190. try {
  191. $result = $this->model::createRecord($params);
  192. Db::commit();
  193. } catch (Exception $e) {
  194. Db::rollback();
  195. $this->error($e->getMessage());
  196. }
  197. if ($result) {
  198. $this->success('创建跟进记录成功');
  199. }
  200. $this->error('创建失败');
  201. }
  202. $follow = Field::getField('客户状态');
  203. $customer = Customer::getList();
  204. $customers = [];
  205. foreach ($customer as $k => $v) {
  206. $customers[$v['id']] = $v['name'];
  207. }
  208. if ($relation_type == 2) {
  209. $contacts = Contacts::where(['id' => $ids])->column('id,name');
  210. $this->assign('contacts', $contacts);
  211. }
  212. $remind = Remind::where(['type' => Remind::RECORD_TYPE])->find();
  213. $staff_ids = $remind['staff_ids'] ?? '';
  214. $this->assign('staff_ids', $staff_ids);
  215. $this->assign('ids', $ids);
  216. $this->assign('relation_type', $relation_type);
  217. $this->assign('customer', $customers);
  218. $this->assign('follow', $follow);
  219. $this->assign('staff', Staff::where([])->column('name', 'id'));
  220. return $this->view->fetch();
  221. }
  222. /**
  223. * 跟进详情
  224. */
  225. public function detail($ids = null)
  226. {
  227. $row = $this->model->with(['staff', 'file'])->where([
  228. 'id' => $ids,
  229. ])->find();
  230. if (empty($row)) {
  231. $this->error(__('No Results were found'));
  232. }
  233. $row = $row->toArray();
  234. if ($row['staff_id']) {
  235. $createname = Staff::where(['id' => $row['staff_id']])->value('name');
  236. } else {
  237. $createname = isset($row['staff']['name']) ? $row['staff']['name'] : '';
  238. }
  239. //标记通知已读
  240. Message::setRead(Message::RECORD_TYPE, $ids, $this->_staff->id);
  241. //添加阅读记录
  242. RecordRead::addRead($ids, $this->_staff->id);
  243. $row['createname'] = $createname;
  244. $this->assign('row', $row);
  245. $this->assignconfig("idinfo", ['id' => $ids]);
  246. return $this->view->fetch();
  247. }
  248. /**
  249. * 评论
  250. */
  251. public function comment()
  252. {
  253. $this->request->filter(['strip_tags']);
  254. if ($this->request->isAjax()) {
  255. $record_id = input('record_id', '');
  256. $wheres['relation_id'] = $record_id;
  257. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  258. $list = Comment::where($where)->where($wheres)->with(['staff', 'record'])->order($sort, $order)->paginate($limit);
  259. $row = $list->items();
  260. $result = array("total" => $list->total(), "rows" => $row);
  261. return json($result);
  262. }
  263. return $this->view->fetch();
  264. }
  265. }