Protector.php 18 KB

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