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.Esb2FeishuHrmAvatarUtil;
import com.weaver.seconddev.chapanda.feishu.util.Esb2FeishuOpenIdUtil;
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("Esb2FeishuHrmAvatarAction")
public class Esb2FeishuHrmAvatarAction implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2FeishuHrmAvatarAction.class);

    @Autowired
    Esb2FeishuHrmAvatarUtil esb2FeishuHrmAvatarUtil;

    @Autowired
    Esb2FeishuOpenIdUtil esb2FeishuOpenIdUtil;

    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {

        String employeeid = String.valueOf(params.get("employeeid"));
        Long avatar = Long.valueOf(String.valueOf(params.get("avatar")));
        if(StringUtils.isBlank(employeeid)){
            return WeaResult.fail(500,"获取人员信息失败");
        }
        if(StringUtils.isBlank(String.valueOf(avatar))){
            return WeaResult.fail(500,"获取人员头像信息失败");
        }
        String openId = esb2FeishuOpenIdUtil.queryFeishuOpenId(employeeid);
        if(StringUtils.isBlank(openId)){
            return WeaResult.fail(500,"获取人员飞书信息失败");
        }

        Map<String,Object>  avatarMap = esb2FeishuHrmAvatarUtil.syncHrmAvatarByEmployee(openId,avatar);
        String avatarOrigin = String.valueOf(avatarMap.get("avatarOrigin"));
        String avatarKey = String.valueOf(avatarMap.get("avatarKey"));
        if(StringUtils.isBlank(avatarOrigin)){
            return WeaResult.fail(500,"更新飞书头像异常");
        }

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

}
