package com.steering.ydm.bc;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.db.DBCommon;
import com.steering.common.EntityTool;
import com.steering.mes.mcp.entity.YdmBcSupervisorEntity;
import org.apache.ibatis.jdbc.SQL;

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


public class FrmSupervisonInfo extends DBCommon {

    /***
     * 查询产线
     * @return
     */
    public CoreReturnObject doQueryPline()
    {
        return  this.getDataBySqlID("FrmSupervisonInfo.doQueryPline");
    }


    /***
     *
     */
    public CoreReturnObject doQueryStorageNo()
    {
        return this.getDataBySqlID("FrmSupervisonInfo.doQueryStorageNo");
    }
    /***
     * 查询
     * @return
     */
    public CoreReturnObject doQuery(ArrayList<String> parmList)
    {
        String sql = CoreXmlSqlParsersFactory.getSql("FrmSupervisonInfo.doQuery");
        if(!parmList.get(0).equals(""))
        {
            sql += " AND T.PLINE_CODE = '"+parmList.get(0)+"'";
        }
        if(!parmList.get(1).equals(""))
        {
            sql += " AND T.SUPERVISOR_NAME LIKE '%'"+parmList.get(1)+"'%'";
        }
        return  this.getData(sql);
    }

    /***
     * 新增监装人信息
     * @return
     */
    public CoreReturnObject doAdd(String ydmEntity)
    {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
        String systime = dateFormat.format(now);
        try {
            ArrayList<String> sqllist = new ArrayList<String>();
            YdmBcSupervisorEntity ydmBcEntity = EntityTool.getEntityByJSONStr(ydmEntity,YdmBcSupervisorEntity.class);
            ydmBcEntity.setCreateTime(systime);
            String insertSql = CoreXmlSqlParsersFactory.getSql("FrmSupervisonInfo.doAdd");
            insertSql  = EntityTool.fillSqlByEntity(insertSql,ydmBcEntity);
            sqllist.add(insertSql);
            cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
            if(cro.getV_errMsg().equals(""))
            {
                cro.setV_errMsg("新增成功!");
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                    cro.setV_errMsg(e.getMessage());
                    connection.rollback(); // 回滚事务
                } catch (Exception e2) {
                }
            }
        } finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (Exception e2) {
            }
        }
        return cro;
    }

    /***
     * 删除
     * @return
     */
    public CoreReturnObject doDelete(String ydmEntity)
    {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();

        try {
            ArrayList<String> sqllist = new ArrayList<String>();
            YdmBcSupervisorEntity ydmBcEntity = EntityTool.getEntityByJSONStr(ydmEntity,YdmBcSupervisorEntity.class);
            String deleteSql = CoreXmlSqlParsersFactory.getSql("FrmSupervisonInfo.doDelete");
            deleteSql  = EntityTool.fillSqlByEntity(deleteSql,ydmBcEntity);
            sqllist.add(deleteSql);
            cro = this.getDao(CoreConstantData.sysbeanstr).ExcuteTransction(sqllist);
            if(cro.getV_errMsg().equals(""))
            {
                cro.setV_errMsg("删除成功!");
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
            if (connection != null) {
                try {
                    cro.setV_errMsg(e.getMessage());
                    connection.rollback(); // 回滚事务
                } catch (Exception e2) {
                }
            }
        } finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (Exception e2) {
            }
        }
        return cro;
    }
}
