<%@ page import="weaver.general.*" %>
<%@page import="weaver.conn.RecordSet"%>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="com.cloudstore.dev.api.bean.MessageBean" %>
<%@ page import="com.cloudstore.dev.api.bean.MessageType" %>
<%@ page import="com.cloudstore.dev.api.util.Util_Message" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.util.*" %>
<%@ page import="com.engine.cube.service.ModeCardService" %>
<%@ page import="com.engine.common.util.ServiceUtil" %>
<%@ page import="com.engine.cube.service.impl.ModCardServiceImpl" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="sun.misc.BASE64Encoder" %>
<%@ page import="sun.misc.BASE64Decoder" %>
<%@ page import="com.api.doc.detail.util.DocDownloadCheckUtil" %>
<jsp:useBean id="ResourceComInfo" class="weaver.hrm.resource.ResourceComInfo" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean" scope="page" />
<%@ page language="java" contentType="text/html; charset=UTF-8" %>

<%
    RecordSet rs = new RecordSet();
    JSONObject jsonObject = new JSONObject();
    Map<String, Object> apidatas = new HashMap<String, Object>();
    User user = HrmUserVarify.getUser (request , response) ;
    String lastname = user.getLastname();

    String layoutid = "1117" ;
    int smstypeid_ = 537 ;

    String billid = request.getParameter("billid");
    String modeid = request.getParameter("modeid");
    String formid = request.getParameter("formid");
    String userid = request.getParameter("userid") ;
    String anniversary = request.getParameter("rzzn") ;

    bb.writeLog("userid3333:"+userid);

    String zfy = request.getParameter("zfy") ;
    String imagefileid = request.getParameter("imagefileid") ;
    String imagebillid = request.getParameter("imagebillid") ;
    String imageurl = "" ;
    if(StringUtils.isNotBlank(imagefileid)){
        String imagefileid_ = DocDownloadCheckUtil.checkPermission(imagefileid,null);
        if(StringUtils.isNotBlank(imagefileid_)){
            imageurl =  "/weaver/weaver.file.FileDownload?fileid="+imagefileid_;
        }
    }else {
        String tp = "";
        String imageid = "";
        String sql = " select tp from uf_rzzntp where id = " + imagebillid;
        rs.executeQuery(sql);
        if (rs.next()) {
            tp = Util.null2String(rs.getString("tp"));
        }

        if (StringUtils.isNotBlank(tp)) {
            sql = " select k.imagefileid \n" +
                    " from DocImageFile t \n" +
                    " inner join imagefile k on k.imagefileid = t.imagefileid \n" +
                    " where docid= " + tp;
            rs.executeQuery(sql);
            if (rs.next()) {
                imageid = Util.null2String(rs.getString("imagefileid"));
            }

            if (StringUtils.isNotBlank(imageid)) {
                String imagefileid_ = DocDownloadCheckUtil.checkPermission(imageid, null);
                if (StringUtils.isNotBlank(imagefileid_)) {
                    imageurl = "/weaver/weaver.file.FileDownload?fileid=" + imagefileid_;
                }
            }
        }
    }

    int smstypeid = Util.getIntValue(request.getParameter("smstypeid"),smstypeid_);

    String from = "reply";
    String src = "submit";
    String iscreate = "1" ;
    String isFormMode = "1";
    String username = "" ;
    if(StringUtils.isNotEmpty(userid)){
        username = ResourceComInfo.getLastname(userid);
    }

    if(StringUtils.isNotBlank(username) && StringUtils.isNotBlank(anniversary)){

//        String zfy = "亲爱的$userid$，入职$anniversary$周年纪念日快乐!";
//        zfy = zfy.replace("$userid$",username);
//        zfy = zfy.replace("$anniversary$",anniversary);

        BASE64Decoder decoder = new BASE64Decoder();
        if(StringUtils.isNotBlank(zfy)){
            zfy = new String(decoder.decodeBuffer(zfy),"UTF-8");
        }

        String contentHtml ="<p><img alt=\"\" src=\""+imageurl+"\" /></p>\n" +
                "<p>&nbsp;</p>\n" +
                "<p>"+zfy+"</p> ";

        BASE64Encoder encoder = new BASE64Encoder();
        String replycontent = encoder.encode(contentHtml.getBytes("UTF-8"));

        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("reqModeId",modeid);
        paramMap.put("reqBillid",billid);
        paramMap.put("reqFormid",formid);
        paramMap.put("isFormMode",isFormMode);
        paramMap.put("from",from);
        paramMap.put("src",src);
        paramMap.put("iscreate",iscreate);
        paramMap.put("replycontent",replycontent);
        paramMap.put("isEditOpt","0");

        Map<String,Object> dataMap = this.getService(user).replySubmit(paramMap,user);
        if(dataMap.isEmpty()){
            apidatas.put("status", "1");

            String linkUrl = "/spa/cube/index.html#/main/cube/card?type=0&modeId="+modeid+"&formId="+formid+"&billid="+billid+"&layoutid="+layoutid;
            String linkMobileUrl = "/spa/cube/index.html#/main/cube/card?type=0&modeId="+modeid+"&formId="+formid+"&billid="+billid+"&layoutid="+layoutid;
            bb.writeLog("linkUrl:"+linkUrl);
            String title = " ";
            String content = lastname+"祝您:</br>"+zfy;
            Set<String> userSet = new HashSet<String>();
            userSet.add(userid);
            sendMessage(smstypeid,userSet,title,content,linkUrl,linkMobileUrl);

            jsonObject.put("code","200");
            jsonObject.put("data",apidatas);
        }else{
            apidatas.put("status", "0");
            jsonObject.put("code","100");
        }
    }else{
        jsonObject.put("code","-100");
    }

    jsonObject.put("result",true);
%>
<%=jsonObject.toJSONString()  %>

<%!

    private ModeCardService getService(User user) {
        return (ModeCardService) ServiceUtil.getService(ModCardServiceImpl.class, user);
    }

    /***
     *
     * @param smstypeid
     * @param sendIdSet
     * @param title
     * @param context
     * @param linkPcUrl
     * @param linkMobileUrl
     * @return
     */
    public boolean sendMessage(int smstypeid, Set<String> sendIdSet, String title, String context, String linkPcUrl, String linkMobileUrl)
    {
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        boolean flag = false;
        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 flag;
        }

        MessageType messageType = MessageType.newInstance(smstypeid); // 消息来源（见文档第四点补充 必填）
        try {
            MessageBean messageBean = Util_Message.createMessage(messageType, sendIdSet, title, context, linkPcUrl, linkMobileUrl);
            messageBean.setCreater(1);// 创建人id
            messageBean.setDetailTitle("");
            flag = Util_Message.store(messageBean);

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


%>



