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

import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.common.hrm.util.HrmCommonUtil;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.seconddev.chapanda.feishu.util.*;
import com.weaver.teams.domain.user.SimpleEmployee;
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("Esb2FeishuCreateCalendarsTimeoffAction")
public class Esb2FeishuCreateCalendarsTimeoffAction implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2FeishuCreateCalendarsTimeoffAction.class);

    @Autowired
    Esb2FeishuCreateCalendarTimeoffUtil esb2FeishuCreateCalendarTimeoffUtil;

    @Autowired
    private HrmCommonUtil hrmCommonUtil;

    @Autowired
    Esb2FeishuOpenIdUtil esb2FeishuOpenIdUtil;

    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {
        String title = (String) params.get("title");
        String description = (String) params.get("description");
        String start_date = (String) params.get("start_date");
        String end_date = (String) params.get("end_date");
        String employee_id = (String) params.get("employee_id");
        String type = (String) params.get("type");
        String timeoffEventId = (String) params.get("timeoffEventId");
        String dataid = (String) params.get("dataid");

        if(StringUtils.isNotBlank(timeoffEventId)){
            return WeaResult.fail(500,"请假日程已创建，无法新建请假日程");
        }

        log.error("start_date:{}",start_date);
        log.error("end_date:{}",end_date);

//        String mobile = "";
//        if(StringUtils.isNotBlank(employee_id)){
//            Long employeeId = Long.valueOf(employee_id);
//            SimpleEmployee simpleEmployee = hrmCommonUtil.getSimpleEmployee(employeeId);
//            mobile = simpleEmployee.getMobile();
//        }
//
//        if(StringUtils.isBlank(mobile)){
//            return WeaResult.fail(500,"获取手机号为空");
//        }

        String openId = esb2FeishuOpenIdUtil.queryFeishuOpenId(employee_id);
//        if(StringUtils.isNotBlank(mobile)){
//            Map<String, Object> userMap = esb2FeishuOpenIdUtil.queryFeishuOpenIdByMobiles(mobile);
//            if(userMap.containsKey(mobile)){
//                openId = userMap.get(mobile).toString();
//            }
//        }

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

        String timeoff_event_id = "";
        if(StringUtils.isNotBlank(openId)){
            timeoff_event_id = esb2FeishuCreateCalendarTimeoffUtil.createCalendarTimeOff(openId,title,description,start_date,end_date,type);
        }
        log.error("timeoff_event_id:{}",timeoff_event_id);

        if(StringUtils.isBlank(timeoff_event_id)){
            return WeaResult.fail(500,"请假日历创建失败");
        }

        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("msg","成功");
        actionMap.put("timeoffEventId",timeoff_event_id);
        actionMap.put("openId",openId);
        actionMap.put("dataid",dataid);
        return WeaResult.success(actionMap);
    }
}
