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

import com.baomidou.dynamic.datasource.annotation.DS;
import org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail;
import org.jeecg.modules.fbsMomOrder.mapper.FbsMomOrderdetailMapper;
import org.jeecg.modules.fbsMomOrder.service.IFbsMomOrderdetailService;
import org.springframework.stereotype.Service;

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

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 生产订单子表
 * @author： jeecg-boot
 * @date：   2020-10-16
 * @version： V1.0
 */
@Service
@DS("multi-datasource1")
public class FbsMomOrderdetailServiceImpl extends ServiceImpl<FbsMomOrderdetailMapper, FbsMomOrderdetail> implements IFbsMomOrderdetailService {

    @Resource
    FbsMomOrderdetailMapper fbsMomOrderdetailMapper;

    @Override
    public Map<String,Object> getFbsMomOrderdetailSum() {
        return fbsMomOrderdetailMapper.getFbsMomOrderdetailSum();
    }

    @Override
    public List<FbsMomOrderdetail> getQualifiedInQty() {
        return fbsMomOrderdetailMapper.getQualifiedInQty();
    }

    @Override
    public Map<String,Object> getQualifiedInQtyWeek() {
        Map<String,Object> map=new HashMap<>();
        List<FbsMomOrderdetail> fbsMomOrderdetailList=fbsMomOrderdetailMapper.getQualifiedInQtyWeek();
        if(fbsMomOrderdetailList!=null){
            for(FbsMomOrderdetail fbsMomOrderdetail:fbsMomOrderdetailList){
                map.put(fbsMomOrderdetail.getCinvname(),fbsMomOrderdetail.getQualifiedinqty());
            }
        }
        return map;
    }

    @Override
    public List<FbsMomOrderdetail> getMonthList() {
        return fbsMomOrderdetailMapper.getMonthList();
    }

    @Override
    public List<FbsMomOrderdetail> getMonthListStatre(FbsMomOrderdetail fbsMomOrderdetail) {
        return fbsMomOrderdetailMapper.getMonthListStatre(fbsMomOrderdetail);
    }

    @Override
    public List<FbsMomOrderdetail> getMonthListStatreReviewed(FbsMomOrderdetail fbsMomOrderdetail) {
        return fbsMomOrderdetailMapper.getMonthListStatreReviewed(fbsMomOrderdetail);
    }

    @Override
    public List<FbsMomOrderdetail> getMonthListNotStatre(String month) {

        return fbsMomOrderdetailMapper.getMonthListNotStatre(month);
    }

    @Override
    public List<FbsMomOrderdetail> getMonthListGroupDefine12() {
        return fbsMomOrderdetailMapper.getMonthListGroupDefine12();
    }

    @Override
    public List<FbsMomOrderdetail> getDayList() {
        return fbsMomOrderdetailMapper.getDayList();
    }

    @Override
    public List<FbsMomOrderdetail> getListByOrderSheet(String type) {

        List<FbsMomOrderdetail> fbsMomOrderdetailListCurrentStock=fbsMomOrderdetailMapper.getListByOrderSheetCurrentStock();
        List<FbsMomOrderdetail> fbsMomOrderdetailListMomMoallocate=fbsMomOrderdetailMapper.getListByOrderSheetMomMoallocate();
        List<FbsMomOrderdetail> fbsMomOrderdetailList=new ArrayList<>();
        if(fbsMomOrderdetailListCurrentStock!=null&&fbsMomOrderdetailListMomMoallocate!=null){
            for(FbsMomOrderdetail momMoallocate:fbsMomOrderdetailListMomMoallocate){
                boolean ismomMoallocateCode=false;
                for(FbsMomOrderdetail currentStock:fbsMomOrderdetailListCurrentStock){
                    if(momMoallocate.getCinvcode().equals(currentStock.getCinvcode())){
                        if(type.equals("1")){//备料/齐套
                            getMaterialPreparation(currentStock,momMoallocate,fbsMomOrderdetailList);
                        }else if(type.equals("2")){//缺料
                            getLackOfMaterial(currentStock,momMoallocate,fbsMomOrderdetailList);
                        }else if(type.equals("3")){
                            getMaterialPreparationAndLackOfMaterial(currentStock,momMoallocate,fbsMomOrderdetailList);
                        }
                        ismomMoallocateCode=true;
                    }
                }
                //没仓仓库找到
                if(ismomMoallocateCode==false){
                    if(type.equals("3")){
                        momMoallocate.setShortageQuantity(momMoallocate.getQtys()-momMoallocate.getIssQty());
                        fbsMomOrderdetailList.add(momMoallocate);
                    }
                    //齐套
                    if(type.equals("1")){
                        if(momMoallocate.getQtys()-momMoallocate.getIssQty()<=0){
                            fbsMomOrderdetailList.add(momMoallocate);
                        }
                    }
                }
            }
        }
        return fbsMomOrderdetailList;
    }

    @Override
    public List<FbsMomOrderdetail> getListByOrderSheetEnough() {
        List<FbsMomOrderdetail> fbsMomOrderdetailListCurrentStock=fbsMomOrderdetailMapper.getListByOrderSheetCurrentStock();
        List<FbsMomOrderdetail> fbsMomOrderdetailListMomMoallocate=fbsMomOrderdetailMapper.getListByOrderSheetMomMoallocate();
        List<FbsMomOrderdetail> fbsListByOrderSheetEnough=fbsMomOrderdetailMapper.getListByOrderSheetEnough();
        return null;
    }



        @Override
    public List<FbsMomOrderdetail> getApplyList() {
        return fbsMomOrderdetailMapper.getApplyList();
    }



    //备料
    public void getMaterialPreparation(FbsMomOrderdetail currentStock,FbsMomOrderdetail momMoallocate,List<FbsMomOrderdetail> fbsMomOrderdetailList){
        if(currentStock.getMaterialPreparationTotal()-momMoallocate.getQtys()>=0){
            fbsMomOrderdetailList.add(momMoallocate);
        }

    }

    //缺料
    public void getLackOfMaterial(FbsMomOrderdetail currentStock,FbsMomOrderdetail momMoallocate,List<FbsMomOrderdetail> fbsMomOrderdetailList){
        if(currentStock.getMaterialPreparationTotal()-momMoallocate.getQtys()<0){

            momMoallocate.setShortageQuantity(momMoallocate.getQtys()-momMoallocate.getIssQty()-currentStock.getMaterialPreparationTotal());
            fbsMomOrderdetailList.add(momMoallocate);
        }
    }

    //备料缺料
    public void getMaterialPreparationAndLackOfMaterial(FbsMomOrderdetail currentStock,FbsMomOrderdetail momMoallocate,List<FbsMomOrderdetail> fbsMomOrderdetailList){
        if(currentStock.getMaterialPreparationTotal()-momMoallocate.getQtys()>=0){
            fbsMomOrderdetailList.add(momMoallocate);
        }
        if(currentStock.getMaterialPreparationTotal()-momMoallocate.getQtys()<0){
            momMoallocate.setShortageQuantity(momMoallocate.getQtys()-momMoallocate.getIssQty()-currentStock.getMaterialPreparationTotal());
            fbsMomOrderdetailList.add(momMoallocate);
        }
    }


    @Override
    public List<FbsMomOrderdetail> getListByOrderSheetNotEnough() {
        List<FbsMomOrderdetail> fbsMomOrderdetailList=fbsMomOrderdetailMapper.getListByOrderSheetNotEnough();
//        if(fbsMomOrderdetailList!=null){
//            String moCode="";
//            for (FbsMomOrderdetail fbsMomOrderdetail:fbsMomOrderdetailList){
//                if(moCode.equals("")||!moCode.equals(fbsMomOrderdetail.getMocode())){
//                    moCode=fbsMomOrderdetail.getMocode();
//                }else{
//                    fbsMomOrderdetail.setMocode("");
//                }
//            }
//        }

        return fbsMomOrderdetailList;
    }

    @Override
    public Integer getApplyCompleteSum() {
        return fbsMomOrderdetailMapper.getApplyCompleteSum();
    }

}
