Lists.php 7.9 KB

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