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.YdmBsMaxwgtEntity;

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

public class FrmBaseCrane  extends DBCommon {

    /***
     * 查询
     * @return
     */
    public CoreReturnObject doQuery(ArrayList<String> parmList) {
        String sql = CoreXmlSqlParsersFactory.getSql("FrmBaseCrane.doQuery");
        if (!parmList.get(0).equals("")) {
            sql += " AND T.STORAGE_NO LIKE '%" + parmList.get(0) + "'%";
        }
        return this.getData(sql);
    }
   
    
//	TZH 24.4.24 注销物流接口
//    /***
//     * 查询装卸点
//     * @return
//     */
//    public CoreReturnObject doQueryUnLoad(String storageNo) {
//        return this.getDataBySqlID("TmsDao", "FrmBaseCrane.doQueryUnLoad", new Object[]{storageNo});
//    }

    /***
     * 新增
     * @return
     */
    public CoreReturnObject doAdd(String ydmBsEntity) {
        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);
        String sql  = "";
        PreparedStatement psta = null;
        ResultSet rs = null;
        int count = 0;
        try {
            ArrayList<String> sqllist = new ArrayList<String>();

            YdmBsMaxwgtEntity ydmBsWt = EntityTool.getEntityByJSONStr(ydmBsEntity, YdmBsMaxwgtEntity.class);
            sql = CoreXmlSqlParsersFactory.getSql("FrmBaseCrane.SelectCount");
            sql = EntityTool.fillSqlByEntity(sql,ydmBsWt);
            psta = connection.prepareStatement(sql);
            rs = psta.executeQuery();
            if(rs.next())
            {
                count = Integer.parseInt(rs.getString("ACOUNT"));
            }
            rs.close();
            psta.close();
            if(count > 0)
            {
                cro.setV_errMsg("已存在相同库区相同装卸点的最大吊装量!");
                return  cro;
            }


            String insertSql = CoreXmlSqlParsersFactory.getSql("FrmBaseCrane.doAdd");
            insertSql = EntityTool.fillSqlByEntity(insertSql, ydmBsWt);
            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 doUpdate(String ydmBsEntity)
    {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        try {
            ArrayList<String> sqllist = new ArrayList<String>();
            YdmBsMaxwgtEntity ydmBsWt = EntityTool.getEntityByJSONStr(ydmBsEntity, YdmBsMaxwgtEntity.class);
            String updateSql = CoreXmlSqlParsersFactory.getSql("FrmBaseCrane.doUpdate");
            updateSql = EntityTool.fillSqlByEntity(updateSql, ydmBsWt);
            sqllist.add(updateSql);

            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;
    }

    /***
     * 删除
     * @param ydmBsEntity
     * @return
     */
    public CoreReturnObject deleteData(String ydmBsEntity)
    {
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        try {
            ArrayList<String> sqllist = new ArrayList<String>();
            YdmBsMaxwgtEntity ydmBsWt = EntityTool.getEntityByJSONStr(ydmBsEntity, YdmBsMaxwgtEntity.class);
            String deleteSql = CoreXmlSqlParsersFactory.getSql("FrmBaseCrane.deleteData");
            deleteSql = EntityTool.fillSqlByEntity(deleteSql, ydmBsWt);
            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;
    }
}
