package weaver.interfaces.htsc.GlobatTools;

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.TimeUtil;
import weaver.general.Util;
import weaver.hrm.User;

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

public class Smstools {

    /**
     * 发送消息提醒
     * @param smstypeid
     * @param sendIdSet
     * @param title
     * @param context
     * @param linkUrl
     * @param linkMobileUrl
     */
    public boolean sendMessage(
                             int smstypeid,
                             Set<String> sendIdSet,
                             String title,
                             String context,
                             String linkUrl,
                             String linkMobileUrl){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        boolean flag=true;
        String sumcount="";
       String sql=" select count(*) as sumcount from  ECOLOGY_MESSAGE_TYPE  where id='"+smstypeid+"'";

        rs.execute(sql);
        if(rs.next()){
            sumcount=Util.null2String(rs.getString("sumcount"));
        }
        if("0".equals(sumcount)){
            return false;
        }
       // title="<span>"+title+"</span>";

        MessageType messageType = MessageType.newInstance(smstypeid); // 消息来源（见文档第四点补充 必填）
        try {
            Set<String>  MainsendidSet=   gethrmmainid(sendIdSet);
            MessageBean messageBean = Util_Message.createMessage(messageType, MainsendidSet, "", context, linkUrl, linkMobileUrl);
            messageBean.setCreater(1);// 创建人id
            messageBean.setDetailTitle(title);
            Map<String,Object> map=new HashMap();
            map.put("maintitle",title);
            messageBean.setDetailTitleParams(map);
            messageBean.setEnablePC("y");
            messageBean.setEnableEM("y");
            //  messageBean.setTargetName(title);
            Util_Message.store(messageBean);

        } catch (IOException e) {
            flag=false;
            e.printStackTrace();
            bs.writeLog("send message error:",e);
        }
        return flag;
    }

        //将所有人的账号换成子账号
    public Set<String> gethrmmainid(Set<String> set){
        Set<String> setmain=new HashSet<>();
        for (String value:set) {
            String hrmid= JAVATools.getmainhrmid(value);
            if(!"".equals(hrmid)){
                setmain.add(hrmid);
            }
        }
        return  setmain;
    }


}
