StoreCouponIssue.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\v1\marketing\coupon;
  12. use app\controller\admin\AuthController;
  13. use app\services\activity\coupon\StoreCouponIssueServices;
  14. use app\services\activity\coupon\StoreCouponProductServices;
  15. use app\services\product\brand\StoreBrandServices;
  16. use app\services\product\category\StoreProductCategoryServices;
  17. use app\services\product\product\StoreProductCouponServices;
  18. use app\services\product\product\StoreProductServices;
  19. use app\services\store\SystemStoreServices;
  20. use think\facade\App;
  21. /**
  22. * 已发布优惠券管理
  23. * Class StoreCouponIssue
  24. * @package app\controller\admin\v1\marketing
  25. */
  26. class StoreCouponIssue extends AuthController
  27. {
  28. public function __construct(App $app, StoreCouponIssueServices $services)
  29. {
  30. parent::__construct($app);
  31. $this->services = $services;
  32. }
  33. /**
  34. * 获取优惠券列表
  35. * @return mixed
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function index()
  41. {
  42. $where = $this->request->getMore([
  43. ['status', 1],
  44. ['coupon_title', ''],
  45. ['receive_type', ''],
  46. ['coupon_type', ''],
  47. ['type', '', '' , 'receive'],
  48. ]);
  49. $list = $this->services->getCouponIssueList($where);
  50. return $this->success($list);
  51. }
  52. /**
  53. * 添加优惠券
  54. * @return mixed
  55. */
  56. public function saveCoupon()
  57. {
  58. $data = $this->request->postMore([
  59. ['coupon_title', ''],
  60. ['coupon_price', 0.00],
  61. ['use_min_price', 0.00],
  62. ['coupon_time', 0],
  63. ['start_use_time', 0],
  64. ['end_use_time', 0],
  65. ['start_time', 0],
  66. ['end_time', 0],
  67. ['receive_type', 0],
  68. ['is_permanent', 0],
  69. ['total_count', 0],
  70. ['product_id', ''],
  71. ['category_id', []],
  72. ['brand_id', []],
  73. ['type', 0],
  74. ['sort', 0],
  75. ['status', 0],
  76. ['coupon_type', 1],
  77. ['applicable_type', 1],//适用门店类型
  78. ['applicable_store_id', []],//适用门店IDS
  79. ]);
  80. if ($data['applicable_type'] == 1) {
  81. $data['applicable_store_id'] = [];
  82. } elseif ($data['applicable_type'] == 2) {
  83. if (!$data['applicable_store_id']) {
  84. return $this->fail('请选择要适用门店');
  85. }
  86. }
  87. if ($data['category_id']) {
  88. $data['category_id'] = end($data['category_id']);
  89. }
  90. if ($data['brand_id']) {
  91. $data['brand_id'] = end($data['brand_id']);
  92. }
  93. if ($data['start_time'] && $data['start_use_time']) {
  94. if ($data['start_use_time'] < $data['start_time']) {
  95. return $this->fail('使用开始时间不能小于领取开始时间');
  96. }
  97. }
  98. if ($data['end_time'] && $data['end_use_time']) {
  99. if ($data['end_use_time'] < $data['end_time']) {
  100. return $this->fail('使用结束时间不能小于领取结束时间');
  101. }
  102. }
  103. //复制优惠券,清空其他数据
  104. switch ($data['type']) {
  105. case 0://通用
  106. $data['product_id'] = '';
  107. $data['category_id'] = 0;
  108. $data['brand_id'] = 0;
  109. break;
  110. case 1://分类
  111. $data['product_id'] = '';
  112. $data['brand_id'] = 0;
  113. break;
  114. case 2://商品
  115. $data['category_id'] = 0;
  116. $data['brand_id'] = 0;
  117. break;
  118. case 3://品牌
  119. $data['product_id'] = '';
  120. $data['category_id'] = 0;
  121. break;
  122. }
  123. //赠送券 、新人券不限量
  124. if (in_array($data['receive_type'], [2, 3])) {
  125. $data['is_permanent'] = 1;
  126. $data['total_count'] = 0;
  127. }
  128. if (!$data['coupon_price']) {
  129. return $this->fail($data['coupon_type'] == 1 ? '请输入优惠券金额' : '请输入优惠券折扣');
  130. }
  131. if ($data['coupon_type'] == 2 && ($data['coupon_price'] < 0 || $data['coupon_price'] > 100)) {
  132. return $this->fail('优惠券折扣为0~100数字');
  133. }
  134. $res = $this->services->saveCoupon($data);
  135. if ($res) return $this->success('添加成功!');
  136. }
  137. /**
  138. * 修改优惠券状态
  139. * @param $id
  140. * @param $status
  141. * @return mixed
  142. */
  143. public function status($id, $status)
  144. {
  145. $this->services->update($id, ['status' => $status]);
  146. return $this->success('修改成功');
  147. }
  148. /**
  149. * 复制优惠券获取优惠券详情
  150. * @param int $id
  151. * @return mixed
  152. */
  153. public function copy($id = 0)
  154. {
  155. if (!$id) return $this->fail('参数错误');
  156. $info = $this->services->get($id);
  157. if ($info) $info = $info->toArray();
  158. if ($info['product_id'] != '') {
  159. $productIds = explode(',', $info['product_id']);
  160. /** @var StoreProductServices $product */
  161. $product = app()->make(StoreProductServices::class);
  162. $productImages = $product->getColumn([['id', 'in', $productIds]], 'image', 'id');
  163. foreach ($productIds as $item) {
  164. $info['productInfo'][] = [
  165. 'product_id' => $item,
  166. 'image' => $productImages[$item]
  167. ];
  168. }
  169. }
  170. if ($info['category_id'] != '') {
  171. /** @var StoreProductCategoryServices $categoryServices */
  172. $categoryServices = app()->make(StoreProductCategoryServices::class);
  173. $category = $categoryServices->get($info['category_id']);
  174. if ($category && $category['pid']) {
  175. $info['category_id'] = [$category['pid'], $info['category_id']];
  176. } else {
  177. $info['category_id'] = [$info['category_id']];
  178. }
  179. }
  180. if ($info['brand_id'] != '') {
  181. /** @var StoreBrandServices $brandServices */
  182. $brandServices = app()->make(StoreBrandServices::class);
  183. $brand = $brandServices->get($info['brand_id']);
  184. if ($brand && $brand['pid']) {
  185. $info['brand_id'] = [$brand['pid'], $info['brand_id']];
  186. } else {
  187. $info['brand_id'] = [$info['brand_id']];
  188. }
  189. }
  190. //适用门店
  191. $info['stores'] = [];
  192. if (isset($info['applicable_type']) && ($info['applicable_type'] == 1 || ($info['applicable_type'] == 2 && isset($info['applicable_store_id']) && $info['applicable_store_id']))) {//查询门店信息
  193. $where = ['is_del' => 0];
  194. if ($info['applicable_type'] == 2) {
  195. $store_ids = is_array($info['applicable_store_id']) ? $info['applicable_store_id'] : explode(',', $info['applicable_store_id']);
  196. $where['id'] = $store_ids;
  197. }
  198. $field = ['id', 'cate_id', 'name', 'phone', 'address', 'detailed_address', 'image', 'is_show', 'day_time', 'day_start', 'day_end'];
  199. /** @var SystemStoreServices $storeServices */
  200. $storeServices = app()->make(SystemStoreServices::class);
  201. $storeData = $storeServices->getStoreList($where, $field, '', '', 0, ['categoryName']);
  202. $info['stores'] = $storeData['list'] ?? [];
  203. }
  204. return $this->success($info);
  205. }
  206. /**
  207. * 删除
  208. * @param string $id
  209. * @return mixed
  210. */
  211. public function delete($id)
  212. {
  213. $this->services->update($id, ['is_del' => 1]);
  214. /** @var StoreProductCouponServices $storeProductService */
  215. $storeProductService = app()->make(StoreProductCouponServices::class);
  216. //删除商品关联这个优惠券
  217. $storeProductService->delete(['issue_coupon_id' => $id]);
  218. /** @var StoreCouponProductServices $storeCouponProductService */
  219. $storeCouponProductService = app()->make(StoreCouponProductServices::class);
  220. //删除商品券辅助信息
  221. $storeCouponProductService->delete(['coupon_id' => $id]);
  222. return $this->success('删除成功!');
  223. }
  224. /**
  225. * 修改状态
  226. * @param $id
  227. * @return mixed
  228. * @throws \FormBuilder\Exception\FormBuilderException
  229. */
  230. public function edit($id)
  231. {
  232. return $this->success($this->services->createForm($id));
  233. }
  234. /**
  235. * 领取记录
  236. * @param string $id
  237. * @return mixed|string
  238. */
  239. public function issue_log($id)
  240. {
  241. $list = $this->services->issueLog($id);
  242. return $this->success($list);
  243. }
  244. }