package weaver.interfaces.htsc.hthy.cron;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.Smstools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ProcessNoticeJob extends BaseCronJob {
    private Log log = LogFactory.getLog(this.getClass());

    @Override
    public void execute() {

        log.info("======定时任务发起通知流程提醒开始======");
        try {
            //流程提醒
            sendProcessNotice();
            //通知提醒
            sendNotice();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("发起通知流程提醒失败" + e.getMessage());
        }
    }

    /**
     * 发送会议通知提醒，会议中“通知时间”设置的前一天天
     *
     */
    private void sendNotice() {
        PropBean pb = new PropBean();
        int smstypeid = Integer.parseInt(Util.null2String(pb.getPropName("uf_db_sms")));
        String meetingSMSNoticeContext = Util.null2String(pb.getPropName("meetingSMSNoticeContext"));

        RecordSet getMeetingList = new RecordSet();
        getMeetingList.execute(" select hycbry,hymc,tzsj  from uf_hywh t WHERE substr(tzsj,0,10)=to_char(sysdate+1,'yyyy-mm-dd') and (t.hyzt is null or t.hyzt = 0) ");
        while (getMeetingList.next()) {
            try {
                //会议名称
                String hymc = Util.null2String(getMeetingList.getString("hymc"));
                //标题
                String title = Util.null2String(pb.getPropName("meetingSMSNoticeTitle"));
                //内容
                String context = String.format(meetingSMSNoticeContext, hymc);
                //筹备人
                String hycbry = Util.null2String(getMeetingList.getString("hycbry"));
                if(StringUtils.isNotBlank(hycbry)){
                    List<String> userList = Arrays.asList(hycbry.split(","));
                    Set<String> sendSet = new HashSet<>(userList);
                    sendSMS(smstypeid, title, context, sendSet);
                }
            } catch (Exception e) {
                log.error("会议消息发送异常" + e.getMessage());
            }
        }
    }

    /**
     * 发送通知流程提醒，会议中“通知流程发起时间”设置的当天
     *
     */
    private void sendProcessNotice() {
        PropBean pb = new PropBean();
        int smstypeid = Integer.parseInt(Util.null2String(pb.getPropName("uf_db_sms")));
        String meetingNoticeProcessContextTemplate = Util.null2String(pb.getPropName("meetingNoticeProcessContext"));

        RecordSet getMeetingList = new RecordSet();
        getMeetingList.execute(" select hycbry,hymc,tzlcfqsj from uf_hywh t WHERE substr(tzlcfqsj,0,10)=to_char(sysdate,'yyyy-mm-dd') and (t.hyzt is null or t.hyzt = 0) ");
        while (getMeetingList.next()) {
            try {
                //会议名称
                String hymc = Util.null2String(getMeetingList.getString("hymc"));
                //标题
                String title = Util.null2String(pb.getPropName("meetingNoticeProcessTitle"));
                //内容

                String context = String.format(meetingNoticeProcessContextTemplate, hymc);
                //筹备人
                String hycbry = Util.null2String(getMeetingList.getString("hycbry"));
                if(StringUtils.isNotBlank(hycbry)){
                    List<String> userList = Arrays.asList(hycbry.split(","));
                    Set<String> sendSet = new HashSet<>(userList);
                    sendSMS(smstypeid, title, context, sendSet);
                }
            } catch (Exception e) {
                log.error("会议消息发送异常" + e.getMessage());
            }

        }
    }

    private void sendSMS(int smstypeid, String title, String context, Set<String> sendSet) {


        try {

            Smstools smstools = new Smstools();
            boolean result = smstools.sendMessage(smstypeid, sendSet, title, context, null, null);
            if (!result) {
                log.error(String.format("会议消息发送异常，smstypeid:%s,sendSet:%s,title:%s,context:%s", smstypeid, sendSet, title, context));
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("会议消息发送异常" + e.getMessage());
        }

    }

}
