Coupon.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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\coupon;
  12. use app\common\repositories\store\coupon\StoreCouponSendRepository;
  13. use app\validate\merchant\StoreCouponSendValidate;
  14. use crmeb\basic\BaseController;
  15. use app\common\repositories\store\coupon\StoreCouponRepository;
  16. use app\common\repositories\store\coupon\StoreCouponUserRepository;
  17. use app\common\repositories\user\UserRepository;
  18. use app\validate\merchant\StoreCouponValidate;
  19. use FormBuilder\Exception\FormBuilderException;
  20. use think\App;
  21. use think\db\exception\DataNotFoundException;
  22. use think\db\exception\DbException;
  23. use think\db\exception\ModelNotFoundException;
  24. use think\exception\ValidateException;
  25. /**
  26. * Class CouponIssue
  27. * @package app\controller\merchant\store\coupon
  28. * @author xaboy
  29. * @day 2020-05-13
  30. */
  31. class Coupon extends BaseController
  32. {
  33. /**
  34. * @var StoreCouponRepository
  35. */
  36. protected $repository;
  37. /**
  38. * CouponIssue constructor.
  39. * @param App $app
  40. * @param StoreCouponRepository $repository
  41. */
  42. public function __construct(App $app, StoreCouponRepository $repository)
  43. {
  44. parent::__construct($app);
  45. $this->repository = $repository;
  46. }
  47. /**
  48. * 获取优惠券列表
  49. *
  50. * @return \Psr\Http\Message\ResponseInterface
  51. */
  52. public function lst()
  53. {
  54. $where = $this->request->params(['is_full_give', 'status', 'is_give_subscribe', 'coupon_name', 'send_type', 'type']);
  55. // 获取分页参数
  56. [$page, $limit] = $this->getPage();
  57. return app('json')->success($this->repository->getList($this->request->merId(), $where, $page, $limit));
  58. }
  59. /**
  60. * 获取优惠券详情
  61. *
  62. * @param int $id 优惠券ID
  63. * @return \think\response\Json
  64. */
  65. public function detail($id)
  66. {
  67. // 判断优惠券是否存在
  68. if (!$this->repository->merExists($this->request->merId(), $id))
  69. return app('json')->fail('数据不存在');
  70. // 获取优惠券信息并添加已使用人数和发放人数
  71. $coupon = $this->repository->get($id)->append(['used_num', 'send_num']);
  72. // 返回优惠券信息
  73. return app('json')->success($coupon->toArray());
  74. }
  75. /**
  76. * 创建表单
  77. *
  78. * @return \Illuminate\Http\JsonResponse
  79. */
  80. public function createForm()
  81. {
  82. // 调用 formToData 方法将表单转换为数据并返回 JSON 格式的成功响应
  83. return app('json')->success(formToData($this->repository->form()));
  84. }
  85. /**
  86. * 创建优惠券验证
  87. *
  88. * @param StoreCouponValidate $validate 优惠券验证对象
  89. * @return \Psr\Http\Message\ResponseInterface
  90. */
  91. public function create(StoreCouponValidate $validate)
  92. {
  93. // 获取商家ID
  94. $merId = $this->request->merId();
  95. // 检查参数并过滤
  96. $data = $this->checkParams($validate);
  97. // 添加商家ID到数据中
  98. $data['mer_id'] = $merId;
  99. // 调用仓库的创建方法
  100. $this->repository->create($data);
  101. // 返回成功信息
  102. return app('json')->success('发布成功');
  103. }
  104. /**
  105. * 检查参数并过滤无效数据
  106. * @param StoreCouponValidate $validate 优惠券验证器
  107. * @return array 处理后的参数数组
  108. * @throws ValidateException 参数校验失败时抛出异常
  109. */
  110. public function checkParams(StoreCouponValidate $validate)
  111. {
  112. $data = $this->request->params(['use_type', 'title', 'coupon_price', 'use_min_price', 'coupon_type', 'coupon_time', ['use_start_time', []], 'sort', ['status', 0], 'type', ['product_id', []], ['range_date', ''], ['send_type', 0], ['full_reduction', 0], ['is_limited', 0], ['is_timeout', 0], ['total_count', ''], ['status', 0]]);
  113. // 校验参数
  114. $validate->check($data);
  115. // 如果设置了领取时间限制
  116. if ($data['is_timeout']) {
  117. // 将有效期限转化为开始时间和结束时间
  118. [$data['start_time'], $data['end_time']] = $data['range_date'];
  119. // 如果结束时间小于当前时间,则抛出异常
  120. if (strtotime($data['end_time']) <= time())
  121. throw new ValidateException('优惠券领取结束时间不能小于当前');
  122. }
  123. // 如果没有设置使用类型,则将最低消费金额设置为0
  124. if (!$data['use_type']) $data['use_min_price'] = 0;
  125. unset($data['use_type']);
  126. if ($data['coupon_type']) {
  127. if (count(array_filter($data['use_start_time'])) != 2)
  128. throw new ValidateException('请选择有效期限');
  129. [$data['use_start_time'], $data['use_end_time']] = $data['use_start_time'];
  130. } else unset($data['use_start_time']);
  131. unset($data['range_date']);
  132. if ($data['is_limited'] == 0) $data['total_count'] = 0;
  133. if (!in_array($data['type'], [0, 1])) {
  134. throw new ValidateException('请选择有效的优惠券类型');
  135. }
  136. return $data;
  137. }
  138. /**
  139. * 修改状态
  140. *
  141. * @param int $id 商品ID
  142. * @return \think\response\Json
  143. */
  144. public function changeStatus($id)
  145. {
  146. // 获取请求参数中的状态值,默认为0
  147. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  148. // 判断商品是否存在
  149. if (!$this->repository->merExists($this->request->merId(), $id))
  150. return app('json')->fail('数据不存在');
  151. // 更新商品状态
  152. $this->repository->update($id, compact('status'));
  153. // 返回操作结果
  154. return app('json')->success('修改成功');
  155. }
  156. /**
  157. * 克隆表单
  158. *
  159. * @param int $id 表单ID
  160. * @return \Psr\Http\Message\ResponseInterface
  161. */
  162. public function cloneForm($id)
  163. {
  164. // 判断表单是否存在
  165. if (!$this->repository->merExists($this->request->merId(), $id))
  166. return app('json')->fail('数据不存在');
  167. // 克隆表单并返回成功响应
  168. return app('json')->success(formToData($this->repository->cloneCouponForm($id)));
  169. }
  170. /**
  171. * 获取优惠券列表
  172. *
  173. * @param StoreCouponUserRepository $repository 优惠券用户仓库
  174. * @return \Psr\Http\Message\ResponseInterface
  175. */
  176. public function issue(StoreCouponUserRepository $repository)
  177. {
  178. // 获取分页参数
  179. [$page, $limit] = $this->getPage();
  180. // 获取查询条件
  181. $where = $this->request->params(['username', 'coupon', 'status', 'coupon_id', 'type', 'send_id']);
  182. $where['mer_id'] = $this->request->merId();
  183. // 调用优惠券用户仓库的 getList 方法获取优惠券列表
  184. return app('json')->success($repository->getList($where, $page, $limit));
  185. }
  186. /**
  187. * 选择优惠券列表
  188. *
  189. * @return \Psr\Http\Message\ResponseInterface
  190. */
  191. public function select()
  192. {
  193. // 获取查询参数
  194. $where = $this->request->params(['coupon_name']);
  195. // 设置查询条件
  196. $where['status'] = 1;
  197. $where['send_type'] = 3;
  198. // 获取分页参数
  199. [$page, $limit] = $this->getPage();
  200. return app('json')->success($this->repository->getList($this->request->merId(), $where, $page, $limit));
  201. }
  202. /**
  203. * 发送优惠券
  204. *
  205. * @param StoreCouponSendValidate $validate 优惠券发送验证器
  206. * @param StoreCouponSendRepository $repository 优惠券发送仓库
  207. * @return \think\response\Json
  208. */
  209. public function send(StoreCouponSendValidate $validate, StoreCouponSendRepository $repository)
  210. {
  211. // 获取请求参数
  212. $data = $this->request->params(['coupon_id', 'mark', 'is_all', 'search', 'uid']);
  213. // 验证请求参数
  214. $validate->check($data);
  215. // 如果不是全部用户并且没有选择用户,则返回错误信息
  216. if (!$data['is_all'] && !count($data['uid'])) {
  217. return app('json')->fail('请选择发送用户');
  218. }
  219. // 创建优惠券发送记录
  220. $repository->create($data, $this->request->merId());
  221. // 返回成功信息
  222. return app('json')->success('创建成功,正在发送中');
  223. }
  224. /**
  225. * 根据ID删除数据
  226. *
  227. * @param int $id 数据ID
  228. * @return \Illuminate\Http\JsonResponse 返回JSON格式的响应结果
  229. */
  230. public function delete($id)
  231. {
  232. // 判断数据是否存在
  233. if (!$this->repository->merExists($this->request->merId(), $id))
  234. return app('json')->fail('数据不存在');
  235. // 删除数据
  236. $this->repository->delete($id);
  237. // 返回删除成功的响应结果
  238. return app('json')->success('删除成功');
  239. }
  240. /**
  241. * 获取更新表单数据
  242. *
  243. * @param int $id 数据ID
  244. * @return \Illuminate\Http\JsonResponse 返回JSON格式的响应结果
  245. */
  246. public function updateForm($id)
  247. {
  248. // 获取更新表单数据并转换为数组形式
  249. return app('json')->success(formToData($this->repository->updateForm($this->request->merId(), $id)));
  250. }
  251. /**
  252. * 更新指定ID的商品信息
  253. *
  254. * @param int $id 商品ID
  255. * @return \think\response\Json 返回JSON格式的操作结果
  256. */
  257. public function update($id)
  258. {
  259. // 判断商品是否存在
  260. if (!$this->repository->merExists($this->request->merId(), $id))
  261. return app('json')->fail('数据不存在');
  262. // 获取需要更新的商品信息
  263. $data = $this->request->params(['title']);
  264. // 调用商品仓库的更新方法
  265. $this->repository->update($id, $data);
  266. // 返回操作成功的JSON格式结果
  267. return app('json')->success('修改成功');
  268. }
  269. }