package weaver.interfaces.htsc.workflow;

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 weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by K1810006 on 2020/9/17.
 */
public class LeaderNoticeAction extends BaseBean implements Action{


    @Override
    public String execute(RequestInfo requestInfo) {
        User user = requestInfo.getRequestManager().getUser();
        int deptid = user.getUserDepartment();
        String securityLevel = user.getSeclevel();

        RecordSet rs = new RecordSet();
        rs.executeQuery(" select * from uf_tzpz where bm=? and aqjb>=? ", new Object[]{deptid,securityLevel});
        if(rs.next()){
            String requestid = requestInfo.getRequestid();
            String workflowid = requestInfo.getWorkflowid();
            String operatorName = user.getUsername();
            int operator = user.getUID();
            String requestName = requestInfo.getRequestManager().getRequestname();
            String operatetime = TimeUtil.getCurrentTimeString();
            String requestmark = getRequestMark(requestid);



            String remark = Util.HTMLtoTxt(requestInfo.getRequestManager().getRemark()).trim();
            writeLog("notice remark2==="+remark);

            if(!"同意".equals(remark) && !"不同意".equals(remark)){
                Set<String> userIdList = new HashSet<>(); // 接收人id 必填

                String sql =  " select * from workflow_monitor_info where id in ( " +
                        "  select distinct infoid from workflow_monitor_detail where workflowid=?  )";
                rs.executeQuery(sql,workflowid);
                while(rs.next()){
                    String jktype = Util.null2String(rs.getString("jktype"));
                    String jkvalue = Util.null2String(rs.getString("jkvalue"));
                    if("1".equals(jktype) || "3".equals(jktype) ){//人力资源和系统管理员类型
                        userIdList.add(jkvalue);
                    }else{//角色类型
                        userIdList = getRoleMembers(userIdList,jkvalue);
                    }
                }
                MessageType messageType = MessageType.newInstance(1169); // 消息来源（见文档第四点补充 必填）
                String title = "领导审阅意见通知"; // 标题
                String context = operatorName+"于"+operatetime+"对“"+requestName+"”（"+requestmark+"）" +
                        "签署意见“"+remark+"”，请知悉。"; // 内容
                context = context.replaceAll("[\\s\\t\\n\\r]", "");
                writeLog("notice context==="+context);
                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"; // 移动端链接
                try {
                    MessageBean messageBean = Util_Message.createMessage(messageType, userIdList, title, context, linkUrl, linkMobileUrl);
                    messageBean.setCreater(1);// 创建人id
                    Util_Message.store(messageBean);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Action.SUCCESS;
    }

    /**
     * 添加流程监控角色中的人员
     * @param set
     * @param roleid
     * @return
     */
    public Set<String> getRoleMembers(Set<String> set,String roleid){
        RecordSet rs = new RecordSet();
        rs.executeQuery(" select * from hrmrolemembers where roleid=? ");
        while(rs.next()){
            String resourceid = Util.null2String(rs.getString("resourceid"));
            set.add(resourceid);
        }
        return set;
    }

    /**
     * 查询流程编号
     * @param requestid
     * @return
     */
    public String getRequestMark(String requestid){
        String requestmark = "";
        RecordSet rs = new RecordSet();
        rs.executeQuery(" select requestmark from workflow_requestbase where requestid=? ", new Object[]{requestid});
        if(rs.next()){
            requestmark = Util.null2String(rs.getString("requestmark"));
        }
        return requestmark;
    }


}
