package com.steering.Demand.sever;


import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.Demand.entity.MatGpMEntity;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.MatBcMEntity;
import com.steering.ydm.bc.writeDemand;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class FrmBcM  extends DBCommon{

    /***
     * 查询半成品库存
     * @return
     */
    public CoreReturnObject doQuery(ArrayList<String> parmList,String[] arr)
    {
        String sql = CoreXmlSqlParsersFactory.getSql("FrmBcM.doQuery");
        if(!parmList.get(0).equals(""))
        {
            String str = CoreConverter.toSqlConditionString("T.JUDGE_STOVE_NO",parmList.get(0).replace("，",",").split(","));
            sql = sql.replace("#JUDGE_STOVE_NO#",str);
            //sql += CoreConverter.toSqlConditionString("T.JUDGE_STOVE_NO",parmList.get(0).replace("，",",").split(","));

        }else
        {
            sql = sql.replace("#JUDGE_STOVE_NO#","");
        }
        if(!parmList.get(1).equals(""))
        {
            String str1 = CoreConverter.toSqlConditionString("T.FLAG_STOVE_NO_ZG",parmList.get(1).replace("，",",").split(","));
            sql = sql.replace("#FLAG_STOVE_NO_ZG#",str1);
            //sql += CoreConverter.toSqlConditionString("T.FLAG_STOVE_NO_ZG",parmList.get(1).replace("，",",").split(","));
        }else
        {
            sql = sql.replace("#FLAG_STOVE_NO_ZG#","");
        }
        //sql += CoreConverter.toSqlConditionString("T.PLINE_CODE",arr);
        String str2= " GROUP BY T.PRODUCT_FLAG,T.LOT_NO,T.JUDGE_STOVE_NO,T.BATCH_NO,T.STOVE_NO,T.FLAG_STOVE_NO_ZG,";
        str2 += " T.PRO_ORDER_ID,T.QCM_STAUS,S.STORAGE_NO,T.MATERIAL_NO,T.MATERIAL_NAME,T.HEAT_PLAN_NO,";
        str2 += " T.PROCESS_CDOE,T.PROCESS_DESC,T.PLINE_CODE,T.PLINE_NAME";
        sql = sql.replace("#GROUP#",str2);
        return  this.getData(sql);
    }


    /***
     * 查询所属产线的需求单位
     * @param plineCode
     * @return
     */
    public CoreReturnObject getDemandUser(String[] plineCode)
    {
        String sql = CoreXmlSqlParsersFactory.getSql("FrmBcM.getDemandUser");
        sql += CoreConverter.toSqlConditionString("T.PLINE_CODE",plineCode);
        return  this.getData(sql);
    }

    /***
     * 查询所属仓库的装卸点
     * @param storageNo
     * @return
     */
    //TZH 24.4.24 注销物流接口
    /*
    public CoreReturnObject getUnLoad(String storageNo)
    {
        return this.getDataBySqlID("TmsDao","FrmBcM.getUnLoad",new Object[]{storageNo});
    }
     */
    
    
    /***
     * 生成用车需求
     * @param parmList
     * @param userName
     * @param tarStorageNo
     * @param tarStorageName
     * @param valueFlag
     * @return
     */
    //TZH 24.4.24 注销物流接口
    /*
    public CoreReturnObject doWriteDemand(ArrayList<String> parmList,String userName,String tarStorageNo,String tarStorageName,String valueFlag,String demandUserCode,String demandUser,String unloadCode,String unloadDesc,String trainType)
    {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        Connection tmsCon = this.getDao("TmsDao").getConnection();

        int acount = 0;
        String msg = "";
        String _tarStorageNo = tarStorageNo;
        String _tarStorageName = tarStorageName;
        try {

            for(String str : parmList)
            {
                MatBcMEntity matBcEntity = EntityTool.getEntityByJSONStr(str,
                        MatBcMEntity.class);
                if (matBcEntity.getQcmStaus().equals("合格")) {
                    matBcEntity.setQcmStaus("00");
                } else if (matBcEntity.getQcmStaus().equals("待处理")) {
                    matBcEntity.setQcmStaus("11");
                } else if (matBcEntity.getQcmStaus().equals("判废")) {
                    matBcEntity.setQcmStaus("22");
                }
                if(valueFlag.equals("1"))
                {
                    msg = writeDemand.writeOutDemand(matBcEntity,userName,tarStorageNo,tarStorageName,tmsCon,connection,"1",demandUserCode,demandUser,unloadCode,unloadDesc,trainType);

                    tarStorageNo = matBcEntity.getStorageNo();
                    tarStorageName = matBcEntity.getStorageName();
                    matBcEntity.setStorageNo(_tarStorageNo);
                    matBcEntity.setStorageName(_tarStorageName);
                    msg = writeDemand.writeOutDemand(matBcEntity,userName,tarStorageNo,tarStorageName,tmsCon,connection,"1",demandUserCode,demandUser,unloadCode,unloadDesc,trainType);

                }else
                {
                     msg = writeDemand.writeOutDemand(matBcEntity,userName,tarStorageNo,tarStorageName,tmsCon,connection,"1",demandUserCode,demandUser,unloadCode,unloadDesc,trainType);
                }

                if(!msg.equals("")) {
                    acount = acount + 1;
                }
            }



            if(acount >0)
            {
                cro.setV_errMsg("生成用车需求出错!");
            }else
            {
                cro.setV_errMsg("生成用车需求成功!");
            }
            return  cro;

        } catch (Exception e) {
            System.out.println(e.getMessage());
            if (connection != null||tmsCon !=null) {
                try {
                    cro.setV_errMsg(e.getMessage());
                    connection.rollback(); // 回滚事务
                    tmsCon.rollback();
                } catch (Exception e2) {
                }
            }
        } finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
                if (tmsCon != null && !tmsCon.isClosed()) {
                    tmsCon.close();
                }
            } catch (Exception e2) {
            }
        }
        return cro;
    }
    */

    /***
     * 查询重量
     * @return
     */
    public CoreReturnObject doQueryWeight(String matBc)
    {
        MatBcMEntity matBcEntity = EntityTool.getEntityByJSONStr(matBc, MatBcMEntity.class);
        if(matBcEntity.getQcmStaus().equals("合格"))
        {
            matBcEntity.setQcmStaus("00");
        }
        if(matBcEntity.getActCount().equals(""))
        {
            matBcEntity.setActCount("0");
        }
        String sql = CoreXmlSqlParsersFactory.getSql("FrmBcM.doQueryWeight");
        sql = EntityTool.fillSqlByEntity(sql, matBcEntity);
        return this.getData(sql);
    }
}
