package com.xdap.self_development.controller;

import com.definesys.mpaas.common.http.Response;
import com.xdap.api.moudle.user.vo.LoginUserVo;
import com.xdap.runtime.service.RuntimeAppContextService;
import com.xdap.runtime.service.RuntimeUserService;
import com.xdap.self_development.controller.form.OptionForm;
import com.xdap.self_development.domain.pojo.ParameterPojo;
import com.xdap.self_development.domain.pojo.TemplatePojo;
import com.xdap.self_development.service.DataEntryService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据录入控制器
 * 管理模板列表，提供筛选条件、列表显示、添加模板、导入模板、查看、下载、版本、修改等功能
 *
 * @version 1.0
 * @since 2025
 */
@RestController
@RequestMapping("/custom/dataEntry")
public class DataEntryController {
    @Resource
    private DataEntryService dataEntryService;
    @Resource
    private RuntimeAppContextService runtimeAppContextService;
    @Resource
    private RuntimeUserService runtimeUserService;

    /**
     * 测试接口
     *
     * @return 当前应用上下文信息
     */
    @GetMapping("/test")
    public Response getTest() {
        Map<String, String> map = new HashMap<>();
        String currentAppId = runtimeAppContextService.getCurrentAppId();
        String currentTenantId = runtimeAppContextService.getCurrentTenantId();
        String currentToken = runtimeAppContextService.getCurrentToken();
        String currentUserId = runtimeAppContextService.getCurrentUserId();
        LoginUserVo loginUserVo = runtimeUserService.queryLoginUserVo(currentUserId, currentTenantId, currentAppId);

        map.put("currentAppId", currentAppId);
        map.put("currentTenantId", currentTenantId);
        map.put("currentToken", currentToken);
        map.put("currentUserId", currentUserId);
        map.put("loginUserVo", loginUserVo.toString());
        return Response.ok().data(map);
    }

    /**
     * 根据条件筛选模板（模板的模糊搜索）
     *
     * @param templateName 模板名称
     * @return 模板列表
     */
    @GetMapping("/getTemplateByCondition")
    public Response getTemplateByCondition(@RequestParam("templateName") String templateName) {
        List<TemplatePojo> templates = dataEntryService.getTemplateByCondition(templateName);
        return Response.ok().data(templates);
    }

    /**
     * 根据条件筛选参数（参数的模糊搜索）
     *
     * @param form 筛选条件表单
     * @return 参数列表
     */
    @GetMapping("/getParameterByCondition")
    public Response getParameterByCondition(OptionForm form) {
        List<ParameterPojo> parameters = dataEntryService.getParameterByCondition(form);
        return Response.ok().data(parameters);
    }
}
