package weaver.interfaces.htsc.yfkqrlc;

import com.alibaba.fastjson.JSONObject;
import netscape.javascript.JSObject;
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 weaver.workflow.logging.Logger;
import weaver.workflow.logging.LoggerFactory;

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

/**
 * @tableName uf_PrepayMageAccRep
 * 自动任务-逾期流程提醒：新增自动任务每天定时扫描待核销金额>0且逾期时间为10、30、60天的付款流程，
 * 并触发通知到经办人铃客
 * 通知模板：【“您的“付款流程号+流程标题”待提供发票，请及时取得发票并提交预付款费用确认流程。】；
 */
public class SendMessage extends BaseCronJob {

    Logger logger = LoggerFactory.getLogger(SendMessage.class);

    @Override
    public void execute() {
        logger.info("=======进入逾期流程铃客消息提醒的定时任务=========");
        PropBean pp = new PropBean();
        int smstypeid = 0;
//        定时扫描uf_PrepayMageAccRep表，获取待核销金额>0并且逾期天数为10、30、60的付款流程编号、流程标题、拟稿人
        List<PaymentWfInfo> needSendMessageList = getNeedSendMessageList();
        logger.info("需要发送消息提醒的流程是：" + JSONObject.toJSONString(needSendMessageList));
        for (PaymentWfInfo paymentWfInfo : needSendMessageList) {
            Smstools smstools = new Smstools();
            smstypeid = Util.getIntValue(pp.getPropName("fptgyqtstx"));
            Set<String> cjrsetuser = new HashSet<>();
            cjrsetuser.add(Util.null2String(paymentWfInfo.getNgr()));
            String bt = "付款流程发票提供逾期天数提醒";
            String context = "【“您的“" + paymentWfInfo.getLcbh() + " " + paymentWfInfo.getLcbt() + "”待提供发票，请及时取得发票并提交预付款费用确认流程。】";
            boolean sendResult = smstools.sendMessage(smstypeid, cjrsetuser, bt, context, "", "");
            if (sendResult) {
                logger.info("消息发送成功");
            } else {
                logger.info("消息发送失败，消息内容是：" + context);
            }
        }

    }

    private List<PaymentWfInfo> getNeedSendMessageList() {
        RecordSet rs = new RecordSet();
        List<PaymentWfInfo> paymentWfInfos = new ArrayList<>();
        String sql = "";
        String fklcbhs = "";
        // 1 获取付款流程编号
        sql = "select wm_concat_old(FKLCBH) as fklcbhs from uf_PrepayMageAccRep where DHXJE > 0 and YQTS in (10,30,60)";
        rs.executeQuery(sql);
        if (rs.next()) {
            fklcbhs = Util.null2String(rs.getString("fklcbhs"));
        }
        if (!"".equals(fklcbhs)) {
            // 2 获取付款流程的编号、流程标题、拟稿人
            for (String fklcbh : fklcbhs.split(",")) {
                String mainTable = "";
                // 2.1 根据流程编号获取流程对应的主表名
                sql = "select t1.tablename from workflow_bill t1,workflow_base t2,workflow_requestbase t3  where t1.id =t2.formid and t2.id = t3.workflowid and t3.REQUESTMARK = ?";
                rs.executeQuery(sql, fklcbh);
                if (rs.next()) {
                    mainTable = Util.null2String(rs.getString("tablename"));
                }
                if (!"".equals(mainTable)) {
                    sql = "select REQUESTNAME,ngr from " + mainTable + " a join workflow_requestbase b on a.requestid = b.REQUESTID where lcbh = ?";
                    rs.executeQuery(sql, fklcbh);
                    if (rs.next()) {
                        PaymentWfInfo paymentWfInfo = new PaymentWfInfo();
                        paymentWfInfo.setLcbh(fklcbh);
                        paymentWfInfo.setLcbt(Util.null2String(rs.getString("REQUESTNAME")));
                        paymentWfInfo.setNgr(Util.null2String(rs.getString("ngr")));
                        paymentWfInfos.add(paymentWfInfo);
                    }
                }
            }
        }
        return paymentWfInfos;
    }


}
