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 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.MdDataField;
import com.ruoyi.interfaces.service.IMdDataFieldService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据集字段Controller
 * 
 * @author ruoyi
 * @date 2025-07-30
 */
@RestController
@RequestMapping("/data/field")
public class MdDataFieldController extends BaseController
{
    @Autowired
    private IMdDataFieldService mdDataFieldService;

    /**
     * 查询数据集字段列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MdDataField mdDataField)
    {
        startPage();
        List<MdDataField> list = mdDataFieldService.selectMdDataFieldList(mdDataField);
        return getDataTable(list);
    }
    @GetMapping("/list/{dsCode}")
    public AjaxResult listByDsCode(MdDataField mdDataField)
    {
        List<MdDataField> list = mdDataFieldService.selectMdDataFieldList(mdDataField);
        return AjaxResult.success(list);
    }
    /**
     * 导出数据集字段列表
     */
    @Log(title = "数据集字段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MdDataField mdDataField)
    {
        List<MdDataField> list = mdDataFieldService.selectMdDataFieldList(mdDataField);
        ExcelUtil<MdDataField> util = new ExcelUtil<MdDataField>(MdDataField.class);
        util.exportExcel(response, list, "数据集字段数据");
    }

    /**
     * 获取数据集字段详细信息
     */
    @GetMapping(value = "/{dsCode}")
    public AjaxResult getInfo(@PathVariable("dsCode") String dsCode)
    {
        return success(mdDataFieldService.selectMdDataFieldByDsCode(dsCode));
    }

    /**
     * 新增数据集字段
     */
    @Log(title = "数据集字段", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MdDataField mdDataField)
    {
        return toAjax(mdDataFieldService.insertMdDataField(mdDataField));
    }

    /**
     * 批量新增数据集字段
     */
    @Log(title = "数据集字段", businessType = BusinessType.INSERT)
    @PostMapping("/addList")
    public AjaxResult addList(@RequestBody List<MdDataField> mdDataFields)
    {
        List<MdDataField> collect = mdDataFields.stream()
                .filter(item -> StringUtils.isNotNull(item.getDsCode()))
                .filter(item -> StringUtils.isNotNull(item.getMetaTable()))
                .filter(item -> StringUtils.isNotNull(item.getFieldCode())).collect(Collectors.toList());
        if (StringUtils.isEmpty(collect)) {
            return AjaxResult.error();
        }
        mdDataFieldService.deleteMdDataFieldByDsCode(collect.get(0).getDsCode());
        collect.forEach(mdDataFieldService::insertMdDataField);
        return AjaxResult.success();
    }


    /**
     * 修改数据集字段
     */
    @Log(title = "数据集字段", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MdDataField mdDataField)
    {
        return toAjax(mdDataFieldService.updateMdDataField(mdDataField));
    }

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

    /**
     * 删除数据集字段
     */
    @Log(title = "数据集字段", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeOne")
    public AjaxResult removeOne(MdDataField mdDataField)
    {
        return toAjax(mdDataFieldService.deleteMdDataFieldOne(mdDataField));
    }
}
