package com.steering.Mcms;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.Mcms.entity.CmmWeightResultEntity;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import org.hsqldb.lib.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 朱少波 on 2020/11/20.
 */
public class ResultServer extends DBCommon {



    public CoreReturnObject doQuery(HashMap<String,Object> para) {

        String sql = CoreXmlSqlParsersFactory
                .getSql("ResultServer.doQuery");

        if(para.containsKey("time") && para.get("time")!=null && !StringUtil.isEmpty(para.get("time").toString()) ){
            sql+= " AND NVL(RECORD_DATE2,RECORD_DATE1) >=  TO_DATE('" + para.get("time") + "','YYYY-MM-DD HH24:MI:SS')";
        }



        if(para.containsKey("time1") && para.get("time1")!=null && !StringUtil.isEmpty(para.get("time1").toString()) ){
            sql+= " AND NVL(RECORD_DATE2,RECORD_DATE1) <=  TO_DATE('" + para.get("time1") + "','YYYY-MM-DD HH24:MI:SS')";
        }

        if(para.containsKey("poundNo") && para.get("poundNo")!=null && !StringUtil.isEmpty(para.get("poundNo").toString()) ){
            sql+= " AND RECODER_LOCATION2 = '" +  para.get("poundNo").toString() +"'";
        }

        if(para.containsKey("carNo") && para.get("carNo")!=null && !StringUtil.isEmpty(para.get("carNo").toString()) ){
            sql += " AND CAR_NO LIKE '%"  + para.get("carNo").toString() + "%'";
        }

        if(para.containsKey("matType") && para.get("matType")!=null && !StringUtil.isEmpty(para.get("matType").toString()) ){
            sql += " AND MAT_TYPE = '"  + para.get("matType").toString() + "'";
        }


        if(para.containsKey("validflag") && para.get("validflag")!=null && para.get("validflag")  instanceof java.util.ArrayList ){

            sql +=   CoreConverter.toSqlConditionStringList("VALIDFLAG", (ArrayList<String>) para.get("validflag"));

        }
        if(para.containsKey("orderName") && para.get("orderName")!=null && !StringUtil.isEmpty(para.get("orderName").toString()) ){
            sql += " AND ORDER_NAME LIKE '%"  + para.get("orderName").toString() + "%'";
        }
        if(para.containsKey("shippersName") && para.get("shippersName")!=null && !StringUtil.isEmpty(para.get("shippersName").toString()) ){
            sql += " AND SHIPPERS_NAME LIKE '%"  + para.get("shippersName").toString() + "%'";
        }
        if(para.containsKey("receiveName") && para.get("receiveName")!=null && !StringUtil.isEmpty(para.get("receiveName").toString()) ){
            sql += " AND RECEIVE_NAME LIKE '%"  + para.get("receiveName").toString() + "%'";
        }
        if(para.containsKey("memo") && para.get("memo")!=null && !StringUtil.isEmpty(para.get("memo").toString()) ){
            sql += " AND MEMO LIKE '%"  + para.get("memo").toString() + "%'";
        }

        sql += " Order by RECORD_DATE2 DESC";

        return this.getData(sql);
    }



    public  static CmmWeightResultEntity getResult(Connection con,
                                           String resultNo) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("ResultServer.doQuery");
        sql += " AND RESULT_NO ='" + resultNo + "'";
        PreparedStatement ps = con.prepareStatement(sql);

        ResultSet rs = ps.executeQuery();
        List<CmmWeightResultEntity> resultEntities = com.steering.comm.converter.EntityTool.getEntitysByRs(rs,
                CmmWeightResultEntity.class);
        rs.close();
        ps.close();


        if (resultEntities == null || resultEntities.size() <= 0) {
            return null;
        }
        return resultEntities.get(0);
    }

    public static String GetId(Connection con,String poundNo) throws SQLException {
        String resultNo="";
        String sql = CoreXmlSqlParsersFactory
                .getSql("ResultServer.doQueryResultId");
        PreparedStatement pst = con.prepareStatement(sql);
        pst.setObject(1, poundNo);
        pst.setObject(2, poundNo);
        ResultSet rs = pst.executeQuery();
        if (rs.next()) {
            resultNo = rs.getString("RESULT_NO");

        }
        rs.close();
        pst.close();

        return resultNo;
    }

    public static CmmWeightResultEntity AddResult(Connection con,String recordNo1,String recordNo2,String poundNo) throws SQLException {
       String id = GetId(con,poundNo);

        String sql = CoreXmlSqlParsersFactory
                .getSql("ResultServer.doSaveID");
        PreparedStatement pst = con.prepareStatement(sql);
        pst.setObject(1,id);
        pst.setObject(2,recordNo1);
        pst.setObject(3,recordNo2);
        pst.executeUpdate();
        pst.close();

        return getResult(con,id );

    }


    public static void Delete(Connection connection, String resultNo,String userName) throws SQLException {
        String sqlAddTemplate = CoreXmlSqlParsersFactory
                .getSql("ResultServer.DoDeleteResult");
        PreparedStatement  pst = connection.prepareStatement(sqlAddTemplate);
        pst.setObject(1, userName);
        pst.setObject(2, resultNo);
        pst.executeUpdate();
        pst.close();

    }

    public static void Update(Connection connection,String resultNo) throws  Exception{
        String sqlAddTemplate = CoreXmlSqlParsersFactory
                .getSql("ResultServer.doUpdateResult");
        PreparedStatement  pst = connection.prepareStatement(sqlAddTemplate);
        pst.setObject(1, resultNo);
        pst.executeUpdate();
        pst.close();
    }


}
