package com.qs.mp.common.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qs.mp.admin.domain.param.ShippingTemplateParam;
import com.qs.mp.admin.domain.param.ShippingTemplateRuleAreaParam;
import com.qs.mp.admin.domain.param.ShippingTemplateRuleParam;
import com.qs.mp.admin.domain.vo.ShippingTemplateCalculateVO;
import com.qs.mp.admin.domain.vo.ShippingTemplateRuleAreaVO;
import com.qs.mp.admin.domain.vo.ShippingTemplateRuleVO;
import com.qs.mp.admin.domain.vo.ShippingTemplateVO;
import com.qs.mp.channel.domain.vo.ChannelOrderSettleVO;
import com.qs.mp.common.domain.ShippingTemplate;
import com.qs.mp.common.domain.ShippingTemplateRule;
import com.qs.mp.common.domain.ShippingTemplateRuleArea;
import com.qs.mp.common.enums.ShippingTemplateRuleTypeEnum;
import com.qs.mp.common.exception.ServiceException;
import com.qs.mp.common.mapper.ShippingTemplateMapper;
import com.qs.mp.common.service.IShippingTemplateRuleAreaService;
import com.qs.mp.common.service.IShippingTemplateRuleService;
import com.qs.mp.common.service.IShippingTemplateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qs.mp.user.domain.UserAddr;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 运费模板表 服务实现类
 * </p>
 *
 * @author quanshu
 * @since 2022-04-29
 */
@Service
public class ShippingTemplateServiceImpl extends ServiceImpl<ShippingTemplateMapper, ShippingTemplate> implements IShippingTemplateService {

    @Autowired
    private IShippingTemplateRuleService shippingTemplateRuleService;;

    @Autowired
    private IShippingTemplateRuleAreaService shippingTemplateRuleAreaService;

    @Override
    public int calculateFreightAmt(Integer num, ShippingTemplateCalculateVO shippingTemplateCalculateVO) {
        int freightAmt = shippingTemplateCalculateVO.getDefaultPrice();
        int createNumber = shippingTemplateCalculateVO.getCreateNumber();
        if (num - shippingTemplateCalculateVO.getDefaultNumber() > 0 && createNumber > 0) {
            // 运费:首件价格 + ((商品数量 - 首件数量) / 续件数量)向上取整 * 续件价格
            freightAmt += ((int) Math.ceil(NumberUtil.div(num - shippingTemplateCalculateVO.getDefaultNumber(), createNumber))) * shippingTemplateCalculateVO.getCreatePrice();
        }
        return freightAmt;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteShippingTemplate(Long id) {
        ShippingTemplate shippingTemplate = this.getById(id);
        if (Objects.isNull(shippingTemplate)) {
            throw new ServiceException("运费模板不存在");
        }
        if (shippingTemplate.getIsDefault() == 1) {
            throw new ServiceException("默认模板不能删除");
        }

        // 清除原来运费模板规则地区
        List<ShippingTemplateRule> shippingTemplateRules = shippingTemplateRuleService.list(new LambdaQueryWrapper<ShippingTemplateRule>().eq(ShippingTemplateRule::getShippingTemplateId, id));
        if (!CollectionUtils.isEmpty(shippingTemplateRules)) {
            for (ShippingTemplateRule shippingTemplateRule : shippingTemplateRules) {
                shippingTemplateRuleAreaService.remove(new LambdaQueryWrapper<ShippingTemplateRuleArea>().eq(ShippingTemplateRuleArea::getShippingTemplateRuleId, shippingTemplateRule.getId()));
            }
            // 清除原来的运费模板规则
            shippingTemplateRuleService.remove(new LambdaQueryWrapper<ShippingTemplateRule>().eq(ShippingTemplateRule::getShippingTemplateId, id));
        }

        // 删除运费模板
        this.removeById(id);
        return true;
    }

    @Override
    public ShippingTemplateVO detail(Long id) {
        ShippingTemplateVO shippingTemplateVO = new ShippingTemplateVO();
        ShippingTemplate shippingTemplate = this.getById(id);
        if (Objects.isNull(shippingTemplate)) {
            throw new ServiceException("运费模板不存在");
        }
        BeanUtils.copyProperties(shippingTemplate, shippingTemplateVO);

        // 获取封装运费模板规则
        List<ShippingTemplateRuleVO> shippingTemplateRuleVOS = new ArrayList<>();
        List<ShippingTemplateRule> shippingTemplateRules = shippingTemplateRuleService.list(new LambdaQueryWrapper<ShippingTemplateRule>().eq(ShippingTemplateRule::getShippingTemplateId, id));
        for (ShippingTemplateRule shippingTemplateRule : shippingTemplateRules) {

            ShippingTemplateRuleVO shippingTemplateRuleVO = new ShippingTemplateRuleVO();
            BeanUtils.copyProperties(shippingTemplateRule, shippingTemplateRuleVO);

            List<ShippingTemplateRuleAreaVO> shippingTemplateRuleAreaVOS = new ArrayList<>();

            // 获取封装运费模板规则区域
            List<ShippingTemplateRuleArea> shippingTemplateRuleAreaList = shippingTemplateRuleAreaService.list(new LambdaQueryWrapper<ShippingTemplateRuleArea>().eq(ShippingTemplateRuleArea::getShippingTemplateRuleId, shippingTemplateRule.getId()));
            for (ShippingTemplateRuleArea shippingTemplateRuleArea : shippingTemplateRuleAreaList) {
                ShippingTemplateRuleAreaVO shippingTemplateRuleAreaVO = new ShippingTemplateRuleAreaVO();
                BeanUtils.copyProperties(shippingTemplateRuleArea, shippingTemplateRuleAreaVO);
                shippingTemplateRuleAreaVOS.add(shippingTemplateRuleAreaVO);
            }

            shippingTemplateRuleVO.setShippingTemplateRuleAreaList(shippingTemplateRuleAreaVOS);
            shippingTemplateRuleVOS.add(shippingTemplateRuleVO);
        }
        shippingTemplateVO.setShippingTemplateRuleList(shippingTemplateRuleVOS);
        return shippingTemplateVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean create(ShippingTemplateParam shippingTemplateParam) {

        // 校验运费模板参数
        this.checkCreateShippingTemplateParam(shippingTemplateParam);

        // 新增运费模板
        ShippingTemplate shippingTemplate = new ShippingTemplate();
        BeanUtils.copyProperties(shippingTemplateParam, shippingTemplate);
        this.save(shippingTemplate);

        // 新增运费模板规则
        List<ShippingTemplateRuleParam> shippingTemplateRuleList = shippingTemplateParam.getShippingTemplateRuleList();
        if (!CollectionUtils.isEmpty(shippingTemplateRuleList)) {
            for (ShippingTemplateRuleParam shippingTemplateRuleParam : shippingTemplateRuleList) {
                ShippingTemplateRule shippingTemplateRule = new ShippingTemplateRule();
                BeanUtils.copyProperties(shippingTemplateRuleParam, shippingTemplateRule);
                shippingTemplateRule.setShippingTemplateId(shippingTemplate.getId());
                shippingTemplateRuleService.save(shippingTemplateRule);

                // 新增运费模板规则地区
                List<ShippingTemplateRuleAreaParam> shippingTemplateRuleAreaList = shippingTemplateRuleParam.getShippingTemplateRuleAreaList();
                List<ShippingTemplateRuleArea> params = new ArrayList<>();
                for (ShippingTemplateRuleAreaParam shippingTemplateRuleAreaParam : shippingTemplateRuleAreaList) {
                    ShippingTemplateRuleArea param = new ShippingTemplateRuleArea();
                    BeanUtils.copyProperties(shippingTemplateRuleAreaParam, param);
                    param.setShippingTemplateRuleId(shippingTemplateRule.getId());
                    params.add(param);
                }
                shippingTemplateRuleAreaService.saveBatch(params);
            }
        }

        return true;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateShippingTemplate(ShippingTemplateParam shippingTemplateParam) {

        Long shippingTemplateId = shippingTemplateParam.getId();
        if (Objects.isNull(shippingTemplateId)) {
            throw new ServiceException("运费模板ID不能为空");
        }

        // 校验运费模板参数
        this.checkCreateShippingTemplateParam(shippingTemplateParam);

        // 清除原来运费模板规则地区
        List<ShippingTemplateRule> shippingTemplateRules = shippingTemplateRuleService.list(new LambdaQueryWrapper<ShippingTemplateRule>().eq(ShippingTemplateRule::getShippingTemplateId, shippingTemplateId));
        if (!CollectionUtils.isEmpty(shippingTemplateRules)) {
            for (ShippingTemplateRule shippingTemplateRule : shippingTemplateRules) {
                shippingTemplateRuleAreaService.remove(new LambdaQueryWrapper<ShippingTemplateRuleArea>().eq(ShippingTemplateRuleArea::getShippingTemplateRuleId, shippingTemplateRule.getId()));
            }
            // 清除原来的运费模板规则
            shippingTemplateRuleService.remove(new LambdaQueryWrapper<ShippingTemplateRule>().eq(ShippingTemplateRule::getShippingTemplateId, shippingTemplateId));
        }

        // 更新运费模板
        ShippingTemplate shippingTemplate = new ShippingTemplate();
        BeanUtils.copyProperties(shippingTemplateParam, shippingTemplate);
        this.updateById(shippingTemplate);

        // 新增运费模板规则
        List<ShippingTemplateRuleParam> shippingTemplateRuleList = shippingTemplateParam.getShippingTemplateRuleList();
        if (!CollectionUtils.isEmpty(shippingTemplateRuleList)) {
            for (ShippingTemplateRuleParam shippingTemplateRuleParam : shippingTemplateRuleList) {
                ShippingTemplateRule shippingTemplateRule = new ShippingTemplateRule();
                BeanUtils.copyProperties(shippingTemplateRuleParam, shippingTemplateRule);
                shippingTemplateRule.setShippingTemplateId(shippingTemplateId);
                shippingTemplateRuleService.save(shippingTemplateRule);

                // 新增运费模板规则地区
                List<ShippingTemplateRuleAreaParam> shippingTemplateRuleAreaList = shippingTemplateRuleParam.getShippingTemplateRuleAreaList();
                List<ShippingTemplateRuleArea> params = new ArrayList<>();
                for (ShippingTemplateRuleAreaParam shippingTemplateRuleAreaParam : shippingTemplateRuleAreaList) {
                    ShippingTemplateRuleArea param = new ShippingTemplateRuleArea();
                    BeanUtils.copyProperties(shippingTemplateRuleAreaParam, param);
                    param.setShippingTemplateRuleId(shippingTemplateRule.getId());
                    params.add(param);
                }
                shippingTemplateRuleAreaService.saveBatch(params);
            }
        }

        return true;
    }

    /**
     * 校验运费模板参数
     * @param shippingTemplateParam 运费模板入参类
     */
    private void checkCreateShippingTemplateParam(ShippingTemplateParam shippingTemplateParam) {

        if (StringUtils.isBlank(shippingTemplateParam.getTitle())) {
            throw new ServiceException("运费模板标题不能为空");
        }

        List<ShippingTemplateRuleParam> shippingTemplateRuleList = shippingTemplateParam.getShippingTemplateRuleList();
        if (!CollectionUtils.isEmpty(shippingTemplateRuleList)) {
            for (ShippingTemplateRuleParam shippingTemplateRuleParam : shippingTemplateRuleList) {
                List<ShippingTemplateRuleAreaParam> shippingTemplateRuleAreaList = shippingTemplateRuleParam.getShippingTemplateRuleAreaList();
                if (CollectionUtils.isEmpty(shippingTemplateRuleAreaList)) {
                    throw new ServiceException("运费模板规则区域不能为空");
                }

                for (ShippingTemplateRuleAreaParam shippingTemplateRuleAreaParam : shippingTemplateRuleAreaList) {

                    if (StringUtils.isBlank(shippingTemplateRuleAreaParam.getProvince())) {
                        throw new ServiceException("省份不能为空");
                    }

                    if (Objects.isNull(shippingTemplateRuleAreaParam.getProvinceId())) {
                        throw new ServiceException("省份ID不能为空");
                    }

                    if (StringUtils.isBlank(shippingTemplateRuleAreaParam.getCity())) {
                        throw new ServiceException("城市不能为空");
                    }

                    if (Objects.isNull(shippingTemplateRuleAreaParam.getCityId())) {
                        throw new ServiceException("城市ID不能为空");
                    }
                }
            }
        }



    }
}
