Protector.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\AuthGroup;
  4. use app\admin\model\AuthGroupAccess;
  5. use app\common\controller\Backend;
  6. use think\Db;
  7. use think\Session;
  8. /**
  9. *
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Protector extends Backend
  14. {
  15. private $status = [
  16. "PROTECTOR" => 0 , //保护中
  17. "OVERDUE" => 1, // 已过期
  18. "SIGN" => 2, // 已签单
  19. "FREE" => 3, // 冻结中
  20. "GIVEUP" => 4, // 已放弃
  21. ];
  22. //快速搜索的字段
  23. protected $searchFields = ['unit_name','contacts'];
  24. /**
  25. * 无需鉴权的方法,但需要登录
  26. * @var array
  27. */
  28. protected $noNeedRight = ['again','giveup','showfollow','addfollow','testingname','allsearch'];
  29. /**
  30. * Protector模型对象
  31. * @var \app\admin\model\Protector
  32. */
  33. protected $model = null;
  34. public function _initialize()
  35. {
  36. parent::_initialize();
  37. $this->model = model('Protector');
  38. }
  39. /**
  40. * 查看
  41. */
  42. public function index($status = false)
  43. {
  44. //设置过滤方法
  45. $this->request->filter(['strip_tags']);
  46. if ($this->request->isAjax())
  47. {
  48. // 先统一处理过期保护客户
  49. \app\admin\model\Protector::updateOverdue();
  50. //如果发送的来源是Selectpage,则转发到Selectpage
  51. if ($this->request->request('keyField'))
  52. {
  53. return $this->selectpage();
  54. }
  55. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  56. $where_usr_id = \app\admin\model\Protector::getGroupWhere();
  57. switch ($status){
  58. case 'protect': //保护中
  59. $total = $this->model
  60. ->where('status',$this->status['PROTECTOR'])
  61. ->where($where_usr_id)
  62. ->where($where)
  63. ->order($sort, $order)
  64. ->count();
  65. $list = $this->model
  66. ->where('status',$this->status['PROTECTOR'])
  67. ->where($where_usr_id)
  68. ->where($where)
  69. ->order($sort, $order)
  70. ->limit($offset, $limit)
  71. ->select();
  72. break;
  73. case 'overdue': //已过期
  74. $total = $this->model
  75. ->where('status',$this->status['OVERDUE'])
  76. ->where($where)
  77. ->order($sort, $order)
  78. ->count();
  79. $list = $this->model
  80. ->where('status',$this->status['OVERDUE'])
  81. ->where($where)
  82. ->order($sort, $order)
  83. ->limit($offset, $limit)
  84. ->select();
  85. break;
  86. case 'contract': //已签单
  87. $total = $this->model
  88. ->where('status',$this->status['SIGN'])
  89. ->where($where_usr_id)
  90. ->where($where)
  91. ->order($sort, $order)
  92. ->count();
  93. $list = $this->model
  94. ->where('status',$this->status['SIGN'])
  95. ->where($where_usr_id)
  96. ->where($where)
  97. ->order($sort, $order)
  98. ->limit($offset, $limit)
  99. ->select();
  100. break;
  101. case 'giveup': //已放弃
  102. $total = $this->model
  103. ->where('status',$this->status['GIVEUP'])
  104. ->where($where_usr_id)
  105. ->where($where)
  106. ->order($sort, $order)
  107. ->count();
  108. $list = $this->model
  109. ->where('status',$this->status['GIVEUP'])
  110. ->where($where_usr_id)
  111. ->where($where)
  112. ->order($sort, $order)
  113. ->limit($offset, $limit)
  114. ->select();
  115. break;
  116. }
  117. $list = collection($list)->toArray();
  118. $result = array("total" => $total, "rows" => $list);
  119. return json($result);
  120. }
  121. return $this->view->fetch('index');
  122. }
  123. /**
  124. * 添加
  125. */
  126. public function add()
  127. {
  128. if ($this->request->isPost())
  129. {
  130. $params = $this->request->post("row/a");
  131. if ($params)
  132. {
  133. if ($this->dataLimit && $this->dataLimitFieldAutoFill)
  134. {
  135. $params[$this->dataLimitField] = $this->auth->id;
  136. }
  137. try
  138. {
  139. //是否采用模型验证
  140. if ($this->modelValidate)
  141. {
  142. $name = basename(str_replace('\\', '/', get_class($this->model)));
  143. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  144. $this->model->validate($validate);
  145. }
  146. $params['usr_id'] = Session::get('admin')['id']; //usr id
  147. $params['usr_nickname'] = Session::get('admin')['nickname']; //nickname
  148. $protect_count = Session::get('admin')['protect_count']; //保护个数
  149. $protect_day = Session::get('admin')['protect_day']; //保护天数
  150. $free_day = Session::get('admin')['free_day']; // 冻结天数
  151. $params['pro_date'] = date('Y-m-d H:i:s'); //保护时间
  152. $params['total'] = ($params['number'] * $params['price']); //总价
  153. // 获取所属组名,id
  154. $params['usr_depart'] = Session::get('admin')['usr_depart']; //获取所属组名
  155. $params['depart_id'] = Session::get('admin')['depart_id']; //获取所属组id
  156. // 获取到期时间和冻结到期时间
  157. $params['ex_date'] = date('Y-m-d H:i:s',strtotime("+".$protect_day." day")); //保护到期时间
  158. $params['free_date'] = date('Y-m-d H:i:s',strtotime("{$params['ex_date']} "."+".$free_day." day")); //保护到期时间
  159. // 根据单位名称判断是否已经存在(已被保护)
  160. $res = \app\admin\model\Protector::unitNameIsExist($params['unit_name']);
  161. if($res){
  162. $this->error($res);
  163. }
  164. // 判断是否超出保护个数
  165. $res = \app\admin\model\Protector::proCountIsExceed($params['usr_id'], $protect_count);
  166. if($res){
  167. $this->error($res);
  168. }
  169. $result = $this->model->allowField(true)->save($params);
  170. if ($result !== false)
  171. {
  172. $this->success();
  173. }
  174. else
  175. {
  176. $this->error($this->model->getError());
  177. }
  178. }
  179. catch (\think\exception\PDOException $e)
  180. {
  181. $this->error($e->getMessage());
  182. }
  183. }
  184. $this->error(__('Parameter %s can not be empty', ''));
  185. }
  186. return $this->view->fetch();
  187. }
  188. /**
  189. * 编辑
  190. */
  191. public function edit($ids = NULL)
  192. {
  193. $row = $this->model->get($ids);
  194. if (!$row)
  195. $this->error(__('No Results were found'));
  196. $adminIds = $this->getDataLimitAdminIds();
  197. if (is_array($adminIds))
  198. {
  199. if (!in_array($row[$this->dataLimitField], $adminIds))
  200. {
  201. $this->error(__('You have no permission'));
  202. }
  203. }
  204. if ($this->request->isPost())
  205. {
  206. $params = $this->request->post("row/a");
  207. if ($params)
  208. {
  209. try
  210. {
  211. //是否采用模型验证
  212. if ($this->modelValidate)
  213. {
  214. $name = basename(str_replace('\\', '/', get_class($this->model)));
  215. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  216. $row->validate($validate);
  217. }
  218. $params['total'] = ($params['number'] * $params['price']); //总价
  219. $result = $row->allowField(true)->save($params);
  220. if ($result !== false)
  221. {
  222. $this->success();
  223. }
  224. else
  225. {
  226. $this->error($row->getError());
  227. }
  228. }
  229. catch (\think\exception\PDOException $e)
  230. {
  231. $this->error($e->getMessage());
  232. }
  233. }
  234. $this->error(__('Parameter %s can not be empty', ''));
  235. }
  236. $this->view->assign("row", $row);
  237. return $this->view->fetch();
  238. }
  239. /**
  240. * 放弃
  241. */
  242. public function giveup() {
  243. try {
  244. //1.获取id
  245. $id = $this->request->post("id");
  246. //2.protector表 通过id 查找出数据
  247. $protector = \app\admin\model\Protector::get($id);
  248. //3.获取用户id
  249. $usr_id = Session::get('admin')['id'];
  250. //4.判断用户id和 protector的用户id是否一致 保存状态
  251. if ($usr_id != 1 && $protector['usr_id'] != $usr_id) {
  252. $this->error('请操作自己的数据!');
  253. }
  254. //5.保护到期时间设置现在 冻结算一下
  255. $free_day = Session::get('admin')['free_day'];
  256. $param['ex_date'] = date('Y-m-d');
  257. $param['free_date'] = date('Y-m-d', strtotime("+" . $free_day . " day"));
  258. $param['status'] = $this->status['GIVEUP'];
  259. $param['id'] = $id;
  260. //6.入库
  261. $res = $this->model->allowField(true)->isUpdate(true)->save($param);
  262. if ($res) {
  263. $this->success('放弃成功!');
  264. }
  265. $this->error();
  266. } catch (\think\exception\PDOException $e)
  267. {
  268. $this->error($e->getMessage());
  269. }
  270. }
  271. /**
  272. * 签单
  273. */
  274. public function sign($ids = NULL) {
  275. //1.protector表 通过id 查找出数据
  276. $protector = \app\admin\model\Protector::get($ids);
  277. //2.获取用户id
  278. $usr_id = Session::get('admin')['id'];
  279. //3.判断用户id和 protector的用户id是否一致 保存状态
  280. // if($usr_id != 1){
  281. // $this->error('只有管理员可以签单!');
  282. // }
  283. // if ($usr_id != 1 && $protector['usr_id'] != $usr_id) {
  284. // $this->error('请操作自己的数据!');
  285. // }
  286. if($_POST){
  287. $data = $this->request->post();
  288. $param['status'] = $this->status['SIGN'];
  289. $param['again_date'] = $data['row']['again_date'];
  290. $res = Db::table('protector')->where('id',$data['id'])->update($param);
  291. if($res){
  292. $this->success();
  293. }else{
  294. $this->error('数据修改失败');
  295. }
  296. }
  297. $this->view->assign("id", $ids);
  298. return $this->view->fetch();
  299. }
  300. /**
  301. * 再次保护
  302. */
  303. public function again() {
  304. try{
  305. //1.获取id
  306. $id = $this->request->post("id");
  307. //2.protector表 通过id 查找出数据
  308. $protector = \app\admin\model\Protector::get($id);
  309. //3.获取用户id
  310. $usr_id = Session::get('admin')['id'];
  311. //4.判断用户保护个数是否达到上限
  312. $res = \app\admin\model\Protector::proCountIsExceed($usr_id, Session::get('admin')['protect_count']);
  313. if($res){
  314. $this->error('用户保护人数已经达到上限('.Session::get('admin')['protect_count'].'个)');
  315. }
  316. //5 判断 protector表 状态 如果是已签单返回
  317. if($protector['status'] == $this->status['SIGN']){
  318. $this->error('已签单!无法再次保护');
  319. }
  320. //6 已过期,已放弃
  321. if($protector['status'] != $this->status['OVERDUE'] && $protector['status'] != $this->status['GIVEUP']){
  322. $this->error('已过期或已放弃才可以再次保护!');
  323. }
  324. //7.判断用户id和 protector的用户id是否一致 保
  325. if($protector['usr_id'] == $usr_id) {
  326. // 同一个人操作,判断是否结束冻结
  327. if(strtotime(date('Y-m-d')) < strtotime($protector['free_date'])){
  328. $this->error('未过冻结时间('.$protector['free_date'].')!无法再次保护');
  329. }
  330. }
  331. // 不同的人操作或者已经过了冻结期
  332. $protect_day = Session::get('admin')['protect_day']; //保护天数
  333. $free_day = Session::get('admin')['free_day']; // 冻结天数
  334. $params['usr_id'] = $usr_id; //usr id
  335. $params['usr_nickname'] = Session::get('admin')['nickname']; //nickname
  336. $params['pro_date'] = date('Y-m-d'); //保护时间
  337. $params['ex_date'] = date('Y-m-d',strtotime("+".$protect_day." day")); //保护到期时间
  338. $params['free_date'] = date('Y-m-d',strtotime("{$params['ex_date']} "."+".$free_day." day")); //保护到期时间
  339. $params['usr_depart'] = AuthGroupAccess::getGroupName($params['usr_id']);
  340. $params['id'] = $id;
  341. $params['status'] = $this->status['PROTECTOR'];
  342. //5.入库
  343. $res = $this->model->allowField(true)->isUpdate(true)->save($params);
  344. if ($res) {
  345. $this->success('再次保护成功!');
  346. }
  347. $this->error();
  348. } catch (\think\exception\PDOException $e){
  349. $this->error($e->getMessage());
  350. }
  351. }
  352. public function follow($ids = NULL){
  353. $info = Db::table('follow')->where('type',0)->where('protect_id',$ids)->field('created_at','main')->select();
  354. if($_POST){
  355. $data = $this->request->post();
  356. $unit = Db::table('protector')->where('id',$data['id'])->field('unit_name')->find();
  357. $param['protect_id'] = $data['id'];
  358. $param['usr_id'] = Session::get('admin')['id'];
  359. $param['usr_nickname'] = $data['nickname'];
  360. $param['protect_name'] = $unit['unit_name'];
  361. $param['follow_time'] = $data['row']['follow_time'];
  362. $param['main'] = $data['row']['main'];
  363. $param['created_at'] = date('Y-m-d H:i:s');
  364. $param['type'] = 0;
  365. $res = Db::table('follow')->insert($param);
  366. if($res){
  367. $this->success();
  368. }else{
  369. $this->error('数据存储失败');
  370. }
  371. }
  372. $this->view->assign("info", $info);
  373. $this->view->assign("id", $ids);
  374. $this->view->assign("nickname", Session::get('admin')['nickname']);
  375. return $this->view->fetch();
  376. }
  377. /**
  378. * 查看所有的跟进
  379. */
  380. public function showFollow()
  381. {
  382. try{
  383. $p_id = $this->request->post('id');
  384. $res = Db::table('follow')
  385. ->where('protect_id', $p_id)
  386. ->select();
  387. $html = '';
  388. if(!empty($res)){
  389. foreach ($res as $k=>$v){
  390. $html .=
  391. ' <div class="form-group col-xs-12">' .
  392. ' <label for="follow_time" class="control-label col-xs-12 col-sm-3"><span class="text-red"></span>操作人:</label>' .
  393. ' <div class="col-xs-12 col-sm-7">' .
  394. '<span style="display: inline-block;padding-top: 7px;">'.$v["usr_nickname"] .'</span>'.
  395. ' </div>' .
  396. ' </div>';
  397. $html .=
  398. ' <div class="form-group col-xs-12">' .
  399. ' <label for="follow_time" class="control-label col-xs-12 col-sm-3">跟进时间:</label>' .
  400. ' <div class="col-xs-12 col-sm-7">' .
  401. '<span style="display: inline-block;padding-top: 7px;">'.$v['follow_time'].'</span>'.
  402. ' </div>' .
  403. ' </div>';
  404. $html .=
  405. ' <div class="form-group col-xs-12" style="border-bottom: 1px solid #eeeeee;">' .
  406. ' <label class="control-label col-xs-12 col-sm-3">跟进内容:</label>' .
  407. ' <div class="col-xs-12 col-sm-7" style="margin-bottom: 10px;">' .
  408. ' <textarea disabled class="form-control " rows="5" name="main" cols="50">'.
  409. $v['main'].
  410. ' </textarea>' .
  411. ' </div>' .
  412. ' </div>';
  413. }
  414. }
  415. $html .=
  416. ' <div class="form-group col-xs-12">' .
  417. ' <label for="follow_time" class="control-label col-xs-12 col-sm-3">跟进时间:</label>' .
  418. ' <div class="col-xs-12 col-sm-7">' .
  419. ' <input id="follow_time" class="form-control datetimepicker" name="follow_time" type="text" data-rule="required">' .
  420. ' </div>' .
  421. ' </div>';
  422. $html .=
  423. ' <div class="form-group col-xs-12">' .
  424. ' <label class="control-label col-xs-12 col-sm-3">跟进内容:</label>' .
  425. ' <div class="col-xs-12 col-sm-7">' .
  426. ' <textarea id="main_text" class="form-control " rows="5" name="main" cols="50">'.
  427. ' </textarea>' .
  428. ' </div>' .
  429. ' </div>';
  430. $this->success('','',$html);
  431. } catch (\think\exception\PDOException $e){
  432. $this->error($e->getMessage());
  433. }
  434. }
  435. /**
  436. * 添加跟进
  437. */
  438. public function addFollow()
  439. {
  440. try{
  441. $data = $this->request->post();
  442. $param['protect_id'] = $data['id'];
  443. $param['usr_id'] = Session::get('admin')['id'];
  444. $param['usr_nickname'] = Session::get('admin')['nickname'];
  445. $param['protect_name'] = $data['unit_name'];
  446. $param['follow_time'] = $data['follow_time'];
  447. $param['main'] = $data['main'];
  448. $param['created_at'] = date('Y-m-d H:i:s');
  449. $res = Db::table('follow')
  450. ->insert($param);
  451. if($res){
  452. $this->success('跟进成功');
  453. }
  454. $this->error();
  455. } catch (\think\exception\PDOException $e){
  456. $this->error($e->getMessage());
  457. }
  458. }
  459. /**
  460. * 测试是否可以添加的单位
  461. * @throws \think\db\exception\DataNotFoundException
  462. * @throws \think\db\exception\ModelNotFoundException
  463. * @throws \think\exception\DbException
  464. */
  465. public function TestingName()
  466. {
  467. $unit_name = $this->request->post('unit_name');
  468. $res = \app\admin\model\Protector::unitNameIsExist($unit_name);
  469. if($res){
  470. $this->error($res);
  471. }
  472. $this->success('可以添加的单位名称!');
  473. }
  474. /**
  475. * 全系统搜索
  476. * @return string
  477. * @throws \think\Exception
  478. * @throws \think\db\exception\DataNotFoundException
  479. * @throws \think\db\exception\ModelNotFoundException
  480. * @throws \think\exception\DbException
  481. */
  482. public function allSearch()
  483. {
  484. $val = $this->request->post('val');
  485. $list = $this->model
  486. ->where('binary `unit_name` LIKE \'%'. $val .'%\'')
  487. ->select();
  488. foreach ($list as $k=>&$v){
  489. $v['unit_name'] = \app\admin\model\Protector::formatUnitName($v['unit_name'],$val);
  490. }
  491. $this->assign('list',$list);
  492. return $this->view->fetch('all_search');
  493. }
  494. }