CouponLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\marketing;
  20. use app\common\enum\CouponEnum;
  21. use app\common\model\Coupon;
  22. use app\common\model\CouponList;
  23. use app\common\model\Goods;
  24. use app\common\service\TimeService;
  25. use app\adminapi\lists\marketing\CouponRecordLists;
  26. use Exception;
  27. class CouponLogic
  28. {
  29. /**
  30. * @notes 优惠券详细
  31. * @param $params
  32. * @return array
  33. * @throws @\think\db\exception\DataNotFoundException
  34. * @throws @\think\db\exception\DbException
  35. * @throws @\think\db\exception\ModelNotFoundException
  36. * @author 张无忌
  37. * @date 2021/7/20 16:26
  38. */
  39. public static function detail($params)
  40. {
  41. $detail = (new Coupon())->withoutField('create_time,update_time,delete_time')
  42. ->append([ 'goods', 'goods_category' ])->findOrEmpty(intval($params['id']))->toArray();
  43. $detail['use_time_start'] = $detail['use_time_start'] ? date('Y-m-d H:i:s', $detail['use_time_start']) : '';
  44. $detail['use_time_end'] = $detail['use_time_end'] ? date('Y-m-d H:i:s', $detail['use_time_end']) : '';
  45. return $detail;
  46. }
  47. /**
  48. * @notes 添加优惠券
  49. * @param $params
  50. * @author 张无忌
  51. * @date 2021/7/19 19:01
  52. */
  53. public static function add($params)
  54. {
  55. Coupon::create([
  56. 'sn' => create_code(8),
  57. "name" => $params['name'],
  58. "money" => $params['money'],
  59. // 使用条件
  60. "condition_type" => $params['condition_type'],
  61. "condition_money" => $params['condition_money'] ?? 0,
  62. "discount_ratio" => $params['discount_ratio'] ?? 0,
  63. 'discount_max_money'=> $params['discount_max_money'] ?? 0,
  64. // 发放数量
  65. "send_total_type" => $params['send_total_type'],
  66. "send_total" => $params['send_total'] ?? 0,
  67. // 使用时间
  68. "use_time_type" => $params['use_time_type'],
  69. "use_time_start" => empty($params['use_time_start']) ? 0 : strtotime($params['use_time_start']),
  70. "use_time_end" => empty($params['use_time_end']) ? 0 : strtotime($params['use_time_end']),
  71. "use_time" => $params['use_time'] ?? 0,
  72. // 获取方式
  73. "get_type" => $params['get_type'],
  74. "get_num_type" => $params['get_num_type'],
  75. "get_num" => $params['get_num'] ?? 0,
  76. // 使用商品
  77. "use_goods_type" => $params['use_goods_type'],
  78. "use_goods_ids" => $params['use_goods_ids'] ?? [],
  79. "use_goods_category_ids" => $params['use_goods_category_ids'] ?? [],
  80. "status" => 1
  81. ]);
  82. }
  83. /**
  84. * @notes 编辑优惠券
  85. * @param $params
  86. * @return bool|string
  87. * @author 张无忌
  88. * @date 2021/7/20 15:14
  89. */
  90. public static function edit($params)
  91. {
  92. $coupon = (new Coupon())->findOrEmpty(intval($params['id']));
  93. if ($coupon['status'] == CouponEnum::COUPON_STATUS_NOT) {
  94. Coupon::update([
  95. "name" => $params['name'],
  96. "money" => $params['money'],
  97. // 使用条件
  98. "condition_type" => $params['condition_type'],
  99. "condition_money" => $params['condition_money'] ?? 0,
  100. "discount_ratio" => $params['discount_ratio'] ?? 0,
  101. 'discount_max_money'=> $params['discount_max_money'] ?? 0,
  102. // 发放数量
  103. "send_total_type" => $params['send_total_type'],
  104. "send_total" => $params['send_total'] ?? 0,
  105. // 使用时间
  106. "use_time_type" => $params['use_time_type'],
  107. "use_time_start" => empty($params['use_time_start']) ? 0 : strtotime($params['use_time_start']),
  108. "use_time_end" => empty($params['use_time_end']) ? 0 : strtotime($params['use_time_end']),
  109. "use_time" => $params['use_time'] ?? 0,
  110. // 获取方式
  111. "get_type" => $params['get_type'],
  112. "get_num_type" => $params['get_num_type'],
  113. "get_num" => $params['get_num'] ?? 0,
  114. // 使用商品
  115. "use_goods_type" => $params['use_goods_type'],
  116. "use_goods_ids" => $params['use_goods_ids'] ?? [],
  117. "use_goods_category_ids" => $params['use_goods_category_ids'] ?? [],
  118. ], ['id' => intval($params['id'])]);
  119. } elseif ($coupon['status'] == CouponEnum::COUPON_STATUS_CONDUCT) {
  120. if ($coupon['send_total_type'] == CouponEnum::SEND_TOTAL_TYPE_FIXED) {
  121. if ($coupon['send_total'] > $params['send_total']) {
  122. return '调整后的发放数量不可少于原来的数量';
  123. }
  124. }
  125. Coupon::update([
  126. "name" => $params['name'],
  127. "send_total_type" => $params['send_total_type'],
  128. "send_total" => $params['send_total'] ?? 0,
  129. ], ['id' => intval($params['id'])]);
  130. } elseif ($coupon['status'] == CouponEnum::COUPON_STATUS_END) {
  131. return '优惠券已结束,禁止编辑';
  132. }
  133. return true;
  134. }
  135. /**
  136. * @notes 删除优惠券
  137. * @param $params
  138. * @return bool|string
  139. * @author 张无忌
  140. * @date 2021/7/20 16:10
  141. */
  142. public static function delete($params)
  143. {
  144. $couponList = (new CouponList())
  145. ->where(['coupon_id'=>intval($params['id'])])
  146. ->findOrEmpty()->toArray();
  147. if ($couponList) {
  148. return '优惠券已被用户领取,不可删除';
  149. }
  150. Coupon::destroy(intval($params['id']));
  151. return true;
  152. }
  153. /**
  154. * @notes 优惠券基本信息
  155. * @param $params
  156. * @return array
  157. * @author 张无忌
  158. * @date 2021/7/21 10:23
  159. */
  160. public static function info($params)
  161. {
  162. $model = new Coupon();
  163. $coupon = $model
  164. ->withoutField('update_time,delete_time')
  165. ->append([ 'use_time_text', 'discount_content', 'status_text', 'use_number', 'receive_number', 'surplus_number', 'send_total_text' ])
  166. ->findOrEmpty($params['id'])
  167. ->toArray();
  168. $coupon['get_method'] = CouponEnum::getTypeDesc($coupon['get_type']);
  169. unset($coupon['condition_type']);
  170. unset($coupon['condition_money']);
  171. unset($coupon['send_total_type']);
  172. unset($coupon['use_time_type']);
  173. unset($coupon['use_time_start']);
  174. unset($coupon['use_time_end']);
  175. unset($coupon['use_time']);
  176. unset($coupon['get_num_type']);
  177. unset($coupon['get_num']);
  178. unset($coupon['use_goods_type']);
  179. unset($coupon['use_goods_ids']);
  180. return $coupon;
  181. }
  182. /**
  183. * @notes 开启领取
  184. * @param $params
  185. * @author 张无忌
  186. * @date 2021/7/21 10:25
  187. */
  188. public static function open($params)
  189. {
  190. Coupon::update([
  191. 'status' => CouponEnum::COUPON_STATUS_CONDUCT
  192. ], ['id'=>$params['id']]);
  193. }
  194. /**
  195. * @notes 结束领券
  196. * @param $params
  197. * @author 张无忌
  198. * @date 2021/7/21 10:26
  199. */
  200. public static function stop($params)
  201. {
  202. Coupon::update([
  203. 'status' => CouponEnum::COUPON_STATUS_END
  204. ], ['id'=>$params['id']]);
  205. }
  206. /**
  207. * @notes 发放优惠券
  208. * @param $params
  209. * @return string
  210. * @author 张无忌
  211. * @date 2021/7/21 10:42
  212. */
  213. public static function send($params)
  214. {
  215. try {
  216. // 获取优惠券库存信息
  217. $coupon = (new Coupon())->where(['id'=>$params['id']])->append(['surplus_number'])->findOrEmpty()->toArray();
  218. if ($coupon['status'] == CouponEnum::COUPON_STATUS_NOT) {
  219. return '优惠券活动尚未开始,不能发放';
  220. }
  221. if ($coupon['status'] == CouponEnum::COUPON_STATUS_END) {
  222. return '优惠券活动已结束,不能发放';
  223. }
  224. $totalSendNum = $params['send_user_num'] * count($params['send_user']);
  225. if ($coupon['send_total_type'] == CouponEnum::SEND_TOTAL_TYPE_FIXED) {
  226. if ($totalSendNum > $coupon['surplus_number']) {
  227. return '发放的总数量,超出库存数量,不能发放';
  228. } else {
  229. // Coupon::update([
  230. // 'send_total' => ['dec', $totalSendNum]
  231. // ], ['id' => $coupon['id']]);
  232. }
  233. }
  234. // 计算出券最后可用时间
  235. $invalid_time = 0;
  236. switch ($coupon['use_time_type']) {
  237. case CouponEnum::USE_TIME_TYPE_FIXED:
  238. $invalid_time = $coupon['use_time_end'];
  239. break;
  240. case CouponEnum::USE_TIME_TYPE_TODAY:
  241. $invalid_time = time() + ($coupon['use_time'] * 86400);
  242. break;
  243. case CouponEnum::USE_TIME_TYPE_TOMORROW:
  244. $time = strtotime(date('Y-m-d', strtotime("+1 day")));
  245. $invalid_time = $time + ($coupon['use_time'] * 86400);
  246. }
  247. // 指定用户发放
  248. $couponListModel = new CouponList();
  249. foreach ($params['send_user'] as $user_id) {
  250. $list = [];
  251. for ($i=1; $i<=$params['send_user_num']; $i++) {
  252. $list[] = [
  253. 'channel' => 1,
  254. 'coupon_code' => create_code(),
  255. 'user_id' => $user_id,
  256. 'coupon_id' => $coupon['id'],
  257. 'order_id' => 0,
  258. 'status' => 0,
  259. 'use_time' => 0,
  260. 'invalid_time' => $invalid_time
  261. ];
  262. }
  263. $couponListModel->saveAll($list);
  264. }
  265. return true;
  266. } catch (Exception $e) {
  267. return $e->getMessage();
  268. }
  269. }
  270. /**
  271. * @notes 作废优惠券
  272. * @param $params
  273. * @author 张无忌
  274. * @date 2021/7/21 17:21
  275. */
  276. public static function void($params)
  277. {
  278. (new CouponList())
  279. ->whereIn('id', $params['cl_id'])
  280. ->update([
  281. 'status' => CouponEnum::USE_STATUS_VOID,
  282. 'update_time' => time()
  283. ]);
  284. }
  285. }