StoreCouponService.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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\services\activity\coupon;
  12. use app\dao\activity\coupon\StoreCouponDao;
  13. use app\services\BaseServices;
  14. use app\services\product\category\StoreProductCategoryServices;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\services\FormBuilder as Form;
  17. use think\facade\Route as Url;
  18. /**
  19. * Class StoreCouponService
  20. * @package app\services\activity\coupon
  21. * @mixin StoreCouponDao
  22. */
  23. class StoreCouponService extends BaseServices
  24. {
  25. public function __construct(StoreCouponDao $dao)
  26. {
  27. $this->dao = $dao;
  28. }
  29. /**
  30. * 获取列表
  31. * @param array $where
  32. * @return array
  33. */
  34. public function getList(array $where)
  35. {
  36. [$page, $limit] = $this->getPageValue();
  37. $where['is_del'] = 0;
  38. $list = $this->dao->getList($where, $page, $limit);
  39. $count = $this->dao->count($where);
  40. return compact('list', 'count');
  41. }
  42. /**
  43. * 添加优惠券表单
  44. * @param int $type
  45. * @return array
  46. * @throws \FormBuilder\Exception\FormBuilderException
  47. */
  48. public function createForm(int $type)
  49. {
  50. $f[] = Form::input('title', '优惠券名称');
  51. switch ($type) {
  52. case 1://品类券
  53. $options = function () {
  54. /** @var StoreProductCategoryServices $storeCategoryService */
  55. $storeCategoryService = app()->make(StoreProductCategoryServices::class);
  56. $list = $storeCategoryService->getTierList(1, 1);
  57. $menus = [];
  58. foreach (sort_list_tier($list) as $menu) {
  59. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => false];
  60. }
  61. return $menus;
  62. };
  63. $f[] = Form::select('category_id', '选择品类')->setOptions(Form::setOptions($options))->filterable(1)->col(12);
  64. break;
  65. case 2://商品券
  66. $f[] = Form::frameImages('image', '商品', Url::buildUrl(config('admin.admin_prefix') . '/store.StoreProduct/index', array('fodder' => 'image', 'type' => 'many')))->icon('ios-add')->width('960px')->height('430px')->modal(['footer-hide' => true])->props(['srcKey' => 'image']);
  67. $f[] = Form::hidden('product_id', '');
  68. break;
  69. }
  70. $f[] = Form::number('coupon_price', '优惠券面值', 0)->min(0);
  71. $f[] = Form::number('use_min_price', '优惠券最低消费', 0)->min(0);
  72. $f[] = Form::number('coupon_time', '优惠券有效期限', 0)->min(0);
  73. $f[] = Form::number('sort', '排序', 1)->min(0);
  74. $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  75. $f[] = Form::hidden('type', $type);
  76. return create_form('添加优惠券', $f, Url::buildUrl('/marketing/coupon/save'), 'POST');
  77. }
  78. /**
  79. * 优惠卷模板修改表单
  80. * @param int $id
  81. * @return array
  82. * @throws \FormBuilder\Exception\FormBuilderException
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\DbException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. */
  87. public function createIssue(int $id)
  88. {
  89. $res = $this->dao->getOne(['id' => $id, 'status' => 1, 'is_del' => 0]);
  90. if (!$res) throw new AdminException('发布的优惠劵已失效或不存在!');
  91. $f = [];
  92. $f[] = Form::input('id', '优惠劵ID', $id)->disabled(1);
  93. $f[] = Form::input('coupon_title', '优惠劵名称', $res['title'])->disabled(1);
  94. $f[] = Form::dateTimeRange('range_date', '领取时间')->placeholder('不填为永久有效');
  95. $f[] = Form::radio('is_permanent', '是否限量', 1)->options([['label' => '不限量', 'value' => 1], ['label' => '限量', 'value' => 0]]);
  96. $f[] = Form::number('count', '发布数量', 0)->min(0)->placeholder('不填或填0,为不限量');
  97. $f[] = Form::radio('is_type', '优惠券类型', 0)->options([['label' => '普通券', 'value' => 0], ['label' => '赠送券', 'value' => 1], ['label' => '新人券', 'value' => 2]]);
  98. $f[] = Form::number('full_reduction', '满赠金额', 0)->min(0)->placeholder('赠送优惠券的最低消费金额');
  99. $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  100. return create_form('发布优惠券', $f, $this->url('/marketing/coupon/issue/' . $id), 'POST');
  101. }
  102. /**
  103. * 发布优惠券
  104. * @param int $id
  105. * @param int $_id
  106. * @param string $coupon_title
  107. * @param array $rangeTime
  108. * @param int $count
  109. * @param int $status
  110. * @param int $is_permanent
  111. * @param float $full_reduction
  112. * @param int $is_give_subscribe
  113. * @param int $is_full_give
  114. * @param int $is_type
  115. * @throws \think\db\exception\DataNotFoundException
  116. * @throws \think\db\exception\DbException
  117. * @throws \think\db\exception\ModelNotFoundException
  118. */
  119. 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)
  120. {
  121. if ($is_type == 1) {
  122. $is_full_give = 1;
  123. } elseif ($is_type == 2) {
  124. $is_give_subscribe = 1;
  125. }
  126. if ($_id != $id) throw new AdminException('操作失败,信息不对称');
  127. if (!$count) $count = 0;
  128. $couponInfo = $this->dao->getOne(['id' => $id, 'status' => 1, 'is_del' => 0]);
  129. if (!$couponInfo) throw new AdminException('发布的优惠劵已失效或不存在!');
  130. if (count($rangeTime) != 2) throw new AdminException('请选择正确的时间区间');
  131. [$startTime, $endTime] = $rangeTime;
  132. if (!$startTime) $startTime = 0;
  133. if (!$endTime) $endTime = 0;
  134. if (!$startTime && $endTime) throw new AdminException('请选择正确的开始时间');
  135. if ($startTime && !$endTime) throw new AdminException('请选择正确的结束时间');
  136. $data['cid'] = $id;
  137. $data['coupon_title'] = $coupon_title;
  138. $data['start_time'] = strtotime($startTime);
  139. $data['end_time'] = strtotime($endTime);
  140. $data['total_count'] = $count;
  141. $data['remain_count'] = $count;
  142. $data['is_permanent'] = $is_permanent;
  143. $data['status'] = $status;
  144. $data['is_give_subscribe'] = $is_give_subscribe;
  145. $data['is_full_give'] = $is_full_give;
  146. $data['full_reduction'] = $full_reduction;
  147. $data['is_del'] = 0;
  148. $data['add_time'] = time();
  149. $data['title'] = $couponInfo['title'];
  150. $data['integral'] = $couponInfo['integral'];
  151. $data['coupon_price'] = $couponInfo['coupon_price'];
  152. $data['use_min_price'] = $couponInfo['use_min_price'];
  153. $data['coupon_time'] = $couponInfo['coupon_time'];
  154. $data['product_id'] = $couponInfo['product_id'];
  155. $data['category_id'] = $couponInfo['category_id'];
  156. $data['type'] = $couponInfo->getData('type');
  157. /** @var StoreCouponIssueServices $storeCouponIssueService */
  158. $storeCouponIssueService = app()->make(StoreCouponIssueServices::class);
  159. $res = $storeCouponIssueService->save($data);
  160. $productIds = explode(',', $data['product_id']);
  161. if (count($productIds)) {
  162. $couponData = [];
  163. foreach ($productIds as $product_id) {
  164. $couponData[] = ['product_id' => $product_id, 'coupon_id' => $res->id];
  165. }
  166. /** @var StoreCouponProductServices $storeCouponProductService */
  167. $storeCouponProductService = app()->make(StoreCouponProductServices::class);
  168. $storeCouponProductService->saveAll($couponData);
  169. }
  170. if (!$res) throw new AdminException('发布优惠劵失败!');
  171. }
  172. /**
  173. * 优惠券失效
  174. * @param int $id
  175. */
  176. public function invalid(int $id)
  177. {
  178. $res = $this->dao->update($id, ['status' => 0]);
  179. if (!$res) throw new AdminException('操作失败');
  180. /** @var StoreCouponIssueServices $storeCouponIssueService */
  181. $storeCouponIssueService = app()->make(StoreCouponIssueServices::class);
  182. $storeCouponIssueService->update($id, ['status' => -1], 'cid');
  183. }
  184. }