package com.ruoyi.interfaces.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import groovyjarjarpicocli.CommandLine;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
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.MdDataSetParam;
import com.ruoyi.interfaces.service.IMdDataSetParamService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据集参数Controller
 * 
 * @author 朱得糠
 * @date 2025-09-03
 */
@RestController
@RequestMapping("/data/set/param")
public class MdDataSetParamController extends BaseController
{
    @Autowired
    private IMdDataSetParamService mdDataSetParamService;

    /**
     * 查询数据集参数列表
     */
    @GetMapping("/list")
    public AjaxResult list(MdDataSetParam mdDataSetParam)
    {
        List<MdDataSetParam> list = mdDataSetParamService.selectMdDataSetParamList(mdDataSetParam);
        return success(list);
    }

    /**
     * 导出数据集参数列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, MdDataSetParam mdDataSetParam)
    {
        List<MdDataSetParam> list = mdDataSetParamService.selectMdDataSetParamList(mdDataSetParam);
        ExcelUtil<MdDataSetParam> util = new ExcelUtil<MdDataSetParam>(MdDataSetParam.class);
        util.exportExcel(response, list, "数据集参数数据");
    }

    /**
     * 获取数据集参数详细信息
     */
    @GetMapping(value = "/{dcCode}")
    public AjaxResult getInfo(@PathVariable("dcCode") String dcCode)
    {
        return success(mdDataSetParamService.selectMdDataSetParamByDcCode(dcCode));
    }

    /**
     * 新增数据集参数
     */
    @PostMapping
    public AjaxResult add(@RequestBody MdDataSetParam mdDataSetParam)
    {
        return toAjax(mdDataSetParamService.insertMdDataSetParam(mdDataSetParam));
    }

    @PostMapping("/addList")
    public AjaxResult addList(@RequestBody List<MdDataSetParam> list)
    {
        List<MdDataSetParam> collect = list.stream().
                filter(p -> StringUtils.isNotNull(p.getParamCode()))
                .filter(p -> StringUtils.isNotNull(p.getDcCode()))
                .filter(p -> StringUtils.isNotNull(p.getParamType())).collect(Collectors.toList());
        if (StringUtils.isEmpty(list)) return error();
        mdDataSetParamService.deleteMdDataSetParamByDcCode(list.get(0).getDcCode());
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSort(i);
        }
        list.forEach(mdDataSetParamService::insertMdDataSetParam);
        return success();
    }
    /**
     * 修改数据集参数
     */
    @PutMapping
    public AjaxResult edit(@RequestBody MdDataSetParam mdDataSetParam)
    {
        return toAjax(mdDataSetParamService.updateMdDataSetParam(mdDataSetParam));
    }

    /**
     * 删除数据集参数
     */
	@DeleteMapping("/{dcCodes}")
    public AjaxResult remove(@PathVariable String[] dcCodes)
    {
        return toAjax(mdDataSetParamService.deleteMdDataSetParamByDcCodes(dcCodes));
    }
}
