package shop.alien.store.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.tea.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.*;
import shop.alien.entity.store.dto.StoreActivityDto;
import shop.alien.entity.store.excelVo.StoreActivityExcelVo;
import shop.alien.entity.store.excelVo.util.ExcelGenerator;
import shop.alien.entity.store.vo.LifeUserVo;
import shop.alien.entity.store.vo.StoreActivityVo;
import shop.alien.entity.store.vo.StoreInfoVo;
import shop.alien.mapper.*;
import shop.alien.store.service.StoreActivityService;
import shop.alien.util.ali.AliOSSUtil;
import shop.alien.util.common.StringToJsonConverterUtils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 活动主表 服务实现类
 * </p>
 *
 * @author ssk
 * @since 2025-05-14
 */
@Service
@RequiredArgsConstructor
public class StoreActivityServiceImpl extends ServiceImpl<StoreActivityMapper, StoreActivity> implements StoreActivityService {

    @Value("${spring.web.resources.excel-path}")
    private String excelPath;

    @Value("${spring.web.resources.excel-generate-path}")
    private String excelGeneratePath;

    private final StoreActivityRuleDetailMapper storeActivityRuleDetailMapper;

    private final StoreInfoMapper storeInfoMapper;

    private final LifeUserMapper lifeUserMapper;

    private final StoreActivityMapper storeActivityMapper;

    private final StoreActivityRuleMapper storeActivityRuleMapper;

    private final AliOSSUtil aliOSSUtil;

    @Override
    public IPage<StoreActivityVo> getActiveList(int page, int size, String status, String createdTime) {
        // 创建格式化对象
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        IPage<StoreActivity> iPage = new Page<>(page, size);
        LambdaQueryWrapper<StoreActivity> lambdaQueryWrapper = new LambdaQueryWrapper<StoreActivity>()
                .eq(!StringUtils.isEmpty(status), StoreActivity::getStatus, status)
                .eq(!StringUtils.isEmpty(createdTime), StoreActivity::getCreatedTime, createdTime)
                .orderByDesc(StoreActivity::getCreatedTime);
        IPage<StoreActivity> storeActivityIPage = storeActivityMapper.selectPage(iPage, lambdaQueryWrapper);
        IPage<StoreActivityVo> storeActivityVoIPage = new Page<>();
        BeanUtils.copyProperties(storeActivityIPage, storeActivityVoIPage);
        List<StoreActivityVo> records = new ArrayList<>();
        for (StoreActivity record : storeActivityIPage.getRecords()) {
            StoreActivityVo storeActivityVo = new StoreActivityVo();
            storeActivityVo.setId(record.getId());
            storeActivityVo.setActivityNo(record.getActivityNo());
            storeActivityVo.setActivityTitle(record.getActivityTitle());
            storeActivityVo.setContractImage(record.getContractImage());
            storeActivityVo.setCreatedTime(record.getCreatedTime());
            storeActivityVo.setStatus(record.getStatus());
            storeActivityVo.setApprovalOpinion(record.getApprovalOpinion());
            String activityDate = "";
            if (record.getIsPermanent()) {
                activityDate = "永久有效";
            } else {
                activityDate = sdf.format(record.getActivityStartTime()) + " 至 " + sdf.format(record.getActivityEndTime());
            }
            storeActivityVo.setActivityTime(activityDate);
            records.add(storeActivityVo);
        }
        storeActivityVoIPage.setRecords(records);
        return storeActivityVoIPage;
    }

    /**
     * 保存商店活动信息(由于此接口类型不同数据库存储类型不同，数据字段存储不同，则不采用Bean cpoy赋值方式，手动存储，过滤不存储字段影响)
     *
     * @param storeActivityDto 商店活动数据传输对象
     * @return 商店活动视图对象
     * @throws Exception 异常
     */
    @Override
    public StoreActivityVo saveActive(StoreActivityDto storeActivityDto) throws Exception {
        //创建存储活动对象
        StoreActivity storeActivity = new StoreActivity();
        storeActivity.setActivityTitle(storeActivityDto.getActivityTitle());
        storeActivity.setActivityType(storeActivityDto.getActivityType());
        JSONObject response = JSONObject.parseObject(StringToJsonConverterUtils.convertToJson(storeActivityDto.getContractImage().get(0).get("response").toString()));
        storeActivity.setContractImage(response.getString("data"));
        storeActivity.setUserIds(storeActivityDto.getUserIds() != null && storeActivityDto.getUserIds().size() > 0 ? storeActivityDto.getUserIds().stream()
                .map(Object::toString)
                .collect(Collectors.joining(",")) : "");
        if (storeActivityDto.getIsPermanent()) {
            storeActivity.setIsPermanent(true);
        } else {
            Instant startInstant = Instant.parse(storeActivityDto.getActivityDate().get(0));
            storeActivity.setActivityStartTime(Date.from(startInstant));
            Instant endInstant = Instant.parse(storeActivityDto.getActivityDate().get(1));
            storeActivity.setActivityEndTime(Date.from(endInstant));
        }
        storeActivity.setSpecificRules(storeActivityDto.getSpecificRules());
        storeActivity.setActivityNo(getCurrentTimeFormatted());
        storeActivity.setStatus(0);
        storeActivityMapper.insert(storeActivity);
        if (storeActivityDto.getActivityType() == 1) {// 邀请好友活动
            //新增邀请好友活动规则
            StoreActivityRule addUserRule = new StoreActivityRule();
            addUserRule.setActivityId(storeActivity.getId());
            addUserRule.setRuleType(1);
            addUserRule.setTimeLimitRedio(storeActivityDto.getTimeLimitRedio());
            addUserRule.setTimeLimit(storeActivityDto.getTimeLimit());
            addUserRule.setLimitationPeopleRedio(storeActivityDto.getLimitationPeopleRedio());
            addUserRule.setLimitationPeople(storeActivityDto.getLimitationPeople());
            addUserRule.setClaimsNumber(storeActivityDto.getClaimsNumber());
            addUserRule.setDiscountType(storeActivityDto.getDiscountType());
            if (storeActivityDto.getDiscountType() == 1) {// 按比例
                addUserRule.setDiscountGradeRedio(storeActivityDto.getDiscountGradeRedio());
                addUserRule.setDiscountGrade(storeActivityDto.getDiscountGrade());
            } else if (storeActivityDto.getDiscountType() == 2) {// 按金额
                addUserRule.setDiscountMoneyRedio(storeActivityDto.getDiscountMoneyRedio());
                addUserRule.setDiscountMoney(storeActivityDto.getDiscountMoney());
            }
            addUserRule.setStoreIds(storeActivityDto.getStoreIds().size() > 0 ? storeActivityDto.getStoreIds().stream()
                    .map(Object::toString)
                    .collect(Collectors.joining(",")) : "");
            addUserRule.setIsDineIn(storeActivityDto.getIsDineIn());
            addUserRule.setIsTakeOut(storeActivityDto.getIsTakeOut());
            addUserRule.setEffectiveDaysRedio(storeActivityDto.getEffectiveDaysRedio());
            addUserRule.setEffectiveDays(storeActivityDto.getEffectiveDays());
            storeActivityRuleMapper.insert(addUserRule);
        } else if (storeActivityDto.getActivityType() == 2) {// 签到领红包活动
            //新增邀请好友活动规则
            StoreActivityRule addUserRule = new StoreActivityRule();
            addUserRule.setActivityId(storeActivity.getId());
            addUserRule.setRuleType(2);
            //存入发放类型
            addUserRule.setSendType(storeActivityDto.getSendType());
            //存入每日最小金额
            addUserRule.setDailyMinAmount(storeActivityDto.getDailyMinAmount());
            //存入每日最大金额
            addUserRule.setDailyMaxAmount(storeActivityDto.getDailyMaxAmount());
            //存入每日领取次数
            addUserRule.setDailyCollectionFrequency(storeActivityDto.getDailyCollectionFrequency());
            //存入保留天数
            addUserRule.setRetentionDays(storeActivityDto.getRetentionDays());
            storeActivityRuleMapper.insert(addUserRule);

            //存入明细详情
            //连续签到奖励
            List<StoreActivityRuleDetail> ruleTableData = storeActivityDto.getRuleTableData();
            for (StoreActivityRuleDetail ruleTable : ruleTableData) {
                StoreActivityRuleDetail storeActivityRuleDetail = new StoreActivityRuleDetail();
                storeActivityRuleDetail.setActivityId(storeActivity.getId());
                storeActivityRuleDetail.setActivityRuleId(addUserRule.getId());
                storeActivityRuleDetail.setDetailType(1);
                storeActivityRuleDetail.setDaysNumber(ruleTable.getDaysNumber());
                storeActivityRuleDetail.setDaysMoney(ruleTable.getDaysMoney());
                storeActivityRuleDetailMapper.insert(storeActivityRuleDetail);
            }
            //任务奖励规则
            List<StoreActivityRuleDetail> taskRuleTableData = storeActivityDto.getTaskRuleTableData();
            for (StoreActivityRuleDetail ruleTable : taskRuleTableData) {
                StoreActivityRuleDetail storeActivityRuleDetail = new StoreActivityRuleDetail();
                storeActivityRuleDetail.setActivityId(storeActivity.getId());
                storeActivityRuleDetail.setActivityRuleId(addUserRule.getId());
                storeActivityRuleDetail.setDetailType(2);
                storeActivityRuleDetail.setTaskType(ruleTable.getTaskType());
                storeActivityRuleDetail.setClaimsNumber(ruleTable.getClaimsNumber());
                storeActivityRuleDetail.setTaskDetail(ruleTable.getTaskDetail());
                storeActivityRuleDetail.setBrowseTime(ruleTable.getBrowseTime());
                storeActivityRuleDetail.setMinAmount(ruleTable.getMinAmount());
                storeActivityRuleDetail.setMaxAmount(ruleTable.getMaxAmount());
                storeActivityRuleDetailMapper.insert(storeActivityRuleDetail);
            }
        }
        StoreActivityVo storeActivityVo = new StoreActivityVo();
        BeanUtils.copyProperties(storeActivity, storeActivityVo);
        return storeActivityVo;
    }

    @Override
    public StoreActivityVo editActive(StoreActivityDto storeActivityDto) throws Exception {
        //创建存储活动对象
        StoreActivity storeActivity = storeActivityMapper.selectById(storeActivityDto.getId());
        storeActivity.setActivityTitle(storeActivityDto.getActivityTitle());
        storeActivity.setActivityType(storeActivityDto.getActivityType());
        JSONObject response = JSONObject.parseObject(StringToJsonConverterUtils.convertToJson(storeActivityDto.getContractImage().get(0).get("response").toString()));
        storeActivity.setContractImage(response.getString("data"));
        storeActivity.setUserIds(storeActivityDto.getUserIds() != null && storeActivityDto.getUserIds().size() > 0 ? storeActivityDto.getUserIds().stream()
                .map(Object::toString)
                .collect(Collectors.joining(",")) : "");
        Instant startInstant = null;
        Instant endInstant = null;
        if (!storeActivityDto.getIsPermanent()) {
            try {
                startInstant = Instant.parse(storeActivityDto.getActivityDate().get(0));
                endInstant = Instant.parse(storeActivityDto.getActivityDate().get(1));
                storeActivity.setActivityStartTime(Date.from(startInstant));
                storeActivity.setActivityEndTime(Date.from(endInstant));
            } catch (Exception e) {
                SimpleDateFormat sdf = new SimpleDateFormat(
                        "EEE MMM dd HH:mm:ss z yyyy",
                        Locale.ENGLISH
                );
                storeActivity.setActivityStartTime(sdf.parse(storeActivityDto.getActivityDate().get(0)));
                storeActivity.setActivityEndTime(sdf.parse(storeActivityDto.getActivityDate().get(1)));
            }
        }
        storeActivity.setSpecificRules(storeActivityDto.getSpecificRules());
        storeActivity.setActivityNo(getCurrentTimeFormatted());
        storeActivity.setStatus(0);
        storeActivity.setApprovalOpinion(null);
        storeActivityMapper.updateById(storeActivity);
        //跟审批意见修改为null
        storeActivityMapper.update(new StoreActivity().setStatus(0), new LambdaUpdateWrapper<StoreActivity>().set(StoreActivity::getApprovalOpinion, null).eq(StoreActivity::getId, storeActivity.getId()));
        if (storeActivityDto.getActivityType() == 1) {// 邀请好友活动
            StoreActivityRule storeActivityRule = storeActivityRuleMapper.selectOne(new LambdaQueryWrapper<StoreActivityRule>().eq(StoreActivityRule::getActivityId, storeActivity.getId()));
            Long id = storeActivityRule.getId();
            storeActivityRule = new StoreActivityRule();
            storeActivityRule.setId(id);
            //新增邀请好友活动规则
            storeActivityRule.setActivityId(storeActivity.getId());
            storeActivityRule.setRuleType(1);
            storeActivityRule.setTimeLimitRedio(storeActivityDto.getTimeLimitRedio());
            storeActivityRule.setTimeLimit(storeActivityDto.getTimeLimit());
            storeActivityRule.setLimitationPeopleRedio(storeActivityDto.getLimitationPeopleRedio());
            storeActivityRule.setLimitationPeople(storeActivityDto.getLimitationPeople());
            storeActivityRule.setClaimsNumber(storeActivityDto.getClaimsNumber());
            storeActivityRule.setDiscountType(storeActivityDto.getDiscountType());
            if (storeActivityDto.getDiscountType() == 1) {// 按比例
                storeActivityRule.setDiscountGradeRedio(storeActivityDto.getDiscountGradeRedio());
                storeActivityRule.setDiscountGrade(storeActivityDto.getDiscountGrade());
            } else if (storeActivityDto.getDiscountType() == 2) {// 按金额
                storeActivityRule.setDiscountMoneyRedio(storeActivityDto.getDiscountMoneyRedio());
                storeActivityRule.setDiscountMoney(storeActivityDto.getDiscountMoney());
            }
            storeActivityRule.setStoreIds(storeActivityDto.getStoreIds().size() > 0 ? storeActivityDto.getStoreIds().stream()
                    .map(Object::toString)
                    .collect(Collectors.joining(",")) : "");
            storeActivityRule.setIsDineIn(storeActivityDto.getIsDineIn());
            storeActivityRule.setIsTakeOut(storeActivityDto.getIsTakeOut());
            storeActivityRule.setEffectiveDaysRedio(storeActivityDto.getEffectiveDaysRedio());
            storeActivityRule.setEffectiveDays(storeActivityDto.getEffectiveDays());
            storeActivityRuleMapper.updateById(storeActivityRule);
        } else if (storeActivityDto.getActivityType() == 2) {// 签到领红包活动
            //新增邀请好友活动规则
            StoreActivityRule storeActivityRule = storeActivityRuleMapper.selectOne(new LambdaQueryWrapper<StoreActivityRule>().eq(StoreActivityRule::getActivityId, storeActivity.getId()));
            storeActivityRule.setActivityId(storeActivity.getId());
            storeActivityRule.setRuleType(2);
            //存入发放类型
            storeActivityRule.setSendType(storeActivityDto.getSendType());
            //存入每日最小金额
            storeActivityRule.setDailyMinAmount(storeActivityDto.getDailyMinAmount());
            //存入每日最大金额
            storeActivityRule.setDailyMaxAmount(storeActivityDto.getDailyMaxAmount());
            //存入每日领取次数
            storeActivityRule.setDailyCollectionFrequency(storeActivityDto.getDailyCollectionFrequency());
            //存入保留天数
            storeActivityRule.setRetentionDays(storeActivityDto.getRetentionDays());
            storeActivityRuleMapper.updateById(storeActivityRule);
            //移除规则表之前数据，重新存入
            storeActivityRuleDetailMapper.delete(new LambdaQueryWrapper<StoreActivityRuleDetail>().eq(StoreActivityRuleDetail::getActivityId, storeActivity.getId()));
            //存入明细详情
            //连续签到奖励
            List<StoreActivityRuleDetail> ruleTableData = storeActivityDto.getRuleTableData();
            for (StoreActivityRuleDetail ruleTable : ruleTableData) {
                StoreActivityRuleDetail storeActivityRuleDetail = new StoreActivityRuleDetail();
                storeActivityRuleDetail.setActivityId(storeActivity.getId());
                storeActivityRuleDetail.setActivityRuleId(storeActivityRule.getId());
                storeActivityRuleDetail.setDetailType(1);
                storeActivityRuleDetail.setDaysNumber(ruleTable.getDaysNumber());
                storeActivityRuleDetail.setDaysMoney(ruleTable.getDaysMoney());
                storeActivityRuleDetailMapper.insert(storeActivityRuleDetail);
            }
            //任务奖励规则
            List<StoreActivityRuleDetail> taskRuleTableData = storeActivityDto.getTaskRuleTableData();
            for (StoreActivityRuleDetail ruleTable : taskRuleTableData) {
                StoreActivityRuleDetail storeActivityRuleDetail = new StoreActivityRuleDetail();
                storeActivityRuleDetail.setActivityId(storeActivity.getId());
                storeActivityRuleDetail.setActivityRuleId(storeActivityRule.getId());
                storeActivityRuleDetail.setDetailType(2);
                storeActivityRuleDetail.setTaskType(ruleTable.getTaskType());
                storeActivityRuleDetail.setClaimsNumber(ruleTable.getClaimsNumber());
                storeActivityRuleDetail.setTaskDetail(ruleTable.getTaskDetail());
                storeActivityRuleDetail.setBrowseTime(ruleTable.getBrowseTime());
                storeActivityRuleDetail.setMinAmount(ruleTable.getMinAmount());
                storeActivityRuleDetail.setMaxAmount(ruleTable.getMaxAmount());
                storeActivityRuleDetailMapper.insert(storeActivityRuleDetail);
            }
        }
        StoreActivityVo storeActivityVo = new StoreActivityVo();
        BeanUtils.copyProperties(storeActivity, storeActivityVo);
        return storeActivityVo;
    }

    @Override
    public StoreActivityVo approvalActivities(StoreActivityDto storeActivityDto) throws Exception {
        StoreActivity storeActivity = storeActivityMapper.selectById(storeActivityDto.getId());
        storeActivity.setApprovalOpinion(storeActivityDto.getApprovalOpinion());
        storeActivity.setStatus(storeActivityDto.getStatus());
        storeActivityMapper.updateById(storeActivity);
        StoreActivityVo storeActivityVo = new StoreActivityVo();
        BeanUtils.copyProperties(storeActivity, storeActivityVo);
        return storeActivityVo;
    }

    @Override
    public List<StoreInfoVo> getStoreInfoByKeyword(String keyword) throws Exception {
        LambdaQueryWrapper<StoreInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StoreInfo::getStoreName, keyword);
        List<StoreInfo> storeInfos = storeInfoMapper.selectList(queryWrapper);
        List<StoreInfoVo> storeInfoVos = new ArrayList<>();
        for (StoreInfo storeInfo : storeInfos) {
            StoreInfoVo storeInfoVo = new StoreInfoVo();
            storeInfoVo.setLabel(storeInfo.getStoreName());
            storeInfoVo.setValue(storeInfo.getId());
            storeInfoVos.add(storeInfoVo);
        }
        return storeInfoVos;
    }

    @Override
    public List<LifeUserVo> getUserInfoByKeyword(String keyword) throws Exception {
        LambdaQueryWrapper<LifeUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(LifeUser::getUserName, keyword);
        List<LifeUser> lifeUsers = lifeUserMapper.selectList(queryWrapper);
        List<LifeUserVo> lifeUserVos = new ArrayList<>();
        for (LifeUser lifeUser : lifeUsers) {
            LifeUserVo lifeUserVo = new LifeUserVo();
            lifeUserVo.setLabel(lifeUser.getUserName());
            lifeUserVo.setValue(lifeUser.getId());
            lifeUserVos.add(lifeUserVo);
        }
        return lifeUserVos;
    }

    @Override
    public StoreActivityVo getActivityDetail(Integer activityId) throws Exception {
        StoreActivityVo storeActivityVo = new StoreActivityVo();
        StoreActivity storeActivity = storeActivityMapper.selectById(activityId);
        if (storeActivity.getActivityType() == 1) {// 邀请好友活动
            StoreActivityRule storeActivityRule = storeActivityRuleMapper.selectOne(new LambdaQueryWrapper<StoreActivityRule>().eq(StoreActivityRule::getActivityId, activityId));
            BeanUtils.copyProperties(storeActivityRule, storeActivityVo);
            BeanUtils.copyProperties(storeActivity, storeActivityVo);
            //处理用户id
            String[] split = storeActivity.getUserIds().split(",");
            List<Integer> integerList = new ArrayList<>();
            for (String s : split) {
                if (!StringUtils.isEmpty(s)) {
                    integerList.add(Integer.parseInt(s));
                }
            }
            // 转换为Integer集合
            if (integerList.size() > 0) {
                List<LifeUserVo> lifeUserVos = new ArrayList<>();
                for (Integer userId : integerList) {
                    LifeUser lifeUser = lifeUserMapper.selectById(userId);
                    LifeUserVo userLifeVo = new LifeUserVo();
                    userLifeVo.setLabel(lifeUser.getUserName());
                    userLifeVo.setValue(lifeUser.getId());
                    lifeUserVos.add(userLifeVo);
                }
                storeActivityVo.setUserOptions(lifeUserVos);
                storeActivityVo.setUserIds(integerList);
                storeActivityVo.setActivityCrowdRedio(1);
            } else {
                storeActivityVo.setActivityCrowdRedio(0);
            }
            //处理商铺id
            String[] splitStore = storeActivityRule.getStoreIds().split(",");
            if (splitStore.length > 0) {
                // 转换为Integer集合
                List<Integer> integerStoreList = new ArrayList<>();
                for (String s : splitStore) {
                    if (!StringUtils.isEmpty(s)) {
                        integerStoreList.add(Integer.parseInt(s));
                    }
                }
                List<StoreInfoVo> storeInfoVos = new ArrayList<>();
                for (Integer storeId : integerStoreList) {
                    StoreInfo storeInfo = storeInfoMapper.selectById(storeId);
                    StoreInfoVo storeInfoVo = new StoreInfoVo();
                    storeInfoVo.setLabel(storeInfo.getStoreName());
                    storeInfoVo.setValue(storeInfo.getId());
                    storeInfoVos.add(storeInfoVo);
                }
                storeActivityVo.setStoreOptions(storeInfoVos);
                storeActivityVo.setStoreIds(integerStoreList);
            }
            //处理一下活动时间
            List<String> dataList = new ArrayList<>();
            dataList.add(storeActivity.getActivityStartTime().toString());
            dataList.add(storeActivity.getActivityEndTime().toString());
            storeActivityVo.setActivityDate(dataList);
        } else if (storeActivity.getActivityType() == 2) {
            StoreActivityRule storeActivityRule = storeActivityRuleMapper.selectOne(new LambdaQueryWrapper<StoreActivityRule>().eq(StoreActivityRule::getActivityId, activityId));
            BeanUtils.copyProperties(storeActivityRule, storeActivityVo);
            BeanUtils.copyProperties(storeActivity, storeActivityVo);
            //处理用户id
            String[] split = storeActivity.getUserIds().split(",");
            List<Integer> integerList = new ArrayList<>();
            for (String s : split) {
                if (!StringUtils.isEmpty(s)) {
                    integerList.add(Integer.parseInt(s));
                }
            }
            // 转换为Integer集合
            if (!integerList.isEmpty()) {
                List<LifeUserVo> lifeUserVos = new ArrayList<>();
                for (Integer userId : integerList) {
                    LifeUser lifeUser = lifeUserMapper.selectById(userId);
                    LifeUserVo userLifeVo = new LifeUserVo();
                    userLifeVo.setLabel(lifeUser.getUserName());
                    userLifeVo.setValue(lifeUser.getId());
                    lifeUserVos.add(userLifeVo);
                }
                storeActivityVo.setUserOptions(lifeUserVos);
                storeActivityVo.setUserIds(integerList);
                storeActivityVo.setActivityCrowdRedio(1);
            } else {
                storeActivityVo.setActivityCrowdRedio(0);
            }
            //处理一下活动时间
            List<String> dataList = new ArrayList<>();
            if (!storeActivity.getIsPermanent()) {
                dataList.add(storeActivity.getActivityStartTime().toString());
                dataList.add(storeActivity.getActivityEndTime().toString());
                storeActivityVo.setActivityDate(dataList);
            }
            //查询连续签到奖励列表
            List<StoreActivityRuleDetail> storeActivityRuleDetails =
                    storeActivityRuleDetailMapper.selectList(
                            new LambdaQueryWrapper<StoreActivityRuleDetail>()
                                    .eq(StoreActivityRuleDetail::getActivityId, storeActivity.getId())
                                    .eq(StoreActivityRuleDetail::getActivityRuleId, storeActivityRule.getId())
                                    .eq(StoreActivityRuleDetail::getDetailType, 1));
            storeActivityVo.setRuleTableData(storeActivityRuleDetails);
            //查询任务奖励列表
            List<StoreActivityRuleDetail> taskStoreActivityRuleDetails =
                    storeActivityRuleDetailMapper.selectList(
                            new LambdaQueryWrapper<StoreActivityRuleDetail>()
                                    .eq(StoreActivityRuleDetail::getActivityId, storeActivity.getId())
                                    .eq(StoreActivityRuleDetail::getActivityRuleId, storeActivityRule.getId())
                                    .eq(StoreActivityRuleDetail::getDetailType, 2));
            storeActivityVo.setTaskRuleTableData(taskStoreActivityRuleDetails);
        }
        return storeActivityVo;
    }

    @Override
    public String exportExcel(String status, String createdTime) throws IOException {
        // 创建格式化对象
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 定义格式化模式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 先将 Date 转换为 Instant
        List<StoreActivity> storeActivities = storeActivityMapper.selectList(
                new LambdaQueryWrapper<StoreActivity>()
                        .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotEmpty(status), StoreActivity::getStatus, status)
                        .like(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotEmpty(createdTime), StoreActivity::getCreatedTime, createdTime));
        List<StoreActivityExcelVo> storeInfoExcelVos = new ArrayList<>();
        int serialNumber = 0;
        for (StoreActivity storeActivity : storeActivities) {
            StoreActivityExcelVo storeActivityExcelVo = new StoreActivityExcelVo();
            //存入序号
            storeActivityExcelVo.setSerialNumber(++serialNumber);
            //获取名称
            storeActivityExcelVo.setActivityTitle(storeActivity.getActivityTitle());
            //存入活动图片
            storeActivityExcelVo.setContractImage(storeActivity.getContractImage());
            //存入创建时间
            Instant instant = storeActivity.getCreatedTime().toInstant();
            String formattedTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime().format(formatter);
            storeActivityExcelVo.setCreatedTime(formattedTime);
            //存入活动时间
            String activityDate = "";
            if (storeActivity.getIsPermanent()) {
                activityDate = "永久有效";
            } else {
                activityDate = sdf.format(storeActivity.getActivityStartTime()) + " 至 " + sdf.format(storeActivity.getActivityEndTime());
            }
            storeActivityExcelVo.setActivityTime(activityDate);
            //格式化审批状态
            Integer storeApplicationStatus = storeActivity.getStatus();
            if (storeApplicationStatus == 0) {
                storeActivityExcelVo.setStatus("待审批");
            } else if (storeApplicationStatus == 1) {
                storeActivityExcelVo.setStatus("审批通过");
            } else if (storeApplicationStatus == 2) {
                storeActivityExcelVo.setStatus("审批拒绝");
            }
            //存入备注
            storeActivityExcelVo.setApprovalOpinion(storeActivity.getApprovalOpinion());
            storeInfoExcelVos.add(storeActivityExcelVo);
        }
        String fileName = UUID.randomUUID().toString().replace("-", "");
        String filePath = ExcelGenerator.generateExcel(excelPath + excelGeneratePath + fileName + ".xlsx", storeInfoExcelVos, StoreActivityExcelVo.class);
        return aliOSSUtil.uploadFile(new File(filePath), "excel/" + fileName + ".xlsx");
    }

    /**
     * 将时分秒毫秒与当前日期组合，转换为 yyyyMMddHHmmssSSS 格式的字符串
     *
     * @return 格式化后的字符串，如 20250516103645789
     */
    public static String getCurrentTimeFormatted() {
        // 获取当前日期时间
        LocalDateTime now = LocalDateTime.now();
        // 定义格式器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        // 格式化输出
        return now.format(formatter);
    }
}
