CouponController.java 4.93 KB
package com.diligrp.etrade.coupon.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.diligrp.etrade.core.domain.Message;
import com.diligrp.etrade.core.domain.PageMessage;
import com.diligrp.etrade.core.util.JsonUtils;
import com.diligrp.etrade.coupon.consotant.CouponConstant;
import com.diligrp.etrade.coupon.consotant.RedisKeyConstant;
import com.diligrp.etrade.coupon.domain.*;
import com.diligrp.etrade.coupon.lisenter.ExcelListener;
import com.diligrp.etrade.coupon.model.CouponBindUserDo;
import com.diligrp.etrade.coupon.model.CouponQueryDo;
import com.diligrp.etrade.coupon.model.CouponResponseDo;
import com.diligrp.etrade.coupon.model.CouponTemplateDo;
import com.diligrp.etrade.coupon.service.BusinessLogService;
import com.diligrp.etrade.coupon.service.CouponService;
import com.diligrp.etrade.coupon.service.ExcelService;
import com.diligrp.etrade.coupon.util.TUtils;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
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 org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.IOException;
import java.util.List;

/**
 * @Author jy
 * @Date 2023/8/28 16:23
 * @Version 1.0
 */
@RestController
@RequestMapping("/coupon")
public class CouponController implements IControllerHandler {
    Logger log = LoggerFactory.getLogger(CouponController.class);

    @Resource
    private ExcelService excelService;
    @Resource
    private CouponService couponService;
    @Resource
    private BusinessLogService businessLogService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/upload")
    public Message<?> upload(MultipartHttpServletRequest request) throws IOException {
        MultipartFile file = request.getFile(CouponConstant.FILE);
        String template = request.getParameter(CouponConstant.TEMPLATE);
        TUtils.isTure(ObjectUtils.isEmpty(file)).throwMessage("未上传文件");
        TUtils.isTure(ObjectUtils.isEmpty(template)).throwMessage("模板信息未传入");
        String name = file.getOriginalFilename().toLowerCase();
        TUtils.isTure(!(name.endsWith(CouponConstant.XLS) || name.endsWith(CouponConstant.XLSX))).throwMessage("文件格式不对,上传失败");
        CouponTemplateDto couponTemplateDto = JsonUtils.fromJsonString(template, CouponTemplateDto.class);
        //对模版进行验证
        couponTemplateDto = couponService.checkTemplate(couponTemplateDto);
        buildOperatorInfo(couponTemplateDto);
        log.info("批量发放优惠劵对应模板信息,{}", couponTemplateDto);
        try {
            long start = System.currentTimeMillis();
            EasyExcel.read(file.getInputStream(), CouponPhoneDto.class, new ExcelListener(couponTemplateDto, excelService, businessLogService)).sheet().doRead();
            long end = System.currentTimeMillis();
            log.info("上传花费时间:{}", (end - start));
        } catch (Exception e) {
            CouponTemplateDto finalCouponTemplateDto = couponTemplateDto;
            TUtils.presentHandler(e.getCause() instanceof ExcelDataConvertException).presentHandle(()->{
                stringRedisTemplate.delete(RedisKeyConstant.TEMPLATE_NO_SEND + finalCouponTemplateDto.getTemplateNo());
            });
            return Message.failure(e.getMessage());
        }
        return Message.success("文件上传成功");
    }

    /**
     * 优惠劵列表查询
     *
     * @param queryDto
     * @return
     */
    @PostMapping("/page")
    public PageMessage<CouponQueryDo> page(@RequestBody CouponQueryDto queryDto) {
        log.info("优惠劵表查询入参,{}", queryDto);
        int pageNo = ObjectUtils.isEmpty(queryDto.getPageNo()) ? 1 : queryDto.getPageNo();
        int pageSize = ObjectUtils.isEmpty(queryDto.getPageSize()) ? 20 : queryDto.getPageSize();
        queryDto.setPageNo(pageNo);
        queryDto.setPageSize(pageSize);
        queryDto.setMarketId(getUserTicket().getFirmId());
        List<CouponQueryDo> list = couponService.page(queryDto);
        return convertPageResult(list);
    }

    /**
     * 导出
     */
    @PostMapping("export")
    public PageMessage<CouponQueryDo> export(@RequestBody CouponQueryDto queryDto) {
        log.info("模板列表导出入参,{}", queryDto);
        queryDto.setPageNo(1);
        queryDto.setPageSize(Integer.MAX_VALUE);
        queryDto.setExport(true);
        queryDto.setMarketId(getUserTicket().getFirmId());
        List<CouponQueryDo> list = couponService.page(queryDto);
        return convertPageResult(list);
    }
}