package com.engine.htsc.cooperation;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.GlobatTools.MessageDetailLog;
import weaver.interfaces.htsc.GlobatTools.Smstools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cooperation.task.TaskTools;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.text.SimpleDateFormat;
import java.util.*;

public class TaskSmsToUser {

    @POST
    @Path("/TaskSmsToUser")
    @Produces({MediaType.TEXT_PLAIN})
    public String TaskSmsToUser(@Context HttpServletRequest request, @Context HttpServletResponse response){
        PropBean propBean = new PropBean();
        Smstools smstools=new Smstools();
        RecordSet rs = new RecordSet();
        ModeRightInfo mode = new ModeRightInfo();
        Map<String, Object> apidatas = new HashMap<String, Object>();
        SimpleDateFormat sd= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        MessageDetailLog meslog = new MessageDetailLog("uf_xzsy_dt1");
        String datetime = sd.format(new Date());
        String billid="";
        try{
            User user = HrmUserVarify.getUser(request, response);
            billid = Util.null2String(request.getParameter("billid"));
            String smsuser = Util.null2String(request.getParameter("smsuser"));
            String bt= Util.null2String(request.getParameter("bt"));
            String context  = Util.null2String(request.getParameter("context"));
            String type= Util.null2String(request.getParameter("type"));
            if(billid==""||smsuser==""||bt==""){
                apidatas.put("status", false);
                apidatas.put("errormsg", "传递参数为空");
            }
            int smstypeid=Integer.parseInt(Util.null2String(propBean.getPropName("uf_ry_sms")));
            JSONObject jsonObject=null;
            //如果type==0为是任务


            String link ="";
            if("0".equals(type)){
                jsonObject=propBean.getModeInfoByUUID(propBean.getPropName("UF_RW_CUBEUUIUD"));
                String url = propBean.getPropName("rw_mobile_url");
                link = url+"&billid="+billid;
            }else {
                jsonObject=propBean.getModeInfoByUUID(propBean.getPropName("UF_XZSY_CUBEUUIUD"));
                String url = propBean.getPropName("xz_mobile_url");
                link = url+"&billid="+billid;
            }

            if(jsonObject.size()<=0){
                apidatas.put("errmsg","消息失败"+type);
                apidatas.put("status", "false");
                return JSON.toJSONString(apidatas);
            }

//            String rwmodeid=jsonObject.get("modeid").toString();
//            String formid=jsonObject.get("formid").toString();

            //String link="/spa/cube/index.html#/main/cube/card?type=0&modeId="+rwmodeid+"&formId="+formid+"&billid="+billid;
            Set<String> setuseridvalue=new HashSet<>();
            setuseridvalue = new HashSet(Arrays.asList(smsuser.split(",")));
            if(setuseridvalue.size()>0){
                boolean flag=smstools.sendMessage(smstypeid,setuseridvalue,bt,context,link,link);

                if(flag==false){
                    apidatas.put("errmsg","消息发送失败");
                    apidatas.put("status", "false");
                }
            }

            apidatas.put("status", "true");

        }catch (Exception e) {
            e.printStackTrace();
            apidatas.put("status", false);
            apidatas.put("errormsg", "catch exception : " + e.getMessage());
          //  meslog.writLogsnotretrun(billid,e.getMessage(),"0",meslog.error);

        }
        return JSON.toJSONString(apidatas);
    }
}
