package weaver.interfaces.htsc.hthy;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.GlobatTools.Smstools;
import weaver.interfaces.htsc.comInfo.PropBean;

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

public class MeetingSmsNotice {

    private final Log log = LogFactory.getLog(MeetingSmsNotice.class.getName());

    /**
     * 发送提醒短信
     *
     * @param billid 会议维护主键
     * @throws Exception
     */
    public void smsNotice(String billid) {
        try {
            PropBean pb = new PropBean();
            int smstypeid = Integer.parseInt(Util.null2String(pb.getPropName("uf_db_sms")));
            //获取会议筹备人员
            RecordSet getMeetingInfo = new RecordSet();
            getMeetingInfo.execute("select hycbry,hymc , hycjr from uf_hywh where id = " + billid);
            if (getMeetingInfo.next()) {
                String hycbry = Util.null2String(getMeetingInfo.getString("hycbry"));
                String hymc = Util.null2String(getMeetingInfo.getString("hymc")); //会议名称
                String hycjr = Util.null2String(getMeetingInfo.getString("hycjr"));
                List<String> userList = Arrays.asList(hycbry.split(","));
                String title = Util.null2String(pb.getPropName("meetingNoticeTitle"));
                String meetingNoticeContext = Util.null2String(pb.getPropName("meetingNoticeContext"));

                ResourceComInfo rc = new ResourceComInfo();
                String createMeetingUserName = rc.getLastname(hycjr);//创建人
                String context = String.format(meetingNoticeContext, createMeetingUserName, hymc);//提醒

                Set<String> sendSet = new HashSet<>(userList);
                if (CollectionUtils.isNotEmpty(sendSet)) {
                    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) {
            log.error(String.format("会议提醒异常，%s",e.getMessage()));
            e.printStackTrace();
        }
    }

}
