Smartrecommand.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\service\BookRelationService;
  5. use think\Controller;
  6. use think\Request;
  7. /**
  8. * 图文推荐书库
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Smartrecommand extends Backend
  13. {
  14. /**
  15. * SmartRecommand模型对象
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = model('SmartRecommand');
  22. $this->view->assign("sexList", $this->model->getSexList());
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. $this->view->assign("locationList", $this->model->getLocationList());
  25. }
  26. /**
  27. * 查看
  28. */
  29. public function index()
  30. {
  31. //设置过滤方法
  32. $this->request->filter(['strip_tags']);
  33. if ($this->request->isAjax())
  34. {
  35. //如果发送的来源是Selectpage,则转发到Selectpage
  36. if ($this->request->request('pkey_name'))
  37. {
  38. return $this->selectpage();
  39. }
  40. list($where, $sort, $order, $offset, $limit) = $this->buildparams('', true);
  41. $total = $this->model
  42. ->join('book', 'book.id=smart_recommand.book_id')
  43. ->where($where)
  44. ->order($sort, $order)
  45. ->count();
  46. $list = $this->model
  47. ->join('book', 'book.id=smart_recommand.book_id')
  48. ->field(['smart_recommand.*','book.cansee','book.state'])
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->limit($offset, $limit)
  52. ->select();
  53. $result = array("total" => $total, "rows" => $list);
  54. return json($result);
  55. }
  56. return $this->view->fetch();
  57. }
  58. /**
  59. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
  60. * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
  61. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  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. /*
  74. * 已经弃用,如果为了兼容老版可取消注释
  75. foreach ($params as $k => &$v)
  76. {
  77. $v = is_array($v) ? implode(',', $v) : $v;
  78. }
  79. */
  80. if ($this->dataLimit)
  81. {
  82. $params[$this->dataLimitField] = $this->auth->id;
  83. }
  84. try
  85. {
  86. $res = $this->model->all(['sex'=>$params['sex']]);
  87. if ( !empty($res) ){
  88. $bookIds = array_column($res, 'book_id');
  89. if( in_array( $params['book_id'], $bookIds ) ){
  90. $this->error('该书籍已经存在,不可重复添加');
  91. }
  92. //获取推荐库里的互斥书
  93. //$bookIds[] = $params['book_id'];
  94. $bookRelations = BookRelationService::instance()->getBookRelationById($params['book_id']);
  95. if ( array_intersect($bookIds,$bookRelations) ){
  96. $this->error('该书籍与已添加的书籍 存在互斥关系,不可添加');
  97. }
  98. unset( $bookRelations );
  99. }
  100. //是否采用模型验证
  101. if ($this->modelValidate)
  102. {
  103. $name = basename(str_replace('\\', '/', get_class($this->model)));
  104. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  105. $this->model->validate($validate);
  106. }
  107. $result = $this->model->allowField(true)->save($params);
  108. if ($result !== false)
  109. {
  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. }