Lists.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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\ProductOrder;
  7. use app\common\model\ProductArea;
  8. use think\exception\DbException;
  9. use think\exception\PDOException;
  10. use think\exception\ValidateException;
  11. /**
  12. * 商品列管理
  13. *
  14. * @icon fa fa-circle-o
  15. */
  16. class Lists extends Backend
  17. {
  18. protected $multiFields = 'status,is_transfer,is_gift,is_freight,is_teac' ;
  19. protected $model = null;
  20. protected $productArea = null;
  21. protected $areaCode = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->productArea = new ProductArea;
  26. $this->model = new \app\common\model\ProductLists;
  27. }
  28. /**
  29. * 查看
  30. *
  31. * @return string|Json
  32. * @throws \think\Exception
  33. * @throws DbException
  34. */
  35. public function index()
  36. {
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if (false === $this->request->isAjax()) {
  40. return $this->view->fetch();
  41. }
  42. //如果发送的来源是 Selectpage,则转发到 Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  47. $list = $this->model->with('products')
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->paginate($limit);
  51. $productOrder = new ProductOrder();
  52. foreach ($list as &$item) {
  53. $item['hold_num'] = $productOrder::where('product_id', $item->id)->where('status', 'in',[$productOrder::Paid, $productOrder::Transferred, $productOrder::Freeze])->count();//持有数量
  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. return $this->view->fetch();
  70. }
  71. $params = $this->request->post('row/a');
  72. if (empty($params)) {
  73. $this->error(__('Parameter %s can not be empty', ''));
  74. }
  75. $params = $this->preExcludeFields($params);
  76. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  77. $params[$this->dataLimitField] = $this->auth->id;
  78. }
  79. $result = false;
  80. Db::startTrans();
  81. try {
  82. //是否采用模型验证
  83. if ($this->modelValidate) {
  84. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  85. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  86. $this->model->validateFailException()->validate($validate);
  87. }
  88. //商品
  89. $result = $this->model->create($params);
  90. //if(!empty($params['is_area'])) $result= self::setEqualArea($result->id, $areaArr, $areaArrTxt, 0);
  91. Db::commit();
  92. } catch (ValidateException|PDOException|Exception $e) {
  93. Db::rollback();
  94. $this->error($e->getMessage());
  95. }
  96. if ($result === false) {
  97. $this->error(__('No rows were inserted'));
  98. }
  99. $this->success();
  100. }
  101. /**
  102. * 编辑
  103. * @param $ids
  104. * @return string
  105. * @throws DbException
  106. * @throws \think\Exception
  107. */
  108. public function edit($ids = null)
  109. {
  110. $row = $this->model->get($ids);
  111. if (!$row) {
  112. $this->error(__('No Results were found'));
  113. }
  114. $adminIds = $this->getDataLimitAdminIds();
  115. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  116. $this->error(__('You have no permission'));
  117. }
  118. if (false === $this->request->isPost()) {
  119. $this->view->assign('row', $row);
  120. return $this->view->fetch();
  121. }
  122. $params = $this->request->post('row/a');
  123. if (empty($params)) {
  124. $this->error(__('Parameter %s can not be empty', ''));
  125. }
  126. $params = $this->preExcludeFields($params);
  127. $result = false;
  128. Db::startTrans();
  129. try {
  130. //是否采用模型验证
  131. if ($this->modelValidate) {
  132. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  133. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  134. $row->validateFailException()->validate($validate);
  135. }
  136. $result=$row->allowField(true)->save($params);
  137. Db::commit();
  138. } catch (ValidateException|PDOException|Exception $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. }
  142. if (false === $result) {
  143. $this->error(__('No rows were updated'));
  144. }
  145. $this->success();
  146. }
  147. //判断是否存在相同元素
  148. private function setEqualArea(int $product_id,array $areaArr, array $areaArrTxt, int $type): bool
  149. {
  150. foreach ($areaArr as $key=>$row)
  151. {
  152. $arr['product_id'] = $product_id;
  153. $arr['province'] = $row[0]??0;
  154. $arr['city'] = $row[1]??0 ;
  155. $arr['area'] = $row[2]??0;
  156. $arr['county'] = $row[3]??0;
  157. $arr['address'] = $areaArrTxt[$key][0];
  158. if(!empty($type)){
  159. if($this->productArea::where('product_id',$arr['product_id'])
  160. ->where('province',$arr['province'])
  161. ->where('city',$arr['city'])->where('area',$arr['area'])
  162. ->where('county',$arr['county'])
  163. ->count() > 0) continue;
  164. }
  165. $this->productArea::create($arr);
  166. }
  167. return true;
  168. }
  169. }