package com.steering.pss.plnsaleord.jggxpln;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;

import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.plnsaleord.jggxpln.model.CouplingMatButtressEntity;
import com.steering.pss.plnsaleord.jggxpln.model.PlnLkJgLogEntity;

public class CoreJgMPropMatch extends DBCommon {

    /**
     * 查询基础信息
     * @param sortCode
     * @return
     */
    public CoreReturnObject selectBaseInfoSort(String sortCode) {
        CoreReturnObject cro = new CoreReturnObject();
        cro = this.getDataBySqlID("CoreJgMPropMatch.selectBaseInfoSort",
                new Object[] { sortCode });
        return cro;
    }

    /**
     * 接箍订单分组
     * @param proPlanId
     * @param jgGroupId
     * @return
     */
    public CoreReturnObject getPlnGxJgMStatus(String proPlanId, String jgGroupId) {
        CoreReturnObject cro = new CoreReturnObject();
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreJgMPropMatch.getPlnGxJgMStatus");
        cro = this.getData(sql, new Object[] { proPlanId, jgGroupId });
        return cro;
    }

    /**
     * 查询接箍利库
     * @param proPlanId
     * @param jgGroupId
     * @return
     */
    public CoreReturnObject queryMatch(String proPlanId, String jgGroupId) {
        CoreReturnObject cro = new CoreReturnObject();
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreJgMPropMatch.queryMatch");
        cro = this.getData(sql, new Object[] { proPlanId, jgGroupId });
        return cro;
    }

    /**
     * 查询订单分组已利库存
     * @param proPlanId
     * @param jgGroupId
     * @return
     */
    public CoreReturnObject queryJgMButtress(String proPlanId, String jgGroupId) {
        CoreReturnObject cro = new CoreReturnObject();
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreJgMPropMatch.queryJgMButtress");
        cro = this.getData(sql, new Object[] { proPlanId, jgGroupId });
        return cro;
    }

    /**
     * 接箍利库
     * @param list
     * @param proPlanId
     * @param jgGroupId
     * @param userName
     * @return
     */
    public CoreReturnObject outBound(ArrayList<CouplingMatButtressEntity> list,
            String proPlanId, String jgGroupId, String userName) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        try {
            String message = "";
            PreparedStatement ps = null;
            ResultSet rs = null;
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);

            String sqlQ = CoreXmlSqlParsersFactory
                    .getSql("CoreJgMPropMatch.getMatButtreStatus");
            String sqlAdd = CoreXmlSqlParsersFactory
                    .getSql("CoreJgMPropMatch.addMatButtre");
            String sqlUpdate = CoreXmlSqlParsersFactory
                    .getSql("CoreJgMPropMatch.updateMatButtre");
            String sqlUpdateP = CoreXmlSqlParsersFactory
                    .getSql("CoreJgMPropMatch.updateMatButtrePieces");
            String sqlUpJgM = CoreXmlSqlParsersFactory
                    .getSql("CoreJgMPropMatch.updateMatchJgGxm");
            String sqlUpJgProp = CoreXmlSqlParsersFactory
                    .getSql("CoreJgMPropMatch.updateMatchJgGxProp");
            String sqlJgMQ = CoreXmlSqlParsersFactory
                    .getSql("CoreJgMPropMatch.getPlnGxJgMStatus");
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreJgMPropMatch.getPlnLkJgLogId");
            String sqlAddLk = CoreXmlSqlParsersFactory
                    .getSql("CoreJgMPropMatch.addPlnLkJgLog");
            int sumMatchNum = 0;
            double sumMatchWt = 0;

            int matchAllNum = 0;
            int matchGroupNum = 0;
            double matchAllWt = 0;
            String demandDetection = "";// 探伤要求（408401）
            String demandBonderizing = "";// 磷化镀铜要求（408402）
            String demandCopper = "";// 压印喷标
            String demandBlast = "";// 喷砂要求

            String impressDealStatus = "";// 利库压印处理状态（0待处理1已处理2无要求）
            String copperDealStatus = "";// 利库喷标处理状态（0待处理1已处理2无要求）
            String detectDealStatus = "";// 利库探伤处理状态（0待处理1已处理2无要求）
            String blastDealStatus = "";// 利库喷砂处理状态（0待处理1已处理2无要求）
            String pltCuDealStatus = "";// 利库镀铜处理状态（0待处理1已处理2无要求）
            String phosDealStatus = "";// 利库磷化处理状态（0待处理1已处理2无要求）

            ps = conn.prepareStatement(sqlJgMQ);
            ps.setObject(1, proPlanId);
            ps.setObject(2, jgGroupId);
            rs = ps.executeQuery();
            if (!rs.next()) {
                message = "未找到该接箍订单分组。";
                cro.setResult(message);
                return cro;
            }
            if (!rs.getString("PLANSTATUS").equals("0")) {
                message = "订单：" + proPlanId + "已经下发了。";
                cro.setResult(message);
                return cro;
            }
            if (!rs.getString("GROUP_STATUS").equals("0")) {
                message = "订单：" + proPlanId + " 分组：" + jgGroupId + "不在计划状态。";
                cro.setResult(message);
                return cro;
            }
            matchAllNum = rs.getInt("MATCHNUM");
            matchAllWt = rs.getDouble("MATCHWEIGHT");
            matchGroupNum = rs.getInt("MATCHNUM_JG");

            if (rs.getString("DEMAND_DETECTION") != null) {
                demandDetection = rs.getString("DEMAND_DETECTION");
            }
            if (rs.getString("DEMAND_BONDERIZING") != null) {
                demandBonderizing = rs.getString("DEMAND_BONDERIZING");
            }
            if (rs.getString("DEMAND_COPPER") != null) {
                demandCopper = rs.getString("DEMAND_COPPER");
            }
            if (rs.getString("DEMAND_BLAST") != null) {
                demandBlast = rs.getString("DEMAND_BLAST");
            }
            rs.close();
            ps.close();

            for (CouplingMatButtressEntity matButtressEntity : list) {
                int matXHNum = 0;//材料组总数
                int matchNum = 0;//所选利库数
                double matXHWt = 0;
                double sinJgWt = 0;
                String sqlNew = "";
                String likuBillId = "";// 利库单号
                PlnLkJgLogEntity lkJgLogEntity = new PlnLkJgLogEntity();
                CouplingMatButtressEntity matButtreStatusEntity = null;
                ps = conn.prepareStatement(sqlQ);
                ps.setObject(1, matButtressEntity.getMatNo());
                ps.setObject(2, matButtressEntity.getMatNoXh());
                rs = ps.executeQuery();
                matButtreStatusEntity = EntityTool.getEntityByRs(rs,
                        CouplingMatButtressEntity.class);
                rs.close();
                ps.close();
                if (matButtreStatusEntity == null) {
                    message = "未找到材料：" + matButtressEntity.getMatNo() + " "
                            + matButtressEntity.getMatNoXh() + ",请刷新。";
                    conn.rollback();
                    cro.setResult(message);
                    return cro;
                }
                if (matButtreStatusEntity.getOfflineLog().equals("1")) {
                    message = "材料：" + matButtressEntity.getMatNo() + " "
                            + matButtressEntity.getMatNoXh() + "是离线材料,请刷新。";
                    conn.rollback();
                    cro.setResult(message);
                    return cro;
                }
                if (!matButtreStatusEntity.getProductFlag().equals("801401")) {
                    message = "材料：" + matButtressEntity.getMatNo() + " "
                            + matButtressEntity.getMatNoXh() + "不是在制品,请刷新。";
                    conn.rollback();
                    cro.setResult(message);
                    return cro;
                }
                if (!matButtreStatusEntity.getMatStatus().equals("80150102")) {
                    message = "材料：" + matButtressEntity.getMatNo() + " "
                            + matButtressEntity.getMatNoXh() + "不是已判定状态,请刷新。";
                    conn.rollback();
                    cro.setResult(message);
                    return cro;
                }

                matchNum = Integer.parseInt(matButtressEntity.getPieces());
                matXHNum = Integer.parseInt(matButtreStatusEntity.getPieces());
                matXHWt = Double.parseDouble(matButtreStatusEntity.getWeight());
                if (matXHNum > 0) {
                    sinJgWt = matXHWt / matXHNum;
                }
                else {
                    sinJgWt = 0;
                }

                matButtressEntity.setWeight(String.valueOf(matchNum * sinJgWt));

                // 探伤状态
//                if (demandDetection.trim().equals("40840101")) 
//                {
//                    if (matButtreStatusEntity.getDemandDetection().trim().equals("40840101"))
//                    {
//                        detectDealStatus = "1";
//                    }
//                    else 
//                    {
//                        detectDealStatus = "0";
//                    }
//                }
//                else {
//                    detectDealStatus = "2";
//                }

                // 压印状态
//                if (demandCopper.trim().equals("40840301")
//                        || demandCopper.trim().equals("40840303")) 
//                {
//                    if (matButtreStatusEntity.getDemandCopper().trim()
//                            .equals("40840301")
//                            || matButtreStatusEntity.getDemandCopper().trim()
//                                    .trim().equals("40840303")) 
//                    {
//                        impressDealStatus = "1";
//                    }
//                    else if (matButtreStatusEntity.getDemandCopper().trim().equals("40840302")) {
//						impressDealStatus = "0";
//						copperDealStatus = "0";
//					}
//                    else 
//                    {
//                        impressDealStatus = "0";
//                    }
//                }
//                else {
//                	if(StringTool.isNullOrEmpty(demandCopper.trim()) || demandCopper.trim().equals("0"))
//                	{
//                		if(matButtreStatusEntity.getDemandCopper().trim().equals("40840302")) {
//							copperDealStatus = "0";
//						}
//                		else if (matButtreStatusEntity.getDemandCopper().trim().equals("40840301")) {
//							impressDealStatus = "0";
//						}
//                		else if (matButtreStatusEntity.getDemandCopper().trim().equals("40840303")) {
//							impressDealStatus ="0";
//							copperDealStatus = "0";
//						} 
//                		else
//                		{
//                			impressDealStatus = "2";
//                		}	
//                	}
//                }
                
//                // 喷标状态
//                if (demandCopper.trim().equals("40840302")
//                        || demandCopper.trim().equals("40840303")) {
//                    if (matButtreStatusEntity.getDemandCopper().trim()
//                            .equals("40840302")
//                            || matButtreStatusEntity.getDemandCopper()
//                                    .trim().equals("40840303")) {
//                        copperDealStatus = "1";
//                    }
//                    else if (matButtreStatusEntity.getDemandCopper().trim().equals("40840301")) {
//						impressDealStatus = "0";
//						copperDealStatus = "0";
//					}
//                    else {
//                        copperDealStatus = "0";
//                    }
//                }
//                else {
//                	if(StringTool.isNullOrEmpty(demandCopper.trim()) || demandCopper.trim().equals("0"))
//                	{
//                		if(matButtreStatusEntity.getDemandCopper().trim().equals("40840302")) {
//							copperDealStatus = "0";
//						}
//                		else if (matButtreStatusEntity.getDemandCopper().trim().equals("40840301")) {
//							impressDealStatus = "0";
//						}
//                		else if (matButtreStatusEntity.getDemandCopper().trim().equals("40840303")) {
//							impressDealStatus ="0";
//							copperDealStatus = "0";
//						} 
//                		else {
//                			impressDealStatus = "2";
//						}
//                	}
//                	else {
//            			impressDealStatus = "2";
//					}
//                }
                


//                // 利库喷砂处理状态
//                if (demandBlast.trim().equals("40840401")) {
//                    if (matButtreStatusEntity.getDemandBlast().trim()
//                            .equals("40840401")) {
//                        blastDealStatus = "1";
//                    }
//                    else {
//                        blastDealStatus = "0";
//                    }
//                }
//                else {
//                	if(matButtreStatusEntity.getDemandBlast().trim()
//                            .equals("40840401"))
//                	{
//                		blastDealStatus = "0";
//                	}
//                	else {
//                		blastDealStatus = "2";
//					}
//                    
//                }

                // 利库镀铜处理状态
//                if (demandBonderizing.trim().equals("40840202")) {
//                    if (matButtreStatusEntity.getDemandBonderizing().trim()
//                            .equals("40840202")) {
//                        pltCuDealStatus = "1";
//                    }
//                    else if (matButtreStatusEntity.getDemandBonderizing().trim().equals("40840201")) {
//                    	pltCuDealStatus = "0";
//                    	phosDealStatus = "0";
//					}
//                    else {
//                        pltCuDealStatus = "0";
//                    }
//                }
//                else {
//                	if(matButtreStatusEntity.getDemandBonderizing().trim()
//                            .equals("40840202"))
//                	{
//                		pltCuDealStatus = "0";
//                	}
//                	else {
//                		pltCuDealStatus = "2";
//					}
//                    
//                }

                // 利库磷化处理状态
//                if (demandBonderizing.trim().equals("40840201")) {
//                    if (matButtreStatusEntity.getDemandBonderizing().trim()
//                            .equals("40840201")) {
//                        phosDealStatus = "1";
//                    }
//                    else if (matButtreStatusEntity.getDemandBonderizing().trim().equals("40840202")) {
//                    	pltCuDealStatus = "0";
//                    	phosDealStatus = "0";
//					}
//                    else {
//                        phosDealStatus = "0";
//                    }
//                }
//                else {
//                	if (matButtreStatusEntity.getDemandBonderizing().trim()
//                            .equals("40840201")) {
//                        phosDealStatus = "0";
//                    }
//                	else {
//                		phosDealStatus = "2";
//					}
//                }

                // 生成利库单号
                ps = conn.prepareStatement(sql);
                rs = ps.executeQuery();
                if (rs.next()) {
                    if (rs.getString("LIKU_BILL_ID") != null) {
                        likuBillId = rs.getString("LIKU_BILL_ID");
                    }
                    else {
                        message = "生成利库单号出错。";
                        conn.rollback();
                        cro.setResult(message);
                        return cro;
                    }
                }
                else {
                    message = "生成利库单号出错。";
                    conn.rollback();
                    cro.setResult(message);
                    return cro;
                }
                rs.close();
                ps.close();

                if (matXHNum > matchNum) {
                    String sqlUp = "";
                    // 新增出库接箍库存子记录
                    matButtressEntity.setMatStatus("80150103");
                    matButtressEntity.setProPlanId(proPlanId);
                    matButtressEntity.setJgGroupId(jgGroupId);

                    matButtressEntity.setDemandDetectionLk(demandDetection);
                    matButtressEntity.setDemandBonderizingLk(demandBonderizing);
                    matButtressEntity.setDemandCopperLk(demandCopper);
                    matButtressEntity.setDemandBlastLk(demandBlast);

                    matButtressEntity.setImpressDealStatus(impressDealStatus);
                    matButtressEntity.setCopperDealStatus(copperDealStatus);
                    matButtressEntity.setDetectDealStatus(detectDealStatus);
                    matButtressEntity.setBlastDealStatus(blastDealStatus);
                    matButtressEntity.setPltCuDealStatus(pltCuDealStatus);
                    matButtressEntity.setPhosDealStatus(phosDealStatus);
                    matButtressEntity.setLockLiku("1");
//                    matButtressEntity.setLikuBillId(likuBillId); 2018.06.13注释

                    // /利库单
                    lkJgLogEntity.setMatNo(matButtressEntity.getMatNo());
                    lkJgLogEntity.setMatNoXh(matButtressEntity.getMatNoXh());
                    lkJgLogEntity.setJgLkNum(matButtressEntity.getPieces());

                    sqlUp = EntityTool.fillSqlByEntity(sqlAdd,
                            matButtressEntity);
                    ps = conn.prepareStatement(sqlUp);
                    ps.executeUpdate();
                    ps.close();

                    // 更新原接箍库存子记录个数
                    matButtreStatusEntity.setPieces(String.valueOf(matXHNum
                            - matchNum));
                    matButtreStatusEntity.setWeight(String
                            .valueOf((matXHNum - matchNum) * sinJgWt));
                    matButtreStatusEntity.setUpdateName(userName);

                    sqlUp = EntityTool.fillSqlByEntity(sqlUpdateP,
                            matButtreStatusEntity);
                    ps = conn.prepareStatement(sqlUp);
                    ps.executeUpdate();
                    ps.close();
                }
                else if (matXHNum == matchNum) {
                    // 更新原接箍库存子记录状态
                    String sqlUp = "";
                    matButtreStatusEntity.setMatStatus("80150103");
                    matButtreStatusEntity.setProPlanId(proPlanId);
                    matButtreStatusEntity.setJgGroupId(jgGroupId);
                    matButtreStatusEntity.setUpdateName(userName);

                    matButtreStatusEntity.setDemandDetectionLk(demandDetection);
                    matButtreStatusEntity
                            .setDemandBonderizingLk(demandBonderizing);
                    matButtreStatusEntity.setDemandCopperLk(demandCopper);
                    matButtreStatusEntity.setDemandBlastLk(demandBlast);

                    matButtreStatusEntity
                            .setImpressDealStatus(impressDealStatus);
                    matButtreStatusEntity.setCopperDealStatus(copperDealStatus);
                    matButtreStatusEntity.setDetectDealStatus(detectDealStatus);
                    matButtreStatusEntity.setBlastDealStatus(blastDealStatus);
                    matButtreStatusEntity.setPltCuDealStatus(pltCuDealStatus);
                    matButtreStatusEntity.setPhosDealStatus(phosDealStatus);
                    matButtreStatusEntity.setLockLiku("1");
//                    matButtreStatusEntity.setLikuBillId(likuBillId);

                    // /利库单
                    lkJgLogEntity.setMatNo(matButtreStatusEntity.getMatNo());
                    lkJgLogEntity
                            .setMatNoXh(matButtreStatusEntity.getMatNoXh());
                    lkJgLogEntity.setJgLkNum(matButtreStatusEntity.getPieces());

                    sqlUp = EntityTool.fillSqlByEntity(sqlUpdate,
                            matButtreStatusEntity);
                    ps = conn.prepareStatement(sqlUp);
                    ps.executeUpdate();
                    ps.close();
                }
                else {
                    message = "材料：" + matButtressEntity.getMatNo() + " "
                            + matButtressEntity.getMatNoXh() + "利库个数有误。";
                    conn.rollback();
                    cro.setResult(message);
                    return cro;
                }
                sumMatchNum += matchNum;
                sumMatchWt += matchNum * sinJgWt;

                // 新增利库单
                lkJgLogEntity.setLikuBillId(likuBillId);
                lkJgLogEntity.setProPlanId(proPlanId);
                lkJgLogEntity.setJgGroupId(jgGroupId);
                lkJgLogEntity.setCreateName(userName);
                sqlNew = EntityTool.fillSqlByEntity(sqlAddLk, lkJgLogEntity);
                ps = conn.prepareStatement(sqlNew);
                ps.executeUpdate();
                ps.close();

            }
            matchAllNum += sumMatchNum;
            matchAllWt += sumMatchWt;
            matchGroupNum += sumMatchNum;

            // 更新接箍订单表利库个数
            ps = conn.prepareStatement(sqlUpJgM);
            ps.setObject(1, matchAllWt);
            ps.setObject(2, matchAllNum);
            ps.setObject(3, proPlanId);
            ps.executeUpdate();
            ps.close();

            // 更新接箍订单分组表利库个数
            ps = conn.prepareStatement(sqlUpJgProp);
            ps.setObject(1, matchGroupNum);
            ps.setObject(2, proPlanId);
            ps.setObject(3, jgGroupId);
            ps.executeUpdate();
            ps.close();

            conn.commit();
            cro.setResult("利库成功。");
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
            cro.setV_errCode(-1);
            cro.setV_errMsg(sqle.getMessage());
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

    /**
     * 撤销接箍利库
     * @param list
     * @param proPlanId
     * @param jgGroupId
     * @param userName
     * @return
     */
    public CoreReturnObject deleteMatJg(
            ArrayList<CouplingMatButtressEntity> list, String proPlanId,
            String jgGroupId, String userName) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection conn = null;
        try {
            PreparedStatement ps = null;
            ResultSet rs = null;
            String message = "";
            conn = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            conn.setAutoCommit(false);
            String sqlQ = CoreXmlSqlParsersFactory
                    .getSql("CoreJgMPropMatch.getMatButtreStatus");
            String sqlUpdate = CoreXmlSqlParsersFactory
                    .getSql("CoreJgMPropMatch.updateMatButtre");
            String sqlUpJgM = CoreXmlSqlParsersFactory
                    .getSql("CoreJgMPropMatch.updateMatchJgGxm");
            String sqlUpJgProp = CoreXmlSqlParsersFactory
                    .getSql("CoreJgMPropMatch.updateMatchJgGxProp");
            String sqlJgMQ = CoreXmlSqlParsersFactory
                    .getSql("CoreJgMPropMatch.getPlnGxJgMStatus");
            String sqlUpdateLk = CoreXmlSqlParsersFactory
                    .getSql("CoreJgMPropMatch.updatePlnLkJgLog");
            int sumMatchNum = 0;
            double sumMatchWt = 0;

            int matchAllNum = 0;
            int matchGroupNum = 0;
            double matchAllWt = 0;
            ps = conn.prepareStatement(sqlJgMQ);
            ps.setObject(1, proPlanId);
            ps.setObject(2, jgGroupId);
            rs = ps.executeQuery();
            if (!rs.next()) {
                message = "未找到该接箍订单分组。";
                cro.setResult(message);
                return cro;
            }
            if (!rs.getString("PLANSTATUS").equals("0")) {
                message = "订单：" + proPlanId + "已经下发了。";
                cro.setResult(message);
                return cro;
            }
            if (!rs.getString("GROUP_STATUS").equals("0")) {
                message = "订单：" + proPlanId + " 分组：" + jgGroupId + "不在计划状态。";
                cro.setResult(message);
                return cro;
            }
            matchAllNum = rs.getInt("MATCHNUM");
            matchAllWt = rs.getDouble("MATCHWEIGHT");
            matchGroupNum = rs.getInt("MATCHNUM_JG");
            rs.close();
            ps.close();

            for (CouplingMatButtressEntity matButtressEntity : list) {
                int matXHNum = 0;
                double matXHWt = 0;
                String sqlUp = "";
                PlnLkJgLogEntity lkJgLogEntity = new PlnLkJgLogEntity();
                CouplingMatButtressEntity matButtreStatusEntity = null;
                ps = conn.prepareStatement(sqlQ);
                ps.setObject(1, matButtressEntity.getMatNo());
                ps.setObject(2, matButtressEntity.getMatNoXh());
                rs = ps.executeQuery();
                matButtreStatusEntity = EntityTool.getEntityByRs(rs,
                        CouplingMatButtressEntity.class);
                rs.close();
                ps.close();

                if (matButtreStatusEntity == null) {
                    message = "未找到材料：" + matButtressEntity.getMatNo() + " "
                            + matButtressEntity.getMatNoXh() + ",请刷新。";
                    conn.rollback();
                    cro.setResult(message);
                    return cro;
                }
                if (!matButtreStatusEntity.getProductFlag().equals("801401")) {
                    message = "材料：" + matButtressEntity.getMatNo() + " "
                            + matButtressEntity.getMatNoXh() + "不是在制品,请刷新。";
                    conn.rollback();
                    cro.setResult(message);
                    return cro;
                }
                matXHNum = Integer.parseInt(matButtreStatusEntity.getPieces());
                matXHWt = Double.parseDouble(matButtreStatusEntity.getWeight());

                matButtreStatusEntity.setMatStatus("80150102");
                matButtreStatusEntity.setProPlanId("");
                matButtreStatusEntity.setJgGroupId("");
                matButtreStatusEntity.setUpdateName(userName);

                matButtreStatusEntity.setDemandDetectionLk("");
                matButtreStatusEntity.setDemandBonderizingLk("");
                matButtreStatusEntity.setDemandCopperLk("");
                matButtreStatusEntity.setDemandBlastLk("");

                matButtreStatusEntity.setImpressDealStatus("");
                matButtreStatusEntity.setCopperDealStatus("");
                matButtreStatusEntity.setDetectDealStatus("");
                matButtreStatusEntity.setBlastDealStatus("");
                matButtreStatusEntity.setPltCuDealStatus("");
                matButtreStatusEntity.setPhosDealStatus("");
                matButtreStatusEntity.setLockLiku("0");
                // 更新库存记录状态
                sqlUp = EntityTool.fillSqlByEntity(sqlUpdate,
                        matButtreStatusEntity);
                ps = conn.prepareStatement(sqlUp);
                ps.executeUpdate();
                ps.close();

                // 利库单脱单
//                lkJgLogEntity.setLikuBillId(matButtreStatusEntity
//                        .getLikuBillId());
                lkJgLogEntity.setIfUnmatched("1");
                lkJgLogEntity.setUpdateName(userName);
                sqlUp = EntityTool.fillSqlByEntity(sqlUpdateLk, lkJgLogEntity);
                ps = conn.prepareStatement(sqlUp);
                ps.executeUpdate();
                ps.close();

                sumMatchNum += matXHNum;
                sumMatchWt += matXHWt;
            }
            matchAllNum -= sumMatchNum;
            matchAllWt -= sumMatchWt;
            matchGroupNum -= sumMatchNum;

            if (matchAllNum < 0) {
                matchAllNum = 0;
            }
            if (matchAllWt < 0.000000000001) {
                matchAllWt = 0;
            }
            if (matchGroupNum < 0) {
                matchGroupNum = 0;
            }

            // 更新接箍订单表利库个数
            ps = conn.prepareStatement(sqlUpJgM);
            ps.setObject(1, matchAllWt);
            ps.setObject(2, matchAllNum);
            ps.setObject(3, proPlanId);
            ps.executeUpdate();
            ps.close();

            // 更新接箍订单分组表利库个数
            ps = conn.prepareStatement(sqlUpJgProp);
            ps.setObject(1, matchGroupNum);
            ps.setObject(2, proPlanId);
            ps.setObject(3, jgGroupId);
            ps.executeUpdate();
            ps.close();

            conn.commit();
            cro.setResult("撤销成功。");
        }
        catch (SQLException sqle) {
            try {
                conn.rollback();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
            cro.setV_errCode(-1);
            cro.setV_errMsg(sqle.getMessage());
        }
        finally {
            try {
                if (conn != null && !conn.isClosed()) {
                    conn.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;
    }

}