Protector.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\AuthGroupAccess;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\Session;
  7. /**
  8. *
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Protector extends Backend
  13. {
  14. private $status = [
  15. "PROTECTOR" => 0 , //保护中
  16. "OVERDUE" => 1, // 已过期
  17. "SIGN" => 2, // 已签单
  18. "FREE" => 3, // 冻结中
  19. "GIVEUP" => 4, // 已放弃
  20. ];
  21. //快速搜索的字段
  22. protected $searchFields = ['unit_name','contacts'];
  23. /**
  24. * Protector模型对象
  25. * @var \app\admin\model\Protector
  26. */
  27. protected $model = null;
  28. public function _initialize()
  29. {
  30. parent::_initialize();
  31. $this->model = model('Protector');
  32. }
  33. /**
  34. * 查看
  35. */
  36. public function index($status = false)
  37. {
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags']);
  40. if ($this->request->isAjax())
  41. {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField'))
  44. {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $where_usr_id = ['usr_id'=>Session::get('admin')['id']]; //只能查看自己的数据
  49. switch ($status){
  50. case 'protect': //保护中
  51. $total = $this->model
  52. ->where('status',$this->status['PROTECTOR'])
  53. ->where($where_usr_id)
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->count();
  57. $list = $this->model
  58. ->where('status',$this->status['PROTECTOR'])
  59. ->where($where_usr_id)
  60. ->where($where)
  61. ->order($sort, $order)
  62. ->limit($offset, $limit)
  63. ->select();
  64. break;
  65. case 'overdue': //已过期
  66. $total = $this->model
  67. ->where('status',$this->status['OVERDUE'])
  68. ->where($where_usr_id)
  69. ->where($where)
  70. ->order($sort, $order)
  71. ->count();
  72. $list = $this->model
  73. ->where('status',$this->status['OVERDUE'])
  74. ->where($where_usr_id)
  75. ->where($where)
  76. ->order($sort, $order)
  77. ->limit($offset, $limit)
  78. ->select();
  79. break;
  80. case 'contract': //已签单
  81. $total = $this->model
  82. ->where('status',$this->status['SIGN'])
  83. ->where($where_usr_id)
  84. ->where($where)
  85. ->order($sort, $order)
  86. ->count();
  87. $list = $this->model
  88. ->where('status',$this->status['SIGN'])
  89. ->where($where_usr_id)
  90. ->where($where)
  91. ->order($sort, $order)
  92. ->limit($offset, $limit)
  93. ->select();
  94. break;
  95. case 'giveup': //已放弃
  96. $total = $this->model
  97. ->where('status',$this->status['GIVEUP'])
  98. ->where($where_usr_id)
  99. ->where($where)
  100. ->order($sort, $order)
  101. ->count();
  102. $list = $this->model
  103. ->where('status',$this->status['GIVEUP'])
  104. ->where($where_usr_id)
  105. ->where($where)
  106. ->order($sort, $order)
  107. ->limit($offset, $limit)
  108. ->select();
  109. break;
  110. }
  111. $list = collection($list)->toArray();
  112. $result = array("total" => $total, "rows" => $list);
  113. return json($result);
  114. }
  115. return $this->view->fetch('index');
  116. }
  117. /**
  118. * 添加
  119. */
  120. public function add()
  121. {
  122. if ($this->request->isPost())
  123. {
  124. $params = $this->request->post("row/a");
  125. if ($params)
  126. {
  127. if ($this->dataLimit && $this->dataLimitFieldAutoFill)
  128. {
  129. $params[$this->dataLimitField] = $this->auth->id;
  130. }
  131. try
  132. {
  133. //是否采用模型验证
  134. if ($this->modelValidate)
  135. {
  136. $name = basename(str_replace('\\', '/', get_class($this->model)));
  137. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  138. $this->model->validate($validate);
  139. }
  140. $params['usr_id'] = Session::get('admin')['id']; //usr id
  141. $params['usr_nickname'] = Session::get('admin')['nickname']; //nickname
  142. $protect_count = Session::get('admin')['protect_count']; //保护个数
  143. $protect_day = Session::get('admin')['protect_day']; //保护天数
  144. $free_day = Session::get('admin')['free_day']; // 冻结天数
  145. // 获取所属组名,id
  146. $params['usr_depart'] = Session::get('admin')['usr_depart']; //获取所属组名
  147. $params['depart_id'] = Session::get('admin')['depart_id']; //获取所属组id
  148. // 获取到期时间和冻结到期时间
  149. $params['ex_date'] = date('Y-m-d',strtotime("+".$protect_day." day")); //保护到期时间
  150. $params['free_date'] = date('Y-m-d',strtotime("{$params['ex_date']} "."+".$free_day." day")); //保护到期时间
  151. // 根据单位名称判断是否已经存在(已被保护)
  152. $res = \app\admin\model\Protector::unitNameIsExist($params['unit_name']);
  153. if($res){
  154. $this->error('"'.$res.'"已经被保护!');
  155. }
  156. // 判断是否超出保护个数
  157. $res = \app\admin\model\Protector::proCountIsExceed($params['usr_id'], $protect_count);
  158. if($res){
  159. $this->error($res);
  160. }
  161. $result = $this->model->allowField(true)->save($params);
  162. if ($result !== false)
  163. {
  164. $this->success();
  165. }
  166. else
  167. {
  168. $this->error($this->model->getError());
  169. }
  170. }
  171. catch (\think\exception\PDOException $e)
  172. {
  173. $this->error($e->getMessage());
  174. }
  175. }
  176. $this->error(__('Parameter %s can not be empty', ''));
  177. }
  178. return $this->view->fetch();
  179. }
  180. /**
  181. * 放弃
  182. */
  183. public function giveup() {
  184. try {
  185. //1.获取id
  186. $id = $this->request->post("id");
  187. //2.protector表 通过id 查找出数据
  188. $protector = \app\admin\model\Protector::get($id);
  189. //3.获取用户id
  190. $usr_id = Session::get('admin')['id'];
  191. //4.判断用户id和 protector的用户id是否一致 保存状态
  192. if ($protector['usr_id'] != $usr_id) {
  193. $this->error('请操作自己的数据!');
  194. }
  195. //5.保护到期时间设置现在 冻结算一下
  196. $free_day = Session::get('admin')['free_day'];
  197. $param['ex_date'] = date('Y-m-d');
  198. $param['free_date'] = date('Y-m-d', strtotime("+" . $free_day . " day"));
  199. $param['status'] = $this->status['GIVEUP'];
  200. $param['id'] = $id;
  201. //6.入库
  202. $res = $this->model->allowField(true)->isUpdate(true)->save($param);
  203. if ($res) {
  204. $this->success('放弃成功!');
  205. }
  206. $this->error();
  207. } catch (\think\exception\PDOException $e)
  208. {
  209. $this->error($e->getMessage());
  210. }
  211. }
  212. /**
  213. * 签单
  214. */
  215. public function sign() {
  216. try{
  217. //1.获取id
  218. $id = $this->request->post("id");
  219. //2.protector表 通过id 查找出数据
  220. $protector = \app\admin\model\Protector::get($id);
  221. //3.获取用户id
  222. $usr_id = Session::get('admin')['id'];
  223. //4.判断用户id和 protector的用户id是否一致 保存状态
  224. if ($protector['usr_id'] != $usr_id) {
  225. $this->error('请操作自己的数据!');
  226. }
  227. $param['id'] = $id;
  228. $param['status'] = $this->status['SIGN'];
  229. //5.入库
  230. $res = $this->model->allowField(true)->isUpdate(true)->save($param);
  231. if ($res) {
  232. $this->success('签单成功!');
  233. }
  234. $this->error();
  235. } catch (\think\exception\PDOException $e){
  236. $this->error($e->getMessage());
  237. }
  238. }
  239. /**
  240. * 再次保护
  241. */
  242. public function again() {
  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.判断用户保护个数是否达到上限
  251. $res = \app\admin\model\Protector::proCountIsExceed($usr_id, Session::get('admin')['protect_count']);
  252. if($res){
  253. $this->error($res);
  254. }
  255. //5 判断 protector表 状态 如果是已签单返回
  256. if($protector['status'] == $this->status['SIGN']){
  257. $this->error('已签单!无法再次保护');
  258. }
  259. //6 已过期,已放弃
  260. if($protector['status'] != $this->status['OVERDUE'] && $protector['status'] != $this->status['GIVEUP']){
  261. $this->error('已过期或已放弃才可以再次保护!');
  262. }
  263. //7.判断用户id和 protector的用户id是否一致 保
  264. if($protector['usr_id'] == $usr_id) {
  265. // 同一个人操作,判断是否结束冻结
  266. if(strtotime(date('Y-m-d')) < strtotime($protector['free_date'])){
  267. $this->error('未过冻结时间!无法再次保护');
  268. }
  269. }
  270. // 不同的人操作或者已经过了冻结期
  271. $protect_day = Session::get('admin')['protect_day']; //保护天数
  272. $free_day = Session::get('admin')['free_day']; // 冻结天数
  273. $params['usr_id'] = $usr_id; //usr id
  274. $params['usr_nickname'] = Session::get('admin')['nickname']; //nickname
  275. $params['ex_date'] = date('Y-m-d',strtotime("+".$protect_day." day")); //保护到期时间
  276. $params['free_date'] = date('Y-m-d',strtotime("{$params['ex_date']} "."+".$free_day." day")); //保护到期时间
  277. $params['usr_depart'] = AuthGroupAccess::getGroupName($params['usr_id']);
  278. $params['id'] = $id;
  279. //5.入库
  280. $res = $this->model->allowField(true)->isUpdate(true)->save($params);
  281. if ($res) {
  282. $this->success('再次保护成功!');
  283. }
  284. $this->error();
  285. } catch (\think\exception\PDOException $e){
  286. $this->error($e->getMessage());
  287. }
  288. }
  289. /**
  290. * 查看所有的跟进
  291. */
  292. public function showFollow()
  293. {
  294. try{
  295. $p_id = $this->request->post('id');
  296. $res = Db::table('follow')
  297. ->where('protect_id', $p_id)
  298. ->select();
  299. $html = '';
  300. if(!empty($res)){
  301. foreach ($res as $k=>$v){
  302. $html .=
  303. ' <div class="form-group col-xs-12">' .
  304. ' <label for="follow_time" class="control-label col-xs-12 col-sm-3"><span class="text-red"></span>操作人:</label>' .
  305. ' <div class="col-xs-12 col-sm-7">' .
  306. '<span style="display: inline-block;padding-top: 7px;">'.$v["usr_nickname"] .'</span>'.
  307. ' </div>' .
  308. ' </div>';
  309. $html .=
  310. ' <div class="form-group col-xs-12">' .
  311. ' <label for="follow_time" class="control-label col-xs-12 col-sm-3">跟进时间:</label>' .
  312. ' <div class="col-xs-12 col-sm-7">' .
  313. '<span style="display: inline-block;padding-top: 7px;">'.$v['follow_time'].'</span>'.
  314. ' </div>' .
  315. ' </div>';
  316. $html .=
  317. ' <div class="form-group col-xs-12" style="border-bottom: 1px solid #eeeeee;">' .
  318. ' <label class="control-label col-xs-12 col-sm-3">跟进内容:</label>' .
  319. ' <div class="col-xs-12 col-sm-7" style="margin-bottom: 10px;">' .
  320. ' <textarea disabled class="form-control " rows="5" name="main" cols="50">'.
  321. $v['main'].
  322. ' </textarea>' .
  323. ' </div>' .
  324. ' </div>';
  325. }
  326. }
  327. $html .=
  328. ' <div class="form-group col-xs-12">' .
  329. ' <label for="follow_time" class="control-label col-xs-12 col-sm-3">跟进时间:</label>' .
  330. ' <div class="col-xs-12 col-sm-7">' .
  331. ' <input id="follow_time" class="form-control datetimepicker" name="follow_time" type="text" data-rule="required">' .
  332. ' </div>' .
  333. ' </div>';
  334. $html .=
  335. ' <div class="form-group col-xs-12">' .
  336. ' <label class="control-label col-xs-12 col-sm-3">跟进内容:</label>' .
  337. ' <div class="col-xs-12 col-sm-7">' .
  338. ' <textarea id="main_text" class="form-control " rows="5" name="main" cols="50">'.
  339. ' </textarea>' .
  340. ' </div>' .
  341. ' </div>';
  342. $this->success('','',$html);
  343. } catch (\think\exception\PDOException $e){
  344. $this->error($e->getMessage());
  345. }
  346. }
  347. /**
  348. * 添加跟进
  349. */
  350. public function addFollow()
  351. {
  352. try{
  353. $data = $this->request->post();
  354. $param['protect_id'] = $data['id'];
  355. $param['usr_id'] = Session::get('admin')['id'];
  356. $param['usr_nickname'] = Session::get('admin')['nickname'];
  357. $param['protect_name'] = $data['unit_name'];
  358. $param['follow_time'] = $data['follow_time'];
  359. $param['main'] = $data['main'];
  360. $param['created_at'] = date('Y-m-d H:i:s');
  361. $res = Db::table('follow')
  362. ->insert($param);
  363. if($res){
  364. $this->success('跟进成功');
  365. }
  366. $this->error();
  367. } catch (\think\exception\PDOException $e){
  368. $this->error($e->getMessage());
  369. }
  370. }
  371. public function departList()
  372. {
  373. }
  374. }