Discounts.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\merchant\store\product;
  12. use app\common\repositories\store\product\StoreDiscountRepository;
  13. use app\validate\merchant\StoreDiscountsValidate;
  14. use crmeb\basic\BaseController;
  15. use think\App;
  16. use think\exception\ValidateException;
  17. class Discounts extends BaseController
  18. {
  19. protected $repository ;
  20. /**
  21. * Product constructor.
  22. * @param App $app
  23. * @param StoreDiscountRepository $repository
  24. */
  25. public function __construct(App $app ,StoreDiscountRepository $repository)
  26. {
  27. parent::__construct($app);
  28. $this->repository = $repository;
  29. }
  30. /**
  31. * 获取列表
  32. */
  33. public function lst()
  34. {
  35. // 获取分页参数
  36. [$page, $limit] = $this->getPage();
  37. // 获取状态参数
  38. $status = $this->request->param('status');
  39. // 获取其他查询条件
  40. $where = $this->request->params(['keyword', 'store_name', 'title', 'type']);
  41. // 设置商家ID查询条件
  42. $where['is_show'] = $status;
  43. $where['mer_id'] = $this->request->merId();
  44. // 调用仓库方法获取数据
  45. $data = $this->repository->getMerlist($where, $page, $limit);
  46. // 返回JSON格式数据
  47. return app('json')->success($data);
  48. }
  49. /**
  50. * 创建数据
  51. */
  52. public function create()
  53. {
  54. // 校验参数
  55. $data = $this->checkParams();
  56. // 保存数据
  57. $this->repository->save($data);
  58. return app('json')->success('添加成功');
  59. }
  60. /**
  61. * 更新数据
  62. * @param int $id 数据ID
  63. */
  64. public function update($id)
  65. {
  66. // 校验参数
  67. $data = $this->checkParams();
  68. // 判断数据是否存在
  69. if (!$this->repository->getWhere(['mer_id' => $data['mer_id'], $this->repository->getPk() => $id]))
  70. return app('json')->fail('数据不存在');
  71. // 设置主键值
  72. $data['discount_id'] = $id;
  73. // 保存数据
  74. $this->repository->save($data);
  75. return app('json')->success('编辑成功');
  76. }
  77. /**
  78. * 获取详情
  79. * @param int $id 数据ID
  80. */
  81. public function detail($id)
  82. {
  83. // 获取数据
  84. $data = $this->repository->detail($id, $this->request->merId());
  85. if (!$data) return app('json')->fail('数据不存在');
  86. // 返回JSON格式数据
  87. return app('json')->success($data);
  88. }
  89. /**
  90. * 切换状态
  91. * @param int $id 数据ID
  92. */
  93. public function switchStatus($id)
  94. {
  95. $status = $this->request->param('status') == 1 ?: 0;
  96. if (!$this->repository->getWhere([$this->repository->getPk() => $id, 'mer_id' => $this->request->merId()]))
  97. return app('json')->fail('数据不存在');
  98. $this->repository->update($id, ['is_show' => $status]);
  99. return app('json')->success('修改成功');
  100. }
  101. /**
  102. * 根据ID删除数据
  103. * @param int $id 数据ID
  104. * @return \think\response\Json 返回JSON格式的删除结果
  105. */
  106. public function delete($id)
  107. {
  108. // 判断数据是否存在
  109. if (!$this->repository->getWhere([$this->repository->getPk() => $id, 'mer_id' => $this->request->merId()]))
  110. return app('json')->fail('数据不存在');
  111. // 更新数据状态为已删除
  112. $this->repository->update($id, ['is_del' => 1]);
  113. // 返回删除成功的JSON格式数据
  114. return app('json')->success('删除成功');
  115. }
  116. /**
  117. * 校验参数
  118. * @return array 返回校验后的参数数组
  119. * @throws \app\common\exception\ValidateException 如果参数校验失败则抛出异常
  120. */
  121. public function checkParams()
  122. {
  123. // 定义参数数组
  124. $params = [
  125. ['title', ''],
  126. ['image', ''],
  127. ['type', 0],
  128. ['is_limit', 0],
  129. ['limit_num', 0],
  130. ['is_time', 0],
  131. ['time', []],
  132. ['sort', 0],
  133. ['free_shipping', 0],
  134. ['status', 0],
  135. ['is_show', 1],
  136. ['products', []],
  137. ['temp_id', 0],
  138. ];
  139. // 获取请求参数并校验
  140. $data = $this->request->params($params);
  141. app()->make(StoreDiscountsValidate::class)->check($data);
  142. // 如果设置了时间限制,则校验时间格式和时间范围
  143. if ($data['is_time'] && is_array($data['time'])) {
  144. if (empty($data['time'])) throw new ValidateException('开始时间必须填写');
  145. [$start, $end] = $data['time'];
  146. $start = strtotime($start);
  147. $end = strtotime($end);
  148. if ($start > $end) {
  149. throw new ValidateException('开始时间必须小于结束时间');
  150. }
  151. if ($start < time() || $end < time()) {
  152. throw new ValidateException('套餐时间不能小于当前时间');
  153. }
  154. }
  155. // 校验商品属性
  156. foreach ($data['products'] as $item) {
  157. if (!isset($item['items']))
  158. throw new ValidateException('请选择' . $item['store_name'] . '的规格');
  159. foreach ($item['attr'] as $attr) {
  160. if ($attr['active_price'] > $attr['price']) throw new ValidateException('套餐价格高于原价');
  161. }
  162. }
  163. $data['mer_id'] = $this->request->merId();
  164. return $data;
  165. }
  166. }