Coupon.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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\admin\store;
  12. use app\common\repositories\store\coupon\StoreCouponProductRepository;
  13. use app\common\repositories\store\coupon\StoreCouponRepository;
  14. use app\common\repositories\store\coupon\StoreCouponSendRepository;
  15. use app\common\repositories\store\coupon\StoreCouponUserRepository;
  16. use app\validate\merchant\StoreCouponSendValidate;
  17. use app\validate\merchant\StoreCouponValidate;
  18. use crmeb\basic\BaseController;
  19. use think\App;
  20. use think\db\exception\DataNotFoundException;
  21. use think\db\exception\DbException;
  22. use think\db\exception\ModelNotFoundException;
  23. use think\exception\ValidateException;
  24. /**
  25. * 优惠券
  26. */
  27. class Coupon extends BaseController
  28. {
  29. /**
  30. * @var StoreCouponRepository
  31. */
  32. protected $repository;
  33. /**
  34. * CouponIssue constructor.
  35. * @param App $app
  36. * @param StoreCouponRepository $repository
  37. */
  38. public function __construct(App $app, StoreCouponRepository $repository)
  39. {
  40. parent::__construct($app);
  41. $this->repository = $repository;
  42. }
  43. /**
  44. * 列表
  45. * @return mixed
  46. * @throws DbException
  47. * @throws DataNotFoundException
  48. * @throws ModelNotFoundException
  49. * @author xaboy
  50. * @day 2020-05-14
  51. */
  52. public function lst()
  53. {
  54. $where = $this->request->params(['is_full_give', 'status', 'is_give_subscribe', 'coupon_name', ['mer_id', null], 'is_trader']);
  55. [$page, $limit] = $this->getPage();
  56. $where['is_mer'] = 1;
  57. return app('json')->success($this->repository->getList($where['mer_id'], $where, $page, $limit));
  58. }
  59. /**
  60. * 详情
  61. * @param $id
  62. * @return \think\response\Json
  63. * @author Qinii
  64. */
  65. public function detail($id)
  66. {
  67. if (!$this->repository->exists($id))
  68. return app('json')->fail('数据不存在');
  69. $coupon = $this->repository->get($id)->append(['used_num', 'send_num']);
  70. return app('json')->success($coupon->toArray());
  71. }
  72. /**
  73. * 优惠券商品列表
  74. * @param $id
  75. * @return \think\response\Json
  76. * @author Qinii
  77. */
  78. public function showLst($id)
  79. {
  80. [$page, $limit] = $this->getPage();
  81. $data = $this->repository->getProductList($id, $page, $limit);
  82. return app('json')->success($data);
  83. }
  84. public function product($id)
  85. {
  86. $merId = $this->request->merId();
  87. if ($merId) {
  88. $exists = app()->make(StoreCouponRepository::class)->merExists($merId, $id);
  89. } else {
  90. $exists = app()->make(StoreCouponRepository::class)->exists($id);
  91. }
  92. if (!$exists) {
  93. return app('json')->fail('优惠券不存在');
  94. }
  95. [$page, $limit] = $this->getPage();
  96. return app('json')->success(app()->make(StoreCouponProductRepository::class)->productList((int)$id, $page, $limit));
  97. }
  98. /**
  99. * 领取记录
  100. * @param StoreCouponUserRepository $repository
  101. * @author xaboy
  102. * @day 2020/6/2
  103. */
  104. public function issue(StoreCouponUserRepository $repository)
  105. {
  106. [$page, $limit] = $this->getPage();
  107. $where = $this->request->params(['username', 'coupon_id', 'coupon', 'coupon_type', 'status', 'type', 'mer_id', ['is_mer', 1]]);
  108. return app('json')->success($repository->getList($where, $page, $limit));
  109. }
  110. /**
  111. * 平台领取记录
  112. * @param StoreCouponUserRepository $repository
  113. * @return \think\response\Json
  114. * @author Qinii
  115. * @day 2/26/22
  116. */
  117. public function platformIssue(StoreCouponUserRepository $repository)
  118. {
  119. [$page, $limit] = $this->getPage();
  120. $where = $this->request->params(['username', 'coupon_id', 'coupon', 'status', 'coupon_type', 'type', 'send_id']);
  121. $where['mer_id'] = 0;
  122. return app('json')->success($repository->getList($where, $page, $limit));
  123. }
  124. /**
  125. * 添加表单
  126. * @return \think\response\Json
  127. * @author Qinii
  128. */
  129. public function createForm()
  130. {
  131. $data = $this->repository->sysForm();
  132. return app('json')->success(formToData($data));
  133. }
  134. /**
  135. * 添加
  136. * @return \think\response\Json
  137. * @author Qinii
  138. */
  139. public function create()
  140. {
  141. $data = $this->checkParams();
  142. $this->repository->create($data);
  143. return app('json')->success('添加成功');
  144. }
  145. /**
  146. * 修改表单
  147. * @param $id
  148. * @return \think\response\Json
  149. * @author Qinii
  150. */
  151. public function updateForm($id)
  152. {
  153. return app('json')->success(formToData($this->repository->updateForm(0, $id)));
  154. }
  155. /**
  156. * 修改
  157. * @param $id
  158. * @return \think\response\Json
  159. * @author Qinii
  160. */
  161. public function update($id)
  162. {
  163. if (!$this->repository->merExists(0, $id))
  164. return app('json')->fail('数据不存在');
  165. $data = $this->request->params(['title']);
  166. $this->repository->update($id, $data);
  167. return app('json')->success('修改成功');
  168. }
  169. /**
  170. * 删除
  171. * @param $id
  172. * @return \think\response\Json
  173. * @author Qinii
  174. */
  175. public function delete($id)
  176. {
  177. if (!$this->repository->merExists(0, $id))
  178. return app('json')->fail('数据不存在');
  179. $this->repository->delete($id);
  180. return app('json')->success('删除成功');
  181. }
  182. /**
  183. * 状态修改
  184. * @param $id
  185. * @return \think\response\Json
  186. * @author Qinii
  187. */
  188. public function switchStatus($id)
  189. {
  190. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  191. if (!$this->repository->merExists(0, $id))
  192. return app('json')->fail('数据不存在');
  193. $this->repository->update($id, compact('status'));
  194. return app('json')->success('修改成功');
  195. }
  196. /**
  197. * 平台优惠券列表
  198. * @return \think\response\Json
  199. * @author Qinii
  200. */
  201. public function platformLst()
  202. {
  203. $where = $this->request->params(['is_full_give', 'status', 'is_give_subscribe', 'coupon_name', 'send_type', 'type', 'not_send_type']);
  204. [$page, $limit] = $this->getPage();
  205. $data = $this->repository->sysLst($where, $page, $limit);
  206. return app('json')->success($data);
  207. }
  208. /**
  209. * 克隆表单
  210. * @param $id
  211. * @return \think\response\Json
  212. * @author Qinii
  213. */
  214. public function cloneForm($id)
  215. {
  216. if (!$this->repository->merExists(0, $id))
  217. return app('json')->fail('数据不存在');
  218. return app('json')->success(formToData($this->repository->cloneSysCouponForm($id)));
  219. }
  220. /**
  221. * 优惠券参数验证
  222. * @param StoreCouponValidate $validate
  223. * @return array
  224. * @author xaboy
  225. * @day 2020/5/20
  226. */
  227. public function checkParams()
  228. {
  229. $data = $this->request->params([
  230. 'use_type',
  231. 'title',
  232. 'coupon_price',
  233. 'use_min_price',
  234. ['coupon_type', 0],
  235. ['coupon_time', 1],
  236. ['use_start_time', []],
  237. 'sort',
  238. ['status', 0],
  239. 'type',
  240. ['product_id', []],
  241. ['range_date', ''],
  242. ['send_type', 0],
  243. ['full_reduction', 0],
  244. ['is_limited', 0],
  245. ['is_timeout', 0],
  246. ['total_count', ''],
  247. ['status', 0],
  248. ['cate_ids', []],
  249. 'mer_type',
  250. 'is_trader',
  251. 'category_id',
  252. 'type_id',
  253. ['mer_ids', []],
  254. ]);
  255. app()->make(StoreCouponValidate::class)->check($data);
  256. if ($data['send_type'] == $this->repository::GET_COUPON_TYPE_SVIP) {
  257. $data['coupon_type'] = 0;
  258. $data['is_timeout'] = 0;
  259. }
  260. if ($data['is_timeout']) {
  261. [$data['start_time'], $data['end_time']] = $data['range_date'];
  262. if (strtotime($data['end_time']) <= time())
  263. throw new ValidateException('优惠券领取结束时间不能小于当前');
  264. }
  265. if (!$data['use_type']) $data['use_min_price'] = 0;
  266. unset($data['use_type']);
  267. if ($data['coupon_type']) {
  268. if (count(array_filter($data['use_start_time'])) != 2)
  269. throw new ValidateException('请选择有效期限');
  270. [$data['use_start_time'], $data['use_end_time']] = $data['use_start_time'];
  271. if ($data['use_start_time'] > $data['use_end_time']) {
  272. throw new ValidateException('使用开始时间小于结束时间');
  273. }
  274. } else unset($data['use_start_time']);
  275. unset($data['range_date']);
  276. if ($data['is_limited'] == 0) $data['total_count'] = 0;
  277. if (!in_array($data['type'], [10, 11, 12])) {
  278. throw new ValidateException('请选择有效的优惠券类型');
  279. }
  280. $data['mer_id'] = $this->request->merId();
  281. return $data;
  282. }
  283. public function send(StoreCouponSendValidate $validate, StoreCouponSendRepository $repository)
  284. {
  285. $data = $this->request->params(['coupon_id', 'mark', 'is_all', 'search', 'uid']);
  286. $validate->check($data);
  287. if (!$data['is_all'] && !count($data['uid'])) {
  288. return app('json')->fail('请选择发送用户');
  289. }
  290. $repository->create($data, 0);
  291. return app('json')->success('创建成功,正在发送中');
  292. }
  293. /**
  294. * 发送记录列表
  295. * @param StoreCouponSendRepository $repository
  296. * @return \think\response\Json
  297. * @author Qinii
  298. */
  299. public function sendLst(StoreCouponSendRepository $repository)
  300. {
  301. [$page, $limit] = $this->getPage();
  302. $where = $this->request->params(['date', 'coupon_type', 'coupon_name', 'status']);
  303. $where['mer_id'] = 0;
  304. return app('json')->success($repository->getList($where, $page, $limit));
  305. }
  306. }