123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace app\admin\service\coupon;
- use app\model\Coupon;
- use app\model\CouponDetail;
- use app\model\SysSerial;
- use support\exception\BusinessException;
- class CouponDetailService
- {
- /**
- * @Desc 手动发券
- * @Author Gorden
- * @Date 2024/8/27 9:57
- *
- * @param $params
- * @return void
- * @throws BusinessException
- */
- public static function customSendCoupon($params)
- {
- $gettype = 'SEND';
- if (!empty($params['gettype'])) {
- $gettype = $params['gettype'];
- }
- try {
- CouponDetail::insert([
- 'coupon_detail_id' => 'CUDT' . date("ymdHi") . random_string(4, 'up'),
- 'join_detail_coupon_id' => $params['coupon_id'],
- 'join_coupon_detail_member_id' => $params['member_id'],
- 'coupon_detail_status' => 'ACTIVED',
- 'coupon_detail_gain_datetime' => $params['coupon_detail_gain_datetime'],
- 'coupon_detail_deadline_datetime' => $params['coupon_detail_deadline_datetime'],
- 'coupon_detail_period_num' => $params['coupon_detail_period_num'] ?? 0,
- 'coupon_detail_extend_json' => json_encode(['gettype' => $gettype]),
- 'coupon_detail_addtimes' => time(),
- ]);
- } catch (\Exception $e) {
- throw new BusinessException('写入优惠券失败');
- }
- }
- public static function customSendCouponHave($params)
- {
- $gettype = 'SEND';
- if (!empty($params['gettype'])) {
- $gettype = $params['gettype'];
- }
- try {
- CouponDetail::where('join_detail_coupon_id', $params['coupon_id'])
- ->whereIn('coupon_detail_status', ['INIT', 'PENDING'])
- ->limit($params['chooseCouponNbr'])
- ->update([
- 'join_coupon_detail_member_id' => $params['member_id'],
- 'coupon_detail_gain_datetime' => $params['coupon_detail_gain_datetime'],
- 'coupon_detail_deadline_datetime' => $params['coupon_detail_deadline_datetime'],
- 'coupon_detail_extend_json' => json_encode(['gettype' => $gettype]),
- 'coupon_detail_period_num' => $params['coupon_detail_period_num'] ?? 0,
- 'coupon_detail_status' => 'ACTIVED',
- 'coupon_detail_addtimes' => time()
- ]);
- } catch (\Exception $e) {
- throw new BusinessException('写入优惠券失败');
- }
- }
- /**
- * @Desc 发周期优惠券 - 没有发行数量
- * @Author Gorden
- * @Date 2024/9/27 13:44
- *
- * @param $params
- * @return void
- */
- public static function sendPeriodCoupon($params)
- {
- $coupon = Coupon::where('coupon_id', $params['coupon_id'])->first();
- if ($coupon->coupon_is_period != 'Y') {
- return;
- }
- $periodJson = json_decode($coupon->coupon_period_json, true);
- if (!empty($periodJson['nbr'])) {
- for ($i = 1; $i < $periodJson['nbr']; $i++) {
- self::generatePeriod($periodJson);
- }
- }
- }
- /**
- * @Desc 有发行数量
- * @Author Gorden
- * @Date 2024/9/27 13:45
- *
- * @param $params
- * @return void
- */
- public static function sendPeriodCouponHave($params)
- {
- }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/9/27 13:54
- *
- * @param $periodJson 周期JSON
- * @return array
- */
- public static function generatePeriod($periodJson)
- {
- $params = [];
- if ($periodJson['unit'] == 'day') {
- $val = $periodJson['val'] - 1;
- if ($val < 1) {
- $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00');
- $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59');
- } else {
- $params['coupon_detail_gain_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . $val . " day"));
- $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime("+" . $val . " day"));
- }
- } elseif ($periodJson['unit'] == 'week') {
- $val = $periodJson['val'] - 1;
- if ($val < 1) {
- $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime('this week Monday'));
- $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime('this week Sunday'));
- } else {
- $params['coupon_detail_gain_datetime'] = date('Y-m-d 00:00:00', strtotime('this week Monday'));
- $params['coupon_detail_deadline_datetime'] = date('Y-m-d 23:59:59', strtotime(date('Y-m-d 23:59:59', strtotime('this week Sunday'))."+" . $val . ' week'));
- }
- } elseif ($periodJson['unit'] == 'month') {
- $val = $periodJson['val'] - 1;
- if ($val < 1) {
- $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59');
- } else {
- $params['coupon_detail_deadline_datetime'] = date('Y-m-t 23:59:59', strtotime("+" . $val . " month"));
- }
- }
- return $params;
- }
- }
|