package com.steering.pss.sale.order.qualityDesign.server.iserver;

import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.tools.EntityTool;
import com.steering.pss.qcm.model.ComBaseWellMonthEntity;
import com.steering.pss.qcm.model.ResBaseResourceAllocationEntity;
import com.steering.pss.sale.order.ReviewForm.CoreDeliveryReview;
import com.steering.pss.sale.order.model.*;
import com.steering.pss.sale.order.qualityDesign.Dao.factory.FactoryDao;
import com.steering.pss.sale.order.qualityDesign.Dao.imp.ProCycleDaoImp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;

/**
 * 产能井投井分析
 * @author WHB 2015-5-30
 */
public class CoreCastWell {

    /**
     * 产能井分析
     * @param con
     * @param ordPk
     * @param form 来自于哪个界面的请求 2合同变更 在这种情况下第二个参数是OrdLnPk (后来由于超时的原因 等于3时
     *            也代表第二个参数接收合同行的PK值。)
     * @return
     * @throws SQLException
     */

    public String castWell(Connection con, String ordPk, String form)
            throws SQLException {

        String errMsg = ""; // 错误提示信息
        String errMsg1 = "";
        String designKey = "";
        List<OrderLineDeliveryEntity> deliverys = new ArrayList<OrderLineDeliveryEntity>();
        ProCycleDaoImp proCycleDaoImp = FactoryDao.getProductCycleDaoImp();
        IProCycle ipc = new IProCycle();

        HashMap<String, String> map = new HashMap<String, String>();

        List<String> line = new ArrayList<String>();
        if (form.equals("2") || form.equals("3")) {
            line.add(ordPk);
        }
        else {
            line = getOrderLnPk(con, ordPk);
        }
        for (String ordLnPk : line) { // 92E40E86412E0068E053AC10026CD56C005
            String msc = "";
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreCastWell.GetDesignKey");
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setObject(1, ordLnPk);
            ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                designKey = rs.getString("DESGIN_KEY");
            }
            else {
                errMsg = "找不到该合同行的DesignKey";
                return errMsg;
            }
            rs.close();
            ps.close();

            String sql1 = CoreXmlSqlParsersFactory
                    .getSql("CoreCastWell.SelectOrdLnPkMSC");
            ps = con.prepareStatement(sql1);
            ps.setObject(1, ordLnPk);
            rs = ps.executeQuery();
            if (rs.next()) {
                msc = rs.getString(1);
                if (msc == null || msc.equals("")) {
                    errMsg = "找不到合同行的MSC";
                    return errMsg;
                }
            }
            else {
                errMsg = "找不到合同行的MSC";
                return errMsg;
            }
            rs.close();
            ps.close();
            // 交货量可能改变了,重新计算一次交货期
            errMsg = ipc.generateProCycle(con, msc, designKey, ordLnPk, form);
            if (!errMsg.equals("")) {
                return errMsg;
            }

            deliverys = proCycleDaoImp.getLineDelivery(con, ordLnPk, form);

            for (OrderLineDeliveryEntity delivery : deliverys) { // 交货行

                java.sql.Date dateNow = new java.sql.Date(
                        new java.util.Date().getTime()); // 当前时间

                String saleOrg = ""; // 销售组织代码
                String saleOrgDesc = ""; // 销售组织描述
                int mon = 0; // 月份
                int year = 0;
                int flag = 0;

                String[] arr = getSaleInfo(con, delivery.getOrdPk());
                if (arr != null && arr.length == 2) {
                    saleOrg = arr[0];
                    saleOrgDesc = arr[1];
                }
                else {
                    errMsg = "该合同没有维护销售组织";
                    return errMsg;
                }

                Date date = null; // 总体交货日期
                Date date1 = null;

                DesignMscPlPcEntity plpc = getDesMscPlPc(con, designKey,
                        delivery.getOrdLnDlyPk());
                if (plpc != null) {
                    date = strToDate(plpc.getDelvryEdate()); // 交货日期
                }
                else {
                    errMsg = "该交货行没有经过生产周期计算！";
                    return errMsg;
                }

                String mscPline = "";
                String wholeBackLog = "";
                String wholeBackLogDesc = "";

                date1 = date;
                String[] plArr = getDesignMscPl(con, designKey); // 质量设计-制程-全程途径码与描述

                if (plArr != null && plArr.length == 3) {
                    mscPline = plArr[0];
                    wholeBackLog = plArr[1];
                    wholeBackLogDesc = plArr[2];
                }
                else {
                    errMsg = "交货行没有经过质量设计的制程！";
                    return errMsg;
                }

                List<DesignStsPlPcLstEntity> stsList = new ArrayList<DesignStsPlPcLstEntity>(); // 生产周期状态明细从表
                List<DesignMscPlPcLstEntity> mscList = new ArrayList<DesignMscPlPcLstEntity>(); // 生产周期状态从表
                List<SlmWellUseListEntity> useList = new ArrayList<SlmWellUseListEntity>(); // 产能井预占

                List<DesignMscPlPcLstEntity> plpclsts = getDesMscPlPcLst(con,
                        designKey, delivery.getOrdLnDlyPk());

                double delQty = Double.parseDouble(delivery.getWgtEstmt()); // 交货量
                int processSize = 0;
                if (plpclsts.size() > 0)
                    processSize = plpclsts.size();
                Double cycedit = 0.0; // 生产周期修正
                if (plpc.getCycEdit() == null || plpc.getCycEdit().equals("")) {
                    cycedit = 0.0;
                }
                else {
                    cycedit = Double.parseDouble(plpc.getCycEdit());
                }
                double cycBatch = 0; // 总的生产时间

                cycBatch = getPlpcLstTime(con, designKey,
                        delivery.getOrdLnDlyPk());
                for (DesignMscPlPcLstEntity plpclst : plpclsts) {
                    Date tempDate = null;
                    // 将周期修正值 均摊（按照比列）到各个工序
                    Double s = Math.ceil(Double.parseDouble(plpclst
                            .getPrdCycBtch())
                            + Double.parseDouble(plpclst.getPrdCycBtch())
                            / cycBatch * cycedit);
                    tempDate = dateAddDay(date1, -s.intValue());
                    mon = getTimeMon(tempDate);
                    year = getTimeYear(tempDate);
                    date1 = tempDate;
                    // System.out.println("##########" + date1);
                    // 炼钢电炉暂时不考虑产能井。
                    if (plpclst.getProcessCode().toString().equals("A")) {

                        DesignStsPlPcLstEntity stsPlpc = new DesignStsPlPcLstEntity();
                        stsPlpc.setDesginKey(designKey);
                        stsPlpc.setOrdLnPk(delivery.getOrdLnPk());
                        stsPlpc.setOrdLnDlyPk(delivery.getOrdLnDlyPk());
                        stsPlpc.setMscPline(mscPline);
                        stsPlpc.setWholeBacklog(wholeBackLog);
                        stsPlpc.setWholeBacklogDesc(wholeBackLogDesc);
                        stsPlpc.setProcessCdoe(plpclst.getProcessCode());
                        stsPlpc.setProcessDesc(plpclst.getProcessDesc());
                        if (date1.getTime() < dateNow.getTime()) {
                            stsPlpc.setPrdcycMeet("F");
                            flag += 1;
                        }
                        else {
                            stsPlpc.setPrdcycMeet("S");
                        }
                        stsList.add(stsPlpc);

                        // 更新生产周期状态从表
                        DesignMscPlPcLstEntity mscPlpc = new DesignMscPlPcLstEntity();
                        mscPlpc.setDesginKey(designKey);
                        mscPlpc.setOrdLnDlyPk(delivery.getOrdLnDlyPk());
                        mscPlpc.setMscPline(mscPline);
                        mscPlpc.setProcessCode(plpclst.getProcessCode());
                        mscPlpc.setLstPrdDate(date1.toString());
                        mscPlpc.setReviewAsk(dateToStr(date1.toString()));
                        mscPlpc.setPrdCycBtch(plpclst.getPrdCycBtch());
                        mscList.add(mscPlpc);
                        continue;
                    }

                    double useCount = 0; // 耗用量
                    double useCountOrg = 0; // 销售组织的耗用量
                    String wellCode = getWellCode(con, designKey,
                            plpclst.getMscPline(), plpclst.getProcessCode()); // 产能井号
                    if (!wellCode.equals("")) { // 存在产能井号
                        // 这里需要修正，消耗量和释放量。且一个产能井对应很对数据。
                        useCount = getWellListUse(con, wellCode, year, mon); // 产能井三个销售组织总的占用
                        useCountOrg = getWellListUseSaleOrg(con, wellCode,
                                year, mon, saleOrg); // /产能井指定销售组织的耗用量
                        List<ResBaseResourceAllocationEntity> listRS = getResource(
                                con, saleOrg, plpclst.getProcessCode(),
                                String.valueOf(year));
                        // 对于资源量 D工序的需要严格控制，其他工序有值则判断，无值则无限大
                        double count = 99999999;
                        if (listRS != null && listRS.size() > 0) {
                            ResBaseResourceAllocationEntity res = listRS.get(0);
                            count = getResCount(res, mon); // 资源总量
                            if (count == 0) // 为0也算作无限大
                                count = 99999999;
                        }
                        else {
                            if (plpclst.getProcessCode().equals("D")) {
                                errMsg = saleOrgDesc + year + "年,工序 "
                                        + plpclst.getProcessDesc()
                                        + "没有相应的销售组织的资源配置信息！";
                                return errMsg;
                            }
                        }

                        if (count != 0) {
                            if (delQty + useCountOrg > count) {
                                // 资源不满足
                                DesignStsPlPcLstEntity stsPlpc = new DesignStsPlPcLstEntity();
                                stsPlpc.setDesginKey(designKey);
                                stsPlpc.setOrdLnPk(delivery.getOrdLnPk());
                                stsPlpc.setOrdLnDlyPk(delivery.getOrdLnDlyPk());
                                stsPlpc.setMscPline(mscPline);
                                stsPlpc.setWholeBacklog(wholeBackLog);
                                stsPlpc.setWholeBacklogDesc(wholeBackLogDesc);
                                stsPlpc.setProcessCdoe(plpclst.getProcessCode());
                                stsPlpc.setProcessDesc(plpclst.getProcessDesc());
                                stsPlpc.setPrdcycMeet("F");
                                stsList.add(stsPlpc);
                                flag += 1; // 不满足
                                // 更新生产周期状态从表
                                DesignMscPlPcLstEntity mscPlpc = new DesignMscPlPcLstEntity();
                                mscPlpc.setDesginKey(designKey);
                                mscPlpc.setOrdLnDlyPk(delivery.getOrdLnDlyPk());
                                mscPlpc.setMscPline(mscPline);
                                mscPlpc.setProcessCode(plpclst.getProcessCode());
                                mscPlpc.setLstPrdDate(date1.toString());
                                mscPlpc.setReviewAsk(dateToStr(date1.toString()));
                                mscPlpc.setPrdCycBtch(plpclst.getPrdCycBtch());
                                mscList.add(mscPlpc);
                                errMsg1 = saleOrgDesc + year + "年" + mon + "月"
                                        + plpclst.getProcessDesc() + "工序"
                                        + "资源量不足!";
                            }
                            else { // 资源满足，继续分析井的能力
                                List<ComBaseWellMonthEntity> wellMonth = getWellMonth(
                                        con, wellCode, year);
                                double wellCount = 0;
                                if (wellMonth != null && wellMonth.size() > 0) {
                                    ComBaseWellMonthEntity cbw = wellMonth
                                            .get(0);
                                    wellCount = getWellCount(cbw, mon);
                                    if (wellCount > 0) {
                                        if (wellCount - useCount >= delQty) {
                                            // 井能力满足
                                            DesignStsPlPcLstEntity stsPlpc = new DesignStsPlPcLstEntity();
                                            stsPlpc.setDesginKey(designKey);
                                            stsPlpc.setOrdLnPk(delivery
                                                    .getOrdLnPk());
                                            stsPlpc.setOrdLnDlyPk(delivery
                                                    .getOrdLnDlyPk());
                                            stsPlpc.setMscPline(mscPline);
                                            stsPlpc.setWholeBacklog(wholeBackLog);
                                            stsPlpc.setWholeBacklogDesc(wholeBackLogDesc);
                                            stsPlpc.setProcessCdoe(plpclst
                                                    .getProcessCode());
                                            stsPlpc.setProcessDesc(plpclst
                                                    .getProcessDesc());
                                            if (date1.getTime() < dateNow
                                                    .getTime()) {
                                                stsPlpc.setPrdcycMeet("F");
                                                flag += 1;
                                            }
                                            else {
                                                stsPlpc.setPrdcycMeet("S");
                                            }
                                            stsList.add(stsPlpc);

                                            // 更新生产周期状态从表
                                            DesignMscPlPcLstEntity mscPlpc = new DesignMscPlPcLstEntity();
                                            mscPlpc.setDesginKey(designKey);
                                            mscPlpc.setOrdLnDlyPk(delivery
                                                    .getOrdLnDlyPk());
                                            mscPlpc.setMscPline(mscPline);
                                            mscPlpc.setProcessCode(plpclst
                                                    .getProcessCode());
                                            mscPlpc.setLstPrdDate(date1
                                                    .toString());
                                            mscPlpc.setReviewAsk(dateToStr(date1
                                                    .toString()));
                                            mscPlpc.setPrdCycBtch(plpclst
                                                    .getPrdCycBtch());
                                            mscList.add(mscPlpc);

                                            // 产能井剩余
                                            SlmWellUseListEntity list = new SlmWellUseListEntity();
                                            list.setWellCode(wellCode);
                                            list.setWellYear(String
                                                    .valueOf(year));
                                            list.setUseMonth(String
                                                    .valueOf(mon));
                                            list.setSaleOrg(saleOrg);
                                            list.setSaleOrgDesc(saleOrgDesc);
                                            list.setOrdLnDlyPk(delivery
                                                    .getOrdLnDlyPk());
                                            list.setUseObj("0");
                                            list.setUseType("消耗");
                                            list.setUseWgt(String
                                                    .valueOf(delQty));
                                            useList.add(list);
                                            map.put(wellCode, wellCode);

                                        }
                                        else {
                                            // 井能力不满足
                                            DesignStsPlPcLstEntity stsPlpc = new DesignStsPlPcLstEntity();
                                            stsPlpc.setDesginKey(designKey);
                                            stsPlpc.setOrdLnPk(delivery
                                                    .getOrdLnPk());
                                            stsPlpc.setOrdLnDlyPk(delivery
                                                    .getOrdLnDlyPk());
                                            stsPlpc.setMscPline(mscPline);
                                            stsPlpc.setWholeBacklog(wholeBackLog);
                                            stsPlpc.setWholeBacklogDesc(wholeBackLogDesc);
                                            stsPlpc.setProcessCdoe(plpclst
                                                    .getProcessCode());
                                            stsPlpc.setProcessDesc(plpclst
                                                    .getProcessDesc());
                                            stsPlpc.setPrdcycMeet("F");
                                            stsList.add(stsPlpc);

                                            // 更新生产周期状态从表
                                            DesignMscPlPcLstEntity mscPlpc = new DesignMscPlPcLstEntity();
                                            mscPlpc.setDesginKey(designKey);
                                            mscPlpc.setOrdLnDlyPk(delivery
                                                    .getOrdLnDlyPk());
                                            mscPlpc.setMscPline(mscPline);
                                            mscPlpc.setProcessCode(plpclst
                                                    .getProcessCode());
                                            mscPlpc.setLstPrdDate(date1
                                                    .toString());
                                            mscPlpc.setReviewAsk(dateToStr(date1
                                                    .toString()));
                                            mscPlpc.setPrdCycBtch(plpclst
                                                    .getPrdCycBtch());
                                            mscList.add(mscPlpc);
                                            flag += 1; // 不满足
                                        }
                                    }
                                    else {
                                        // 错误信息，产能井月产值为0或未维护。
                                        java.sql.Date ndate = new java.sql.Date(
                                                new java.util.Date().getTime());
                                        int nyear = getTimeYear(ndate);
                                        if (year > nyear) {
                                            errMsg = "产能井" + wellCode + nyear
                                                    + "年" + "到" + year
                                                    + "年之间的产能井产值都不满足";
                                        }
                                        else {
                                            errMsg = wellCode + year + "年"
                                                    + mon + "月" + "产能井月产值没有维护！";
                                        }
                                        return errMsg;
                                    }
                                }
                                else {
                                    // 错误信息，产能井基础信息（月能力）没找到
                                    java.sql.Date ndate = new java.sql.Date(
                                            new java.util.Date().getTime());
                                    int nyear = getTimeYear(ndate);
                                    if (year > nyear) {
                                        errMsg = "产能井" + wellCode + nyear + "年"
                                                + "到" + year + "年之间的产能井产值都不满足";
                                    }
                                    else {
                                        errMsg = year + "年" + wellCode
                                                + "基础信息没有找到产能井月产值"; // ???
                                    }
                                    return errMsg;
                                }
                            }
                        }
                    }
                    else {
                        errMsg = "找不到对应的产能井信息！";
                        return errMsg;
                    }
                }

                // 交货行的结尾处理
                String status = "";
                if (flag == 0) { // 该交货行总体通过了
                    if (dateAddDay(
                            dateNow,
                            Integer.parseInt(plpc.getPrdCycBtch())
                                    + cycedit.intValue()).getTime() > strToDate(
                            delivery.getDelvryEdate()).getTime()) {
                        status = "F";
                        flag = 1;
                    }
                    else {
                        status = "S";
                    }
                }
                else {
                    status = "F"; // 交货行存在不通过的工序
                }

                // 周期明细主表
                DesignStsPlPcEntity stsPl = new DesignStsPlPcEntity();
                stsPl.setDesginKey(designKey);
                stsPl.setOrdLnPk(delivery.getOrdLnPk());
                stsPl.setOrdLnDlyPk(delivery.getOrdLnDlyPk());
                stsPl.setMscPline(mscPline);
                stsPl.setWholeBacklog(wholeBackLog);
                stsPl.setWholeBacklogDesc(wholeBackLogDesc);
                stsPl.setPrdcycMeet(status);

                // 生产周期状态主表
                DesignMscPlPcEntity mscPl = new DesignMscPlPcEntity();
                mscPl.setDesginKey(designKey);
                mscPl.setMscPline(mscPline);
                mscPl.setOrdLnDlyPk(delivery.getOrdLnDlyPk());

                if (useList.size() > 0) {
                    CoreDeliveryReview cdr = new CoreDeliveryReview();
                    for (int k = 0; k < useList.size(); k++) {
                        cdr.saveWellUseList(con, useList.get(k));
                    }
                }
                // 第一次满足的不再推荐交货期,不满足的推荐交货期
                if (flag != 0) {
                    Date fdate = strToDate(dateToStr(mscList.get(0)
                            .getLstPrdDate()));
                    String s = mscList.get(0).getPrdCycBtch().toString();
                    if (s.equals(""))
                        s = "0";
                    Double d = Math.ceil(Double.parseDouble(s)
                            + Double.parseDouble(s) / cycBatch * cycedit);
                    Date datef = dateAddDay(fdate, d.intValue());
                    mscPl.setRcmdDelvryEdate(dateToStr(datef.toString()));
                }

                if (stsList != null && stsList.size() > 0) {
                    // 删除之前同一designKey的数据
                    deletePlpcLst(con, designKey, delivery.getOrdLnDlyPk());
                    deletePlpc(con, designKey, delivery.getOrdLnDlyPk());

                    for (int j = 0; j < stsList.size(); j++) {
                        savePlpcLst(con, stsList.get(j));
                    }
                    savePlpc(con, stsPl);

                    if (flag == 0) { // 满足
                        for (int k = 0; k < mscList.size(); k++) {
                            updateMscPlPcLstNoLstDate(con, mscList.get(k));
                        }
                        updateMscPlPc(con, mscPl);
                    }
                    else {// 不满足 再次计算找出推荐交货期
                        int ng = 0; // 循环推荐交货期的次数
                        for (int k = 0; k < mscList.size(); k++) {
                            updateMscPlPcLst(con, mscList.get(k));
                        }
                        updateMscPlPc(con, mscPl);
                        Date newDate = null;
                        if (dateAddDay(
                                dateNow,
                                Integer.parseInt(plpc.getPrdCycBtch())
                                        + cycedit.intValue() + 1).getTime() > strToDate(
                                delivery.getDelvryEdate()).getTime()) { // 今天+1+生产周期，作为交货期。
                            newDate = dateAddDay(dateAddDay(dateNow, 1),
                                    Integer.parseInt(plpc.getPrdCycBtch()));
                            newDate = dateAddDay(newDate, processSize);
                            newDate = dateAddDay(newDate, cycedit.intValue());
                        }
                        else { // 交货期+1一月
                            newDate = dateAddMonth(
                                    strToDate(delivery.getDelvryEdate()), 1);
                            newDate = dateAddDay(newDate, processSize); // 工序长度
                            newDate = dateAddDay(newDate, cycedit.intValue());
                        }

                        String returnVal = "";
                        returnVal = castWellNext(con, ordLnPk, newDate,
                                delivery, map);
                        while (returnVal.equals("FALSE")) {
                            ng = ng + 1;
                            Date midDate = null;
                            midDate = dateAddMonth(
                                    strToDate(delivery.getDelvryEdate()), ng);
                            midDate = dateAddDay(midDate, processSize);

                            newDate = strToDate(dateToStr(newDate.toString())); // 转化掉后面的时间
                            if (newDate.getTime() > midDate.getTime()) {
                                newDate = dateAddMonth(
                                        strToDate(delivery.getDelvryEdate()), 1);
                                newDate = dateAddDay(newDate, processSize);

                            }
                            else {
                                newDate = dateAddMonth(midDate, 0);
                            }
                            returnVal = castWellNext(con, ordLnPk, newDate,
                                    delivery, map);
                        }
                        errMsg = returnVal;
                    }
                }
            }
        }
        for (Entry<String, String> entry : map.entrySet()) {
            String value = entry.getValue();
            deleteWellListUse(con, value);
        }
        if (errMsg1.equals(""))
            return errMsg;
        else
            return errMsg1;
    }

    /**
     * 多次寻找推荐交货期
     * @param con
     * @param ordLnPk
     * @param dlyDate 推荐交货期
     * @param delivery
     * @return
     * @throws SQLException
     */
    public String castWellNext(Connection con, String ordLnPk, Date dlyDate,
            OrderLineDeliveryEntity delivery, HashMap<String, String> map)
            throws SQLException {

        String errMsg = ""; // 错误提示信息
        String designKey = "";

        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCastWell.GetDesignKey");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordLnPk);
        ResultSet rs = ps.executeQuery();

        if (rs.next()) {
            designKey = rs.getString("DESGIN_KEY");
        }
        else {
            errMsg = "找不到该合同行的DesignKey";
            return errMsg;
        }
        rs.close();
        ps.close();
        String saleOrg = ""; // 销售组织代码
        String saleOrgDesc = ""; // 销售组织描述
        int mon = 0; // 月份
        int year = 0;
        int flag = 0;
        String[] arr = getSaleInfo(con, delivery.getOrdPk());
        if (arr != null && arr.length == 2) {
            saleOrg = arr[0];
            saleOrgDesc = arr[1];
        }
        else {
            errMsg = "该合同没有维护销售组织";
            return errMsg;
        }

        Date date = null; // 总体交货日期
        Date date1 = null;

        DesignMscPlPcEntity plpc = getDesMscPlPc(con, designKey,
                delivery.getOrdLnDlyPk());
        if (plpc != null && dlyDate == null) {
            date = strToDate(plpc.getDelvryEdate()); // 交货日期
        }
        else if (plpc == null) {

        }
        else {
            date = dlyDate;
        }

        String mscPline = "";
        date1 = date;
        String[] plArr = getDesignMscPl(con, designKey); // 质量设计-制程-全程途径码与描述

        if (plArr != null && plArr.length == 3) {
            mscPline = plArr[0];
        }
        else {
            errMsg = "交货行没有经过质量设计的制程！";
            return errMsg;
        }

        List<DesignMscPlPcLstEntity> mscList = new ArrayList<DesignMscPlPcLstEntity>(); // 生产周期状态从表

        List<SlmWellUseListEntity> useList = new ArrayList<SlmWellUseListEntity>(); // 产能井预占

        List<DesignMscPlPcLstEntity> plpclsts = getDesMscPlPcLst(con,
                designKey, delivery.getOrdLnDlyPk());

        double delQty = Double.parseDouble(delivery.getWgtEstmt()); // 交货量
        double cycBatch = 0; // 总的生产时间
        Double cycedit = 0.0; // 生产周期修正

        cycBatch = getPlpcLstTime(con, designKey, delivery.getOrdLnDlyPk());

        if (plpc.getCycEdit() == null || plpc.getCycEdit().equals("")) {
            cycedit = 0.0;
        }
        else {
            cycedit = 0.0; //Double.parseDouble(plpc.getCycEdit());
        }

        for (DesignMscPlPcLstEntity plpclst : plpclsts) {

            Double s = Math.ceil(Double.parseDouble(plpclst.getPrdCycBtch())
                    + Double.parseDouble(plpclst.getPrdCycBtch()) / cycBatch
                    * cycedit);

            date1 = dateAddDay(date1, -s.intValue());
            mon = getTimeMon(date1);
            year = getTimeYear(date1);

            // 炼钢电炉暂时不考虑产能井。
            if (plpclst.getProcessCode().toString().equals("A")) {

                DesignMscPlPcLstEntity mscPlpc = new DesignMscPlPcLstEntity();
                mscPlpc.setDesginKey(designKey);
                mscPlpc.setOrdLnDlyPk(delivery.getOrdLnDlyPk());
                mscPlpc.setMscPline(mscPline);
                mscPlpc.setProcessCode(plpclst.getProcessCode());
                mscPlpc.setLstPrdDate(date1.toString());
                mscPlpc.setPrdCycBtch(plpclst.getPrdCycBtch());
                mscList.add(mscPlpc);

                continue;
            }

            double useCount = 0; // 耗用量
            String wellCode = getWellCode(con, designKey,
                    plpclst.getMscPline(), plpclst.getProcessCode());
            if (!wellCode.equals("")) { // 得到产能井号

                useCount = getWellListUse(con, wellCode, year, mon);

                List<ResBaseResourceAllocationEntity> listRS = getResource(con,
                        saleOrg, plpclst.getProcessCode(), String.valueOf(year));

                double count = 99999999;
                if (listRS != null && listRS.size() > 0) {
                    ResBaseResourceAllocationEntity res = listRS.get(0);
                    count = getResCount(res, mon); // 资源总量
                    if (count == 0) // 为0也算作无限大
                        count = 99999999;
                }
                else {
                    if (plpclst.getProcessCode().equals("D")) {
                        errMsg = saleOrgDesc + year + "年，工序"
                                + plpclst.getProcessDesc()
                                + "没有相应的销售组织的资源配置信息！";
                        return errMsg;
                    }
                }
                if (count != 0) {
                    if (delQty + useCount > count) {
                        // 资源不满足
                        flag += 1; // 不满足
                    }
                    else { // 资源满足，继续分析井的能力
                        List<ComBaseWellMonthEntity> wellMonth = getWellMonth(
                                con, wellCode, year);
                        double wellCount = 0;
                        if (wellMonth != null && wellMonth.size() > 0) {
                            ComBaseWellMonthEntity cbw = wellMonth.get(0);
                            wellCount = getWellCount(cbw, mon);
                            if (wellCount > 0) {
                                if (wellCount >= delQty) {
                                    // 井能力满足

                                    DesignMscPlPcLstEntity mscPlpc = new DesignMscPlPcLstEntity();
                                    mscPlpc.setDesginKey(designKey);
                                    mscPlpc.setOrdLnDlyPk(delivery
                                            .getOrdLnDlyPk());
                                    mscPlpc.setMscPline(mscPline);
                                    mscPlpc.setProcessCode(plpclst
                                            .getProcessCode());
                                    mscPlpc.setLstPrdDate(date1.toString());
                                    mscPlpc.setPrdCycBtch(plpclst
                                            .getPrdCycBtch());
                                    mscList.add(mscPlpc);

                                    // 预占产能井
                                    // 产能井剩余
                                    SlmWellUseListEntity list = new SlmWellUseListEntity();
                                    list.setWellCode(wellCode);
                                    list.setWellYear(String.valueOf(year));
                                    list.setUseMonth(String.valueOf(mon));
                                    list.setSaleOrg(saleOrg);
                                    list.setSaleOrgDesc(saleOrgDesc);
                                    list.setOrdLnDlyPk(delivery.getOrdLnDlyPk());
                                    list.setUseObj("0");
                                    list.setUseType("消耗");
                                    list.setUseWgt(String.valueOf(delQty));
                                    map.put(wellCode, wellCode);
                                    useList.add(list);

                                }
                                else {
                                    // 井能力不满足
                                    DesignMscPlPcLstEntity mscPlpc = new DesignMscPlPcLstEntity();
                                    mscPlpc.setDesginKey(designKey);
                                    mscPlpc.setOrdLnDlyPk(delivery
                                            .getOrdLnDlyPk());
                                    mscPlpc.setMscPline(mscPline);
                                    mscPlpc.setProcessCode(plpclst
                                            .getProcessCode());
                                    mscPlpc.setLstPrdDate(date1.toString());
                                    mscPlpc.setPrdCycBtch(plpclst
                                            .getPrdCycBtch());
                                    mscList.add(mscPlpc);
                                    flag += 1; // 不满足
                                }
                            }
                            else {
                                // 错误信息，产能井月产值为0或未维护。
                                java.sql.Date ndate = new java.sql.Date(
                                        new java.util.Date().getTime());
                                int nyear = getTimeYear(ndate);
                                if (year > nyear) {
                                    errMsg = wellCode + nyear + "年" + "到"
                                            + year + "年之间的产能井产值都不满足";
                                }
                                else {
                                    errMsg = wellCode + year + "年" + mon + "月"
                                            + "产能井月产值没有维护！";
                                }
                                return errMsg;
                            }
                        }
                        else {
                            // 错误信息，产能井基础信息（月能力）没找到
                            java.sql.Date ndate = new java.sql.Date(
                                    new java.util.Date().getTime());
                            int nyear = getTimeYear(ndate);
                            if (year > nyear) {
                                errMsg = wellCode + nyear + "年" + "到" + year
                                        + "年之间的产能井产值都不满足";
                            }
                            else {
                                errMsg = year + "年" + wellCode + "基础信息没有找到产能产值";
                            }
                            return errMsg;
                        }
                    }
                }
            }
        }

        // 生产周期状态主表
        DesignMscPlPcEntity mscPl = new DesignMscPlPcEntity();
        mscPl.setDesginKey(designKey);
        mscPl.setMscPline(mscPline);
        mscPl.setOrdLnDlyPk(delivery.getOrdLnDlyPk());
        // mscPl.setRcmdDelvryEdate(dlyDate.toString());

        Date fdate = strToDate(dateToStr(mscList.get(0).getLstPrdDate()));
        String s = mscList.get(0).getPrdCycBtch();
        if (s.equals(""))
            s = "0";
        Double d = Math.ceil(Double.parseDouble(s) + Double.parseDouble(s)
                / cycBatch * cycedit);

        Date datef = dateAddDay(fdate, d.intValue());
        mscPl.setRcmdDelvryEdate(dateToStr(datef.toString()));

        if (useList.size() > 0) {
            CoreDeliveryReview cdr = new CoreDeliveryReview();
            for (int k = 0; k < useList.size(); k++) {
                cdr.saveWellUseList(con, useList.get(k));
            }
        }

        if (flag == 0) { // 交货期满足
            // 更新生产周期主表,写入推荐交货期
            // updateMscPlPc(con, mscPl);

            for (int k = 0; k < mscList.size(); k++) {
                updateMscPlPcLstNoAsk(con, mscList.get(k));
            }
            updateMscPlPc(con, mscPl);
        }
        else { // 不满足，返回
            errMsg = "FALSE";
        }

        return errMsg;
    }

    /**
     * 获取生产周期主表信息
     * @param con
     * @param designKey 设计KEY
     * @param ordLnDlyPk 交货行
     * @return
     * @throws SQLException
     */

    public DesignMscPlPcEntity getDesMscPlPc(Connection con, String designKey,
            String ordLnDlyPk) throws SQLException {

        DesignMscPlPcEntity pLpc = new DesignMscPlPcEntity();
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCastWell.GET_MSC_PL_PC");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        ps.setObject(2, ordLnDlyPk);
        ResultSet rs = ps.executeQuery();

        while (rs.next()) {
            pLpc.setDesginKey(rs.getString("DESGIN_KEY"));
            pLpc.setOrdLnDlyPk(rs.getString("ORD_LN_DLY_PK"));
            pLpc.setMscPline(rs.getString("MSC_PLINE"));
            pLpc.setDelvryEdate(rs.getString("DELVRY_EDATE"));
            pLpc.setDelvryQty(rs.getString("DELVRY_QTY"));
            pLpc.setOrderUnit(rs.getString("ORDER_UNIT"));
            pLpc.setReceivNo(rs.getString("RECEIV_NO"));
            pLpc.setReceivNm(rs.getString("RECEIV_NM"));
            pLpc.setRcmdDelvryEdate(rs.getString("RCMD_DELVRY_EDATE"));
            pLpc.setPrdCyc(rs.getString("PRD_CYC"));
            pLpc.setPrdCycBtch(rs.getString("PRD_CYC_BTCH"));
            pLpc.setNMaterialFl(rs.getString("N_MATERIAL_FL"));
            pLpc.setCycEdit(rs.getString("CYC_EDIT"));
            pLpc.setCreateName(rs.getString("CREATE_NAME"));
            pLpc.setCreateTime(rs.getString("CREATE_TIME"));
            pLpc.setUpdateName(rs.getString("UPDATE_NAME"));
            pLpc.setUpdateTime(rs.getString("UPDATE_TIME"));
            return pLpc;
        }
        pLpc = null;
        rs.close();
        ps.close();
        return pLpc;
    }

    /**
     * 获取销售信息
     * @param con
     * @param ordPk 合同行
     * @return
     * @throws SQLException
     */
    public String[] getSaleInfo(Connection con, String ordPk)
            throws SQLException {
        return getSaleInfo(con,ordPk,2);
    }
    public String[] getSaleInfo(Connection con, String ordPk,int num)
            throws SQLException {
        String[] arr = new String[num];
        String sql = CoreXmlSqlParsersFactory.getSql("CoreCastWell.GETSaleOrg");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            arr[0] = rs.getString("SALE_ORG");
            arr[1] = rs.getString("SALE_ORG_DESC");
            if(num == 3){
                arr[2] = rs.getString("SUPPLY_UNIT");
            }
        }
        rs.close();
        ps.close();
        return arr;
    }

    /**
     * 获取生产周期从表
     * @param con
     * @param designKey
     * @param ordLnDlyPk
     * @return
     * @throws SQLException
     */
    public List<DesignMscPlPcLstEntity> getDesMscPlPcLst(Connection con,
            String designKey, String ordLnDlyPk) throws SQLException {

        List<DesignMscPlPcLstEntity> list = new ArrayList<DesignMscPlPcLstEntity>();
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCastWell.Get_MSC_PLPCLST");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        ps.setObject(2, ordLnDlyPk);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            DesignMscPlPcLstEntity des = new DesignMscPlPcLstEntity();
            des.setDesginKey(rs.getString("DESGIN_KEY"));
            des.setOrdLnDlyPk(rs.getString("ORD_LN_DLY_PK"));
            des.setMscPline(rs.getString("MSC_PLINE"));
            des.setBacklogSeq(rs.getString("BACKLOG_SEQ"));
            des.setProcessCode(rs.getString("PROCESS_CODE"));
            des.setProcessDesc(rs.getString("PROCESS_DESC"));
            des.setReviewAsk(rs.getString("REVIEW_ASK"));
            des.setLstPrdDate(rs.getString("LST_PRD_DATE"));
            des.setPrdCyc(rs.getString("PRD_CYC"));
            des.setPrdCycBtch(rs.getString("PRD_CYC_BTCH"));
            list.add(des);
        }
        rs.close();
        ps.close();
        return list;
    }

    /**
     * 获取产能井代码
     * @param con
     * @param designKey KEY
     * @param mscPline 全程产线号
     * @param processCode 工序代码
     * @return
     * @throws SQLException
     */
    public String getWellCode(Connection con, String designKey,
            String mscPline, String processCode) throws SQLException {
        String wellCode = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCastWell.GetWellCode");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        ps.setObject(2, mscPline);
        ps.setObject(3, processCode);

        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            wellCode = rs.getString(1);
        }
        rs.close();
        ps.close();
        return wellCode;
    }

    /**
     * 字符换转时间
     * @param str
     * @return
     */
    public static Date strToDate(String str) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(str);
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 时间转化Tue Jan 27 23:12:02 CST 2015 - 2015-1-27
     * @param date
     * @return
     */
    public static String dateToStr(String str) {
        // String str = "Sat Oct 10 00:00:00 CST 2009";
        SimpleDateFormat sdf = new SimpleDateFormat(
                "EEE MMM dd HH:mm:ss z yyyy", Locale.US);
        Date date = null;
        try {
            date = sdf.parse(str);
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date).toString();
    }

    /**
     * 时间加天数
     * @param date
     * @param n
     * @return
     */
    public static Date dateAddDay(Date date, int n) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.DATE, n);
        date = ca.getTime();
        format.format(date);
        System.out.println("AAAAAAAAAAAAAAAAAAAAAA"
                + format.format(date).toString());
        return date;
    }

    /**
     * 时间月份+n
     * @param date
     * @param n
     * @return
     */
    public static Date dateAddMonth(Date date, int n) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.MONTH, n);
        date = ca.getTime();
        format.format(date);
        return date;
    }

    /**
     * 得到时间的年份
     * @param date
     * @return
     */
    public static int getTimeYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int year = c.get(Calendar.YEAR);
        return year;
    }

    /**
     * 得到时间的月份
     * @param date
     * @return
     */
    public static int getTimeMon(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int mon = c.get(Calendar.MONTH) + 1;
        return mon;
    }

    /**
     * 产能井消耗
     * @param con
     * @param wellCode
     * @param year
     * @param month
     * @param saleOrg
     * @return
     * @throws SQLException
     */
    public SlmWellUseListEntity getWellUse(Connection con, String wellCode,
            String year, String month, String saleOrg) throws SQLException {
        SlmWellUseListEntity wu = null;
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCastWell.GetWellUseList");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, wellCode);
        ps.setObject(2, year);
        ps.setObject(3, month);
        ps.setObject(4, saleOrg);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            wu = new SlmWellUseListEntity();
            wu.setWellCode(rs.getString("WELL_CODE"));
            wu.setWellName(rs.getString("WELL_NAME"));
            wu.setSaleOrg(rs.getString("SALE_ORG"));
            wu.setSaleOrgDesc(rs.getString("SALE_ORG_DESC"));
            wu.setWellYear(rs.getString("WELL_YEAR"));
            wu.setUseMonth(rs.getString("USE_MONTH"));
            wu.setOrdLnDlyPk(rs.getString("ORD_LN_DLY_PK"));
            wu.setUseObj(rs.getString("USE_OBJ"));
            wu.setUseType(rs.getString("USE_TYPE"));
            wu.setUseWgt(rs.getString("USE_WGT"));
            wu.setCreateName(rs.getString("CREATE_NAME"));
            wu.setCreateTime(rs.getString("CREATE_TIME"));
            wu.setUpdateName(rs.getString("UPDATE_NAME"));
            wu.setUpdateTime(rs.getString("UPDATE_TIME"));
            wu.setDeleteName(rs.getString("DELETE_NAME"));
            wu.setDeleteTime(rs.getString("DELETE_TIME"));
        }
        rs.close();
        ps.close();
        return wu;
    }

    /**
     * 资源配置表信息
     * @param con
     * @param saleOrg
     * @param processCode
     * @param year
     * @return
     * @throws SQLException
     */
    public List<ResBaseResourceAllocationEntity> getResource(Connection con,
            String saleOrg, String processCode, String year)
            throws SQLException {
        List<ResBaseResourceAllocationEntity> rb = new ArrayList<ResBaseResourceAllocationEntity>();

        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCastWell.GetWellResource");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, saleOrg);
        ps.setObject(2, processCode);
        ps.setObject(3, year);
        ResultSet rs = ps.executeQuery();

        rb = EntityTool.getEntitysByRs(rs,
                ResBaseResourceAllocationEntity.class);
        rs.close();
        ps.close();
        return rb;
    }

    /**
     * 资源配置量
     * @param r
     * @param mon
     * @return
     */
    public double getResCount(ResBaseResourceAllocationEntity r, int mon) {
        double n = 0;
        switch (mon) {
        case 1:
            if (!r.getMonth1().equals(""))
                n = Double.parseDouble(r.getMonth1());
            break;
        case 2:
            if (!r.getMonth2().equals(""))
                n = Double.parseDouble(r.getMonth2());
            break;
        case 3:
            if (!r.getMonth3().equals(""))
                n = Double.parseDouble(r.getMonth3());
            break;
        case 4:
            if (!r.getMonth4().equals(""))
                n = Double.parseDouble(r.getMonth4());
            break;
        case 5:
            if (!r.getMonth5().equals(""))
                n = Double.parseDouble(r.getMonth5());
            break;
        case 6:
            if (!r.getMonth6().equals(""))
                n = Double.parseDouble(r.getMonth6());
            break;
        case 7:
            if (!r.getMonth7().equals(""))
                n = Double.parseDouble(r.getMonth7());
            break;
        case 8:
            if (!r.getMonth8().equals(""))
                n = Double.parseDouble(r.getMonth8());
            break;
        case 9:
            if (!r.getMonth9().equals(""))
                n = Double.parseDouble(r.getMonth9());
            break;
        case 10:
            if (!r.getMonth10().equals(""))
                n = Double.parseDouble(r.getMonth10());
            break;
        case 11:
            if (!r.getMonth11().equals(""))
                n = Double.parseDouble(r.getMonth11());
            break;
        case 12:
            if (!r.getMonth12().equals(""))
                n = Double.parseDouble(r.getMonth12());
            break;
        default:
            break;
        }

        return n;
    }

    /**
     * 产能井的产值
     * @param r
     * @param mon
     * @return
     */
    public double getWellCount(ComBaseWellMonthEntity r, int mon) {
        double n = 0;
        switch (mon) {
        case 1:
            if (!r.getMonth1().equals(""))
                n = Double.parseDouble(r.getMonth1());
            break;
        case 2:
            if (!r.getMonth2().equals(""))
                n = Double.parseDouble(r.getMonth2());
            break;
        case 3:
            if (!r.getMonth3().equals(""))
                n = Double.parseDouble(r.getMonth3());
            break;
        case 4:
            if (!r.getMonth4().equals(""))
                n = Double.parseDouble(r.getMonth4());
            break;
        case 5:
            if (!r.getMonth5().equals(""))
                n = Double.parseDouble(r.getMonth5());
            break;
        case 6:
            if (!r.getMonth6().equals(""))
                n = Double.parseDouble(r.getMonth6());
            break;
        case 7:
            if (!r.getMonth7().equals(""))
                n = Double.parseDouble(r.getMonth7());
            break;
        case 8:
            if (!r.getMonth8().equals(""))
                n = Double.parseDouble(r.getMonth8());
            break;
        case 9:
            if (!r.getMonth9().equals(""))
                n = Double.parseDouble(r.getMonth9());
            break;
        case 10:
            if (!r.getMonth10().equals(""))
                n = Double.parseDouble(r.getMonth10());
            break;
        case 11:
            if (!r.getMonth11().equals(""))
                n = Double.parseDouble(r.getMonth11());
            break;
        case 12:
            if (!r.getMonth12().equals(""))
                n = Double.parseDouble(r.getMonth12());
            break;
        default:
            break;

        }

        return n;
    }

    /**
     * 获取制程的信息
     * @param con
     * @param designKey
     * @return
     * @throws SQLException
     */
    public String[] getDesignMscPl(Connection con, String designKey)
            throws SQLException {
        String[] arr = new String[3];
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCastWell.GetDesignMscPl");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);

        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            arr[0] = rs.getString("MSC_PLINE");
            arr[1] = rs.getString("WHOLE_BACKLOG");
            arr[2] = rs.getString("WHOLE_BACKLOG_DESC");
        }
        rs.close();
        ps.close();
        return arr;

    }

    /**
     * 保存生产周期状态主表
     * @param con
     * @param stsPl
     * @throws SQLException
     */
    public void savePlpc(Connection con, DesignStsPlPcEntity stsPl)
            throws SQLException {

        if (stsPl == null) {
            return;
        }

        String sqlInsert = CoreXmlSqlParsersFactory
                .getSql("CoreCastWell.InsertPlPC");

        PreparedStatement ps = con.prepareStatement(sqlInsert);

        ps.setObject(1, stsPl.getDesginKey());
        ps.setObject(2, stsPl.getOrdLnPk());
        ps.setObject(3, stsPl.getOrdLnDlyPk());
        ps.setObject(4, stsPl.getMscPline());
        ps.setObject(5, stsPl.getWholeBacklog());
        ps.setObject(6, stsPl.getWholeBacklogDesc());
        ps.setObject(7, stsPl.getPrdcycMeet());
        ps.executeUpdate();
        ps.close();
    }

    public void deletePlpc(Connection con, String designKey, String ordDlyLnPk)
            throws SQLException {
        String sqlDelete = CoreXmlSqlParsersFactory
                .getSql("CoreCastWell.DeletePLPC");

        PreparedStatement ps = con.prepareStatement(sqlDelete);
        ps.setObject(1, designKey);
        ps.setObject(2, ordDlyLnPk);
        ps.executeUpdate();
        ps.close();

    }

    /**
     * 保存生产周期状态从表
     * @param con
     * @param stsplLst
     * @throws SQLException
     */
    public void savePlpcLst(Connection con, DesignStsPlPcLstEntity stsplLst)
            throws SQLException {

        if (stsplLst == null) {
            return;
        }

        String sqlInsert = CoreXmlSqlParsersFactory
                .getSql("CoreCastWell.InsertPlPCLST");

        PreparedStatement ps = con.prepareStatement(sqlInsert);

        ps.setObject(1, stsplLst.getDesginKey());
        ps.setObject(2, stsplLst.getOrdLnPk());
        ps.setObject(3, stsplLst.getOrdLnDlyPk());
        ps.setObject(4, stsplLst.getMscPline());
        ps.setObject(5, stsplLst.getWholeBacklog());
        ps.setObject(6, stsplLst.getWholeBacklogDesc());
        ps.setObject(7, stsplLst.getProcessCdoe());
        ps.setObject(8, stsplLst.getProcessDesc());
        ps.setObject(9, stsplLst.getPrdcycMeet());

        ps.executeUpdate();
        ps.close();
    }

    public void deletePlpcLst(Connection con, String designKey,
            String ordDlyLnPk) throws SQLException {
        String sqlDelete = CoreXmlSqlParsersFactory
                .getSql("CoreCastWell.DeleteWithDesignKey");
        PreparedStatement ps = con.prepareStatement(sqlDelete);
        ps.setObject(1, designKey);
        ps.setObject(2, ordDlyLnPk);
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 获取产能井月产值
     * @param con
     * @param wellCode
     * @param year
     * @return
     * @throws SQLException
     */
    public List<ComBaseWellMonthEntity> getWellMonth(Connection con,
            String wellCode, int year) throws SQLException {
        List<ComBaseWellMonthEntity> rb = new ArrayList<ComBaseWellMonthEntity>();

        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCastWell.SELECTWELLMONTH");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, wellCode);
        ps.setObject(2, year);

        ResultSet rs = ps.executeQuery();

        rb = EntityTool.getEntitysByRs(rs, ComBaseWellMonthEntity.class);
        rs.close();
        ps.close();
        return rb;
    }

    /**
     * 更新生产周期主表
     * @param con
     * @param plpc
     * @throws SQLException
     */
    public void updateMscPlPc(Connection con, DesignMscPlPcEntity plpc)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory.getSql("CoreCastWell.UpdatePlPC");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, plpc.getRcmdDelvryEdate().toString());
        ps.setObject(2, plpc.getDesginKey());
        ps.setObject(3, plpc.getOrdLnDlyPk());
        // ps.setObject(4, plpc.getMscPline());
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 更行生产周期从 表带评审要求
     * @param con
     * @param plpc
     * @throws SQLException
     */
    public void updateMscPlPcLst(Connection con, DesignMscPlPcLstEntity plpc)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCastWell.UpdatePlPCLST");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, dateToStr(plpc.getLstPrdDate().toString()));
        ps.setObject(2, plpc.getReviewAsk());
        ps.setObject(3, plpc.getDesginKey());
        ps.setObject(4, plpc.getOrdLnDlyPk());
        ps.setObject(5, plpc.getMscPline());
        ps.setObject(6, plpc.getProcessCode());
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 更行生产周期从表 不带评审要求
     * @param con
     * @param plpc
     * @throws SQLException
     */
    public void updateMscPlPcLstNoAsk(Connection con,
            DesignMscPlPcLstEntity plpc) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCastWell.UpdatePlPCLSTNOASK");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, dateToStr(plpc.getLstPrdDate().toString()));
        ps.setObject(2, plpc.getDesginKey());
        ps.setObject(3, plpc.getOrdLnDlyPk());
        ps.setObject(4, plpc.getMscPline());
        ps.setObject(5, plpc.getProcessCode());
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 更行生产周期从表 不带最迟生产时间 带评审要求
     * @param con
     * @param plpc
     * @throws SQLException
     */
    public void updateMscPlPcLstNoLstDate(Connection con,
            DesignMscPlPcLstEntity plpc) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCastWell.UpdatePlPCLSTNOLSTDATE");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, plpc.getReviewAsk());
        ps.setObject(2, plpc.getDesginKey());
        ps.setObject(3, plpc.getOrdLnDlyPk());
        // ps.setObject(4, plpc.getMscPline());
        ps.setObject(4, plpc.getProcessCode());
        ps.executeUpdate();
        ps.close();
    }

    /**
     * 获取产能井的总消耗量（三个销售组织之和）
     * @param con 连接
     * @param wellCode 产能井号
     * @param year 年
     * @param month 月
     * @return
     * @throws SQLException
     */
    public double getWellListUse(Connection con, String wellCode, int year,
            int month) throws SQLException {
        double count = 0;

        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreDeliveryReview.GetWellUse");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, wellCode);
        ps.setObject(2, year);
        ps.setObject(3, month);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            count = rs.getDouble(1);
        }
        rs.close();
        ps.close();
        return count;
    }

    /**
     * 获取产能井指定销售组织的消耗量
     * @param con
     * @param wellCode
     * @param year
     * @param month
     * @param saleOrg
     * @return
     * @throws SQLException
     */
    public double getWellListUseSaleOrg(Connection con, String wellCode,
            int year, int month, String saleOrg) throws SQLException {
        double count = 0;

        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreDeliveryReview.GetWellUseSaleOrg");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, wellCode);
        ps.setObject(2, year);
        ps.setObject(3, month);
        ps.setObject(4, saleOrg);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            count = rs.getDouble(1);
        }
        rs.close();
        ps.close();
        return count;
    }

    public void deleteWellListUse(Connection con, String wellCode)
            throws SQLException {

        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreDeliveryReview.DeleteWellUse");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, wellCode);

        ps.executeUpdate();
        ps.close();
    }

    /**
     * 获取ORDPK下的所有ORDLNPK
     * @param con
     * @param ordPk
     * @return
     * @throws SQLException
     */
    public List<String> getOrderLnPk(Connection con, String ordPk)
            throws SQLException {
        List<String> list = new ArrayList<String>();
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCastWell.SelectOrdLnPk");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, ordPk);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            list.add(rs.getString(1));
        }
        rs.close();
        ps.close();
        return list;
    }

    /**
     * 获取交货行的每个工序的生产周期之和
     * @param con
     * @param designKey
     * @param ordLnPk
     * @return
     * @throws SQLException
     */
    public Double getPlpcLstTime(Connection con, String designKey,
            String ordLnDlyPk) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreCastWell.getPlpcLstTime");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, designKey);
        ps.setObject(2, ordLnDlyPk);
        ResultSet rs = ps.executeQuery();
        Double time = 0.0;
        if (rs.next()) {
            time = rs.getDouble(1);
        }
        rs.close();
        ps.close();
        return time;

    }
}
