package com.steering.mes.mcp.coup;

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

import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.CouplingOutsourcResultEntity;

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

public class FrmWeiTuo extends DBCommon{
	/*查询委托单信息*/
	public CoreReturnObject geQueryEntrusted(ArrayList<String> list) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("FrmWeiTuo.geQueryEntrusted");
        if (!list.get(0).equals("")) {
			sql += " AND T.ENTRUSTED_NO || '&' LIKE '%' || '" + list.get(0)
					+ "' || '%'";
		}
		if (!list.get(1).equals("")) {
			sql += " AND T.PLINE_CODE='" + list.get(1)+ "'";
		}
		// sql += CoreConverter.toSqlConditionString("T.PLINE_CODE", plineCode);
		if (!list.get(2).equals("")) {
			sql += " AND T.ENTRUSTED_LOG ='" + list.get(2) + "'";
		}
	    if (!list.get(3).equals("")) {
				sql += " AND T.JUDGE_STOVE_NO ='" + list.get(3) + "'";
			}
        return this.getData(sql);
    }
	 /**
     * 审批通过
     * @param r
     * @return·
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject commEntrustedResult(ArrayList<Object> pram,String name) {
        CoreReturnObject cor = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        try {
        	 Date now = new Date();
     		SimpleDateFormat dateFormat = new SimpleDateFormat(
     				"yyyy-MM-dd HH:mm:ss");
     		String systime = dateFormat.format(now);
            con.setAutoCommit(false); // 取消自动提交事务
            for (int i = 0; i < pram.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                String entrustedNo = list.get(0);
                CouplingOutsourcResultEntity outEntity=FrmDownLineManage.getSendOut(con,entrustedNo);
				
				if (outEntity==null) {
					cor.setV_errMsg("选择委外信息已撤銷,不允许此操作!");
					return cor;
				} 
				if (!outEntity.getEntrustedLog().equals("2")) {
					cor.setV_errMsg("选择委外信息不是已申请状态,不允许此操作!");
					return cor;
				}
				// 修改委外加工表
				String deleteOff ="UPDATE COUPLING_OUTSOURC_RESULT T SET T.ENTRUSTED_LOG='3',T.AUDIT_NAME=?, T.AUDIT_TIME = TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') WHERE ENTRUSTED_NO = ?";// 修改委外標準
				psta = con.prepareStatement(deleteOff);
				psta.setObject(1, name);
				psta.setObject(2, systime);
				psta.setObject(3, entrustedNo);
				psta.executeUpdate();
				psta.close();
            }
            cor.setV_errMsg("审批通过!");
            con.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            if (con != null) {
                try {
                    cor.setV_errMsg(e.getMessage());

                    con.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cor;
    }
    /**
     * 审批不通过
     * @param r
     * @return·
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject kownCommEntrustedResult(ArrayList<Object> pram,String name) {
        CoreReturnObject cor = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        try {
        	 Date now = new Date();
      		SimpleDateFormat dateFormat = new SimpleDateFormat(
      				"yyyy-MM-dd HH:mm:ss");
      		String systime = dateFormat.format(now);
            con.setAutoCommit(false); // 取消自动提交事务
            for (int i = 0; i < pram.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                String entrustedNo = list.get(0);
                CouplingOutsourcResultEntity outEntity=FrmDownLineManage.getSendOut(con,entrustedNo);
				
				if (outEntity==null) {
					cor.setV_errMsg("选择委外信息已撤銷,不允许此操作!");
					return cor;
				} 
				if (!outEntity.getEntrustedLog().equals("2")) {
					cor.setV_errMsg("选择委外信息不是已申请状态,不允许此操作!");
					return cor;
				}
				// 修改委外加工表
				String deleteOff ="UPDATE COUPLING_OUTSOURC_RESULT T SET T.ENTRUSTED_LOG='4',T.AUDIT_NAME=?, T.AUDIT_TIME = TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS') WHERE ENTRUSTED_NO = ?";// 修改委外標準
				psta = con.prepareStatement(deleteOff);
				psta.setObject(1, name);
				psta.setObject(2, systime);
				psta.setObject(3, entrustedNo);
				psta.executeUpdate();
				psta.close();
				
				// 修改离线表返线支数
				String updateOff = CoreXmlSqlParsersFactory
						.getSql("FrmHttDownLineManage.UpdateOffOutSourc");// 修改离线表委外支数	
				psta = con.prepareStatement(updateOff);
				psta.setObject(1, "");
				psta.setObject(2, "-"+outEntity.getEntrustedNum1());
				psta.setObject(3, "-"+outEntity.getEntrustedWt1());
				psta.setObject(4, "");
				psta.setObject(5, "");
				psta.setObject(6, outEntity.getResultNo());
				psta.setObject(7, outEntity.getOffXh());
				psta.executeUpdate();
				psta.close();

            }
            cor.setV_errMsg("审批不通过!");
            con.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            if (con != null) {
                try {
                    cor.setV_errMsg(e.getMessage());

                    con.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cor;
    }

    /**
     * 委外回报
     * @param r
     * @return·
     */
    @SuppressWarnings("unchecked")
    public CoreReturnObject doOutsourcReturn(ArrayList<Object> pram,String name) {
        CoreReturnObject cor = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        try {
            con.setAutoCommit(false); // 取消自动提交事务
            Date now = new Date();
    		SimpleDateFormat dateFormat = new SimpleDateFormat(
    				"yyyy-MM-dd HH:mm:ss");
    		String systime = dateFormat.format(now);
            for (int i = 0; i < pram.size(); i++) {
                ArrayList<String> list = (ArrayList<String>) pram.get(i);
                String subcontractcompleteNum = list.get(0);
                String subcontractcompleteWt = list.get(1);
                String hbFpNum = list.get(2);
                String hbFpWt = list.get(3);
                String entrustedNo = list.get(4);
                CouplingOutsourcResultEntity outEntity=FrmDownLineManage.getSendOut(con,entrustedNo);
				
				if (outEntity==null) {
					cor.setV_errMsg("选择委外信息撤銷,不允许此操作!");
					return cor;
				} 
				if (!outEntity.getEntrustedLog().equals("3")) {
					cor.setV_errMsg("选择委外信息不是审批通过,不允许此操作!");
					return cor;
				}
				// 修改委外加工表
				String update =CoreXmlSqlParsersFactory
						.getSql("FrmWeiTuo.UpdateSubCount");// 修改委外標準;// 修改委外標準
				psta = con.prepareStatement(update);
				psta.setObject(1, subcontractcompleteNum);
				psta.setObject(2, subcontractcompleteWt);
				psta.setObject(3, hbFpNum);
				psta.setObject(4, hbFpWt);
				psta.setObject(5, systime);
				psta.setObject(6, name);
				psta.setObject(7, "5");
				psta.setObject(8, entrustedNo);
				psta.executeUpdate();
				psta.close();
				
				// 修改离线表返线支数
				String updateOff = CoreXmlSqlParsersFactory
						.getSql("FrmWeiTuo.UpdateOffSubCount");// 修改离线表委外支数	
				psta = con.prepareStatement(updateOff);
				psta.setObject(1, subcontractcompleteNum);
				psta.setObject(2, subcontractcompleteWt);
				psta.setObject(3, hbFpNum);
				psta.setObject(4, hbFpWt);
				psta.setObject(5, systime);
				psta.setObject(6, name);
				psta.setObject(7, outEntity.getResultNo());
				psta.setObject(8, outEntity.getOffXh());
				psta.setObject(9, entrustedNo);
				psta.executeUpdate();
				psta.close();

            }
            cor.setV_errMsg("委外回报成功！");
            con.commit();
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            if (con != null) {
                try {
                    cor.setV_errMsg(e.getMessage());

                    con.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return cor;
    }
}
