package com.engine.htsc.coworkreport.cmd;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.GlobatTools.Smstools;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.*;

public class TaskMessageCmd extends AbstractCommonCommand<Map<String, Object>> {
    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    public TaskMessageCmd(Map<String, Object> params, User user) {
        this.params = params;
        this.user = user;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> appdate = new HashMap<String, Object>();
        ResourceComInfo resComInfo;
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();

        Smstools Smstools = new Smstools();
        PropBean propBean = new PropBean();
        try{
            resComInfo = new ResourceComInfo();
            int userid = user.getUID();
            String lastname = resComInfo.getLastname(userid+"");
            String billid = Util.null2String(params.get("billid"));
            String jbr = "" ;
            String rwbt = "" ;
            String sql = " select rwbt,jbr from uf_rw where id="+billid;
            rs.execute(sql);
            if (rs.next()){
                jbr = Util.null2String(rs.getString("jbr"));
                rwbt = Util.null2String(rs.getString("rwbt"));
            }
            if(!"".equals(jbr))
            {
                String jbrzgid = "";
                sql = " select case when belongto is not null then belongto else id end as id  from hrmresource where id in("+jbr+") ";
                rs.execute(sql);
                while (rs.next()){
                    jbrzgid += jbrzgid == "" ? Util.null2String(rs.getString("id")) : Util.null2String(rs.getString("id")) ;
                }
                int smstypeid = Integer.parseInt(Util.null2String(propBean.getPropName("uf_ry_sms")));
                String link = "";
                String bt = "任务未处理提醒";
                String contextjbr = lastname+"提醒您及时处理协作任务“"+ rwbt +"”，您可以在PC端查看详情";
                Set<String> jbrsetuser = new HashSet(Arrays.asList(jbrzgid.split(",")));
                boolean flag = Smstools.sendMessage(smstypeid,jbrsetuser,bt,contextjbr,link,link);
                System.out.println("flag:"+flag);
                if(flag){
                    appdate.put("api_status",true);
                    appdate.put("status","1");
                    appdate.put("message","成功发送提醒");
                }else{
                    appdate.put("api_status",false);
                    appdate.put("api_errormsg","发生消息异常");
                    appdate.put("status","0");
                    appdate.put("message","发送提醒失败");
                }
            }else{
                appdate.put("api_status",false);
                appdate.put("api_errormsg","经办人未空");
            }
        }catch (Exception e){
            bb.writeLog("e:"+e);
        }
        return appdate;
    }

    public static void main(String[] args) {
        GCONST.setRootPath("WEB-INF/");
        GCONST.setServerName("ecology");
        Map<String, Object> params=new HashMap<>();
        params.put("billid","1765");

        User user = new User();
        user.setUid(11260);
        TaskMessageCmd htscMhjcCmd=new TaskMessageCmd(params,user);
        CommandContext ceshi = new CommandContext(null,null);
        htscMhjcCmd.execute(ceshi);
    }
}
