package com.cf.imes.module.system.service.funds.productpromotion;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.Assert.AssertUtils;
import com.cf.imes.framework.common.util.date.LocalDateTimeUtils;
import com.cf.imes.framework.common.util.i18n.core.util.I18nUtils;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.module.system.controller.admin.funds.productpromotion.vo.ProductPromotionPageReqVO;
import com.cf.imes.module.system.controller.admin.funds.productpromotion.vo.ProductPromotionSaveReqVO;
import com.cf.imes.module.system.dal.dataobject.funds.productpromotion.ProductPromotionDO;
import com.cf.imes.module.system.dal.dataobject.funds.products.ProductsDO;
import com.cf.imes.module.system.dal.mysql.funds.productpromotion.ProductPromotionMapper;
import com.cf.imes.module.system.enums.pay.DurationUnitEnum;
import com.cf.imes.module.system.service.funds.products.ProductsDetailService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import jakarta.annotation.Resource;

import java.time.LocalDateTime;
import java.util.List;

import static com.cf.imes.module.system.enums.ErrorCodeConstants.PRODUCT_PROMOTION_ACTIVE_NAME_IS_EXIST;
import static com.cf.imes.module.system.enums.ErrorCodeConstants.PRODUCT_PROMOTION_NO_EXIST;
import static com.cf.imes.module.system.enums.ErrorCodeConstants.PRODUCT_PROMOTION_PURCHASEDURATION_IS_EXIST;

/**
 * 优惠活动表 product_promotion Service 实现类
 *
 * @author 晨丰科技
 */
@Service
@Validated
public class ProductPromotionServiceImpl implements ProductPromotionService {

    @Resource
    private ProductPromotionMapper productPromotionMapper;

    @Resource
    private ProductsDetailService productsDetailService;

    @Resource
    private I18nUtils i18nUtils;

    @Override
    public Long create(ProductPromotionSaveReqVO createReqVO) {
        Long productId = createReqVO.getProductId();
        ProductsDO productsDO = productsDetailService.validateProductExist(productId);

        // 校验活动名称唯一
        ProductPromotionDO productPromotionDO = productPromotionMapper.selectOne(new LambdaQueryWrapperX<ProductPromotionDO>()
                .eq(ProductPromotionDO::getDeleted, false)
                .eq(ProductPromotionDO::getActivityName, createReqVO.getActivityName()));

        AssertUtils.empty(productPromotionDO, PRODUCT_PROMOTION_ACTIVE_NAME_IS_EXIST);

        // 校验对应产品购买时长唯一
        productPromotionDO = productPromotionMapper.selectOne(new LambdaQueryWrapperX<ProductPromotionDO>()
                .eq(ProductPromotionDO::getDeleted, false)
                .eq(ProductPromotionDO::getPurchaseDuration, createReqVO.getPurchaseDuration())
                .eq(ProductPromotionDO::getPurchaseDurationUnit, createReqVO.getPurchaseDurationUnit())
                .eq(ProductPromotionDO::getProductId, productId));

        AssertUtils.empty(productPromotionDO, PRODUCT_PROMOTION_PURCHASEDURATION_IS_EXIST, createReqVO.getPurchaseDuration(), i18nUtils.getMessage(DurationUnitEnum.getDesc(createReqVO.getPurchaseDurationUnit())));

        ProductPromotionDO promotionDO = BeanUtils.toBean(createReqVO, ProductPromotionDO.class);
        promotionDO.setProductId(productsDO.getId());
        promotionDO.setProductName(productsDO.getProductName());
        productPromotionMapper.insert(promotionDO);
        return promotionDO.getId();
    }

    @Override
    public void update(ProductPromotionSaveReqVO updateReqVO) {
        Long productId = updateReqVO.getProductId();
        ProductsDO productsDO = productsDetailService.validateProductExist(productId);

        validateRechargeActiveExists(updateReqVO.getId());

        // 校验活动名称唯一
        ProductPromotionDO productPromotionDO = productPromotionMapper.selectOne(new LambdaQueryWrapperX<ProductPromotionDO>()
                .eq(ProductPromotionDO::getDeleted, false)
                .eq(ProductPromotionDO::getActivityName,updateReqVO.getActivityName())
                .ne(ProductPromotionDO::getId,updateReqVO.getId()));

        AssertUtils.empty(productPromotionDO, PRODUCT_PROMOTION_ACTIVE_NAME_IS_EXIST);

        // 校验购买时长唯一
        productPromotionDO = productPromotionMapper.selectOne(new LambdaQueryWrapperX<ProductPromotionDO>()
                .eq(ProductPromotionDO::getDeleted, false)
                .eq(ProductPromotionDO::getPurchaseDuration, updateReqVO.getPurchaseDuration())
                .eq(ProductPromotionDO::getPurchaseDurationUnit, updateReqVO.getPurchaseDurationUnit())
                .ne(ProductPromotionDO::getId, updateReqVO.getId())
                .eq(ProductPromotionDO::getProductId, productId));

        AssertUtils.empty(productPromotionDO, PRODUCT_PROMOTION_PURCHASEDURATION_IS_EXIST, updateReqVO.getPurchaseDuration(), i18nUtils.getMessage(DurationUnitEnum.getDesc(updateReqVO.getPurchaseDurationUnit())));

        ProductPromotionDO promotionDO = BeanUtils.toBean(updateReqVO, ProductPromotionDO.class);
        promotionDO.setProductId(productsDO.getId());
        promotionDO.setProductName(productsDO.getProductName());
        productPromotionMapper.updateById(promotionDO);

    }


    @Override
    public void delete(Long id) {
        validateRechargeActiveExists(id);
        productPromotionMapper.deleteById(id);
    }


    @Override
    public ProductPromotionDO get(Long id) {
        return validateRechargeActiveExists(id);
    }

    @Override
    public PageResult<ProductPromotionDO> getPage(ProductPromotionPageReqVO pageReqVO) {
        // 生成时间0点和止时间12点的区间
        pageReqVO.setStartTime(LocalDateTimeUtils.generateTimeSection(pageReqVO.getStartTime()));
        pageReqVO.setEndTime(LocalDateTimeUtils.generateTimeSection(pageReqVO.getEndTime()));

        return productPromotionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProductPromotionDO> getAvailableProductPromotionList(Long productId) {
        LocalDateTime now = LocalDateTime.now();
        // 筛选掉投放时间还没到和过期的
        return productPromotionMapper.selectList(new LambdaQueryWrapper<ProductPromotionDO>()
                .eq(ProductPromotionDO::getDeleted, false)
                .eq(ProductPromotionDO::getProductId, productId)
                .le(ProductPromotionDO::getStartTime, now)
                .ge(ProductPromotionDO::getEndTime, now)
        );
    }

    // 校验是否存在
    private ProductPromotionDO validateRechargeActiveExists(Long id) {
        if (id == null) {
            throw new ServiceException(PRODUCT_PROMOTION_NO_EXIST);
        }
        ProductPromotionDO giftMoneyDetailsDO = productPromotionMapper.selectOne(new LambdaQueryWrapperX<ProductPromotionDO>()
                .eq(ProductPromotionDO::getId, id)
                .eq(ProductPromotionDO::getDeleted, false));
        if (giftMoneyDetailsDO == null) {
            throw new ServiceException(PRODUCT_PROMOTION_NO_EXIST);
        }

        return giftMoneyDetailsDO;
    }


}
