package com.steering.mes.mcp.Mch;

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

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

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

public class FrmMchPerson extends DBCommon{
    public CoreReturnObject doQuery(String banci,String pline) {

        String sql = "";
        sql = CoreXmlSqlParsersFactory
                .getSql("FrmMchPerson.doQuery");

        if (!banci.equals("")) {
            sql += " AND T.PRO_BZ ='" + banci + "'";
        }     
        if (!pline.equals("")) {
            sql += " AND T.PLINE_CODE ='" + pline + "'";
        }   
        sql += " ORDER BY T.PLINE_CODE";
        return this.getData(sql);
    }
    
    public CoreReturnObject doQuerypline() {
    	String sql = CoreXmlSqlParsersFactory
                .getSql("FrmMchPerson.doQuerypline");
        return this.getData(sql);
    }
    
    
    /**
     * 新增
     * @param load
     * @return
     */
    public CoreReturnObject addPerson(String name, String pline, String banci) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        Date now = new Date();
        SimpleDateFormat matter1 = new SimpleDateFormat("yyMM");
        SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat matter2 = new SimpleDateFormat("yyyyMMddHHmm");
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DATE));
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String systime = dateFormat.format(now);
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlAdd = CoreXmlSqlParsersFactory
                    .getSql("FrmMchPerson.addPerson");// 新增操作人员
            PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
            //查询最大人员编号
            String mancode = "";
            String sqlloading = CoreXmlSqlParsersFactory
                    .getSql("FrmMchPerson.queryOffMaxRESULT");// 查询最大的人员编号
            psta = connection.prepareStatement(sqlloading);
            psta.setObject(1, matter1.format(now));
            ResultSet rs1 = psta.executeQuery();
            if (rs1.next()) {
                mancode = rs1.getString("MAN_CODE");
            }
            rs1.close();
            psta.close();
            // 新增操作人员
            patAdd.setObject(1, name);
            patAdd.setObject(2, mancode);
            patAdd.setObject(3, pline);
            patAdd.setObject(4, banci);
            patAdd.addBatch();
            patAdd.executeBatch();
            core.setV_errMsg("新增成功！");
            core.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;
    }
    
    /**
     * 修改
     * @param load
     * @return
     */
    public CoreReturnObject updatePerson(FrmMchPersonentity load,String name) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        Date now = new Date();
        SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
        SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat matter2 = new SimpleDateFormat("yyyyMMddHHmm");
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DATE));
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String systime = dateFormat.format(now);
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlAdd = CoreXmlSqlParsersFactory
                    .getSql("FrmMchPerson.updatePerson");// 修改
            PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
            // 修改人员
            patAdd.setObject(1, name);
            patAdd.setObject(2, load.getPlineCode());
            patAdd.setObject(3, load.getProBc());
            patAdd.setObject(4, load.getManName());
            patAdd.setObject(5, load.getManCode());
            patAdd.addBatch();
            patAdd.executeBatch();
            core.setV_errMsg("修改成功！");
            core.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;
    }
    
    /**
     * 删除
     * @param load
     * @return
     */
    public CoreReturnObject deletePerson(String name,String code) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;
        Date now = new Date();
        SimpleDateFormat matter1 = new SimpleDateFormat("yyMMdd");
        SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat matter2 = new SimpleDateFormat("yyyyMMddHHmm");
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DATE, ca.getActualMaximum(Calendar.DATE));
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String systime = dateFormat.format(now);
        try {
            connection.setAutoCommit(false); // 取消自动提交事务
            String sqlAdd = CoreXmlSqlParsersFactory
                    .getSql("FrmMchPerson.deletePerson");// 修改
            PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
            // 删除人员
            patAdd.setObject(1, name);
            patAdd.setObject(2, code);
            patAdd.addBatch();
            patAdd.executeBatch();
            core.setV_errMsg("删除成功！");
            core.setResult(1);
            connection.commit();
        }
        catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                }
                catch (Exception e2) {
                }
            }
        }
        finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            }
            catch (Exception e2) {
            }
        }
        return core;
    }
	

}
