package weaver.interfaces.zjrb.job;

import com.cloudstore.dev.api.bean.MessageBean;
import com.cloudstore.dev.api.bean.MessageType;
import com.cloudstore.dev.api.util.Util_Message;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

import java.io.IOException;
import java.util.*;


public class MicroExaminationNoticeSendRunnable extends BaseCronJob {

    @Override
    public void execute() {
        BaseBean bb = new BaseBean();
        int code = 20 ;
        String context = "要开始考试了" ;

        RecordSet rs = new RecordSet();

        List<Map<String,String>> ksList = new ArrayList<Map<String,String>>();
        String sql = " select kjbt from uf_ks_kjb where (to_date(t1.ksksrq||' '||t1.kskssj,'yyyy-mm-dd hh24:mi:ss')-sysdate)*24*60*60 <= 10*60";
        rs.execute(sql);
        while (rs.next()){
            String billid = Util.null2String(rs.getString("id"));
            String kjbt = Util.null2String(rs.getString("kjbt"));
            Map<String,String> ksMap = new HashMap<String,String>();
            ksMap.put("billid",billid);
            ksMap.put("kjbt",kjbt);
            ksList.add(ksMap);
        }

        for(int i=0;i<ksList.size();i++){
            Map<String,String> ksMap = ksList.get(i);
            String title = ksMap.get("kjbt");
            String billid = ksMap.get("billid");
            int pageSize = 500;
            boolean hasNextPage = true;
            while (hasNextPage){
                try{
                    List<List> dt2List = new ArrayList<List>();
                    List<String> userList = new ArrayList<String>();
                    sql = " select id,ks from uf_ks_kjb_dt2 where mainid= "+billid +" and nvl(sfyfstz,1) = 1 and order by id ";
                    rs.execute(sql);
                    while (rs.next()){
                        String dt2id = Util.null2String(rs.getString("id"));
                        String ks = Util.null2String(rs.getString("ks"));
                        userList.add(ks);

                        List<String> dtIdList = new ArrayList<String>();
                        dtIdList.add(dt2id);
                        dt2List.add(dtIdList);
                    }
                    senNotice(code,title,context,dt2List,userList);
                    if(dt2List.size() == 0){
                        hasNextPage = false;
                    }
                    if(dt2List.size() < pageSize){
                        hasNextPage = false;
                    }
                }catch (Exception e){

                }
            }
        }

//        for(int i=0;i<noticeList.size();i++){
//            Map<String,Object> noticeMap = noticeList.get(i);
//            String title = Util.null2String(noticeMap.get("title"));
//            List<String> userList = (List)noticeMap.get("userList");
//            senNotice(code,title,context,userList);
//        }
    }


    public void senNotice(int code,String title,String context,List<List> dt2List,List<String> userList){
        RecordSet rs = new RecordSet();
        MessageType messageType = MessageType.newInstance(code); // 消息来源（见文档第四点补充 必填）
        String linkUrl = ""; // PC端链接
        String linkMobileUrl = ""; // 移动端链接
        try {
            Set<String> userSet = new HashSet<>(userList);
            MessageBean messageBean = Util_Message.createMessage(messageType, userSet, title, context, linkUrl, linkMobileUrl);
            messageBean.setCreater(1);// 创建人id
            //message.setBizState("0");// 需要修改消息为已处理等状态时传入,表示消息最初状态为待处理
            //messageBean.setTargetId("121|22"); //消息来源code +“|”+业务id需要修改消息为已处理等状态时传入
            boolean isTrue = Util_Message.store(messageBean);
            if(isTrue){
                String sql = " update uf_ks_kjb_dt2 set sfyfstz = 0 where id = ? ";
                isTrue = rs.executeBatchSql(sql,dt2List);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
