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.util.*;
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("Esb2FeishuCalendarsCancelAction")
public class Esb2FeishuCalendarsCancelAction implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2FeishuCalendarsCancelAction.class);

    @Autowired
    Esb2FeishuCancelCalendarUtil esb2FeishuCancelCalendarUtil;

    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {
        String calendar_id = (String) params.get("calendar_id");
        String event_id = (String) params.get("event_id");
        String dataid = (String) params.get("dataid");
        String datacode = (String) params.get("datacode");

        log.error("calendar_id:{}",calendar_id);
        log.error("event_id:{}",event_id);

        if("0".equals(datacode)){
            return WeaResult.fail(500,"飞书日程已删除");
        }

        if(StringUtils.isBlank(calendar_id)){
            return WeaResult.fail(500,"获取飞书的日历信息失败");
        }

        if(StringUtils.isBlank(event_id)){
            return WeaResult.fail(500,"获取飞书的日程信息失败");
        }

        String code = "";
        if(StringUtils.isNotBlank(calendar_id) && StringUtils.isNotBlank(event_id)){
            code = esb2FeishuCancelCalendarUtil.cancelCalendarEvents(calendar_id,event_id);
        }
        log.error("code:{}",code);
        if(!"0".equals(code)){
            return WeaResult.fail(500,"飞书日程取消失败");
        }

        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("msg","成功");
        actionMap.put("calendarId",calendar_id);
        actionMap.put("eventId",event_id);
        actionMap.put("datacode",code);
        actionMap.put("dataid",dataid);
        return WeaResult.success(actionMap);
    }
}
