package com.weaver.seconddev.chapanda.feishu.esb;

import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.seconddev.chapanda.feishu.dao.WorkflowBotMessageDao;
import com.weaver.seconddev.chapanda.feishu.util.Esb2FeishuBotUpdateTodoUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Service("Esb2FeishuBotUpdateTodoAction")
public class Esb2FeishuBotUpdateTodoAction implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2FeishuBotUpdateTodoAction.class);

    @Autowired
    Esb2FeishuBotUpdateTodoUtil esb2FeishuBotUpdateTodoUtil;

    @Autowired
    WorkflowBotMessageDao workflowBotMessageDao;
    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {

        String requestid = String.valueOf(params.get("requestid"));
        String requestName = String.valueOf(params.get("requestName"));

        String nodeid = String.valueOf(params.get("nodeid"));
        String operatorid = String.valueOf(params.get("operatorid"));
        String msgType = String.valueOf(params.get("msgType"));

        log.error("requestName:{}",requestName);

        log.error("msgType:{}",msgType);
        log.error("operatorid:{}",operatorid);

        String update_message_id = "";
        String message_id = "";
        if("1".equals(msgType) || "5".equals(msgType)){
            String status = "PROCESSED";
            if(StringUtils.isNotBlank(operatorid)){
                message_id = workflowBotMessageDao.queryUserBotMessageId(requestid,nodeid,operatorid);
            }
            log.error("message_id:{}",message_id);
            if(StringUtils.isNotBlank(message_id)){
                update_message_id = esb2FeishuBotUpdateTodoUtil.updateBotTodoMessage(status,message_id);
            }else{
                log.error("todoOpenIdList:{}","获取待办人员为空");
            }
        }

        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("message","成功");
        actionMap.put("data",update_message_id);
        actionMap.put("messageid",message_id);
        actionMap.put("updatemessageid",update_message_id);

        return WeaResult.success(actionMap);
    }
}
