package com.steerinfo.baseinfo.meterbasemattermap.service.impl;

import com.steerinfo.baseinfo.combaseinfo.mapper.ComBaseInfoMapper;
import com.steerinfo.baseinfo.combaseinfo.model.ComBaseInfo;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasemattermap.model.MeterBaseMatterMap;
import com.steerinfo.baseinfo.meterbasemattermap.mapper.MeterBaseMatterMapMapper;
import com.steerinfo.baseinfo.meterbasemattermap.service.IMeterBaseMatterMapService;
import com.steerinfo.meterwork.meterworkcaractualfirst.mapper.MeterWorkCarActualFirstMapper;
import com.steerinfo.meterwork.meterworkcaractualfirst.model.MeterWorkCarActualFirst;
import com.steerinfo.meterwork.meterworkprewgttype.mapper.MeterWorkPreWgttypeMapper;
import com.steerinfo.meterwork.meterworkprewgttype.model.MeterWorkPreWgttype;
import com.steerinfo.pretrack.prerailwayscale.mapper.PreRailwayScaleMapper;
import com.steerinfo.pretrack.pretrackscale.mapper.PreTrackScaleMapper;
import com.steerinfo.pretrack.pretrackscale.model.PreTrackScale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * MeterBaseMatterMap服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2022-08-30 09:28
 * 类描述
 * 修订历史：
 * 日期：2022-08-30
 * 作者：generator
 * 参考：
 * 描述：MeterBaseMatterMap服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "meterBaseMatterMapService")
public class MeterBaseMatterMapServiceImpl extends BaseServiceImpl<MeterBaseMatterMap, String> implements IMeterBaseMatterMapService {

    @Resource
    private MeterBaseMatterMapMapper meterBaseMatterMapMapper;

    @Resource
    MeterWorkPreWgttypeMapper meterWorkPreWgttypeMapper;

    @Resource
    ComBaseInfoMapper comBaseInfoMapper;

    @Resource
    PreTrackScaleMapper preTrackScaleMapper;

    @Resource
    MeterWorkCarActualFirstMapper meterWorkCarActualFirstMapper;

    @Override
    protected IBaseMapper<MeterBaseMatterMap, String> getMapper() {
        return meterBaseMatterMapMapper;
    }

    @Override
    public RESTfulResult isDoublePre(String carNo) {
        RESTfulResult rm = new RESTfulResult();
        try {
            List<PreTrackScale> tempList = new ArrayList<PreTrackScale>();

            String forwardingUnitNo = "";
            String matterNo = "";
            String receivingUintNo = "";
            rm.setCode("0");
            rm.setMessage("操作成功");
            rm.setSucceed(true);
            //查询车辆是否为内倒双委托车辆
            HashMap preBase = new HashMap();
            preBase.put("pBaseCode","001047");
            preBase.put("valueFlag","1");
            preBase.put("baseName",carNo);
            List<ComBaseInfo> cbis = comBaseInfoMapper.selectByParameters(preBase);
            if(cbis.size() != 1){
                rm.setData(null);
                return rm;
            }
            //查询改成已选择是皮重还是毛重
            HashMap preMap = new HashMap();
            preMap.put("carNo",carNo);
            preMap.put("valueFlag","1");
            List<MeterWorkPreWgttype> mwpws = meterWorkPreWgttypeMapper.selectByParameters(preMap);
            if(mwpws.size() >= 1) {
                if(mwpws.get(0).getWeightTypeNo().equals("001033001")){
                    //查询车辆已选择的委托并拿到委托的发货单位和物料编号
                    HashMap preSel = new HashMap();
                    preSel.put("carNo",carNo);
                    preSel.put("lineDesc","1");
                    preSel.put("valueFlag","1");
                    List<PreTrackScale> ptsSel = preTrackScaleMapper.selectByParameters(preSel);
                    if(ptsSel.size() < 1) {
                        rm.setData(null);
                        return rm;
                    }
                    forwardingUnitNo = ptsSel.get(0).getForwardingUnitNo();
                    matterNo = ptsSel.get(0).getMatterNo();
                    receivingUintNo = ptsSel.get(0).getReceivingUintNo();
                    //查询改发货单位是否满足内倒双委托
                    HashMap preBaseUnit = new HashMap();
                    preBaseUnit.put("pBaseCode","001049");
                    preBaseUnit.put("valueFlag","1");
                    preBaseUnit.put("baseName",forwardingUnitNo);
                    List<ComBaseInfo> cbisUnit = comBaseInfoMapper.selectByParameters(preBaseUnit);
                    if(cbisUnit.size() != 1){
                        rm.setData(null);
                        return rm;
                    }
                }
                //当选择的是毛重时
                if(mwpws.get(0).getWeightTypeNo().equals("001033001")){
                    //判断未被选择的委托中是否存在跟已选择的委托存在双委托关系
                    HashMap preNoSel = new HashMap();
                    preNoSel.put("carNo",carNo);
                    preNoSel.put("valueFlag","1");
                    preNoSel.put("lineDesc","0");
                    List<PreTrackScale> pts = preTrackScaleMapper.selectByParameterDoubles(preNoSel);
                    if(pts.size() > 0) {
                        //循环判断是否有满足的双委托，有则计数
                        for (PreTrackScale model : pts){
                            if((model.getForwardingUnitNo().equals(receivingUintNo) && model.getMatterNo().equals(matterNo)) ||
                                    (model.getReceivingUintNo().equals(forwardingUnitNo) && model.getMatterNo().equals(matterNo))){
                                tempList.add(model);
                            }
                        }
                    }
                    //查询映射物料编号
                    HashMap matterMap = new HashMap();
                    matterMap.put("maaterNo1",matterNo);
                    matterMap.put("valueFlag","1");
                    List<MeterBaseMatterMap> mbmm = meterBaseMatterMapMapper.selectByParameters(matterMap);
                    if(mbmm.size() > 0){
                        for(MeterBaseMatterMap map : mbmm){
                            //判断未被选择的委托中是否存在跟映射关系已选择的委托存在双委托关系
                            HashMap preNoSelMbmm = new HashMap();
                            preNoSelMbmm.put("carNo",carNo);
                            preNoSelMbmm.put("valueFlag","1");
                            preNoSelMbmm.put("lineDesc","0");
                            preNoSelMbmm.put("matterNo",map.getMaaterNo2());
                            List<PreTrackScale> ptsMbmm = preTrackScaleMapper.selectByParameterDoubles(preNoSelMbmm);
                            if(ptsMbmm.size() > 0){
                                for (PreTrackScale model : ptsMbmm){
                                    if((model.getForwardingUnitNo().equals(receivingUintNo) && model.getMatterNo().equals(map.getMaaterNo2())) ||
                                            (model.getReceivingUintNo().equals(forwardingUnitNo) && model.getMatterNo().equals(map.getMaaterNo2()))){
                                        tempList.add(model);
                                    }
                                }
                            }
                        }
                    }
                }
                //当选择的是皮重时
                else {
                    List<MeterWorkCarActualFirst> mwcaf = meterWorkCarActualFirstMapper.selectByCarNo(carNo);
                    if(mwcaf.size() == 0){
                        rm.setData(null);
                        return rm;
                    }
                    else if(mwcaf.size() == 1){
                        for (MeterWorkCarActualFirst model : mwcaf){
                            if(model.getWeightType() == "0" && !model.getPredictionType().equals("")){
                                //判断未被选择的委托中是否存在跟已选择的委托存在双委托关系
                                HashMap preNoSel = new HashMap();
                                preNoSel.put("carNo",carNo);
                                preNoSel.put("lineDesc","0");
                                preNoSel.put("valueFlag","4");
                                List<PreTrackScale> pts = preTrackScaleMapper.selectByParameters(preNoSel);
                                if(pts.size() > 0) {
                                    //循环判断是否有满足的双委托，有则计数
                                    for (PreTrackScale modelPre : pts){
                                        if((modelPre.getForwardingUnitNo().equals(receivingUintNo) && modelPre.getMatterNo().equals(model.getMatterNo())) ||
                                                (modelPre.getReceivingUintNo().equals(forwardingUnitNo) && modelPre.getMatterNo().equals(model.getMatterNo()))){
                                            tempList.add(modelPre);
                                        }
                                    }
                                }
                                //查询映射物料编号
                                HashMap matterMap = new HashMap();
                                matterMap.put("maaterNo1",model.getMatterNo());
                                matterMap.put("valueFlag","1");
                                List<MeterBaseMatterMap> mbmm = meterBaseMatterMapMapper.selectByParameters(matterMap);
                                if(mbmm.size() > 0){
                                    for(MeterBaseMatterMap map : mbmm){
                                        //判断未被选择的委托中是否存在跟映射关系已选择的委托存在双委托关系
                                        HashMap preNoSelMbmm = new HashMap();
                                        preNoSelMbmm.put("carNo",carNo);
                                        preNoSelMbmm.put("lineDesc","0");
                                        preNoSelMbmm.put("valueFlag","4");
                                        preNoSelMbmm.put("matterNo",map.getMaaterNo2());
                                        List<PreTrackScale> ptsMbmm = preTrackScaleMapper.selectByParameters(preNoSelMbmm);
                                        if(ptsMbmm.size() > 0){
                                            for (PreTrackScale modelPre : ptsMbmm){
                                                if((model.getForwardingUnitNo().equals(receivingUintNo) && model.getMatterNo().equals(map.getMaaterNo2())) ||
                                                        (model.getReceivingUintNo().equals(forwardingUnitNo) && model.getMatterNo().equals(map.getMaaterNo2()))){
                                                    tempList.add(modelPre);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else{
                        for (MeterWorkCarActualFirst model : mwcaf){
                            if(model.getWeightType().equals("0") && model.getPredictionType().equals("5")){
                                PreTrackScale res = new PreTrackScale();
                                res.setPredictionNo("111111");
                                res.setMatterNo("11111");
                                res.setForwardingUnitNo("11111");
                                res.setReceivingUintNo("1111");
                                tempList.add(res);
                                break;
                            }
                        }
                    }
                }
            }

            if(tempList.size() > 0){
                String matter = "";
                String forwarding = "";
                String receiving = "";
                boolean flag = true;
                for (PreTrackScale item : tempList)
                {
                    if (matter.equals("") && forwarding.equals("")  && receiving.equals("") )
                    {
                        matter = item.getMatterNo();
                        forwarding = item.getForwardingUnitNo();
                        receiving = item.getReceivingUintNo();
                    }
                    else
                    {
                        if (matter.equals(item.getMatterNo())  && forwarding.equals(item.getForwardingUnitNo()) && receiving.equals(item.getReceivingUintNo()))
                        { }
                        else
                        {
                            flag = false;
                        }
                    }
                }
                if(flag){
                    rm.setData(tempList.get(0));
                }
                else {
                    rm.setData(null);
                }
            }
            else {
                rm.setData(null);
            }
            return rm;
        }
        catch (Exception e){
            rm.setCode("500");
            rm.setSucceed(false);
            rm.setData(null);
            rm.setMessage("操作失败--》" + e.getMessage());
            e.printStackTrace();
            return rm;
        }
    }
}
