package com.steering.Mcms;

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

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

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



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

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


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


        sql += " Order by RECORD_NO, RECORD_SEQ";

        return this.getData(sql);
    }

    public static void  addMat(Connection connection, List<CmmWeightMatEntity> list,String recordNo,String wt,String hookWt) throws SQLException {

        BigDecimal HookWt = BigDecimal.ZERO;
        BigDecimal NetWt = BigDecimal.ZERO;
        BigDecimal DWt = BigDecimal.ZERO;
        BigDecimal uDWt = BigDecimal.ZERO;

        if(!StringUtil.isEmpty(hookWt)){
             HookWt  = new BigDecimal(hookWt);
             NetWt = new BigDecimal(wt);
             DWt = NetWt.subtract(HookWt);

        }


        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                //有误差
              if(DWt.compareTo(BigDecimal.ZERO)!=0){

                  BigDecimal AddWt = DWt.multiply(new BigDecimal( list.get(i).getActWeight()) ).divide(HookWt,3, BigDecimal.ROUND_HALF_EVEN);
                  if(i==list.size()-1) AddWt = DWt.subtract(uDWt);
                  uDWt = uDWt.add(AddWt);
                  list.get(i).setActWeight(new BigDecimal( list.get(i).getActWeight()).add(AddWt).toString());
              }


                list.get(i).setRecordNo(recordNo);
                String sqlAddMat = CoreXmlSqlParsersFactory
                        .getSql("PoundMatServer.doSaveMat");
                sqlAddMat = com.steering.common.EntityTool.fillSqlByEntity(sqlAddMat,  list.get(i));
                PreparedStatement pst = connection.prepareStatement(sqlAddMat);
                pst.executeUpdate();
                pst.close();

                InputRecordServer.Save(connection, list.get(i));
            }
        }
    }


    public static void  deleteMat(Connection connection,String recordNo) throws SQLException {

                String delete = CoreXmlSqlParsersFactory
                        .getSql("PoundMatServer.deleteMat");
                PreparedStatement pst = connection.prepareStatement(delete);
                pst.setObject(1,recordNo);
                pst.executeUpdate();
                pst.close();
    }

    public static void UpdateMatResultNo(Connection connection,String resultNo, String recordNo)throws Exception {

        String sqlUpdate = CoreXmlSqlParsersFactory
                .getSql("PoundMatServer.doUpdateMatResultNo");
        PreparedStatement pst = connection.prepareStatement(sqlUpdate);
        pst.setObject(1,resultNo);
        pst.setObject(2, recordNo);
        pst.executeUpdate();
        pst.close();
    }


    public static List<CmmWeightMatEntity>  getGroupByResultNo(Connection connection,String resultNo)throws Exception {

        String sql = CoreXmlSqlParsersFactory
                .getSql("PoundMatServer.getGroupByResultNo");

        PreparedStatement ps =  connection.prepareStatement(sql);
        ps.setObject(1,resultNo);
        ResultSet rs = ps.executeQuery();
        List<CmmWeightMatEntity> resultEntities = EntityTool.getEntitysByRs(rs,
                CmmWeightMatEntity.class);
        rs.close();
        ps.close();

        return resultEntities;
    }

}
