package com.weaver.seconddev.interfaces.workflow.controller;

import cn.hutool.json.JSONObject;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.ebuilder.common.util.TenantContext;
import com.weaver.ebuilder.datasource.api.entity.ExecuteSqlEntity;
import com.weaver.ebuilder.datasource.api.enums.SourceType;
import com.weaver.ebuilder.datasource.api.service.DataSetService;
import com.weaver.framework.rpc.context.impl.TenantRpcContext;
import com.weaver.teams.api.tenant.Tenant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Author: calyrex
 * @CreateTime: 2025-03-04
 * @Description: 获取人员信息
 */
@Slf4j
@RestController
@RequestMapping({"/papi/secondev/workflow/controller" })
public class GetUserInfo_controller {

    @Autowired
    private DataSetService dataSetService;
    @RequestMapping("/getUserInfo")
    public WeaResult<JSONObject> run(@RequestParam Map<String,Object> params) {
        log.error("GetUserInfo_controller execute start");
        log.error("GetUserInfo_controller execute params : " + params);
        JSONObject json = new JSONObject();
        try {
            String id = null2String(params.get("id"));
            String sql = "select * from eteams.dbo.employee where id = " + id;
            log.error("GetUserInfo_controller sql--" + sql);
            String sourceType = null2String(SourceType.LOGIC);
            String groupId = "weaver-hr-service";
            Map<String, Object> datas = executeForQuery(sourceType, groupId, sql);
            if(String.valueOf(datas.get("status")).equals("OK")){
                List<Map<String,Object>> records = (List<Map<String,Object>>)datas.get("records");
                if (records.size()>0){
                    Map<String, Object> map = records.get(0);
                    json.put("username",map.get("USERNAME"));
                    return WeaResult.success(json);
                }else {
                    log.error("getCustomId 相关字段未找到");
                }
            }else {
                log.error("getUserId status-->"+ datas.get("status"));
            }
        }catch (Exception e){
            log.error("GetUserInfo_controller execute error : " ,e);
            return WeaResult.fail(500, "getgsjc_controller execute error", e);
        }
        return WeaResult.success(json);
    }

    public Map<String, Object> executeForQuery(String sourceType, String groupId, String sql) {
//        log.error("executeForQuery sourceType-->" + sourceType + ",groupId-->" + groupId + ",sql-->" + sql);
        final Tenant currentTenant = TenantContext.getCurrentTenant();
        TenantRpcContext.setTargetTenantKey(String.valueOf(currentTenant));
        ExecuteSqlEntity executeSqlEntity = new ExecuteSqlEntity();
        executeSqlEntity.setSql(cn.hutool.core.codec.Base64.encode(sql));
        executeSqlEntity.setGroupId(groupId); //groupid,可以访问  E10地址/api/datasource/ds/group?sourceType=LOGIC 获取
        executeSqlEntity.setSourceType(SourceType.valueOf(sourceType));
//        log.error("executeForQuery executeSqlEntity-->"+executeSqlEntity.getSql()+"-->"+executeSqlEntity.getGroupId()+"-->"+executeSqlEntity.getSourceType());
        Map<String, Object> datas = dataSetService.executeSql(executeSqlEntity);
        log.error("executeForQuery datas-->" + datas);
        TenantRpcContext.removeTargetTenantKey();
        return datas;
    }

    public static String null2String(String s){
        return s == null ? "" : s;
    }

    public static String null2String(Object o){
        return o == null ? "" : o.toString();
    }

    public static String null2String(String s1,String s2){
        return s1 == null ? (s2 == null ? "" : s2) : s1;
    }
}
