Discount.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace app\admin\controller\marketing;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use Exception;
  6. use think\exception\DbException;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 营销活动表(整体活动)
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Discount extends Backend
  15. {
  16. /**
  17. * Discount模型对象
  18. * @var \app\admin\model\marketing\Discount
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\marketing\discount\Discount;
  25. }
  26. /**
  27. * 规格折扣设置
  28. */
  29. public function spec_discount()
  30. {
  31. $goods_id = $this->request->get('goods_id');
  32. if (!$goods_id) {
  33. $this->error('参数错误');
  34. }
  35. if ($this->request->isAjax()) {
  36. // 处理提交的数据
  37. $specs = $this->request->post('specs', []);
  38. if ($specs) {
  39. $result = [
  40. 'goodsId' => $goods_id,
  41. 'specs' => $specs
  42. ];
  43. $this->success('设置成功', null, $result);
  44. }
  45. $this->error('提交数据失败');
  46. }
  47. // 获取商品信息
  48. $goods = Db::name('shop_goods')->where('id', $goods_id)->find();
  49. if (!$goods) {
  50. $this->error('商品不存在');
  51. }
  52. // 获取规格信息
  53. $goods_skus = Db::name('shop_goods_sku')
  54. ->where('goods_id', $goods_id)
  55. ->select();
  56. // 获取规格属性名称和值
  57. $sku_specs = [];
  58. foreach ($goods_skus as &$sku) {
  59. $sku_id_arr = explode(',', $sku['sku_id']);
  60. // 查询规格名称和值
  61. $spec_values = Db::name('shop_goods_sku_spec')
  62. ->alias('p')
  63. ->field('p.*, sp.name as spec_name, sv.value as spec_value')
  64. ->join('shop_spec sp', 'sp.id=p.spec_id', 'LEFT')
  65. ->join('shop_spec_value sv', 'sv.id=p.spec_value_id', 'LEFT')
  66. ->where('p.id', 'in', $sku_id_arr)
  67. ->select();
  68. $specs_text = [];
  69. foreach ($spec_values as $spec) {
  70. $specs_text[] = $spec['spec_name'] . ':' . $spec['spec_value'];
  71. }
  72. $sku['specs_text'] = implode(' | ', $specs_text);
  73. }
  74. $this->view->assign('goods', $goods);
  75. $this->view->assign('skus', $goods_skus);
  76. return $this->view->fetch();
  77. }
  78. /**
  79. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  80. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  81. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  82. */
  83. /**
  84. * 添加
  85. *
  86. * @return string
  87. * @throws \think\Exception
  88. */
  89. public function add()
  90. {
  91. if (false === $this->request->isPost()) {
  92. return $this->view->fetch();
  93. }
  94. $params = $this->request->post('row/a');
  95. if (empty($params)) {
  96. $this->error(__('Parameter %s can not be empty', ''));
  97. }
  98. $params = $this->preExcludeFields($params);
  99. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  100. $params[$this->dataLimitField] = $this->auth->id;
  101. }
  102. $result = false;
  103. Db::startTrans();
  104. try {
  105. //是否采用模型验证
  106. if ($this->modelValidate) {
  107. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  108. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  109. $this->model->validateFailException()->validate($validate);
  110. }
  111. $result = $this->model->allowField(true)->save($params);
  112. Db::commit();
  113. } catch (ValidateException|PDOException|Exception $e) {
  114. Db::rollback();
  115. $this->error($e->getMessage());
  116. }
  117. if ($result === false) {
  118. $this->error(__('No rows were inserted'));
  119. }
  120. $this->success();
  121. }
  122. /**
  123. * 编辑
  124. *
  125. * @param $ids
  126. * @return string
  127. * @throws DbException
  128. * @throws \think\Exception
  129. */
  130. public function edit($ids = null)
  131. {
  132. $row = $this->model->get($ids);
  133. if (!$row) {
  134. $this->error(__('No Results were found'));
  135. }
  136. $adminIds = $this->getDataLimitAdminIds();
  137. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  138. $this->error(__('You have no permission'));
  139. }
  140. if (false === $this->request->isPost()) {
  141. $this->view->assign('row', $row);
  142. return $this->view->fetch();
  143. }
  144. $params = $this->request->post('row/a');
  145. if (empty($params)) {
  146. $this->error(__('Parameter %s can not be empty', ''));
  147. }
  148. $params = $this->preExcludeFields($params);
  149. $result = false;
  150. Db::startTrans();
  151. try {
  152. //是否采用模型验证
  153. if ($this->modelValidate) {
  154. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  155. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  156. $row->validateFailException()->validate($validate);
  157. }
  158. $result = $row->allowField(true)->save($params);
  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. }