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

/**
 * 数据集字段Controller
 * 
 * @author 朱得糠
 * @date 2025-09-04
 */
@RestController
@RequestMapping("/data/field")
public class MdDataFieldController extends BaseController
{
    @Autowired
    private IMdDataFieldService mdDataFieldService;

    /**
     * 查询数据集字段列表
     */
    @GetMapping("/list")
    public AjaxResult list(MdDataField mdDataField)
    {
        if (StringUtils.isNull(mdDataField.getDcCode())) return error("数据集id不可为空");
        List<MdDataField> list = mdDataFieldService.selectMdDataFieldList(mdDataField);
        return 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 = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mdDataFieldService.selectMdDataFieldById(id));
    }

    /**
     * 新增数据集字段
     */
    @PostMapping
    public AjaxResult add(@RequestBody MdDataField mdDataField)
    {
        return toAjax(mdDataFieldService.insertMdDataField(mdDataField));
    }


    @PostMapping("/addList")
    public AjaxResult addList(@RequestBody List<MdDataField> list) {
        List<MdDataField> collect = list.stream().filter(p -> StringUtils.isNotNull(p.getId()))
                .filter(p -> StringUtils.isNotNull(p.getParentId()))
                .filter(p -> StringUtils.isNotNull(p.getDcCode()))
                .filter(p -> StringUtils.isNotNull(p.getFieldType())).collect(Collectors.toList());
        if (StringUtils.isEmpty(collect)) return error("数据为空");

        mdDataFieldService.deleteMdDataFieldByDcCode(collect.get(0).getDcCode());

        for (int i = 0; i < collect.size(); i++) {
            collect.get(i).setFieldOrder(i);
        }

        collect.stream().forEach(mdDataFieldService::insertMdDataField);
        return success();
    }

    /**
     * 修改数据集字段
     */
    @PutMapping
    public AjaxResult edit(@RequestBody MdDataField mdDataField)
    {
        return toAjax(mdDataFieldService.updateMdDataField(mdDataField));
    }

    /**
     * 删除数据集字段
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mdDataFieldService.deleteMdDataFieldByIds(ids));
    }
}
