package com.steering.mes.mcp.Mch;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.HttCrackSampleTypeEntity;

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

public class HttCrackSampleTypeService extends DBCommon {

    public CoreReturnObject query(ArrayList<Object> parm) {
        String sql = CoreXmlSqlParsersFactory
                .getSql("HttCrackSampleType.query");
        if (!parm.get(0).equals("")) {
            sql += " AND t.SPEC_NAME LIKE '%" + parm.get(0) + "%'";
        }
        return this.getData(sql);
    }

    public CoreReturnObject insert(String obj, String userName) {
        CoreReturnObject core = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        try {
            HttCrackSampleTypeEntity typeEntity = EntityTool.getEntityByJSONStr(obj, HttCrackSampleTypeEntity.class);
            if (typeEntity.getTypeNo().equals("")) {
                //获取id
                typeEntity.setTypeNo(strNo());
                typeEntity.setCreateName(userName);
                typeEntity.setValidflag("1");

                String sqlAdd = CoreXmlSqlParsersFactory
                        .getSql("HttCrackSampleType.insert");// 新增
                sqlAdd = EntityTool.fillSqlByEntity(sqlAdd, typeEntity);
                PreparedStatement ps = con.prepareStatement(sqlAdd);
                ps.executeUpdate();
                ps.close();

            } else {

                String sqlUpdate = CoreXmlSqlParsersFactory
                        .getSql("HttCrackSampleType.update");// 修改

                typeEntity.setUpdateName(userName);

                sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, typeEntity);
                PreparedStatement ps = con.prepareStatement(sqlUpdate);
                ps.executeUpdate();
                ps.close();

            }
            core.setV_errMsg("保存成功！");
            core.setResult(1);
        } catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (con != null) {
                try {
                    con.rollback(); // 回滚事务
                } catch (Exception e2) {
                }
            }
        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            } catch (Exception e2) {
            }
        }
        return core;
    }

    /**
     * 废除
     *
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject delete(String obj,String userName) {
        CoreReturnObject core = new CoreReturnObject();
        Connection con = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        try {
            HttCrackSampleTypeEntity typeEntity = EntityTool.getEntityByJSONStr(obj, HttCrackSampleTypeEntity.class);
            String sqlUpdate = CoreXmlSqlParsersFactory
                    .getSql("HttCrackSampleType.delete");

            typeEntity.setDeleteName(userName);
            typeEntity.setValidflag("0");

            sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, typeEntity);
            PreparedStatement ps = con.prepareStatement(sqlUpdate);
            ps.executeUpdate();
            ps.close();

            core.setV_errMsg("删除成功！");
            core.setResult(1);
        } catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (con != null) {
                try {
                    con.rollback(); // 回滚事务
                } catch (Exception e2) {
                }
            }
        } finally {
            try {
                if (con != null && !con.isClosed()) {
                    con.close();
                }
            } catch (Exception e2) {
            }
        }
        return core;
    }

    public String queryCount() throws SQLException {
        String str = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("HttCrackSampleType.queryCount");
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = connection.prepareStatement(sql);
        ResultSet rs = psta.executeQuery();
        if (rs.next()) {
            str = rs.getString("COUNT(*)");
        }
        rs.close();
        psta.close();
        return str;
    }

    public String strNo() throws SQLException {
        SimpleDateFormat f = new SimpleDateFormat("yyMMdd");
        String yyMMdd = f.format(new Date());
        String strId = "T" + yyMMdd;
        String str = String.valueOf(Integer.valueOf(queryCount()) + 1);
        while (str.length() < 3) {
            str = "0" + str;
        }
        return strId + str;
    }
}
