package weaver.interfaces.htsc.wdzx;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.*;
import java.util.stream.Collectors;


public class ChangeUfZt  extends BaseCronJob {
    @Override
    public void execute() {
        orderBySXRQ();
    }
    public void changeZt(){
        DateTime nowDate = DateUtil.parse(DateUtil.today());
        String sql= "select * from uf_wdzx_dt1 where zt != 2 order by id asc";
        RecordSet rs = new RecordSet();
        rs.execute(sql);
        while(rs.next()){
            String mainid = rs.getString("mainid");
            String id = rs.getString("id");
            String bbh = rs.getString("bbh");
            //生效时间
            DateTime sxrq = DateUtil.parse(rs.getString("sxrq"));
            DateTime sxrq1 = null;
            if(StringUtils.isNotBlank(rs.getString("sxrq1"))){
                sxrq1 = DateUtil.parse(rs.getString("sxrq1"));
            }
            //获取档前时间大于生效时间的
            if(!(DateUtil.between(nowDate,sxrq, DateUnit.DAY,false)>0)){
                //判断失效时间是否为null
                //1失效时间为null
                if(StringUtils.isBlank(rs.getString("sxrq1"))){
                    //修改状态为已生效
                    String sql1 = "update uf_wdzx_dt1 set zt = 1 where id = " + id;
                    RecordSet rs1 = new RecordSet();
                    rs1.execute(sql1);
                    //更新主表的生效文件
                    String mainsql = "select * from uf_wdzx_dt1 where id = " + id;
                    RecordSet mainrs = new RecordSet();
                    mainrs.execute(mainsql);
                    String upwjcjsj = rs.getString("cjsj");
                    String upwj = rs.getString("wj");
                    String upsxrq = rs.getString("sxrq");
                    String sql4 = "update uf_wdzx set bb = '"+bbh.substring(1)+"',wjcjsj = '"+upwjcjsj+"' ,sxrq = '"+upsxrq+"',wj = '"+upwj+"' where id = " + mainid;
                    RecordSet rs4 = new RecordSet();
                    rs4.execute(sql4);
                }else{
                    //2失效时间不为null
                    //1档前时间大于生效时间且小于失效时间
                    if(DateUtil.between(sxrq,nowDate, DateUnit.DAY)>=0 && DateUtil.between(nowDate,sxrq1,DateUnit.DAY,false) >= 0){
                        //修改状态为已生效
                        String sql2 = "update uf_wdzx_dt1 set zt = 1 where id = " + id;
                        RecordSet rs2 = new RecordSet();
                        rs2.execute(sql2);
                        //更新主表的生效文件
                        String mainsql = "select * from uf_wdzx_dt1 where id = " + id;
                        RecordSet mainrs = new RecordSet();
                        mainrs.execute(mainsql);
                        String upwjcjsj = rs.getString("cjsj");
                        String upwj = rs.getString("wj");
                        String upsxrq = rs.getString("sxrq");
                        String sql4 = "update uf_wdzx set bb = '"+bbh.substring(1)+"',wjcjsj = '"+upwjcjsj+"' ,sxrq = '"+upsxrq+"',wj = '"+upwj+"' where id = " + mainid;
                        RecordSet rs4 = new RecordSet();
                        rs4.execute(sql4);
                    }else{
                        //修改状态已失效
                        String sql3 = "update uf_wdzx_dt1 set zt = 2 where id = " + id;
                        RecordSet rs3 = new RecordSet();
                        rs3.execute(sql3);
                        //修改主表的文件创建时间和生效日期为空
                        String sql4 = "update uf_wdzx set bb = '',wjcjsj = '' ,sxrq = '',wj = '' where id = " + mainid;
                        RecordSet rs4 = new RecordSet();
                        rs4.execute(sql4);
                    }
                }
            }
        }

        //根据明细表更新建模主表的状态
        String ztsql = " select distinct(mainid) mainid   from uf_wdzx_dt1  where mainid is not null order by mainid desc";
        List<Map<String,String>> listAll = JAVATools.GetTableToListMap(ztsql, new HashMap<String, String>());
        for (Map map :listAll) {
            //更新建模主表的状态
            String mainid = (String)map.get("mainid");
            String ztsqsl = "select * from uf_wdzx_dt1 where mainid = " + mainid;
            RecordSet ztrs = new RecordSet();
            ztrs.execute(ztsqsl);
            ArrayList<String> list = new ArrayList<>();
            while(ztrs.next()){
                list.add(ztrs.getString("zt"));
            }
            if(list.contains("1")){
                String zbsql = "update uf_wdzx set zt = 1"+" where id = " + mainid;
                RecordSet zbrs = new RecordSet();
                zbrs.execute(zbsql);
            }else if(list.contains("0")){
                String zbsql = "update uf_wdzx set zt = 0"+" where id = " + mainid;
                RecordSet zbrs = new RecordSet();
                zbrs.execute(zbsql);
            }else{
                String zbsql = "update uf_wdzx set zt = 2"+" where id = " + mainid;
                RecordSet zbrs = new RecordSet();
                zbrs.execute(zbsql);
            }
        }
    }
    public void orderBySXRQ(){
        //获取所有的mainid 分别去获取明细表
        String mmainSql = "select distinct(mainid) mainid,BBH from uf_wdzx_dt1 where mainid is not null";
        RecordSet mainnrs = new RecordSet();
        mainnrs.execute(mmainSql);
        while(mainnrs.next()){
            DateTime now = DateUtil.parse(DateUtil.today());
            String mainid =  mainnrs.getString("mainid");
            String bbh =  mainnrs.getString("BBH");
            //获取当前mianid下的未失效的明细表
            String dtSql = "select * from uf_wdzx_dt1 where mainid = "+ mainid +" and zt != 2";
            RecordSet dtrs = new RecordSet();
            dtrs.execute(dtSql);
            //时生效的时间和id
            ArrayList<Map<DateTime,String>> list = new ArrayList<>();
            //生效的时间
            ArrayList<DateTime> listTime = new ArrayList<DateTime>();
            //生效的id
            ArrayList<String> listId = new ArrayList<String>();

            while(dtrs.next()){
                String id = dtrs.getString("id");
                //生效时间
                DateTime sxrq = DateUtil.parse(dtrs.getString("sxrq"));
                DateTime sxrq1 = null;
                if(StringUtils.isNotBlank(dtrs.getString("sxrq1"))){
                    sxrq1 = DateUtil.parse(dtrs.getString("sxrq1"));
                }
                //当前时间大于生效时间
                if(DateUtil.between(sxrq,now,DateUnit.DAY,false)>=0 ){
                    //失效日期为空的一直生效
                    if(StringUtils.isBlank(dtrs.getString("sxrq1"))){
                        HashMap<DateTime, String> map = new HashMap<>();
                        map .put(sxrq,id);
                        list.add(map);
                        listTime.add(sxrq);
                        listId.add(id);
                        //更新明细表的状态
                        String upSql = "update uf_wdzx_dt1 set zt = 1 where id = " + id;
                        RecordSet uprs = new RecordSet();
                        uprs.execute(upSql);
                    }else{
                        if(DateUtil.between(sxrq,now,DateUnit.DAY,false)>=0 && DateUtil.between(sxrq1,now,DateUnit.DAY,false)<0){
                            HashMap<DateTime, String> map = new HashMap<>();
                            map .put(sxrq,id);
                            list.add(map);
                            listTime.add(sxrq);
                            listId.add(id);
                            String upSql = "update uf_wdzx_dt1 set zt = 1 where id = " + id;
                            RecordSet uprs = new RecordSet();
                            uprs.execute(upSql);
                        }else{
                            String upSql = "update uf_wdzx_dt1 set zt = 2 where id = " + id;
                            RecordSet uprs = new RecordSet();
                            uprs.execute(upSql);
                        }
                    }

                }else{
                    //失效状态
                    if(StringUtils.isNotBlank(dtrs.getString("sxrq1")) &&DateUtil.between(sxrq1,now,DateUnit.DAY,false)>0){
                        String upSql = "update uf_wdzx_dt1 set zt = 2 where id = " + id;
                        RecordSet uprs = new RecordSet();
                        uprs.execute(upSql);
                        //修改主表的文件创建时间和生效日期为空
                        String sql4 = "update uf_wdzx set bb = '', wjcjsj = '' ,sxrq = '',wj = '',sxrq1='' where id = " + mainid;
                        RecordSet rs4 = new RecordSet();
                        rs4.execute(sql4);
                    }
                }
            }
            Optional<DateTime> max = listTime.stream().max(new Comparator<DateTime>() {
                @Override
                public int compare(DateTime o1, DateTime o2) {
                    return (int) DateUtil.between(o2, o1, DateUnit.DAY, false);
                }
            });
            //若同一个时间有多个id
            ArrayList<String> listadd= new ArrayList<String>();
            if(list.size()>0){
                for(int i = 0; i<list.size();i++){
                    if(list.get(i).get(max.get()) != null){
                        listadd.add(list.get(i).get(max.get()));
                    }
                }
            }
            if(listadd.size()>0){
                List<Integer> collect = listadd.stream().map((item) -> {
                    return Integer.parseInt(item);
                }).collect(Collectors.toList());
                Integer id = Collections.max(collect);
                listId.remove(id.toString());
                for(String upid : listId){
                    if(upid .equals("183")|| upid .equals("184")){
                        System.out.println(upid);
                    }
                    String sql = "update uf_wdzx_dt1 set zt = 2 ,sxrq1 = '"+ DateUtil.format(max.get(),"yyyy-MM-dd")+"'  where id = " +upid ;
                    RecordSet rs = new RecordSet();
                    rs.execute(sql);
                }
                //更新主表的生效文件
                String sql = "select * from  uf_wdzx_dt1 where id = "+id;
                RecordSet rs = new RecordSet();
                rs.execute(sql);
                rs.next();
                String wjcjsj = rs.getString("cjsj");
                String sxrq = rs.getString("sxrq");
                String sxrq1 = rs.getString("sxrq1");
                String wj = rs.getString("wj");
                String sql4 = "update uf_wdzx set bb = '"+bbh.substring(1)+"',wjcjsj = '"+wjcjsj+"' ,sxrq = '"+sxrq+"',wj = '"+wj+"', sxrq1 ='"+sxrq1+"' where id = " + mainid;
                RecordSet rs4 = new RecordSet();
                rs4.execute(sql4);
            }


            //更新主表的状态
            String ztsqsl = "select * from uf_wdzx_dt1 where mainid = " + mainid;
            RecordSet ztrs = new RecordSet();
            ztrs.execute(ztsqsl);
            ArrayList<String> ztlist = new ArrayList<>();
            while(ztrs.next()){
                ztlist.add(ztrs.getString("zt"));
            }
            if(ztlist.contains("1")){
                String zbsql = "update uf_wdzx set zt = 1"+" where id = " + mainid;
                RecordSet zbrs = new RecordSet();
                zbrs.execute(zbsql);
            }else if(ztlist.contains("0")){
                String zbsql = "update uf_wdzx set zt = 0"+" where id = " + mainid;
                RecordSet zbrs = new RecordSet();
                zbrs.execute(zbsql);
            }else{
                String zbsql = "update uf_wdzx set zt = 2"+" where id = " + mainid;
                RecordSet zbrs = new RecordSet();
                zbrs.execute(zbsql);
                //修改主表的文件创建时间和生效日期为空
                String sql4 = "update uf_wdzx set bb = '' ,wjcjsj = '' ,sxrq = '',wj = '',sxrq1='' where id = " + mainid;
                RecordSet rs4 = new RecordSet();
                rs4.execute(sql4);
            }
        }
    }

}
