Cplist.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\service\LogService;
  4. use app\common\controller\Backend;
  5. use app\common\library\Redis;
  6. use think\Controller;
  7. use think\Request;
  8. /**
  9. *
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Cplist extends Backend
  14. {
  15. /**
  16. * Cplist模型对象
  17. */
  18. protected $model = null;
  19. /**
  20. * 无需鉴权的方法,但需要登录
  21. * @var array
  22. */
  23. protected $noNeedRight = ['ajaxlapse','show','synccplist','clearcache'];
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->model = model('Cplist');
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. /**
  35. * 查看
  36. */
  37. public function index()
  38. {
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags']);
  41. if ($this->request->isAjax())
  42. {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('pkey_name'))
  45. {
  46. return $this->selectpage();
  47. }
  48. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  49. $total = $this->model
  50. ->where($where)
  51. ->order($sort, $order)
  52. ->count();
  53. $list = $this->model
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->limit($offset, $limit)
  57. ->select();
  58. $result = array("total" => $total, "rows" => $list);
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. public function ajaxlapse()
  64. {
  65. $id = $this->request->param('id');
  66. $cp_state = $this->request->param('cp_state');
  67. $obj = $this->model->find($id);
  68. if ($obj == false){
  69. $this->error('cp不存在');
  70. }
  71. $update = [];
  72. if ($cp_state == 2) {//恢复的时候
  73. $update['state'] = 1;
  74. $update['cansee'] = 1;
  75. $cp_state = 1;
  76. $this->setOldCpBookState($obj);
  77. } else {
  78. $update['state'] = 0;
  79. $update['cansee'] = 0;
  80. $cp_state = 2;
  81. $this->saveCpBookState($obj);
  82. model('book')->update($update,['cp_name'=>$obj->cp_name]);
  83. }
  84. $obj->updatetime = time();
  85. $obj->state = $cp_state;
  86. $rst = $obj->save();
  87. $this->success('操作成功');
  88. }
  89. public function clearcache()
  90. {
  91. $cp_name = $this->request->param('cp_name');
  92. if ($cp_name== false){
  93. $this->error('异常操作');
  94. }
  95. $bookIds = model('book')->field('id')->where('cp_name',$cp_name)->select();
  96. if (!empty($bookIds)){
  97. $bookIds = array_column($bookIds,'id');
  98. $c = count($bookIds);
  99. $redis = Redis::instance();
  100. for ($i=0;$i<$c;$i++){
  101. $key = 'B:'.$bookIds[$i];
  102. $redis->del($key);
  103. }
  104. }
  105. $this->success('清除成功',null,json_encode($bookIds));
  106. }
  107. public function show()
  108. {
  109. $list = $this->model->select();
  110. $searchlist = [];
  111. foreach ($list as $key => $value) {
  112. $searchlist[] = ['id' => $value['cp_name'], 'name' => $value['cp_name']];
  113. }
  114. $data = ['searchlist' => $searchlist];
  115. $this->success('', null, $data);
  116. }
  117. /**
  118. * 原始书库的cp_id不是唯一值
  119. * 同步书籍cp
  120. */
  121. public function synccplist()
  122. {
  123. $oldList = $this->model->field('cp_name')->select();
  124. $oldIds = [];
  125. if (!empty($oldList)){
  126. $oldIds = array_column($oldList,'cp_name');
  127. }
  128. $cpList = model('book')->field('cp_id,cp_name')->where('cp_id','>',0)->group('cp_id,cp_name')->select();
  129. if (!empty($cpList)){
  130. foreach ($cpList as $k =>$v){
  131. $insert = [];
  132. if (in_array($v['cp_name'],$oldIds)){
  133. continue;
  134. }
  135. $insert['cp_id'] = $v['cp_id'];
  136. $insert['cp_name'] = $v['cp_name'];
  137. $insert['updatetime'] = time();
  138. $this->model->insert($insert);
  139. }
  140. }
  141. $this->success('同步成功');
  142. }
  143. public function setOldCpBookState($obj)
  144. {
  145. $res = Redis::instance()->get('CBS:'.$obj->id);
  146. LogService::info('update'.$res);
  147. $res = json_decode($res,true);
  148. if (!empty($res)){
  149. foreach ($res as $k=>$v){
  150. $tempUpdate = [];
  151. $tempUpdate['state'] = $v['state'];
  152. $tempUpdate['cansee'] = $v['cansee'];
  153. $r = model('book')->isUpdate(true)->save($tempUpdate,['id'=>$v['id']]);
  154. LogService::info('update'.$v['id'].$r);
  155. }
  156. }
  157. return true;
  158. }
  159. public function saveCpBookState($obj)
  160. {
  161. $res = model('book')->field('id,state,cansee')->whereIn('cp_name',$obj->cp_name)->select();
  162. $oldStates = [];
  163. if(!empty($res))foreach ($res as $k=>$v){
  164. $oldState = [];
  165. $oldState['id'] = $v['id'];
  166. $oldState['state'] = $v['state'];
  167. $oldState['cansee'] = $v['cansee'];
  168. $oldStates[] = $oldState;
  169. }
  170. $re = Redis::instance()->set('CBS:'.$obj->id,json_encode($oldStates));
  171. return (bool)$re;
  172. }
  173. }