Lists.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. protected $multiFields = 'status,is_transfer,is_gift,is_freight,is_teac' ;
  18. protected $model = null;
  19. protected $productArea = null;
  20. protected $areaCode = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->productArea = new ProductArea;
  25. $this->model = new \app\common\model\ProductLists;
  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('products')
  47. ->where($where)
  48. ->order($sort, $order)
  49. ->paginate($limit);
  50. foreach ($list as &$item) {
  51. $item['total_num']= $this->productArea::where('product_id', $item->id)->count();
  52. $item['sell_num'] = $this->productArea::where('product_id', $item->id)->where('status', $this->productArea::Stop)->count();
  53. }
  54. $result = ['total' => $list->total(), 'rows' => $list->items()];
  55. return json($result);
  56. }
  57. /**
  58. * 添加
  59. *
  60. * @return string
  61. * @throws \think\Exception
  62. */
  63. public function add()
  64. {
  65. if (false === $this->request->isPost()) {
  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. //商品
  86. $result = $this->model->create($params);
  87. //if(!empty($params['is_area'])) $result= self::setEqualArea($result->id, $areaArr, $areaArrTxt, 0);
  88. Db::commit();
  89. } catch (ValidateException|PDOException|Exception $e) {
  90. Db::rollback();
  91. $this->error($e->getMessage());
  92. }
  93. if ($result === false) {
  94. $this->error(__('No rows were inserted'));
  95. }
  96. $this->success();
  97. }
  98. /**
  99. * 编辑
  100. * @param $ids
  101. * @return string
  102. * @throws DbException
  103. * @throws \think\Exception
  104. */
  105. public function edit($ids = null)
  106. {
  107. $row = $this->model->get($ids);
  108. if (!$row) {
  109. $this->error(__('No Results were found'));
  110. }
  111. $adminIds = $this->getDataLimitAdminIds();
  112. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  113. $this->error(__('You have no permission'));
  114. }
  115. if (false === $this->request->isPost()) {
  116. $this->view->assign('row', $row);
  117. return $this->view->fetch();
  118. }
  119. $params = $this->request->post('row/a');
  120. if (empty($params)) {
  121. $this->error(__('Parameter %s can not be empty', ''));
  122. }
  123. $params = $this->preExcludeFields($params);
  124. $result = false;
  125. Db::startTrans();
  126. try {
  127. //是否采用模型验证
  128. if ($this->modelValidate) {
  129. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  130. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  131. $row->validateFailException()->validate($validate);
  132. }
  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. //判断是否存在相同元素
  145. private function setEqualArea(int $product_id,array $areaArr, array $areaArrTxt, int $type): bool
  146. {
  147. foreach ($areaArr as $key=>$row)
  148. {
  149. $arr['product_id'] = $product_id;
  150. $arr['province'] = $row[0]??0;
  151. $arr['city'] = $row[1]??0 ;
  152. $arr['area'] = $row[2]??0;
  153. $arr['county'] = $row[3]??0;
  154. $arr['address'] = $areaArrTxt[$key][0];
  155. if(!empty($type)){
  156. if($this->productArea::where('product_id',$arr['product_id'])
  157. ->where('province',$arr['province'])
  158. ->where('city',$arr['city'])->where('area',$arr['area'])
  159. ->where('county',$arr['county'])
  160. ->count() > 0) continue;
  161. }
  162. $this->productArea::create($arr);
  163. }
  164. return true;
  165. }
  166. }