Lists.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. foreach ($list as &$item) {
  54. $item['total_num']= $this->productArea::where('product_id', $item->id)->count();
  55. $item['sell_num'] = $this->productArea::where('product_id', $item->id)->where('status', $this->productArea::STOP)->count();
  56. }
  57. $result = ['total' => $list->total(), 'rows' => $list->items()];
  58. return json($result);
  59. }
  60. /**
  61. * 添加
  62. *
  63. * @return string
  64. * @throws \think\Exception
  65. */
  66. public function add()
  67. {
  68. if (false === $this->request->isPost()) {
  69. $this->assignconfig('areaData', []);
  70. return $this->view->fetch();
  71. }
  72. $params = $this->request->post('row/a');
  73. if (empty($params)) {
  74. $this->error(__('Parameter %s can not be empty', ''));
  75. }
  76. $params = $this->preExcludeFields($params);
  77. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  78. $params[$this->dataLimitField] = $this->auth->id;
  79. }
  80. $result = false;
  81. Db::startTrans();
  82. try {
  83. //是否采用模型验证
  84. if ($this->modelValidate) {
  85. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  86. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  87. $this->model->validateFailException()->validate($validate);
  88. }
  89. $areaArr = json_decode($params['product_area'], true);
  90. $areaArrTxt = json_decode($params['product_area_txt'], true);
  91. if(empty($areaArr) || empty($areaArrTxt)) throw new ValidateException('请添加商品关联地区');
  92. unset($params['product_area'], $params['product_area_txt']);
  93. //商品
  94. $add = $this->model->create($params);
  95. $result= $this->setEqualArea($add->id, $areaArr, $areaArrTxt);
  96. Db::commit();
  97. } catch (ValidateException|PDOException|Exception $e) {
  98. Db::rollback();
  99. $this->error($e->getMessage());
  100. }
  101. if ($result === false) {
  102. $this->error(__('No rows were inserted'));
  103. }
  104. $this->success();
  105. }
  106. /**
  107. * 编辑
  108. *
  109. * @param $ids
  110. * @return string
  111. * @throws DbException
  112. * @throws \think\Exception
  113. */
  114. public function edit($ids = null)
  115. {
  116. $row = $this->model->get($ids);
  117. if (!$row) {
  118. $this->error(__('No Results were found'));
  119. }
  120. $adminIds = $this->getDataLimitAdminIds();
  121. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  122. $this->error(__('You have no permission'));
  123. }
  124. if (false === $this->request->isPost()) {
  125. $area = $this->productArea::where('product_id', $ids)->select();
  126. $areaData = array();
  127. $areaCode = array();
  128. $areaTxt = array();
  129. foreach ($area as $key =>$item) {
  130. if($item->province > 0){
  131. $areaData[$key] = $item->province;
  132. $areaCode[$key][0] = $item->province;
  133. }
  134. if($item->city > 0){
  135. $areaData[$key] = $item->city;
  136. $areaCode[$key][1] = $item->city;
  137. }
  138. if($item->area > 0) {
  139. $areaData[$key] = $item->area;
  140. $areaCode[$key][2] = $item->area;
  141. }
  142. if($item->county > 0) {
  143. $areaData[$key] = $item->county;
  144. $areaCode[$key][3] = $item->county;
  145. }
  146. $areaTxt[] = [$item->address];
  147. }
  148. $this->assignconfig('areaData', json_encode($areaData));
  149. $this->view->assign('row', $row);
  150. $this->view->assign('areaTxt', json_encode($areaTxt, JSON_UNESCAPED_UNICODE));
  151. $this->view->assign('areaCode', json_encode($areaCode));
  152. return $this->view->fetch();
  153. }
  154. $params = $this->request->post('row/a');
  155. if (empty($params)) {
  156. $this->error(__('Parameter %s can not be empty', ''));
  157. }
  158. $params = $this->preExcludeFields($params);
  159. $result = false;
  160. Db::startTrans();
  161. try {
  162. //是否采用模型验证
  163. if ($this->modelValidate) {
  164. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  165. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  166. $row->validateFailException()->validate($validate);
  167. }
  168. $areaArr = json_decode($params['product_area'], true);
  169. $areaArrTxt = json_decode($params['product_area_txt'], true);
  170. if(empty($areaArr) || empty($areaArrTxt)) throw new ValidateException('请添加商品关联地区');
  171. unset($params['product_area'], $params['product_area_txt']);
  172. $row->allowField(true)->save($params);
  173. $this->productArea::where('product_id', $ids)->where('status', 1)->delete();
  174. $result= $this->setEqualArea($ids, $areaArr, $areaArrTxt);
  175. Db::commit();
  176. } catch (ValidateException|PDOException|Exception $e) {
  177. Db::rollback();
  178. $this->error($e->getMessage());
  179. }
  180. if (false === $result) {
  181. $this->error(__('No rows were updated'));
  182. }
  183. $this->success();
  184. }
  185. //判断是否存在相同元素
  186. private function setEqualArea(int $product_id,array $areaArr, array $areaArrTxt): bool
  187. {
  188. foreach ($areaArr as $key=>$row)
  189. {
  190. $arr['product_id'] = $product_id;
  191. $arr['province'] = $row[0]??0;
  192. $arr['city'] = $row[1]??0 ;
  193. $arr['area'] = $row[2]??0;
  194. $arr['county'] = $row[3]??0;
  195. $arr['address'] = $areaArrTxt[$key][0];
  196. if($this->productArea::where('product_id',$arr['product_id'])
  197. ->where('province',$arr['province'])
  198. ->where('city',$arr['city'])->where('area',$arr['area'])
  199. ->where('county',$arr['county'])->where('status', 0)
  200. ->count() > 0) continue;
  201. $this->productArea::create($arr);
  202. }
  203. return true;
  204. }
  205. }