Novelendrecommend.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\service\BookRelationService;
  5. use app\main\constants\ErrorCodeConstants;
  6. use app\main\service\BookService;
  7. use think\Controller;
  8. use think\Exception;
  9. use think\Request;
  10. use app\common\library\Redis;
  11. use app\common\model\Book;
  12. /**
  13. * 关注推荐书库
  14. *
  15. * @icon fa fa-circle-o
  16. */
  17. class Novelendrecommend extends Backend
  18. {
  19. /**
  20. * FollowRecommand模型对象
  21. */
  22. protected $model = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->model = model('NovelEndRecommend');
  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=novel_end_recommend.book_id')
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->count();
  55. $list = $this->model
  56. ->join('book', 'book.id=novel_end_recommend.book_id')
  57. ->field(['novel_end_recommend.*','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. //$bookIds[] = $params['book_id'];
  91. $bookRelations = BookRelationService::instance()->getBookRelationById($params['book_id']);
  92. if ( array_intersect($bookIds,$bookRelations) ){
  93. $this->error('该书籍与已添加的书籍 存在互斥关系,不可添加');
  94. }
  95. unset( $bookRelations );
  96. }
  97. //是否采用模型验证
  98. if ($this->modelValidate)
  99. {
  100. $name = basename(str_replace('\\', '/', get_class($this->model)));
  101. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  102. $this->model->validate($validate);
  103. }
  104. $result = $this->model->allowField(true)->save($params);
  105. if ($result !== false)
  106. {
  107. /*$redis = Redis::instance();
  108. $key = 'NERB:' . $params['sex'];
  109. $redis->del($key);*/
  110. $this->success();
  111. }
  112. else
  113. {
  114. $this->error($this->model->getError());
  115. }
  116. }
  117. catch (\think\exception\PDOException $e)
  118. {
  119. $this->error($e->getMessage());
  120. }
  121. }
  122. $this->error(__('Parameter %s can not be empty', ''));
  123. }
  124. return $this->view->fetch();
  125. }
  126. /**
  127. * 编辑
  128. */
  129. public function edit($ids = NULL)
  130. {
  131. $row = $this->model->get($ids);
  132. if (!$row)
  133. $this->error(__('No Results were found'));
  134. $adminIds = $this->getDataLimitAdminIds();
  135. if (is_array($adminIds))
  136. {
  137. if (!in_array($row[$this->dataLimitField], $adminIds))
  138. {
  139. $this->error(__('You have no permission'));
  140. }
  141. }
  142. if ($this->request->isPost())
  143. {
  144. $params = $this->request->post("row/a");
  145. if ($params)
  146. {
  147. /*
  148. * 已经弃用,如果为了兼容老版可取消注释
  149. foreach ($params as $k => &$v)
  150. {
  151. $v = is_array($v) ? implode(',', $v) : $v;
  152. }
  153. */
  154. try
  155. {
  156. $res = $this->model->where('book_id', $params['book_id'])->where('id', 'neq', $row->id)->find();
  157. if($res){
  158. $this->error('该书籍已经存在,不可重复添加');
  159. }
  160. //是否采用模型验证
  161. if ($this->modelValidate)
  162. {
  163. $name = basename(str_replace('\\', '/', get_class($this->model)));
  164. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
  165. $row->validate($validate);
  166. }
  167. $result = $row->allowField(true)->save($params);
  168. if ($result !== false)
  169. {
  170. /*$redis = Redis::instance();
  171. $key = 'NERB:' . $params['sex'];
  172. $redis->del($key);*/
  173. $this->success();
  174. }
  175. else
  176. {
  177. $this->error($row->getError());
  178. }
  179. }
  180. catch (\think\exception\PDOException $e)
  181. {
  182. $this->error($e->getMessage());
  183. }
  184. }
  185. $this->error(__('Parameter %s can not be empty', ''));
  186. }
  187. $this->view->assign("row", $row);
  188. return $this->view->fetch();
  189. }
  190. public function clearcache()
  191. {
  192. try {
  193. $result = BookService::instance()->clearNovelEndRecommendBookIds();
  194. if ($result->code == ErrorCodeConstants::SUCCESS) {
  195. $this->success();
  196. } else {
  197. $this->error($result->msg);
  198. }
  199. } catch (Exception $e) {
  200. $this->error($e->getMessage());
  201. }
  202. }
  203. }