package shop.alien.storeplatform.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.data.geo.Point;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import shop.alien.config.redis.BaseRedisService;
import shop.alien.entity.result.R;
import shop.alien.entity.store.*;
import shop.alien.entity.store.dto.NearMeDto;
import shop.alien.entity.store.dto.StoreInfoDto;
import shop.alien.entity.store.excelVo.StoreInfoExcelVo;
import shop.alien.entity.store.excelVo.StoreInfoExpiredRecordsExcelVo;
import shop.alien.entity.store.excelVo.util.ExcelExporter;
import shop.alien.entity.store.excelVo.util.ExcelGenerator;
import shop.alien.entity.store.vo.*;
import shop.alien.entity.storePlatform.StoreLicenseHistory;
import shop.alien.mapper.*;
import shop.alien.mapper.storePlantform.StoreLicenseHistoryMapper;
import shop.alien.storeplatform.config.GaoDeMapUtil;
import shop.alien.storeplatform.service.NearMeService;
import shop.alien.storeplatform.service.StoreBusinessService;
import shop.alien.storeplatform.util.FileUploadUtil;
import shop.alien.util.ali.AliOSSUtil;
import shop.alien.util.common.DistanceUtil;
import shop.alien.util.common.constant.CouponStatusEnum;
import shop.alien.util.common.constant.CouponTypeEnum;
import shop.alien.util.common.constant.OrderStatusEnum;
import shop.alien.util.type.LifeCollectIdentityQuery;
import shop.alien.util.type.LifeFansIdentityQuery;
import shop.alien.util.type.LifeNoticeUtil;
import shop.alien.util.type.TypeUtil;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 二期-门店信息 服务实现类
 *
 * @author ssk
 * @since 2024-12-05
 */
@Service
@RequiredArgsConstructor
@Transactional
public class StoreBusinessServiceImpl extends ServiceImpl<StoreInfoMapper, StoreInfo> implements StoreBusinessService {

    private final String DEFAULT_PASSWORD = "123456";

    private final StoreInfoMapper storeInfoMapper;

    private final StoreImgMapper storeImgMapper;

    private final StoreBusinessInfoMapper storeBusinessInfoMapper;

    private final StoreLabelMapper storeLabelMapper;

    private final StoreMenuMapper storeMenuMapper;

    private final StoreUserMapper storeUserMapper;

    private final FileUploadUtil fileUploadUtil;

    private final StoreDictionaryMapper storeDictionaryMapper;

    private final GaoDeMapUtil gaoDeMapUtil;

    private final BaseRedisService baseRedisService;

    private final TagStoreRelationMapper tagStoreRelationMapper;

    private final NearMeService nearMeService;

    private final WebAuditMapper webAuditMapper;

    private final EssentialCityCodeMapper essentialCityCodeMapper;

    private final LifeCouponMapper lifeCouponMapper;

    private final StoreEvaluationMapper storeEvaluationMapper;

    private final LifeUserOrderMapper lifeUserOrderMapper;

    private final LifeCollectMapper lifeCollectMapper;

    private final TypeUtil typeUtil;

    private final StoreStaffConfigMapper storeStaffConfigMapper;

    private final StoreClockInMapper storeClockInMapper;

    private final LifeUserDynamicsMapper lifeUserDynamicsMapper;

    private final StoreCommentMapper storeCommentMapper;

    private final StoreInfoDraftMapper storeInfoDraftMapper;

    private final LifeNoticeMapper lifeNoticeMapper;

    private final LifeNoticeUtil lifeNoticeUtil;

    private final LifeGroupBuyMainMapper lifeGroupBuyMainMapper;


    private final AliOSSUtil aliOSSUtil;


    private final LifeFansMapper lifeFansMapper;

    private final LifeBlacklistMapper lifeBlacklistMapper;

    /** 商户证照历史记录数据访问对象 */
    private final StoreLicenseHistoryMapper licenseHistoryMapper;

    private final EssentialHolidayComparisonMapper essentialHolidayComparisonMapper;

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

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

    /**
     * 懒得查, 留着导出Excel
     */
    List<StoreInfoVo> toExcel = new ArrayList<>();
    private final LifeUserMapper lifeUserMapper;

    /**
     * 门店详情
     *
     * @return StoreMainInfoVo
     */
    @Override
    public StoreMainInfoVo getDetail(Integer id) {

        StoreInfo storeInfo = storeInfoMapper.selectById(id);
        if(storeInfo == null){
             return null;
        }
        StoreMainInfoVo storeMainInfoVo = storeInfoMapper.getStoreInfo(id);

        //判断门店是否到期
        if (ObjectUtils.isNotEmpty(storeMainInfoVo.getExpirationTime())) {
            if (new Date().after(storeMainInfoVo.getExpirationTime())) {
                storeMainInfoVo.setExpirationFlag(0);
            }else {
                storeMainInfoVo.setExpirationFlag(1);
            }
        }else {
            storeMainInfoVo.setExpirationFlag(1);
        }

        //存入门店地址、
        storeMainInfoVo.setStoreAddress(storeInfo.getStoreAddress());
        //入口图
        LambdaQueryWrapper<StoreImg> inletsUrlQueryWrapper = new LambdaQueryWrapper<>();
        inletsUrlQueryWrapper.eq(StoreImg::getImgType, 1);
        inletsUrlQueryWrapper.eq(StoreImg::getStoreId, id);
        List<StoreImg> inletsUrlList = storeImgMapper.selectList(inletsUrlQueryWrapper);
        storeMainInfoVo.setInletsUrl(inletsUrlList.stream().sorted(Comparator.comparing(StoreImg::getImgSort)).map(StoreImg::getImgUrl).collect(Collectors.toList()));
        //相册
        LambdaQueryWrapper<StoreImg> albumUrlQueryWrapper = new LambdaQueryWrapper<>();
        albumUrlQueryWrapper.eq(StoreImg::getImgType, 2);
        albumUrlQueryWrapper.eq(StoreImg::getStoreId, id);
        List<StoreImg> albumUrlList = storeImgMapper.selectList(albumUrlQueryWrapper);
        storeMainInfoVo.setAlbumUrl(albumUrlList.stream().sorted(Comparator.comparing(StoreImg::getImgSort)).map(StoreImg::getImgUrl).collect(Collectors.toList()));
        //推荐菜
        storeMainInfoVo.setRecommendUrl(storeMenuMapper.getStoreMenuList(id, 1).stream().sorted(Comparator.comparing(StoreMenuVo::getImgSort)).collect(Collectors.toList()));
        //菜单
        storeMainInfoVo.setMenuUrl(storeMenuMapper.getStoreMenuList(id, 0).stream().sorted(Comparator.comparing(StoreMenuVo::getImgSort)).collect(Collectors.toList()));
        //门店标签
        storeMainInfoVo.setStoreLabel(storeLabelMapper.selectOne(new LambdaQueryWrapper<StoreLabel>().eq(StoreLabel::getStoreId, id)));
        //营业时间
        //storeMainInfoVo.setStoreBusinessInfo(storeBusinessInfoMapper.selectList(new LambdaQueryWrapper<StoreBusinessInfo>().eq(StoreBusinessInfo::getStoreId, id)));

        //营业时间（通过 getStoreInfoBusinessHours 方法获取，包含节假日信息）
        List<StoreBusinessInfoVo> storeBusinessInfoVos = this.getStoreInfoBusinessHoursNew(id);
        // 设置包含节假日信息的营业时间列表
        storeMainInfoVo.setStoreBusinessInfoVos(storeBusinessInfoVos);


        //门店头像
        LambdaQueryWrapper<StoreImg> eq = new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getImgType, 10).eq(StoreImg::getStoreId, id);
        StoreImg storeImg = storeImgMapper.selectOne(eq);
        storeMainInfoVo.setHeadImgUrl(storeImg != null ? storeImg.getImgUrl() : "");
        List<StoreUser> storeUsers = storeUserMapper.selectList(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getStoreId, storeInfo.getId()));
        for (StoreUser storeUser : storeUsers) {
            storeMainInfoVo.setLogoutFlagUser(storeUser.getLogoutFlag());
        }
        // 计算店铺到最近地铁站的距离
        JSONObject nearbySubway = gaoDeMapUtil.getNearbySubway(storeMainInfoVo.getStorePosition().split(",")[0], storeMainInfoVo.getStorePosition().split(",")[1]);
        // 地铁名
        String subWayName = nearbySubway.getString("name");
        storeMainInfoVo.setSubwayName(subWayName);
        // 地铁站经纬度
        String subWayJing = nearbySubway.getString("location") == null ? null : nearbySubway.getString("location").split(",")[0];
        String subWayWei = nearbySubway.getString("location") == null ? null : nearbySubway.getString("location").split(",")[1];
        if ((subWayJing != null && !subWayJing.isEmpty()) && (subWayWei != null && !subWayWei.isEmpty())) {
            double storeJing = Double.parseDouble(storeMainInfoVo.getStorePosition().split(",")[0]);
            double storeWei = Double.parseDouble(storeMainInfoVo.getStorePosition().split(",")[1]);
            double storeDistance2 = DistanceUtil.haversineCalculateDistance(Double.parseDouble(subWayJing), Double.parseDouble(subWayWei), storeJing, storeWei);
            storeMainInfoVo.setDistance2(storeDistance2);
        } else {
            storeMainInfoVo.setDistance2(0);
        }
        return storeMainInfoVo;
    }

    @Override
    public StoreMainInfoVo getDecorationDetail(Integer id) {
        StoreInfo storeInfo = storeInfoMapper.selectById(id);
        StoreMainInfoVo storeMainInfoVo = storeInfoMapper.getStoreInfo(id);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String expirationDate = sdf.format(storeMainInfoVo.getExpirationTime());
        storeMainInfoVo.setExpirationDate(expirationDate);
        //审核通过给前台反显未提交
        if (storeMainInfoVo.getRenewContractStatus() == 1) {
            storeMainInfoVo.setRenewContractStatus(0);
        }
        if (storeMainInfoVo.getStoreStatus() == -1) {
            LocalDateTime localDateTime = storeMainInfoVo.getLogoutTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            LocalDateTime future = localDateTime.plusDays(7);
            LocalDateTime now = LocalDateTime.now();
            Duration duration = Duration.between(now, future);
            long correct = duration.toMillis();
            storeMainInfoVo.setCountdown(correct);
        }
        //存入门店地址
        storeMainInfoVo.setStoreAddress(storeInfo.getStoreAddress());
        //经营种类
        if (storeInfo.getBusinessTypes() != null) {
            String[] strings = storeInfo.getBusinessTypes().split(",");
            storeMainInfoVo.setBusinessTypesList(Arrays.stream(strings).collect(Collectors.toList()));
        }
        //门店标签
        storeMainInfoVo.setStoreLabel(storeLabelMapper.selectOne(new LambdaQueryWrapper<StoreLabel>().eq(StoreLabel::getStoreId, id)));
        //营业时间
        List<StoreBusinessInfo> storeBusinessInfoList = storeBusinessInfoMapper.selectList(new LambdaQueryWrapper<StoreBusinessInfo>().eq(StoreBusinessInfo::getStoreId, id));


        //营业时间（通过 getStoreInfoBusinessHours 方法获取，包含节假日信息）
        List<StoreBusinessInfoVo> storeBusinessInfoVos = this.getStoreInfoBusinessHoursNew(id);
        // 设置包含节假日信息的营业时间列表
        storeMainInfoVo.setStoreBusinessInfoVos(storeBusinessInfoVos);


        //营业执照
        storeMainInfoVo.setBusinessLicenseList(storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getImgType, 14).eq(StoreImg::getStoreId, id)));
        //合同照片
        storeMainInfoVo.setContractList(storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getImgType, 15).eq(StoreImg::getStoreId, id)));
        //经营许可证图片照片
        storeMainInfoVo.setFoodLicenceList(storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getImgType, 25).eq(StoreImg::getStoreId, id)));
        //是否连锁
        String isChain = (storeInfo.getIsChain() == 0) ? "否" : "是";
        storeMainInfoVo.setIsChainStr(isChain);
        storeMainInfoVo.setFoodLicenceExpirationTime(storeInfo.getFoodLicenceExpirationTime());
        return storeMainInfoVo;
    }

    @Override
    public List<StoreBusinessInfo> getStoreInfoBusinessHours(Integer id) {
        //营业时间
        List<StoreBusinessInfo> storeBusinessInfoList = storeBusinessInfoMapper.selectList(new LambdaQueryWrapper<StoreBusinessInfo>().eq(StoreBusinessInfo::getStoreId, id));
        return storeBusinessInfoList;
    }


    public List<StoreBusinessInfoVo> getStoreInfoBusinessHoursNew(Integer id) {
        // 查询营业时间（包含正常时间和特殊时间）
        List<StoreBusinessInfo> storeBusinessInfoList = storeBusinessInfoMapper.selectList(
                new LambdaQueryWrapper<StoreBusinessInfo>()
                        .eq(StoreBusinessInfo::getStoreId, id)
                        .eq(StoreBusinessInfo::getDeleteFlag, 0)
                        .orderByAsc(StoreBusinessInfo::getBusinessType) // 先按类型排序：1-正常时间，2-特殊时间
                        .orderByAsc(StoreBusinessInfo::getBusinessDate) // 再按日期排序
        );

        // 转换为 VO 并关联节假日信息
        // store_business_info 的 essential_id 关联 essential_holiday_comparison 的 id
        List<StoreBusinessInfoVo> resultList = new ArrayList<>();
        for (StoreBusinessInfo businessInfo : storeBusinessInfoList) {
            StoreBusinessInfoVo vo = new StoreBusinessInfoVo();
            // 复制基本信息
            BeanUtils.copyProperties(businessInfo, vo);

            // 如果有关联的节假日ID（essential_id），查询 essential_holiday_comparison 表的节假日信息
            if (businessInfo.getEssentialId() != null && !businessInfo.getEssentialId().trim().isEmpty()) {
                Integer essentialId = null;
                try {
                    essentialId = Integer.parseInt(businessInfo.getEssentialId().trim());
                    EssentialHolidayComparison holiday = essentialHolidayComparisonMapper.selectById(essentialId);
                    if (holiday != null) {
                        vo.setHolidayInfo(holiday);
                    } else {
                        log.warn("门店营业时间关联的节假日信息不存在，storeId=" + id + ", essentialId=" + essentialId);
                    }
                } catch (NumberFormatException e) {
                    log.warn("门店营业时间关联的节假日ID格式错误，storeId=" + id + ", essentialId=" + businessInfo.getEssentialId());
                } catch (Exception e) {
                    log.error("查询节假日信息失败，storeId=" + id + ", essentialId=" + businessInfo.getEssentialId(), e);
                }
            }

            resultList.add(vo);
        }

        return resultList;
    }


    /**
     * 门店信息-修改后展示
     *
     * @param id 门店id
     * @return StoreMainInfoVo
     */
    @Override
    public StoreMainInfoVo getStoreInfo(Integer id) {
        StoreMainInfoVo storeMainInfoVo = storeInfoMapper.getStoreInfo(id);
        //门店标签
        storeMainInfoVo.setStoreLabel(storeLabelMapper.selectOne(new LambdaQueryWrapper<StoreLabel>().eq(StoreLabel::getStoreId, id)));
        //营业时间
        //storeMainInfoVo.setStoreBusinessInfo(storeBusinessInfoMapper.selectList(new LambdaQueryWrapper<StoreBusinessInfo>().eq(StoreBusinessInfo::getStoreId, id)));

        //营业时间（通过 getStoreInfoBusinessHours 方法获取，包含节假日信息）
        List<StoreBusinessInfoVo> storeBusinessInfoVos = this.getStoreInfoBusinessHoursNew(id);
        // 设置包含节假日信息的营业时间列表
        storeMainInfoVo.setStoreBusinessInfoVos(storeBusinessInfoVos);

        return storeMainInfoVo;
    }


    /**
     * web-分页查询店铺信息
     *
     * @param page         页码
     * @param size         页容
     * @param storeName    门店名称
     * @param storeContact 联系人
     * @param storePhone   门店电话
     * @param storeType    门店类型
     * @return IPage<StoreInfoVo>
     */
    @Override
    public IPage<StoreInfoVo> getStorePage(int page, int size, String storeName, String storeContact, String id, String storePhone, String storeType, String expiredState, String storeApplicationStatus, String storeStatus, String businessSection, String jingdu, String weidu, String renewContractStatus, String foodLicenceStatus, String foodLicenceWhetherExpiredStatus) {
        checkAndUpdateExpiredRecords();
        IPage<StoreInfoVo> iPage = new Page<>(page, size);
        QueryWrapper<StoreInfoVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(storeName != null && !storeName.isEmpty(), "a.store_name", storeName).like(storeContact != null && !storeContact.isEmpty(), "b.name", storeContact).like(storePhone != null && !storePhone.isEmpty(), "b.phone", storePhone).like(storeType != null && !storeType.isEmpty(), "a.store_type", storeType).eq(StringUtils.isNotEmpty(storeApplicationStatus), "a.store_application_status", storeApplicationStatus).eq(StringUtils.isNotEmpty(businessSection), "a.business_section", businessSection).eq(StringUtils.isNotEmpty(storeStatus), "a.store_status", storeStatus).eq(StringUtils.isNotEmpty(renewContractStatus), "a.renew_contract_status", renewContractStatus).eq(StringUtils.isNotEmpty(foodLicenceStatus), "a.food_licence_status", foodLicenceStatus).like(StringUtils.isNotEmpty(id), "a.id", id).eq("a.delete_flag", 0).eq("b.delete_flag", 0).orderByDesc("a.created_time");
        //如果查询未过期
        // 获取当前时刻
        Date currentDate = new Date();
        // 获取当前日期和时间
        Calendar calendar = Calendar.getInstance();
        // 将时间设置为 0 点
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        // 加上 30 天
        calendar.add(Calendar.DAY_OF_MONTH, 30);
        // 获取 Date 对象
        Date thirtyDaysAgo = calendar.getTime();
        if ("0".equals(expiredState)) {
            queryWrapper.ge("a.expiration_time", thirtyDaysAgo);
        } else if ("1".equals(expiredState)) {
            queryWrapper.lt("a.expiration_time", thirtyDaysAgo);
            queryWrapper.ge("a.expiration_time", currentDate);
        } else if ("2".equals(expiredState)) {
            queryWrapper.lt("a.expiration_time", currentDate);
        }
        // 获取当前时间
        LocalDate nowDay = LocalDate.now();
        IPage<StoreInfoVo> storeInfoVoPage = storeInfoMapper.getStoreInfoVoPage(iPage, queryWrapper);
        List<StoreInfoVo> records = storeInfoVoPage.getRecords();
        if (!records.isEmpty()) {
            // 提前查询所有需要的字典数据
            List<StoreInfoVo> collect = records.stream().filter(record -> StringUtils.isNotEmpty(record.getStoreType())).collect(Collectors.toList());
            Set<String> allTypes = collect.stream().map(StoreInfoVo::getStoreType).flatMap(type -> Arrays.stream(type.split(","))).collect(Collectors.toSet());

            List<StoreDictionary> storeDictionaries = storeDictionaryMapper.selectList(new LambdaQueryWrapper<StoreDictionary>().eq(StoreDictionary::getTypeName, "storeType").isNull(StoreDictionary::getParentId).in(!allTypes.isEmpty(), StoreDictionary::getDictId, allTypes));
            Map<String, String> typeMap = storeDictionaries.stream().collect(Collectors.toMap(StoreDictionary::getDictId, StoreDictionary::getDictDetail));

            Map<String, List<LifeCoupon>> quanListByStoreId = new HashMap<>();
            Map<Object, List<Map<String, Object>>> avgScoreMap = new HashMap<>();
            Map<Object, List<Map<String, Object>>> avgPriceMap = new HashMap<>();
            Map<Integer, List<StoreComment>> commentMap = new HashMap<>();
            // 如果获取美食列表进行以下前置操作
            if (StringUtils.isNotEmpty(businessSection) && businessSection.equals("1")) {
                List<Integer> storeIds = records.stream().map(StoreInfoVo::getId).collect(Collectors.toList());
                LambdaUpdateWrapper<LifeCoupon> quanWrapper = new LambdaUpdateWrapper<>();
                quanWrapper.in(LifeCoupon::getStoreId, storeIds).eq(LifeCoupon::getStatus, 1).orderByDesc(LifeCoupon::getCreatedTime);
                List<LifeCoupon> quanList = lifeCouponMapper.selectList(quanWrapper);
                quanListByStoreId = quanList.stream().collect(Collectors.groupingBy(LifeCoupon::getStoreId));
                // 获取全部店铺的评分与平均花销
                // TODO sotre_comment 没有花销 怎么算平均花销
                avgScoreMap = storeEvaluationMapper.allStoreAvgScore().stream().collect(Collectors.groupingBy(o -> o.get("store_id")));
                commentMap = storeCommentMapper.selectList(new QueryWrapper<StoreComment>().eq("business_type", "5").eq("delete_flag", 0)).stream().collect(Collectors.groupingBy(StoreComment::getStoreId));
                avgPriceMap = lifeUserOrderMapper.allStoreAvgPrice().stream().collect(Collectors.groupingBy(o -> o.get("store_id")));
            }
            for (StoreInfoVo record : records) {
                //处理类型
                if (StringUtils.isNotEmpty(record.getStoreType())) {
                    String[] types = record.getStoreType().split(",");
                    List<String> typeDetails = Arrays.stream(types).map(typeMap::get).filter(Objects::nonNull).collect(Collectors.toList());
                    record.setStoreTypeStr(String.join(",", typeDetails));
                    record.setStoreTypeList(Arrays.asList(types));
                }
                //写经纬度
                String[] split = record.getStorePosition().split(",");
                record.setStorePositionLongitude(split[0]);
                record.setStorePositionLatitude(split[1]);
                //处理一下到期状态
                Date expirationTime = record.getExpirationTime();
                if (expirationTime != null) {
                    // 获取当前时间
                    Calendar now = Calendar.getInstance();
                    Date nowCurrentDate = now.getTime();
                    // 计算 30 天后的时间
                    now.add(Calendar.DAY_OF_YEAR, 30);
                    Date thirtyDaysLater = now.getTime();
                    // 比较两个日期
                    if (expirationTime.after(currentDate)) {
                        record.setExpiredState("0");
                        if (expirationTime != null && (expirationTime.after(nowCurrentDate) || expirationTime.equals(nowCurrentDate)) && expirationTime.before(thirtyDaysLater)) {
                            record.setExpiredState("1");
                        }
                    } else {
                        record.setExpiredState("2");
                    }

                    // 获取当前时间
                    LocalDate nowLocal = LocalDate.now();
                    // 将 expirationTime 转换为 LocalDate
                    LocalDate expDate = expirationTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
                    // 计算距离到期的天数
                    long daysToExpire = ChronoUnit.DAYS.between(nowLocal, expDate);
                    record.setDaysToExpire(daysToExpire);
                }
                // 处理经营许可证到期时间回显状态 根据经营许可证筛选状态筛选
                Date foodDate = record.getFoodLicenceExpirationTime();
                if(foodDate != null){
                    // 获取当前时间
                    LocalDate nowLocal = LocalDate.now();
                    LocalDate localDate = foodDate.toInstant()  // Date -> Instant（UTC时间戳）
                            .atZone(ZoneId.systemDefault())  // Instant -> ZonedDateTime（系统默认时区）
                            .toLocalDate();
                    long remainingDays = ChronoUnit.DAYS.between(nowLocal, localDate);
                    if(remainingDays == -1){
                        record.setFoodLicenceRemainingDays("0天");
                        record.setFoodLicenceExpireStatus("已到期");
                    }else if(remainingDays >= 0 && remainingDays <= 30){
                        record.setFoodLicenceRemainingDays(remainingDays + "天");
                        record.setFoodLicenceExpireStatus("即将到期");
                    }else if(remainingDays > 30){
                        record.setFoodLicenceRemainingDays(remainingDays + "天");
                        record.setFoodLicenceExpireStatus("未到期");
                    }
                }


                // 处理状态
                if (record.getLogoutFlag() == 1) {
                    record.setStoreStatusStr("已注销");
                    record.setStoreStatus(2);
                }
                // 根据八大类不同进行个性化操作
                if (StringUtils.isNotEmpty(businessSection)) {
                    switch (businessSection) {
                        case "1":
                            record.setAvgScore("0");
                            record.setAvgPrice("0");
                            record.setTotalNum("0");

                            if ((jingdu != null && !jingdu.isEmpty()) && (weidu != null && !weidu.isEmpty())) {
                                double storeJing = Double.parseDouble(split[0]);
                                double storeWei = Double.parseDouble(split[1]);
                                double storeDistance = DistanceUtil.haversineCalculateDistance(Double.parseDouble(jingdu), Double.parseDouble(weidu), storeJing, storeWei);
                                record.setDistance(storeDistance);
                            } else {
                                record.setDistance(0);
                            }

                            // 设置店铺得分，设置店铺人均消费，设置总评论数
                            if (avgScoreMap.containsKey(String.valueOf(record.getId()))) {
                                record.setAvgScore(String.valueOf(avgScoreMap.get(String.valueOf(record.getId())).get(0).get("avg_score")));
                                record.setAvgPrice(String.valueOf(avgPriceMap.get(String.valueOf(record.getId())).get(0).get("avg_price")));
                            }
                            // 设置店铺得分，设置店铺人均消费，设置总评论数
                            if (commentMap.containsKey(record.getId())) {
                                record.setTotalNum(String.valueOf(commentMap.get(record.getId()).size()));
                            }

                            // 设置店铺优惠券列表
                            if (!quanListByStoreId.isEmpty() && quanListByStoreId.containsKey(String.valueOf(record.getId()))) {
                                record.setQuanList(quanListByStoreId.get(String.valueOf(record.getId())));
                            }
                            break;
                    }
                }
            }
        }
        toExcel = storeInfoVoPage.getRecords();
        return storeInfoVoPage;
    }

    /**
     * 重置门店密码
     *
     * @param storeId 门店id
     * @return boolean
     */
    @Override
    public boolean resetPassword(String storeId) {
        QueryWrapper<StoreUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId);
        StoreUser storeUser = new StoreUser();
        storeUser.setPassword("000000");
        return storeUserMapper.update(storeUser, queryWrapper) > 0;
    }

    /**
     * 修改门店状态
     *
     * @param storeId        门店id
     * @param businessStatus 营业状态
     * @return boolean
     */
    @Override
    public boolean setStoreState(String storeId, Integer businessStatus) {
        LambdaUpdateWrapper<StoreInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(StoreInfo::getId, storeId).set(StoreInfo::getBusinessStatus, businessStatus);
        return this.update(null, updateWrapper);
    }

    /**
     * web-新增店铺
     *
     * @param multipartRequest 文件请求
     * @param store            店铺信息
     * @return boolean
     */
    @Override
    public boolean saveStore(MultipartRequest multipartRequest, StoreInfo store) {
        try {
            String LonAndLat = gaoDeMapUtil.getLonAndLatByAddress(store.getStoreAddress());
            if (LonAndLat == null || LonAndLat.isEmpty()) {
                log.error("LifeStoreController.addOrUpdateStore ERROR 经纬度获取失败");
                return false;
            }
            store.setStorePosition(LonAndLat);
            if (org.apache.commons.lang3.StringUtils.isBlank(store.getCommissionRate())) {
                store.setCommissionRate("3");
            }
            this.save(store);
            Set<String> fileNameSet = multipartRequest.getMultiFileMap().keySet();
            if (!fileNameSet.isEmpty()) {
                for (int i = 0; i < fileNameSet.size(); i++) {
                    MultipartFile multipartFile = multipartRequest.getFileMap().get(i);
                    if (null != multipartFile) {
                        StoreImg storeImg = new StoreImg();
                        storeImg.setStoreId(store.getId());
                        storeImg.setImgType(12);
                        storeImg.setImgSort(i + 1);
                        storeImg.setImgUrl(fileUploadUtil.uploadOneFile(multipartFile));
                        storeImgMapper.insert(storeImg);
                    }
                }
            }
            return true;
        } catch (Exception e) {
            log.error("LifeStoreController.addOrUpdateStore ERROR Msg={}", e);
            return false;
        }
    }

    /**
     * 导出Excel
     *
     * @return ResponseEntity
     */
    @Override
    public ResponseEntity<byte[]> exportToExcel() {
        try {
            String[] headers = {"商家名称", "联系人", "联系电话", "经营种类", "创建时间", "状态"};
            byte[] excelData = ExcelExporter.exportToExcel(toExcel, headers, "商家列表清单");
            HttpHeaders responseHeaders = new HttpHeaders();
            responseHeaders.add("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode("商家列表清单.xlsx") + "\";charset=utf-8");
            responseHeaders.set("Charset", "UTF-8");
            return new ResponseEntity<>(excelData, responseHeaders, HttpStatus.OK);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public StoreInfoVo saveStoreInfo(StoreInfoDto storeInfoDto) throws Exception {
        //获取经营板块id
        Integer businessSection = storeInfoDto.getBusinessSection();
        //查询经营板块名称
        StoreDictionary businessSectionName = storeDictionaryMapper.selectOne(new LambdaQueryWrapper<StoreDictionary>().eq(StoreDictionary::getDictId, businessSection).eq(StoreDictionary::getTypeName, "business_section"));
        //查询经营种类
        List<String> businessTypes = storeInfoDto.getBusinessTypes();
        List<String> businessTypeNames = new ArrayList<>();
        //获取经营种类名称
        for (String businessType : businessTypes) {
            StoreDictionary storeDictionary = storeDictionaryMapper.selectOne(new LambdaQueryWrapper<StoreDictionary>().eq(StoreDictionary::getDictId, businessType).eq(StoreDictionary::getParentId, businessSectionName.getId()));
            businessTypeNames.add(storeDictionary.getDictDetail());
        }

        StoreInfoVo result = new StoreInfoVo();
        StoreInfo storeInfo = new StoreInfo();
        BeanUtils.copyProperties(storeInfoDto, storeInfo);
//        List<String> storeTypeList = storeInfoDto.getStoreTypeList();
//        String storeType = String.join(",", storeTypeList);
//        //存入营运类型
//        storeInfo.setStoreType(storeType);
        //存入门店状态
        storeInfo.setStoreStatus(storeInfoDto.getStoreStatus());
        //存入经纬度
        storeInfo.setStorePosition(storeInfoDto.getStorePositionLongitude() + "," + storeInfoDto.getStorePositionLatitude());
        //如果有门店密码，并且门店密码不为默认密码。则存入密码
        if (StringUtils.isNotEmpty(storeInfoDto.getStorePass()) && !DEFAULT_PASSWORD.equals(storeInfoDto.getStorePass())) {
            storeInfo.setStorePass(storeInfoDto.getStorePass());
            storeInfo.setPassType(1);
        } else {
            storeInfo.setStorePass(DEFAULT_PASSWORD);
            storeInfo.setPassType(0);
        }
        //板块及类型
        storeInfo.setBusinessSection(businessSection);
        storeInfo.setBusinessSectionName(businessSectionName.getDictDetail());
        storeInfo.setBusinessTypes(String.join(",", businessTypes));
        storeInfo.setBusinessTypesName(String.join(",", businessTypeNames));
        //存入审批状态为待审批
        storeInfo.setStoreApplicationStatus(0);
        //处理一下行政区域信息
        EssentialCityCode essentialCityCode1 = essentialCityCodeMapper.selectOne(new LambdaQueryWrapper<EssentialCityCode>().eq(EssentialCityCode::getAreaCode, storeInfo.getAdministrativeRegionProvinceAdcode()));
        storeInfo.setAdministrativeRegionProvinceName(essentialCityCode1.getAreaName());
        EssentialCityCode essentialCityCode2 = essentialCityCodeMapper.selectOne(new LambdaQueryWrapper<EssentialCityCode>().eq(EssentialCityCode::getAreaCode, storeInfo.getAdministrativeRegionCityAdcode()));
        storeInfo.setAdministrativeRegionCityName(essentialCityCode2.getAreaName());
        EssentialCityCode essentialCityCode3 = essentialCityCodeMapper.selectOne(new LambdaQueryWrapper<EssentialCityCode>().eq(EssentialCityCode::getAreaCode, storeInfo.getAdministrativeRegionDistrictAdcode()));
        storeInfo.setAdministrativeRegionDistrictName(essentialCityCode3.getAreaName());
        if (null == storeInfo.getCommissionRate()) {
            storeInfo.setCommissionRate("3");
        }
        storeInfoMapper.insert(storeInfo);
        result.setId(storeInfo.getId());
        nearMeService.inGeolocation(new Point(Double.parseDouble(storeInfoDto.getStorePositionLongitude()), Double.parseDouble(storeInfoDto.getStorePositionLatitude())), storeInfo.getId().toString(), Boolean.TRUE);
        //修改门店店铺用户绑定关系
        String userAccount = storeInfoDto.getUserAccount();
        StoreUser storeUser = storeUserMapper.selectById(userAccount);
        storeUser.setStoreId(storeInfo.getId());
        if(StringUtils.isNotEmpty(storeInfoDto.getStoreContact())){
            storeUser.setName(storeInfoDto.getStoreContact());
        }
        if(StringUtils.isNotEmpty(storeInfoDto.getIdCard())){
            storeUser.setIdCard(storeInfoDto.getIdCard());
        }
        storeUserMapper.updateById(storeUser);
        //存入店铺营业执照图片
        List<String> businessLicenseAddress = storeInfoDto.getBusinessLicenseAddress();
        for (String licenseAddress : businessLicenseAddress) {
            StoreImg storeImg = new StoreImg();
            storeImg.setStoreId(storeInfo.getId());
            storeImg.setImgType(14);
            storeImg.setImgSort(0);
            storeImg.setImgDescription("营业执照");
            storeImg.setImgUrl(licenseAddress);
            storeImgMapper.insert(storeImg);
        }
        //存入店铺合同图片
        if(!CollectionUtils.isEmpty(storeInfoDto.getContractImageList())){
            List<String> contractImageList = storeInfoDto.getContractImageList();
            //先移除此前数据
            storeImgMapper.delete(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getStoreId, storeInfo.getId()).eq(StoreImg::getImgType, 15));
            for (String licenseAddress : contractImageList) {
                StoreImg storeImg = new StoreImg();
                storeImg.setStoreId(storeInfo.getId());
                storeImg.setImgType(15);
                storeImg.setImgSort(0);
                storeImg.setImgDescription("合同图片");
                storeImg.setImgUrl(licenseAddress);
                storeImgMapper.insert(storeImg);
            }
        }

        //存入店铺经营许可证图片
        if(StringUtils.isNotEmpty(storeInfoDto.getFoodLicenceUrl())){
                StoreImg storeImg = new StoreImg();
                storeImg.setStoreId(storeInfo.getId());
                storeImg.setImgType(25);
                storeImg.setImgSort(0);
                storeImg.setImgDescription("经营许可证图片");
                storeImg.setImgUrl(storeInfoDto.getFoodLicenceUrl());
                storeImgMapper.insert(storeImg);
        }

        //初始化标签数据
        LambdaQueryWrapper<TagStoreRelation> tagStoreRelationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tagStoreRelationLambdaQueryWrapper.eq(TagStoreRelation::getStoreId, storeInfo.getId());
        tagStoreRelationLambdaQueryWrapper.eq(TagStoreRelation::getDeleteFlag, 0);
        int tagStoreCount = tagStoreRelationMapper.selectCount(tagStoreRelationLambdaQueryWrapper);
        if(tagStoreCount == 0){
            TagStoreRelation tagStoreRelation = new TagStoreRelation();
            tagStoreRelation.setStoreId(storeInfo.getId());
            tagStoreRelation.setDeleteFlag(0);
            tagStoreRelationMapper.insert(tagStoreRelation);
        }


        // 发送通知
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String commonDate = simpleDateFormat.format(new Date());
        String text = "您在"+commonDate+"提交的入驻店铺申请，平台已受理，1-3个工作日将审核结果发送至应用内的消息-通知中，请注意查收。";
        com.alibaba.fastjson2.JSONObject jsonObject = new com.alibaba.fastjson2.JSONObject();
        jsonObject.put("message", text);
        LifeNotice lifeMessage = new LifeNotice();
        lifeMessage.setReceiverId("store_" + storeUser.getPhone());
        lifeMessage.setContext(jsonObject.toJSONString());
        lifeMessage.setTitle("入驻店铺申请");
        lifeMessage.setSenderId("system");
        lifeMessage.setIsRead(0);
        lifeMessage.setNoticeType(1);
        lifeNoticeUtil.fillUserTypeAndRefId(lifeMessage);
        lifeNoticeMapper.insert(lifeMessage);

        WebSocketVo websocketVo = new WebSocketVo();
        websocketVo.setSenderId("system");
        websocketVo.setReceiverId("store_" + storeUser.getPhone());
        websocketVo.setCategory("notice");
        websocketVo.setNoticeType("1");
        websocketVo.setIsRead(0);
        websocketVo.setText(com.alibaba.fastjson2.JSONObject.from(lifeMessage).toJSONString());

        return result;
    }

    @Override
    public int saveStoreInfoDraft(StoreInfoDraft storeInfoDraft) {
        //处理一下行政区域信息
        if (storeInfoDraft.getAdministrativeRegionProvinceAdcode() != null) {
            EssentialCityCode essentialCityCode1 = essentialCityCodeMapper.selectOne(new LambdaQueryWrapper<EssentialCityCode>().eq(EssentialCityCode::getAreaCode, storeInfoDraft.getAdministrativeRegionProvinceAdcode()));
            if (null != essentialCityCode1) {
                storeInfoDraft.setAdministrativeRegionProvinceName(essentialCityCode1.getAreaName());
            }
        }
        if (storeInfoDraft.getAdministrativeRegionCityAdcode() != null) {
            EssentialCityCode essentialCityCode2 = essentialCityCodeMapper.selectOne(new LambdaQueryWrapper<EssentialCityCode>().eq(EssentialCityCode::getAreaCode, storeInfoDraft.getAdministrativeRegionCityAdcode()));
            if (null != essentialCityCode2) {
                storeInfoDraft.setAdministrativeRegionCityName(essentialCityCode2.getAreaName());
            }
        }
        if (storeInfoDraft.getAdministrativeRegionDistrictAdcode() != null) {
            EssentialCityCode essentialCityCode3 = essentialCityCodeMapper.selectOne(new LambdaQueryWrapper<EssentialCityCode>().eq(EssentialCityCode::getAreaCode, storeInfoDraft.getAdministrativeRegionDistrictAdcode()));
            if (null != essentialCityCode3) {
                storeInfoDraft.setAdministrativeRegionDistrictName(essentialCityCode3.getAreaName());
            }
        }
        if (storeInfoDraft.getBusinessLicenseUrl().isEmpty()) {
            storeInfoDraft.setBusinessLicenseUrl(null);
        }
        if (storeInfoDraft.getContractUrl().isEmpty()) {
            storeInfoDraft.setContractUrl(null);
        }

        if (storeInfoDraft.getFoodLicenceUrl().isEmpty()) {
            storeInfoDraft.setFoodLicenceUrl(null);
        }
        return storeInfoDraftMapper.insert(storeInfoDraft);
    }

    @Override
    public StoreInfoDraft selectDraftByUserId(int storeUserId) {
        LambdaQueryWrapper<StoreInfoDraft> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StoreInfoDraft::getStoreUserId, storeUserId);
        wrapper.orderByDesc(StoreInfoDraft::getCreatedTime);
        List<StoreInfoDraft> list = storeInfoDraftMapper.selectList(wrapper);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    @Override
    public StoreInfoVo editStoreInfo(StoreInfoDto storeInfoDto) {

        //判断是否修改变更过的用户账户
        String userAccountId = storeInfoDto.getUserAccount();
        Integer id = storeInfoDto.getId();
        StoreUser storeUserEntity = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getStoreId, id));
        //如果更换了用户，则原有用户绑定门店删除
        if (!userAccountId.equals(storeUserEntity.getId().toString())) {
            LambdaUpdateWrapper<StoreUser> storeUserLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            storeUserLambdaUpdateWrapper.set(StoreUser::getStoreId, null).eq(StoreUser::getId, storeUserEntity.getId());
            storeUserMapper.update(null, storeUserLambdaUpdateWrapper);
        }
        //获取经营板块id
        Integer businessSection = storeInfoDto.getBusinessSection();
        //查询经营板块名称
        StoreDictionary businessSectionName = storeDictionaryMapper.selectOne(new LambdaQueryWrapper<StoreDictionary>().eq(StoreDictionary::getDictId, businessSection).eq(StoreDictionary::getTypeName, "business_section"));
        //查询经营种类
        List<String> businessTypes = storeInfoDto.getBusinessTypes();
        List<String> businessTypeNames = new ArrayList<>();
        //获取经营种类名称
        for (String businessType : businessTypes) {
            StoreDictionary storeDictionary = storeDictionaryMapper.selectOne(new LambdaQueryWrapper<StoreDictionary>().eq(StoreDictionary::getDictId, businessType).eq(StoreDictionary::getParentId, businessSectionName.getId()));
            businessTypeNames.add(storeDictionary.getDictDetail());
        }

        StoreInfoVo result = new StoreInfoVo();
        StoreInfo storeInfo = new StoreInfo();
        BeanUtils.copyProperties(storeInfoDto, storeInfo);
//        List<String> storeTypeList = storeInfoDto.getStoreTypeList();
//        String storeType = String.join(",", storeTypeList);
        //存入营运类型
//        storeInfo.setStoreType(storeType);
        //存入门店状态
        storeInfo.setStoreStatus(storeInfoDto.getStoreStatus());
        //存入经纬度
        storeInfo.setStorePosition(storeInfoDto.getStorePositionLongitude() + "," + storeInfoDto.getStorePositionLatitude());
        //如果有门店密码，并且门店密码不为默认密码。则存入密码
        if (StringUtils.isNotEmpty(storeInfoDto.getStorePass()) && !DEFAULT_PASSWORD.equals(storeInfoDto.getStorePass())) {
            storeInfo.setStorePass(storeInfoDto.getStorePass());
            storeInfo.setPassType(1);
        } else {
            storeInfo.setStorePass(DEFAULT_PASSWORD);
            storeInfo.setPassType(0);
        }
        //板块及类型
        storeInfo.setBusinessSection(businessSection);
        storeInfo.setBusinessSectionName(businessSectionName.getDictDetail());
        storeInfo.setBusinessTypes(String.join(",", businessTypes));
        storeInfo.setBusinessTypesName(String.join(",", businessTypeNames));
        //处理一下行政区域信息
        EssentialCityCode essentialCityCode1 = essentialCityCodeMapper.selectOne(new LambdaQueryWrapper<EssentialCityCode>().eq(EssentialCityCode::getAreaCode, storeInfo.getAdministrativeRegionProvinceAdcode()));
        storeInfo.setAdministrativeRegionProvinceName(essentialCityCode1.getAreaName());
        EssentialCityCode essentialCityCode2 = essentialCityCodeMapper.selectOne(new LambdaQueryWrapper<EssentialCityCode>().eq(EssentialCityCode::getAreaCode, storeInfo.getAdministrativeRegionCityAdcode()));
        storeInfo.setAdministrativeRegionCityName(essentialCityCode2.getAreaName());
        EssentialCityCode essentialCityCode3 = essentialCityCodeMapper.selectOne(new LambdaQueryWrapper<EssentialCityCode>().eq(EssentialCityCode::getAreaCode, storeInfo.getAdministrativeRegionDistrictAdcode()));
        storeInfo.setAdministrativeRegionDistrictName(essentialCityCode3.getAreaName());
        storeInfoMapper.updateById(storeInfo);
        nearMeService.inGeolocation(new Point(Double.parseDouble(storeInfoDto.getStorePositionLongitude()), Double.parseDouble(storeInfoDto.getStorePositionLatitude())), storeInfo.getId().toString(), Boolean.TRUE);

        //修改门店店铺用户绑定关系
        String userAccount = storeInfoDto.getUserAccount();
        StoreUser storeUser = storeUserMapper.selectById(userAccount);
        storeUser.setStoreId(storeInfo.getId());
        storeUserMapper.updateById(storeUser);
        BeanUtils.copyProperties(storeInfoDto, result);
        //存入店铺营业执照图片
        if(!CollectionUtils.isEmpty(storeInfoDto.getBusinessLicenseAddress())){
            List<String> businessLicenseAddress = storeInfoDto.getBusinessLicenseAddress();
            //先移除此前数据
            storeImgMapper.delete(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getStoreId, storeInfo.getId()).eq(StoreImg::getImgType, 14));
            for (String licenseAddress : businessLicenseAddress) {
                StoreImg storeImg = new StoreImg();
                storeImg.setStoreId(storeInfo.getId());
                storeImg.setImgType(14);
                storeImg.setImgDescription("营业执照");
                storeImg.setImgSort(0);
                storeImg.setImgUrl(licenseAddress);
                storeImgMapper.insert(storeImg);
            }
        }

        //存入店铺合同图片
        List<String> contractImageList = storeInfoDto.getContractImageList();
        if(!CollectionUtils.isEmpty(contractImageList)){
            //先移除此前数据
            storeImgMapper.delete(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getStoreId, storeInfo.getId()).eq(StoreImg::getImgType, 15));
            for (String licenseAddress : contractImageList) {
                StoreImg storeImg = new StoreImg();
                storeImg.setStoreId(storeInfo.getId());
                storeImg.setImgType(15);
                storeImg.setImgSort(0);
                storeImg.setImgDescription("合同图片");
                storeImg.setImgUrl(licenseAddress);
                storeImgMapper.insert(storeImg);
            }
        }

        //存入店铺经营许可证图片
        if(StringUtils.isNotEmpty(storeInfoDto.getFoodLicenceUrl())){
            //先移除此前数据 type:25 审核通过后的图片
            storeImgMapper.delete(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getStoreId, storeInfo.getId()).eq(StoreImg::getImgType, 25));
            //先移除此前数据 type:24 审核前的或者审核拒绝的图片
            storeImgMapper.delete(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getStoreId, storeInfo.getId()).eq(StoreImg::getImgType, 24));
            StoreImg storeImg = new StoreImg();
            storeImg.setStoreId(storeInfo.getId());
            //type:25 审核通过的类型
            storeImg.setImgType(25);
            storeImg.setImgSort(0);
            storeImg.setImgDescription("经营许可证审核通过图片");
            storeImg.setImgUrl(storeInfoDto.getFoodLicenceUrl());
            storeImgMapper.insert(storeImg);
        }

        return result;
    }

    @Override
    public String deleteStoreInfo(StoreInfoDto storeInfoDto) {
        StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getStoreId, storeInfoDto.getId()));
        //判断是否有未完成的订单
        List<LifeUserOrder> lifeUserOrders = lifeUserOrderMapper.selectList(new LambdaQueryWrapper<LifeUserOrder>().in(LifeUserOrder::getStatus, 0, 3).eq(ObjectUtils.isNotEmpty(storeUser), LifeUserOrder::getUserId, storeUser.getId()).eq(LifeUserOrder::getDeleteFlag, 0));
        if (ObjectUtils.isNotEmpty(lifeUserOrders)) {
            return "您有暂未完成的订单无法删除店铺，请在完成后删除";
        } else {
            //判断是否修改变更过的用户账户
            if (storeUser != null) {
                LambdaUpdateWrapper<StoreUser> storeUserLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                storeUserLambdaUpdateWrapper.set(StoreUser::getStoreId, null).eq(StoreUser::getId, storeUser.getId());
                storeUserMapper.update(null, storeUserLambdaUpdateWrapper);
            }
            //删除商铺信息
            storeInfoMapper.deleteById(storeInfoDto.getId());
            return "成功";
        }
    }

    @Override
    public void restPassword(StoreInfoDto storeInfoDto) {
        StoreInfo storeInfo = storeInfoMapper.selectById(storeInfoDto.getId());
        storeInfo.setStorePass(DEFAULT_PASSWORD);
        //修改商铺信息密码
        storeInfoMapper.updateById(storeInfo);
        //删除商铺的用户
        StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getStoreId, storeInfoDto.getId()));
        storeUser.setPassword(DEFAULT_PASSWORD);
        storeUserMapper.updateById(storeUser);
    }

    @Override
    public List<StoreDictionaryVo> getBusinessSection() {
        List<StoreDictionary> businessSection = storeDictionaryMapper.selectList(new LambdaQueryWrapper<StoreDictionary>().eq(StoreDictionary::getTypeName, "business_section").ne(StoreDictionary::getDictId, 0));
        List<StoreDictionaryVo> voList = new ArrayList<>();
        for (StoreDictionary storeDictionary : businessSection) {
            StoreDictionaryVo vo = new StoreDictionaryVo();
            BeanUtils.copyProperties(storeDictionary, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<StoreDictionaryVo> getBusinessSectionTypes(String parentId) {
        StoreDictionary businessSection = storeDictionaryMapper.selectOne(new LambdaQueryWrapper<StoreDictionary>().eq(StoreDictionary::getTypeName, "business_section").eq(StoreDictionary::getDictId, parentId));
        List<StoreDictionary> storeDictionaries = storeDictionaryMapper.selectList(new LambdaQueryWrapper<StoreDictionary>().eq(StoreDictionary::getParentId, businessSection.getId()));
        List<StoreDictionaryVo> voList = new ArrayList<>();
        for (StoreDictionary storeDictionary : storeDictionaries) {
            StoreDictionaryVo vo = new StoreDictionaryVo();
            BeanUtils.copyProperties(storeDictionary, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<StoreUserVo> getUnboundAccountList(String id) {
        List<StoreUserVo> storeUserVoList = new ArrayList<>();
        List<StoreUser> storeUsers = storeUserMapper.selectList(new LambdaQueryWrapper<StoreUser>().isNull(StoreUser::getStoreId));
        for (StoreUser storeUser : storeUsers) {
            StoreUserVo vo = new StoreUserVo();
            BeanUtils.copyProperties(storeUser, vo);
            vo.setPayPassword("");
            vo.setPassword("");
            storeUserVoList.add(vo);
        }
        if (StringUtils.isNotEmpty(id)) {
            StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getStoreId, id));
            if (storeUser != null) {
                StoreUserVo vo = new StoreUserVo();
                BeanUtils.copyProperties(storeUser, vo);
                vo.setPayPassword("");
                vo.setPassword("");
                storeUserVoList.add(vo);
            }
        }
        return storeUserVoList;
    }

    @Override
    public void addBusinessSectionAndTypes(StoreInfoDto storeInfoDto) {
        StoreDictionary businessSectionParent = storeDictionaryMapper.selectOne(new LambdaQueryWrapper<StoreDictionary>().eq(StoreDictionary::getTypeName, "business_section").eq(StoreDictionary::getDictId, 0));
        //查看当前最大id
        List<StoreDictionary> businessSection = storeDictionaryMapper.selectList(new LambdaQueryWrapper<StoreDictionary>().eq(StoreDictionary::getTypeName, "business_section"));
        Integer maxId = 0;
        for (StoreDictionary storeDictionary : businessSection) {
            if (Integer.parseInt(storeDictionary.getDictId()) > maxId) {
                maxId = Integer.parseInt(storeDictionary.getDictId());
            }
        }
        //构建新增的经营板块
        StoreDictionary addStoreDictionary = new StoreDictionary();
        BeanUtils.copyProperties(businessSection.get(0), addStoreDictionary);
        addStoreDictionary.setId(null);
        addStoreDictionary.setDictDetail(storeInfoDto.getBusinessSectionName());
        addStoreDictionary.setDictId((maxId + 1) + "");
        addStoreDictionary.setParentId(businessSectionParent.getId());
        storeDictionaryMapper.insert(addStoreDictionary);
        //构建新增的经营类型
        List<String> businessTypesList = storeInfoDto.getBusinessTypesList();
        Integer dictgId = 0;
        for (String businessTypeName : businessTypesList) {
            StoreDictionary storeDictionary = new StoreDictionary();
            storeDictionary.setTypeName(storeInfoDto.getBusinessTypeValue());
            storeDictionary.setTypeDetail(addStoreDictionary.getDictDetail());
            storeDictionary.setParentId(addStoreDictionary.getId());
            storeDictionary.setDictId(dictgId + "");
            storeDictionary.setDictDetail(businessTypeName);
            storeDictionary.setCreatedTime(new Date());
            storeDictionaryMapper.insert(storeDictionary);
            dictgId++;
        }
    }

    @Override
    public StoreInfoVo getStoreDetail(String storeId, String userId, String jingdu, String weidu) {
        StoreInfoVo result = new StoreInfoVo();
        StoreInfo storeInfo = storeInfoMapper.selectById(storeId);
        BeanUtils.copyProperties(storeInfo, result);
        //将经营板块和种类拆分成集合
        String businessTypes = storeInfo.getBusinessTypes();
        if (StringUtils.isNotEmpty(businessTypes)) {
            String[] split = businessTypes.split(",");
            List<String> list = Arrays.asList(split);
            result.setBusinessTypesList(list);
        }
        //存入用户账户
        StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getStoreId, storeId));
        if (storeUser != null) {
            result.setUserAccount(storeUser.getId().toString());
            result.setStorePhone(storeUser.getPhone());
            result.setStoreUserName(storeUser.getName());
            result.setIdCard(storeUser.getIdCard());
        }
        //存入执照图片地址
        List<StoreImg> storeImgs = storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getStoreId, storeId).eq(StoreImg::getImgType, 14));
        List<String> storeImgPaths = new ArrayList<>();
        for (StoreImg storeImg : storeImgs) {
            storeImgPaths.add(storeImg.getImgUrl());
        }
        result.setBusinessLicenseAddress(storeImgPaths);
        //存入合同图片地址
        List<StoreImg> storeContractImageImgs = storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getStoreId, storeId).eq(StoreImg::getImgType, 15));
        List<String> storeContractImagePathImgs = new ArrayList<>();
        for (StoreImg storeImg : storeContractImageImgs) {
            storeContractImagePathImgs.add(storeImg.getImgUrl());
        }
        result.setContractImageList(storeContractImagePathImgs);
        //存入续签合同地址
        List<StoreImg> renewContractImgs = storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getStoreId, storeId).eq(StoreImg::getImgType, 22));
        List<String> renewContractImagePathImgs = new ArrayList<>();
        for (StoreImg storeImg : renewContractImgs) {
            renewContractImagePathImgs.add(storeImg.getImgUrl());
        }
        result.setRenewContractImageList(renewContractImagePathImgs);
        //存入经营许可证通过地址
        List<StoreImg> foodLicenceImgs = storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getStoreId, storeId).eq(StoreImg::getImgType, 25));
        List<String> foodLicenceImgsPathImgs = new ArrayList<>();
        for (StoreImg storeImg : foodLicenceImgs) {
            foodLicenceImgsPathImgs.add(storeImg.getImgUrl());
        }
        result.setFoodLicenceImageList(foodLicenceImgsPathImgs);
        //存入经营许可证未通过地址
        List<StoreImg> notPassFoodLicenceImgs = storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getStoreId, storeId).eq(StoreImg::getImgType, 24));
        List<String> notPassFoodLicenceList = new ArrayList<>();
        for (StoreImg storeImg : notPassFoodLicenceImgs) {
            notPassFoodLicenceList.add(storeImg.getImgUrl());
        }
        result.setNotPassFoodLicenceImageList(notPassFoodLicenceList);
        // 存放商家入口图
        List<StoreImg> storeEntranceImageImgs = storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getStoreId, storeId).eq(StoreImg::getImgType, 1));
        if (!storeEntranceImageImgs.isEmpty()) {
            result.setEntranceImage(storeEntranceImageImgs.get(0).getImgUrl());
        } else {
            result.setEntranceImage("null");
        }
        // 存放商家头像
        List<StoreImg> storeImgs1 = storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getStoreId, storeId).eq(StoreImg::getImgType, 10));
        if (!storeImgs1.isEmpty()) {
            result.setImgUrl(storeImgs1.get(0).getImgUrl());
        } else {
            result.setImgUrl("null");
        }

        // 设置经纬度
        result.setStorePositionLongitude(result.getStorePosition().split(",")[0]);
        result.setStorePositionLatitude(result.getStorePosition().split(",")[1]);
        // 设置距离
        if ((jingdu != null && !jingdu.isEmpty()) && (weidu != null && !weidu.isEmpty())) {
            /*double storeJing = Double.parseDouble(result.getStorePosition().split(",")[0]);
            double storeWei = Double.parseDouble(result.getStorePosition().split(",")[1]);
            double storeDistance = DistanceUtil.haversineCalculateDistance(Double.parseDouble(jingdu), Double.parseDouble(weidu), storeJing, storeWei);*/

            Double distance = storeInfoMapper.getStoreDistance(jingdu + "," + weidu,result.getId());

            result.setDistance(distance);
        }
        // 计算店铺到最近地铁站的距离
        JSONObject nearbySubway = gaoDeMapUtil.getNearbySubway(result.getStorePosition().split(",")[0], result.getStorePosition().split(",")[1]);
        // 地铁名
        String subWayName = nearbySubway.getString("name");
        result.setSubwayName(subWayName);
        // 地铁站经纬度
        String subWayJing = nearbySubway.getString("location") == null ? null : nearbySubway.getString("location").split(",")[0];
        String subWayWei = nearbySubway.getString("location") == null ? null : nearbySubway.getString("location").split(",")[1];
        if ((subWayJing != null && !subWayJing.isEmpty()) && (subWayWei != null && !subWayWei.isEmpty())) {
            double storeJing = Double.parseDouble(result.getStorePosition().split(",")[0]);
            double storeWei = Double.parseDouble(result.getStorePosition().split(",")[1]);
            double storeDistance2 = DistanceUtil.haversineCalculateDistance(Double.parseDouble(subWayJing), Double.parseDouble(subWayWei), storeJing, storeWei);
            result.setDistance2(storeDistance2);
        } else {
            result.setDistance2(0);
        }
        // 当前登录用户是否收藏
        LambdaUpdateWrapper<LifeCollect> shouCangWrapper = new LambdaUpdateWrapper<>();
        LifeCollectIdentityQuery.applyUserSide(shouCangWrapper, LifeCollectIdentityQuery.resolve(userId, typeUtil));
        shouCangWrapper.eq(LifeCollect::getStoreId, storeId);
        List<LifeCollect> shouCangList = lifeCollectMapper.selectList(shouCangWrapper);
        if (null == shouCangList || shouCangList.isEmpty()) {
            result.setCollection(0);
        } else {
            result.setCollection(1);
        }
        // 获取店铺代金券列表
        LambdaUpdateWrapper<LifeCoupon> quanWrapper = new LambdaUpdateWrapper<>();
        quanWrapper.eq(LifeCoupon::getStoreId, storeId).eq(LifeCoupon::getStatus, CouponStatusEnum.ONGOING.getCode()).eq(LifeCoupon::getType, 1);
        List<LifeCoupon> quanList = lifeCouponMapper.selectList(quanWrapper);
        List<LifeCouponVo> quanVoList = new ArrayList<>();
        List<String> collect = quanList.stream().map(LifeCoupon::getId).collect(Collectors.toList());
        // 设置已售数量
        // 定义需要的订单状态集合
        Set<Integer> excludeStatuses = new HashSet<>(Arrays.asList(
                OrderStatusEnum.WAIT_PAY.getStatus(),
                OrderStatusEnum.WAIT_USE.getStatus(),
                OrderStatusEnum.USED.getStatus()
        ));
        if (!collect.isEmpty()) {
            List<LifeUserOrderVo> quanCount = lifeUserOrderMapper.getQuanCount(new QueryWrapper<LifeUserOrderVo>()
                    .eq("luo.store_id",storeId)
                    .eq("luo.coupon_type", CouponTypeEnum.COUPON.getCode())
                    .eq("luo.delete_flag",0)
                    .in("ocm.status",excludeStatuses)
                    .groupBy("ocm.coupon_id"));
            quanList.forEach(a -> {
                LifeCouponVo lifeCouponVo = new LifeCouponVo();
                BeanUtils.copyProperties(a, lifeCouponVo);
                quanCount.forEach(item -> {
                    if (a.getId().equals(item.getCouponId().toString())) {
                        lifeCouponVo.setCount(item.getCount());
                    }
                });
                quanVoList.add(lifeCouponVo);
            });
        }
        result.setCouponList(quanVoList);

        // 获取店铺团购
        LambdaUpdateWrapper<LifeGroupBuyMain> tuangouWrapper = new LambdaUpdateWrapper<>();
        tuangouWrapper.eq(LifeGroupBuyMain::getStoreId, storeId).eq(LifeGroupBuyMain::getStatus, CouponStatusEnum.ONGOING.getCode()).orderByDesc(LifeGroupBuyMain::getCreatedTime);
        List<LifeGroupBuyMain> tuangouList = lifeGroupBuyMainMapper.selectList(tuangouWrapper);
        List<LifeGroupBuyMainVo> tuangouVOList = new ArrayList<>();
        for (LifeGroupBuyMain lifeGroupBuyMain : tuangouList) {
            LifeGroupBuyMainVo lifeGroupBuyMainVo = new LifeGroupBuyMainVo();
            BeanUtils.copyProperties(lifeGroupBuyMain, lifeGroupBuyMainVo);
            if (StringUtils.isNotEmpty(lifeGroupBuyMain.getImageId())) {
                List<String> ids = Arrays.stream(lifeGroupBuyMain.getImageId().split(",")).map(String::trim).collect(Collectors.toList());
                List<StoreImg> imgList = storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().in(StoreImg::getId, ids));
                if (imgList != null) {
                    String imgs = imgList.stream().map(StoreImg::getImgUrl).collect(Collectors.joining(","));
                    lifeGroupBuyMainVo.setImageId(imgs);
                }
            }
            tuangouVOList.add(lifeGroupBuyMainVo);
        }
        result.setTuangouList(tuangouVOList);

        // 获取员工列表
        LambdaQueryWrapper<StoreStaffConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StoreStaffConfig::getStoreId, storeId);
        List<StoreStaffConfig> storeStaffConfigs = storeStaffConfigMapper.selectList(lambdaQueryWrapper);
        result.setEmployeeList(storeStaffConfigs);

        // 该用户的打卡记录（仅审核通过 check_flag=2）
        LambdaQueryWrapper<StoreClockIn> clockInWrapper = new LambdaQueryWrapper<>();
        clockInWrapper.eq(StoreClockIn::getUserId, userId);
        clockInWrapper.eq(StoreClockIn::getCheckFlag, 2);
        List<StoreClockIn> clockInList = storeClockInMapper.selectList(clockInWrapper);

        List<StoreClockIn> clockStoreList = clockInList.stream().filter(item -> item.getStoreId() == Integer.parseInt(storeId)).collect(Collectors.toList());
        // 该用户是否在该店铺打过卡
        if (!clockStoreList.isEmpty()) {
            result.setClockInStore(1);
        } else {
            result.setClockInStore(0);
        }

        // 今天在该店铺是否打过卡
        int today = (int) clockStoreList.stream().filter(item -> item.getCreatedTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate().equals(LocalDate.now())).count();
        if (today > 0) {
            result.setClockInStoreToday(1);
        } else {
            result.setClockInStoreToday(0);
        }

        // 店铺平均分
        /*Map<Object, List<Map<String, Object>>> avgScoreMap = storeEvaluationMapper.allStoreAvgScore().stream().collect(Collectors.groupingBy(o -> o.get("store_id")));
        if (avgScoreMap.containsKey(String.valueOf(result.getId()))) {
            result.setScore(Double.parseDouble(avgScoreMap.get(String.valueOf(result.getId())).get(0).get("avg_score").toString()));
        }*/



        // 在该店铺的打卡次数
        result.setClockInStoreNum(clockStoreList.size());

        // 该用户打卡的所有店铺次数（一个店铺只算一次）
        int clockInNum = (int) clockInList.stream().map(StoreClockIn::getStoreId).distinct().count();
        result.setClockInNum(clockInNum);

        // 获取店铺动态列表
        LambdaQueryWrapper<LifeBlacklist> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(LifeBlacklist :: getBlockerId, userId);
        lambdaQueryWrapper1.eq(LifeBlacklist :: getBlockedPhoneId, "store_" + result.getStorePhone());
        LifeBlacklist blacklist = lifeBlacklistMapper.selectOne(lambdaQueryWrapper1);
        List<LifeUserDynamicsVo> storeDynamicslist = new ArrayList<>();

        //判断没有拉黑当前门店账户 查出门店动态
        if(blacklist == null){
            StoreUser storePublisherUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>()
                    .eq(StoreUser::getPhone, result.getStorePhone())
                    .eq(StoreUser::getDeleteFlag, 0)
                    .last("LIMIT 1"));
            Integer storeRefId = storePublisherUser != null ? storePublisherUser.getId() : null;
            storeDynamicslist = lifeUserDynamicsMapper.getStoreDynamicslist(userId, 2, storeRefId);
        }

        List<String> followList = new ArrayList<>();
        List<String> fansList = new ArrayList<>();

        if (StringUtils.isNotEmpty(userId)) {
            LifeUser lifeUser = lifeUserMapper.selectById(userId);
            if (lifeUser != null && StringUtils.isNotEmpty(lifeUser.getUserPhone())) {
                // 查询我的关注信息，构建关注者ID列表
                LifeFansIdentityQuery.Scope storeViewerScope = LifeFansIdentityQuery.resolve("user_" + result.getStorePhone(), typeUtil);
                LambdaQueryWrapper<LifeFans> lifeFansWrapper = new LambdaQueryWrapper<>();
                lifeFansWrapper.eq(LifeFans::getDeleteFlag, 0);
                LifeFansIdentityQuery.applyFansSide(lifeFansWrapper, storeViewerScope);
                List<LifeFans> lifeFansList = lifeFansMapper.selectList(lifeFansWrapper);
                if (!CollectionUtils.isEmpty(lifeFansList)) {
                    followList = lifeFansList.stream().map(LifeFans::getFollowedId).collect(Collectors.toList());
                }

                lifeFansWrapper = new LambdaQueryWrapper<>();
                lifeFansWrapper.eq(LifeFans::getDeleteFlag, 0);
                LifeFansIdentityQuery.applyFollowedSide(lifeFansWrapper, storeViewerScope);
                lifeFansList = lifeFansMapper.selectList(lifeFansWrapper);
                if (!CollectionUtils.isEmpty(lifeFansList)) {
                    fansList = lifeFansList.stream().map(LifeFans::getFansId).collect(Collectors.toList());
                }
            }
        }

        for (LifeUserDynamicsVo vo : storeDynamicslist) {
            if (followList.contains(vo.getPhoneId())) {
                vo.setIsFollowThis("1");
            } else {
                vo.setIsFollowThis("0");
            }
            if (fansList.contains(vo.getPhoneId())) {
                vo.setIsFollowMe("1");
            } else {
                vo.setIsFollowMe("0");
            }
        }

        // 返回动态最新的5条
        List<LifeUserDynamicsVo> storeDynamicslist2 = storeDynamicslist.stream()
                .limit(5).collect(Collectors.toList());
        result.setDynamicsList(storeDynamicslist2);
        //设置动态条数
        Integer dynamicsNum = storeDynamicslist2.size();
        result.setDynamicsNum(dynamicsNum);

        // 获取店铺动态总数
        result.setTotalDynamicsNum(storeDynamicslist.size());

        //营业时间
        List<StoreBusinessInfo> storeBusinessInfos = storeBusinessInfoMapper.selectList(new LambdaQueryWrapper<StoreBusinessInfo>().eq(StoreBusinessInfo::getStoreId, storeId).eq(StoreBusinessInfo::getDeleteFlag, 0));
        if (ObjectUtils.isNotEmpty(storeBusinessInfos)) {
            result.setStoreBusinessInfo(storeBusinessInfos.get(0));
            result.setStoreBusinessInfos(storeBusinessInfos);
            //StoreBusinessInfo storeBusinessInfo = result.getStoreBusinessInfo();
            StoreBusinessInfo storeBusinessInfo = result.getStoreBusinessInfos().stream().filter(item -> item.getBusinessType() == 1).findFirst().orElse(null) ;
            if (ObjectUtils.isNotEmpty(storeBusinessInfo)) {

                Calendar calendar = Calendar.getInstance(); // 获取Calendar实例
                int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); // 获取星期几，注意Calendar中的DAY_OF_WEEK是从1（代表星期天）开始的
                String[] days = {"7", "1", "2", "3", "4", "5", "6"};
                String day = days[dayOfWeek - 1];
                if (storeBusinessInfo.getBusinessDate().contains(day)) {
                    if (StringUtils.isNotEmpty(storeBusinessInfo.getStartTime()) && StringUtils.isNotEmpty(storeBusinessInfo.getEndTime())) {
                        LocalTime now = LocalTime.now();
                        List<String> startList = Arrays.asList(storeBusinessInfo.getStartTime().split(":"));
                        List<String> endList = Arrays.asList(storeBusinessInfo.getEndTime().split(":"));
                        LocalTime start = LocalTime.of(Integer.parseInt(startList.get(0)), Integer.parseInt(startList.get(1)));
                        LocalTime end = LocalTime.of(Integer.parseInt(endList.get(0)), Integer.parseInt(startList.get(1)));
                        if (now.isAfter(start) && now.isBefore(end)) {
                            result.setYyFlag(1);
                        } else {
                            result.setYyFlag(0);
                        }
                    }
                } else {
                    result.setYyFlag(0);
                }
            }
        }
        LambdaQueryWrapper<StoreDictionary> storeDictionaryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        storeDictionaryLambdaQueryWrapper.eq(StoreDictionary::getTypeName, "businessStatus")
                .eq(StringUtils.isNotEmpty(result.getBusinessStatus().toString()), StoreDictionary::getDictId, result.getBusinessStatus());
        List<StoreDictionary> storeDictionaries = storeDictionaryMapper.selectList(storeDictionaryLambdaQueryWrapper);
        if (!storeDictionaries.isEmpty()) {
            result.setBusinessStatusStr(storeDictionaries.get(0).getDictDetail());
        }
        return result;
    }

    @Override
    public void approveStoreInfo(String storeId, Integer approvalStatus, String reason) {
        StoreInfo storeInfo = storeInfoMapper.selectById(storeId);
        storeInfo.setStoreApplicationStatus(approvalStatus);
        storeInfo.setReason(reason);
        StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getStoreId, storeInfo.getId()).eq(StoreUser::getDeleteFlag, 0));
        storeInfoMapper.updateById(storeInfo);

        //处理中台审核记录
        LambdaUpdateWrapper<WebAudit> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(WebAudit::getStoreInfoId, storeId);
        wrapper.set(WebAudit::getStatus, "1");
        webAuditMapper.update(null, wrapper);

        // 发送审核通知
        LifeNotice lifeNotice = new LifeNotice();
        lifeNotice.setSenderId("system");
        lifeNotice.setReceiverId("store_" + storeUser.getPhone());
        lifeNotice.setBusinessId(storeInfo.getId());
        lifeNotice.setTitle("店铺审核通知");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createDate = simpleDateFormat.format(storeInfo.getCreatedTime());

        com.alibaba.fastjson2.JSONObject jsonObject = new com.alibaba.fastjson2.JSONObject();
        if (2 == approvalStatus) {
            jsonObject.put("message", "您在"+createDate+"提交的入驻店铺申请，审核失败。失败原因："+reason+"。");
        } else {
            jsonObject.put("message", "您在"+createDate+"提交的入驻店铺申请，已通过审核，欢迎您的加入。");
        }
        lifeNotice.setContext(jsonObject.toJSONString());
        lifeNotice.setNoticeType(1); // 系统通知
        lifeNotice.setIsRead(0);
        lifeNoticeUtil.fillUserTypeAndRefId(lifeNotice);
        lifeNoticeMapper.insert(lifeNotice);

        WebSocketVo websocketVo = new WebSocketVo();
        websocketVo.setSenderId("system");
        websocketVo.setReceiverId("store_" + storeUser.getPhone());
        websocketVo.setCategory("notice");
        websocketVo.setNoticeType("1");
        websocketVo.setIsRead(0);
        websocketVo.setText(com.alibaba.fastjson2.JSONObject.from(lifeNotice).toJSONString());
    }

    @Override
    public String exportExcel(String id, String storePhone, String businessSection, String storeApplicationStatusStr) throws IOException {
        // 定义格式化模式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 先将 Date 转换为 Instant
        List<StoreInfoVo> storeInfoVos = storeInfoMapper.getStoreInfoVo(new QueryWrapper<StoreInfoVo>().like(StringUtils.isNotEmpty(id), "a.id", id).like(StringUtils.isNotEmpty(storePhone), "b.phone", storePhone).eq(StringUtils.isNotEmpty(storeApplicationStatusStr), "a.store_application_status", storeApplicationStatusStr).eq(StringUtils.isNotEmpty(businessSection), "a.business_section", businessSection).eq("a.delete_flag", 0).eq("b.delete_flag", 0).orderByDesc("a.created_time"));
        List<StoreInfoExcelVo> storeInfoExcelVos = new ArrayList<>();
        int serialNumber = 0;
        for (StoreInfoVo storeInfoVo : storeInfoVos) {
            StoreInfoExcelVo storeInfoExcelVo = new StoreInfoExcelVo();
            //存入序号
            storeInfoExcelVo.setSerialNumber(++serialNumber);
            //获取营业执照图片
            List<StoreImg> storeImgs = storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getStoreId, storeInfoVo.getId()).eq(StoreImg::getImgType, 14));
            storeInfoExcelVo.setBusinessLicense(storeImgs != null && storeImgs.size() > 0 ? storeImgs.get(0).getImgUrl() : "");
            //获取合同图片
            List<StoreImg> storeContractImgs = storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getStoreId, storeInfoVo.getId()).eq(StoreImg::getImgType, 15));
            storeInfoExcelVo.setContractImage(storeContractImgs != null && storeContractImgs.size() > 0 ? storeContractImgs.get(0).getImgUrl() : "");
            BeanUtils.copyProperties(storeInfoVo, storeInfoExcelVo);
            Instant instant = storeInfoVo.getCreatedTime().toInstant();
            // 格式化时间
            String formattedTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime().format(formatter);
            storeInfoExcelVo.setCreatedTime(formattedTime);
            //格式化状态
            storeInfoExcelVo.setStoreStatus(storeInfoVo.getStoreStatus() == 0 ? "禁用" : "正常");
            //格式化审批状态
            Integer storeApplicationStatus = storeInfoVo.getStoreApplicationStatus();
            if (storeApplicationStatus == 0) {
                storeInfoExcelVo.setStoreApplicationStatus("待审批");
            } else if (storeApplicationStatus == 1) {
                storeInfoExcelVo.setStoreApplicationStatus("审批通过");
            } else if (storeApplicationStatus == 2) {
                storeInfoExcelVo.setStoreApplicationStatus("审批拒绝");
            }
            //存入创建时间
            storeInfoExcelVos.add(storeInfoExcelVo);
        }
        String fileName = UUID.randomUUID().toString().replace("-", "");
        String filePath = ExcelGenerator.generateExcel(excelPath + excelGeneratePath + fileName + ".xlsx", storeInfoExcelVos, StoreInfoExcelVo.class);
        return aliOSSUtil.uploadFile(new File(filePath), "excel/" + fileName + ".xlsx");
    }

    @Override
    public String exportExcelExpirationTime(String id, String expiredState) throws IOException {
        // 获取当前时刻
        Date currentDate = new Date();
        // 定义格式化模式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 先将 Date 转换为 Instant
        List<StoreInfoVo> storeInfoVos = storeInfoMapper.getStoreInfoVo(new QueryWrapper<StoreInfoVo>().like(StringUtils.isNotEmpty(id), "a.id", id).eq("a.delete_flag", 0).eq("b.delete_flag", 0).orderByDesc("a.created_time"));
        List<StoreInfoExpiredRecordsExcelVo> storeInfoExpiredRecordsExcelVos = new ArrayList<>();
        int serialNumber = 0;
        for (StoreInfoVo storeInfoVo : storeInfoVos) {
            StoreInfoExpiredRecordsExcelVo storeInfoExpiredRecordsExcelVo = new StoreInfoExpiredRecordsExcelVo();
            storeInfoExpiredRecordsExcelVo.setSerialNumber(++serialNumber);
            BeanUtils.copyProperties(storeInfoVo, storeInfoExpiredRecordsExcelVo);
            if (storeInfoVo.getExpirationTime() != null) {
                Instant instant = storeInfoVo.getExpirationTime().toInstant();
                // 格式化时间
                String formattedTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime().format(formatter);
                storeInfoExpiredRecordsExcelVo.setExpirationTime(formattedTime);
                //格式化过期状态
                //处理一下到期状态
                Date expirationTime = storeInfoVo.getExpirationTime();
                // 获取当前时间
                Calendar now = Calendar.getInstance();
                Date nowCurrentDate = now.getTime();

                // 计算 30 天后的时间
                now.add(Calendar.DAY_OF_YEAR, 30);
                Date thirtyDaysLater = now.getTime();
                // 比较两个日期
                if (expirationTime.after(currentDate)) {
                    storeInfoExpiredRecordsExcelVo.setExpiredState("未过期");
                    if (expirationTime != null && (expirationTime.after(nowCurrentDate) || expirationTime.equals(nowCurrentDate)) && expirationTime.before(thirtyDaysLater)) {
                        storeInfoExpiredRecordsExcelVo.setExpiredState("即将过期");
                    }
                } else {
                    storeInfoExpiredRecordsExcelVo.setExpiredState("已过期");
                }
                // 获取当前时间
                LocalDate nowLocal = LocalDate.now();
                // 将 expirationTime 转换为 LocalDate
                LocalDate expDate = expirationTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

                // 计算距离到期的天数
                long daysToExpire = ChronoUnit.DAYS.between(nowLocal, expDate);
                storeInfoExpiredRecordsExcelVo.setDaysToExpire(daysToExpire > 0 ? daysToExpire : 0);

                //存入创建时间
                if (StringUtils.isNotEmpty(expiredState)) {
                    String expiredStateName = storeInfoExpiredRecordsExcelVo.getExpiredState();
                    if (expiredState.equals("0") && expiredStateName.equals("未过期")) {
                        storeInfoExpiredRecordsExcelVos.add(storeInfoExpiredRecordsExcelVo);
                    }
                    if (expiredState.equals("1") && expiredStateName.equals("即将过期")) {
                        storeInfoExpiredRecordsExcelVos.add(storeInfoExpiredRecordsExcelVo);
                    }
                    if (expiredState.equals("2") && expiredStateName.equals("已过期")) {
                        storeInfoExpiredRecordsExcelVos.add(storeInfoExpiredRecordsExcelVo);
                    }
                } else {
                    storeInfoExpiredRecordsExcelVos.add(storeInfoExpiredRecordsExcelVo);
                }
            }
        }
        String fileName = UUID.randomUUID().toString().replace("-", "");
        String filePath = ExcelGenerator.generateExcel(excelPath + excelGeneratePath + fileName + ".xlsx", storeInfoExpiredRecordsExcelVos, StoreInfoExpiredRecordsExcelVo.class);
        String url = aliOSSUtil.uploadFile(new File(filePath), "excel/" + fileName + ".xlsx");
        new File(filePath).delete();
        return url;
    }

    @Override
    public IPage<StoreInfo> getScreening(ScreeningOfEightMajorCategoriesVO vo) {
        // 参数校验
        if (vo == null) {
            throw new IllegalArgumentException("筛选参数不能为空");
        }

        // 创建分页对象
        IPage<StoreInfo> page = new Page<>(vo.getPageNum(), vo.getPageSize());

        // 获取筛选条件
        String screeningId = vo.getScreeningId();
        Double lon = vo.getLon();
        Double lat = vo.getLat();
        Double distance = vo.getDistance();
        Boolean flag = vo.getFlag();
        String startTime = vo.getStartTime();
        String endTime = vo.getEndTime();
        Double priceStr = vo.getPriceStr();
        Double priceEnd = vo.getPriceEnd();

        // 验证经纬度和距离参数
        if (flag != null && flag) {
            if (lon == null || lat == null || distance == null) {
                throw new IllegalArgumentException("启用距离筛选时，经纬度和距离参数不能为空");
            }
            if (distance <= 0) {
                throw new IllegalArgumentException("距离参数必须大于0");
            }
        }

        String storeId = "";

        // 通过筛选id获取商家id
        if (StringUtils.isNotEmpty(screeningId)) {
            String[] list = screeningId.split(",");
            for (String id : list) {
                if (StringUtils.isEmpty(storeId)) {
                    storeId = baseRedisService.getString(id);
                } else {
                    String redisStoreId = baseRedisService.getString(id);
                    if (redisStoreId != null) {
                        storeId = findCommon(storeId, redisStoreId);
                    }
                }
            }
        }

        // 通过商家id获取商家信息
        IPage<StoreInfo> storeInfoIPage = new Page<>();

        if (StringUtils.isNotEmpty(storeId)) {
            List<String> storeIds = Arrays.asList(storeId.split(","));
            if (StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
                List<StoreBusinessInfo> storeBusinessInfos = storeBusinessInfoMapper.selectList(new LambdaQueryWrapper<StoreBusinessInfo>().in(StoreBusinessInfo::getStoreId, storeIds));
                DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendValue(ChronoField.HOUR_OF_DAY, 1, 2, java.time.format.SignStyle.NOT_NEGATIVE).appendLiteral(':').appendValue(ChronoField.MINUTE_OF_HOUR, 2).toFormatter();
                List<StoreBusinessInfo> list = storeBusinessInfos.stream().filter(item -> {
                    // 商家开门时间
                    LocalTime timeStart = LocalTime.parse(item.getEndTime(), formatter);
                    // 商家关门时间
                    LocalTime timeEnd = LocalTime.parse(item.getEndTime(), formatter);
                    // 开始时间
                    LocalTime time1 = LocalTime.parse(startTime);
                    // 结束时间
                    LocalTime time2 = LocalTime.parse(endTime);
                    return !time1.isBefore(timeStart) && !time2.isAfter(timeEnd);
                }).collect(Collectors.toList());

                storeIds = list.stream().map(item -> String.valueOf(item.getStoreId())).collect(Collectors.toList());
            }
            storeIds.stream().filter(Objects::nonNull) // 确保 storeId 不为 null
                    .distinct() // 去重
                    .map(String::valueOf).collect(Collectors.toList());
            if (priceStr != null && priceEnd != null) {
                List<LifeCoupon> lifeCoupons = lifeCouponMapper.selectList(new LambdaQueryWrapper<LifeCoupon>().in(LifeCoupon::getStoreId, storeIds));
                List<LifeCoupon> list = lifeCoupons.stream().filter(item -> Double.parseDouble(item.getPrice()) >= priceStr && Double.parseDouble(item.getPrice()) < priceEnd).collect(Collectors.toList());
                storeIds = list.stream().map(item -> String.valueOf(item.getStoreId())).collect(Collectors.toList());
            }
            if (!CollectionUtils.isEmpty(storeIds)) {
                LambdaQueryWrapper<StoreInfo> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.in(StoreInfo::getId, storeIds);
                storeInfoIPage = storeInfoMapper.selectPage(page, queryWrapper);
            }
        }
        return storeInfoIPage;
    }

    @Override
    public IPage<StoreInfoVo> getScreeningNew(ScreeningOfEightMajorCategoriesVO vo) {
        // 参数校验
        if (vo == null) {
            throw new IllegalArgumentException("筛选参数不能为空");
        }

        // 获取筛选条件
        String screeningId = vo.getScreeningId();
        Double lon = vo.getLon();
        Double lat = vo.getLat();
        Double distance = vo.getDistance();
        Boolean flag = vo.getFlag();
        String startTime = vo.getStartTime();
        String endTime = vo.getEndTime();
        Double priceStr = vo.getPriceStr();
        Double priceEnd = vo.getPriceEnd();

        if (lon == null || lat == null) {
            throw new IllegalArgumentException("经纬度参数不能为空");
        }
        // 创建分页对象
        IPage<StoreInfoVo> page = new Page<>(vo.getPageNum(), vo.getPageSize());

        String storeId = "";

        // 通过筛选id获取商家id
        if (StringUtils.isNotEmpty(screeningId)) {
            String[] list = screeningId.split(",");
            for (String id : list) {
                if (StringUtils.isEmpty(storeId)) {
                    storeId = baseRedisService.getString(id);
                } else {
                    String redisStoreId = baseRedisService.getString(id);
                    if (redisStoreId != null) {
                        storeId = findCommon(storeId, redisStoreId);
                    }
                }
            }
        }

        // 通过商家id获取商家信息
        IPage<StoreInfoVo> storeInfoIPage = new Page<>();
        QueryWrapper<StoreInfoVo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(storeId)) {
            List<String> storeIds = Arrays.asList(storeId.split(","));
            if (StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
                List<StoreBusinessInfo> storeBusinessInfos = storeBusinessInfoMapper.selectList(new LambdaQueryWrapper<StoreBusinessInfo>().in(StoreBusinessInfo::getStoreId, storeIds));
                DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendValue(ChronoField.HOUR_OF_DAY, 1, 2, java.time.format.SignStyle.NOT_NEGATIVE).appendLiteral(':').appendValue(ChronoField.MINUTE_OF_HOUR, 2).toFormatter();
                List<StoreBusinessInfo> list = storeBusinessInfos.stream().filter(item -> {
                    // 商家开门时间
                    LocalTime timeStart = LocalTime.parse(item.getEndTime(), formatter);
                    // 商家关门时间
                    LocalTime timeEnd = LocalTime.parse(item.getEndTime(), formatter);
                    // 开始时间
                    LocalTime time1 = LocalTime.parse(startTime);
                    // 结束时间
                    LocalTime time2 = LocalTime.parse(endTime);
                    return !time1.isBefore(timeStart) && !time2.isAfter(timeEnd);
                }).collect(Collectors.toList());

                storeIds = list.stream().map(item -> String.valueOf(item.getStoreId())).collect(Collectors.toList());
            }
            storeIds.stream().filter(Objects::nonNull) // 确保 storeId 不为 null
                    .distinct() // 去重
                    .map(String::valueOf).collect(Collectors.toList());
            if (priceStr != null && priceEnd != null) {
                List<LifeCoupon> lifeCoupons = lifeCouponMapper.selectList(new LambdaQueryWrapper<LifeCoupon>().in(LifeCoupon::getStoreId, storeIds));
                List<LifeCoupon> list = lifeCoupons.stream().filter(item -> Double.parseDouble(item.getPrice()) >= priceStr && Double.parseDouble(item.getPrice()) < priceEnd).collect(Collectors.toList());
                storeIds = list.stream().map(item -> String.valueOf(item.getStoreId())).collect(Collectors.toList());
            }
            if (!CollectionUtils.isEmpty(storeIds)) {
                queryWrapper.in("a.id", storeIds);
            }
        }

        queryWrapper.eq("a.delete_flag", 0);
        queryWrapper.eq("a.logout_flag", 0);
        queryWrapper.ne("a.store_status", 0);

        queryWrapper.ge("a.food_licence_expiration_time", new Date());

        queryWrapper.like(StringUtils.isNotEmpty(vo.getFoodType()), "a.business_types_name", vo.getFoodType());
        queryWrapper.like(StringUtils.isNotEmpty(vo.getStoreName()), "a.store_name", vo.getStoreName());

        if (ObjectUtils.isNotEmpty(vo.getRqyx())) {
            if (vo.getRqyx() == 1) {
                queryWrapper.orderByDesc("collect_num");
            }
        }

        if (ObjectUtils.isNotEmpty(vo.getKwyx())) {
            if (vo.getKwyx() == 1) {
                queryWrapper.orderByDesc("taste_score");
            }
        }

        if (ObjectUtils.isNotEmpty(vo.getHjyx())) {
            if (vo.getHjyx() == 1) {
                queryWrapper.orderByDesc("en_score");
            }
        }

        if (ObjectUtils.isNotEmpty(vo.getFwyx())) {
            if (vo.getFwyx() == 1) {
                queryWrapper.orderByDesc("service_score");
            }
        }

        if (ObjectUtils.isNotEmpty(vo.getHpyx())) {
            if (vo.getHpyx() == 1) {
                queryWrapper.orderByDesc("score_avg");
            }
        }
        queryWrapper.orderByAsc("dist");
        storeInfoIPage = storeInfoMapper.getPageForDistance(page, lon + "," + lat, queryWrapper);
        List<StoreInfoVo> storeInfoVoList = storeInfoIPage.getRecords();

        // 提取所有符合条件的门店ID
        List<Integer> storeIds = storeInfoVoList.stream().map(StoreInfoVo::getId).collect(Collectors.toList());
        // 构建查询优惠券的条件对象
        LambdaUpdateWrapper<LifeCoupon> quanWrapper = new LambdaUpdateWrapper<>();
        // 添加门店ID筛选条件
        quanWrapper.in(ObjectUtils.isNotEmpty(storeIds),LifeCoupon::getStoreId, storeIds)
                // 添加优惠券状态筛选条件
                .eq(LifeCoupon::getStatus, CouponStatusEnum.ONGOING.getCode())
                // 按照创建时间降序排序
                .orderByDesc(LifeCoupon::getCreatedTime);
        // 查询符合条件的优惠券列表
        List<LifeCoupon> quanList = lifeCouponMapper.selectList(quanWrapper);
        // 构建查询团购套餐条件对象
        LambdaUpdateWrapper<LifeGroupBuyMain> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        // 添加门店ID筛选条件
        lambdaUpdateWrapper.in(ObjectUtils.isNotEmpty(storeIds),LifeGroupBuyMain::getStoreId, storeIds)
                // 添加团购套餐状态筛选条件
                .eq(LifeGroupBuyMain::getStatus, 5)
                .orderByDesc(LifeGroupBuyMain::getCreatedTime);
        // 查询符合条件的团购套餐列表
        List<LifeGroupBuyMain> lifeGroupBuyMainList = lifeGroupBuyMainMapper.selectList(lambdaUpdateWrapper);
        // 获取全部店铺的评分与平均花销
        Map<String, List<Map<String, Object>>> avgScoreMap = storeEvaluationMapper.allStoreAvgScore().stream().collect(Collectors.groupingBy(o -> o.get("store_id").toString()));
        // 获取用户订单信息，用于计算平均消费
        Map<String, List<Map<String, Object>>> avgPriceMap = lifeUserOrderMapper.allStoreAvgPrice().stream().collect(Collectors.groupingBy(o -> o.get("store_id").toString()));
        // 获取所有店铺的打卡次数
        // 遍历所有门店信息，构造返回结果
        for (StoreInfoVo store : storeInfoVoList) {
            Map<String, Object> storeMap = new HashMap<>();
            storeMap.put("dist", store.getDist());
            // 添加门店的业务状态及其描述
            storeMap.put("businessStatus", store.getBusinessStatus());
            storeMap.put("businessStatusStr", store.getBusinessStatusStr());
            // 添加门店ID
            storeMap.put("storeId", store.getId());
            // 如果存在评分数据，则添加评分、平均消费及评价总数
            if (avgScoreMap.containsKey(String.valueOf(store.getId()))) {
                store.setAvgScore(new BigDecimal(avgScoreMap.get(String.valueOf(store.getId())).get(0).get("avg_score").toString()).setScale(1, RoundingMode.HALF_UP).toString());
                store.setTotalNum(avgScoreMap.get(String.valueOf(store.getId())).get(0).get("total_num").toString());
                /*storeMap.put("avgScore", avgScoreMap.get(String.valueOf(store.getId())).get(0).get("avg_score"));
                storeMap.put("avgPrice", avgPriceMap.get(String.valueOf(store.getId())).get(0).get("avg_price"));
                // 条数
                storeMap.put("totalNum", avgScoreMap.get(String.valueOf(store.getId())).get(0).get("total_num"));*/
            } else {
                // 否则，设置默认值
                /*storeMap.put("avgScore", 0);
                storeMap.put("avgPrice", 0);
                storeMap.put("totalNum", 0);*/
                store.setAvgScore("0");
                store.setTotalNum("0");
            }
            if (avgPriceMap.containsKey(String.valueOf(store.getId()))) {
                store.setAvgPrice(avgPriceMap.get(String.valueOf(store.getId())).get(0).get("avg_price").toString());
            } else {
                store.setAvgPrice("0");
            }
            // 添加门店名称
            storeMap.put("storeName", store.getStoreName());
            // 添加门店地址
            storeMap.put("storeAddress", store.getStoreAddress());
            // 添加门店类型
            storeMap.put("storeType", store.getStoreType());
            // 添加门店入口图片
            storeMap.put("entranceImage", store.getEntranceImage());
            // 添加门店图片
            storeMap.put("storeImage", store.getImgUrl());
            // 解析并添加门店经纬度
            String[] position = store.getStorePosition().split(",");
            storeMap.put("longitude", position.length == 2 ? position[0] : "");
            storeMap.put("latitude", position.length == 2 ? position[1] : "");
            // 构造该门店的优惠券列表
            List<Map<String, Object>> quanMapList = new ArrayList<>();
            if (!quanList.isEmpty()) {
                for (LifeCoupon quan : quanList) {
                    // 如果优惠券的门店ID与当前门店ID匹配，则添加到优惠券列表中
                    if (store.getId().toString().equals(quan.getStoreId())) {
                        Map<String, Object> quanMap = new HashMap<>();
                        quanMap.put("quanType", quan.getType());
                        quanMap.put("name", quan.getName());
                        quanMap.put("price", quan.getPrice());
                        // 折扣价
                        quanMap.put("offprice", quan.getOffprice());
                        quanMap.put("quanId", quan.getId());
                        quanMapList.add(quanMap);
                    }
                }
                // 将优惠券列表添加到门店信息中
                storeMap.put("quanList", quanMapList);
                store.setQuanList(quanMapList);
            }

            // 构造该门店的团购套餐列表
            List<LifeGroupBuyMainVo> lifeGroupBuyMains = new ArrayList<>();
            if (!lifeGroupBuyMainList.isEmpty()) {
                for (LifeGroupBuyMain quan : lifeGroupBuyMainList) {
                    // 如果团购套餐的门店ID与当前门店ID匹配，则添加到团购列表中
                    if (store.getId().toString().equals(quan.getStoreId())) {
                        LifeGroupBuyMainVo lifeGroupBuyMainVo = new LifeGroupBuyMainVo();
                        BeanUtils.copyProperties(quan, lifeGroupBuyMainVo);
                        lifeGroupBuyMains.add(lifeGroupBuyMainVo);
                    }
                }
                // 将团购套餐列表添加到门店信息中
                storeMap.put("tuangouList", lifeGroupBuyMains);
                // 将团购套餐列表添加到门店信息中
                store.setTuangouList(lifeGroupBuyMains);
            }
            // 添加门店类型的描述
            storeMap.put("storeTypeStr", store.getStoreTypeStr());
            // 如果未找到打卡次数，则设置为0
            if (null == storeMap.get("storeClockInCount")) {
                storeMap.put("storeClockInCount", 0);
                store.setStoreClockInCount("0");
            }
            // 将当前门店的信息添加到返回结果列表中
//            returnMaps.add(storeMap);
        }

        return storeInfoIPage;
    }

    @Override
    public List<StoreInfo> getBusinessTypesName(String businessTypesNames) {
        String[] businessTypesName = businessTypesNames.split(",");
        List<StoreInfo> storeInfos = new ArrayList<>();
        for (String typesName : businessTypesName) {
            LambdaQueryWrapper<StoreInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(StoreInfo::getBusinessTypesName, typesName);
            List<StoreInfo> infos = storeInfoMapper.selectList(queryWrapper);
            storeInfos.addAll(infos);
        }
        return new ArrayList<>(storeInfos.stream().collect(Collectors.toMap(StoreInfo::getId, info -> info, (existing, replacement) -> existing)).values());
    }

    public static String findCommon(String... strings) {
        if (strings == null || strings.length == 0) {
            return "";
        }
        // 将第一个字符串拆分成集合
        Set<String> common = new HashSet<>(Arrays.asList(strings[0].split(",")));
        // 遍历其余字符串
        for (int i = 1; i < strings.length; i++) {
            Set<String> currentSet = new HashSet<>(Arrays.asList(strings[i].split(",")));
            // 找出共同元素
            common.retainAll(currentSet);
        }
        // 将共同元素集合转换为以逗号分隔的字符串
        return String.join(",", common);
    }

    /**
     * 根据过期时间修改状态
     */
    public void checkAndUpdateExpiredRecords() {
        LambdaUpdateWrapper<StoreInfo> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.isNotNull(StoreInfo::getExpirationTime).lt(StoreInfo::getExpirationTime, LocalDateTime.now()).eq(StoreInfo::getBusinessStatus, 0).set(StoreInfo::getBusinessStatus, 99);
        this.update(queryWrapper);
    }

    @Override
    public void logoutStore(StoreInfoVo storeInfo) {
        throw new IllegalArgumentException("店铺注销已合并至商家账号注销，请通过商家账号注销接口操作");
    }

    @Override
    public void cancelLogoutStore(StoreInfoVo storeInfo) {
        throw new IllegalArgumentException("店铺撤销注销已合并至商家账号注销，请通过商家账号撤销注销接口操作");
    }

    @Override
    public List<StoreInfo> getStoreInfoByStoreTel(String storeTel) {
        LambdaQueryWrapper<StoreInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StoreInfo::getStoreTel, storeTel);
        return storeInfoMapper.selectList(queryWrapper);
    }

    @Override
    public int editStoreCommissionRate(StoreInfoDto storeInfoDto) {
        LambdaUpdateWrapper<StoreInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(StoreInfo::getId, storeInfoDto.getId());
        wrapper.set(StoreInfo::getCommissionRate, storeInfoDto.getCommissionRate());
        return storeInfoMapper.update(null, wrapper);
    }

    @Override
    public int saveOrUpdateStoreInfo(StoreInfoDto storeInfodto) {
        if (storeInfodto.getId() != null) {
            // 校验当前店铺存在未完成的订单及正在销售的商品
            verificationStoreInfoStatus(storeInfodto);

            // 1. 处理经营板块及经营种类
            Integer businessSection = storeInfodto.getBusinessSection();
            StoreDictionary businessSectionDict = null;
            List<String> businessTypeNames = new ArrayList<>();

            // 查询经营板块（非空判断，避免后续空指针）
            if (Objects.nonNull(businessSection)) {
                businessSectionDict = storeDictionaryMapper.selectOne(
                        new LambdaQueryWrapper<StoreDictionary>()
                                .eq(StoreDictionary::getDictId, businessSection)
                                .eq(StoreDictionary::getTypeName, "business_section")
                );
                // 若经营板块不存在，可根据业务抛出异常或默认处理
                if (Objects.isNull(businessSectionDict)) {
                    throw new IllegalArgumentException("经营板块不存在：" + businessSection);
                }
            }

            // 批量查询经营种类（替代循环查询，解决N+1问题）
            List<String> businessTypes = storeInfodto.getBusinessTypes();
            if (Objects.nonNull(businessSectionDict) && !CollectionUtils.isEmpty(businessTypes)) {
                // 一次查询所有符合条件的经营种类
                List<StoreDictionary> typeDicts = storeDictionaryMapper.selectList(
                        new LambdaQueryWrapper<StoreDictionary>()
                                .in(StoreDictionary::getDictId, businessTypes) // 批量匹配id
                                .eq(StoreDictionary::getParentId, businessSectionDict.getId())
                );
                // 转为Map<dictId, StoreDictionary>，方便快速获取
                Map<String, StoreDictionary> typeDictMap = typeDicts.stream()
                        .collect(Collectors.toMap(
                                dict -> dict.getDictId().toString(), // 假设dictId是字符串类型，若为数字需转换
                                Function.identity(),
                                (existing, replacement) -> existing // 处理重复id（理论上不会有）
                        ));
                // 提取经营种类名称
                for (String typeId : businessTypes) {
                    StoreDictionary dict = typeDictMap.get(typeId);
                    if (Objects.nonNull(dict)) {
                        businessTypeNames.add(dict.getDictDetail());
                    } else {
                        // 可选：记录无效的经营种类id，便于排查
                        log.warn("无效的经营种类id：" + typeId + "（所属板块：" + businessSectionDict.getDictDetail() + "）");
                    }
                }
            }

            // 2. 构建StoreInfo对象
            StoreInfo storeInfo = new StoreInfo();
            BeanUtils.copyProperties(storeInfodto, storeInfo);

            // 3. 处理经纬度
            String longitude = storeInfodto.getStorePositionLongitude();
            String latitude = storeInfodto.getStorePositionLatitude();
            if (StringUtils.isEmpty(longitude) || StringUtils.isEmpty(latitude)) {
                // 经纬度为空时，复用原数据
                StoreInfo oldStore = storeInfoMapper.selectById(storeInfodto.getId());
                if (Objects.nonNull(oldStore)) {
                    storeInfo.setStorePosition(oldStore.getStorePosition());
                }
            } else {
                // 拼接经纬度
                storeInfo.setStorePosition(longitude + "," + latitude);
            }

            // 4. 设置门店状态、经营板块及类型
            storeInfo.setStoreStatus(storeInfodto.getStoreStatus());
            // 经营板块（非空时才设置）
            if (Objects.nonNull(businessSection)) {
                storeInfo.setBusinessSection(businessSection);
            }
            // 经营板块名称（非空时才设置）
            if (Objects.nonNull(businessSectionDict)) {
                storeInfo.setBusinessSectionName(businessSectionDict.getDictDetail());
            }
            // 经营种类及名称（非空时拼接）
            if (!CollectionUtils.isEmpty(businessTypes) && !CollectionUtils.isEmpty(businessTypeNames)) {
                storeInfo.setBusinessTypes(String.join(",", businessTypes));
                storeInfo.setBusinessTypesName(String.join(",", businessTypeNames));
            }

            // 5. 处理行政区域信息（抽取为方法，减少重复） 不存在单独查的情况
            if (!Objects.isNull(storeInfo.getAdministrativeRegionProvinceAdcode())) {

                storeInfo.setAdministrativeRegionProvinceName(
                        getAreaName(storeInfo.getAdministrativeRegionProvinceAdcode())
                );
                storeInfo.setAdministrativeRegionCityName(
                        getAreaName(storeInfo.getAdministrativeRegionCityAdcode())
                );
                storeInfo.setAdministrativeRegionDistrictName(
                        getAreaName(storeInfo.getAdministrativeRegionDistrictAdcode())
                );
            }

            // 6. 执行更新
            int updateNum = storeInfoMapper.updateById(storeInfo);

            // 7. 更新地理位置信息（处理经纬度解析异常）
            if (!StringUtils.isEmpty(longitude) && !StringUtils.isEmpty(latitude)) {
                try {
                    double lon = Double.parseDouble(longitude);
                    double lat = Double.parseDouble(latitude);
                    nearMeService.inGeolocation(new Point(lon, lat), storeInfo.getId().toString(), Boolean.TRUE);
                } catch (NumberFormatException e) {
                    log.error("经纬度解析失败：longitude=" + longitude + ", latitude=" + latitude);
                    // 可选：根据业务是否抛出异常，或忽略
                    // throw new IllegalArgumentException("经纬度格式错误", e);
                }
            }
            return updateNum;
        } else {
            StoreInfo storeInfo = new StoreInfo();
            BeanUtils.copyProperties(storeInfodto, storeInfo);
            int num = storeInfoMapper.insert(storeInfo);
            return num;
        }
    }

    // 抽取：根据区域编码查询区域名称（通用方法）
    private String getAreaName(String areaCode) {
        if (StringUtils.isEmpty(areaCode)) {
            return ""; // 或返回null，根据业务定义
        }
        EssentialCityCode cityCode = essentialCityCodeMapper.selectOne(
                new LambdaQueryWrapper<EssentialCityCode>().eq(EssentialCityCode::getAreaCode, areaCode)
        );
        return Objects.nonNull(cityCode) ? cityCode.getAreaName() : "";
    }

    @Override
    public Map<String, String> storeInfoVerification(int id) {
        Map<String, String> storeInfoMap = new HashMap<>();
        LambdaQueryWrapper<StoreInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StoreInfo::getId, id);
        StoreInfo storeInfo = storeInfoMapper.selectOne(queryWrapper);
//      当前时间 string类型的当前时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String formattedDate = sdf.format(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, -4);
        Date startDay = calendar.getTime();
        String startDayS = sdf.format(startDay);
        if (storeInfo != null) {
            //判断店铺存在未体现金额 0：未通过 1：通过
            //判断店铺存在未完成订单 0：未通过 1：通过
//            List<LifeUserOrder> lifeUserOrders = lifeUserOrderMapper.selectList(new LambdaQueryWrapper<LifeUserOrder>().in(LifeUserOrder::getStatus, 0, 1).eq(LifeUserOrder::getStoreId, id).eq(LifeUserOrder::getDeleteFlag, 0));
//            if (!CollectionUtils.isEmpty(lifeUserOrders)) {
//                storeInfoMap.put("storeOrderStatus", "0");
//            } else {
//                storeInfoMap.put("storeOrderStatus", "1");
//            }
            //判断店铺存在正在售卖的商品 0：未通过 1：通过  storeGoodsStatus 代金券   storeGroupStatus 团购
            List<LifeGroupBuyMain> lifeGroupBuyMainList = lifeGroupBuyMainMapper.selectList(new LambdaQueryWrapper<LifeGroupBuyMain>().eq(LifeGroupBuyMain::getStoreId, id).eq(LifeGroupBuyMain::getStatus, 5).eq(LifeGroupBuyMain::getDeleteFlag, 0));
            List<LifeCoupon> lifeCoupons = lifeCouponMapper.selectList(new LambdaQueryWrapper<LifeCoupon>().in(LifeCoupon::getStatus, 5).and(qw -> qw.gt(LifeCoupon::getSingleQty, 0)).eq(LifeCoupon::getStoreId, id).eq(LifeCoupon::getType, 1).eq(LifeCoupon::getDeleteFlag, 0));
            if (!CollectionUtils.isEmpty(lifeCoupons) ) {
                storeInfoMap.put("storeGoodsStatus", "0");
            } else {
                storeInfoMap.put("storeGoodsStatus", "1");
            }
            if (!CollectionUtils.isEmpty(lifeGroupBuyMainList)) {
                storeInfoMap.put("storeGroupStatus", "0");
            } else {
                storeInfoMap.put("storeGroupStatus", "1");
            }

        }
        return storeInfoMap;
    }

    @Override
    public void deleteManualStoreInfo(int id) {
        LambdaQueryWrapper<StoreInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StoreInfo::getId, id);
        StoreInfo storeInfo = storeInfoMapper.selectOne(queryWrapper);
        if (storeInfo != null && storeInfo.getStoreStatus() == -1) {
            storeInfoMapper.deleteById(id);
            StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>().eq(StoreUser::getStoreId,id));
            if(storeUser!= null){
                UpdateWrapper<StoreUser> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("id", storeUser.getId());
                updateWrapper.set("store_id", null);
                storeUserMapper.update(null, updateWrapper);
            }
        }
    }

    @Override
    public int uploadRenewalContract(List<StoreImg> storeImgList) {
        //取出集合中门店id
        Optional<Integer> storeId = storeImgList.stream().map(StoreImg::getStoreId).findFirst();
        int value = storeId.orElse(0);
        int num = 0;
        //先清除数据
        storeImgMapper.delete(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getImgType,22).eq(StoreImg::getStoreId,value));
        for (StoreImg renewContract : storeImgList) {
            StoreImg storeImg = new StoreImg();
            storeImg.setStoreId(renewContract.getStoreId());
            storeImg.setImgType(22);
            storeImg.setImgSort(renewContract.getImgSort());
            storeImg.setImgDescription("续签合同图片");
            storeImg.setImgUrl(renewContract.getImgUrl());
            storeImg.setCreatedUserId(renewContract.getCreatedUserId());
            num = storeImgMapper.insert(storeImg);
        }
        if (num > 0) {
            StoreInfo storeInfo = new StoreInfo();
            storeInfo.setRenewContractStatus(2);
            storeInfo.setUpdateRenewContractTime(new Date());
            storeInfo.setId(value);
            storeInfoMapper.updateById(storeInfo);
        }
        return num;
    }

    @Override
    public int uploadfoodLicence(StoreImg storeImg) {
            storeImgMapper.delete(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getImgType,24).eq(StoreImg::getStoreId,storeImg.getStoreId()));
            storeImg.setImgType(24);
            storeImg.setImgDescription("经营许可证审核通过前图片");
            storeImgMapper.insert(storeImg);

            licenseHistoryMapper.delete(new LambdaQueryWrapper<StoreLicenseHistory>()
                    .eq(StoreLicenseHistory::getLicenseStatus,2)
                    .eq(StoreLicenseHistory::getStoreId,storeImg.getStoreId()));
            // 经营许可证历史表插入
            StoreLicenseHistory licenseHistory = new StoreLicenseHistory();
            licenseHistory.setStoreId(storeImg.getStoreId());
            licenseHistory.setLicenseStatus(2);
            licenseHistory.setLicenseExecuteStatus(2);
            licenseHistory.setImgUrl(storeImg.getImgUrl());
            licenseHistory.setDeleteFlag(0);
            licenseHistoryMapper.insert(licenseHistory);

            //更新店铺
            StoreInfo storeInfo = new StoreInfo();
            storeInfo.setFoodLicenceStatus(2);
            storeInfo.setId(storeImg.getStoreId());
            storeInfo.setUpdateFoodLicenceTime(new Date());
        return storeInfoMapper.updateById(storeInfo);
    }

    @Override
    public Map<String, Object> getStoreContractStatus(int id) {
        Map<String, Object> map = new HashMap<>();
        StoreInfo storeInfo = storeInfoMapper.selectOne(new LambdaQueryWrapper<StoreInfo>().eq(StoreInfo::getId, id));
        //审核通过给前台反显未提交
        if (storeInfo.getRenewContractStatus() == 1) {
            map.put("renewContractStatus", 0);
        } else {
            map.put("renewContractStatus", storeInfo.getRenewContractStatus());
        }
        //续签合同照片列表
        List<StoreImg> storeImgList = storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getImgType, 22).eq(StoreImg::getStoreId, id));
        if (!CollectionUtils.isEmpty(storeImgList)) {
            map.put("contractImgList", storeImgList);
        } else {
            map.put("contractImgList", "");
        }
        if (storeInfo.getContractReason() != null) {
            map.put("contractReason", storeInfo.getContractReason());
        } else {
            map.put("contractReason", "");
        }
        return map;
    }

    @Override
    public Map<String, Object> getStoreFoodLicenceStatus(int id) {
        Map<String, Object> map = new HashMap<>();
        StoreInfo storeInfo = storeInfoMapper.selectOne(new LambdaQueryWrapper<StoreInfo>().eq(StoreInfo::getId, id));
        //审核通过给前台反显未提交
        if (storeInfo.getFoodLicenceStatus() == 1) {
            map.put("foodLicenceStatus", 0);
        } else {
            map.put("foodLicenceStatus", storeInfo.getFoodLicenceStatus());
        }
        //食品经营许可证照片列表
        List<StoreImg> storeImgList = storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getImgType, 24).eq(StoreImg::getStoreId, id));
            if (!CollectionUtils.isEmpty(storeImgList)) {
                map.put("foodLicenceImgList", storeImgList);
            } else {
                map.put("foodLicenceImgList", "");
        }
        if (storeInfo.getFoodLicenceReason() != null) {
            map.put("foodLicenceReason", storeInfo.getFoodLicenceReason());
        } else {
            map.put("foodLicenceReason", "");
        }
        return map;
    }

    @Override
    public int conversionContract(int id) {
        //删除原合同照片
        LambdaQueryWrapper<StoreImg> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StoreImg::getStoreId, id);
        lambdaQueryWrapper.eq(StoreImg::getImgType, 15);
        storeImgMapper.delete(lambdaQueryWrapper);
        //修改续签合同类型为合同类型
        List<StoreImg> storeImgList = storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getStoreId, id).eq(StoreImg::getImgType, 22));
        List<Integer> imgList = storeImgList.stream().map(StoreImg::getId).collect(Collectors.toList());
        LambdaUpdateWrapper<StoreImg> lambdaUpdateWrapper = new LambdaUpdateWrapper();
        lambdaUpdateWrapper.in(StoreImg::getId, imgList).set(StoreImg::getImgType, 15).set(StoreImg::getImgDescription,"合同图片");
        int num = storeImgMapper.update(null, lambdaUpdateWrapper);
        return num;
    }

    @Override
    public int foodLicenceType(int id) {
        //删除原合同照片
        LambdaUpdateWrapper<StoreImg> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(StoreImg::getStoreId, id);
        lambdaUpdateWrapper.eq(StoreImg::getImgType, 25);
        lambdaUpdateWrapper.set(StoreImg::getDeleteFlag,1);
        storeImgMapper.update(null,lambdaUpdateWrapper);
        //修改续签合同类型为合同类型
        List<StoreImg> storeImgList = storeImgMapper.selectList(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getStoreId, id).eq(StoreImg::getImgType, 24));
        List<Integer> imgList = storeImgList.stream().map(StoreImg::getId).collect(Collectors.toList());
        LambdaUpdateWrapper<StoreImg> imgLambdaUpdateWrapper = new LambdaUpdateWrapper();
        imgLambdaUpdateWrapper.in(StoreImg::getId, imgList).set(StoreImg::getImgType, 25).set(StoreImg::getImgDescription,"经营许可证审核通过图片");
        int num = storeImgMapper.update(null, imgLambdaUpdateWrapper);

        // 将原来的食品经营许可证历史表数据删除
        licenseHistoryMapper.delete(new LambdaQueryWrapper<StoreLicenseHistory>()
                .eq(StoreLicenseHistory::getStoreId,id)
                .eq(StoreLicenseHistory::getLicenseStatus,2)
                .eq(StoreLicenseHistory::getLicenseExecuteStatus,1)
                .eq(StoreLicenseHistory::getDeleteFlag,0));

        // 将新的食品经营许可证历史表数据变为审核通过
        LambdaUpdateWrapper<StoreLicenseHistory> wrapper1 = new LambdaUpdateWrapper<>();
        wrapper1.eq(StoreLicenseHistory::getStoreId, id);
        wrapper1.eq(StoreLicenseHistory::getLicenseStatus, 2);
        wrapper1.eq(StoreLicenseHistory::getLicenseExecuteStatus, 2);
        wrapper1.eq(StoreLicenseHistory::getDeleteFlag, 0);
        wrapper1.set(StoreLicenseHistory::getLicenseExecuteStatus, 1);
        licenseHistoryMapper.update(null, wrapper1);

        return num;
    }

    void verificationStoreInfoStatus(StoreInfoDto storeInfo) {
        //营业状态 0:正常营业, 1:暂停营业, 2:筹建中, 99:永久关门
        if (storeInfo.getBusinessStatus() == 99) {
            //判断店铺有未完成的订单
            List<LifeUserOrder> lifeUserOrders = lifeUserOrderMapper.selectList(new LambdaQueryWrapper<LifeUserOrder>().in(LifeUserOrder::getStatus, 0, 3).eq(LifeUserOrder::getStoreId, storeInfo.getId()).eq(LifeUserOrder::getDeleteFlag, 0));
            if (!CollectionUtils.isEmpty(lifeUserOrders)) {
                throw new RuntimeException("店铺有未完成的订单");
            }
            //判断店铺有正在售卖的商品
            List<LifeGroupBuyMain> lifeGroupBuyMainList = lifeGroupBuyMainMapper.selectList(new LambdaQueryWrapper<LifeGroupBuyMain>().eq(LifeGroupBuyMain::getStoreId, storeInfo.getId()).eq(LifeGroupBuyMain::getStatus, 5).eq(LifeGroupBuyMain::getDeleteFlag, 0));
            List<LifeCoupon> lifeCoupons = lifeCouponMapper.selectList(new LambdaQueryWrapper<LifeCoupon>().in(LifeCoupon::getStatus, 0, 1).and(qw -> qw.gt(LifeCoupon::getStockQty, 0).or().gt(LifeCoupon::getSingleQty, 0)).eq(LifeCoupon::getStoreId, storeInfo.getId()).eq(LifeCoupon::getType, 1).eq(LifeCoupon::getDeleteFlag, 0));
            if (!CollectionUtils.isEmpty(lifeCoupons) || !CollectionUtils.isEmpty(lifeGroupBuyMainList)) {
                throw new RuntimeException("店铺有正在销售的商品");
            }
        }
    }

    public int updateStoreImgModeInfo(StoreImgInfoVo storeImgInfoVo) {
        int result = 0;
        if (storeImgInfoVo != null) {
            // 校验必要参数是否为null
            if (storeImgInfoVo.getStoreId() == null || storeImgInfoVo.getImgMode() == null) {
                return result;
            }
            int imgType = storeImgInfoVo.getImgType();
            LambdaUpdateWrapper<StoreInfo> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(StoreInfo::getId, storeImgInfoVo.getStoreId());
            wrapper.set(StoreInfo::getImgMode, storeImgInfoVo.getImgMode());
            result = storeInfoMapper.update(null, wrapper);
        }
        return result;
    }

    @Override
    public boolean updateUserStatus(String phone, String type) {
        LambdaQueryWrapper<StoreUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StoreUser::getPhone, phone);
        StoreUser one = storeUserMapper.selectOne(lambdaQueryWrapper);
        if ("face".equals(type)) {
            one.setPassType(2);
        }
        if ("pass".equals(type)) {
            one.setPassType(2);
        }
        return storeUserMapper.updateById(one) > 0;
    }

    @Override
    public R<IPage<StoreUserVo>> getStoreUserList(int pageNum, int pageSize, String id, String phone, Integer status) {
        IPage<StoreUser> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<StoreUser> storeUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        storeUserLambdaQueryWrapper.like(!StringUtils.isEmpty(id), StoreUser::getId, id);
        storeUserLambdaQueryWrapper.like(!StringUtils.isEmpty(phone), StoreUser::getPhone, phone);
        storeUserLambdaQueryWrapper.eq(status != null, StoreUser::getStatus, status);
        storeUserLambdaQueryWrapper.orderByDesc(StoreUser::getCreatedTime);
        IPage<StoreUser> storeUsers = storeUserMapper.selectPage(page, storeUserLambdaQueryWrapper);
        IPage<StoreUserVo> storeUserVoIPage = new Page<>();
        BeanUtils.copyProperties(storeUsers, storeUserVoIPage);
        for (StoreUser storeUser : storeUserVoIPage.getRecords()) {
            //不返回密码
            storeUser.setPassword(null);
            storeUser.setPayPassword(null);
        }
        return R.data(storeUserVoIPage);
    }
}
