package com.steering.Mcms;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.Mcms.entity.CmmWeightMatEntity;
import com.steering.Mcms.entity.CmmWeightRecordEntity;
import com.steering.comm.converter.CoreConverter;
import com.steering.comm.db.DBCommon;
import com.steering.common.EntityTool;
import net.sf.json.JSONObject;
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;
import java.util.Map;

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


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

        String sql = CoreXmlSqlParsersFactory
                .getSql("RecordServer.doQueryRecord");


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

        if(para.containsKey("poundNo") && para.get("poundNo")!=null && !StringUtil.isEmpty(para.get("poundNo").toString()) ){
            sql += " AND RECODER_LOCATION = '"  + para.get("poundNo").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"));
        }

        sql += " Order by RECORD_DATE ";

        return this.getData(sql);
    }



    public static CmmWeightRecordEntity getRecord(Connection con,
                                           String recordNo) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("RecordServer.doQueryRecord");
        sql += " AND RECORD_NO ='" + recordNo + "'";
        PreparedStatement ps = con.prepareStatement(sql);

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

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

    public static CmmWeightRecordEntity SaveRecord(String recordJson,String hookWtStr, Connection connection) throws SQLException {
        PreparedStatement pst;
        ResultSet rs;JSONObject jsonObj = JSONObject.fromObject(recordJson);

        Map<String, Class> classMap = new HashMap<String, Class>();
        classMap.put("matInfos", CmmWeightMatEntity.class);
        CmmWeightRecordEntity cmmWeightRecordEntity = (CmmWeightRecordEntity) JSONObject.toBean(jsonObj,
                CmmWeightRecordEntity.class, classMap);

        String sql = CoreXmlSqlParsersFactory
                .getSql("RecordServer.doQueryRecordId");
        pst = connection.prepareStatement(sql);
        rs = pst.executeQuery();
        if (rs.next()) {
            cmmWeightRecordEntity.setRecordNo(rs.getString("RECORD_NO"));
        }
        rs.close();
        pst.close();

        String sqlAdd = CoreXmlSqlParsersFactory
                .getSql("RecordServer.doSaveRecordId");
        sqlAdd = EntityTool.fillSqlByEntity(sqlAdd, cmmWeightRecordEntity);
        pst = connection.prepareStatement(sqlAdd);
        pst.executeUpdate();
        pst.close();


        //保存过磅物资信息
        PoundMatServer.addMat(connection,cmmWeightRecordEntity.getMatInfos(),cmmWeightRecordEntity.getRecordNo(),
                cmmWeightRecordEntity.getRealWt(), hookWtStr);

        return cmmWeightRecordEntity;
    }


    public static void UpdateValidFlag(Connection connection,String validflag, String recordNo)throws Exception {

        String sqlUpdate = CoreXmlSqlParsersFactory
                .getSql("RecordServer.doUpdateValidFlag");
        PreparedStatement pst = connection.prepareStatement(sqlUpdate);
        pst.setObject(1,validflag);
        pst.setObject(2, recordNo);
        pst.executeUpdate();
        pst.close();
    }

    public CoreReturnObject DoUpdateRecord(String recordNo) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();

        try {
            connection.setAutoCommit(false); //


            UpdateValidFlag(connection,"0", recordNo);


            core.setV_errMsg("操作成功！");
            core.setResult(1);
            connection.commit();

        } catch (Exception e) {
            core.setV_errMsg(e.getMessage());
            if (connection != null) {
                try {
                    connection.rollback(); // 回滚事务
                } catch (Exception e2) {
                }
            }
        } finally {
            try {
                if (connection != null && !connection.isClosed()) {
                    connection.close();
                }
            } catch (Exception e2) {
            }
        }
        return core;
    }



    public static void Update(String recordJson, Connection connection ) throws SQLException {

        ResultSet rs;JSONObject jsonObj = JSONObject.fromObject(recordJson);

        Map<String, Class> classMap = new HashMap<String, Class>();
        classMap.put("matInfos", CmmWeightMatEntity.class);
        CmmWeightRecordEntity cmmWeightRecordEntity = (CmmWeightRecordEntity) JSONObject.toBean(jsonObj,
                CmmWeightRecordEntity.class, classMap);

        String sqlUpdate = CoreXmlSqlParsersFactory
                .getSql("RecordServer.Update");
        sqlUpdate = EntityTool.fillSqlByEntity(sqlUpdate, cmmWeightRecordEntity);
        PreparedStatement pst = connection.prepareStatement(sqlUpdate);
        pst.executeUpdate();
        pst.close();

        PoundMatServer.deleteMat(connection,cmmWeightRecordEntity.getRecordNo());

        //保存过磅物资信息
        PoundMatServer.addMat(connection,cmmWeightRecordEntity.getMatInfos(),cmmWeightRecordEntity.getRecordNo(),"","");
    }


}
