gorden 2 luni în urmă
părinte
comite
f747a759b2

+ 120 - 5
app/admin/controller/coupon/CouponPacketController.php

@@ -3,6 +3,7 @@
 namespace app\admin\controller\coupon;
 
 use app\admin\service\coupon\CouponDetailService;
+use app\admin\service\coupon\CouponService;
 use app\admin\validate\coupon\CouponValidate;
 use app\admin\validate\device\DeviceValidate;
 use app\controller\Curd;
@@ -10,6 +11,7 @@ use app\model\Coupon;
 use app\model\CouponDetail;
 use app\model\CouponGoods;
 use app\model\CouponPacket;
+use app\model\CouponPacketSequence;
 use app\model\Device;
 use app\model\Member;
 use app\model\SysDept;
@@ -282,7 +284,7 @@ class CouponPacketController extends Curd
             if (isset($packetJson['coupons'])) {
                 $couponIds = array_keys($packetJson['coupons']);
                 $couponModel = Coupon::whereIn('coupon_id', $couponIds)
-                    ->select('coupon_id', 'coupon_name')
+                    ->select('coupon_id', 'coupon_name','coupon_classify','coupon_value','coupon_minimum_limit','coupon_category')
                     ->get()
                     ->toArray();
                 foreach ($couponModel as $coupon) {
@@ -291,7 +293,9 @@ class CouponPacketController extends Curd
                             $coupons[] = [
                                 'id' => $key,
                                 'name' => $coupon['coupon_name'],
-                                'nbr' => $jsonCoupon['num']
+                                'nbr' => $jsonCoupon['num'],
+                                'classify'=>$coupon['coupon_classify'],
+                                'info'=>CouponService::couponClassifyInfo($coupon['coupon_classify'],$coupon['coupon_category'],$coupon['coupon_value'],$coupon['coupon_minimum_limit'])
                             ];
                         }
                     }
@@ -333,6 +337,14 @@ class CouponPacketController extends Curd
         }
     }
 
+    /**
+     * @Desc 红包发行
+     * @Author Gorden
+     * @Date 2024/9/12 13:21
+     *
+     * @param Request $request
+     * @return Response
+     */
     public function packetIssue(Request $request)
     {
         $packetId = $request->post('packet_id');
@@ -345,14 +357,117 @@ class CouponPacketController extends Curd
         if (!$packet) {
             return json_fail('数据异常');
         }
+        Db::beginTransaction();
+        try {
+            // 生成红包记录
+            $this->generatePacket($packet);
 
+            $packet->coupon_packet_status = 'ACTIVED';
+            $packet->save();
+
+            Db::commit();
+
+            return json_success("success");
+        } catch (BusinessException $e) {
+            Db::rollBack();
+            return json_fail($e->getMessage());
+        } catch (\Exception $e) {
+            Db::rollBack();
+
+            return json_fail("发行失败");
+        }
     }
 
     private function generatePacket(CouponPacket $couponPacket)
     {
-        $length = $couponPacket->coupon_packet_seq_length;
-        $prefixLength = strlen($couponPacket->coupon_packet_seq_prefix);
-        $suffixLength = strlen($couponPacket->coupon_packet_seq_suffix);
+        try {
+            $length = $couponPacket->coupon_packet_seq_length;
+            $prefixLength = strlen($couponPacket->coupon_packet_seq_prefix);
+            $suffixLength = strlen($couponPacket->coupon_packet_seq_suffix);
+            $seqBegin = $couponPacket->coupon_packet_seq_begin;
+            $realLength = $length - $prefixLength - $suffixLength;
+            $data = [];
+            for ($i = 0; $i < $couponPacket->coupon_packet_issue_num; $i++) {
+                if ($seqBegin > 0) {
+                    $seqBegin = $i === 0 ? $seqBegin : $seqBegin + 1;
+                    $data[] = $couponPacket->coupon_packet_seq_prefix . str_pad($seqBegin, $realLength, $couponPacket->coupon_packet_seq_fill, STR_PAD_LEFT) . $couponPacket->coupon_packet_seq_suffix;
+                } else {
+                    $data[] = $this->generateRandomStr($data, $realLength, $couponPacket->coupon_packet_seq_prefix, $couponPacket->coupon_packet_seq_suffi);
+                }
+            }
+
+            $chunk = collect($data);
+            $chunks = $chunk->chunk(100);
+            foreach ($chunks as $item) {
+                $insertData = [];
+                foreach ($item as $k => $value) {
+                    $insertData[$k] = [
+                        'coupon_packet_sequence_id' => $value,
+                        'join_sequence_coupon_packet_id' => $couponPacket->coupon_packet_id,
+                        'coupon_packet_sequence_status' => 'PENDING',
+                        'coupon_packet_sequence_json' => $couponPacket->coupon_packet_json,
+                        'coupon_packet_sequence_addtimes' => time()
+                    ];
+                }
+                CouponPacketSequence::insert($insertData);
+            }
+        } catch (\Exception $e) {
+            throw new BusinessException("发行失败");
+        }
+    }
+
+    private function generateRandomStr($data, $length, $prefix, $suffix)
+    {
+        $str = $prefix . random_string($length, 'up') . $suffix;
+        if (in_array($str, $data)) {
+            $this->generateRandomStr($data, $length, $prefix, $suffix);
+        }
+
+        return $str;
+    }
+
+    /**
+     * @Desc 红包重新发行
+     * @Author Gorden
+     * @Date 2024/9/12 13:21
+     *
+     * @param Request $request
+     * @return Response
+     */
+    public function packetReIssue(Request $request)
+    {
+        $packetId = $request->post('packet_id');
+        if (!$packetId) {
+            return json_fail("参数异常");
+        }
+        if (CouponPacketSequence::where('join_sequence_coupon_packet_id',$packetId)->where('coupon_packet_sequence_status','USED')->exists()){
+            return json_fail('已有领取记录,无法重新发行');
+        }
+        $packet = CouponPacket::where('coupon_packet_id', $packetId)
+            ->where('coupon_packet_status', 'ACTIVED')
+            ->first();
+        if (!$packet) {
+            return json_fail('数据异常');
+        }
+        Db::beginTransaction();
+        try {
+            // 删除旧的记录
+            CouponPacketSequence::where('join_sequence_coupon_packet_id',$packetId)->delete();
+            // 生成红包记录
+            $this->generatePacket($packet);
+
+            $packet->save();
+
+            Db::commit();
 
+            return json_success("success");
+        } catch (BusinessException $e) {
+            Db::rollBack();
+            return json_fail($e->getMessage());
+        } catch (\Exception $e) {
+            Db::rollBack();
+
+            return json_fail("发行失败");
+        }
     }
 }

+ 246 - 0
app/admin/controller/coupon/CouponPacketSequenceController.php

@@ -0,0 +1,246 @@
+<?php
+
+namespace app\admin\controller\coupon;
+
+use app\admin\service\coupon\CouponService;
+use app\admin\service\member\MemberService;
+use app\controller\Curd;
+use app\model\Coupon;
+use app\model\CouponDetail;
+use app\model\CouponGoods;
+use app\model\CouponPacketSequence;
+use support\Request;
+use support\Response;
+
+class CouponPacketSequenceController extends Curd
+{
+    public function __construct()
+    {
+        $this->model = new CouponPacketSequence();
+    }
+
+    public function select(Request $request): Response
+    {
+        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
+//        $where['join_coupon_detail_member_id'] = ['<>', ''];
+        $order = $request->get('order', 'desc');
+        $field = $field ?? 'coupon_packet_sequence_addtimes';
+
+        $couponName = $request->get('coupon_name', '');
+        if (!empty($couponName)) {
+            $couponIds = Coupon::where('coupon_name', 'like', '%' . $couponName . '%')->pluck('coupon_id')->toArray();
+            if (empty($couponIds)) {
+                $couponIds = ['default'];
+            }
+            $where['join_detail_coupon_id'] = ['in', $couponIds];
+        }
+        $addTimes = $request->get('coupon_packet_sequence_addtimes', []);
+        if (!empty($addTimes)) {
+            $addTimes[0] = isset($addTimes[0]) ? strtotime($addTimes[0]) : time();
+            $addTimes[1] = isset($addTimes[1]) ? strtotime($addTimes[1]) : time();
+
+            $where['coupon_packet_sequence_addtimes'] = $addTimes;
+        }
+
+        $query = $this->doSelect($where, $field, $order);
+        return $this->doFormat($query, $format, $limit);
+    }
+
+    protected function doSelect(array $where, string $field = null, string $order = 'desc')
+    {
+        $model = $this->model->with([
+            'packet',
+//            'coupon',
+            'member' => function ($query) {
+                $query->select('member_id', 'member_mobile');
+            },
+            'cert' => function ($query) {
+                $query->select('join_cert_member_id', 'member_cert_name');
+            },
+            'info' => function ($query) {
+                $query->select('join_info_member_id', 'member_info_nickname');
+            }
+        ]);
+        foreach ($where as $column => $value) {
+            if (is_array($value)) {
+                if ($value[0] === 'like' || $value[0] === 'not like') {
+                    $model = $model->where($column, $value[0], "%$value[1]%");
+                } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
+                    $model = $model->where($column, $value[0], $value[1]);
+                } elseif ($value[0] == 'in' && !empty($value[1])) {
+                    $valArr = $value[1];
+                    if (is_string($value[1])) {
+                        $valArr = explode(",", trim($value[1]));
+                    }
+                    $model = $model->whereIn($column, $valArr);
+                } elseif ($value[0] == 'not in' && !empty($value[1])) {
+                    $valArr = $value[1];
+                    if (is_string($value[1])) {
+                        $valArr = explode(",", trim($value[1]));
+                    }
+                    $model = $model->whereNotIn($column, $valArr);
+                } elseif ($value[0] == 'null') {
+                    $model = $model->whereNull($column);
+                } elseif ($value[0] == 'not null') {
+                    $model = $model->whereNotNull($column);
+                } elseif ($value[0] !== '' || $value[1] !== '') {
+                    $model = $model->whereBetween($column, $value);
+                }
+            } else {
+                $model = $model->where($column, $value);
+            }
+        }
+//        $model = $model->selectRaw('join_detail_coupon_id,join_coupon_detail_member_id,MAX(coupon_detail_addtimes) as coupon_detail_addtimes,COUNT(*) as total');
+//        $model = $model->groupBy('join_coupon_detail_member_id', 'join_detail_coupon_id');
+        if ($field) {
+            $model = $model->orderBy($field, $order);
+        }
+        return $model;
+    }
+
+    public function afterQuery($items)
+    {
+//        foreach ($items as &$item) {
+//            $mobile = $certName = $nickname = '';
+//            if (!empty($item['member']) && !empty($item['member']['member_mobile'])) {
+//                $mobile = $item['member']['member_mobile'];
+//            }
+//            if (!empty($item['cert']) && !empty($item['cert']['member_cert_name'])) {
+//                $certName = $item['cert']['member_cert_name'];
+//            }
+//            if (!empty($item['info']) && !empty($item['info']['member_info_nickname'])) {
+//                $nickname = $item['info']['member_info_nickname'];
+//            }
+//            $item['member_name'] = MemberService::getMemberName($mobile, $certName, $nickname);
+//
+//            $item['member_used'] = 0;
+//            foreach ($item['detail'] as $detail) {
+//                if ($detail['coupon_detail_status'] == 'USED') {
+//                    $item['member_used'] += 1;
+//                }
+//            }
+//            if (!empty($item['coupon_detail_extend_json'])) {
+//                $couponDetailExtendJson = json_decode($item['coupon_detail_extend_json'], true);
+//                if (isset($couponDetailExtendJson['gettype'])) {
+//                    $item['gettype'] = $couponDetailExtendJson['gettype'];
+//                }
+//            }
+//        }
+
+        return $items;
+    }
+
+    /**
+     * @Desc 修改状态
+     * @Author Gorden
+     * @Date 2024/8/22 8:44
+     *
+     * @param Request $request
+     * @return Response
+     */
+    public function updateStatus(Request $request)
+    {
+        $detailId = $request->post('coupon_detail_id', '');
+        $status = $request->post('status', '');
+
+        $detail = CouponDetail::where('coupon_detail_id', $detailId)->first();
+        if (!$detail) {
+            return json_fail('数据不存在');
+        }
+        //状态 INIT|PENDING=等待领取|ACTIVED=可用|WAITING=已占用(待完成)|USED=已使用|DISABLED|CANCEL|EXPIRED
+        if (!in_array($status, ['INIT', 'PENDING', 'ACTIVED', 'WAITING', 'USED', 'DISABLED', 'CANCEL', 'EXPIRED'])) {
+            return json_fail("状态异常");
+        }
+        try {
+            CouponDetail::where('coupon_detail_id', $detailId)->update(['coupon_detail_status' => $status]);
+
+            return json_success('success');
+        } catch (\Exception $e) {
+            dump($e->getMessage());
+            return json_fail('数据修改失败');
+        }
+    }
+
+    /**
+     * @Desc 详情
+     * @Author Gorden
+     * @Date 2024/8/22 11:46
+     *
+     * @param Request $request
+     * @return Response
+     */
+    public function info(Request $request): Response
+    {
+        $detailId = $request->get('detail_id', '');
+        if (empty($detailId)) {
+            return json_fail('参数异常');
+        }
+
+        $detail = CouponDetail::where('coupon_detail_id', $detailId)
+            ->select('coupon_detail_id', 'coupon_detail_extend_json')
+            ->first();
+        if (!empty($detail->coupon_detail_extend_json)) {
+            $couponDetailExtendJson = json_decode($detail->coupon_detail_extend_json, true);
+            if (isset($couponDetailExtendJson['from'])) {
+                $detail['from'] = MemberService::getMemberNameByMemberId($couponDetailExtendJson['from']);
+            }
+        }
+
+        return json_success('', $detail);
+    }
+
+    public function myCouponGrouping(Request $request)
+    {
+        $memberId = $request->get('member_id', '');
+        $goods = $request->get('goods', []);
+        $settlementNow = $request->get('settlement_now', 'N');
+        if ($memberId == '') {
+            return json_fail("参数异常");
+        }
+
+        $goodsIds = array_column($goods, 'goods_id');
+        $couponGoods = CouponGoods::whereIn('join_coupon_goods_id', $goodsIds)->get()->toArray();
+        $couponIds = [];
+        foreach ($couponGoods as $couponGood) {
+            foreach ($goods as $good) {
+                if ($good['sku_id'] = $couponGood['join_coupon_goods_sku_id']) {
+                    $couponIds[] = $couponGood['join_goods_coupon_id'];
+                }
+            }
+        }
+
+        $details = CouponDetail::with([
+            'coupon' => function ($query) {
+                $query->select('coupon_id', 'coupon_name', 'coupon_classify', 'coupon_value', 'coupon_category', 'coupon_minimum_limit');
+            }
+        ])->whereIn('join_detail_coupon_id', $couponIds)
+            ->where('join_coupon_detail_member_id', $memberId);
+            if ($settlementNow == 'Y') {
+                $details = $details->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED', 'WAITING']);
+            } else {
+                $details = $details->where('coupon_detail.coupon_detail_status', 'ACTIVED');
+            }
+            $details = $details->selectRaw('join_detail_coupon_id,join_coupon_detail_member_id,COUNT(*) as count')
+            ->groupBy('join_detail_coupon_id', 'join_coupon_detail_member_id')
+            ->get()
+            ->toArray();
+        $data = [];
+        foreach ($details as $detail) {
+            if (empty($detail['coupon'])) {
+                continue;
+            }
+            $classify = CouponService::couponClassifyInfo($detail['coupon']['coupon_classify'], $detail['coupon']['coupon_category'], $detail['coupon']['coupon_value'], $detail['coupon']['coupon_minimum_limit']);
+            $data[] = [
+                'coupon_id' => $detail['coupon']['coupon_id'],
+                'coupon_name' => $detail['coupon']['coupon_name'],
+                'classify' => $classify,
+                'count' => $detail['count'],
+                'coupon_classify' => $detail['coupon']['coupon_classify'],
+                'coupon_value' => $detail['coupon']['coupon_value'] ?? '',
+                'coupon_minimum_limit' => $detail['coupon']['coupon_minimum_limit'] ?? '',
+            ];
+        }
+
+        return json_success('', $data);
+    }
+}

+ 74 - 0
app/model/CouponPacketSequence.php

@@ -0,0 +1,74 @@
+<?php
+
+namespace app\model;
+
+use DateTimeInterface;
+use support\Model;
+
+class CouponPacketSequence extends Model
+{
+    protected $table = 'coupon_packet_sequence';
+
+    protected $primaryKey = 'coupon_packet_sequence_id';
+
+    protected $keyType = 'string';
+
+    protected $dateFormat = 'U';
+
+    const CREATED_AT = 'coupon_packet_sequence_addtimes';
+
+    const UPDATED_AT = null;
+
+    public function serializeDate(DateTimeInterface $date)
+    {
+        return $date->format('Y-m-d H:i:s');
+    }
+
+    /**
+     * @Desc 关联券包
+     * @Author Gorden
+     * @Date 2024/9/12 14:33
+     *
+     * @return \Illuminate\Database\Eloquent\Relations\HasOne
+     */
+    public function packet()
+    {
+        return $this->hasOne(CouponPacket::class,'coupon_packet_id','join_sequence_coupon_packet_id');
+    }
+
+    /**
+     * @Desc 关联会员
+     * @Author Gorden
+     * @Date 2024/3/30 13:08
+     *
+     * @return \Illuminate\Database\Eloquent\Relations\HasOne
+     */
+    public function member()
+    {
+        return $this->hasOne(Member::class, 'member_id', 'join_coupon_packet_sequence_member_id');
+    }
+
+    /**
+     * @Desc 关联实名
+     * @Author Gorden
+     * @Date 2024/8/21 13:50
+     *
+     * @return \Illuminate\Database\Eloquent\Relations\HasOne
+     */
+    public function cert()
+    {
+        return $this->hasOne(MemberCert::class, 'join_cert_member_id', 'join_coupon_packet_sequence_member_id');
+    }
+
+    /**
+     * @Desc 关联详情
+     * @Author Gorden
+     * @Date 2024/8/21 13:51
+     *
+     * @return \Illuminate\Database\Eloquent\Relations\HasOne
+     */
+    public function info()
+    {
+        return $this->hasOne(MemberInfo::class, 'join_info_member_id', 'join_coupon_packet_sequence_member_id');
+    }
+}

+ 10 - 0
route/admin.php

@@ -894,6 +894,16 @@ Route::group('/admin', function () {
             Route::post('/update', [\app\admin\controller\coupon\CouponPacketController::class, 'update']);
             Route::get('/info', [\app\admin\controller\coupon\CouponPacketController::class, 'info']);
             Route::delete('/delete', [\app\admin\controller\coupon\CouponPacketController::class, 'delete']);
+            Route::post('/packetIssue',[\app\admin\controller\coupon\CouponPacketController::class,'packetIssue']);
+            Route::post('/packetReIssue',[\app\admin\controller\coupon\CouponPacketController::class,'packetReIssue']);
+        })->middleware([
+            \app\middleware\AdminAuthCheck::class
+        ]);
+
+        Route::group('/packetSequence', function () {
+            Route::get('/list', [\app\admin\controller\coupon\CouponPacketSequenceController::class, 'select']);
+            Route::get('/info', [\app\admin\controller\coupon\CouponPacketSequenceController::class, 'info']);
+            Route::post('/updateStatus', [\app\admin\controller\coupon\CouponPacketSequenceController::class, 'updateStatus']);
         })->middleware([
             \app\middleware\AdminAuthCheck::class
         ]);