Bookshelfrecommand.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\Redis;
  5. use app\common\service\BookRelationService;
  6. use app\main\constants\ErrorCodeConstants;
  7. use app\main\service\BookService;
  8. use think\Controller;
  9. use think\Exception;
  10. use think\Request;
  11. /**
  12. * 已签到推荐书库
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class Bookshelfrecommand extends Backend
  17. {
  18. protected $noNeedRight = ['*'];
  19. /**
  20. * BookshelfRecommand模型对象
  21. */
  22. protected $model = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = model('BookshelfRecommand');
  27. $this->view->assign("sexList", $this->model->getSexList());
  28. $this->view->assign("statusList", $this->model->getStatusList());
  29. }
  30. /**
  31. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  32. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  33. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  34. */
  35. /**
  36. * 查看
  37. */
  38. public function index()
  39. {
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags']);
  42. if ($this->request->isAjax())
  43. {
  44. //如果发送的来源是Selectpage,则转发到Selectpage
  45. if ($this->request->request('pkey_name'))
  46. {
  47. return $this->selectpage();
  48. }
  49. list($where, $sort, $order, $offset, $limit) = $this->buildparams('', true);
  50. $total = $this->model
  51. ->join('book', 'book.id=bookshelf_recommand.book_id')
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->count();
  55. $list = $this->model
  56. ->join('book', 'book.id=bookshelf_recommand.book_id')
  57. ->field(['bookshelf_recommand.*','book.cansee','book.state'])
  58. ->where($where)
  59. ->order($sort, $order)
  60. ->limit($offset, $limit)
  61. ->select();
  62. $result = array("total" => $total, "rows" => $list);
  63. return json($result);
  64. }
  65. return $this->view->fetch();
  66. }
  67. /**
  68. * 添加
  69. */
  70. public function add()
  71. {
  72. if ($this->request->isPost())
  73. {
  74. $params = $this->request->post("row/a");
  75. if ($params)
  76. {
  77. if ($this->dataLimit)
  78. {
  79. $params[$this->dataLimitField] = $this->auth->id;
  80. }
  81. try
  82. {
  83. $res = $this->model->all(['sex'=>$params['sex']]);
  84. if ( !empty($res) ){
  85. $bookIds = array_column($res, 'book_id');
  86. if( in_array( $params['book_id'], $bookIds ) ){
  87. $this->error('该书籍已经存在,不可重复添加');
  88. }
  89. //获取推荐库里的互斥书
  90. $bookRelations = BookRelationService::instance()->getBookRelationById($params['book_id']);
  91. if ( array_intersect($bookIds,$bookRelations) ){
  92. $this->error('该书籍与已添加的书籍 存在互斥关系,不可添加');
  93. }
  94. unset( $bookRelations );
  95. }
  96. //是否采用模型验证
  97. if ($this->modelValidate)
  98. {
  99. $name = basename(str_replace('\\', '/', get_class($this->model)));
  100. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  101. $this->model->validate($validate);
  102. }
  103. $result = $this->model->allowField(true)->save($params);
  104. if ($result !== false)
  105. {
  106. /*$redis = Redis::instance();
  107. $key = 'BKSR-RB:' . $params['sex'];
  108. $redis->del($key);*/
  109. $this->success();
  110. }
  111. else
  112. {
  113. $this->error($this->model->getError());
  114. }
  115. }
  116. catch (\think\exception\PDOException $e)
  117. {
  118. $this->error($e->getMessage());
  119. }
  120. }
  121. $this->error(__('Parameter %s can not be empty', ''));
  122. }
  123. return $this->view->fetch();
  124. }
  125. /**
  126. * 编辑
  127. */
  128. public function edit($ids = NULL)
  129. {
  130. $row = $this->model->get($ids);
  131. if (!$row)
  132. $this->error(__('No Results were found'));
  133. $adminIds = $this->getDataLimitAdminIds();
  134. if (is_array($adminIds))
  135. {
  136. if (!in_array($row[$this->dataLimitField], $adminIds))
  137. {
  138. $this->error(__('You have no permission'));
  139. }
  140. }
  141. if ($this->request->isPost())
  142. {
  143. $params = $this->request->post("row/a");
  144. if ($params)
  145. {
  146. /*
  147. * 已经弃用,如果为了兼容老版可取消注释
  148. foreach ($params as $k => &$v)
  149. {
  150. $v = is_array($v) ? implode(',', $v) : $v;
  151. }
  152. */
  153. try
  154. {
  155. $res = $this->model->where('book_id', $params['book_id'])->where('id', 'neq', $row->id)->find();
  156. if($res){
  157. $this->error('该书籍已经存在,不可重复添加');
  158. }
  159. //是否采用模型验证
  160. if ($this->modelValidate)
  161. {
  162. $name = basename(str_replace('\\', '/', get_class($this->model)));
  163. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  164. $row->validate($validate);
  165. }
  166. $result = $row->allowField(true)->save($params);
  167. if ($result !== false)
  168. {
  169. /*$redis = Redis::instance();
  170. $key = 'BKSR-RB:' . $params['sex'];
  171. $redis->del($key);*/
  172. $this->success();
  173. }
  174. else
  175. {
  176. $this->error($row->getError());
  177. }
  178. }
  179. catch (\think\exception\PDOException $e)
  180. {
  181. $this->error($e->getMessage());
  182. }
  183. }
  184. $this->error(__('Parameter %s can not be empty', ''));
  185. }
  186. $this->view->assign("row", $row);
  187. return $this->view->fetch();
  188. }
  189. /**
  190. * 删除
  191. */
  192. public function del($ids = "")
  193. {
  194. if ($ids)
  195. {
  196. $pk = $this->model->getPk();
  197. $adminIds = $this->getDataLimitAdminIds();
  198. if (is_array($adminIds))
  199. {
  200. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  201. }
  202. $list = $this->model->where($pk, 'in', $ids)->select();
  203. $count = 0;
  204. foreach ($list as $k => $v)
  205. {
  206. $count += $v->delete();
  207. }
  208. if ($count)
  209. {
  210. /*$redis = Redis::instance();
  211. $redis->del('BKSR-RB:1');
  212. $redis->del('BKSR-RB:2');*/
  213. $this->success();
  214. }
  215. else
  216. {
  217. $this->error(__('No rows were deleted'));
  218. }
  219. }
  220. $this->error(__('Parameter %s can not be empty', 'ids'));
  221. }
  222. public function clearcache()
  223. {
  224. try {
  225. $result = BookService::instance()->clearShelfRecommendBookIds();
  226. if ($result->code == ErrorCodeConstants::SUCCESS) {
  227. $this->success();
  228. } else {
  229. $this->error($result->msg);
  230. }
  231. } catch (Exception $e) {
  232. $this->error($e->getMessage());
  233. }
  234. }
  235. }