package com.steering.pss.ydm.pipemanage;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import CoreFS.Util.StringUtils;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.data.ComBaseInfo;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.sale.order.qualityDesign.QualityDesignTool;
import com.steering.pss.ydm.base.MeanValueCalculation;
import com.steering.pss.ydm.entity.OYdmZcStorcktakinglistEntity;
import com.steering.pss.ydm.entity.YdmBsStorageEntity;
import com.steering.pss.ydm.entity.YdmZcInlistEntity;
import org.apache.commons.beanutils.BeanUtils;

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

/**
 * @author TJG 2015-04-28
 */
public class FrmPipeInventoryIn extends DBCommon {

    /**
     * 查询盘盈信息
     * @param list 查询参数
     * @param stoveages 仓库权限
     * @return CoreReturnObject
     */
    public CoreReturnObject getYdmZcStocktakingList(ArrayList<Object> list,
                                                    String[] stoveages) {
        return getYdmZcStocktakingList(list,stoveages,"");
    }
    public CoreReturnObject getYdmZcStocktakingList(ArrayList<Object> list,
            String[] stoveages,String supp) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeInventoryIn.getYdmZcStocktakingList");
        sql += CoreConverter.toSqlConditionString("T1.STORAGE_NO", stoveages);
        if (!StringUtils.isEmpty(supp)) {
            sql += " AND T1.SUPPLY_UNIT_CODE = '" + supp +"'";
        }
        sql += " GROUP BY T1.STOCKTAKING_DOC,T1.STOCKTAKING_TYPE_CODE,T1.STOCKTAKING_TYPE_NAME,T1.STOCKTAKING_RSN_CODE,T1.STOCKTAKING_RSN_NAME,T1.LOT_NO, T1.STOVE_NO,"
                + "T1.JUDGE_STOVE_NO,T1.BATCH_NO,T1.BATCH_GROUD_NO,T1.MATERIAL_NO,T1.MATERIAL_NAME,T1.PRODUCCODE,T1.PRODUCNAME,T1.STD_CODE,T1.STD_NAME,T1.GRADECODE,"
                + "T1.GRADENAME,T1.STEELCODE,T1.STEELNAME,T1.STD_STYLE,T1.STD_STYLE_DESC,T1.SPEC_CODE,T1.SPEC_NAME,T1.MODEL_CODE,T1.MODEL_DESC,T1.PLINE_CODE,T1.SUPPLY_UNIT_CODE,T1.SUPPLY_UNIT,"
                + "T1.PLINE_NAME,T1.ACT_DIMATER,T1.ACT_HEIGHT,T1.ACT_LEN,T1.ACT_LEN_MIN,T1.ACT_LEN_MAX,T1.BELONG_CODE,T1.BELONG_NAME,T1.LOCATION_NO,T1.STORAGE_ATTR,T1.BAL_YEAR_MONTH,"
                + "T1.STORAGE_ATTR_NAME,T1.STORAGE_TYPE_NO,T1.STORAGE_TYPE_NAME,T1.APPROVAL_FLAG,T1.MEMO,T1.CREATE_NAME, T1.STORAGE_NO,T2.STORAGE_NAME,T3.ORDER_NO,T3.ORDER_SEQ,T3.ORDER_SEQ_NO ORDER BY T1.STOCKTAKING_DOC, MIN(T1.CREATE_TIME) ";
        return this.getData(sql, list.toArray());
    }

    /**
     * 查询盘盈信息(半成品)
     * @param list 查询参数
     * @param stoveages 仓库权限
     * @return CoreReturnObject
     */
    public CoreReturnObject getYdmZcStocktakingList1(ArrayList<Object> list,
            String[] stoveages) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmPipeInventoryIn.getYdmZcStocktakingList1");
        sql += CoreConverter.toSqlConditionString("T1.STORAGE_NO", stoveages);
        sql += " GROUP BY T1.STOCKTAKING_DOC,T1.STOCKTAKING_TYPE_CODE,T1.STOCKTAKING_TYPE_NAME,T1.STOCKTAKING_RSN_CODE,T1.STOCKTAKING_RSN_NAME,T1.LOT_NO, T1.STOVE_NO,"
                + "T1.JUDGE_STOVE_NO,T1.BATCH_NO,T1.BATCH_GROUD_NO,T1.MATERIAL_NO,T1.MATERIAL_NAME,T1.PRODUCCODE,T1.PRODUCNAME,T1.STD_CODE,T1.STD_NAME,T1.GRADECODE,T1.PRO_ORDER_NO,"
                + "T1.GRADENAME,T1.STEELCODE,T1.STEELNAME,T1.STD_STYLE,T1.STD_STYLE_DESC,T1.SPEC_CODE,T1.SPEC_NAME,T1.MODEL_CODE,T1.MODEL_DESC,T1.PLINE_CODE,"
                + "T1.PLINE_NAME,T1.ACT_DIMATER,T1.ACT_HEIGHT,T1.ACT_LEN,T1.ACT_LEN_MIN,T1.ACT_LEN_MAX,T1.BELONG_CODE,T1.BELONG_NAME,T1.LOCATION_NO,T1.STORAGE_ATTR,T1.BAL_YEAR_MONTH,"
                + "T1.STORAGE_ATTR_NAME,T1.STORAGE_TYPE_NO,T1.STORAGE_TYPE_NAME,T1.APPROVAL_FLAG,T1.MEMO,T1.CREATE_NAME, T1.STORAGE_NO,T2.STORAGE_NAME,T3.ORDER_NO,T3.ORDER_SEQ,T3.ORDER_SEQ_NO ORDER BY T1.STOCKTAKING_DOC, MIN(T1.CREATE_TIME) ";
        return this.getData(sql, list.toArray());
    }

    
    
    /**
     * 查询料位号
     * @param strageNo 仓库号
     * @return CoreReturnObject
     */
    public CoreReturnObject getLocationNo(String strageNo) {
        Object[] params = new Object[] { strageNo };
        return this.getDataBySqlID("FrmPipeInventoryIn.getLocationNo", params);
    }
    /**
     * 查询材料类别
     * @return CoreReturnObject
     */
    public CoreReturnObject queryMaterialSTyle() {
        return this.getDataBySqlID("FrmPipeInventoryIn.queryMaterialSTyle");
    }

    /**
     * 查询材料类别
     * @return CoreReturnObject
     */
    public CoreReturnObject queryMaterialSTyle1() {
        return this.getDataBySqlID("FrmPipeInventoryIn.queryMaterialSTyle1");
    }
    
    /**
     * 查询产品码信息
     * @return CoreReturnObject
     */
    public CoreReturnObject getPsc() {
        return this.getDataBySqlID("FrmPipeInventoryIn.getPsc");
    }

    /**
     * 查询物料信息
     * @param producCode 品名
     * @param steelCode 钢级
     * @param stdStyle 牌号
     * @return CoreReturnObject
     */
    public CoreReturnObject getMaterial(String producCode, String steelCode,
            String stdStyle) {
        Object[] params = new Object[] { producCode, steelCode, stdStyle };
        return this.getDataBySqlID("FrmPipeInventoryIn.getMaterial", params);
    }

    /**
     * 查询物料信息
     * @param
     * @param
     * @param
     * @return CoreReturnObject
     */
    public CoreReturnObject getMaterial1() {
        return this.getDataBySqlID("FrmPipeInventoryIn.getMaterial1");
    }

    
    /**
     * 查询产线
     * @return CoreReturnObject
     */
    public CoreReturnObject getPline() {
        return this.getDataBySqlID("FrmPipeInventoryIn.getPline",new Object[]{});
    }

    /**
     * 查询产线
     * @return CoreReturnObject
     */
    public CoreReturnObject getPline1(String custom) {
        return this.getDataBySqlID("FrmPipeInventoryIn.getPline1",new Object[]{custom});
    }

    /**
     * 查询材料状态
     * @return CoreReturnObject
     */
    public CoreReturnObject getmagStatus(String value) {
        String str = "";
        if (value.endsWith("1")) {
            str = "801501";
        }
        else if (value.endsWith("2")) {
            str = "801502";
        }
        else {
            str = "801503";
        }
        Object[] params = new Object[] { str};
        return this.getDataBySqlID("FrmPipeInventoryIn.getmagStatus", params);
    }

    /**
     * 查询材料来源
     * @return CoreReturnObject
     */
    public CoreReturnObject getSourse(String sortkey) {
        Object[] params = new Object[] { sortkey };
        return this.getDataBySqlID("FrmPipeInventoryIn.getSourse", params);
    }

    /**
     * 查询属权单位
     * @param
     * @return 平台统一返回对象
     */
    public CoreReturnObject getBelongCode() {
        return this.getDataBySqlID("FrmPipeInventoryIn.getBelongCode");
    }

    /**
     * 新增盘盈信息
     * @param
     * @return
     */
    public CoreReturnObject doAdd(OYdmZcStorcktakinglistEntity storckEntity) {
        QualityDesignTool.nullConverNullString(storckEntity);
        CoreReturnObject core = new CoreReturnObject();
        Connection con = null;
        PreparedStatement ps = null;
        PreparedStatement psXs = null;
        ResultSet rs = null;
        String sql = "";
        String processCode = "";
        String processName = "";
        Date now = new Date();
        try {
            con = this.getDao("TgDao").getConnection();
            con.setAutoCommit(false);
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeInventoryIn.getStorgeInfo");
            ps = con.prepareStatement(sql);
            ps.setObject(1, storckEntity.getStorageNo());
            rs = ps.executeQuery();
            List<YdmBsStorageEntity> storageEntitys = EntityTool
                    .getEntitysByRs(rs, YdmBsStorageEntity.class);
            ps.close();
            rs.close();

            sql = CoreXmlSqlParsersFactory.getSql("FrmPipeEnter.selectProcess");
            ps = con.prepareStatement(sql);
            ps.setString(1,storckEntity.getPlineCode());
            rs = ps.executeQuery();
            if(rs.next())
            {
                processCode = rs.getString("PROCESS_CODE");
                processName = rs.getString("PROCESS_DESC");
            }
            rs.close();
            ps.close();
            if(processCode.equals(""))
            {
                processCode = "D";
                processName = "轧制";
            }

            // 查询仓库信息
            YdmBsStorageEntity storageEntity = null;
            if (storageEntitys != null && storageEntitys.size() > 0) {
                for (YdmBsStorageEntity ydmBsStorageEntity : storageEntitys) {
                    QualityDesignTool.nullConverNullString(ydmBsStorageEntity);
                }
                storageEntity = storageEntitys.get(0);
            }

            if (storageEntity == null) {
                core.setV_errMsg("仓库信息不存在");
                return core;
            }
            storckEntity.setStorageAttr(storageEntity.getStorageAttr());
            storckEntity.setStorageAttrName(storageEntity.getStorageAttrName());
            storckEntity.setStorageTypeNo(storageEntity.getStorageTypeNo());
            storckEntity.setStorageTypeName(storageEntity.getStorageTypeName());
            // 计算支数与重量
            int actNum = Integer.parseInt(storckEntity.getActCount());
            double actWgt = Double.parseDouble(storckEntity.getActWeight());
            List<double[]> list = MeanValueCalculation.MeanCalculation(actWgt,
                    actNum);
            int index = -1;
            DecimalFormat strDf = new DecimalFormat("0000");
            // 查询最大调查单号
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeInventoryIn.getMaxStockTakingDoc");
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            String stockTakingDoc = "";
            if (rs.next()) {
                stockTakingDoc = rs.getString("INDEX_SEQ");
            }

            rs.close();
            ps.close();
            // 查询最大物料编号
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeInventoryIn.getMaxMatNo");
            ps = con.prepareStatement(sql);
            ps.setObject(1, storckEntity.getJudgeStoveNo());
            ps.setObject(2, storckEntity.getBatchNo());
            ps.setObject(3, storckEntity.getJudgeStoveNo());
            ps.setObject(4, storckEntity.getBatchNo());
            rs = ps.executeQuery();
            if (rs.next()) {
                index = rs.getInt("MAX_SEQ");
            }
            rs.close();
            ps.close();
            // 新增物料SQL
            String sqlMatNo = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeInventoryIn.addMatZcM");
         // 新增销售物料SQL
            String sqlMatNoXs = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeInventoryInXs.addMatZcM");
            // 新增位置SQL
            String sqlButtress = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeInventoryIn.addYdmZcButtress");
            // 新增盘盈SQL
            String sqlDoc = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeInventoryIn.addStocktakingDoc");
            // 新增入库SQL
            String sqlInlist = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeInventoryIn.addYdmZcInlist");
            // 新增销售入库SQL
            String sqlInlistXs = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeInventoryInXs.addYdmZcInlist");
            // 循环支数
            for (int i = 0; i < list.size(); i++) {
                double[] dataArr = list.get(i);
                int dataArr1 = (int) dataArr[0];
                if (dataArr1 > 0) {
                    for (int j = 0; j < dataArr1; j++) {
                        storckEntity.setActCount("1");
                        storckEntity.setActWeight(String.valueOf(dataArr[1]));
                        index++;
                        // 新增物料
                        String matNo = storckEntity.getJudgeStoveNo()
                                + storckEntity.getBatchNo()
                                + strDf.format(index);
                        storckEntity.setMatNo(matNo);
                        storckEntity.setProcessCdoe(processCode);
                        storckEntity.setProcessDesc(processName);
                        storckEntity.setBalYearMonth("120504".equals(storckEntity.getSupplyUnitCode())? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
                        String sqlMatNostr = EntityTool.fillSqlByEntity(
                                sqlMatNo, storckEntity);
                        String sqlMatNostrXs = EntityTool.fillSqlByEntity(
                                sqlMatNoXs, storckEntity);
                        ps = con.prepareStatement(sqlMatNostr);
                        psXs = con.prepareStatement(sqlMatNostrXs);
                        ps.executeUpdate();
                        psXs.executeUpdate();
                        ps.close();
                        // 新增位置
                        ps = con.prepareStatement(sqlButtress);
                        ps.setObject(1, matNo);
                        ps.setObject(2, storckEntity.getJudgeStoveNo());
                        ps.setObject(3, storckEntity.getBatchNo());
                        ps.setObject(4, "001");
                        ps.setObject(5, storckEntity.getStorageNo());
                        ps.setObject(6, storckEntity.getLocationNo());
                        ps.setObject(7, storckEntity.getStorageAttr());
                        ps.setObject(8, storckEntity.getStorageAttrName());
                        ps.setObject(9, storckEntity.getStorageTypeNo());
                        ps.setObject(10, storckEntity.getStorageTypeName());
                        ps.setObject(11, storckEntity.getMemo());
                        ps.setObject(12, storckEntity.getCreateName());
                        ps.executeUpdate();
                        ps.close();
                        // 新增盘盈实绩
                        // 调差流水号
                        String stocktakingSqe = stockTakingDoc
                                + strDf.format(index);
                        storckEntity.setStocktakingSqe(stocktakingSqe);
                        storckEntity.setStocktakingDoc(stockTakingDoc);
                        storckEntity.setMatNo(matNo);
                        String sqlDocStr = EntityTool.fillSqlByEntity(sqlDoc,
                                storckEntity);
                        ps = con.prepareStatement(sqlDocStr);
                        ps.executeUpdate();
                        ps.close();
                        // 新增入库信息
                        YdmZcInlistEntity ydmZcList = new YdmZcInlistEntity();
                        BeanUtils.copyProperties(ydmZcList, storckEntity);
                        ydmZcList.setInstockSqe(stocktakingSqe);
                        ydmZcList.setMatNo(matNo);
                        ydmZcList.setInstockDoc(stockTakingDoc);
                        ydmZcList.setInstockTypeCode("800601");
                        ydmZcList.setInstockTypeName("盘盈入库");
                        ydmZcList.setProcessCdoe(processCode);
                        ydmZcList.setProcessDesc(processName);
                        ydmZcList.setBalYearMonth("120504".equals(storckEntity.getSupplyUnitCode())? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now));
                        String sqlInlistStr = EntityTool.fillSqlByEntity(
                                sqlInlist, ydmZcList);
                        ps = con.prepareStatement(sqlInlistStr);
                        String sqlInlistStrXs = EntityTool.fillSqlByEntity(
                                sqlInlistXs, ydmZcList);
                        psXs = con.prepareStatement(sqlInlistStrXs);
                        ps.executeUpdate();
                        ps.close();
                        psXs.executeUpdate();
                        psXs.close();
                    }
                }
            }
//            sql = CoreXmlSqlParsersFactory.getSql("FrmPipeInventoryIn.updateProcessIn");
//            ps = con.prepareStatement(sql);
//            ps.setString(1, String.valueOf(actNum));
//            ps.setString(2, new DecimalFormat("0.000").format(actWgt));
//            ps.setString(3, "");
//            ps.setString(4, "");//合同行键值
//            ps.executeUpdate();
//            ps.close();
            core.setV_errMsg("盘盈成功!");
            con.commit();
        }
        catch (Exception e) {

            e.printStackTrace();
        }
        finally {
            if (con != null) {
                try {
                    con.close();
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return core;
    }
    
    /**
     * 新增盘盈信息(半成品)
     * @param
     * @return
     */
    public CoreReturnObject doAdd1(OYdmZcStorcktakinglistEntity storckEntity) {
        QualityDesignTool.nullConverNullString(storckEntity);
        CoreReturnObject core = new CoreReturnObject();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "";
        Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		String systime = dateFormat.format(now);
		//String yearMoth = df.format(now);
        String yearMoth = "120504".equals(storckEntity.getSupplyUnitCode())? ComBaseInfo.GetBalYear_2416(now): ComBaseInfo.GetBalYear(now);
		String processCode = "";
		String processName = "";
        try {
            con = this.getDao("TgDao").getConnection();
            con.setAutoCommit(false);
            //查询工序代码
            sql = CoreXmlSqlParsersFactory
            .getSql("FrmPipeInventoryIn.getProcess");
            ps = con.prepareStatement(sql);
            ps.setObject(1, storckEntity.getPlineCode());
            rs = ps.executeQuery();
            if(rs.next())
            {
            	processCode = rs.getString("PROCESS_CODE");
            	processName = rs.getString("PROCESS_DESC");
            }
            rs.close();
            ps.close();
            
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeInventoryIn.getStorgeInfo");
            ps = con.prepareStatement(sql);
            ps.setObject(1, storckEntity.getStorageNo());
            rs = ps.executeQuery();
            List<YdmBsStorageEntity> storageEntitys = EntityTool
                    .getEntitysByRs(rs, YdmBsStorageEntity.class);
            ps.close();
            rs.close();
            // 查询仓库信息
            YdmBsStorageEntity storageEntity = null;
            if (storageEntitys != null && storageEntitys.size() > 0) {
                for (YdmBsStorageEntity ydmBsStorageEntity : storageEntitys) {
                    QualityDesignTool.nullConverNullString(ydmBsStorageEntity);
                }
                storageEntity = storageEntitys.get(0);
            }

            if (storageEntity == null) {
                core.setV_errMsg("仓库信息不存在");
                return core;
            }
            storckEntity.setStorageAttr(storageEntity.getStorageAttr());
            storckEntity.setStorageAttrName(storageEntity.getStorageAttrName());
            storckEntity.setStorageTypeNo(storageEntity.getStorageTypeNo());
            storckEntity.setStorageTypeName(storageEntity.getStorageTypeName());
            // 计算支数与重量
            int actNum = Integer.parseInt(storckEntity.getActCount());
            double actWgt = Double.parseDouble(storckEntity.getActWeight());
            List<double[]> list = MeanValueCalculation.MeanCalculation(actWgt,
                    actNum);
            int index = -1;
            DecimalFormat strDf = new DecimalFormat("0000");
            // 查询最大调查单号
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeInventoryIn.getMaxStockTakingDoc1");
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            String stockTakingDoc = "";
            if (rs.next()) {
                stockTakingDoc = rs.getString("INDEX_SEQ");
            }

            rs.close();
            ps.close();
            // 查询最大物料编号
            sql = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeInventoryIn.getMaxMatNo1");
            ps = con.prepareStatement(sql);
            ps.setObject(1, storckEntity.getJudgeStoveNo());
            ps.setObject(2, storckEntity.getBatchNo());
            ps.setObject(3, storckEntity.getJudgeStoveNo());
            ps.setObject(4, storckEntity.getBatchNo());
            rs = ps.executeQuery();
            if (rs.next()) {
                index = rs.getInt("MAX_SEQ");
            }
            rs.close();
            ps.close();
            // 新增物料SQL
            String sqlMatNo = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeInventoryIn.addMatZcM1");
            // 新增位置SQL
            String sqlButtress = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeInventoryIn.addYdmZcButtress1");
            // 新增盘盈SQL
            String sqlDoc = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeInventoryIn.addStocktakingDoc1");
            // 新增入库SQL
            String sqlInlist = CoreXmlSqlParsersFactory
                    .getSql("FrmPipeInventoryIn.addYdmZcInlist1");
            // 循环支数
            for (int i = 0; i < list.size(); i++) {
                double[] dataArr = list.get(i);
                int dataArr1 = (int) dataArr[0];
                if (dataArr1 > 0) {
                    for (int j = 0; j < dataArr1; j++) {
                        storckEntity.setActCount("1");
                        storckEntity.setActWeight(String.valueOf(dataArr[1]));
                        storckEntity.setActTheoryWeight(String.valueOf(dataArr[1]));
                        index++;
                        // 新增物料
                        String matNo = storckEntity.getJudgeStoveNo()
                                + storckEntity.getBatchNo()
                                + strDf.format(index);
                        storckEntity.setMatNo(matNo);
                        storckEntity.setCreateTime(systime);
                        storckEntity.setBalYearMonth(yearMoth);
                        storckEntity.setProcessCdoe(processCode);
                        storckEntity.setProcessDesc(processName);
                        if(storckEntity.getProductFlag().equals("801401"))
                        {
                        	storckEntity.setTarget("B");
                        }else if(storckEntity.getProductFlag().equals("801402"))
                        {
                        	storckEntity.setTarget("A");
                        }
                     
                        String sqlMatNostr = EntityTool.fillSqlByEntity(
                                sqlMatNo, storckEntity);
//                        String sqlMatNostr = EntityTool.fillSqlByEntity(
//                                sqlMatNo, storckEntity);
                        ps = con.prepareStatement(sqlMatNostr);
                        ps.executeUpdate();
                        ps.close();
                        // 新增位置
                        ps = con.prepareStatement(sqlButtress);
                        ps.setObject(1, matNo);
                        ps.setObject(2, storckEntity.getJudgeStoveNo());
                        ps.setObject(3, storckEntity.getBatchNo());
                        ps.setObject(4, "001");
                        ps.setObject(5, storckEntity.getStorageNo());
                        ps.setObject(6, storckEntity.getLocationNo());
                        ps.setObject(7, storckEntity.getStorageAttr());
                        ps.setObject(8, storckEntity.getStorageAttrName());
                        ps.setObject(9, storckEntity.getStorageTypeNo());
                        ps.setObject(10, storckEntity.getStorageTypeName());
                        ps.setObject(11, storckEntity.getMemo());
                        ps.setObject(12, storckEntity.getCreateName());
                        ps.executeUpdate();
                        ps.close();
                        // 新增盘盈实绩
                        // 调差流水号
                        String stocktakingSqe = stockTakingDoc
                                + strDf.format(index);
                        storckEntity.setStocktakingSqe(stocktakingSqe);
                        storckEntity.setStocktakingDoc(stockTakingDoc);
                        storckEntity.setMatNo(matNo);
                        String sqlDocStr = EntityTool.fillSqlByEntity(sqlDoc,
                                storckEntity);
                        ps = con.prepareStatement(sqlDocStr);
                        ps.executeUpdate();
                        ps.close();
                        // 新增入库信息
                        YdmZcInlistEntity ydmZcList = new YdmZcInlistEntity();
                        BeanUtils.copyProperties(ydmZcList, storckEntity);
                        ydmZcList.setInstockSqe(stocktakingSqe);
                        ydmZcList.setMatNo(matNo);
                        ydmZcList.setInstockDoc(stockTakingDoc);
                        ydmZcList.setInstockTypeCode("800702");
                        ydmZcList.setInstockTypeName("盘盈入库");
                        ydmZcList.setCreateTime(systime);
                        ydmZcList.setProcessCdoe(processCode);
                        ydmZcList.setProcessDesc(processName);
                        String sqlInlistStr = EntityTool.fillSqlByEntity(
                                sqlInlist, ydmZcList);
                        ps = con.prepareStatement(sqlInlistStr);
                        ps.executeUpdate();
                        ps.close();
                    }
                }
            }
//            sql = CoreXmlSqlParsersFactory.getSql("FrmPipeInventoryIn.updateProcessIn");
//            ps = con.prepareStatement(sql);
//            ps.setString(1, String.valueOf(actNum));
//            ps.setString(2, new DecimalFormat("0.000").format(actWgt));
//            ps.setString(3, "");
//            ps.setString(4, "");//合同行键值
//            ps.executeUpdate();
//            ps.close();
            core.setV_errMsg("盘盈成功!");
            con.commit();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (con != null) {
                try {
                    con.close();
                }
                catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return core;
    }
    /***
     * 撤销盘盈
     * @param
     * @return
     */
    public CoreReturnObject doDelete(String deleteTity)
    {
		CoreReturnObject core = new CoreReturnObject();
		Connection con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
		String systime = dateFormat.format(now);
		String yearMoth = df.format(now);
		int outCount = 0;
		int acount = 0;
		try {
			ArrayList<String> sqllist = new ArrayList<String>();
			OYdmZcStorcktakinglistEntity ydmZcEntity = EntityTool.getEntityByJSONStr(deleteTity, OYdmZcStorcktakinglistEntity.class);
			
			sql = CoreXmlSqlParsersFactory.getSql("FrmPipeInventoryIn.SelectOutCount");//查询是否有已出库支数
			sql = EntityTool.fillSqlByEntity(sql, ydmZcEntity);
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			if(rs.next())
			{
				acount = Integer.parseInt(rs.getString("ACOUNT"));
			}
			rs.close();
			ps.close();
			if(acount > 0)
			{
				core.setV_errMsg("已有出库支数,不允许撤销盘盈!");
				return core;
			}
			
			
			sql = CoreXmlSqlParsersFactory.getSql("FrmPipeInventoryIn.SelectCount");
			sql = EntityTool.fillSqlByEntity(sql, ydmZcEntity);
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			if(rs.next())
			{
				outCount = Integer.parseInt(rs.getString("ACOUNT"));
			}
			rs.close();
			ps.close();
			if(outCount > 0)
			{
				core.setV_errMsg("已有支数挂下工序订单号,不允许撤销盘盈!");
				return core;
			}
			
			String deleteSql = CoreXmlSqlParsersFactory.getSql("FrmPipeInventoryIn.doDelete");
			deleteSql = EntityTool.fillSqlByEntity(deleteSql, ydmZcEntity);
			sqllist.add(deleteSql);
			core = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
			if(core.getV_errMsg().equals(""))
			{
				core.setV_errMsg("撤销成功!");
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return core;
    }
    
    
    /***
     * 修改结转年月 
     * @param list
     * @return
     */
    public CoreReturnObject updateYear(ArrayList<String> list)
    {
    	return this.setDataBySqlID("FrmPipeInventoryIn.updateYear", list.toArray());
    }
    /***
     * 获取规格
     * @return
     */
    public CoreReturnObject getSpec()
    {
    	return this.getDataBySqlID("FrmPipeInventoryIn.getSpec");
    }
    /***
     * 获取品名
     * @return
     */
    public CoreReturnObject getProcduce()
    {
    	return this.getDataBySqlID("FrmPipeInventoryIn.getProcduce");
    }
    /***
     * 获取扣型
     * @return
     */
    public CoreReturnObject getModel()
    {
    	return this.getDataBySqlID("FrmPipeInventoryIn.getModel");
    }
    /***
     * 获取标准名称
     * @return
     */
    public CoreReturnObject getStdCode()
    {
    	return this.getDataBySqlID("FrmPipeInventoryIn.getStdCode");
    }
    /***
     * 获取钢级
     * @return
     */
    public CoreReturnObject getSteel()
    {
    	return this.getDataBySqlID("FrmPipeInventoryIn.getSteel");
    }
    /***
     * 获取钢种
     * @return
     */
    public CoreReturnObject getGrade()
    {
    	return this.getDataBySqlID("FrmPipeInventoryIn.getGrade");
    }
}
