package com.gree.mall.miniapp.logic.material.parts;

import com.gree.mall.miniapp.bean.material.parts.WebsitPartsStockBean;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.commonmapper.MaterialMapper;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.MaterialConfig;
import com.gree.mall.miniapp.plus.service.MaterialConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class WebsitPartsStockLogic {

    @Resource
    MaterialMapper materialMapper;
    @Resource
    CommonLogic commonLogic;
    @Resource
    MaterialConfigService materialConfigService;

    public List<String> websitGroupList(String websitId, String partsWebsitId) {
        CurrentCompanyWechat wechat = commonLogic.getCurrentCompanyWechat();
        return materialMapper.websitGroupList(wechat.getCompanyWechatId(), websitId, partsWebsitId);
    }

    public List<WebsitPartsStockBean> existWebsitStockList(String websitId, String partsWebsitId, String partsNumber, String partsName, String materialGroupName) {
        CurrentCompanyWechat wechat = commonLogic.getCurrentCompanyWechat();
        List<WebsitPartsStockBean> partsStockBeans = materialMapper.existWebsitStockList(wechat.getCompanyWechatId(), websitId, partsWebsitId, partsNumber, partsName, materialGroupName);
        for (WebsitPartsStockBean partsStockBean : partsStockBeans) {
            this.computeSalesPrice(partsStockBean);
        }
        return partsStockBeans;
    }

    /**
     * 计算销售价
     * @param partsStockBean
     */
    private void computeSalesPrice(WebsitPartsStockBean partsStockBean) {
        final MaterialConfig materialConfig = materialConfigService.lambdaQuery()
                .eq(MaterialConfig::getCompanyWechatId, partsStockBean.getCompanyWechatId())
                .one();
        switch (Optional.ofNullable(partsStockBean.getPartType()).orElse("")) {
            case "空调":
                partsStockBean.setSalesPrice(partsStockBean.getMarketPrice().multiply(materialConfig.getPriceRate1()).setScale(2, BigDecimal.ROUND_HALF_UP));
                break;
            case "冰箱":
                partsStockBean.setSalesPrice(partsStockBean.getMarketPrice().multiply(materialConfig.getPriceRate2()).setScale(2, BigDecimal.ROUND_HALF_UP));
                break;
            case "生活电器":
                partsStockBean.setSalesPrice(partsStockBean.getMarketPrice().multiply(materialConfig.getPriceRate3()).setScale(2, BigDecimal.ROUND_HALF_UP));
                break;
            default:
                partsStockBean.setSalesPrice(partsStockBean.getMarketPrice().multiply(new BigDecimal("1.2")).setScale(2, BigDecimal.ROUND_HALF_UP));
                break;
        }
    }
}
