Signedrecommand.php 7.8 KB

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