From 8b4b4eb2a6d06469d0e2a61ec0f8ce4d5953c375 Mon Sep 17 00:00:00 2001
From: 无关风月 <443237572@qq.com>
Date: 星期五, 20 十二月 2024 16:59:27 +0800
Subject: [PATCH] 代码

---
 manage/src/main/java/com/jilongda/manage/controller/TCouponController.java |  148 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 148 insertions(+), 0 deletions(-)

diff --git a/manage/src/main/java/com/jilongda/manage/controller/TCouponController.java b/manage/src/main/java/com/jilongda/manage/controller/TCouponController.java
index 026a948..920f049 100644
--- a/manage/src/main/java/com/jilongda/manage/controller/TCouponController.java
+++ b/manage/src/main/java/com/jilongda/manage/controller/TCouponController.java
@@ -1,9 +1,49 @@
 package com.jilongda.manage.controller;
 
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.jilongda.common.basic.ApiResult;
+import com.jilongda.common.basic.PageInfo;
+import com.jilongda.common.utils.UUIDUtil;
+import com.jilongda.manage.dto.TModelDTO;
+import com.jilongda.manage.model.TAppUser;
+import com.jilongda.manage.model.TCoupon;
+import com.jilongda.manage.model.TCouponReceive;
+import com.jilongda.manage.model.TModel;
+import com.jilongda.manage.query.TAppUserQuery;
+import com.jilongda.manage.query.TCouponQuery;
+import com.jilongda.manage.service.TAppUserService;
+import com.jilongda.manage.service.TCouponReceiveService;
+import com.jilongda.manage.service.TCouponService;
+import com.jilongda.manage.utils.QRCodeUtil;
+import com.jilongda.manage.vo.TAppUserVO;
+import com.jilongda.manage.vo.TCouponInfoVO;
+import com.jilongda.manage.vo.TCouponVO;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;
+import org.springframework.beans.BeanUtils;
+import org.springframework.core.io.ByteArrayResource;
+import org.springframework.mock.web.MockMultipartFile;
+import org.springframework.util.StringUtils;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.annotation.Resource;
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -14,8 +54,116 @@
  * @since 2024-12-09
  */
 @RestController
+@Api(tags = "优惠券管理")
 @RequestMapping("/t-coupon")
 public class TCouponController {
+    @Resource
+    private TCouponService couponService;
+    @Resource
+    private TCouponReceiveService couponReceiveService;
+    @Resource
+    private TAppUserService appUserService;
+    @ApiOperation(value = "优惠券列表")
+    @PostMapping(value = "/pageList")
+    public ApiResult<PageInfo<TCouponVO>> pageList(@RequestBody TCouponQuery query) {
+        PageInfo<TCouponVO> appUserVOPageInfo = couponService.pageList(query);
+        return ApiResult.success(appUserVOPageInfo);
+    }
+    @ApiOperation(value = "添加优惠券")
+    @PostMapping(value = "/add")
+    public ApiResult<String> add( @RequestBody TCoupon dto) throws Exception {
+        couponService.save(dto);
+        switch (dto.getType()){
+            case 2:
+                // 全局发放
+                List<Integer> collect = appUserService.lambdaQuery().list().stream()
+                        .map(TAppUser::getId).collect(Collectors.toList());
+                List<TCouponReceive> tCouponReceives = new ArrayList<>();
+                for (Integer i : collect) {
+                    TCouponReceive tCouponReceive = new TCouponReceive();
+                    tCouponReceive.setCouponId(dto.getId());
+                    tCouponReceive.setUserId(i);
+                    tCouponReceive.setType(2);
+                    tCouponReceive.setAmount(dto.getAmount());
+                    tCouponReceive.setStoreId(dto.getStoreId());
+                    if (dto.getTime()!=0){
+                        tCouponReceive.setEndTime(LocalDateTime.now().plusDays(dto.getTime()));
+                    }
+                    tCouponReceive.setAmountCondition(dto.getAmountCondition());
+                    tCouponReceive.setStatus(1);
+                    tCouponReceives.add(tCouponReceive);
+                }
+                couponReceiveService.saveBatch(tCouponReceives);
+                break;
+            case 3:
+                List<TCouponReceive> tCouponReceives1 = new ArrayList<>();
 
+                for (Integer userId : dto.getUserIds()) {
+                    TCouponReceive tCouponReceive = new TCouponReceive();
+                    tCouponReceive.setCouponId(dto.getId());
+                    tCouponReceive.setUserId(userId);
+                    tCouponReceive.setType(2);
+                    tCouponReceive.setAmount(dto.getAmount());
+                    tCouponReceive.setStoreId(dto.getStoreId());
+                    if (dto.getTime()!=0){
+                        tCouponReceive.setEndTime(LocalDateTime.now().plusDays(dto.getTime()));
+                    }
+                    tCouponReceive.setAmountCondition(dto.getAmountCondition());
+                    tCouponReceive.setStatus(1);
+                    tCouponReceives1.add(tCouponReceive);
+                }
+                couponReceiveService.saveBatch(tCouponReceives1);
+                break;
+            case 4:
+                String code = "{\"id\": "+dto.getId()+ "}";
+                BufferedImage blueImage = QRCodeUtil.createImage(code);
+                MultipartFile blueFile = convert(blueImage, new Date().getTime() + UUIDUtil.getRandomCode(3) + ".PNG");
+                // todo 没有云存储
+//                String s = OssUploadUtil.ossUpload("img/", blueFile);
+                break;
+        }
+        return ApiResult.success();
+    }
+    @ApiOperation(value = "修改优惠券")
+    @PostMapping(value = "/update")
+    public ApiResult<String> update( @RequestBody TCoupon dto) throws Exception {
+        couponService.updateById(dto);
+        return ApiResult.success();
+    }
+    @ApiOperation(value = "优惠券详情")
+    @PostMapping(value = "/getDetail")
+    public ApiResult<TCouponInfoVO> getDetail(Integer id) {
+        TCouponInfoVO tCouponInfoVO = new TCouponInfoVO();
+        TCoupon byId = couponService.getById(id);
+        BeanUtils.copyProperties(byId, tCouponInfoVO);
+        int size = couponReceiveService.list(new LambdaQueryWrapper<TCouponReceive>()
+                .eq(TCouponReceive::getCouponId, id)).size();
+        tCouponInfoVO.setGrantCout(size);
+        int size1 = couponReceiveService.list(new LambdaQueryWrapper<TCouponReceive>()
+                .eq(TCouponReceive::getCouponId, id)
+                .eq(TCouponReceive::getStatus, 2)).size();
+        tCouponInfoVO.setUseCount(size1);
+
+        return ApiResult.success(tCouponInfoVO);
+    }
+    public static MultipartFile convert(BufferedImage bufferedImage, String fileName) throws IOException {
+        // 将 BufferedImage 转换为字节数组
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        ImageIO.write(bufferedImage, "png", baos);
+        byte[] bytes = baos.toByteArray();
+
+        // 创建 ByteArrayResource
+        ByteArrayResource resource = new ByteArrayResource(bytes);
+
+        // 创建 MockMultipartFile
+        MockMultipartFile multipartFile = new MockMultipartFile(
+                "file",
+                fileName,
+                "image/png",
+                resource.getInputStream()
+        );
+
+        return multipartFile;
+    }
 }
 

--
Gitblit v1.7.1