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 SlmGlAccrued extends DBCommon {

    public CoreReturnObject doQuery(ArrayList<Object> parm)  {
        String sql = CoreXmlSqlParsersFactory
                .getSql("SlmGlAccrued.doQuery");
        if(!parm.get(0).equals("")){
            sql += " AND t.BALANCE_NO LIKE '%"+parm.get(0)+"%'";
        }
        if(!parm.get(1).equals("")){
            sql += " AND t.CG_ORDER_NO LIKE '%"+parm.get(1)+"%'";
        }
        if(!parm.get(2).equals("")){
            sql +=" AND t.CREATE_TIME >= to_date('"+parm.get(2)+"','yyyy-mm-dd hh24:mi:ss')";
        }
        if(!parm.get(3).equals("")){
            sql +=" AND t.CREATE_TIME <= to_date('"+parm.get(3)+"','yyyy-mm-dd hh24:mi:ss')";
        }
        if(!parm.get(4).equals("")){
            sql +=" AND t.BALANCE_TYPE = '"+parm.get(4)+"'";
        }
        return this.getData(sql);
    }

    public CoreReturnObject doQueryOrderHead(ArrayList<Object> parm)  {
        String sql = CoreXmlSqlParsersFactory
                .getSql("SlmGlAccrued.doQueryOrderHead");
            if(parm.size()==1&&!parm.get(0).equals("")){
                sql += " AND t.ORD_NO = '"+parm.get(0)+"'";
            }
            if(parm.size()==2&&!parm.get(1).equals("")){
                sql +=" AND t.CREATE_TIME >= to_date('"+parm.get(1)+"','yyyy-mm-dd hh24:mi:ss')";
            }
            if(parm.size()==3&&!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 CoreReturnObject doQueryOrderLine(ArrayList<Object> parm)  {
        return this.getDataBySqlID("SlmGlAccrued.doQueryOrderLine",parm.toArray());
    }

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

    public CoreReturnObject doUpdate(ArrayList<Object> parm) {
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("SlmGlAccrued.doUpdate");
        try {
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    public CoreReturnObject addBalance(ArrayList<Object> parm){
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("SlmGlAccrued.addBalance");
        try {
            parm.set(0,strbalanceId());
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    public CoreReturnObject doQueryFreight(ArrayList<Object> parm)  {
        String sql = CoreXmlSqlParsersFactory
                .getSql("SlmGlAccrued.doQueryFreight");
        if(!parm.get(0).equals("")){
            sql += " AND t.CG_ORDER_NO LIKE '%"+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 CoreReturnObject doAddFreight(ArrayList<Object> parm){
        CoreReturnObject cro = new CoreReturnObject();
        String strSql = CoreXmlSqlParsersFactory
                .getSql("SlmGlAccrued.doAddFreight");
        try {
            parm.set(0,strbalanceId());
            cro = this.getDao("TgDao").ExcuteNonQuery(strSql, parm.toArray());
        }
        catch (SQLException e) {
            cro = cro.SetErrInfo(e.getMessage());
        }
        return cro;
    }

    // 生成结算单号
    public String strbalanceId() throws SQLException {
        SimpleDateFormat f = new SimpleDateFormat("yyMMdd");
        String yyMMdd = f.format(new Date());
        String strInvId = "JS" + yyMMdd;
        String str = String.valueOf(Integer.valueOf(queryCount())  + 1);
        while (str.length() < 5) {
            str = "0" + str;
        }
        return strInvId + str;
    }

    public String queryCount() throws SQLException {
        String str = "";
        String sql = CoreXmlSqlParsersFactory
                .getSql("SlmGlAccrued.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;
    }
}
