Intention.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 Intention 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 = ['again','giveup','unittypelist'];
  29. /**
  30. * Intention模型对象
  31. * @var \app\admin\model\Intention
  32. */
  33. protected $model = null;
  34. public function _initialize()
  35. {
  36. parent::_initialize();
  37. $this->model = model('Intention');
  38. }
  39. /**
  40. * 查看
  41. */
  42. public function index()
  43. {
  44. //设置过滤方法
  45. $this->request->filter(['strip_tags']);
  46. if ($this->request->isAjax())
  47. {
  48. //如果发送的来源是Selectpage,则转发到Selectpage
  49. if ($this->request->request('keyField'))
  50. {
  51. return $this->selectpage();
  52. }
  53. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  54. $rules = AuthGroup::getRulesByGroupId(Session::get('admin')['depart_id']);
  55. if($rules == '*'){
  56. $where_usr_id = false; // 管理员可以看到所有
  57. } else {
  58. $auth_group = Db::table('auth_group')->select(); // 所有组别
  59. $depart_id = Session::get('admin')['depart_id']; // 所属组别
  60. // 获取所有子级部门
  61. $arr = AuthGroup::getSubs($auth_group,$depart_id);
  62. array_push($arr,$depart_id);
  63. // 只能查看自己和自己子级的保护客户和签单客户
  64. $where_usr_id = ['depart_id'=>['in',$arr]];
  65. }
  66. $where1 = array('isdel'=>0);
  67. $total = $this->model
  68. ->where($where)
  69. ->where($where_usr_id)
  70. ->where($where1)
  71. ->order($sort, $order)
  72. ->count();
  73. $list = $this->model
  74. ->where($where)
  75. ->where($where_usr_id)
  76. ->where($where1)
  77. ->order($sort, $order)
  78. ->limit($offset, $limit)
  79. ->select();
  80. $list = collection($list)->toArray();
  81. $result = array("total" => $total, "rows" => $list);
  82. return json($result);
  83. }
  84. return $this->view->fetch();
  85. }
  86. /**
  87. * 添加
  88. */
  89. public function add()
  90. {
  91. if ($this->request->isPost())
  92. {
  93. $params = $this->request->post("row/a");
  94. if ($params)
  95. {
  96. if ($this->dataLimit && $this->dataLimitFieldAutoFill)
  97. {
  98. $params[$this->dataLimitField] = $this->auth->id;
  99. }
  100. try
  101. {
  102. //是否采用模型验证
  103. if ($this->modelValidate)
  104. {
  105. $name = basename(str_replace('\\', '/', get_class($this->model)));
  106. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  107. $this->model->validate($validate);
  108. }
  109. $params['usr_id'] = Session::get('admin')['id']; //usr id
  110. $params['usr_nickname'] = Session::get('admin')['nickname']; //nickname
  111. $params['created_at'] = date('Y-m-d H:i:s'); //创建时间
  112. $params['usr_depart'] = Session::get('admin')['usr_depart']; //获取所属组名
  113. $params['depart_id'] = Session::get('admin')['depart_id']; //获取所属组id
  114. \app\admin\model\Intention::addUnitType($params['unit_type']);
  115. // 根据单位名称判断是否已经存在(已被保护)
  116. $res = \app\admin\model\Protector::unitNameIsExist($params['unit_name']);
  117. if($res){
  118. $this->error('"'.$res.'"已经被保护!');
  119. }
  120. $result = $this->model->allowField(true)->save($params);
  121. if ($result !== false)
  122. {
  123. $this->success();
  124. }
  125. else
  126. {
  127. $this->error($this->model->getError());
  128. }
  129. }
  130. catch (\think\exception\PDOException $e)
  131. {
  132. $this->error($e->getMessage());
  133. }
  134. }
  135. $this->error(__('Parameter %s can not be empty', ''));
  136. }
  137. return $this->view->fetch();
  138. }
  139. /**
  140. * 性质列表
  141. */
  142. public function unitTypeList()
  143. {
  144. $list = Db::table('unit_type')
  145. ->select();
  146. $searchlist = [];
  147. foreach ($list as $key => $value)
  148. {
  149. $searchlist[] = ['id'=>$value['name'], 'name' => $value['name']];
  150. }
  151. $data = ['rows' => $searchlist];
  152. return $data;
  153. }
  154. /**
  155. * 放弃
  156. */
  157. public function giveup() {
  158. try {
  159. //1.获取id
  160. $id = $this->request->post("id");
  161. //2.protector表 通过id 查找出数据
  162. $protector = \app\admin\model\Intention::get($id);
  163. //3.获取用户id
  164. $usr_id = Session::get('admin')['id'];
  165. //4.判断用户id和 protector的用户id是否一致 保存状态
  166. if ($usr_id != 1 && $protector['usr_id'] != $usr_id) {
  167. $this->error('请操作自己的数据!');
  168. }
  169. //5.保护到期时间设置现在 冻结算一下
  170. $free_day = Session::get('admin')['free_day'];
  171. $param['ex_date'] = date('Y-m-d');
  172. $param['free_date'] = date('Y-m-d', strtotime("+" . $free_day . " day"));
  173. // $param['status'] = $this->status['GIVEUP'];
  174. $param['isdel'] = 1;
  175. $param['id'] = $id;
  176. //6.入库
  177. $res = $this->model->allowField(true)->isUpdate(true)->save($param);
  178. if ($res) {
  179. $this->success('放弃成功!');
  180. }
  181. $this->error();
  182. } catch (\think\exception\PDOException $e)
  183. {
  184. $this->error($e->getMessage());
  185. }
  186. }
  187. public function follow($ids = NULL){
  188. $info = Db::table('follow')->where('type',1)->where('protect_id',$ids)->field('created_at','main')->select();
  189. if($_POST){
  190. $data = $this->request->post();
  191. $unit = Db::table('protector')->where('id',$data['id'])->field('unit_name')->find();
  192. $param['protect_id'] = $data['id'];
  193. $param['usr_id'] = Session::get('admin')['id'];
  194. $param['usr_nickname'] = $data['nickname'];
  195. $param['protect_name'] = $unit['unit_name'];
  196. $param['follow_time'] = $data['row']['follow_time'];
  197. $param['main'] = $data['row']['main'];
  198. $param['created_at'] = date('Y-m-d H:i:s');
  199. $param['type'] = 1;
  200. $res = Db::table('follow')->insert($param);
  201. if($res){
  202. $this->success();
  203. }else{
  204. $this->error('数据存储失败');
  205. }
  206. }
  207. $this->view->assign("info", $info);
  208. $this->view->assign("id", $ids);
  209. $this->view->assign("nickname", Session::get('admin')['nickname']);
  210. return $this->view->fetch();
  211. }
  212. }