Protector.php 18 KB

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