package weaver.interfaces.zjrb.job;

import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.schedule.BaseCronJob;
import java.util.*;
import com.cloudstore.dev.api.bean.MessageBean;
import com.cloudstore.dev.api.bean.MessageType;
import com.cloudstore.dev.api.util.Util_Message;
import java.io.IOException;


public class MicroExaminationNoticeSendJob extends BaseCronJob {

    @Override
    public void execute() {
        BaseBean bb = new BaseBean();
        int code = Util.getIntValue(Util.null2String(bb.getPropValue("PICC_MICROEXAMINATION","code")),1315);
        int pagesize = Util.getIntValue(Util.null2String(bb.getPropValue("PICC_MICROEXAMINATION","pagesize")),500);

        ResourceComInfo resourceComInfo = null ;

        RecordSet rs = new RecordSet();
        List<Map<String,String>> ksList = new ArrayList<Map<String,String>>();
        try{
            resourceComInfo = new ResourceComInfo();

            String sql = " select t1.id,t1.kjbt,t1.kjsm,t1.ksksrq,t1.kskssj,t1.ksjsrq,t1.ksjssj,t1.modedatacreater,t1.modedatacreatedate,t1.modedatacreatetime from uf_ks_kjb t1 where to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') > (t1.ksksrq||' '||t1.kskssj) and  to_char(sysdate-2 ,'yyyy-mm-dd hh24:mi:ss') < (t1.ksksrq||' '||t1.kskssj)  ";
            bb.writeLog("MicroExaminationNoticeSendTimeJob-sql:"+sql);
            rs.execute(sql);
            while (rs.next()){
                String billid = Util.null2String(rs.getString("id"));
                String kjbt = Util.null2String(rs.getString("kjbt"));
                String kjsm = Util.null2String(rs.getString("kjsm"));
                Map<String,String> ksMap = new HashMap<String,String>();
                ksMap.put("billid",billid);
                ksMap.put("kjbt",kjbt);
                ksMap.put("kjsm",kjsm);

                String ksksrq = Util.null2String(rs.getString("ksksrq"));
                String kskssj = Util.null2String(rs.getString("kskssj"));
                String ksjsrq = Util.null2String(rs.getString("ksjsrq"));
                String ksjssj = Util.null2String(rs.getString("ksjssj"));
                String modedatacreater = Util.null2String(rs.getString("modedatacreater"));
                String modedatacreatedate = Util.null2String(rs.getString("modedatacreatedate"));
                String modedatacreatetime = Util.null2String(rs.getString("modedatacreatetime"));
                String cjrname = "";
                if(StringUtils.isNotBlank(modedatacreater)){
                    cjrname = resourceComInfo.getLastname(modedatacreater);
                }

                ksMap.put("ksksrq",ksksrq);
                ksMap.put("kskssj",kskssj);
                ksMap.put("ksjsrq",ksjsrq);
                ksMap.put("ksjssj",ksjssj);
                ksMap.put("cjrname",cjrname);
                ksMap.put("modedatacreatedate",modedatacreatedate);
                ksMap.put("modedatacreatetime",modedatacreatetime);

                ksList.add(ksMap);
            }
        }catch (Exception e){
            bb.writeLog("e:"+e);
        }

        bb.writeLog("ksList:"+ksList.size());
        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");
            String kjsm = ksMap.get("kjsm");

            String ksksrq = ksMap.get("ksksrq");
            String kskssj = ksMap.get("kskssj");
            String ksjsrq = ksMap.get("ksjsrq");
            String ksjssj = ksMap.get("ksjssj");
            String cjrname = ksMap.get("cjrname");
            String modedatacreatedate = ksMap.get("modedatacreatedate");
            String modedatacreatetime = ksMap.get("modedatacreatetime");

            String context =""+kjsm+"</br>"+
                            "开始时间:"+ksksrq+" "+kskssj+"</br>"+
                            "结束时间:"+ksjsrq+" "+ksjssj+"</br>";

            bb.writeLog("MicroExaminationNoticeSendJob-title:"+title);
            bb.writeLog("MicroExaminationNoticeSendJob-billid:"+billid);
            
            int unSendCount = 0;
            String sql = " select id from uf_ksks where mainid= "+billid +" and nvl(sfyfstz,1) = 1  ";
            bb.writeLog("MicroExaminationNoticeSendJob-sql:"+sql);
            rs.executeQuery(sql);
            if(rs.next()){
                unSendCount++;
            }
            bb.writeLog("MicroExaminationNoticeSendJob-unSendCount:"+unSendCount);

            if(unSendCount > 0){
                boolean hasNextPage = true;
                while (hasNextPage){
                    bb.writeLog("MicroExaminationNoticeSendJob-hasNextPage:"+hasNextPage);
                    try{
                        List<List> dt2List = new ArrayList<List>();
                        List<String> userList = new ArrayList<String>();

                        sql = " select * from ( select id,ks from uf_ksks where mainid= "+billid +" and nvl(sfyfstz,1) = 1 order by id ) where rownum < "+pagesize;
                        bb.writeLog("MicroExaminationNoticeSendJob-sql:"+sql);
                        rs.executeQuery(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);
                        }
                        bb.writeLog("MicroExaminationNoticeSendJob-userList:"+userList.size());
                        if(userList.size() > 0){
                            senNotice(code,title,context,dt2List,userList,billid);
                        }
                        
                        if(userList.size() == 0){
                            hasNextPage = false;
                        }
                        if(userList.size() < pagesize){
                            hasNextPage = false;
                        }

                        bb.writeLog("MicroExaminationNoticeSendJob-hasNextPage2:"+hasNextPage);
                    }catch (Exception e){
                        bb.writeLog("MicroExaminationNoticeSendJob-e2:"+e);
                    }
                }
            }
        }
    }

    /***
     *
     * @param code
     * @param title
     * @param context
     * @param dt2List
     * @param userList
     */
    public void senNotice(int code,String title,String context,List<List> dt2List,List<String> userList,String billid){
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        MessageType messageType = MessageType.newInstance(code); // 消息来源（见文档第四点补充 必填）
        String linkUrl = "/mobilemode/mobile/view.html?appid=28003#&page_38505?kjid="+billid; // PC端链接
        String linkMobileUrl = "/mobilemode/mobile/view.html?appid=28003#&page_38505?kjid="+billid; // 移动端链接
        try {
            Set<String> userSet = new HashSet<>(userList);
            MessageBean messageBean = Util_Message.createMessage(messageType, userSet, title, context, linkUrl, linkMobileUrl);
            messageBean.setCreater(1);// 创建人id
            boolean isTrue = Util_Message.store(messageBean);
            bb.writeLog("MicroExaminationNoticeSendJob-senNotice:"+isTrue);
            if(isTrue){
                String sql = " update uf_ksks set sfyfstz = 0 where id = ? ";
                isTrue = rs.executeBatchSql(sql,dt2List);
                bb.writeLog("MicroExaminationNoticeSendJob-senNotice2:"+isTrue);
            }
        } catch (IOException e) {
            bb.writeLog("MicroExaminationNoticeSendJob-e3:"+e);
        }
    }

}
