package com.steering.pss.sale.order;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.comm.tools.StringTool;

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

/**
 * 监制维护界面
 * @author Administrator
 */
public class CoreOrderProducer extends DBCommon {
    /**
     * 合同交货期评审界面数据查询.
     * @param doQuery
     * @param appdate 签约时间
     * @param order 合同号
     * @param status 审批状态
     * @param arr 权限数组
     * @param customInfo 界面参数
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderProducerHeadQuery(String appdate,
            String order, String[] status, String[] arr, String customInfo,
            String tabNum) {
           String statusSql="";
        if (tabNum.equals("1")) {
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderProducer.CoreOrderProducerHeadQuery");

            sql = sql.replaceAll("#TIMECON#", appdate);
            if (StringTool.isNullOrEmpty(order))
                order = "";
            sql = sql.replaceAll("#ORDER_NO#", order);
            String condition = CoreConverter
                    .toSqlConditionString("T1.CREATE_DEPT", arr);
            sql = sql.replaceAll("#DATAACCESS#", condition);

            if (customInfo.equals("1")) { // 监制维护界面
                sql += "  WHERE  R.PRDCR_STS IN ('L','M'  ,'W',  'F','S' )  ";
//                sql += "  WHERE  R.PRDCR_STS IN ('L','M'  ,'W',  'F' )  ";
            }
            else { // 监制审批界面
                sql += " WHERE  R.PRDCR_STS IN ('S','W') ";
            }
            if (status.length>0&&status!=null) {
                statusSql = CoreConverter.toSqlConditionString(" R.PRDCR_STS", status);
               sql+=statusSql;
            }

            return this.getData(sql);
        }
        else {
            String sql = CoreXmlSqlParsersFactory
                    .getSql("CoreOrderProducer.CoreOrderProducerHeadQueryView");

            sql = sql.replaceAll("#TIMECON#", appdate);
            if (StringTool.isNullOrEmpty(order))
                order = "";
            sql = sql.replaceAll("#ORDER_NO#", order);
            String condition = CoreConverter
                    .toSqlConditionString("T1.CREATE_DEPT", arr);
            sql = sql.replaceAll("#DATAACCESS#", condition);
            //
            // if (customInfo.equals("1")) { // 监制维护界面
            // sql += " WHERE R.PRDCR_STS IN ('L','M' ,'W', 'F' ) ";
            // }
            // else { // 监制审批界面
            // sql += " WHERE R.PRDCR_STS IN ('S','W') ";
            // }

            if (status.length>0) {
                statusSql = CoreConverter.toSqlConditionString(" R.PRDCR_STS", status);
                sql+="  where 1=1  "+statusSql;
            }

            return this.getData(sql);
        }

    }

    /**
     * 获取监制主表信息
     * @param prdcrNo
     * @return
     */
    public CoreReturnObject getOrderLinePrdcr(String prdcrNo) {
        Object[] obj = new Object[] { prdcrNo };
        return this.getDataBySqlID("CoreOrderProducer.SelectOrderLineProdcr",
                obj);
    }

    /**
     * 获取监制从表信息
     * @param prdcrNo
     * @return
     */
    public CoreReturnObject getOrderLinePrdcrLst(String prdcrNo) {
        Object[] obj = new Object[] { prdcrNo };
        return this.getDataBySqlID("CoreOrderProducer.SelectOrderLinePrdcrLst",
                obj);
    }

    /**
     * 合同产品生产监制要求监制从表信息单位绑定
     * @param order 合同范本
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderProducerPdeptBase() {
        return this
                .getDataBySqlID("CoreOrderProducer.CoreOrderProducerPdeptBase");
    }

    /**
     * 合同产品生产监制要求监制从表信息监制类型绑定
     * @param order 合同范本
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderProducerPrcBase() {
        return this
                .getDataBySqlID("CoreOrderProducer.CoreOrderProducerPrcBase");
    }

    /**
     * 合同产品生产监制要求监制从表信息根据单位找出电话
     * @param phone 合同范本
     * @return CoreReturnObject
     */
    public CoreReturnObject CoreOrderProducerPhoneQuery(String phone) {
        Object[] obj = new Object[] { phone };
        return this.getDataBySqlID(
                "CoreOrderProducer.CoreOrderProducerPhoneQuery", obj);
    }

    /**
     * 合同产品生产监制要求监制审批提报功能
     * @param name
     * @return
     */
    public CoreReturnObject coreOrderProducerDeptProFe(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();

        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);

            for (int i = 0; i < parm.size(); i++) {
                List<String> list = getOrderLinePrdcrStatus(con,
                        parm.get(i).toString());
                if (list.contains("L")) {
                    con.rollback();
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("监制没有维护完整，不能提报！");
                }
                else if (list.contains("W")) {
                    con.rollback();
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("监制已经提报，不能提报！");
                }
                else if (list.contains("S")) {
                    con.rollback();
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("监制已经生效，不能提报！");
                }
                String sql = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderProducer.CoreOrderProducerDeptProFe");
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setObject(1, parm.get(i));
                ps.executeUpdate();
                ps.close();
            }
            con.commit();
            cro.setV_errCode(0);

        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setV_errMsg(e.getMessage());
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cro;
    }

    /**
     * 合同产品生产监制要求监制审批退回功能
     * @param name
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject coreOrderProducerDeptProBa(ArrayList<Object> parm) {

        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            for (int i = 0; i < parm.size(); i++) {
                ArrayList<Object> clist = (ArrayList<Object>) parm.get(i);
                List<String> list = getOrderLinePrdcrStatus(con,
                        clist.get(clist.size() - 1).toString());
                if (list.contains("S")) {
                    con.rollback();
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("监制已经生效,不能回退！");
                }
                String sql = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderProducer.CoreOrderProducerDeptProBa");
                PreparedStatement ps = con.prepareStatement(sql);
                for (int j = 0; j < clist.size(); j++) {
                    ps.setObject(j + 1, clist.get(j));
                }
                ps.executeUpdate();
                ps.close();
            }
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setV_errMsg(e.getMessage());
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cro;
    }

    /**
     * 合同产品生产监制要求监制审批通过功能
     * @param name
     * @return
     */
    public CoreReturnObject coreOrderProducerDeptProsu(ArrayList<Object> parm,String approver) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;
        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            for (int i = 0; i < parm.size(); i++) {
                List<String> list = getOrderLinePrdcrStatus(con,
                        parm.get(i).toString());
                if (list.contains("S")) {
                    con.rollback();
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("监制已经生效,不能再次生效!");
                }
                String sql = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderProducer.CoreOrderProducerDeptProsu");
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setObject(1, approver);
                ps.setObject(2, parm.get(i));
                ps.executeUpdate();
                ps.close();
            }
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setV_errMsg(e.getMessage());
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }

        }
        finally {
            try {
                if (con != null && con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cro;

    }

    public CoreReturnObject getPrdcrDesc() {
        return this.getDataBySqlID("CoreOrderProducer.GetPrdCrDesc");
    }

    /**
     * 获取监制状态
     * @param orderNo
     * @return
     */
    public CoreReturnObject getPrdcrStatus(String prdcrNo,
            String prdcrNoIndex) {
        Object[] obj = new Object[] { prdcrNo, prdcrNoIndex };
        return this.getDataBySqlID("CoreOrderProducer.GetPrdcrStatus", obj);
    }

    /**
     * 监制编码下的所有状态
     * @param prdcrNo
     * @return
     */
    public CoreReturnObject getPrdcrAllStatus(String prdcrNo) {
        Object[] obj = new Object[] { prdcrNo };
        return this.getDataBySqlID("CoreOrderProducer.GetPrdcrAllStatus", obj);
    }

    /**
     * 获取合同状态
     * @param ordNo
     * @return
     */
    public CoreReturnObject getOrderStatus(String ordNo) {
        Object[] obj = new Object[] { ordNo };
        return this.getDataBySqlID("CoreOrderProducer.GetOrderStatus", obj);
    }

    public CoreReturnObject getDeptPerson(String deptNo) {
        Object[] obj = new Object[] { deptNo };
        return this.getDataBySqlID("CoreOrderProducer.GetDeptPerson", obj);
    }

    public CoreReturnObject getPrdcrStationCode(String code) {
        Object[] obj = new Object[] { code };
        return this.getDataBySqlID("CoreOrderProducer.getPrdcrStationCode",
                obj);
    }

    /**
     * 是否已存在监制编码
     * @param prdcrNo 监制编码集合 以逗号隔开
     * @param mscPline
     * @return
     */
    public CoreReturnObject isExistPrdcrNo(String prdcrNo, String mscPline) {
        Object[] obj = new Object[] { prdcrNo, mscPline };
        return this.getDataBySqlID("CoreOrderProducer.IsExistPrdcrNo", obj);
    }

    public CoreReturnObject selectPrdCrNoArr(String ordLnPk) {
        Object[] obj = new Object[] { ordLnPk };
        return this.getDataBySqlID("CoreOrderProducer.SelectPrdCrNoArr", obj);
    }

    /**
     * 监制保存功能
     * @param prdcr
     * @param prdcrLst
     * @param del
     * @return
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject save(ArrayList<Object> prdcr,
            ArrayList<Object> prdcrLst, ArrayList<Object> del) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;

        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            for (int i = 0; i < prdcr.size(); i++) {
                ArrayList<Object> prd = (ArrayList<Object>) prdcr.get(i);
                String sql = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderProducer.GetPrdcrStatus");
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setObject(1, prd.get(3));
                ps.setObject(2, prd.get(4));
                ResultSet rs = ps.executeQuery();
                String sts = "";
                if (rs.next()) {
                    sts = rs.getString(1);
                }
                
                //改流程注释
                if (sts.equals("W")) {
//                if (sts.equals("W") || sts.equals("S")) {
                    cro.setV_errCode(-1);
                    cro.setV_errMsg("监制所处状态不允许保存！");
                    con.rollback();
                    return cro;
                }
                //改流程注释
                
                rs.close();
                ps.close();
                String sql1 = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderProducer.UpdateOrderLinePrdcr");
                ps = con.prepareStatement(sql1);
                for (int j = 0; j < prd.size(); j++) {
                    ps.setObject(j + 1, prd.get(j));
                }
                ps.executeUpdate();
                ps.close();
            }

            for (int i = 0; i < prdcrLst.size(); i++) {
                ArrayList<Object> clist = (ArrayList<Object>) prdcrLst.get(i);
                String sql2 = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderProducer.UpdateOrderLinePrdcrLst");
                PreparedStatement ps = con.prepareStatement(sql2);
                for (int n = 0; n < clist.size(); n++) {
                    ps.setObject(n + 1, clist.get(n));
                }
                ps.executeUpdate();
                ps.close();
            }

            for (int i = 0; i < del.size(); i++) {
                ArrayList<Object> delst = (ArrayList<Object>) del.get(i);
                String sql = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderProducer.ClearOrderLinePrdcrLst");
                PreparedStatement ps = con.prepareStatement(sql);
                for (int j = 0; j < delst.size(); j++) {
                    ps.setObject(j + 1, delst.get(j));
                }
                ps.executeUpdate();
                ps.close();
            }
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setV_errMsg(e.getMessage());
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;

    }

    public CoreReturnObject doAddHt(ArrayList<Object> prdcr,ArrayList<Object> prdcrLst) {
        CoreReturnObject cro = new CoreReturnObject();
        Connection con = null;

        try {
            con = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            con.setAutoCommit(false);
            for (int i = 0; i < prdcr.size(); i++) {
                ArrayList<Object> prd = (ArrayList<Object>) prdcr.get(i);
                String sql1 = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderProducer.UpdateOrderLinePrdcrHT");
                PreparedStatement ps = con.prepareStatement(sql1);
                for (int j = 0; j < prd.size(); j++) {
                    ps.setObject(j + 1, prd.get(j));
                }
                ps.executeUpdate();
                ps.close();
            }

            for (int i = 0; i < prdcrLst.size(); i++) {
                ArrayList<Object> clist = (ArrayList<Object>) prdcrLst.get(i);
                String sql2 = CoreXmlSqlParsersFactory
                        .getSql("CoreOrderProducer.UpdateOrderLinePrdcrLstHT");
                PreparedStatement ps = con.prepareStatement(sql2);
                for (int n = 0; n < clist.size(); n++) {
                    ps.setObject(n + 1, clist.get(n));
                }
                ps.executeUpdate();
                ps.close();
            }
            con.commit();
            cro.setV_errCode(0);
        }
        catch (SQLException e) {
            try {
                con.rollback();
                cro.setV_errCode(-1);
                cro.setV_errMsg(e.getMessage());
            }
            catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
        finally {
            try {
                if (con != null && con.isClosed()) {
                    con.close();
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cro;

    }

    /**
     * 获取监制的所有状态
     * @param con
     * @param prdcrNo
     * @return
     * @throws SQLException
     */
    public List<String> getOrderLinePrdcrStatus(Connection con, String prdcrNo)
            throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("CoreOrderProducer.GetPrdcrAllStatus");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1, prdcrNo);
        ResultSet rs = ps.executeQuery();
        List<String> list = new ArrayList<String>();
        while (rs.next()) {
            list.add(rs.getString(1));
        }
        return list;
    }

}
