package com.czg.service.market.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.czg.constant.TableValueConstant;
import com.czg.exception.CzgException;
import com.czg.market.dto.MkConsumeDiscountDTO;
import com.czg.market.entity.MkConsumeDiscountRandom;
import com.czg.market.vo.MkConsumeDiscountVO;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.market.entity.MkConsumeDiscount;
import com.czg.market.service.MkConsumeDiscountService;
import com.czg.service.market.mapper.MkConsumeDiscountMapper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 新客立减 服务层实现。
 *
 * @author zs
 * @since 2025-09-16
 */
@Service
@CacheConfig(cacheNames = "consumeDiscount")
public class MkConsumeDiscountServiceImpl extends ServiceImpl<MkConsumeDiscountMapper, MkConsumeDiscount> implements MkConsumeDiscountService {
    @Override
    @Cacheable(key = "#shopId")
    public MkConsumeDiscountVO detail(Long shopId) {
        MkConsumeDiscount consumeDiscount = getOne(new QueryWrapper().eq(MkConsumeDiscount::getShopId, shopId));
        if (consumeDiscount == null) {
            consumeDiscount = new MkConsumeDiscount();
            consumeDiscount.setShopId(shopId);
            save(consumeDiscount);
            consumeDiscount = getOne(new QueryWrapper().eq(MkConsumeDiscount::getShopId, shopId));

        }
        MkConsumeDiscountVO consumeDiscountVO = BeanUtil.copyProperties(consumeDiscount, MkConsumeDiscountVO.class, "randomDiscountList", "useTypeList");
        if (StrUtil.isNotBlank(consumeDiscount.getRandomDiscountList())) {
            consumeDiscountVO.setRandomDiscountList(JSONArray.parseArray(consumeDiscount.getRandomDiscountList()).toList(MkConsumeDiscountRandom.class));
        }

        if (StrUtil.isNotBlank(consumeDiscount.getUseTypeList())) {
            consumeDiscountVO.setUseTypeList(JSONArray.parseArray(consumeDiscount.getUseTypeList()).toList(String.class));
        }
        return consumeDiscountVO;
    }

    @Override
    @CacheEvict(key = "#shopId")
    public Boolean edit(Long shopId, MkConsumeDiscountDTO consumeDiscountDTO) {
        if (consumeDiscountDTO.getRandomDiscountList() == null && consumeDiscountDTO.getDiscountAmount() == null) {
            throw new CzgException("优惠金额和随机优惠金额不能同时为空");
        }
        MkConsumeDiscount consumeDiscount = getOne(new QueryWrapper().eq(MkConsumeDiscount::getShopId, shopId));
        BeanUtil.copyProperties(consumeDiscountDTO, consumeDiscount);
        if (TableValueConstant.ConsumeDiscount.DiscountType.RANDOM.getCode().equals(consumeDiscountDTO.getDiscountType())) {
            if (consumeDiscountDTO.getRandomDiscountList() == null || consumeDiscountDTO.getRandomDiscountList().isEmpty()) {
                throw new CzgException("随机优惠金额不能为空");
            }

            if (consumeDiscountDTO.getRandomDiscountList().stream().map(MkConsumeDiscountRandom::getProbability).reduce(BigDecimal.ZERO, BigDecimal::add).compareTo(new BigDecimal("100")) != 0) {
                throw new CzgException("随机概率综合必须为100%");
            }
            consumeDiscount.setRandomDiscountList(JSONArray.toJSONString(consumeDiscountDTO.getRandomDiscountList()));
        } else {
            if (consumeDiscountDTO.getDiscountAmount() == null) {
                throw new CzgException("减免金额不能为空");
            }

            if (consumeDiscountDTO.getDiscountAmount().compareTo(BigDecimal.ZERO) <= 0) {
                throw new CzgException("减免金额不能等于0");
            }
        }



        if (consumeDiscountDTO.getUseTypeList() != null) {
            consumeDiscount.setUseTypeList(JSONArray.toJSONString(consumeDiscountDTO.getUseTypeList()));
        }

        return updateById(consumeDiscount);
    }
}
