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

import com.alibaba.fastjson.JSONObject;
import com.weaver.common.authority.annotation.WeaPermission;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.common.security.json.JSON;
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.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: calyrex
 * @CreateTime: 2025-03-06
 * @Description: ISO的IP获取
 */


@Slf4j
@RestController
@RequestMapping({"/papi/secondev/workflow/controller" })
public class GetISOIP_controller {

    @Autowired
    private DataSetService dataSetService;
    @GetMapping("/getisoip")
    @ResponseBody
    @WeaPermission(publicPermission = true)
    public WeaResult<String> run(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("application/x-www-form-urlencoded; charset=utf-8");
        log.error("HTGD_IT13_GETIP_CONTROLLER"+ JSON.parseObjectToString(request));
        String userid = null2String(request.getParameter("userid"));
        String remoteAddr = request.getRemoteAddr();
        Map<String,Object> data = new HashMap<>();
        data.put("ip",remoteAddr);
        String sql = "select * from eteams.dbo.employee where id = " + userid;
        Map<String, Object> stringObjectMap = executeForQuery(String.valueOf(SourceType.LOGIC), "weaver-hr-service", sql);
        if(stringObjectMap.get("status").equals("OK")){
            List<Map<String,Object>> records = (List<Map<String,Object>>)stringObjectMap.get("records");
            if (records.size()>0){
                Map<String, Object> map = records.get(0);
                data.put("username",map.get("USERNAME"));
                data.put("job_num",map.get("JOB_NUM"));
            }
        }
        return WeaResult.success(JSONObject.toJSONString(data));
    }

    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;
    }
}
