package com.tashow.cloud.product.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tashow.cloud.common.util.date.DateUtils;
import com.tashow.cloud.mybatis.mybatis.core.util.MyBatisUtils;
import com.tashow.cloud.productapi.api.product.dto.*;
import com.tashow.cloud.product.mapper.*;
import com.tashow.cloud.product.service.ProdPropService;
import com.tashow.cloud.product.service.ProdService;

import com.tashow.cloud.productapi.api.product.vo.prod.*;
import com.tashow.cloud.productapi.api.product.vo.prodadditionalfeedates.ProdAdditionalFeeBlackVO;
import com.tashow.cloud.productapi.api.product.vo.prodemergencyresponse.ProdEmergencyInfoVO;
import com.tashow.cloud.productapi.api.product.vo.prodreservationconfig.ProdReservationInfoReqVO;
import com.tashow.cloud.productapi.api.product.vo.prodreservationconfig.ProdReservationInfoVO;
import com.tashow.cloud.productapi.api.product.vo.sku.SkuPageReqVO;
import com.tashow.cloud.productapi.api.product.vo.sku.SkuRecycleBinVO;
import com.tashow.cloud.productapi.enums.BaseEnum;
import lombok.val;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.common.pojo.PageParam;
import com.tashow.cloud.common.util.object.BeanUtils;


import static com.tashow.cloud.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 商品 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ProdServiceImpl implements ProdService {

    @Resource
    private ProdMapper prodMapper;
    @Resource
    private SkuMapper skuMapper;
    @Resource
    private ProdPropService prodPropService;
    @Resource
    private ProdServiceAreasMapper prodServiceAreasMapper;
    @Resource
    private ProdServiceAreaRelevanceMapper prodServiceAreaRelevanceMapper;
    @Resource
    private ProdServiceOverAreaRulesMapper prodServiceOverAreaRulesMapper;
    @Resource
    private ProdReservationConfigMapper prodReservationConfigMapper;
    @Resource
    private ProdEmergencyResponseIntervalsMapper prodEmergencyResponseIntervalsMapper;
    @Resource
    private ProdEmergencyResponseMapper prodEmergencyResponseMapper;
    @Resource
    private ProductOrderLimitMapper productOrderLimitMapper;
    @Resource
    private ProdAdditionalFeeDatesMapper prodAdditionalFeeDatesMapper;
    @Resource
    private ProdAdditionalFeePeriodsMapper prodAdditionalFeePeriodsMapper;
    @Resource
    private ProdWeightRangePricesMapper prodWeightRangePricesMapper;
    @Resource
    private ProdExtendMapper prodExtendMapper;

    @Override
    @Transactional
    public Long createProd(ProdSaveReqVO createReqVO) {
        // 插入
        ProdDO prod = BeanUtils.toBean(createReqVO, ProdDO.class);
        prodMapper.insert(prod);
        //保存sku
        if (CollectionUtil.isNotEmpty(createReqVO.getSkuList())) {
            List<SkuDO> skuList = createReqVO.getSkuList();
            for (SkuDO sku : skuList) {
                sku.setProdId(prod.getProdId());
            }
            skuMapper.insertBatch(skuList);
        }
        createReqVO.setProdId(prod.getProdId());
        //保存规格
        prodPropService.saveProdPropAndValues(createReqVO);
        ProdExtendDO prodExtendDO = new ProdExtendDO();
        prodExtendDO.setIsExpire(BaseEnum.YES_ONE.getKey());
        prodExtendDO.setIsExpire(BaseEnum.YES_ONE.getKey());
        prodExtendDO.setProdId(prod.getProdId());
        prodExtendMapper.insert(prodExtendDO);
        // 返回
        return prod.getProdId();
    }

    @Override
    @Transactional
    public void createProdService(ProdServiceVO prodServiceVO) {
        ProdDO prodDO = new ProdDO();
        prodDO.setProdId(prodServiceVO.getProdId());
        //服务区域设置
        if (Objects.equals(prodServiceVO.getRegionSwitch(), BaseEnum.YES_ONE.getKey())) {
            prodDO.setRegionSwitch(BaseEnum.YES_ONE.getKey());
            //保存区域设置信息
            ProdServiceOverAreaRulesDO prodServiceOverAreaRules = BeanUtils.toBean(prodServiceVO.getProdServiceAreasInfo(), ProdServiceOverAreaRulesDO.class);
            prodServiceOverAreaRules.setProdId(prodDO.getProdId());
            prodServiceOverAreaRulesMapper.insert(prodServiceOverAreaRules);
            //保存区域
            List<String> areaNameList = prodServiceVO.getProdServiceAreasInfo().getAreaNameList();
            for (String areaName : areaNameList) {
                //查询区域信息
                ProdServiceAreasDO prodServiceAreas = prodServiceAreasMapper.selectOne(new LambdaQueryWrapper<ProdServiceAreasDO>()
                        .eq(ProdServiceAreasDO::getAreaName, areaName)
                );
                if (prodServiceAreas != null) {
                    //插入关联表
                    prodServiceAreaRelevanceMapper.insert(new ProdServiceAreaRelevanceDO()
                            .setProdId(prodDO.getProdId())
                            .setAreaId(prodServiceAreas.getId())
                    );
                }
            }
        }
        //预约设置设置
        /*if (Objects.equals(prodServiceVO.getReservationSwitch(), BaseEnum.YES_ONE.getKey())) {
            prodDO.setReservationSwitch(BaseEnum.YES_ONE.getKey());
            ProdReservationInfoVO prodReservationConfigDO = prodServiceVO.getProdReservationConfig();
            prodReservationConfigDO.setProdId(prodDO.getProdId());
            prodReservationConfigMapper.insert(prodReservationConfigDO);
        }*/
        //紧急响应设置
        if (Objects.equals(prodServiceVO.getEmergencySwitch(), BaseEnum.YES_ONE.getKey())) {
            prodDO.setEmergencySwitch(BaseEnum.YES_ONE.getKey());
            // 插入
            ProdEmergencyResponseDO prodEmergencyResponse = BeanUtils.toBean(prodServiceVO.prodEmergencyInfoVO, ProdEmergencyResponseDO.class);
            prodEmergencyResponse.setProdId(prodDO.getProdId());
            prodEmergencyResponseMapper.insert(prodEmergencyResponse);
            for (ProdEmergencyResponseIntervalsDO prodEmergencyResponseIntervals : prodServiceVO.prodEmergencyInfoVO.getProdEmergencyResponseIntervalsList()) {
                prodEmergencyResponseIntervals.setConfigId(prodEmergencyResponse.getId());
                prodEmergencyResponseIntervalsMapper.insert(prodEmergencyResponseIntervals);
            }
        }
        //接单上线设置
        if (Objects.equals(prodServiceVO.getOrderLimitSwitch(), BaseEnum.YES_ONE.getKey())) {
            prodDO.setOrderLimitSwitch(BaseEnum.YES_ONE.getKey());
            ProductOrderLimitDO productOrderLimit = prodServiceVO.productOrderLimitVO;
            productOrderLimit.setProdId(prodDO.getProdId());
            productOrderLimitMapper.insert(productOrderLimit);
        }
        //特殊时段设置
        if (Objects.equals(prodServiceVO.getAdditionalSwitch(), BaseEnum.YES_ONE.getKey())) {
            prodDO.setAdditionalSwitch(BaseEnum.YES_ONE.getKey());
            for (ProdAdditionalFeeDatesDO prodAdditionalFeeDates : prodServiceVO.getProdAdditionalFeeDatesList()) {
                prodAdditionalFeeDates.setProdId(prodServiceVO.getProdId());
                prodAdditionalFeeDatesMapper.insert(prodAdditionalFeeDates);
            }
        }
        //特殊日期设置
        if (Objects.equals(prodServiceVO.getAdditionalFeeSwitch(), BaseEnum.YES_ONE.getKey())) {
            prodDO.setAdditionalFeeSwitch(BaseEnum.YES_ONE.getKey());
            for (ProdAdditionalFeePeriodsDO prodAdditionalFeePeriods : prodServiceVO.getProdAdditionalFeePeriodsList()) {
                prodAdditionalFeePeriods.setProdId(prodServiceVO.getProdId());
                prodAdditionalFeePeriodsMapper.insert(prodAdditionalFeePeriods);
            }
        }
        //体重设置
       /* if (Objects.equals(prodServiceVO.getWeightSwitch(), BaseEnum.YES_ONE.getKey())) {
            prodDO.setWeightSwitch(BaseEnum.YES_ONE.getKey());
            ProdWeightRangePricesDO prodWeightRangePrices = prodServiceVO.prodWeightConfig;
            prodWeightRangePrices.setProdId(prodDO.getProdId());
            prodWeightRangePricesMapper.insert(prodWeightRangePrices);
        }*/
        prodMapper.updateById(prodDO);
    }

    @Override
    @Transactional
    public void uptateProdService(ProdServiceInfoVO prodServiceVO) {

        ProdDO prod = BeanUtils.toBean(prodServiceVO, ProdDO.class);
        //服务区域设置
        if (Objects.equals(prodServiceVO.getRegionSwitch(), BaseEnum.YES_ONE.getKey())) {
            ProdServiceOverAreaRulesDO overAreaRules = prodServiceOverAreaRulesMapper.selectOne(new LambdaQueryWrapper<ProdServiceOverAreaRulesDO>()
                    .eq(ProdServiceOverAreaRulesDO::getProdId, prodServiceVO.getProdId()));
            if (overAreaRules == null) {
                prod.setRegionSwitch(BaseEnum.YES_ONE.getKey());
                //保存区域设置信息
                ProdServiceOverAreaRulesDO prodServiceOverAreaRules = BeanUtils.toBean(prodServiceVO.getProdServiceAreasInfo(), ProdServiceOverAreaRulesDO.class);
                prodServiceOverAreaRules.setProdId(prod.getProdId());
                prodServiceOverAreaRulesMapper.insert(prodServiceOverAreaRules);
                //保存区域
                List<String> areaNameList = prodServiceVO.getProdServiceAreasInfo().getAreaNameList();
                for (String areaName : areaNameList) {
                    //查询区域信息
                    ProdServiceAreasDO prodServiceAreas = prodServiceAreasMapper.selectOne(new LambdaQueryWrapper<ProdServiceAreasDO>()
                            .eq(ProdServiceAreasDO::getAreaName, areaName)
                    );
                    if (prodServiceAreas != null) {
                        //插入关联表
                        prodServiceAreaRelevanceMapper.insert(new ProdServiceAreaRelevanceDO()
                                .setProdId(prod.getProdId())
                                .setAreaId(prodServiceAreas.getId())
                        );
                    }
                }
            } else {
                //保存区域设置信息
                ProdServiceOverAreaRulesDO prodServiceOverAreaRules = BeanUtils.toBean(prodServiceVO.getProdServiceAreasInfo(), ProdServiceOverAreaRulesDO.class);
                prodServiceOverAreaRules.setProdId(prodServiceVO.getProdId());
                prodServiceOverAreaRulesMapper.updateById(prodServiceOverAreaRules);
                //先批量删除区域关联表
                prodServiceAreaRelevanceMapper.deleteRelevance(prodServiceVO.getProdId());
                //保存区域
                List<String> areaNameList = prodServiceVO.getProdServiceAreasInfo().getAreaNameList();
                for (String areaName : areaNameList) {
                    //查询区域信息
                    ProdServiceAreasDO prodServiceAreas = prodServiceAreasMapper.selectOne(new LambdaQueryWrapper<ProdServiceAreasDO>()
                            .eq(ProdServiceAreasDO::getAreaName, areaName)
                    );
                    if (prodServiceAreas != null) {
                        //插入关联表
                        prodServiceAreaRelevanceMapper.insert(new ProdServiceAreaRelevanceDO()
                                .setProdId(prodServiceVO.getProdId())
                                .setAreaId(prodServiceAreas.getId())
                        );
                    }
                }
            }

        }
        //预约设置设置
        if (Objects.equals(prodServiceVO.getReservationSwitch(), BaseEnum.YES_ONE.getKey())) {
            ProdReservationConfigDO reservationConfig = prodReservationConfigMapper.selectOne(new LambdaQueryWrapper<ProdReservationConfigDO>()
                    .eq(ProdReservationConfigDO::getProdId, prodServiceVO.getProdId()));
            ProdReservationInfoReqVO prodReservationInfoVO = prodServiceVO.getProdReservationConfig();
            if (reservationConfig == null) {
                prod.setReservationSwitch(BaseEnum.YES_ONE.getKey());
                ProdReservationConfigDO prodReservationConfigDO = BeanUtils.toBean(prodReservationInfoVO, ProdReservationConfigDO.class);
                prodReservationConfigDO.setProdId(prod.getProdId());
                prodReservationConfigDO.setTimeSlot(prodReservationInfoVO.getTimeBook().getTimeSlot());
                prodReservationConfigDO.setReservationTimeSlots(prodReservationInfoVO.getTimeBook().getReservationTimeSlots());
                prodReservationConfigMapper.insert(prodReservationConfigDO);
                //先删除在新增
                prodAdditionalFeeDatesMapper.deleteAdditionalFeeDates(prod.getProdId(), 2);
                if (prodServiceVO.getProdReservationConfig().getProdReservationBlackList() != null) {
                    for (ProdAdditionalFeeBlackVO prodAdditionalFeeBlackVO : prodServiceVO.getProdReservationConfig().getProdReservationBlackList()) {
                        ProdAdditionalFeeDatesDO prodAdditionalFeeDatesDO = BeanUtils.toBean(prodAdditionalFeeBlackVO, ProdAdditionalFeeDatesDO.class);
                        prodAdditionalFeeDatesDO.setProdId(prod.getProdId());
                        prodAdditionalFeeDatesDO.setType(2);
                        prodAdditionalFeeDatesMapper.insert(prodAdditionalFeeDatesDO);
                    }
                }
            } else {
                ProdReservationConfigDO prodReservationConfigDO = BeanUtils.toBean(prodServiceVO.getProdReservationConfig(), ProdReservationConfigDO.class);
                prodReservationConfigDO.setTimeSlot(prodReservationInfoVO.getTimeBook().getTimeSlot());
                prodReservationConfigDO.setReservationTimeSlots(prodReservationInfoVO.getTimeBook().getReservationTimeSlots());
                prodReservationConfigMapper.updateById(prodReservationConfigDO);
                //先删除在新增
                prodAdditionalFeeDatesMapper.deleteAdditionalFeeDates(prod.getProdId(), 2);
                if (prodServiceVO.getProdReservationConfig().getProdReservationBlackList() != null) {
                    for (ProdAdditionalFeeBlackVO prodAdditionalFeeBlackVO : prodServiceVO.getProdReservationConfig().getProdReservationBlackList()) {
                        ProdAdditionalFeeDatesDO prodAdditionalFeeDatesDO = BeanUtils.toBean(prodAdditionalFeeBlackVO, ProdAdditionalFeeDatesDO.class);
                        prodAdditionalFeeDatesDO.setProdId(prod.getProdId());
                        prodAdditionalFeeDatesDO.setType(2);
                        prodAdditionalFeeDatesMapper.insert(prodAdditionalFeeDatesDO);
                    }
                }
            }
        }
        //紧急响应设置
        if (Objects.equals(prodServiceVO.getEmergencySwitch(), BaseEnum.YES_ONE.getKey())) {
            ProdEmergencyResponseDO rmergencyResponse = prodEmergencyResponseMapper.selectOne(new LambdaQueryWrapper<ProdEmergencyResponseDO>()
                    .eq(ProdEmergencyResponseDO::getProdId, prodServiceVO.getProdId()));
            if (rmergencyResponse == null) {
                prod.setEmergencySwitch(BaseEnum.YES_ONE.getKey());
                // 插入
                ProdEmergencyResponseDO prodEmergencyResponse = BeanUtils.toBean(prodServiceVO.getProdEmergencyInfoVO(), ProdEmergencyResponseDO.class);
                prodEmergencyResponse.setProdId(prod.getProdId());
                prodEmergencyResponseMapper.insert(prodEmergencyResponse);
                if (prodServiceVO.getProdEmergencyInfoVO().getProdEmergencyResponseIntervalsList() != null) {
                    for (ProdEmergencyResponseIntervalsDO prodEmergencyResponseIntervals : prodServiceVO.getProdEmergencyInfoVO().getProdEmergencyResponseIntervalsList()) {
                        prodEmergencyResponseIntervals.setId(null);
                        prodEmergencyResponseIntervals.setConfigId(prodEmergencyResponse.getId());
                        prodEmergencyResponseIntervals.setProdId(prod.getProdId());
                        prodEmergencyResponseIntervalsMapper.insert(prodEmergencyResponseIntervals);
                    }
                }
                if (prodServiceVO.getProdEmergencyInfoVO().getProdEmergencyResponseBlackList() != null) {
                    for (ProdAdditionalFeeBlackVO prodAdditionalFeeBlackVO : prodServiceVO.getProdEmergencyInfoVO().getProdEmergencyResponseBlackList()) {
                        ProdAdditionalFeeDatesDO prodAdditionalFeeDatesDO = BeanUtils.toBean(prodAdditionalFeeBlackVO, ProdAdditionalFeeDatesDO.class);
                        prodAdditionalFeeDatesDO.setId(null);
                        prodAdditionalFeeDatesDO.setProdId(prod.getProdId());
                        prodAdditionalFeeDatesDO.setType(3);
                        prodAdditionalFeeDatesMapper.insert(prodAdditionalFeeDatesDO);
                    }
                }
            } else {
                ProdEmergencyResponseDO prodEmergencyResponse = BeanUtils.toBean(prodServiceVO.getProdEmergencyInfoVO(), ProdEmergencyResponseDO.class);
                if (prodEmergencyResponse != null) {
                    prodEmergencyResponseMapper.updateById(prodEmergencyResponse);
                    prodEmergencyResponseIntervalsMapper.deleteIntervals(prodEmergencyResponse.getId());
                    if (prodServiceVO.getProdEmergencyInfoVO().getProdEmergencyResponseIntervalsList() != null) {
                        for (ProdEmergencyResponseIntervalsDO prodEmergencyResponseIntervals : prodServiceVO.getProdEmergencyInfoVO().getProdEmergencyResponseIntervalsList()) {
                            prodEmergencyResponseIntervals.setId(null);
                            prodEmergencyResponseIntervals.setConfigId(prodEmergencyResponse.getId());
                            prodEmergencyResponseIntervals.setProdId(prod.getProdId());
                            prodEmergencyResponseIntervalsMapper.insert(prodEmergencyResponseIntervals);
                        }
                    }
                    //先删除在新增
                    prodAdditionalFeeDatesMapper.deleteAdditionalFeeDates(prod.getProdId(), 3);
                    if (prodServiceVO.getProdReservationConfig().getProdReservationBlackList() != null) {
                        for (ProdAdditionalFeeBlackVO prodAdditionalFeeBlackVO : prodServiceVO.getProdReservationConfig().getProdReservationBlackList()) {
                            ProdAdditionalFeeDatesDO prodAdditionalFeeDatesDO = BeanUtils.toBean(prodAdditionalFeeBlackVO, ProdAdditionalFeeDatesDO.class);
                            prodAdditionalFeeDatesDO.setId(null);
                            prodAdditionalFeeDatesDO.setProdId(prod.getProdId());
                            prodAdditionalFeeDatesDO.setType(3);
                            prodAdditionalFeeDatesMapper.insert(prodAdditionalFeeDatesDO);
                        }
                    }
                }
            }
        }
        //接单上线设置
        if (Objects.equals(prodServiceVO.getOrderLimitSwitch(), BaseEnum.YES_ONE.getKey())) {
            ProductOrderLimitDO orderLimit = productOrderLimitMapper.selectOne(new LambdaQueryWrapper<ProductOrderLimitDO>()
                    .eq(ProductOrderLimitDO::getProdId, prodServiceVO.getProdId()));
            if (orderLimit == null) {
                prod.setOrderLimitSwitch(BaseEnum.YES_ONE.getKey());
                ProductOrderLimitDO productOrderLimit = prodServiceVO.getProductOrderLimitVO();
                productOrderLimit.setProdId(prod.getProdId());
                productOrderLimitMapper.insert(productOrderLimit);
            } else {
                productOrderLimitMapper.updateById(prodServiceVO.getProductOrderLimitVO());
            }
        }
        //特殊时段设置
        if (Objects.equals(prodServiceVO.getAdditionalSwitch(), BaseEnum.YES_ONE.getKey())) {
            if (prodServiceVO.getProdAdditionalFeeDatesList() != null)
                for (ProdAdditionalFeeDatesDO prodAdditionalFeeDates : prodServiceVO.getProdAdditionalFeeDatesList()) {
                    if (prodAdditionalFeeDates.getId() == null) {
                        prodAdditionalFeeDates.setProdId(prodServiceVO.getProdId());
                        prodAdditionalFeeDates.setType(1);
                        prodAdditionalFeeDatesMapper.insert(prodAdditionalFeeDates);
                    } else {
                        prodAdditionalFeeDatesMapper.updateById(prodAdditionalFeeDates);
                    }
                }
        }
        //特殊日期设置
        if (Objects.equals(prodServiceVO.getAdditionalFeeSwitch(), BaseEnum.YES_ONE.getKey())) {
            for (ProdAdditionalFeePeriodsDO prodAdditionalFeePeriods : prodServiceVO.getProdAdditionalFeePeriodsList()) {
                if (prodAdditionalFeePeriods.getId() == null) {
                    prodAdditionalFeePeriods.setProdId(prodServiceVO.getProdId());
                    prodAdditionalFeePeriodsMapper.insert(prodAdditionalFeePeriods);
                } else {
                    prodAdditionalFeePeriodsMapper.updateById(prodAdditionalFeePeriods);
                }
            }
        }
        //体重设置
        if (Objects.equals(prodServiceVO.getWeightSwitch(), BaseEnum.YES_ONE.getKey())) {
            prod.setWeightSwitch(prodServiceVO.getWeightSwitch());
            ProdExtendDO prodExtendDO = prodExtendMapper.selectOne(ProdExtendDO::getProdId, prod.getProdId());
            if (prodExtendDO != null) {
                prodExtendDO.setIsWeightCharge(prodServiceVO.getProdWeightConfig().getIsWeightCharge());
                prodExtendMapper.updateById(prodExtendDO);
            } else {
                prodExtendDO = new ProdExtendDO();
                prodExtendDO.setProdId(prod.getProdId());
                prodExtendDO.setIsWeightCharge(prodServiceVO.getProdWeightConfig().getIsWeightCharge());
                prodExtendMapper.insert(prodExtendDO);
            }
            prodWeightRangePricesMapper.deleteWeightRangePrices(prod.getProdId());
            if (prodServiceVO.getProdWeightConfig().getProdWeightConfigList() != null) {
                for (ProdWeightRangePricesDO prodWeightRangePricesDO : prodServiceVO.getProdWeightConfig().getProdWeightConfigList()) {
                    prodWeightRangePricesDO.setProdId(prod.getProdId());
                    prodWeightRangePricesMapper.insert(prodWeightRangePricesDO);
                }
            }
        }
        prodMapper.updateById(prod);
    }

    @Override
    public ProdServiceVO getProdService(Long prodId) {
        ProdDO prodDO = prodMapper.selectById(prodId);
        ProdServiceVO prodServiceVO = prodMapper.selectProdService(prodDO.getProdId());

        if (prodServiceVO.getProdEmergencyInfoVO().getId()== null) {
            prodServiceVO.setProdEmergencyInfoVO(null);
        }else {
            if (prodServiceVO.getProdEmergencyInfoVO().getId()!= null&&prodServiceVO.getProdEmergencyInfoVO().getProdEmergencyResponseIntervalsList().size()== 0) {
                ProdEmergencyInfoVO prodEmergencyInfoVO = prodServiceVO.getProdEmergencyInfoVO();
                prodEmergencyInfoVO.setProdEmergencyResponseIntervalsList(new ArrayList<>());
            }
            if (prodServiceVO.getProdEmergencyInfoVO().getId()!= null&&prodServiceVO.getProdEmergencyInfoVO().getProdEmergencyResponseBlackList().get(0).isEmpty()) {
                ProdEmergencyInfoVO prodEmergencyInfoVO = prodServiceVO.getProdEmergencyInfoVO();
                prodEmergencyInfoVO.setProdEmergencyResponseBlackList(new ArrayList<>());
            }
        }
        if (prodServiceVO.getProdAdditionalFeeDatesList().get(0).getId()==null) {
            prodServiceVO.setProdAdditionalFeeDatesList(new ArrayList<>());
        }
        if (prodServiceVO.getProdAdditionalFeePeriodsList().get(0).getId()==null) {
            prodServiceVO.setProdAdditionalFeePeriodsList(new ArrayList<>());
        }
        return prodServiceVO;
      /*  if (prodDO != null && prodDO.getRegionSwitch() == BaseEnum.YES_ONE.getKey()) {
            ProdServiceVO prodServiceVO = prodMapper.selectProdService(prodDO.getProdId());
            if (Objects.equals(prodDO.getReservationSwitch(), BaseEnum.YES_ONE.getKey())&&prodServiceVO.getProdReservationConfig().getProdReservationBlackList().get(0).isEmpty()) {
                ProdReservationInfoVO prodReservationInfoVO = prodServiceVO.getProdReservationConfig();
                prodReservationInfoVO.setProdReservationBlackList(new ArrayList<>());
            }
            if (Objects.equals(prodDO.getEmergencySwitch(), BaseEnum.YES_ONE.getKey())&&prodServiceVO.getProdEmergencyInfoVO().getProdEmergencyResponseIntervalsList().get(0).isEmpty()) {
                ProdEmergencyInfoVO prodEmergencyInfoVO = prodServiceVO.getProdEmergencyInfoVO();
                prodEmergencyInfoVO.setProdEmergencyResponseIntervalsList(new ArrayList<>());
            }
            if (Objects.equals(prodDO.getAdditionalSwitch(), BaseEnum.YES_ONE.getKey())&&prodServiceVO.getProdAdditionalFeeDatesList().get(0).isEmpty()) {
                prodServiceVO.setProdAdditionalFeeDatesList(new ArrayList<>());
            }
            if (Objects.equals(prodDO.getAdditionalFeeSwitch(), BaseEnum.YES_ONE.getKey())&&prodServiceVO.getProdAdditionalFeePeriodsList().get(0).isEmpty()) {
                prodServiceVO.setProdAdditionalFeePeriodsList(new ArrayList<>());
            }
            return prodServiceVO;
        } else {
            ProdServiceVO prodServiceVO = prodMapper.selectProdServiceInfo(prodDO.getProdId());
            if (Objects.equals(prodDO.getReservationSwitch(), BaseEnum.YES_ONE.getKey())&&prodServiceVO.getProdReservationConfig()!=null&&prodServiceVO.getProdReservationConfig().getProdReservationBlackList().get(0).isEmpty()) {
                ProdReservationInfoVO prodReservationInfoVO = prodServiceVO.getProdReservationConfig();
                prodReservationInfoVO.setProdReservationBlackList(new ArrayList<>());
            }
            if (Objects.equals(prodDO.getEmergencySwitch(), BaseEnum.YES_ONE.getKey())&&prodServiceVO.getProdEmergencyInfoVO()!=null&&prodServiceVO.getProdEmergencyInfoVO().getProdEmergencyResponseIntervalsList().get(0).isEmpty()) {
                ProdEmergencyInfoVO prodEmergencyInfoVO = prodServiceVO.getProdEmergencyInfoVO();
                prodEmergencyInfoVO.setProdEmergencyResponseIntervalsList(new ArrayList<>());
            }
            if (Objects.equals(prodDO.getEmergencySwitch(), BaseEnum.YES_ONE.getKey())&&prodServiceVO.getProdEmergencyInfoVO()!=null&&prodServiceVO.getProdEmergencyInfoVO().getProdEmergencyResponseBlackList().get(0).isEmpty()) {
                ProdEmergencyInfoVO prodEmergencyInfoVO = prodServiceVO.getProdEmergencyInfoVO();
                prodEmergencyInfoVO.setProdEmergencyResponseBlackList(new ArrayList<>());
            }
            if (Objects.equals(prodDO.getAdditionalSwitch(), BaseEnum.YES_ONE.getKey())&&prodServiceVO.getProdAdditionalFeeDatesList()!=null&&prodServiceVO.getProdAdditionalFeeDatesList().get(0).isEmpty()) {
                prodServiceVO.setProdAdditionalFeeDatesList(new ArrayList<>());
            }
            if (Objects.equals(prodDO.getAdditionalFeeSwitch(), BaseEnum.YES_ONE.getKey())&&prodServiceVO.getProdAdditionalFeePeriodsList()!=null&&prodServiceVO.getProdAdditionalFeePeriodsList().get(0).isEmpty()) {
                prodServiceVO.setProdAdditionalFeePeriodsList(new ArrayList<>());
            }
            return prodServiceVO;
        }*/


    }

    @Override
    public void updateProd(ProdSaveReqVO updateReqVO) {
        // 校验存在
        validateProdExists(updateReqVO.getProdId());
        // 更新
        ProdDO updateObj = BeanUtils.toBean(updateReqVO, ProdDO.class);
        prodMapper.updateById(updateObj);
    }

    @Override
    public void deleteProd(Long id) {
        // 校验存在
        validateProdExists(id);
        ProdDO prod = new ProdDO();
        prod.setProdId(id);
        prod.setDeleted(BaseEnum.YES_ONE.getKey());
        prod.setDeleteTime(new Date());
        // 删除
        prodMapper.deleteById(prod);
    }

    private void validateProdExists(Long id) {
        if (prodMapper.selectById(id) == null) {
            //throw exception(100, "商品不存在");
        }
    }

    @Override
    public ProdDO getProd(Long id) {
        return prodMapper.selectById(id);
    }

    @Override
    public PageResult<ProdListVO> getProdPage(ProdPageReqVO pageReqVO) {
        IPage<ProdListVO> prodPageList = prodMapper.getProdPageList(MyBatisUtils.buildPage(pageReqVO),
                pageReqVO.getCreateTime(), pageReqVO.getProdName(), pageReqVO.getShopId(), pageReqVO.getStatus(), pageReqVO.getCategoryId());
        return new PageResult<>(prodPageList.getRecords(), prodPageList.getTotal());
    }

    @Override
    public PageResult<ProdRestoreListVO> getProdRecycleBinPageList(ProdRecycleBinVO prodRecycleBinVO) {
        IPage<ProdRestoreListVO> prodPageList = prodMapper.getProdRecycleBinPageList(MyBatisUtils.buildPage(prodRecycleBinVO), prodRecycleBinVO.getDeleteTime(), prodRecycleBinVO.getProdName());
        for (ProdRestoreListVO prodPage : prodPageList.getRecords()) {
            if (prodPage.getDeleteTime() != null) {
                prodPage.setRemainingDays(DateUtils.getRemainingDays(prodPage.getDeleteTime()));
            }
        }
        return new PageResult<>(prodPageList.getRecords(), prodPageList.getTotal());
    }

    @Override
    public void restoreProdList(List<Long> ids) {
        prodMapper.batchRestoreProd(ids);
    }

}