Lists.php 6.8 KB

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