Protector.php 16 KB

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