package weaver.interfaces.message;

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.common.util.ServiceUtil;
import com.engine.workflow.publicApi.WorkflowRequestTodoPA;
import com.engine.workflow.publicApi.impl.WorkflowRequestListPAImpl;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.hrm.User;
import weaver.interfaces.schedule.BaseCronJob;
import java.io.IOException;
import java.util.*;

public class SendTodoDataCron extends BaseCronJob {

    public String code ;
    String userid ;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    private WorkflowRequestTodoPA getWorkflowTodoPA(){
        return (WorkflowRequestListPAImpl) ServiceUtil.getService(WorkflowRequestListPAImpl.class);
    }

    @Override
    public void execute() {
        BaseBean bb = new BaseBean();
        WorkflowRequestTodoPA requestTodoPA = getWorkflowTodoPA();

        List<String> list = new ArrayList<String>();
        RecordSet rs = new RecordSet();
        String where = "";
        if(StringUtils.isNotBlank(userid)){
            String userids = "'"+userid.replace(",", "','")+"'" ;
            where = " and id in("+userids+") ";
        }
        String sql = " select id from hrmresource where status in(0,1,2,3,4) " + where ;
        bb.writeLog("sql:"+sql);
        rs.execute(sql);
        while (rs.next()){
            String userid = rs.getString("id");
            list.add(userid);
        }
        bb.writeLog("list:"+list.size());
        if(StringUtils.isNotBlank(code)){
            int createrId = 1;
            MessageType messageType = MessageType.newInstance(Integer.valueOf(code)); // 消息来源（见文档第四点补充 必填）
            for (String userid : list){
                User user = new User(Integer.valueOf(userid));
                Map<String,String> conditions = new HashMap<String, String>();
//                conditions.put("doingStatus","5");

                long toDoWorkflowRequestCount = requestTodoPA.getDoingWorkflowRequestCount(user, conditions, false, false);

                if(toDoWorkflowRequestCount > 0){

                    Set<String> userIdList = new HashSet<>(); // 接收人id 必填
                    userIdList.add(userid);
                    String title = "审批提醒"; // 标题
                    String context = "截至到目前你共有"+toDoWorkflowRequestCount+"条审批，请及时处理"; // 内容
                    String linkUrl = "/spa/workflow/static/index.html#/main/workflow/listDoing"; // PC端链接
                    String linkMobileUrl = "/spa/workflow/static4mobile/index.html#/center/doing?moduleid=1"; // 移动端链接
                    try {
                        MessageBean messageBean = Util_Message.createMessage(messageType, userIdList, title, context, linkUrl, linkMobileUrl);
                        messageBean.setCreater(createrId);// 创建人id
                        Util_Message.store(messageBean);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
