StoreCouponService.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\activity\coupon;
  12. use app\dao\activity\coupon\StoreCouponDao;
  13. use app\services\BaseServices;
  14. use app\services\order\StoreCartServices;
  15. use app\services\product\product\StoreCategoryServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\services\FormBuilder as Form;
  18. use think\facade\Route as Url;
  19. /**
  20. * Class StoreCouponService
  21. * @package app\services\coupon
  22. * @method save(array $data)
  23. */
  24. class StoreCouponService extends BaseServices
  25. {
  26. public function __construct(StoreCouponDao $dao)
  27. {
  28. $this->dao = $dao;
  29. }
  30. /**
  31. * 获取列表
  32. * @param array $where
  33. * @return array
  34. */
  35. public function getList(array $where)
  36. {
  37. [$page, $limit] = $this->getPageValue();
  38. $where['is_del'] = 0;
  39. $list = $this->dao->getList($where, $page, $limit);
  40. $count = $this->dao->count($where);
  41. return compact('list', 'count');
  42. }
  43. /**
  44. * 添加优惠券表单
  45. * @param int $type
  46. * @return array
  47. * @throws \FormBuilder\Exception\FormBuilderException
  48. */
  49. public function createForm(int $type)
  50. {
  51. $f[] = Form::input('title', '优惠券名称');
  52. switch ($type) {
  53. case 1://品类券
  54. $options = function () {
  55. /** @var StoreCategoryServices $storeCategoryService */
  56. $storeCategoryService = app()->make(StoreCategoryServices::class);
  57. $list = $storeCategoryService->getTierList(1, 1);
  58. $menus = [];
  59. foreach (sort_list_tier($list) as $menu) {
  60. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => false];
  61. }
  62. return $menus;
  63. };
  64. $f[] = Form::select('category_id', '选择品类')->setOptions(Form::setOptions($options))->filterable(1)->col(12);
  65. break;
  66. case 2://商品券
  67. $f[] = Form::frameImages('image', '商品', Url::buildUrl(config('app.admin_prefix', 'admin') . '/store.StoreProduct/index', array('fodder' => 'image', 'type' => 'many')))->icon('el-icon-picture-outline')->width('950px')->height('560px')->props(['srcKey' => 'image', 'footer' => false]);
  68. $f[] = Form::hidden('product_id', '');
  69. break;
  70. }
  71. $f[] = Form::number('coupon_price', '优惠券面值', 0)->min(0);
  72. $f[] = Form::number('use_min_price', '优惠券最低消费', 0)->min(0);
  73. $f[] = Form::number('coupon_time', '优惠券有效期限', 0)->min(0);
  74. $f[] = Form::number('sort', '排序')->value(0)->precision(0);
  75. $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  76. $f[] = Form::hidden('type', $type);
  77. return create_form('添加优惠券', $f, Url::buildUrl('/marketing/coupon/save'), 'POST');
  78. }
  79. /**
  80. * 优惠卷模板修改表单
  81. * @param int $id
  82. * @return array
  83. * @throws \FormBuilder\Exception\FormBuilderException
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. */
  88. public function createIssue(int $id)
  89. {
  90. $res = $this->dao->getOne(['id' => $id, 'status' => 1, 'is_del' => 0]);
  91. if (!$res) throw new AdminException('发布的优惠劵已失效或不存在!');
  92. $f = [];
  93. $f[] = Form::input('id', '优惠劵ID', $id)->disabled(1);
  94. $f[] = Form::input('coupon_title', '优惠劵名称', $res['title'])->disabled(1);
  95. $f[] = Form::dateTimeRange('range_date', '领取时间')->placeholder('不填为永久有效');
  96. $f[] = Form::radio('is_permanent', '是否限量', 1)->options([['label' => '不限量', 'value' => 1], ['label' => '限量', 'value' => 0]]);
  97. $f[] = Form::number('count', '发布数量', 0)->min(0)->placeholder('不填或填0,为不限量');
  98. $f[] = Form::radio('is_type', '优惠券类型', 0)->options([['label' => '普通券', 'value' => 0], ['label' => '赠送券', 'value' => 1], ['label' => '新人券', 'value' => 2]]);
  99. $f[] = Form::number('full_reduction', '满赠金额', 0)->min(0)->placeholder('赠送优惠券的最低消费金额');
  100. $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  101. return create_form('发布优惠券', $f, $this->url('/marketing/coupon/issue/' . $id), 'POST');
  102. }
  103. /**
  104. * 发布优惠券
  105. * @param int $id
  106. * @param int $_id
  107. * @param string $coupon_title
  108. * @param array $rangeTime
  109. * @param int $count
  110. * @param int $status
  111. * @param int $is_permanent
  112. * @param float $full_reduction
  113. * @param int $is_give_subscribe
  114. * @param int $is_full_give
  115. * @param int $is_type
  116. * @throws \think\db\exception\DataNotFoundException
  117. * @throws \think\db\exception\DbException
  118. * @throws \think\db\exception\ModelNotFoundException
  119. */
  120. public function upIssue(int $id, int $_id, string $coupon_title, array $rangeTime, int $count, int $status, int $is_permanent, float $full_reduction, int $is_give_subscribe, int $is_full_give, int $is_type)
  121. {
  122. if ($is_type == 1) {
  123. $is_full_give = 1;
  124. } elseif ($is_type == 2) {
  125. $is_give_subscribe = 1;
  126. }
  127. if ($_id != $id) throw new AdminException('操作失败,信息不对称');
  128. if (!$count) $count = 0;
  129. $couponInfo = $this->dao->getOne(['id' => $id, 'status' => 1, 'is_del' => 0]);
  130. if (!$couponInfo) throw new AdminException('发布的优惠劵已失效或不存在!');
  131. if (count($rangeTime) != 2) throw new AdminException('请选择正确的时间区间');
  132. list($startTime, $endTime) = $rangeTime;
  133. if (!$startTime) $startTime = 0;
  134. if (!$endTime) $endTime = 0;
  135. if (!$startTime && $endTime) throw new AdminException('请选择正确的开始时间');
  136. if ($startTime && !$endTime) throw new AdminException('请选择正确的结束时间');
  137. $data['cid'] = $id;
  138. $data['coupon_title'] = $coupon_title;
  139. $data['start_time'] = strtotime($startTime);
  140. $data['end_time'] = strtotime($endTime);
  141. $data['total_count'] = $count;
  142. $data['remain_count'] = $count;
  143. $data['is_permanent'] = $is_permanent;
  144. $data['status'] = $status;
  145. $data['is_give_subscribe'] = $is_give_subscribe;
  146. $data['is_full_give'] = $is_full_give;
  147. $data['full_reduction'] = $full_reduction;
  148. $data['is_del'] = 0;
  149. $data['add_time'] = time();
  150. $data['title'] = $couponInfo['title'];
  151. $data['integral'] = $couponInfo['integral'];
  152. $data['coupon_price'] = $couponInfo['coupon_price'];
  153. $data['use_min_price'] = $couponInfo['use_min_price'];
  154. $data['coupon_time'] = $couponInfo['coupon_time'];
  155. $data['product_id'] = $couponInfo['product_id'];
  156. $data['category_id'] = $couponInfo['category_id'];
  157. $data['type'] = $couponInfo->getData('type');
  158. /** @var StoreCouponIssueServices $storeCouponIssueService */
  159. $storeCouponIssueService = app()->make(StoreCouponIssueServices::class);
  160. $res = $storeCouponIssueService->save($data);
  161. $productIds = explode(',', $data['product_id']);
  162. if (count($productIds)) {
  163. $couponData = [];
  164. foreach ($productIds as $product_id) {
  165. $couponData[] = ['product_id' => $product_id, 'coupon_id' => $res->id];
  166. }
  167. /** @var StoreCouponProductServices $storeCouponProductService */
  168. $storeCouponProductService = app()->make(StoreCouponProductServices::class);
  169. $storeCouponProductService->saveAll($couponData);
  170. }
  171. if (!$res) throw new AdminException('发布优惠劵失败!');
  172. }
  173. /**
  174. * 优惠券失效
  175. * @param int $id
  176. */
  177. public function invalid(int $id)
  178. {
  179. $res = $this->dao->update($id, ['status' => 0]);
  180. if (!$res) throw new AdminException('操作失败');
  181. /** @var StoreCouponIssueServices $storeCouponIssueService */
  182. $storeCouponIssueService = app()->make(StoreCouponIssueServices::class);
  183. $storeCouponIssueService->update($id, ['status' => -1], 'cid');
  184. }
  185. /**
  186. * 获取下单可使用的优惠券列表
  187. * @param int $uid
  188. * @param $cartId
  189. * @param string $price
  190. * @param bool $new
  191. * @return array
  192. * @throws \Psr\SimpleCache\InvalidArgumentException
  193. * @throws \think\db\exception\DataNotFoundException
  194. * @throws \think\db\exception\DbException
  195. * @throws \think\db\exception\ModelNotFoundException
  196. */
  197. public function beUsableCouponList(int $uid, $cartId, bool $new)
  198. {
  199. /** @var StoreCartServices $services */
  200. $services = app()->make(StoreCartServices::class);
  201. $cartGroup = $services->getUserProductCartListV1($uid, $cartId, $new);
  202. /** @var StoreCouponUserServices $coupServices */
  203. $coupServices = app()->make(StoreCouponUserServices::class);
  204. return $coupServices->getUsableCouponList($uid, $cartGroup);
  205. }
  206. }