Protector.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 = ['sign','again','showfollow','addfollow'];
  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'); //保护时间
  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',strtotime("+".$protect_day." day")); //保护到期时间
  167. $params['free_date'] = date('Y-m-d',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 ($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() {
  284. try{
  285. //1.获取id
  286. $id = $this->request->post("id");
  287. //2.protector表 通过id 查找出数据
  288. $protector = \app\admin\model\Protector::get($id);
  289. //3.获取用户id
  290. $usr_id = Session::get('admin')['id'];
  291. //4.判断用户id和 protector的用户id是否一致 保存状态
  292. if ($protector['usr_id'] != $usr_id) {
  293. $this->error('请操作自己的数据!');
  294. }
  295. $param['id'] = $id;
  296. $param['status'] = $this->status['SIGN'];
  297. //5.入库
  298. $res = $this->model->allowField(true)->isUpdate(true)->save($param);
  299. if ($res) {
  300. $this->success('签单成功!');
  301. }
  302. $this->error();
  303. } catch (\think\exception\PDOException $e){
  304. $this->error($e->getMessage());
  305. }
  306. }
  307. /**
  308. * 再次保护
  309. */
  310. public function again() {
  311. try{
  312. //1.获取id
  313. $id = $this->request->post("id");
  314. //2.protector表 通过id 查找出数据
  315. $protector = \app\admin\model\Protector::get($id);
  316. //3.获取用户id
  317. $usr_id = Session::get('admin')['id'];
  318. //4.判断用户保护个数是否达到上限
  319. $res = \app\admin\model\Protector::proCountIsExceed($usr_id, Session::get('admin')['protect_count']);
  320. if($res){
  321. $this->error('用户保护人数已经达到上限('.Session::get('admin')['protect_count'].'个)');
  322. }
  323. //5 判断 protector表 状态 如果是已签单返回
  324. if($protector['status'] == $this->status['SIGN']){
  325. $this->error('已签单!无法再次保护');
  326. }
  327. //6 已过期,已放弃
  328. if($protector['status'] != $this->status['OVERDUE'] && $protector['status'] != $this->status['GIVEUP']){
  329. $this->error('已过期或已放弃才可以再次保护!');
  330. }
  331. //7.判断用户id和 protector的用户id是否一致 保
  332. if($protector['usr_id'] == $usr_id) {
  333. // 同一个人操作,判断是否结束冻结
  334. if(strtotime(date('Y-m-d')) < strtotime($protector['free_date'])){
  335. $this->error('未过冻结时间('.$protector['free_date'].')!无法再次保护');
  336. }
  337. }
  338. // 不同的人操作或者已经过了冻结期
  339. $protect_day = Session::get('admin')['protect_day']; //保护天数
  340. $free_day = Session::get('admin')['free_day']; // 冻结天数
  341. $params['usr_id'] = $usr_id; //usr id
  342. $params['usr_nickname'] = Session::get('admin')['nickname']; //nickname
  343. $params['pro_date'] = date('Y-m-d'); //保护时间
  344. $params['ex_date'] = date('Y-m-d',strtotime("+".$protect_day." day")); //保护到期时间
  345. $params['free_date'] = date('Y-m-d',strtotime("{$params['ex_date']} "."+".$free_day." day")); //保护到期时间
  346. $params['usr_depart'] = AuthGroupAccess::getGroupName($params['usr_id']);
  347. $params['id'] = $id;
  348. $params['status'] = $this->status['PROTECTOR'];
  349. //5.入库
  350. $res = $this->model->allowField(true)->isUpdate(true)->save($params);
  351. if ($res) {
  352. $this->success('再次保护成功!');
  353. }
  354. $this->error();
  355. } catch (\think\exception\PDOException $e){
  356. $this->error($e->getMessage());
  357. }
  358. }
  359. /**
  360. * 查看所有的跟进
  361. */
  362. public function showFollow()
  363. {
  364. try{
  365. $p_id = $this->request->post('id');
  366. $res = Db::table('follow')
  367. ->where('protect_id', $p_id)
  368. ->select();
  369. $html = '';
  370. if(!empty($res)){
  371. foreach ($res as $k=>$v){
  372. $html .=
  373. ' <div class="form-group col-xs-12">' .
  374. ' <label for="follow_time" class="control-label col-xs-12 col-sm-3"><span class="text-red"></span>操作人:</label>' .
  375. ' <div class="col-xs-12 col-sm-7">' .
  376. '<span style="display: inline-block;padding-top: 7px;">'.$v["usr_nickname"] .'</span>'.
  377. ' </div>' .
  378. ' </div>';
  379. $html .=
  380. ' <div class="form-group col-xs-12">' .
  381. ' <label for="follow_time" class="control-label col-xs-12 col-sm-3">跟进时间:</label>' .
  382. ' <div class="col-xs-12 col-sm-7">' .
  383. '<span style="display: inline-block;padding-top: 7px;">'.$v['follow_time'].'</span>'.
  384. ' </div>' .
  385. ' </div>';
  386. $html .=
  387. ' <div class="form-group col-xs-12" style="border-bottom: 1px solid #eeeeee;">' .
  388. ' <label class="control-label col-xs-12 col-sm-3">跟进内容:</label>' .
  389. ' <div class="col-xs-12 col-sm-7" style="margin-bottom: 10px;">' .
  390. ' <textarea disabled class="form-control " rows="5" name="main" cols="50">'.
  391. $v['main'].
  392. ' </textarea>' .
  393. ' </div>' .
  394. ' </div>';
  395. }
  396. }
  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. ' <input id="follow_time" onclick="showDatetime()" class="form-control datetimepicker" name="follow_time" type="text" data-rule="required">' .
  402. ' </div>' .
  403. ' </div>';
  404. $html .=
  405. ' <div class="form-group col-xs-12">' .
  406. ' <label class="control-label col-xs-12 col-sm-3">跟进内容:</label>' .
  407. ' <div class="col-xs-12 col-sm-7">' .
  408. ' <textarea id="main_text" class="form-control " rows="5" name="main" cols="50">'.
  409. ' </textarea>' .
  410. ' </div>' .
  411. ' </div>';
  412. $this->success('','',$html);
  413. } catch (\think\exception\PDOException $e){
  414. $this->error($e->getMessage());
  415. }
  416. }
  417. /**
  418. * 添加跟进
  419. */
  420. public function addFollow()
  421. {
  422. try{
  423. $data = $this->request->post();
  424. $param['protect_id'] = $data['id'];
  425. $param['usr_id'] = Session::get('admin')['id'];
  426. $param['usr_nickname'] = Session::get('admin')['nickname'];
  427. $param['protect_name'] = $data['unit_name'];
  428. $param['follow_time'] = $data['follow_time'];
  429. $param['main'] = $data['main'];
  430. $param['created_at'] = date('Y-m-d H:i:s');
  431. $res = Db::table('follow')
  432. ->insert($param);
  433. if($res){
  434. $this->success('跟进成功');
  435. }
  436. $this->error();
  437. } catch (\think\exception\PDOException $e){
  438. $this->error($e->getMessage());
  439. }
  440. }
  441. }