123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- namespace app\admin\controller;
- use app\admin\service\LogService;
- use app\common\controller\Backend;
- use app\common\library\Redis;
- use think\Controller;
- use think\Request;
- /**
- *
- *
- * @icon fa fa-circle-o
- */
- class Cplist extends Backend
- {
-
- /**
- * Cplist模型对象
- */
- protected $model = null;
- /**
- * 无需鉴权的方法,但需要登录
- * @var array
- */
- protected $noNeedRight = ['ajaxlapse','show','synccplist','clearcache'];
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('Cplist');
- }
-
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
- * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- */
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax())
- {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('pkey_name'))
- {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $total = $this->model
- ->where($where)
- ->order($sort, $order)
- ->count();
- $list = $this->model
- ->where($where)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- public function ajaxlapse()
- {
- $id = $this->request->param('id');
- $cp_state = $this->request->param('cp_state');
- $obj = $this->model->find($id);
- if ($obj == false){
- $this->error('cp不存在');
- }
- $update = [];
- if ($cp_state == 2) {//恢复的时候
- $update['state'] = 1;
- $update['cansee'] = 1;
- $cp_state = 1;
- $this->setOldCpBookState($obj);
- } else {
- $update['state'] = 0;
- $update['cansee'] = 0;
- $cp_state = 2;
- $this->saveCpBookState($obj);
- model('book')->update($update,['cp_name'=>$obj->cp_name]);
- }
- $obj->updatetime = time();
- $obj->state = $cp_state;
- $rst = $obj->save();
- $this->success('操作成功');
- }
- public function clearcache()
- {
- $cp_name = $this->request->param('cp_name');
- if ($cp_name== false){
- $this->error('异常操作');
- }
- $bookIds = model('book')->field('id')->where('cp_name',$cp_name)->select();
- if (!empty($bookIds)){
- $bookIds = array_column($bookIds,'id');
- $c = count($bookIds);
- $redis = Redis::instance();
- for ($i=0;$i<$c;$i++){
- $key = 'B:'.$bookIds[$i];
- $redis->del($key);
- }
- }
- $this->success('清除成功',null,json_encode($bookIds));
- }
- public function show()
- {
- $list = $this->model->select();
- $searchlist = [];
- foreach ($list as $key => $value) {
- $searchlist[] = ['id' => $value['cp_name'], 'name' => $value['cp_name']];
- }
- $data = ['searchlist' => $searchlist];
- $this->success('', null, $data);
- }
- /**
- * 原始书库的cp_id不是唯一值
- * 同步书籍cp
- */
- public function synccplist()
- {
- $oldList = $this->model->field('cp_name')->select();
- $oldIds = [];
- if (!empty($oldList)){
- $oldIds = array_column($oldList,'cp_name');
- }
- $cpList = model('book')->field('cp_id,cp_name')->where('cp_id','>',0)->group('cp_id,cp_name')->select();
- if (!empty($cpList)){
- foreach ($cpList as $k =>$v){
- $insert = [];
- if (in_array($v['cp_name'],$oldIds)){
- continue;
- }
- $insert['cp_id'] = $v['cp_id'];
- $insert['cp_name'] = $v['cp_name'];
- $insert['updatetime'] = time();
- $this->model->insert($insert);
- }
- }
- $this->success('同步成功');
- }
- public function setOldCpBookState($obj)
- {
- $res = Redis::instance()->get('CBS:'.$obj->id);
- LogService::info('update'.$res);
- $res = json_decode($res,true);
- if (!empty($res)){
- foreach ($res as $k=>$v){
- $tempUpdate = [];
- $tempUpdate['state'] = $v['state'];
- $tempUpdate['cansee'] = $v['cansee'];
- $r = model('book')->isUpdate(true)->save($tempUpdate,['id'=>$v['id']]);
- LogService::info('update'.$v['id'].$r);
- }
- }
- return true;
- }
- public function saveCpBookState($obj)
- {
- $res = model('book')->field('id,state,cansee')->whereIn('cp_name',$obj->cp_name)->select();
- $oldStates = [];
- if(!empty($res))foreach ($res as $k=>$v){
- $oldState = [];
- $oldState['id'] = $v['id'];
- $oldState['state'] = $v['state'];
- $oldState['cansee'] = $v['cansee'];
- $oldStates[] = $oldState;
- }
- $re = Redis::instance()->set('CBS:'.$obj->id,json_encode($oldStates));
- return (bool)$re;
- }
- }
|