package com.customization.htsc.cus;

import com.cloudstore.dev.api.bean.MessageBean;
import com.cloudstore.dev.api.bean.MessageType;
import com.cloudstore.dev.api.util.Util_Message;
import com.engine.core.cfg.annotation.CommandDynamicProxy;
import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.workflow.cmd.requestForm.RequestSubmitCmd;
import com.engine.workflow.entity.requestForm.RequestOperationResultBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Author:wanxq
 * Date:2021/1/05 14:53
 * Description: 流程终止发送通知
 * Copyright:上海泛微网络科技股份有限公司
 */
@CommandDynamicProxy(target = RequestSubmitCmd.class, desc = "流程提交处理业务")
public class WfStopNoticeRequestSubCmd extends AbstractCommandProxy<Map<String, Object>> {
    @Override
    public Map<String, Object> execute(Command<Map<String, Object>> targetCommand) {
        RequestSubmitCmd requestSubmitCmd = (RequestSubmitCmd) targetCommand;
        HttpServletRequest request = requestSubmitCmd.getRequest();//从request中可以取到参数，不用以前的方法了
        String requestid = Util.null2String(request.getParameter("requestid"));
        String src = Util.null2String(request.getParameter("src"));
        String operator = Util.null2String(request.getParameter("f_weaver_belongto_userid"));
        //执行标准的业务处理
        Map<String, Object> result = nextExecute(targetCommand);
        RequestOperationResultBean resultBean = (RequestOperationResultBean) result.get("data");
        String executeResult = resultBean.getType().name();

        if("SUCCESS".equals(executeResult) && "dostopc".equals(src)){
            Set<String> userIdSet = new HashSet<String>(); // 消息接收人员集合
            RecordSet rs = new RecordSet();
            //流程参与人员
            rs.executeQuery(" select distinct userid from workflow_currentoperator where requestid=? ",new Object[]{requestid});
            while(rs.next()){
                String userid = Util.null2String(rs.getString("userid"));
                userIdSet.add(userid);
            }

            //发送消息
            try {

                rs.executeQuery(" select * from workflow_requestbase where requestid=? ",new Object[]{requestid});
                rs.next();
                String requestmark = Util.null2String(rs.getString("requestmark"));
                String requestName = Util.null2String(rs.getString("requestname"));

                ResourceComInfo resourceComInfo = new ResourceComInfo();
                String operatorName = resourceComInfo.getLastname(operator);

                PropBean propBean = new PropBean();
                int typecode = Util.getIntValue(propBean.getPropName("wfStopMsgTypeCode"),1303);
                MessageType messageType = MessageType.newInstance(typecode); // 消息来源

                String title = "流程终止提醒"; // 标题
                String context = "注意！您审核的 “"+requestName+"”（"+requestmark+"）已被"+operatorName+"终止，请知悉，您可点击查看详情";
                String linkUrl = "/spa/workflow/static4form/index.html#/main/workflow/req?requestid="+requestid+"&ismonitor=1"; // PC端链接
                String linkMobileUrl = "/spa/workflow/static4mobileform/index.html#/req?requestid="+requestid+"&ismonitor=1"; // 移动端链接

                MessageBean messageBean = Util_Message.createMessage(messageType, userIdSet, title, context, linkUrl, linkMobileUrl);
                messageBean.setCreater(1);// 创建人id
                Util_Message.store(messageBean);

            }catch (Exception e){
                e.printStackTrace();
            }

        }

        return result;
    }





    public static void main(String[] args) {

    }
}
