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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.front.goods.model.GoodSku;
import com.fzm.mall.server.front.goods.model.MarketingPromotion;
import com.fzm.mall.server.front.goods.mapper.MarketingPromotionMapper;
import com.fzm.mall.server.front.goods.model.MarketingPromotionRule;
import com.fzm.mall.server.front.goods.model.vo.PromotionVo;
import com.fzm.mall.server.front.goods.model.vo.SkuVo;
import com.fzm.mall.server.front.goods.service.IMarketingPromotionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.front.goods.service.ISkuService;
import com.fzm.mall.server.front.order.model.GoodsPromotion;
import com.fzm.mall.server.front.order.model.vo.OrderSkuVo;
import com.fzm.mall.server.front.order.service.IGoodsPromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * <p>
 * 商品促销 服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-03-09
 */
@Service
public class MarketingPromotionServiceImpl extends ServiceImpl<MarketingPromotionMapper, MarketingPromotion> implements IMarketingPromotionService {

    @Autowired
    private MarketingPromotionMapper marketingPromotionMapper;

    @Autowired
    private IGoodsPromotionService iGoodsPromotionService;

    @Autowired
    private ISkuService skuService;

    @Autowired
    private IGoodsPromotionService goodsPromotionService;

    @Override
    public PromotionVo getByGoodsId(String goodsId,Integer type,String merchantId) {
        PromotionVo promotionVo = marketingPromotionMapper.getByGoodsId(goodsId,type,System.currentTimeMillis(),merchantId);
        if(promotionVo == null){
            return  null;
        }
        List<MarketingPromotionRule> marketingPromotionRules = marketingPromotionMapper.getRule(promotionVo.getPromotionId());
        promotionVo.setMarketingPromotionRules(marketingPromotionRules);
        return promotionVo;
    }

    @Override
    public PromotionVo getBySkuId(String skuId) {
        PromotionVo promotionVo = marketingPromotionMapper.getBySkuId(skuId,System.currentTimeMillis());
        if(promotionVo == null){
            return  null;
        }
        List<MarketingPromotionRule> marketingPromotionRules = marketingPromotionMapper.getRule(promotionVo.getPromotionId());
        promotionVo.setMarketingPromotionRules(marketingPromotionRules);
        return promotionVo;
    }

    @Override
    public PromotionVo getByMerchantId(String merchantId) {
        PromotionVo promotionVo = marketingPromotionMapper.getByMerchantId(merchantId,System.currentTimeMillis());
        if(promotionVo == null){
            return  null;
        }
        List<MarketingPromotionRule> marketingPromotionRules = marketingPromotionMapper.getRule(promotionVo.getPromotionId());
        promotionVo.setMarketingPromotionRules(marketingPromotionRules);
        return promotionVo;
    }

    @Override
    @Transactional
    public BigDecimal promotion(String merchantId, List<OrderSkuVo> orderSkuVos,Integer type,String oid) {
        BigDecimal price = BigDecimal.ZERO;
        for (OrderSkuVo orderSkuVo: orderSkuVos) {
            SkuVo skuVo = skuService.getBySkuId(orderSkuVo.getSkuId());
            BigDecimal amount = skuVo.getOriginalPrice().multiply(new BigDecimal(orderSkuVo.getNum()).setScale(2,BigDecimal.ROUND_UP));
            PromotionVo promotionVo = marketingPromotionMapper.getBySkuIdAndType(orderSkuVo.getSkuId(),System.currentTimeMillis(),type,skuVo.getMerchantId(),skuVo.getGoodsId());
            if(promotionVo != null){
                List<MarketingPromotionRule> marketingPromotionRules = marketingPromotionMapper.getRule(promotionVo.getPromotionId());
                BigDecimal temp = BigDecimal.ZERO;
                if(promotionVo.getType() == 2){
                    //满减从大到小
                    for (MarketingPromotionRule marketingPromotionRule:marketingPromotionRules) {
                        if(amount.compareTo(marketingPromotionRule.getConditionVal())!=-1){
                            temp = marketingPromotionRule.getPromotionVal();
                            break;
                        }
                    }
                }else if(promotionVo.getType() == 3){
                    for (MarketingPromotionRule marketingPromotionRule:marketingPromotionRules) {
                        if(amount.compareTo(marketingPromotionRule.getConditionVal())!=-1){
                            temp = (amount.divide(marketingPromotionRule.getConditionVal()).setScale(0, BigDecimal.ROUND_DOWN)).multiply(marketingPromotionRule.getPromotionVal()).setScale(2,BigDecimal.ROUND_DOWN);
                            break;
                        }
                    }
                }else if(promotionVo.getType() == 4){
                    for (MarketingPromotionRule marketingPromotionRule:marketingPromotionRules) {
                        if(amount.compareTo(marketingPromotionRule.getConditionVal())!=-1){
                            temp =  marketingPromotionRule.getPromotionVal();
                            break;
                        }
                    }
                }else if(promotionVo.getType() == 5){
                    for (MarketingPromotionRule marketingPromotionRule:marketingPromotionRules) {
                        if(amount.compareTo(marketingPromotionRule.getConditionVal())!=-1){
                            temp =  amount.subtract(amount.multiply(marketingPromotionRule.getPromotionVal()).setScale(2,BigDecimal.ROUND_DOWN));
                            break;
                        }
                    }
                }
                if(temp.compareTo(BigDecimal.ZERO)==1){
                    GoodsPromotion goodsPromotion = new GoodsPromotion();
                    goodsPromotion.setOid(oid);
                    goodsPromotion.setPromotionId(promotionVo.getPromotionId());
                    goodsPromotion.setGoodsId(skuVo.getSkuId());
                    goodsPromotion.setAmount(temp);
                    goodsPromotionService.save(goodsPromotion);
                    price = price.add(temp);
                }

            }
        }
        return price;
    }

    @Override
    public PromotionVo getByCategoryId(String categoryId) {
        PromotionVo promotionVo = marketingPromotionMapper.getByCategoryId(categoryId,System.currentTimeMillis());
        if(promotionVo == null){
            return  null;
        }
        List<MarketingPromotionRule> marketingPromotionRules = marketingPromotionMapper.getRule(promotionVo.getPromotionId());
        promotionVo.setMarketingPromotionRules(marketingPromotionRules);
        return promotionVo;
    }
   /* //param 商品id  总额  个数  类型（2.APP 3.PC 4.微信小程序）
    @Override
    @Transactional
    public BigDecimal getPrice(String goodsId,BigDecimal amount ,Integer size ,Integer type) {
        PromotionVo byGoodsId = marketingPromotionMapper.getByGoodsId(goodsId, type, System.currentTimeMillis());
        if(byGoodsId == null){
            return BigDecimal.ZERO;
        }else{
            List<MarketingPromotionRule> marketingPromotionRules = marketingPromotionMapper.getRule(byGoodsId.getPromotionId());
            if(byGoodsId.getType() == 2){
                //满减从大到小
                for (MarketingPromotionRule marketingPromotionRule:marketingPromotionRules) {
                    if(amount.compareTo(marketingPromotionRule.getConditionVal())!=-1){
                        return marketingPromotionRule.getPromotionVal();
                    }
                }
            }else if(byGoodsId.getType() == 3){
                for (MarketingPromotionRule marketingPromotionRule:marketingPromotionRules) {
                    if(amount.compareTo(marketingPromotionRule.getConditionVal())!=-1){
                        return amount.subtract(amount.multiply(marketingPromotionRule.getPromotionVal()).setScale(2,BigDecimal.ROUND_DOWN));
                    }
                }
            }else if(byGoodsId.getType() == 4){
                for (MarketingPromotionRule marketingPromotionRule:marketingPromotionRules) {
                    if(amount.compareTo(marketingPromotionRule.getConditionVal())!=-1){
                        return amount.subtract(BigDecimal.valueOf(size).multiply(marketingPromotionRule.getPromotionVal()).setScale(2,BigDecimal.ROUND_DOWN));
                    }
                }
            }else if(byGoodsId.getType() == 5){
                for (MarketingPromotionRule marketingPromotionRule:marketingPromotionRules) {
                    if(amount.compareTo(marketingPromotionRule.getConditionVal())!=-1){
                        return amount.subtract(amount.multiply(marketingPromotionRule.getPromotionVal()).setScale(2,BigDecimal.ROUND_DOWN));
                    }
                }
            }
        }
        return BigDecimal.ZERO;
    }
*/
}
