package com.steering.ydm.bc;

import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.Demand.entity.MatGpMEntity;
import com.steering.comm.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;



public class writeDemand extends DBCommon {
    /***\
     *倒运申请用车需求
     * @param matBcMEntity
     * @param userName
     * @param tmsCon
     * @param con
     * @return
     */
    public static  String writeOutDemand(MatBcMEntity matBcMEntity, String userName, String tarStorageNo, String tarStorageName, Connection tmsCon, Connection con,String flag,String demandUserCode,String demandUser,String unLoadCode,String unLoadDesc,String trainType) throws SQLException {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat df1 = new SimpleDateFormat("yyyyMM");
        String systime = dateFormat.format(now);
        String msg = "";
        String demandNoXx = "";//下线需求号
        String maxExSeqx = "";//下线最大序号

        String storageType = "";//物料所在库类型
        String tarStorageType = "";//目标库库类型

        PreparedStatement psta = null;

        String carNum = "";//车数
        String[] startStorageNoUn = null;//下线运输起点(装卸点)
        String[] endStorageNoUn = new String[2];//下线运输终点(装卸点)

        storageType = getStorageType(matBcMEntity.getStorageNo(),con);
        tarStorageType = getStorageType(tarStorageNo,con);

        if(matBcMEntity.getActLen() == null || matBcMEntity.getActLen().equals(""))
        {
            matBcMEntity.setActLen("0");
        }
        carNum = getCarNum(matBcMEntity.getActCount().toString(),matBcMEntity.getActWeight().toString(),matBcMEntity.getActDimater().toString(),matBcMEntity.getActLen().toString());

        if(matBcMEntity.getStorageNo().equals(""))
        {
            msg = "该炉已在运输途中不允许申请用车!";
            return  msg;
        }
        startStorageNoUn =getStorageNoUn(matBcMEntity.getStorageNo(),tmsCon);
        if(flag.equals("1"))
        {
            endStorageNoUn[0] = unLoadCode;
            endStorageNoUn[1] = unLoadDesc;
        }else
        {
            endStorageNoUn = getStorageNoUn(tarStorageNo,tmsCon);
        }

        if(trainType.equals("0"))
        {
            if(matBcMEntity.getQcmStaus().equals("22"))
            {
                demandNoXx = getDemandNo(tmsCon, "FP");
                maxExSeqx = getDemandSeq(tmsCon, "FP");
            }else
            {
                demandNoXx = getDemandNo(tmsCon, "DK");
                maxExSeqx = getDemandSeq(tmsCon, "DK");
            }
        }else
        {
            demandNoXx = getDemandNo(tmsCon, "FY");
            maxExSeqx = getDemandSeq(tmsCon, "FY");
        }

        TmsDemandMEntity tmsDemandMEntity = new TmsDemandMEntity();
        tmsDemandMEntity.setDemandNo(demandNoXx);
        if(matBcMEntity.getQcmStaus().equals("22") && !matBcMEntity.getStorageNo().equals("LDC1"))
        {
            tmsDemandMEntity.setDemandDesc(demandUser + "倒运需求");
            //tmsDemandMEntity.setDemandDesc(matBcMEntity.getPlineName() + "倒运需求");
        }else
        {
            if(flag.equals("0"))
            {
                tmsDemandMEntity.setDemandDesc(matBcMEntity.getStorageName() + "倒运需求");
            }
            else
            {
                tmsDemandMEntity.setDemandDesc(demandUser + "倒运需求");
            }
        }

        tmsDemandMEntity.setExecuteSqe(maxExSeqx);
        tmsDemandMEntity.setUrgencyTypeCode("L1");
        tmsDemandMEntity.setUrgencyTypeDesc("*");
        if(trainType.equals("0"))
        {
            tmsDemandMEntity.setTranClassCode("DK");
            tmsDemandMEntity.setTranClassDesc("倒库运输");
        }
        else
        {
            tmsDemandMEntity.setTranClassCode("FY");
            tmsDemandMEntity.setTranClassDesc("取复样运输");
        }
        tmsDemandMEntity.setCostTypeCode("");//计费类型代码
        tmsDemandMEntity.setCostTypeDesc("");
        tmsDemandMEntity.setCarrierCode("");
        tmsDemandMEntity.setCarrierName("");//承人人
        tmsDemandMEntity.setStartNodeCode(startStorageNoUn[0].toString());
        tmsDemandMEntity.setStartNodeDesc(startStorageNoUn[1].toString());
        tmsDemandMEntity.setEndNodeCode(endStorageNoUn[0].toString());
        tmsDemandMEntity.setEndNodeDesc(endStorageNoUn[1].toString());
        tmsDemandMEntity.setStartAreaCode(matBcMEntity.getStorageNo());
        tmsDemandMEntity.setStartAreaDesc(matBcMEntity.getStorageName());
        tmsDemandMEntity.setEndAreaCode(tarStorageNo);
        tmsDemandMEntity.setEndAreaDesc(tarStorageName);
        if(matBcMEntity.getQcmStaus().equals("22"))
        {
//            if(matBcMEntity.getStorageNo().equals("LDC1"))
//            {
//                tmsDemandMEntity.setDemandOrgCode(demandUserCode);
//                tmsDemandMEntity.setDemandOrgDesc(demandUser);
//            }else
//            {
//                tmsDemandMEntity.setDemandOrgCode(matBcMEntity.getPlineCode());
//                tmsDemandMEntity.setDemandOrgDesc(matBcMEntity.getPlineName());
//            }
            tmsDemandMEntity.setDemandOrgCode(demandUserCode);
            tmsDemandMEntity.setDemandOrgDesc(demandUser);

            tmsDemandMEntity.setTranClassCode("FP");
            tmsDemandMEntity.setTranClassDesc("废品运输");
        }else
        {
            if(flag.equals("1"))
            {
                tmsDemandMEntity.setDemandOrgCode(demandUserCode);
                tmsDemandMEntity.setDemandOrgDesc(demandUser);
            }else
            {
                tmsDemandMEntity.setDemandOrgCode(matBcMEntity.getStorageNo());
                tmsDemandMEntity.setDemandOrgDesc(matBcMEntity.getStorageName());
            }
        }

        tmsDemandMEntity.setTransportStartTime(systime);
        tmsDemandMEntity.setTransportEndTime("");
        tmsDemandMEntity.setCarUseCode("SC");
        tmsDemandMEntity.setCarUseDesc("生产用车");
        tmsDemandMEntity.setCarTypeCode("CAR01");
        tmsDemandMEntity.setCarTypeDesc("拖车");
        tmsDemandMEntity.setCarWtVal("30");
        tmsDemandMEntity.setCalculateCarVal(carNum);//车数

        tmsDemandMEntity.setWeighingIf("0");
        tmsDemandMEntity.setHeatNo(matBcMEntity.getJudgeStoveNo());
        tmsDemandMEntity.setBatchNo(matBcMEntity.getBatchNo());
        tmsDemandMEntity.setLogoNo(matBcMEntity.getFlagStoveNoZg());
        tmsDemandMEntity.setGroupNo(matBcMEntity.getBatchGroudNo());
        tmsDemandMEntity.setMaterialsCode(matBcMEntity.getMaterialNo());
        tmsDemandMEntity.setMaterialsDesc(matBcMEntity.getMaterialName());
        tmsDemandMEntity.setMaterialsSpec(matBcMEntity.getSpecName());
        tmsDemandMEntity.setDimaterVal(matBcMEntity.getActDimater());
        tmsDemandMEntity.setHeightVal(matBcMEntity.getActHeight());
        tmsDemandMEntity.setLenVal("");
        tmsDemandMEntity.setLenMinVal(matBcMEntity.getActLenMin());
        tmsDemandMEntity.setLenMaxVal(matBcMEntity.getActLenMax());
        tmsDemandMEntity.setUomCode("");
        tmsDemandMEntity.setUomDesc("");
        tmsDemandMEntity.setDemandQtyVal(matBcMEntity.getActCount());
        tmsDemandMEntity.setDemandWtVal(matBcMEntity.getActWeight());
        tmsDemandMEntity.setProOrderNo("");
        tmsDemandMEntity.setProOrderSqe("");
        tmsDemandMEntity.setCurrentJobNo(matBcMEntity.getHeatPlanNo());
        tmsDemandMEntity.setPreviousJobNo("");
        tmsDemandMEntity.setStatus("20");
        tmsDemandMEntity.setCreateName(userName);
        tmsDemandMEntity.setCreateTime(systime);
        tmsDemandMEntity.setIsSure("1");
        tmsDemandMEntity.setLotNo(matBcMEntity.getLotNo());
        tmsDemandMEntity.setMatType("1");

        String insertDemandxSql = CoreXmlSqlParsersFactory.getSql("writeDemand.insertDemand");
        insertDemandxSql = EntityTool.fillSqlByEntity(insertDemandxSql, tmsDemandMEntity);
        psta = tmsCon.prepareStatement(insertDemandxSql);
        psta.executeUpdate();
        psta.close();

        TmsComLineMEntity tmsComLineEntity  = new TmsComLineMEntity();
        tmsComLineEntity.setLineDesc(startStorageNoUn[1]+"->"+endStorageNoUn[1]);
        tmsComLineEntity.setStartNodeCode(startStorageNoUn[0]);
        tmsComLineEntity.setStartNodeDesc(startStorageNoUn[1]);
        tmsComLineEntity.setEndNodeCode(endStorageNoUn[0]);
        tmsComLineEntity.setEndNodeDesc(endStorageNoUn[1]);
        tmsComLineEntity.setStartLatitudeVal("1");
        tmsComLineEntity.setStartLongitudeVal("1");
        tmsComLineEntity.setCreateName("admin");
        tmsComLineEntity.setCreateTime(systime);
        tmsComLineEntity.setCreateUserid("admin");
        tmsComLineEntity.setSegmentDesc(tmsComLineEntity.getLineDesc());

        String insertTmsLineSql = CoreXmlSqlParsersFactory.getSql("writeDemand.insertComLine");
        insertTmsLineSql = EntityTool.fillSqlByEntity(insertTmsLineSql,tmsComLineEntity);
        psta = tmsCon.prepareStatement(insertTmsLineSql);
        psta.executeUpdate();
        psta.close();
        return  msg;
    }


    /***\
     *倒运申请用车需求(管坯)
     * @param matBcMEntity
     * @param userName
     * @param tmsCon
     * @param con
     * @return
     */
    public static  String writeOutDemandGp(MatGpMEntity matBcMEntity, String userName, String tarStorageNo, String tarStorageName, Connection tmsCon, Connection con, String demandUserCode, String demandUser) throws SQLException {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat df1 = new SimpleDateFormat("yyyyMM");
        String systime = dateFormat.format(now);
        String msg = "";
        String demandNoXx = "";//下线需求号
        String maxExSeqx = "";//下线最大序号

        String storageType = "";//物料所在库类型
        String tarStorageType = "";//目标库库类型

        PreparedStatement psta = null;

        String carNum = "";//车数
        String[] startStorageNoUn = null;//下线运输起点(装卸点)
        String[] endStorageNoUn = null;//下线运输终点(装卸点)

        storageType = getStorageType(matBcMEntity.getStorageNo(),con);
        tarStorageType = getStorageType(tarStorageNo,con);

        carNum = getCarNum(matBcMEntity.getActCount().toString(),matBcMEntity.getActWeight().toString(),matBcMEntity.getDimater().toString(),matBcMEntity.getActLen().toString());

        startStorageNoUn =getStorageNoUn(matBcMEntity.getStorageNo(),tmsCon);
        endStorageNoUn = getStorageNoUn(tarStorageNo,tmsCon);
        if(matBcMEntity.getQcmStaus().equals("22"))
        {
            demandNoXx = getDemandNo(tmsCon, "FP");
            maxExSeqx = getDemandSeq(tmsCon, "FP");
        }else
        {
            demandNoXx = getDemandNo(tmsCon, "DK");
            maxExSeqx = getDemandSeq(tmsCon, "DK");
        }
        TmsDemandMEntity tmsDemandMEntity = new TmsDemandMEntity();
        tmsDemandMEntity.setDemandNo(demandNoXx);

        tmsDemandMEntity.setDemandDesc(demandUser + "倒运需求");
        tmsDemandMEntity.setExecuteSqe(maxExSeqx);
        tmsDemandMEntity.setUrgencyTypeCode("L1");
        tmsDemandMEntity.setUrgencyTypeDesc("*");
        tmsDemandMEntity.setTranClassCode("DK");
        tmsDemandMEntity.setTranClassDesc("倒库运输");
        tmsDemandMEntity.setCostTypeCode("");//计费类型代码
        tmsDemandMEntity.setCostTypeDesc("");
        tmsDemandMEntity.setCarrierCode("");
        tmsDemandMEntity.setCarrierName("");//承人人
        tmsDemandMEntity.setStartNodeCode(startStorageNoUn[0]);
        tmsDemandMEntity.setStartNodeDesc(startStorageNoUn[1]);
        tmsDemandMEntity.setEndNodeCode(endStorageNoUn[0]);
        tmsDemandMEntity.setEndNodeDesc(endStorageNoUn[1]);
        tmsDemandMEntity.setStartAreaCode(matBcMEntity.getStorageNo());
        tmsDemandMEntity.setStartAreaDesc(matBcMEntity.getStorageName());
        tmsDemandMEntity.setEndAreaCode(tarStorageNo);
        tmsDemandMEntity.setEndAreaDesc(tarStorageName);

        tmsDemandMEntity.setDemandOrgCode(demandUserCode);
        tmsDemandMEntity.setDemandOrgDesc(demandUser);
        tmsDemandMEntity.setTransportStartTime(systime);
        tmsDemandMEntity.setTransportEndTime("");
        tmsDemandMEntity.setCarUseCode("SC");
        tmsDemandMEntity.setCarUseDesc("生产用车");
        tmsDemandMEntity.setCarTypeCode("CAR01");
        tmsDemandMEntity.setCarTypeDesc("拖车");
        tmsDemandMEntity.setCarWtVal("30");
        tmsDemandMEntity.setCalculateCarVal(carNum);//车数

        tmsDemandMEntity.setWeighingIf("0");
        tmsDemandMEntity.setHeatNo(matBcMEntity.getJudgeStoveNo());
        if(matBcMEntity.getFlagStoveNo().equals(""))
        {
            tmsDemandMEntity.setLogoNo(matBcMEntity.getJudgeStoveNo());
        }else
        {
            tmsDemandMEntity.setLogoNo(matBcMEntity.getFlagStoveNo());
        }


        tmsDemandMEntity.setMaterialsCode(matBcMEntity.getMaterialNo());
        tmsDemandMEntity.setMaterialsDesc(matBcMEntity.getMaterialName());
        tmsDemandMEntity.setMaterialsSpec(matBcMEntity.getDimater());
        tmsDemandMEntity.setDimaterVal(matBcMEntity.getDimater());
        tmsDemandMEntity.setLenVal("");
        tmsDemandMEntity.setLenMinVal(matBcMEntity.getActLen());
        tmsDemandMEntity.setLenMaxVal(matBcMEntity.getActLen());
        tmsDemandMEntity.setUomCode("");
        tmsDemandMEntity.setUomDesc("");
        tmsDemandMEntity.setDemandQtyVal(matBcMEntity.getActCount());
        tmsDemandMEntity.setDemandWtVal(matBcMEntity.getActWeight());
        tmsDemandMEntity.setProOrderNo("");
        tmsDemandMEntity.setProOrderSqe("");
        tmsDemandMEntity.setCurrentJobNo(matBcMEntity.getFurnaceno()+"/"+matBcMEntity.getSequenceno());
        tmsDemandMEntity.setPreviousJobNo("");
        tmsDemandMEntity.setStatus("20");
        tmsDemandMEntity.setCreateName(userName);
        tmsDemandMEntity.setCreateTime(systime);
        tmsDemandMEntity.setIsSure("0");
        tmsDemandMEntity.setLotNo("");
        tmsDemandMEntity.setMatType("2");

        String insertDemandxSql = CoreXmlSqlParsersFactory.getSql("writeDemand.insertDemand");
        insertDemandxSql = EntityTool.fillSqlByEntity(insertDemandxSql, tmsDemandMEntity);
        psta = tmsCon.prepareStatement(insertDemandxSql);
        psta.executeUpdate();
        psta.close();

        TmsComLineMEntity tmsComLineEntity  = new TmsComLineMEntity();
        tmsComLineEntity.setLineDesc(startStorageNoUn[1]+"->"+endStorageNoUn[1]);
        tmsComLineEntity.setStartNodeCode(startStorageNoUn[0]);
        tmsComLineEntity.setStartNodeDesc(startStorageNoUn[1]);
        tmsComLineEntity.setEndNodeCode(endStorageNoUn[0]);
        tmsComLineEntity.setEndNodeDesc(endStorageNoUn[1]);
        tmsComLineEntity.setStartLatitudeVal("1");
        tmsComLineEntity.setStartLongitudeVal("1");
        tmsComLineEntity.setCreateName("admin");
        tmsComLineEntity.setCreateTime(systime);
        tmsComLineEntity.setCreateUserid("admin");
        tmsComLineEntity.setSegmentDesc(tmsComLineEntity.getLineDesc());

        String insertTmsLineSql = CoreXmlSqlParsersFactory.getSql("writeDemand.insertComLine");
        insertTmsLineSql = EntityTool.fillSqlByEntity(insertTmsLineSql,tmsComLineEntity);
        psta = tmsCon.prepareStatement(insertTmsLineSql);
        psta.executeUpdate();
        psta.close();
        return  msg;
    }

    /***
     * 计算车数
     * @param
     * @param plnOutsingleNum
     * @param plnOutsingleW
     * @return
     */
    private static String getCarNum(String plnOutsingleNum, String plnOutsingleW,String dimater,String len) {

        String num = "";
        double lenMax = 13.0;//长度
        double wgtMax = 30;//最大载重
        double height = 2.6;//高度
        double width = 2.5;//宽度
        double  carNum = 0;//按体积估算车数
        double  carNumW = 0;//按载重估算车数

        double carArea = lenMax * width*height;//车体体积
        double tubeArea = 3.14*(Double.parseDouble(dimater)/1000/2)*(Double.parseDouble(dimater)/1000/2)*Double.parseDouble(len);//单支管体积
        double numMax =Math.ceil(carArea/tubeArea);//车最多装的支数


        double numMaxW = wgtMax/(Double.parseDouble(plnOutsingleW)/Double.parseDouble(plnOutsingleNum));//最大载重时所装支数
        double bulkMaxW = numMaxW * 3.14*(Double.parseDouble(dimater)/1000/2)*(Double.parseDouble(dimater)/1000/2)*Double.parseDouble(len);

        //最大载重未超体积时
        if(bulkMaxW < carArea)
        {
            num = String.valueOf(Math.round(Math.ceil(Double.parseDouble(plnOutsingleW)/wgtMax)));
        }else
        {
            //限制体积所得车数
            num = String.valueOf(Math.round(Math.ceil(Integer.parseInt(plnOutsingleNum)/numMax)));
        }
        return  num;
    }



    /***
     * 获取需求号
     * @param tmsCon
     * @return
     */
    private static String getDemandNo(Connection tmsCon, String typeCode) throws SQLException {
        String demandNo = "";
        String sql  = "";
        PreparedStatement ps = null;
        ResultSet rs = null;
        sql = CoreXmlSqlParsersFactory.getSql("writeDemand.getDemandNo");
        ps = tmsCon.prepareStatement(sql);
        ps.setString(1,typeCode);
        ps.setString(2,typeCode);
        rs = ps.executeQuery();
        if(rs.next())
        {
            demandNo = rs.getString("DEMAND_NO");
        }
        rs.close();
        ps.close();
        return  demandNo;
    }

    /***
     * 取最大执行顺序号
     * @param tmsCon
     * @param
     * @return
     */
    private static String getDemandSeq(Connection tmsCon, String typeCode) throws SQLException {
        String seq = "";
        String sql  = "";
        PreparedStatement ps = null;
        ResultSet rs = null;
        sql = CoreXmlSqlParsersFactory.getSql("writeDemand.getDemandSeq");
        ps = tmsCon.prepareStatement(sql);
        ps.setString(1,typeCode);
        rs = ps.executeQuery();
        if(rs.next())
        {
            seq = rs.getString("EXECUTE_SQE");
        }
        rs.close();
        ps.close();
        return  seq;
    }
    /***
     * 获取对应仓库的装卸点
     * @param storageNo
     * @param tmsCon
     * @return
     */
    public static String[] getStorageNoUn(String storageNo,Connection tmsCon) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        String[] storageNoUn = new String[2];
        String sql = CoreXmlSqlParsersFactory.getSql("writeDemand.getStorageNoUn");
        ps = tmsCon.prepareStatement(sql);
        ps.setString(1,storageNo);
        rs = ps.executeQuery();
        if(rs.next())
        {
            storageNoUn[0] = rs.getString("UNLOADING_CODE");
            storageNoUn[1] =  rs.getString("UNLOADING_DESC");
        }
        rs.close();
        ps.close();
        return  storageNoUn;
    }

    /***
     * 获取库类型
     * @param storageNo
     * @param con
     * @return
     * @throws SQLException
     */
    public  static String  getStorageType(String storageNo,Connection con) throws SQLException {
        String storageType = "";
        String sql  = "";
        PreparedStatement ps = null;
        ResultSet rs = null;
        sql = CoreXmlSqlParsersFactory.getSql("writeOutDemand.selectStorageType");
        ps = con.prepareStatement(sql);
        ps.setString(1,storageNo);
        rs = ps.executeQuery();
        if(rs.next())
        {
            storageType = rs.getString("STORAGE_ATTR");
        }
        rs.close();
        ps.close();
        return  storageType;
    }

    /***
     * 撤销申请用车运输需求
     * @param tmsDemandEntity
     * @param userName
     * @return
     */
    public static  String deleteDemand(TmsDemandMEntity tmsDemandEntity,String userName,Connection tmsCon) throws SQLException {
        String msg = "";
        PreparedStatement psta = null;
        String userNameDemand = "";
        String status= "";
        String sql = CoreXmlSqlParsersFactory.getSql("writeOutDemand.SelectDemand");
        sql = EntityTool.fillSqlByEntity(sql,tmsDemandEntity);
        psta = tmsCon.prepareStatement(sql);
        ResultSet rs = psta.executeQuery();
        if(rs.next())
        {
            userNameDemand = rs.getString("CREATE_NAME");
            status = rs.getString("VALIDFLAG");
        }
        rs.close();;
        psta.close();
        if(!status.equals("20"))
        {
            msg = "用车需求不是待分派状态,不允许撤销!";
            return  msg;
        }
        if(!userNameDemand.equals(userName))
        {
            msg = "不是当前用户申请的用车需求,不允许撤销!";
            return  msg;
        }

        String deleteDemandSql = CoreXmlSqlParsersFactory.getSql("writeOutDemand.deleteDemand");
        deleteDemandSql = EntityTool.fillSqlByEntity(deleteDemandSql,tmsDemandEntity);
        psta = tmsCon.prepareStatement(deleteDemandSql);
        psta.executeUpdate();
        psta.close();
        return  msg;
    }
}
