package com.steering.pss.glbusiness;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.db.DBCommon;

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;

public class SlmGlPay extends DBCommon {

    public CoreReturnObject query(ArrayList<Object> parm)  {
        String sql = CoreXmlSqlParsersFactory
                .getSql("SlmGlPay.query");
        if(!parm.get(0).equals("")){
            sql += " AND t.INVOICE_NO = '"+parm.get(0)+"'";
        }
        if(!parm.get(1).equals("")){
            sql +=" AND t.CREATE_TIME >= to_date('"+parm.get(1)+"','yyyy-mm-dd hh24:mi:ss')";
        }
        if(!parm.get(2).equals("")){
            sql +=" AND t.CREATE_TIME <= to_date('"+parm.get(2)+"','yyyy-mm-dd hh24:mi:ss')";
        }
        return this.getData(sql);
    }

    public String queryCount() throws SQLException {
        String str = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("SlmGlPay.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 CoreReturnObject insert(ArrayList<Object> parm){
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("SlmGlPay.insert");
        try {
            parm.set(0,strPayNo());
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    /**
     * 废除
     * @param parm 参数
     * @return CoreReturnObject 必须返回CoreReturnObject
     */
    public CoreReturnObject delete(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("SlmGlPay.delete");
        try {
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    public CoreReturnObject queryBase(String parm)  {
        String sql = CoreXmlSqlParsersFactory
                .getSql("SlmGlPay.queryBase");
        if(!parm.equals("")){
            sql += " AND T.SORTCODE = '"+parm+"'";
        }
        return this.getData(sql);
    }

    public String strPayNo() throws SQLException {
        SimpleDateFormat f = new SimpleDateFormat("yyMMdd");
        String yyMMdd = f.format(new Date());
        String strInvId = "BL" + yyMMdd;
        String str = String.valueOf(Integer.valueOf(queryCount())  + 1);
        while (str.length() < 5) {
            str = "0" + str;
        }
        return strInvId + str;
    }
}
