package org.jeecg.modules.fbsPuArrivalvouch.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.fbsPuArrivalvouch.entity.FbsPuArrivalvouch;
import org.jeecg.modules.fbsPuArrivalvouch.mapper.FbsPuArrivalvouchMapper;
import org.jeecg.modules.fbsPuArrivalvouch.service.IFbsPuArrivalvouchService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static org.jeecg.common.util.oConvertUtils.isEmpty;

/**
 * @Description: 采购到货退货单主表（表头）
 * @author： jeecg-boot
 * @date：   2020-10-21
 * @version： V1.0
 */
@Service
@DS("multi-datasource1")
public class FbsPuArrivalvouchServiceImpl extends ServiceImpl<FbsPuArrivalvouchMapper, FbsPuArrivalvouch> implements IFbsPuArrivalvouchService {
    @Resource
    private FbsPuArrivalvouchMapper fbsPuArrivalvouchMapper;

    public List<Map<String,Object>> getWatingCheckList(){
        return fbsPuArrivalvouchMapper.getWatingCheckList();
    }

    public int getCheckSum(){
        return fbsPuArrivalvouchMapper.getCheckSum();
    }


    public List<Map<String,Object>> getWatingInList(){
        return fbsPuArrivalvouchMapper.getWatingInList();
    }

    public int getInSum(){
        return fbsPuArrivalvouchMapper.getInSum();
    }

    @Override
    public Map<String,Object> getPassRateByVendor() {
        //获取合格率数据
        List<FbsPuArrivalvouch> fbsPuArrivalvouchList=fbsPuArrivalvouchMapper.getPassRateByVendor();
        //封装map数据
        Map<String,Object> map=new HashMap<>();
        if(fbsPuArrivalvouchList!=null){
            for(FbsPuArrivalvouch fbsPuArrivalvouch:fbsPuArrivalvouchList){
                map.put(fbsPuArrivalvouch.getCvencode(),fbsPuArrivalvouch.getPassRate());
            }
        }
        return map;
    }

    @Override
    public Integer getTodayArriveSum() {
        return fbsPuArrivalvouchMapper.getTodayArriveSum();
    }

    @Override
    public Integer getWillArriveSum() {
        return fbsPuArrivalvouchMapper.getWillArriveSum();
    }

    @Override
    public Integer getThisMonthArriveSum() {
        return fbsPuArrivalvouchMapper.getThisMonthArriveSum();
    }

    @Override
    public Integer getNotInSum() {
        return fbsPuArrivalvouchMapper.getNotInSum();
    }
    public BigDecimal getDecimal(Double d){
        BigDecimal decimal= new BigDecimal(d);
        return decimal;
    }

    private boolean isNumber(String str){
        if(isEmpty(str)){
            return false;
        }
        String reg = "\\d+(\\.\\d+)?";
        return str.matches(reg);

    }

    public static String formatDouble5(double d) {
        return String.format("%.2f", d);
    }
    @Override
    public  Map<String,Object> getCheckRateByBatchno() {
        //获取合格率数据
        List<FbsPuArrivalvouch> fbsPuArrivalvouchList=fbsPuArrivalvouchMapper.getCheckRateByBatchno();
        //质量异常合格数据
        List<Map<String, Object>> qualityUnpassList=fbsPuArrivalvouchMapper.getQualityUnpassList();
        //封装map数据
        Map<String,Object> map=new LinkedHashMap<>();
        if(fbsPuArrivalvouchList!=null){
            for(FbsPuArrivalvouch fbsPuArrivalvouch:fbsPuArrivalvouchList){
                BigDecimal unQuality=new BigDecimal("0.00");
                //找到对应供应商的不良品异常数量
                for(Map<String, Object> unMap:qualityUnpassList){
                    if(StringUtils.isNotBlank(fbsPuArrivalvouch.getCvencode())&&StringUtils.isNotBlank(String.valueOf(unMap.get("cvencode")))){
                        if(fbsPuArrivalvouch.getCvencode().equals(String.valueOf(unMap.get("cvencode")))){
                            unQuality=unQuality.add(getDecimal(Double.valueOf(String.valueOf(unMap.get("fdisquantity")))));
                        }
                    }
                }
                //总数量减去不良品数量
                BigDecimal yesQua=new BigDecimal("0.00");
                yesQua=fbsPuArrivalvouch.getFvalidquantity().subtract(unQuality);
                //获取良品合格率
                fbsPuArrivalvouch.setPassRate((yesQua.divide(fbsPuArrivalvouch.getIquantity(), 2, BigDecimal.ROUND_HALF_UP).multiply(getDecimal(100.0))).doubleValue()+"");
                map.put(fbsPuArrivalvouch.getCvencode(),fbsPuArrivalvouch.getPassRate());
            }
        }
        return map;
    }

    @Override
    public List<Map<String, Object>> getQualityUnpassList() {
        return fbsPuArrivalvouchMapper.getQualityUnpassList();
    }

}
