package com.ruoyi.interfaces.controller;

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

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.interfaces.domain.MdDataField;
import com.ruoyi.interfaces.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.interfaces.domain.MdDataSet;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据集Controller
 *
 * @author 朱得糠
 * @date 2025-07-30
 */
@RestController
@RequestMapping("/data/set")
public class MdDataSetController extends BaseController {
    @Autowired
    private IMdDataSetService mdDataSetService;
    @Autowired
    private IMdDataFieldService mdDataFieldService;


    /**
     * 查询数据集列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MdDataSet mdDataSet) {
        startPage();
        List<MdDataSet> list = mdDataSetService.selectMdDataSetList(mdDataSet);
        return getDataTable(list);
    }

    /**
     * 导出数据集列表
     */
    @Log(title = "数据集", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MdDataSet mdDataSet) {
        List<MdDataSet> list = mdDataSetService.selectMdDataSetList(mdDataSet);
        ExcelUtil<MdDataSet> util = new ExcelUtil<MdDataSet>(MdDataSet.class);
        util.exportExcel(response, list, "数据集数据");
    }

    /**
     * 获取数据集详细信息
     */
    @GetMapping(value = "/{dsCode}")
    public AjaxResult getInfo(@PathVariable("dsCode") String dsCode) {
        HashMap<String, Object> map = new HashMap<>();
        MdDataSet mdDataSet = mdDataSetService.selectMdDataSetByDsCode(dsCode);
        map.put("dataSet", mdDataSet);
        if (StringUtils.isNull(mdDataSet)) return error();
        List<MdDataField> fieldList = mdDataFieldService.selectMdDataFieldByDsCode(mdDataSet.getDcCode());
        map.put("fieldList", fieldList);
        return success(map);
    }

    /**
     * 新增数据集
     */
    @Log(title = "数据集", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MdDataSet mdDataSet) {
        return toAjax(mdDataSetService.insertMdDataSet(mdDataSet));
    }

    /**
     * 修改数据集
     */
    @Log(title = "数据集", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MdDataSet mdDataSet) {
        return toAjax(mdDataSetService.updateMdDataSet(mdDataSet));
    }

    /**
     * 删除数据集
     */
    @Log(title = "数据集", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dsCodes}")
    public AjaxResult remove(@PathVariable String[] dsCodes) {
        return toAjax(mdDataSetService.deleteMdDataSetByDsCodes(dsCodes));
    }

    @Autowired
    private IMdDataSetApiService setApiService;
    @Autowired
    private IMdDataSetFileService setFileService;
    @Autowired
    private IMdDataSetSqlService setSqlService;

    @GetMapping("/getSetConfig")
    public AjaxResult getSetConfig(MdDataSet dataSet) {

        if (StringUtils.isNull(dataSet.getDcCode())) return error("数据集不可为空");
        if (StringUtils.isNull(dataSet.getDcType())) return error("请配置数据集类型");
        switch (dataSet.getDcType()) {
            case "sql":
                return success(setSqlService.selectMdDataSetSqlByDcCode(dataSet.getDcCode()));
            case "api":
                return success(setApiService.selectMdDataSetApiByDcCode(dataSet.getDcCode()));
            case "file":
                return success(setFileService.selectMdDataSetFileByDcCode(dataSet.getDcCode()));
        }

        return error("数据集类型异常");
    }

}


