Synthesis.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\admin\controller\product;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use think\Db;
  6. use app\common\model\AnnouncementModel;
  7. use think\exception\DbException;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. /**
  11. * 产品合成管理
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Synthesis extends Backend
  16. {
  17. /**
  18. * Synthesis模型对象
  19. * @var \app\admin\model\product\Synthesis
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\common\model\Synthesis;
  26. }
  27. /**
  28. * 查看
  29. *
  30. * @return string|Json
  31. * @throws \think\Exception
  32. * @throws DbException
  33. */
  34. public function index()
  35. {
  36. //设置过滤方法
  37. $this->request->filter(['strip_tags', 'trim']);
  38. if (false === $this->request->isAjax()) {
  39. return $this->view->fetch();
  40. }
  41. //如果发送的来源是 Selectpage,则转发到 Selectpage
  42. if ($this->request->request('keyField')) {
  43. return $this->selectpage();
  44. }
  45. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  46. $list = $this->model->with('announcement,productlists')
  47. ->where($where)
  48. ->order($sort, $order)
  49. ->paginate($limit);
  50. $result = ['total' => $list->total(), 'rows' => $list->items()];
  51. return json($result);
  52. }
  53. /**
  54. * 添加
  55. *
  56. * @return string
  57. * @throws \think\Exception
  58. */
  59. public function add()
  60. {
  61. if (false === $this->request->isPost()) {
  62. return $this->view->fetch();
  63. }
  64. $params = $this->request->post('row/a');
  65. if (empty($params)) {
  66. $this->error(__('Parameter %s can not be empty', ''));
  67. }
  68. $params = $this->preExcludeFields($params);
  69. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  70. $params[$this->dataLimitField] = $this->auth->id;
  71. }
  72. $result = false;
  73. Db::startTrans();
  74. try {
  75. //是否采用模型验证
  76. if ($this->modelValidate) {
  77. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  78. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  79. $this->model->validateFailException()->validate($validate);
  80. }
  81. //更新公告关联产品
  82. AnnouncementModel::setAnnouncementByProduct($params['announcement_id'], [$params['product_id'], $params['material_one'], $params['material_two'], $params['material_three']]);
  83. //添加
  84. $result = $this->model->allowField(true)->save($params);
  85. Db::commit();
  86. } catch (ValidateException|PDOException|Exception $e) {
  87. Db::rollback();
  88. $this->error($e->getMessage());
  89. }
  90. if ($result === false) {
  91. $this->error(__('No rows were inserted'));
  92. }
  93. $this->success();
  94. }
  95. /**
  96. * 编辑
  97. *
  98. * @param $ids
  99. * @return string
  100. * @throws DbException
  101. * @throws \think\Exception
  102. */
  103. public function edit($ids = null)
  104. {
  105. $row = $this->model->get($ids);
  106. if (!$row) {
  107. $this->error(__('No Results were found'));
  108. }
  109. $adminIds = $this->getDataLimitAdminIds();
  110. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  111. $this->error(__('You have no permission'));
  112. }
  113. if (false === $this->request->isPost()) {
  114. $this->view->assign('row', $row);
  115. return $this->view->fetch();
  116. }
  117. $params = $this->request->post('row/a');
  118. if (empty($params)) {
  119. $this->error(__('Parameter %s can not be empty', ''));
  120. }
  121. $params = $this->preExcludeFields($params);
  122. $result = false;
  123. Db::startTrans();
  124. try {
  125. //是否采用模型验证
  126. if ($this->modelValidate) {
  127. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  128. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  129. $row->validateFailException()->validate($validate);
  130. }
  131. //更新公告关联产品
  132. AnnouncementModel::setAnnouncementByProduct($params['announcement_id'], [$params['product_id'], $params['material_one'], $params['material_two'], $params['material_three']]);
  133. $result = $row->allowField(true)->save($params);
  134. Db::commit();
  135. } catch (ValidateException|PDOException|Exception $e) {
  136. Db::rollback();
  137. $this->error($e->getMessage());
  138. }
  139. if (false === $result) {
  140. $this->error(__('No rows were updated'));
  141. }
  142. $this->success();
  143. }
  144. }