package com.steering.mes.mcp.Mch;

import CoreFS.SA06.CoreReturnObject;
import CoreFS.SA08.CoreXmlSqlParsersFactory;
import CoreFS.SC01.CoreConstantData;
import com.steering.comm.converter.EntityTool;
import com.steering.comm.db.DBCommon;
import com.steering.mes.mcp.common.PlanService;
import com.steering.mes.mcp.entity.MchEntity.PlnZyJgxMEntity;
import com.steering.mes.mcp.entity.MchGroupnoEntity;

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

/**
 * Created by 朱少波 on 2018/12/4.
 */
public class FrmGroupSetting extends DBCommon {

    public CoreReturnObject doQuery(String OrderNo) {
        return this.getDataBySqlID("FrmGroupSetting.doQuery",
                new Object[] { OrderNo});
    }

    public CoreReturnObject DoSave(String OrderNo,
                                  ArrayList<String> Datas){
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;

        try {
            connection.setAutoCommit(false); //

            String sqlDelete = CoreXmlSqlParsersFactory
                    .getSql("FrmGroupSetting.doDelete");// 修改加工计划表
            PreparedStatement psDelete = connection
                    .prepareStatement(sqlDelete);

            psDelete.setObject(1,OrderNo);
            psDelete.addBatch();
            psDelete.executeBatch();


            for (String data :Datas
                 ) {
                MchGroupnoEntity entity = EntityTool.getEntityByJSONStr(data,MchGroupnoEntity.class);
                entity.setOrderNo(OrderNo);

               String  sql =   CoreXmlSqlParsersFactory
                        .getSql("FrmGroupSetting.doQueryOffLine");
                psta = connection.prepareStatement(sql);
                psta.setObject(1, OrderNo );
                psta.setObject(2,  entity.getGroupNo());
                psta.setObject(3, OrderNo );
                psta.setObject(4,  entity.getGroupNo());
                ResultSet rs1 = psta.executeQuery();
                if (rs1.next()) {
                    entity.setOfflineNum(rs1.getString("OFFLINE_NUM"));
                    entity.setOfflineMax(rs1.getString("OFFLINE_MAX"));
                    entity.setOfflineMin(rs1.getString("OFFLINE_MIN"));
                }else{
                    entity.setOfflineNum("");
                    entity.setOfflineMax("");
                    entity.setOfflineMin("");
                }
                rs1.close();
                psta.close();

                if(entity.getOfflineNum().equals("")){
                    if(!entity.getGroupMax().equals("")){
                      if(entity.getGroupMaxSign().equals("<=") &&  Double.parseDouble(entity.getOfflineMax()) - Double.parseDouble(entity.getGroupMax()) >= 0.0001)
                      {
                            core.setV_errMsg("组号" + entity.getGroupNo() +  "已入库的管超出该设置的范围");
                            return core;
                      }

                        if(entity.getGroupMaxSign().equals("<") &&  Double.parseDouble(entity.getOfflineMax()) - Double.parseDouble(entity.getGroupMax()) > 0.0001)
                        {
                            core.setV_errMsg("组号" + entity.getGroupNo() +  "已入库的管超出该设置的范围");
                            return core;
                        }

                        if(entity.getGroupMaxSign().equals("<=") &&  Double.parseDouble(entity.getOfflineMin()) - Double.parseDouble(entity.getGroupMax()) >= 0.0001)
                        {
                            core.setV_errMsg("组号" + entity.getGroupNo() +  "已入库的管超出该设置的范围");
                            return core;
                        }

                        if(entity.getGroupMaxSign().equals("<") &&  Double.parseDouble(entity.getOfflineMin()) - Double.parseDouble(entity.getGroupMax()) > 0.0001)
                        {
                            core.setV_errMsg("组号" + entity.getGroupNo() +  "已入库的管超出该设置的范围");
                            return core;
                        }

                        if(entity.getGroupMinSign().equals(">=") &&  Double.parseDouble(entity.getOfflineMax()) - Double.parseDouble(entity.getGroupMax()) >= 0.0001)
                        {
                            core.setV_errMsg("组号" + entity.getGroupNo() +  "已入库的管超出该设置的范围");
                            return core;
                        }

                        if(entity.getGroupMinSign().equals(">") &&  Double.parseDouble(entity.getOfflineMax()) - Double.parseDouble(entity.getGroupMax()) > 0.0001)
                        {
                            core.setV_errMsg("组号" + entity.getGroupNo() +  "已入库的管超出该设置的范围");
                            return core;
                        }

                        if(entity.getGroupMinSign().equals(">=") &&  Double.parseDouble(entity.getOfflineMin()) - Double.parseDouble(entity.getGroupMax()) >= 0.0001)
                        {
                            core.setV_errMsg("组号" + entity.getGroupNo() +  "已入库的管超出该设置的范围");
                            return core;
                        }

                        if(entity.getGroupMinSign().equals(">") &&  Double.parseDouble(entity.getOfflineMin()) - Double.parseDouble(entity.getGroupMax()) > 0.0001)
                        {
                            core.setV_errMsg("组号" + entity.getGroupNo() +  "已入库的管超出该设置的范围");
                            return core;
                        }

                    }
                }

                String sqlAdd = CoreXmlSqlParsersFactory
                        .getSql("FrmGroupSetting.doAdd");// 新增上料实绩

                sqlAdd = com.steering.common.EntityTool.fillSqlByEntity(sqlAdd, entity);
                PreparedStatement patAdd = connection.prepareStatement(sqlAdd);
                patAdd.addBatch();
                patAdd.executeBatch();

            }


            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 DoChange(String OrderNo,String HeatPlanID,
                                   String Type){
        CoreReturnObject core = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr)
                .getConnection();
        PreparedStatement psta = null;

        try {
            connection.setAutoCommit(false); //

            PlnZyJgxMEntity pln = PlanService.getSendHttPlanCInfo3(connection,
                    HeatPlanID);

            String sql = CoreXmlSqlParsersFactory
                    .getSql("FrmGroupSetting.DeleteGroupNo");// 先删除没用的不在合同内的组号规则

            psta  = connection.prepareStatement(sql);
            psta.setObject(1,OrderNo);
            psta.addBatch();
            psta.executeBatch();

            String flag = "1";

            String NewLen ="";
            if(Type.equals("SystemLen")){
                NewLen =  pln.getShortest();
            }else if (Type.equals("R1Len")){
                NewLen = "4.88";

            }else if (Type.equals("R2Len")){
                NewLen = "7.62";
            }else if (Type.equals("R3Len")){
                NewLen =  "10.36";
            }else if (Type.equals("lr2Len")){
                NewLen =  "8.53";
            }else {
                return core;
            }
            Double OrderMaxLen = Double.parseDouble(pln.getLengthmax());
            Double OrderMinLen = Double.parseDouble(pln.getLengthmin());
            if(NewLen.equals("")) {
                 sql = CoreXmlSqlParsersFactory
                        .getSql("FrmGroupSetting.DeleteLessGroup");
                psta = connection.prepareStatement(sql);
                psta.setObject(1, OrderNo);
                psta.addBatch();
                psta.executeBatch();

                flag = "0";
                NewLen = (OrderMinLen-1) +"";
            }






                if (Double.parseDouble(NewLen) - OrderMinLen > -0.00001) {
                    core.setV_errMsg("短尺大于合同的的最小范围");
                    return core;
                }


                sql = CoreXmlSqlParsersFactory
                        .getSql("FrmGroupSetting.SelectLessGroup");
                psta = connection.prepareStatement(sql);
                psta.setObject(1, OrderNo);
                psta.setObject(2, OrderMaxLen);
                ResultSet rs = psta.executeQuery();
                List<MchGroupnoEntity> data = EntityTool.getEntitysByRs(rs,
                        MchGroupnoEntity.class);
                rs.close();
                psta.close();


                if (data == null || data.size() <= 0  ) {
                    sql = CoreXmlSqlParsersFactory
                            .getSql("FrmGroupSetting.InsertGroup");// 修改队列表材料，工序状态
                    psta = connection.prepareStatement(sql);
                    psta.setObject(1, OrderNo);
                    psta.setObject(2, pln.getLengthmin());
                    psta.setObject(3, NewLen);
                    psta.setObject(4, "<");
                    psta.setObject(5, ">=");
                    psta.setObject(6, OrderNo);
                    psta.addBatch();
                    psta.executeBatch();
                } else if( data.size()==1 && (data.get(0).getOfflineMax().equals("")|| Double.parseDouble(NewLen) <= Double.parseDouble(data.get(0).getOfflineMax()) ) ){
                    sql = CoreXmlSqlParsersFactory
                            .getSql("FrmGroupSetting.UpdateLessGroup");// 修改队列表材料，工序状态
                    psta = connection.prepareStatement(sql);
                    psta.setObject(1, NewLen);
                    psta.setObject(2, flag);
                    psta.setObject(3, OrderNo);
                    psta.setObject(4, data.get(0).getGroupNo());

                    psta.addBatch();
                    psta.executeBatch();

                }else {
                    core.setV_errMsg("已生成短尺组号,请先撤销该合同的短尺的入库");
                    return core;
                }



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