package com.steering.Mcms;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.Mcms.entity.CmmWeightMatEntity;
import com.steering.Mcms.entity.YdmZcLoadvehicleEntity;
import com.steering.comm.db.DBCommon;

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

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




    public CoreReturnObject doQueryLoadvehicleInfo(String carNo) {
        return this.getDataBySqlID("LoadVehicleServer.doQueryLoadvehicleInfo",new Object[]{carNo});
    }

    public CoreReturnObject doQueryLoadvehicleMatInfo(String carNo) {
        return this.getDataBySqlID("LoadVehicleServer.doQueryLoadvehicleMatInfo",new Object[]{carNo});
    }

    public CoreReturnObject doQueryHook(String carNo) {
        return this.getDataBySqlID("LoadVehicleServer.doQueryHook",new Object[]{carNo});
    }




    public static YdmZcLoadvehicleEntity getLoadList(Connection con,
                                              String outDoc) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("LoadVehicleServer.doQueryLoadList");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1,outDoc);

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

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

        YdmZcLoadvehicleEntity entity = resultEntities.get(0);
        return entity;
    }

    public static  void UpdateLoadInfo(Connection connection, String loadNo, String resultNo, String wtStr) throws Exception
    {

        YdmZcLoadvehicleEntity outInfo =getLoadList(connection,loadNo);
        if(outInfo==null ) return;

        //if(!StringUtil.isEmpty(outInfo.getPonderNo()))  throw new Exception("装车单已存在过记录！");

        String sqlUpdateOut = CoreXmlSqlParsersFactory
                .getSql("LoadVehicleServer.doUpdateLoadList");

        Double wt = Double.parseDouble(wtStr);

        PreparedStatement pst = connection.prepareStatement(sqlUpdateOut);
        pst.setObject(1,resultNo);
        pst.setObject(2,wt);
        pst.setObject(3,loadNo);
        pst.executeUpdate();
        pst.close();


        List<CmmWeightMatEntity> list =  PoundMatServer.getGroupByResultNo(connection,resultNo);

        for (CmmWeightMatEntity cmmWeightMatEntity : list) {
            Double Jwt = Double.parseDouble(cmmWeightMatEntity.getActWeight());
            Double Jnum = Double.parseDouble(cmmWeightMatEntity.getActCount());
            Double SingleWt =Math.round(Jwt * 1000/  Jnum )/1000D;


            sqlUpdateOut = CoreXmlSqlParsersFactory
                    .getSql("LoadVehicleServer.doUpdateLoadMat");

            pst = connection.prepareStatement(sqlUpdateOut);
            pst.setObject(1,resultNo);
            pst.setObject(2,SingleWt);
            pst.setObject(3,SingleWt);
            pst.setObject(4,loadNo);
            pst.setObject(5,cmmWeightMatEntity.getJudgeStoveNo());
            pst.executeUpdate();
            pst.close();

            sqlUpdateOut = CoreXmlSqlParsersFactory
                    .getSql("LoadVehicleServer.doUpdateLoadMat2");

            pst = connection.prepareStatement(sqlUpdateOut);
            pst.setObject(1, Jwt - SingleWt * Jnum);
            pst.setObject(2, Jwt - SingleWt * Jnum);
            pst.setObject(3,loadNo);
            pst.setObject(4,cmmWeightMatEntity.getJudgeStoveNo());
            pst.executeUpdate();
            pst.close();


            sqlUpdateOut = CoreXmlSqlParsersFactory
                    .getSql("LoadVehicleServer.doUpdateLoadHack");

            pst = connection.prepareStatement(sqlUpdateOut);
            pst.setObject(1,SingleWt);
            pst.setObject(2,loadNo);
            pst.setObject(3,cmmWeightMatEntity.getJudgeStoveNo());
            pst.executeUpdate();
            pst.close();

            sqlUpdateOut = CoreXmlSqlParsersFactory
                    .getSql("LoadVehicleServer.doUpdateLoadHack2");

            pst = connection.prepareStatement(sqlUpdateOut);
            pst.setObject(1,Jwt - SingleWt * Jnum);
            pst.setObject(2,loadNo);
            pst.setObject(3,cmmWeightMatEntity.getJudgeStoveNo());
            pst.executeUpdate();
            pst.close();

        }



    }

   public static void ClearLoadWt(Connection connection,String loadNo) throws Exception {
       YdmZcLoadvehicleEntity outInfo =getLoadList(connection,loadNo);
       if(outInfo==null ) return;

       if("1".equals(outInfo.getAffirmFlag())) throw new Exception("装车单已经确认出厂,请先撤销确认！");

       String sqlUpdateOut = CoreXmlSqlParsersFactory
               .getSql("LoadVehicleServer.doUpdateLoadList");



       PreparedStatement  pst = connection.prepareStatement(sqlUpdateOut);
       pst.setObject(1, "");
       pst.setObject(2,"");
       pst.setObject(3,loadNo);
       pst.executeUpdate();
       pst.close();

       sqlUpdateOut = CoreXmlSqlParsersFactory
               .getSql("LoadVehicleServer.doUpdateLoadMat3");

       pst = connection.prepareStatement(sqlUpdateOut);
       pst.setObject(1,"");
       pst.setObject(2,"");
       pst.setObject(3,"");
       pst.setObject(4,loadNo);
       pst.executeUpdate();
       pst.close();

       sqlUpdateOut = CoreXmlSqlParsersFactory
               .getSql("LoadVehicleServer.doUpdateLoadHack3");

       pst = connection.prepareStatement(sqlUpdateOut);
       pst.setObject(1,"");
       pst.setObject(2,loadNo);
       pst.executeUpdate();
       pst.close();
   }
}
