package com.ruoyi.interfaces.controller;

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

import com.ruoyi.interfaces.domain.MdDataCate;
import com.ruoyi.interfaces.domain.MdDataSet;
import com.ruoyi.interfaces.domain.PtTreeCate;
import com.ruoyi.interfaces.service.IMdDataCateService;
import com.ruoyi.interfaces.service.IMdDataSetService;
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.common.utils.poi.ExcelUtil;

/**
 * 数据项分类管理Controller
 *
 * @author 朱得糠
 * @date 2025-07-25
 */
@RestController
@RequestMapping("/data/cate")
public class MdDataCateController extends BaseController {
    @Autowired
    private IMdDataCateService mdDataCateService;

    @Autowired
    private IMdDataSetService mdDataSetService;

    /**
     * 查询数据项分类管理列表
     */
    @GetMapping("/list")
    public AjaxResult list(MdDataCate mdDataCate) {
        List<MdDataCate> list = mdDataCateService.selectMdDataCateList(mdDataCate);
        return success(list);
    }

    /**
     * 导出数据项分类管理列表
     */
    @Log(title = "数据项分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MdDataCate mdDataCate) {
        List<MdDataCate> list = mdDataCateService.selectMdDataCateList(mdDataCate);
        ExcelUtil<MdDataCate> util = new ExcelUtil<MdDataCate>(MdDataCate.class);
        util.exportExcel(response, list, "数据项分类管理数据");
    }

    /**
     * 获取数据项分类管理详细信息
     */
    @GetMapping(value = "/{cateId}")
    public AjaxResult getInfo(@PathVariable("cateId") String cateId) {
        return success(mdDataCateService.selectMdDataCateByCateId(cateId));
    }

    /**
     * 构建数据项树状列表
     * @param mdDataCate
     * @return
     */
    @GetMapping(value = "/treeselect")
    public AjaxResult treeselect(MdDataCate mdDataCate) {
        List<MdDataCate> mdDataCates = mdDataCateService.selectMdDataCateList(mdDataCate).stream().peek(item -> item.setIcon("TREE")).collect(Collectors.toList());

      /*  List<MdDataSet> mdDataSets = mdDataSetService.selectMdDataSetList(new MdDataSet());
        for (MdDataSet mdDataSet : mdDataSets) {
            mdDataCates.add(new MdDataCate(mdDataSet.getDcCode(), mdDataSet.getCateId(), mdDataSet.getDcName(), "SET"));
        }*/
        return success(mdDataCateService.buildDataTreeSelect(mdDataCates));
    }

    /**
     * 新增数据项分类管理
     */
    @Log(title = "数据项分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MdDataCate mdDataCate) {
        return toAjax(mdDataCateService.insertMdDataCate(mdDataCate));
    }

    /**
     * 修改数据项分类管理
     */
    @Log(title = "数据项分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MdDataCate mdDataCate) {
        return toAjax(mdDataCateService.updateMdDataCate(mdDataCate));
    }

    /**
     * 删除数据项分类管理
     */
    @Log(title = "数据项分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cateIds}")
    public AjaxResult remove(@PathVariable String[] cateIds) {
        return toAjax(mdDataCateService.deleteMdDataCateByCateIds(cateIds));
    }
}
