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

import com.baomidou.dynamic.datasource.annotation.DS;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.fbsMomOrder.entity.Cache;
import org.jeecg.modules.fbsMomOrder.entity.CacheManager;
import org.jeecg.modules.fbsMomOrder.entity.FbsMomOrderdetail;
import org.jeecg.modules.fbsMomOrder.mapper.FbsMomOrderdetailMapper;
import org.jeecg.modules.fbsMomOrder.service.IFbsMomOrderdetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @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> getLatelyDayMomOrderList() {
        return fbsMomOrderdetailMapper.getLatelyDayMomOrderList();
    }

    @Override
    public FbsMomOrderdetail getMomOrderDetailByMoCode(String moCode) {
        return fbsMomOrderdetailMapper.getMomOrderDetailByMoCode(moCode);
    }

    @Override
    public Map<String,Object> getListByOrderSheetNow(String type) {
        Map<String,Object> map=new LinkedHashMap<>();
        //保存子件
        List<FbsMomOrderdetail> fbsMomOrderdetailListAdd=new ArrayList<>();
        //保存子子件
        List<FbsMomOrderdetail> fbsMomOrderdetailListChildAdd=new ArrayList<>();
        //订单子子件明细
        List<FbsMomOrderdetail> fbsMomOrderdetailList=new ArrayList<>();
        List<FbsMomOrderdetail> fbsMomOrderdetailList2=new ArrayList<>();
        String key=CacheManager.findAllEstate;
        Cache cache= CacheManager.getCacheInfo(key);
        if (null == cache){
            fbsMomOrderdetailList2=fbsMomOrderdetailMapper.getMomMoallocate();
            cache = new Cache();
            cache.setKey(key);
            cache.setValue(fbsMomOrderdetailList2);
            CacheManager.putCache(key, cache);


        }else {
            fbsMomOrderdetailList2=(List<FbsMomOrderdetail>) cache.getValue();
        }
        //现存量
        List<FbsMomOrderdetail> currentStockList=fbsMomOrderdetailMapper.getListByOrderSheetCurrentStock();

        if(fbsMomOrderdetailList2!=null&&fbsMomOrderdetailList2.size()>0){
            //以防缓存的数据发生改变
//            for(FbsMomOrderdetail f:fbsMomOrderdetailList2){
//                fbsMomOrderdetailList.add(f);
//            }
            fbsMomOrderdetailList.addAll(fbsMomOrderdetailList2);
            Integer qtysTotal=0;//数量总计
            Integer issQtyTotal=0;//已领总计
            Integer shortageQuantityTotal=0;//缺料总计
            String name="";
            String code="";
            FbsMomOrderdetail fbsMomOrderdetailAdd=new FbsMomOrderdetail();
            /////////////子件
            for(FbsMomOrderdetail fbsMomOrderdetail:fbsMomOrderdetailList){
                //如果本次循环与上次的生产订单不同则添加上一个订单信息
                if(!code.equals(fbsMomOrderdetail.getInvcode())&&!code.equals("")){
                    //解决不改变原有对象的值
                    FbsMomOrderdetail a=new FbsMomOrderdetail();
                    BeanUtils.copyProperties(fbsMomOrderdetailAdd, a);
                    //
                    a.setQtys(qtysTotal);
                    a.setIssQty(issQtyTotal);
                    a.setShortageQuantity(shortageQuantityTotal);
                    fbsMomOrderdetailListAdd.add(a);
                    qtysTotal=0;
                    issQtyTotal=0;
                    shortageQuantityTotal=0;
                }
                Integer  qtys=0;//数量
                if(StringUtils.isNotBlank(fbsMomOrderdetail.getQtys()+"")){
                    qtys=fbsMomOrderdetail.getQtys();
                }
                Integer issQty=0;//已领
                if(StringUtils.isNotBlank(fbsMomOrderdetail.getIssQty()+"")){
                    issQty=fbsMomOrderdetail.getIssQty();
                }
                //////////////////现存量库存
                for(FbsMomOrderdetail currentStock:currentStockList){
                    //找到库存对应的订单数量
                    if(fbsMomOrderdetail.getMInvCode().equals(currentStock.getCinvcode())){
                        Integer qtysStock=0;//库存数量
                        qtysStock=currentStock.getMaterialPreparationTotal();
                        //是否小于库存 小于则缺料
                        if(qtysStock<(qtys-issQty)){
                            fbsMomOrderdetail.setShortageQuantity(qtys-issQty-qtysStock);
                        }else {
                            fbsMomOrderdetail.setShortageQuantity(0);
                        }
                        //剩余库存数量
                        currentStock.setMaterialPreparationTotal(qtysStock-(qtys-issQty));
                        //缺料数量
//                        shortageQuantityTotal=fbsMomOrderdetail.getShortageQuantity();
                    }
                }
                fbsMomOrderdetailAdd=fbsMomOrderdetail;
                name=fbsMomOrderdetail.getMocode();
                code=fbsMomOrderdetail.getInvcode();
                //累加
                if(StringUtils.isNotBlank(fbsMomOrderdetail.getQtys()+"")){
                    qtysTotal=qtysTotal+qtys;
                }
                if(StringUtils.isNotBlank(fbsMomOrderdetail.getIssQty()+"")){
                    issQtyTotal=issQtyTotal+issQty;
                }
                if(fbsMomOrderdetail.getShortageQuantity()==null){
                    fbsMomOrderdetail.setShortageQuantity(qtys-issQty);
                }

                shortageQuantityTotal=shortageQuantityTotal+fbsMomOrderdetail.getShortageQuantity();
                //获取缺料明细
                if(fbsMomOrderdetail.getShortageQuantity()>0){
                    fbsMomOrderdetailListChildAdd.add(fbsMomOrderdetail);
                }
            }

            //添加最后一个缺料信息
            //解决不改变原有对象的值
            FbsMomOrderdetail b=new FbsMomOrderdetail();
            BeanUtils.copyProperties(fbsMomOrderdetailAdd, b);
            //
            b.setQtys(qtysTotal);
            b.setIssQty(issQtyTotal);
            b.setShortageQuantity(shortageQuantityTotal);
            fbsMomOrderdetailListAdd.add(b);
        }

//        if(type.equals("1")){//缺料齐套
//            return fbsMomOrderdetailListAdd;
//        }else if(type.equals("2")){//齐套
//            List<FbsMomOrderdetail> fbsMomOrderdetailListQitao=new ArrayList<>();
//            for(FbsMomOrderdetail fbsMomOrderdetail:fbsMomOrderdetailListAdd){
//                if(fbsMomOrderdetail.getShortageQuantity()!=null&&fbsMomOrderdetail.getShortageQuantity()<=0){
//                    fbsMomOrderdetailListQitao.add(fbsMomOrderdetail);
//                }
//            }
//            return fbsMomOrderdetailListQitao;
//        }else if(type.equals("3")){//缺料明细
//            return fbsMomOrderdetailListChildAdd;
//        }else {
//            return new ArrayList<>();
//        }
        //备料
        map.put("1",fbsMomOrderdetailListAdd);
        //齐套
        List<FbsMomOrderdetail> fbsMomOrderdetailListQitao=new ArrayList<>();
        for(FbsMomOrderdetail fbsMomOrderdetail:fbsMomOrderdetailListAdd){
            if(fbsMomOrderdetail.getShortageQuantity()!=null&&fbsMomOrderdetail.getShortageQuantity()<=0){
                fbsMomOrderdetailListQitao.add(fbsMomOrderdetail);
            }
        }
        map.put("2",fbsMomOrderdetailListQitao);
        //缺料
        map.put("3",fbsMomOrderdetailListChildAdd);
        return map;
    }
    public BigDecimal getDecimalNew(String d){
        BigDecimal decimal= new BigDecimal(d);
        return decimal;
    }
    @Override
    public List<FbsMomOrderdetail> getMomMoallocateByMoCode(String moCode) {
        return fbsMomOrderdetailMapper.getMomMoallocateByMoCode(moCode);
    }

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



    @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();
    }


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

    @Override
    public List<FbsMomOrderdetail> getMoallocateList() {
//        List<FbsMomOrderdetail> orderdetailList=fbsMomOrderdetailMapper.getOrderDetailList();
        List<FbsMomOrderdetail> momdetailList=fbsMomOrderdetailMapper.getMoallocateList();
        List<FbsMomOrderdetail> orderAll=new ArrayList<>();
//        for(FbsMomOrderdetail fbsMomOrderdetail:orderdetailList){
//            int i=0;
//            for(FbsMomOrderdetail momdetail:momdetailList){
//                if(fbsMomOrderdetail.getMocode().equals(momdetail.getMocode())){
//                    orderAll.add(momdetail);
//                    i++;
//                }
//            }
//            if(i<18){
//                i=18-i;
//            }else{
//                i=i%18;
//            }
//            for(int ii=0;ii<i;ii++){
//                FbsMomOrderdetail fbsMomOrderdetail1=new FbsMomOrderdetail();
//                fbsMomOrderdetail1.setMocode(fbsMomOrderdetail.getMocode());
//                fbsMomOrderdetail1.setInvcode(fbsMomOrderdetail.getInvcode());;
//                fbsMomOrderdetail1.setCinvname(fbsMomOrderdetail.getCinvname());
//                orderAll.add(fbsMomOrderdetail1);
//            }
//        }
        String code="";
        int i=0;
        for(FbsMomOrderdetail fbsMomOrderdetail:momdetailList){
            if(code.equals("")){
                code=fbsMomOrderdetail.getMocode();
            }

            if(!code.equals(fbsMomOrderdetail.getMocode())){
                int length=0;
                if(i<18){
                    length=18-i;
                }else{
                    length=18-(i%18);
                }
                for(int ii=0;ii<length;ii++){
                    FbsMomOrderdetail fbsMomOrderdetail1=new FbsMomOrderdetail();
                    fbsMomOrderdetail1.setMocode(code);
                    orderAll.add(fbsMomOrderdetail1);
                }
                code=fbsMomOrderdetail.getMocode();
                i=0;
            }
            orderAll.add(fbsMomOrderdetail);
            i++;
        }
        int b=0;
        for(FbsMomOrderdetail fbsMomOrderdetail:orderAll){
            if(b==0||b%18==0){
                fbsMomOrderdetail.setFree1("1");
            }else{
                fbsMomOrderdetail.setFree1("0");
            }
            b++;
        }


        return orderAll;
    }

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

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


}
