package com.neusoft.smart.pos.promotion.service;

import com.neusoft.smart.pos.dto.promotion.bean.CalculateBean;
import org.springframework.stereotype.Service;

import java.math.RoundingMode;

/**
 * 手动设置营销
 */
@Service
public class ManualPromotionCalculate implements IPromotionCalculate {
    
    @Override
    public CalculateBean calculatePrice(CalculateBean calculateBean) {
        //暂时只支持抹零和手动折扣
        if(null!=calculateBean.getDiscount()) {//订单不给折扣信息,说明不支持折扣和抹零
            calculateBean.setActualPrice(calculateBean.getActualPrice().multiply(calculateBean.getDiscount())
                    .setScale(2, RoundingMode.UP).subtract(calculateBean.getFloorPrice()));
            //根据折扣,计算每件商品的实际价格
            if (null != calculateBean.getOrderItems()) {
                calculateBean.getOrderPreferentialItems().forEach(item ->
                        item.setPreferentialUnitPrice(item.getPreferentialUnitPrice().multiply(calculateBean.getDiscount()).setScale(2, RoundingMode.UP)));
            }
        }
        return calculateBean;
    }
    
}
