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

/**
 * 元数据字段Controller
 *
 * @author 朱得糠
 * @date 2025-08-02
 */
@RestController
@RequestMapping("/meta/field")
public class SysMetaFieldController extends BaseController {
    @Autowired
    private ISysMetaFieldService sysMetaFieldService;

    /**
     * 查询元数据字段列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysMetaField sysMetaField) {
        startPage();
        List<SysMetaField> list = sysMetaFieldService.selectSysMetaFieldList(sysMetaField);
        return getDataTable(list);
    }

    /**
     * 导出元数据字段列表
     */
    @Log(title = "元数据字段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMetaField sysMetaField) {
        List<SysMetaField> list = sysMetaFieldService.selectSysMetaFieldList(sysMetaField);
        ExcelUtil<SysMetaField> util = new ExcelUtil<SysMetaField>(SysMetaField.class);
        util.exportExcel(response, list, "元数据字段数据");
    }

    /**
     * 获取元数据字段列表
     */
    @GetMapping("/allList")
    public AjaxResult getInfo(SysMetaField sysMetaField) {
        return success(sysMetaFieldService.selectSysMetaFieldList(sysMetaField));
    }

    /**
     * 新增元数据字段
     */
    @Log(title = "元数据字段", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMetaField sysMetaField) {
        return toAjax(sysMetaFieldService.insertSysMetaField(sysMetaField));
    }

    /**
     * 新增元数据字段
     */
    @Log(title = "元数据字段", businessType = BusinessType.INSERT)
    @PostMapping("/addList")
    public AjaxResult addList(@RequestBody List<SysMetaField> sysMetaFields) {
        sysMetaFields = sysMetaFields.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(sysMetaFields)) return error("数据为空");
        sysMetaFieldService.deleteSysMetaFieldAll(sysMetaFields.get(0));
        sysMetaFields.forEach(sysMetaFieldService::insertSysMetaField);
        return success("插入成功");
    }

    /**
     * 修改元数据字段
     */
    @Log(title = "元数据字段", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMetaField sysMetaField) {
        return toAjax(sysMetaFieldService.updateSysMetaField(sysMetaField));
    }

    /**
     * 删除元数据字段
     */
    @Log(title = "元数据字段", businessType = BusinessType.DELETE)
    @DeleteMapping
    public AjaxResult remove(SysMetaField sysMetaField) {
        return toAjax(sysMetaFieldService.deleteSysMetaField(sysMetaField));
    }
}
