123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- <?php
- namespace app\admin\controller;
- use app\admin\model\AuthGroupAccess;
- use app\common\controller\Backend;
- use think\Db;
- use think\Session;
- /**
- *
- *
- * @icon fa fa-circle-o
- */
- class Protector extends Backend
- {
- private $status = [
- "PROTECTOR" => 0 , //保护中
- "OVERDUE" => 1, // 已过期
- "SIGN" => 2, // 已签单
- "FREE" => 3, // 冻结中
- "GIVEUP" => 4, // 已放弃
- ];
- //快速搜索的字段
- protected $searchFields = ['unit_name','contacts'];
- /**
- * Protector模型对象
- * @var \app\admin\model\Protector
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('Protector');
- }
- /**
- * 查看
- */
- public function index($status = false)
- {
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax())
- {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField'))
- {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $where_usr_id = ['usr_id'=>Session::get('admin')['id']]; //只能查看自己的数据
- switch ($status){
- case 'protect': //保护中
- $total = $this->model
- ->where('status',$this->status['PROTECTOR'])
- ->where($where_usr_id)
- ->where($where)
- ->order($sort, $order)
- ->count();
- $list = $this->model
- ->where('status',$this->status['PROTECTOR'])
- ->where($where_usr_id)
- ->where($where)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- break;
- case 'overdue': //已过期
- $total = $this->model
- ->where('status',$this->status['OVERDUE'])
- ->whereOr('status',$this->status['GIVEUP'])
- ->where($where_usr_id)
- ->where($where)
- ->order($sort, $order)
- ->count();
- $list = $this->model
- ->where('status',$this->status['OVERDUE'])
- ->whereOr('status',$this->status['GIVEUP'])
- ->where($where_usr_id)
- ->where($where)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- break;
- case 'contract': //已签单
- $total = $this->model
- ->where('status',$this->status['SIGN'])
- ->where($where_usr_id)
- ->where($where)
- ->order($sort, $order)
- ->count();
- $list = $this->model
- ->where('status',$this->status['SIGN'])
- ->where($where_usr_id)
- ->where($where)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- break;
- case 'giveup': //已放弃
- $total = $this->model
- ->where('status',$this->status['GIVEUP'])
- ->where($where_usr_id)
- ->where($where)
- ->order($sort, $order)
- ->count();
- $list = $this->model
- ->where('status',$this->status['GIVEUP'])
- ->where($where_usr_id)
- ->where($where)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- break;
- }
- $list = collection($list)->toArray();
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch('index');
- }
- /**
- * 添加
- */
- public function add()
- {
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- if ($params)
- {
- if ($this->dataLimit && $this->dataLimitFieldAutoFill)
- {
- $params[$this->dataLimitField] = $this->auth->id;
- }
- try
- {
- //是否采用模型验证
- if ($this->modelValidate)
- {
- $name = basename(str_replace('\\', '/', get_class($this->model)));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
- $this->model->validate($validate);
- }
- $params['usr_id'] = Session::get('admin')['id']; //usr id
- $params['usr_nickname'] = Session::get('admin')['nickname']; //nickname
- $protect_count = Session::get('admin')['protect_count']; //保护个数
- $protect_day = Session::get('admin')['protect_day']; //保护天数
- $free_day = Session::get('admin')['free_day']; // 冻结天数
- // 获取所属组名
- $params['usr_depart'] = AuthGroupAccess::getGroupName($params['usr_id']);
- // 获取到期时间和冻结到期时间
- $params['ex_date'] = date('Y-m-d',strtotime("+".$protect_day." day")); //保护到期时间
- $params['free_date'] = date('Y-m-d',strtotime("{$params['ex_date']} "."+".$free_day." day")); //保护到期时间
- // 根据单位名称判断是否已经存在(已被保护)
- $res = \app\admin\model\Protector::unitNameIsExist($params['unit_name']);
- if($res){
- $this->error('"'.$res.'"已经被保护!');
- }
- // 判断是否超出保护个数
- $res = \app\admin\model\Protector::proCountIsExceed($params['usr_id'], $protect_count);
- if($res){
- $this->error($res);
- }
- $result = $this->model->allowField(true)->save($params);
- if ($result !== false)
- {
- $this->success();
- }
- else
- {
- $this->error($this->model->getError());
- }
- }
- catch (\think\exception\PDOException $e)
- {
- $this->error($e->getMessage());
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- return $this->view->fetch();
- }
- /**
- * 放弃
- */
- public function giveup() {
- try {
- //1.获取id
- $id = $this->request->post("id");
- //2.protector表 通过id 查找出数据
- $protector = \app\admin\model\Protector::get($id);
- //3.获取用户id
- $usr_id = Session::get('admin')['id'];
- //4.判断用户id和 protector的用户id是否一致 保存状态
- if ($protector['usr_id'] != $usr_id) {
- $this->error('请操作自己的数据!');
- }
- //5.保护到期时间设置现在 冻结算一下
- $free_day = Session::get('admin')['free_day'];
- $param['ex_date'] = date('Y-m-d');
- $param['free_date'] = date('Y-m-d', strtotime("+" . $free_day . " day"));
- $param['status'] = $this->status['GIVEUP'];
- $param['id'] = $id;
- //6.入库
- $res = $this->model->allowField(true)->isUpdate(true)->save($param);
- if ($res) {
- $this->success('放弃成功!');
- }
- $this->error();
- } catch (\think\exception\PDOException $e)
- {
- $this->error($e->getMessage());
- }
- }
- /**
- * 签单
- */
- public function sign() {
- try{
- //1.获取id
- $id = $this->request->post("id");
- //2.protector表 通过id 查找出数据
- $protector = \app\admin\model\Protector::get($id);
- //3.获取用户id
- $usr_id = Session::get('admin')['id'];
- //4.判断用户id和 protector的用户id是否一致 保存状态
- if ($protector['usr_id'] != $usr_id) {
- $this->error('请操作自己的数据!');
- }
- $param['id'] = $id;
- $param['status'] = $this->status['SIGN'];
- //5.入库
- $res = $this->model->allowField(true)->isUpdate(true)->save($param);
- if ($res) {
- $this->success('签单成功!');
- }
- $this->error();
- } catch (\think\exception\PDOException $e){
- $this->error($e->getMessage());
- }
- }
- /**
- * 再次保护
- */
- public function again() {
- try{
- //1.获取id
- $id = $this->request->post("id");
- //2.protector表 通过id 查找出数据
- $protector = \app\admin\model\Protector::get($id);
- //3.获取用户id
- $usr_id = Session::get('admin')['id'];
- //4.判断用户保护个数是否达到上限
- $res = \app\admin\model\Protector::proCountIsExceed($usr_id, Session::get('admin')['protect_count']);
- if($res){
- $this->error($res);
- }
- //5 判断 protector表 状态 如果是已签单返回
- if($protector['status'] == $this->status['SIGN']){
- $this->error('已签单!无法再次保护');
- }
- //6 已过期,已放弃
- if($protector['status'] != $this->status['OVERDUE'] && $protector['status'] != $this->status['GIVEUP']){
- $this->error('已过期或已放弃才可以再次保护!');
- }
- //7.判断用户id和 protector的用户id是否一致 保
- if($protector['usr_id'] == $usr_id) {
- // 同一个人操作,判断是否结束冻结
- if(strtotime(date('Y-m-d')) < strtotime($protector['free_date'])){
- $this->error('未过冻结时间!无法再次保护');
- }
- }
- // 不同的人操作或者已经过了冻结期
- $protect_day = Session::get('admin')['protect_day']; //保护天数
- $free_day = Session::get('admin')['free_day']; // 冻结天数
- $params['usr_id'] = $usr_id; //usr id
- $params['usr_nickname'] = Session::get('admin')['nickname']; //nickname
- $params['ex_date'] = date('Y-m-d',strtotime("+".$protect_day." day")); //保护到期时间
- $params['free_date'] = date('Y-m-d',strtotime("{$params['ex_date']} "."+".$free_day." day")); //保护到期时间
- $params['usr_depart'] = AuthGroupAccess::getGroupName($params['usr_id']);
- $params['id'] = $id;
- $params['status'] = $this->status['PROTECTOR'];
- //5.入库
- $res = $this->model->allowField(true)->isUpdate(true)->save($params);
- if ($res) {
- $this->success('再次保护成功!');
- }
- $this->error();
- } catch (\think\exception\PDOException $e){
- $this->error($e->getMessage());
- }
- }
- /**
- * 查看所有的跟进
- */
- public function showFollow()
- {
- try{
- $p_id = $this->request->post('id');
- $res = Db::table('follow')
- ->where('protect_id', $p_id)
- ->select();
- $html = '';
- if(!empty($res)){
- foreach ($res as $k=>$v){
- $html .=
- ' <div class="form-group col-xs-12">' .
- ' <label for="follow_time" class="control-label col-xs-12 col-sm-3"><span class="text-red"></span>操作人:</label>' .
- ' <div class="col-xs-12 col-sm-7">' .
- '<span style="display: inline-block;padding-top: 7px;">'.$v["usr_nickname"] .'</span>'.
- ' </div>' .
- ' </div>';
- $html .=
- ' <div class="form-group col-xs-12">' .
- ' <label for="follow_time" class="control-label col-xs-12 col-sm-3">跟进时间:</label>' .
- ' <div class="col-xs-12 col-sm-7">' .
- '<span style="display: inline-block;padding-top: 7px;">'.$v['follow_time'].'</span>'.
- ' </div>' .
- ' </div>';
- $html .=
- ' <div class="form-group col-xs-12" style="border-bottom: 1px solid #eeeeee;">' .
- ' <label class="control-label col-xs-12 col-sm-3">跟进内容:</label>' .
- ' <div class="col-xs-12 col-sm-7" style="margin-bottom: 10px;">' .
- ' <textarea disabled class="form-control " rows="5" name="main" cols="50">'.
- $v['main'].
- ' </textarea>' .
- ' </div>' .
- ' </div>';
- }
- }
- $html .=
- ' <div class="form-group col-xs-12">' .
- ' <label for="follow_time" class="control-label col-xs-12 col-sm-3">跟进时间:</label>' .
- ' <div class="col-xs-12 col-sm-7">' .
- ' <input id="follow_time" class="form-control datetimepicker" name="follow_time" type="text" data-rule="required">' .
- ' </div>' .
- ' </div>';
- $html .=
- ' <div class="form-group col-xs-12">' .
- ' <label class="control-label col-xs-12 col-sm-3">跟进内容:</label>' .
- ' <div class="col-xs-12 col-sm-7">' .
- ' <textarea id="main_text" class="form-control " rows="5" name="main" cols="50">'.
- ' </textarea>' .
- ' </div>' .
- ' </div>';
- $this->success('','',$html);
- } catch (\think\exception\PDOException $e){
- $this->error($e->getMessage());
- }
- }
- /**
- * 添加跟进
- */
- public function addFollow()
- {
- try{
- $data = $this->request->post();
- $param['protect_id'] = $data['id'];
- $param['usr_id'] = Session::get('admin')['id'];
- $param['usr_nickname'] = Session::get('admin')['nickname'];
- $param['protect_name'] = $data['unit_name'];
- $param['follow_time'] = $data['follow_time'];
- $param['main'] = $data['main'];
- $param['created_at'] = date('Y-m-d H:i:s');
- $res = Db::table('follow')
- ->insert($param);
- if($res){
- $this->success('跟进成功');
- }
- $this->error();
- } catch (\think\exception\PDOException $e){
- $this->error($e->getMessage());
- }
- }
- }
|