package com.fzm.mall.server.front.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlLike;
import com.fzm.mall.server.front.goods.mapper.DistributionTemplateItemMapper;
import com.fzm.mall.server.front.goods.model.DistributionTemplate;
import com.fzm.mall.server.front.goods.mapper.DistributionTemplateMapper;
import com.fzm.mall.server.front.goods.model.DistributionTemplateItem;
import com.fzm.mall.server.front.goods.service.IDistributionTemplateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 * 物流中心-运费模板 服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-03-19
 */
@Service
public class DistributionTemplateServiceImpl extends ServiceImpl<DistributionTemplateMapper, DistributionTemplate> implements IDistributionTemplateService {

    @Autowired
    private DistributionTemplateItemMapper distributionTemplateItemMapper;

    @Autowired
    private DistributionTemplateMapper distributionTemplateMapper;

    @Override
    public DistributionTemplate getTemplateId(String templateId) {
        QueryWrapper<DistributionTemplate> q = new QueryWrapper<>();
        q.eq("template_id",templateId);
        return getOne(q);
    }

    @Override
    public BigDecimal getPrice(String templateId, String province, BigDecimal bigDecimal,Integer size,BigDecimal weight) {
        province = "%"+province+"%";
        DistributionTemplateItem distributionTemplateItem = distributionTemplateItemMapper.getByTemplateIdAndProvince(templateId,province);
        if(distributionTemplateItem == null){
            return getBigDecimal(templateId, size, weight);
        }else{
            if(distributionTemplateItem.getPricingType()==5){
                //不支持快递
                return  new BigDecimal("-1");
            }else if(distributionTemplateItem.getPricingType()==4){
                if(distributionTemplateItem.getFreeShippingAmount().intValue()>size){
                    return getBigDecimal(templateId, size, weight);
                }else{
                    return  BigDecimal.ZERO;
                }
            }else if(distributionTemplateItem.getPricingType()==3){
                if(distributionTemplateItem.getFreeShippingAmount().compareTo(bigDecimal)==1){
                    return getBigDecimal(templateId, size, weight);
                }else{
                    return  BigDecimal.ZERO;
                }
            }else if(distributionTemplateItem.getPricingType()==2){
                BigDecimal firstnum = distributionTemplateItem.getBaseNumber();
                if(firstnum.compareTo(weight)!=-1){
                    return distributionTemplateItem.getBaseAmount();
                }
                BigDecimal lastNum = (weight.subtract(firstnum)).divide(distributionTemplateItem.getAddNumber()).setScale(BigDecimal.ROUND_UP);
                return distributionTemplateItem.getBaseAmount().add(distributionTemplateItem.getAddCost().multiply(lastNum)).setScale(2,BigDecimal.ROUND_UP);
            }else{
                BigDecimal firstnum = distributionTemplateItem.getBaseNumber();
                if(firstnum.intValue()>=size){
                    return distributionTemplateItem.getBaseAmount();
                }

                BigDecimal lastNum = (new BigDecimal(size).subtract(firstnum)).divide(distributionTemplateItem.getAddNumber()).setScale(BigDecimal.ROUND_UP);
                return distributionTemplateItem.getBaseAmount().add(distributionTemplateItem.getAddCost().multiply(lastNum)).setScale(2,BigDecimal.ROUND_UP);
            }
        }
    }

    @Override
    public DistributionTemplate getByMerchantId(String merchantId) {
        QueryWrapper<DistributionTemplate> q = new QueryWrapper<>();
        System.out.println(merchantId);
        q.eq("merchant_id",merchantId);
        return getOne(q);
    }

    private BigDecimal getBigDecimal(String templateId, Integer size, BigDecimal weight) {
        System.out.println(templateId);
        DistributionTemplate distributionTemplate = getTemplateId(templateId);
        if(distributionTemplate.getBillingType()==1){
            if(distributionTemplate.getPricingType()==1){
                BigDecimal firstnum = distributionTemplate.getBaseNumber();
                if(firstnum.compareTo(new BigDecimal(size))!=-1){
                    return distributionTemplate.getBaseAmount();
                }
                BigDecimal lastNum = (new BigDecimal(size).subtract(firstnum)).divide(distributionTemplate.getAddNumber().setScale(BigDecimal.ROUND_UP));
                return distributionTemplate.getBaseAmount().add(distributionTemplate.getAddCost().multiply(lastNum)).setScale(2,BigDecimal.ROUND_UP);
            }else{
                BigDecimal firstnum = distributionTemplate.getBaseNumber();
                if(firstnum.compareTo(weight)!=-1){
                    return distributionTemplate.getBaseAmount();
                }
                BigDecimal lastNum = (weight.subtract(firstnum)).divide(distributionTemplate.getAddNumber().setScale(BigDecimal.ROUND_UP));
                return distributionTemplate.getBaseAmount().add(distributionTemplate.getAddCost().multiply(lastNum)).setScale(2,BigDecimal.ROUND_UP);
            }
        }else{
            return distributionTemplate.getFreightAmount();
        }
    }
}
