package com.steering.Mcms;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SC01.CoreConstantData;
import com.steering.Mcms.entity.CmmWeightRecordEntity;
import com.steering.Mcms.entity.CmmWeightResultEntity;
import com.steering.comm.db.DBCommon;
import org.hsqldb.lib.StringUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

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

    public CoreReturnObject DoAdd(String firstRecordNo, String recordJson) {

      return  DoAdd(firstRecordNo,"",recordJson);
    }

    /**
     *  新增
     * @param firstRecordNo 第一次流水号
     * @param recordJson 当前称重记录JSON
     * @param resultNo 过磅结果单号
     * @return
     */
    public CoreReturnObject DoAdd(String firstRecordNo,String hookWtStr, String recordJson) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();

        try {
            connection.setAutoCommit(false); //


            PreparedStatement pst = null;
            ResultSet rs = null;
            String msg = "";

            //保存称重记录
            CmmWeightRecordEntity cmmWeightRecordEntity = RecordServer.SaveRecord(recordJson,hookWtStr, connection);

            msg = cmmWeightRecordEntity.getCarNo() +  "  本次称重完成, 重量为" + cmmWeightRecordEntity.getRecordWeight() + "吨";


            //两次称重全部完成  写入结果表
            if(!StringUtil.isEmpty(firstRecordNo)){
                CmmWeightRecordEntity firstRecord = RecordServer.getRecord(connection,firstRecordNo);
                if(firstRecord==null ||  StringUtil.isEmpty(firstRecord.getRecordNo())) throw new Exception("找不到第一次称重记录！");

                if(firstRecord.getWtType().equals( cmmWeightRecordEntity.getWtType())){
                    throw new Exception("请选择正常的二次称重类型！");
                }


                CmmWeightResultEntity cmmWeightResultEntity = ResultServer.AddResult(connection,firstRecordNo,
                        cmmWeightRecordEntity.getRecordNo(),
                        cmmWeightRecordEntity.getRecoderLocation());

                     //不是空车过皮的  关闭
                if(!"101".equals(cmmWeightResultEntity.getRecordType1())){
                    RecordServer.UpdateValidFlag(connection,"2", cmmWeightResultEntity.getRecordNo1());

                    PoundMatServer.UpdateMatResultNo(connection,cmmWeightResultEntity.getResultNo(),cmmWeightResultEntity.getRecordNo1());
                }else {
                    RecordServer.UpdateValidFlag(connection,"2", cmmWeightResultEntity.getRecordNo1());
                    PoundMatServer.UpdateMatResultNo(connection,cmmWeightResultEntity.getResultNo(),cmmWeightResultEntity.getRecordNo2());

                }



                if("201".equals(cmmWeightRecordEntity.getRecordType())){

                    LoadVehicleServer.UpdateLoadInfo
                            (connection,cmmWeightResultEntity.getLoadvehicleNo(),
                                    cmmWeightResultEntity.getResultNo(),
                                    cmmWeightResultEntity.getNetWt()
                                    );
                }else if("208".equals(cmmWeightRecordEntity.getRecordType())){
                    MatOutServer.UpdateLoadInfo(
                            connection,cmmWeightResultEntity.getLoadvehicleNo(),
                            cmmWeightResultEntity.getResultNo(),
                            cmmWeightResultEntity.getNetWt()
                    );
                }



                msg +=  " 净重" + cmmWeightResultEntity.getNetWt()  + "吨|" +cmmWeightResultEntity.getResultNo()  ;
            }

            InputRecordServer.Save(connection, cmmWeightRecordEntity);

            core.setV_errMsg(msg);
            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 CoreReturnObject DoDeleteResult(String resultNo, String userName, String Type) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();

        try {
            connection.setAutoCommit(false); //

            PreparedStatement pst = null;


            CmmWeightResultEntity  cmmWeightResultEntity = ResultServer.getResult(connection,resultNo);


            PoundMatServer.UpdateMatResultNo(connection,"",cmmWeightResultEntity.getRecordNo1());
            PoundMatServer.UpdateMatResultNo(connection,"",cmmWeightResultEntity.getRecordNo2());


           ResultServer.Delete(connection,resultNo,userName);

            CmmWeightRecordEntity recordEntity = RecordServer.getRecord(connection,cmmWeightResultEntity.getRecordNo2());

                if("201".equals(recordEntity.getRecordType())){
                          LoadVehicleServer.ClearLoadWt(connection,recordEntity.getLoadvehicleNo());
                }else if("208".equals(recordEntity.getRecordType())){
                     MatOutServer.ClearLoadWt(connection,recordEntity.getLoadvehicleNo());
                }

                //重过逻辑
                if("2".equals(Type)){
                    RecordServer.UpdateValidFlag(connection,"1", cmmWeightResultEntity.getRecordNo1());
                }

            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 CoreReturnObject DoUpdate(String resultNo, String recordJson) {
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();

        try {
            connection.setAutoCommit(false); //




            //保存称重记录
            RecordServer.Update(recordJson, connection);
            ResultServer.Update(connection,resultNo);

            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;
    }





}


