package com.steering.Mcms;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.entity.YdmBcOutlistEntity;

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 MatOutServer extends DBCommon {

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



    public static List<YdmBcOutlistEntity> getOutList(Connection con,
                                               String outDoc) throws SQLException {
        String sql = CoreXmlSqlParsersFactory
                .getSql("MatOutServer.doQueryOutList");
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setObject(1,outDoc);

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

        return resultEntities;
    }


    public static void UpdateLoadInfo(Connection connection,String outDoc, String resultNo, String wtStr)throws Exception{
        List<YdmBcOutlistEntity> outInfo =getOutList(connection,outDoc);
        if(outInfo==null || outInfo.size()<=0) throw new Exception("找不到出库记录,请确认出库是否撤销！");


        String sqlUpdateOut = CoreXmlSqlParsersFactory
                .getSql("MatOutServer.doUpdateOutList");

        PreparedStatement  pst = connection.prepareStatement(sqlUpdateOut);

        Double wt = Double.parseDouble(wtStr);

        Double SingleWt =Math.round(wt * 1000/  outInfo.size())/1000D;

        for (int i = 0; i < outInfo.size(); i++) {

            pst.setObject(1,resultNo);
            pst.setObject(2,i== outInfo.size()-1? wt- (outInfo.size()-1) *SingleWt  : SingleWt);
            pst.setObject(3,outInfo.get(i).getOutstockSqe());
            pst.addBatch();
        }
        pst.executeBatch();
        pst.close();
    }

    public static  void ClearLoadWt(Connection connection,String outDoc) throws  Exception{

        List<YdmBcOutlistEntity> outInfo =getOutList(connection,outDoc);
        if(outInfo==null || outInfo.size()<=0) throw new Exception("找不到出库记录,请确认出库是否撤销！");


        String sqlUpdateOut = CoreXmlSqlParsersFactory
                .getSql("MatOutServer.doUpdateOutList");

        PreparedStatement pst = connection.prepareStatement(sqlUpdateOut);


        for (int i = 0; i < outInfo.size(); i++) {

            if("1".equals(outInfo.get(i).getTarRecFlag())){
                throw new Exception("废品已经接收,不允许删除过磅记录！");
            }

            pst.setObject(1,"");
            pst.setObject(2, "");
            pst.setObject(3,outInfo.get(i).getOutstockSqe());
            pst.addBatch();
        }
        pst.executeBatch();
        pst.close();
    }
}
