Protector.php 20 KB

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