package com.steering.mes.mcp.Vrp;

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.entity.FrmBaoJianResultDetailEntity;
import com.steering.mes.mcp.entity.FrmBaoJianResultMainEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * Created by 刘昌民 on 2021/6/16.
 */
@Component
public class FrmBaoJianResult extends DBCommon {
    /**
     * 查询报检主表
     * @param parmlist
     * @return
     */
    public CoreReturnObject QueryMain(ArrayList<String> parmlist){
        String sql = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.QueryMain");
        sql += " AND T.CREATE_TIME between to_date('"+parmlist.get(0)+"','YYYY-MM-DD HH24:mi:ss') AND to_date('"+parmlist.get(1)+"','YYYY-MM-DD HH24:mi:ss')";
        if(!parmlist.get(2).equals("")){
            sql += " AND T.PLINE_CODE IN ('"+parmlist.get(2)+"')";
        }
        if(!parmlist.get(3).equals("")&&!parmlist.get(3).equals("4")){
            sql += " AND T.INSPECTION_FLAG = '"+parmlist.get(3)+"'";
        }
        if(!parmlist.get(4).equals("")){
            if(parmlist.get(4).equals("0")){
                sql += " AND T.INSPECTION_RESULT = '合格'";
            }else if(parmlist.get(4).equals("1")){
                sql += " AND T.INSPECTION_RESULT = '不合格'";
            }else{
                sql += " AND T.INSPECTION_RESULT IS NULL";
            }
        }
        if(!parmlist.get(5).equals("")){
            sql += " AND T.JUDGE_STOVE_NO = '"+parmlist.get(5)+"'";
        }
        if(parmlist.get(7).equals("")){
            sql += " AND T.VALIDFLAG = '1'";
        }
        sql += " AND T.PROCESS_CODE = '"+parmlist.get(6)+"'";
        if(!parmlist.get(8).equals("")){
            sql += " AND T.DISPOSAL_FLAG = '"+parmlist.get(8)+"'";
        }
        return this.getData(sql);
    }



    public CoreReturnObject QueryMain1(ArrayList<String> parmlist){
        String sql = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.QueryMain");
        sql += " AND INSPECTION_NO = '"+parmlist.get(0)+"'";
        sql += " AND INSPECTION_NUM = '"+parmlist.get(1)+"'";
        return this.getData(sql);
    }


    /**
     * 查询报检子表
     * @param parmlist
     * @return
     */
    public CoreReturnObject QueryDetail(ArrayList<String> parmlist){
        String sql = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.QueryDetail");
        sql += " AND INSPECTION_NO = '"+parmlist.get(0)+"'";
        sql += " AND INSPECTION_NUM = '"+parmlist.get(1)+"'";
        CoreReturnObject cro = this.getData(sql);
        return this.getData(sql);
    }

    /**
     * 报检子表字段信息修改
     * @param parmlist
     * @return
     */
    public CoreReturnObject SaveDetail(ArrayList<String> parmlist){
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        try {
            connection.setAutoCommit(false);//取消自动提交事务

            String sql = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.savedetail");
            sql +=  " AND T.GUID = '"+parmlist.get(4)+"'";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setObject(1,parmlist.get(0));
            ps.setObject(2,parmlist.get(1));
            ps.setObject(3,parmlist.get(2));
            ps.setObject(4,parmlist.get(3));
            ps.addBatch();
            ps.executeBatch();

            connection.commit();
            cro.setV_errMsg("修改成功!");

        }catch (Exception e){
            System.out.println(e.getMessage());
            if(connection != null){
                try{
                    cro.setV_errMsg(e.getMessage());
                    connection.rollback();
                }catch (Exception e2){
                }
            }
        }finally {
            try {
                if(connection !=null&&!connection.isClosed()){
                    connection.close();
                }
            }catch (Exception e2){

            }
        }
        return cro;
    }

    /**
     * 更新主表
     * @param parmlist
     * @return
     */
    public CoreReturnObject UpdateMain(String parmlist){
        CoreReturnObject cro1 = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        try{
            connection.setAutoCommit(false);//取消自动提交

            FrmBaoJianResultMainEntity entity = EntityTool.getEntityByJSONStr(parmlist,FrmBaoJianResultMainEntity.class);

            String sql = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.UpdateMain");
            sql += " AND T.INSPECTION_NO = '"+entity.getInspectionNo()+"'";
            sql += " AND T.INSPECTION_NUM = '"+entity.getInspectionNum()+"'";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setObject(1,entity.getInspectionResult());
            ps.setObject(2,entity.getUnqualifiedReasons());
            ps.setObject(3,entity.getDisposalFlag());
            ps.setObject(4,entity.getInspectionTime());
            ps.setObject(5,entity.getInspectionName());
            String result = entity.getInspectionResult();
            if(entity.getInspectionResult().equals("合格")){
                ps.setObject(6,0);
            }else{
                ps.setObject(6,1);
            }
            ps.addBatch();
            ps.executeBatch();
            connection.commit();

        }catch (Exception e){
            System.out.println(e.getMessage());
            if(connection != null){
                try{
                    cro1.setV_errMsg(e.getMessage());
                    connection.rollback();
                }catch (Exception e2){
                }
            }
        }finally {
            try {
                if(connection !=null&&!connection.isClosed()){
                    connection.close();
                }
            }catch (Exception e2){

            }
        }
        return cro1;
    }

    /**
     * 对报检结果为不合格的数据进行处置
     * @param parmlist
     * @return
     */
    public CoreReturnObject Chuzhi(String parmlist){
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        try {
            connection.setAutoCommit(false);
            FrmBaoJianResultMainEntity entity = EntityTool.getEntityByJSONStr(parmlist,FrmBaoJianResultMainEntity.class);

            String sql = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.Chuzhi");
            sql += " AND T.INSPECTION_NO = '"+entity.getInspectionNo()+"'";
            sql += " AND T.INSPECTION_NUM = '"+entity.getInspectionNum()+"'";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setObject(1,entity.getDisposalMethod());
            ps.setObject(2,entity.getDisposalFlag());
            ps.setObject(3,entity.getDisposalTime());
            ps.setObject(4,entity.getDisposalName());
            ps.addBatch();
            ps.executeBatch();
            connection.commit();
            cro.setV_errMsg("处置成功!");

        } catch (Exception e) {
            System.out.println(e.getMessage());
            if(connection != null){
                try{
                    cro.setV_errMsg(e.getMessage());
                    connection.rollback();
                }catch (Exception e2){

                }
            }
        } finally {
            try {
                if(connection != null&&!connection.isClosed()){
                    connection.close();
                }
            }catch (Exception e){

            }
        }
        return cro;
    }

    /**
     * 撤销处置
     * @param parmlist
     * @return
     */
    public CoreReturnObject RevokeChuzhi(String parmlist){
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        try {
            connection.setAutoCommit(false);
            FrmBaoJianResultMainEntity entity = EntityTool.getEntityByJSONStr(parmlist,FrmBaoJianResultMainEntity.class);

            String sql = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.RevokeChuzhi");
            sql += " AND INSPECTION_NO = '"+entity.getInspectionNo()+"'";
            sql += " AND INSPECTION_NUM = '"+entity.getInspectionNum()+"'";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setObject(1,entity.getDisposalMethod());
            ps.setObject(2,entity.getDisposalFlag());
            ps.setObject(3,entity.getDisposalTime());
            ps.setObject(4,entity.getDisposalName());
            ps.addBatch();
            ps.executeBatch();
            connection.commit();
            cro.setV_errMsg("撤销成功!");

        } catch (Exception e) {
            System.out.println(e.getMessage());
            if(connection != null){
                try {
                    cro.setV_errMsg(e.getMessage());
                    connection.rollback();
                }catch (Exception e2){

                }
            }
        } finally {
            try {
                if(connection != null&&!connection.isClosed()){
                    connection.close();
                }
            }catch (Exception e){

            }
        }
        return cro;
    }

    /**
     * 废除
     * @param parmlist
     * @return
     */
    public CoreReturnObject DelMain(ArrayList<String> parmlist){
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        try {
            connection.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.DelMain");
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setObject(1, parmlist.get(0));
            ps.setObject(2, parmlist.get(1));
            ps.addBatch();
            ps.executeBatch();
            connection.commit();
            cro.setV_errMsg("退回成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            if(connection != null){
                try {
                    cro.setV_errMsg(e.getMessage());
                    connection.rollback();
                }catch (Exception e2){

                }
            }
        } finally {
            try {
                if(connection != null && !connection.isClosed()){
                    connection.close();
                }
            }catch (Exception e){

            }
        }
        return cro;
    }

    /**
     * 撤销废除
     * @param parmlist
     * @return
     */
    public CoreReturnObject RevokeDel(ArrayList<String> parmlist){
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        try {
            connection.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.RevokeDel");
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setObject(1, parmlist.get(0));
            ps.setObject(2, parmlist.get(1));
            ps.addBatch();
            ps.executeBatch();
            connection.commit();
            cro.setV_errMsg("撤销废除成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            if(connection != null){
                try {
                    cro.setV_errMsg(e.getMessage());
                    connection.rollback();
                }catch (Exception e2){

                }
            }
        } finally {
            try {
                if(connection != null && !connection.isClosed()){
                    connection.close();
                }
            }catch (Exception e){

            }
        }
        return cro;
    }

    /**
     * 查询符合抽检的数据
     * @return
     */
    public ArrayList QuerybyDaijian(){
        ArrayList<FrmBaoJianResultMainEntity> list = new ArrayList();
        try {
            Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            String sql = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.QueryMain");
            sql += " AND VALIDFLAG = '1'";
            sql += " AND PROCESS_CODE = 'D'";
            sql += " AND INSPECTION_FLAG = '0'";
            Statement st = connection.createStatement();
            ResultSet rs = st.executeQuery(sql);
            while(rs.next()){
                FrmBaoJianResultMainEntity entity = new FrmBaoJianResultMainEntity();
                entity.setIdcode(rs.getString("IDCODE"));
                entity.setStdName(rs.getString("STD_NAME"));
                entity.setSteelname(rs.getString("STEELNAME"));
                entity.setPlineCode(rs.getString("PLINE_CODE"));
                list.add(entity);
            }
            rs.close();
        }catch (Exception e){
            System.out.print("MYSQL ERROR:" + e.getMessage());
        }
        return list;
    }


    /**
     * 查询产线代码
     * @return
     */
    public ArrayList QueryPlinecode(){
        ArrayList<FrmBaoJianResultMainEntity> codelist = new ArrayList();
        try {
            Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
            String sql = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.QueryPlineCode");
//            sql += " AND PROCESS_CODE = 'D'";
            Statement st = connection.createStatement();
            ResultSet rs = st.executeQuery(sql);
            while(rs.next()){
                FrmBaoJianResultMainEntity entity = new FrmBaoJianResultMainEntity();
                entity.setPlineCode(rs.getString("PLINE_CODE"));
                codelist.add(entity);
            }
        } catch (Exception e) {
            System.out.print("MYSQL ERROR:" + e.getMessage());
        }
        return codelist;
    }

    /**
     * 抽检（停用）
     * @return
     */
    //@Scheduled(cron = "0 15 8,12 * * ?")
    /*public void Choujian(){
        ArrayList<FrmBaoJianResultMainEntity> parmlistPT = new ArrayList<FrmBaoJianResultMainEntity>();
        ArrayList<FrmBaoJianResultMainEntity> parmlistPTjy = new ArrayList<FrmBaoJianResultMainEntity>();
        ArrayList<String> parmlistTS = new ArrayList<String>();
        ArrayList<FrmBaoJianResultMainEntity> arrayList = QuerybyDaijian();
        ArrayList<FrmBaoJianResultMainEntity> codelist = QueryPlinecode();
        for(int i=0;i<arrayList.size();i++){
            if(arrayList.get(i).getSteelname()!=null&&!arrayList.get(i).getSteelname().equals("")&&!arrayList.get(i).getStdName().equals("")&&arrayList.get(i).getStdName()!=null){
                if(arrayList.get(i).getSteelname().equals("20")&&arrayList.get(i).getStdName().contains("8162")||arrayList.get(i).getStdName().contains("8163")||arrayList.get(i).getStdName().contains("3087")){
                    FrmBaoJianResultMainEntity entity = new FrmBaoJianResultMainEntity();
                    entity.setIdcode(arrayList.get(i).getIdcode());
                    entity.setPlineCode(arrayList.get(i).getPlineCode());
                    parmlistPT.add(entity);
                }else{//这是后续添加JY的分开抽
                    if(arrayList.get(i).getStdName().toUpperCase().contains("JY")) {
                        FrmBaoJianResultMainEntity entity = new FrmBaoJianResultMainEntity();
                        entity.setIdcode(arrayList.get(i).getIdcode());
                        entity.setPlineCode(arrayList.get(i).getPlineCode());
                        parmlistPTjy.add(entity);
                    }
                    else {
                        parmlistTS.add(arrayList.get(i).getIdcode());
                    }
                }
            }else{
                parmlistTS.add(arrayList.get(i).getIdcode());
            }
            }
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        try {
            for (int i = 0; i < codelist.size(); i++) {
                ArrayList<String> listXH = new ArrayList();
                for (int j = 0; j < parmlistPT.size(); j++) {//添加待抽的
                    if(codelist.get(i).getPlineCode().equals(parmlistPT.get(j).getPlineCode())){
                        listXH.add(parmlistPT.get(j).getIdcode());
                    }
                }

                ArrayList<String> listXHJY = new ArrayList();
                for (int j = 0; j < parmlistPTjy.size(); j++) {//添加JY待抽的
                    if(codelist.get(i).getPlineCode().equals(parmlistPTjy.get(j).getPlineCode())){
                        listXHJY.add(parmlistPTjy.get(j).getIdcode());
                    }
                }
                connection.setAutoCommit(false);
                Random rd = new Random();
                int parm = (int)Math.ceil(((double)listXH.size())/10);
                ArrayList list = new ArrayList();
                for(int x=0;x<parm;x++){
                    list.add(rd.nextInt(listXH.size()));
                }//十抽一

                int parmJY = (int)Math.ceil(((double)listXHJY.size())/10);//含有jy字段的十抽一
                for(int x=0;x<parmJY;x++){
                    list.add(rd.nextInt(listXHJY.size()));//抽中的合并成一个
                }//十抽一
                 for (int y=0;y<listXHJY.size();y++)//合并成一个进行判断
                 {
                     listXH.add(listXHJY.get(y));
                 }
                for(int y=0;y<listXH.size();y++){
                    if(list.contains(y)){//抽中执行待检
                        String idcode =  listXH.get(y);
                        String sql = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.Chouajian");
                        sql += " AND T.IDCODE = '"+idcode+"'";
                        PreparedStatement ps = connection.prepareStatement(sql);
                        ps.setObject(1,1);
                        ps.addBatch();
                        ps.executeBatch();
                        connection.commit();
                    }else{//没抽中执行免检
                        String idcode = listXH.get(y);
                        String sql = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.Chouajian");
                        String sqlSP = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.SongPanPT");
                        sql += " AND T.IDCODE = '"+idcode+"'";
                        sqlSP += " AND T.IDCODE = '"+idcode+"'";
                        PreparedStatement ps = connection.prepareStatement(sql);
                        ps.setObject(1,3);
                        ps.addBatch();
                        ps.executeBatch();
                        ps = connection.prepareStatement(sqlSP);
                        ps.setObject(1,1);
                        ps.addBatch();
                        ps.executeBatch();
                        connection.commit();
                    }
                }
            }

            for(int i=0;i<parmlistTS.size();i++){//剩下特殊的执行待检
                connection.setAutoCommit(false);
                String sql = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.Chouajian");
                sql += " AND T.IDCODE = '"+parmlistTS.get(i)+"'";
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setObject(1,1);
                ps.addBatch();
                ps.executeBatch();
                connection.commit();
            }
            cro.setV_errMsg("已抽检!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            if(connection != null){
                try{
                    cro.setV_errMsg(e.getMessage());
                    connection.rollback();
                }catch (Exception e2){
                }
            }
        } finally {
            try {
                if(connection != null && !connection.isClosed()){
                    connection.close();
                }
            }catch (Exception e){
            }
        }
    }*/

    /**
     * 抽检
     */
    @Scheduled(cron = "0 15 8,12 * * ?")
    public void Choujian2(){
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        try {
        	String sql = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.Chouajian2");
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.addBatch();
            ps.executeBatch();
            connection.commit();
            cro.setV_errMsg("已抽检!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            if(connection != null){
                try{
                    cro.setV_errMsg(e.getMessage());
                    connection.rollback();
                }catch (Exception e2){
                }
            }
        } finally {
            try {
                if(connection != null && !connection.isClosed()){
                    connection.close();
                }
            }catch (Exception e){
            }
        }
    }

    /**
     * 修改支数
     * @param parmlist
     * @return
     */
    public CoreReturnObject UpdateActCount(ArrayList<String> parmlist){
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();
        try {
            connection.setAutoCommit(false);
            String sql = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.UpdateActCount");
            sql += " AND T.INSPECTION_NO = '"+parmlist.get(0)+"'";
            sql += " AND T.INSPECTION_NUM = '"+parmlist.get(1)+"'";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setObject(1,parmlist.get(2));
            ps.setObject(2,parmlist.get(3));
            ps.setObject(3,parmlist.get(4));
            ps.setObject(4,parmlist.get(5));
            ps.addBatch();
            ps.executeBatch();
            connection.commit();
            cro.setV_errMsg("修改成功!");

        } catch (Exception e) {
            System.out.println(e.getMessage());
            if(connection!=null){
                try {
                    cro.setV_errMsg(e.getMessage());
                    connection.rollback();
                }catch (Exception e2){

                }
            }
        } finally {
            try {
                if(connection!=null&&!connection.isClosed()){
                    connection.close();
                }
            }catch (Exception e){

            }
        }
        return cro;
    }

    /**
     * 查询产线
     * @param PROCESS_CODE
     * @return
     */
    public CoreReturnObject doQueryPlineCode(String PROCESS_CODE){
        String sql =CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.doQueryPlineCode");
        return this.getData(sql,new Object[]{ PROCESS_CODE });
    }


    /**
     * 待抽改 待检/免检
     * @return
     */
    public CoreReturnObject UpdateInsflag(ArrayList<String> arrayList){
        CoreReturnObject cro = new CoreReturnObject();
        Connection connection = this.getDao(CoreConstantData.sysbeanstr).getConnection();

        try {
            connection.setAutoCommit(false);
            if(arrayList.get(2).equals("1")){
                String sql = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.UpdateInsflag1");
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setObject(1,arrayList.get(2));
                ps.setObject(2,arrayList.get(0));
                ps.setObject(3,arrayList.get(1));
                ps.addBatch();
                ps.executeBatch();
            }else if(arrayList.get(2).equals("3")){
                String sql = CoreXmlSqlParsersFactory.getSql("FrmBaoJianResult.UpdateInsflag2");
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setObject(1,arrayList.get(2));
                ps.setObject(2,1);
                ps.setObject(3,arrayList.get(0));
                ps.setObject(4,arrayList.get(1));
                ps.addBatch();
                ps.executeBatch();
            }
            connection.commit();
            cro.setV_errMsg("修改成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            if(connection != null){
                try {
                    cro.setV_errMsg(e.getMessage());
                    connection.rollback();
                }catch (Exception e2){
                }
            }
        } finally {
            try {
                if(connection !=null && !connection.isClosed()){
                    connection.close();
                }
            }catch (Exception e){
            }
        }
        return cro;
    }

}
