Lists.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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\ProductArea;
  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 Lists extends Backend
  16. {
  17. /**
  18. * Lists模型对象
  19. * @var \app\admin\model\product\Lists
  20. */
  21. protected $model = null;
  22. protected $productArea = null;
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->productArea = new ProductArea;
  27. $this->model = new \app\common\model\ProductLists;
  28. }
  29. /**
  30. * 查看
  31. *
  32. * @return string|Json
  33. * @throws \think\Exception
  34. * @throws DbException
  35. */
  36. public function index()
  37. {
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if (false === $this->request->isAjax()) {
  41. return $this->view->fetch();
  42. }
  43. //如果发送的来源是 Selectpage,则转发到 Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  48. $list = $this->model->with('products')
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. $result = ['total' => $list->total(), 'rows' => $list->items()];
  53. return json($result);
  54. }
  55. /**
  56. * 添加
  57. *
  58. * @return string
  59. * @throws \think\Exception
  60. */
  61. public function add()
  62. {
  63. if (false === $this->request->isPost()) {
  64. return $this->view->fetch();
  65. }
  66. $params = $this->request->post('row/a');
  67. if (empty($params)) {
  68. $this->error(__('Parameter %s can not be empty', ''));
  69. }
  70. $params = $this->preExcludeFields($params);
  71. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  72. $params[$this->dataLimitField] = $this->auth->id;
  73. }
  74. $result = false;
  75. Db::startTrans();
  76. try {
  77. //是否采用模型验证
  78. if ($this->modelValidate) {
  79. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  80. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  81. $this->model->validateFailException()->validate($validate);
  82. }
  83. $areaArr = json_decode($params['product_area'], true);
  84. if(empty($areaArr)) throw new ValidateException('请添加商品关联地区');
  85. if(self::isEqualArea($areaArr) == false) throw new ValidateException('该商品地区存在重复');
  86. unset($params['product_area']);
  87. //商品
  88. $add = $this->model->create($params);
  89. foreach ($areaArr as $row) {
  90. $row['product_id'] = $add->id;
  91. $result = $this->productArea::create($row);
  92. }
  93. Db::commit();
  94. } catch (ValidateException|PDOException|Exception $e) {
  95. Db::rollback();
  96. $this->error($e->getMessage());
  97. }
  98. if ($result === false) {
  99. $this->error(__('No rows were inserted'));
  100. }
  101. $this->success();
  102. }
  103. /**
  104. * 编辑
  105. *
  106. * @param $ids
  107. * @return string
  108. * @throws DbException
  109. * @throws \think\Exception
  110. */
  111. public function edit($ids = null)
  112. {
  113. $row = $this->model->get($ids);
  114. if (!$row) {
  115. $this->error(__('No Results were found'));
  116. }
  117. $adminIds = $this->getDataLimitAdminIds();
  118. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  119. $this->error(__('You have no permission'));
  120. }
  121. if (false === $this->request->isPost()) {
  122. $area = $this->productArea::where('product_id', $ids)->select();
  123. $this->view->assign('row', $row);
  124. $this->view->assign('area', json_encode($area, JSON_UNESCAPED_UNICODE) );
  125. return $this->view->fetch();
  126. }
  127. $params = $this->request->post('row/a');
  128. if (empty($params)) {
  129. $this->error(__('Parameter %s can not be empty', ''));
  130. }
  131. $params = $this->preExcludeFields($params);
  132. $result = false;
  133. Db::startTrans();
  134. try {
  135. //是否采用模型验证
  136. if ($this->modelValidate) {
  137. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  138. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  139. $row->validateFailException()->validate($validate);
  140. }
  141. $areaArr = json_decode($params['product_area'], true);
  142. if(empty($areaArr)) throw new ValidateException('请添加商品关联地区');
  143. if(self::isEqualArea($areaArr) == false) throw new ValidateException('该商品地区存在重复');
  144. unset($params['product_area']);
  145. $row->allowField(true)->save($params);
  146. $this->productArea::where('product_id', $ids)->delete();
  147. foreach ($areaArr as $row) {
  148. $row = (array)$row;
  149. $row['product_id'] = $ids;
  150. $result = $this->productArea::create($row);
  151. }
  152. Db::commit();
  153. } catch (ValidateException|PDOException|Exception $e) {
  154. Db::rollback();
  155. $this->error($e->getMessage());
  156. }
  157. if (false === $result) {
  158. $this->error(__('No rows were updated'));
  159. }
  160. $this->success();
  161. }
  162. //判断是否存在相同元素
  163. private static function isEqualArea(array $areaArr): bool
  164. {
  165. $result = array();
  166. if(count($areaArr) > 1){
  167. foreach ($areaArr as $value) {
  168. $value = (array)$value;
  169. foreach ($result as $v) {
  170. if ($value['province'] == $v['province'] &&
  171. $value['city'] == $v['city'] &&
  172. $value['area'] == $v['area'] &&
  173. $value['county'] == $v['county']) {
  174. return false;
  175. }
  176. }
  177. $result[] = $value;
  178. }
  179. }
  180. return true;
  181. }
  182. }