package com.steering.jobface.lims;

import CoreFS.Util.StringUtils;
import com.steering.jobface.lims.service.LimsDataService;
import com.steering.model.lims.*;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.TimerTask;

public class LimsDataTask extends TimerTask {
    Logger logger = Logger.getLogger(LimsDataTask.class);


    private Map<String,String> codeMap = new HashMap<String,String>();
    private Map<String,String> nameMap = new HashMap<String,String>();

    public LimsDataTask() {
        codeMap.put("00001","429");
        codeMap.put("00002","431");
        codeMap.put("00003","423");
        codeMap.put("00004","424");
        codeMap.put("00005","430");
        codeMap.put("00006","428");
        codeMap.put("00007","427");
        codeMap.put("00008","425");
        codeMap.put("00009","426");
        codeMap.put("00010","421");
        codeMap.put("00011","425");

        nameMap.put("720汽车衡","720检斤站");
        nameMap.put("公转铁汽车衡","公转铁检斤站");
        nameMap.put("2炼钢汽车衡","二炼钢检斤站");
        nameMap.put("1炼钢汽车衡","一炼钢检斤站");
        nameMap.put("合金库汽车衡","合金库检斤站");
        nameMap.put("炼铁厂80T汽车衡","海绵铁80T检斤站");
        nameMap.put("炼铁厂150T汽车衡","海绵铁150T检斤站");
        nameMap.put("7号路南汽车衡","七号路南检斤站");
        nameMap.put("7号路北汽车衡","七号路北检斤站");
        nameMap.put("8号路汽车衡","八号路检斤站");
        nameMap.put("废钢汽车衡","七号路南检斤站");
    }

    @Override
    public synchronized void run() {
        try {
            LimsDataService limsDataService = new LimsDataService();
            // 一次计量
//            LimsActualFirstSync(limsDataService);
            // 洁净计量
            LimsActualSync(limsDataService);
        } catch (Exception e) {
            logger.error("计量同步报错！ " + e.getMessage());
            e.printStackTrace();
        }
    }

    private synchronized void LimsActualSync(LimsDataService limsDataService){
        MeterWorkCarActualEntity carActualEntity = new MeterWorkCarActualEntity();
        // 查询新计量数据
        ArrayList<MeterWorkCarActualEntity> carActuals = limsDataService.getCarActual(carActualEntity);
        if (carActuals == null || carActuals.size() <= 0) {
            return;
        }
        for (MeterWorkCarActualEntity carActual : carActuals) {
            MMeasureTEntity mMeasureTEntity = new MMeasureTEntity();
            try {
                // 合金库汽车衡 扣杂0 不同步到老计量
//                if (carActual.getBaseSpot2No().equals("00005")
//                        && (StringUtils.isEmpty(carActual.getImpurityWgt()) || Integer.valueOf(carActual.getImpurityWgt()) == 0)) {
//                    // 修改新计量状态
//                    carActual.setInterRead("9");
//                    limsDataService.updateCarActual(carActual);
//                    continue;
//                }

                //验配ID
                mMeasureTEntity.setMatchid(carActual.getActualNo());
                if (!StringUtils.isEmpty(carActual.getNoticeNo()) && "001006003".equals(carActual.getMeterTypeNo())) { // 中控倒运
                    mMeasureTEntity.setMatchid(carActual.getNoticeNo());
                }
                // 查询同步计量的物料
                MMaterialTEntity materialTEntity = new MMaterialTEntity();
                materialTEntity.setMaterielname(carActual.getMatterName());
                MMaterialTEntity matInfo = limsDataService.getMaterial(materialTEntity);
                if(matInfo != null && !StringUtils.isEmpty(matInfo.getMaterielcode())){
                    carActual.setMatterNo(matInfo.getMaterielcode());
                }
                // 查询同步计量的供应商
                MUnitTEntity mUnitTEntity = new MUnitTEntity();
                mUnitTEntity.setUnitname(carActual.getCustomerSupplierName());
                MUnitTEntity unit = limsDataService.getOldUnit(mUnitTEntity);
                if(unit != null && !StringUtils.isEmpty(unit.getUnitcode())){
                    carActual.setCustomerSupplierNo(unit.getUnitcode());
                }
                mUnitTEntity.setUnitname(carActual.getForwardingUnitName());
                MUnitTEntity unit1 = limsDataService.getOldUnit(mUnitTEntity);
                if(unit1 != null && !StringUtils.isEmpty(unit1.getUnitcode())){
                    carActual.setForwardingUnitNo(unit1.getUnitcode());
                }
                mUnitTEntity.setUnitname(carActual.getReceivingUintName());
                MUnitTEntity unit2 = limsDataService.getOldUnit(mUnitTEntity);
                if(unit2 != null && !StringUtils.isEmpty(unit2.getUnitcode())){
                    carActual.setReceivingUintNo(unit2.getUnitcode());
                }

                mMeasureTEntity.setIcno(carActual.getBindCardNo());
                mMeasureTEntity.setCarno(carActual.getCarNo());
                mMeasureTEntity.setCartype("C");

                mMeasureTEntity.setOperatype("0");
                mMeasureTEntity.setTaskcode("0");
                if ("001006001".equals(carActual.getMeterTypeNo())) {// 采购
                    mMeasureTEntity.setOperatype("9");
                    mMeasureTEntity.setTaskcode("9");
                } else if ("001006002".equals(carActual.getMeterTypeNo())) {// 销售
                    mMeasureTEntity.setOperatype("8");
                    mMeasureTEntity.setTaskcode("8");
                }

                mMeasureTEntity.setOrderno(carActual.getContractNo());
                mMeasureTEntity.setMaterialcode(carActual.getMatterNo());
                mMeasureTEntity.setMaterialname(carActual.getMatterName());
                mMeasureTEntity.setMaterialrule(carActual.getSpecName());
                mMeasureTEntity.setMaterialquality("");

                mMeasureTEntity.setSourcecode(carActual.getForwardingUnitNo());
                mMeasureTEntity.setSourcename(carActual.getForwardingUnitName());
                // 卸货地点
                mMeasureTEntity.setTargetcode(carActual.getReceivingUintNo());
                mMeasureTEntity.setTargetname(carActual.getReceivingUintName());
                if("001006001".equals(carActual.getMeterTypeNo())){ // 采购进场
                    mUnitTEntity.setUnitname(carActual.getLoadPointName());
                    MUnitTEntity unit3 = limsDataService.getOldUnit(mUnitTEntity);
                    if(unit3 != null && !StringUtils.isEmpty(unit3.getUnitcode())){
                        mMeasureTEntity.setTargetcode(unit3.getUnitcode());
                    }
                    mMeasureTEntity.setTargetname(carActual.getLoadPointName());
                }

                mMeasureTEntity.setBasket(carActual.getAddWgtNo());

                // 毛重
                mMeasureTEntity.setGross(String.valueOf((Double.valueOf(carActual.getGrossWeight()) / 1000) -  (Double.valueOf(carActual.getGrossCarWeight()) / 1000)));
                mMeasureTEntity.setGrosstime(carActual.getGrossTime());
                mMeasureTEntity.setGrossweighid(codeMap.get(carActual.getBaseSpot1No()));
                mMeasureTEntity.setGrossweigh(nameMap.get(carActual.getBaseSpot1Name()));
                mMeasureTEntity.setGrossweighgroup("");
                mMeasureTEntity.setGrossoperatorid(carActual.getGrossManNo());
                mMeasureTEntity.setGrossoperator(carActual.getGrossManName());
                mMeasureTEntity.setGrossgroupno("");
                mMeasureTEntity.setGrossserial("");

                // 皮重
                mMeasureTEntity.setTare(String.valueOf((Double.valueOf(carActual.getTareWeight()) / 1000) -  (Double.valueOf(carActual.getTareCarWeight()) / 1000)));
                mMeasureTEntity.setTaretime(carActual.getTareTime());
                mMeasureTEntity.setTareweighid(codeMap.get(carActual.getBaseSpot2No()));
                mMeasureTEntity.setTareweigh(nameMap.get(carActual.getBaseSpot2Name()));
                mMeasureTEntity.setTareweighgroup("");
                mMeasureTEntity.setTareoperatorid(carActual.getTareManNo());
                mMeasureTEntity.setTareoperator(carActual.getTareManName());
                mMeasureTEntity.setTaregroupno("");
                mMeasureTEntity.setTareserial("");


                // 扣重
                mMeasureTEntity.setDeduction(String.valueOf(Double.valueOf(carActual.getImpurityWgt()) / 1000));
                mMeasureTEntity.setDeductiontime(carActual.getMinusTime());
                mMeasureTEntity.setDeductionunit("");
                mMeasureTEntity.setDeductionoperatorid(carActual.getMinusManNo());
                mMeasureTEntity.setDeductionoperator(carActual.getMinusManName());

                // 净重
                mMeasureTEntity.setSuttle(String.valueOf((Double.valueOf(carActual.getNetWeight()) - Double.valueOf(carActual.getImpurityWgt())) / 1000));
                if("001006006".equals(carActual.getMeterTypeNo())){
                    mMeasureTEntity.setSuttle(String.valueOf(Double.valueOf(carActual.getNetWeight()) / 1000 + Double.valueOf(mMeasureTEntity.getDeduction())));
                }
                if ("废钢料场".equals(carActual.getReceivingUintName()) && !StringUtils.isEmpty(carActual.getMinusManName())) {
                    mMeasureTEntity.setSuttle(String.valueOf((Double.valueOf(carActual.getNetWeight()) - Double.valueOf(carActual.getImpurityWgt()) - Double.valueOf(carActual.getCutWgt())) / 1000));
                }
                mMeasureTEntity.setSuttletime(carActual.getNetTime());
                mMeasureTEntity.setSuttleweighid(codeMap.get(carActual.getNetSpot3No()));
                mMeasureTEntity.setSuttleweigh(nameMap.get(carActual.getNetSpot3Name()));
                mMeasureTEntity.setSuttleweighgroup("");
                mMeasureTEntity.setSuttleoperatorid(carActual.getNetManNo());
                mMeasureTEntity.setSuttleoperator(carActual.getNetManName());

                mMeasureTEntity.setSysmemo(carActual.getMemo());
                mMeasureTEntity.setValidstate("1");
                mMeasureTEntity.setValidstatetime(carActual.getUpdateTime());
                mMeasureTEntity.setValidstateoperator(carActual.getUpdateManName());
                mMeasureTEntity.setAddtime(carActual.getCheckTime());
                mMeasureTEntity.setRecordtype("1");
                mMeasureTEntity.setTcode(carActual.getCarrierUnitNo());
                mMeasureTEntity.setTname(carActual.getCarrierUnitName());

                mMeasureTEntity.setMaterialcount(carActual.getMeterNum());
                mMeasureTEntity.setSysmemo("新计量同步");

                QSteelsTEntity qSteelsTEntity = new QSteelsTEntity();
                qSteelsTEntity.setMatchid(mMeasureTEntity.getMatchid());

                // 作废删除数据
                if("1".equals(carActual.getValueFlag())){
                    // 删除老计量数据
                    // 计量
                    MMeasureTEntity measure = limsDataService.getMeasure(mMeasureTEntity);
                    if(measure!=null){
                        limsDataService.deleteMeasure(measure);
                    }
                    // 扣杂
                    QSteelsTEntity steels = limsDataService.getSteels(qSteelsTEntity);
                    if(steels != null){
                        limsDataService.deleteSteels(steels);
                    }
                    // 修改新计量状态
                    carActual.setInterRead("3");
                    limsDataService.updateCarActual(carActual);
                    continue;
                }
                // 铁水料蓝
                if("001006006".equals(carActual.getMeterTypeNo()) || "001006007".equals(carActual.getMeterTypeNo())){
                    mMeasureTEntity.setOldmatchid(carActual.getActualFirst1No()+"-1");
                    MMeasureTEntity measure = limsDataService.getMeasure(mMeasureTEntity);
                    if(measure == null){
                        mMeasureTEntity.setSysmemo("新计量同步新增铁水料蓝");
                        // 添加旧计量数据
                        limsDataService.insertMeasure(mMeasureTEntity);
                    }
                    else {
                        mMeasureTEntity.setSysmemo("新计量同步修改铁水料蓝");
                        // 修改旧计量数据
                        limsDataService.updateMeasure(mMeasureTEntity);
                    }

                } else {
                        mMeasureTEntity.setOldmatchid(mMeasureTEntity.getMatchid());
                        MMeasureTEntity measure = limsDataService.getMeasure(mMeasureTEntity);
                        if(measure!=null){
                            // 删除旧计量数据
                            limsDataService.deleteMeasure(measure);
                        }
                        // 添加旧计量数据
                        limsDataService.insertMeasure(mMeasureTEntity);
                }

                // 扣杂 扣超长料 扣机铁 扣轻薄料   写老计量
                if(!StringUtils.isEmpty(carActual.getJudgeTypeName())
                        || (("废钢料场".equals(carActual.getReceivingUintName()) || "废钢料场".equals(carActual.getLoadPointName())) && !StringUtils.isEmpty(carActual.getMinusManName()))
                ){
                    qSteelsTEntity.setMaterielRule(carActual.getJudgeTypeName());
                    qSteelsTEntity.setDeduction1(String.valueOf((Double.valueOf(carActual.getImpurityWgt()) + Double.valueOf(carActual.getCutWgt())) / 1000));
                    qSteelsTEntity.setDeduction2(String.valueOf(Double.valueOf(carActual.getFeWgt()) / 1000));
                    qSteelsTEntity.setDeduction3(String.valueOf(Double.valueOf(carActual.getLenWgt()) / 1000));
                    qSteelsTEntity.setDeduction4(String.valueOf(Double.valueOf(carActual.getLightWgt()) / 1000));
                    qSteelsTEntity.setCheckOperator1(carActual.getValidMan());
                    qSteelsTEntity.setSpaces(carActual.getUnloadAddrName());
                    qSteelsTEntity.setSpacetype("卸料");
//                    qSteelsTEntity.setCheckMemo(carActual.getMeme2());
                    qSteelsTEntity.setCheckMemo(carActual.getCheckMemo() + " " + carActual.getMeme2());
                    qSteelsTEntity.setAddtime(carActual.getMinusTime());
                    qSteelsTEntity.setAddoperatorid(carActual.getMinusManNo());
                    qSteelsTEntity.setAddoperator(carActual.getMinusManName());
                    qSteelsTEntity.setCanceled("1");
                    qSteelsTEntity.setDownunit(carActual.getUnloadUnitName());
                    qSteelsTEntity.setMateriel(carActual.getMatterName());
                    qSteelsTEntity.setSysFlag("1");

                    // 查询老系统是否存在数据
                    QSteelsTEntity steels = limsDataService.getSteels(qSteelsTEntity);
                    if(steels != null){
                        limsDataService.deleteSteels(steels);
                    }
                    limsDataService.insertSteels(qSteelsTEntity);
                }

                // 修改新计量状态
                carActual.setInterRead("1");
                limsDataService.updateCarActual(carActual);
            } catch (Exception e) {
                logger.error("计量同步失败！ " + mMeasureTEntity.getMatchid() + " :" + e.getMessage());
                carActual.setInterRead("2");

                limsDataService.updateCarActual(carActual);
            }

        }
    }

    private void LimsActualFirstSync( LimsDataService limsDataService){
        // 查询新计量数据
        ArrayList<MeterWorkCarActualFirstEntity> carActuals = limsDataService.getCarActualFirst();
        if (carActuals == null || carActuals.size() <= 0) {
            return;
        }
        for (MeterWorkCarActualFirstEntity carActual : carActuals) {
            MMeasureTEntity mMeasureTEntity = new MMeasureTEntity();

            //验配ID
            mMeasureTEntity.setMatchid(carActual.getActualFirstNo() + "-1");
            mMeasureTEntity.setIcno(carActual.getBindCardNo());
            mMeasureTEntity.setCarno(carActual.getCarNo());
            mMeasureTEntity.setCartype("C");

            mMeasureTEntity.setOperatype("0");
            mMeasureTEntity.setTaskcode("0");

            mMeasureTEntity.setOrderno(carActual.getContractNo());
            mMeasureTEntity.setMaterialcode(carActual.getMatterNo());
            mMeasureTEntity.setMaterialname(carActual.getMatterName());
            mMeasureTEntity.setMaterialrule(carActual.getSpecName());
            mMeasureTEntity.setMaterialquality("");
            mMeasureTEntity.setSourcecode(carActual.getForwardingUnitNo());
            mMeasureTEntity.setSourcename(carActual.getForwardingUnitName());
            mMeasureTEntity.setTargetcode(carActual.getReceivingUintNo());
            mMeasureTEntity.setTargetname(carActual.getReceivingUintName());
            mMeasureTEntity.setBasket(carActual.getAddWgtNo());

            // 毛重
            mMeasureTEntity.setGross(String.valueOf((Double.valueOf(carActual.getMeterWeight()) / 1000) -  (Double.valueOf(carActual.getAddWeight()) / 1000)));
            mMeasureTEntity.setGrossweighid(codeMap.get(carActual.getBaseSpotNo()));
            mMeasureTEntity.setGrossweigh(nameMap.get(carActual.getBaseSpotName()));
            mMeasureTEntity.setGrosstime(carActual.getCreateTime());

            // 皮重
            mMeasureTEntity.setTare("0");
            mMeasureTEntity.setTaretime(carActual.getCreateTime());

            // 净重
            mMeasureTEntity.setSuttle("0");
            mMeasureTEntity.setSuttletime(carActual.getCreateTime());

            // 扣重
            mMeasureTEntity.setDeduction("0");
            mMeasureTEntity.setDeductiontime(carActual.getCreateTime());

            mMeasureTEntity.setValidstate("1");
            mMeasureTEntity.setValidstatetime(carActual.getUpdateTime());
            mMeasureTEntity.setValidstateoperator(carActual.getUpdateManName());
            mMeasureTEntity.setAddtime(carActual.getCheckTime());
            mMeasureTEntity.setRecordtype("1");
            mMeasureTEntity.setTcode(carActual.getCarrierUnitNo());
            mMeasureTEntity.setTname(carActual.getCarrierUnitName());

            mMeasureTEntity.setMaterialcount(carActual.getMeterNum());
            mMeasureTEntity.setSysmemo("新计量一次同步");


            try {
                if("0".equals(carActual.getValueFlag())){
                    limsDataService.deleteMeasure(mMeasureTEntity);
                } else {
                    // 添加旧计量数据
                    limsDataService.insertMeasure(mMeasureTEntity);
                }
                // 修改新计量状态
                carActual.setLimsSamplingFlag("1");
                limsDataService.updateCarActualFirst(carActual);
            } catch (Exception e) {
                logger.error("计量同步失败！ " + mMeasureTEntity.getMatchid() + " :" + e.getMessage());
                carActual.setLimsSamplingFlag("2");
                limsDataService.updateCarActualFirst(carActual);
            }
        }
    }

}
