package com.weaver.seconddev.chapanda.beisen.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.beisen.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("Esb2BeisenCreateBlackListAction")
public class Esb2BeisenCreateBlackListAction implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2BeisenCreateBlackListAction.class);

    @Autowired
    BlackList2BeiSenUtil blackList2BeiSenUtil;

    @Autowired
    Token2BeiSenUtil token2BeiSenUtil ;

    @Autowired
    private HrmCommonUtil hrmCommonUtil;

//    @Autowired
//    BeisenStaffInfoByStaffCodeUtil beisenStaffInfoByStaffCodeUtil;


    @Autowired
    BeisenQueryUseridUtil beisenQueryUseridUtil;

    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {
        log.error("EsbRequestSubmitAction start");
//        String beisenUserId = (String) params.get("beisenuserid");
        Long userId = 0L;
        if(params.containsKey("userid")){
            String userIdStr = String.valueOf(params.get("userid"));
            log.error("userIdStr:{}",userIdStr);
            if(StringUtils.isNotBlank(userIdStr)){
                userId = Long.valueOf(userIdStr);
            }
        }

        String lastWorkDate = (String) params.get("lastworkdate");
        String blackAddReason = (String) params.get("blackaddreason");
        String blackStaffDesc = (String) params.get("blackstaffdesc");
        String name = (String) params.get("name");
        String staffemail = (String) params.get("staffemail");

        log.error("userId:{}",userId);
        log.error("lastWorkDate:{}",lastWorkDate);
        log.error("blackAddReason:{}",blackAddReason);
        log.error("blackStaffDesc:{}",blackStaffDesc);
        log.error("name:{}",name);
        log.error("staffemail:{}",staffemail);
        int beisenUserId = -1;
        String jobNum = "";
        if(userId.longValue() >0){
            SimpleEmployee simpleEmployee = hrmCommonUtil.getSimpleEmployee(userId);
            jobNum = simpleEmployee.getJobNum();
        }

        log.error("jobNum:{}",jobNum);
        if(StringUtils.isNotBlank(jobNum)){

            String beisenUser = beisenQueryUseridUtil.queryBeisenUserIdByJobNum(jobNum);
            if(StringUtils.isNotBlank(beisenUser)){
                beisenUserId = Integer.valueOf(beisenUser);
            }
//            BeisenStaffDto beisenStaffDto = beisenStaffInfoByStaffCodeUtil.getBeisenStaffInfoByStaffCode(jobNum);
        }
        log.error("beisenUserId:{}",beisenUserId);

//        String tokenUrl = "https://openapi.italent.cn/token";
//        String beishenHost = "https://openapi.italent.cn";
//        String createBlackListUrl = "/TenantBaseExternal/api/v5/BlackList/Create";
//        String app_key = "0F0F2B3F6356464EB59FB651E8DAACBA";
//        String app_secret = "AF4A2BFC6BB741FD9D80B7F56DA07F08F703AC14B40C453E8DA5BCABAF43A003";

        String token = token2BeiSenUtil.getToken();
        log.error("token:{}",token);
        String objectId = "";
        if(StringUtils.isNotBlank(token)){
            if(beisenUserId > 0){
                objectId = blackList2BeiSenUtil.createBlackList4ResignUser(token,String.valueOf(beisenUserId),lastWorkDate,blackAddReason,blackStaffDesc);
            }else{
                objectId = blackList2BeiSenUtil.createBlackList4NonSystem(token,name,staffemail,blackAddReason,blackStaffDesc);
            }
        }

        log.error("objectId:{}",objectId);
        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("msg","Esb2BeisenCreateBlackListAction");
        actionMap.put("objectId",objectId);
        return WeaResult.success(actionMap);
    }
}
