package com.ruoyi.interfaces.controller;

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

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.interfaces.domain.MdModelFlow;
import com.ruoyi.interfaces.domain.MdModelInfo;
import com.ruoyi.interfaces.domain.PtService;
import com.ruoyi.interfaces.domain.vo.MdModelInfoVo;
import com.ruoyi.interfaces.service.IMdModelFlowService;
import com.ruoyi.interfaces.service.IMdModelInfoService;
import com.ruoyi.interfaces.service.PtServiceService;
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.PtTreeCate;
import com.ruoyi.interfaces.service.IPtTreeCateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 树状目录Controller
 *
 * @author 朱得糠
 * @date 2025-07-29
 */
@RestController
@RequestMapping("/interfaces/cate")
public class PtTreeCateController extends BaseController {
    @Autowired
    private IPtTreeCateService ptTreeCateService;

    @Autowired
    private PtServiceService ptServiceService;

    @Autowired
    private IMdModelInfoService mdModelInfoService;

    @Autowired
    private IMdModelFlowService mdModelFlowService;

    /**
     * 查询树状目录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PtTreeCate ptTreeCate) {
        startPage();
        List<PtTreeCate> list = ptTreeCateService.selectPtTreeCateList(ptTreeCate);
        return getDataTable(list);
    }

    /**
     * 导出树状目录列表
     */
    @Log(title = "树状目录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PtTreeCate ptTreeCate) {
        List<PtTreeCate> list = ptTreeCateService.selectPtTreeCateList(ptTreeCate);
        ExcelUtil<PtTreeCate> util = new ExcelUtil<PtTreeCate>(PtTreeCate.class);
        util.exportExcel(response, list, "树状目录数据");
    }

    /**
     * 获取树状目录详细信息
     */
    @GetMapping(value = "/{treeId}")
    public AjaxResult getInfo(@PathVariable("treeId") String treeId) {
        return success(ptTreeCateService.selectPtTreeCateByTreeId(treeId));
    }

    /**
     * 构建数据项树状列表
     *
     * @param ptTreeCate
     * @return
     */
    @GetMapping(value = "/treeselect")
    public AjaxResult treeselect(PtTreeCate ptTreeCate) {
        List<PtTreeCate> ptTreeCates = ptTreeCateService.selectPtTreeCateList(ptTreeCate);
        return success(ptTreeCateService.buildDataTreeSelect(ptTreeCates));
    }

    /**
     * 构建模型流程图树状列表
     *
     * @param par
     * @return
     */
    @GetMapping(value = "/treeFlow")
    public AjaxResult treeFlow(MdModelInfo par) {
        List<PtTreeCate> ptTreeCates = new ArrayList<>();
        List<MdModelInfo> mdModelInfos = mdModelInfoService.selectMdModelInfoList(par);

        for (MdModelInfo mdModelInfo : mdModelInfos) {
            if (StringUtils.isNotNull(mdModelInfo.getMdid()) && StringUtils.isNotNull(mdModelInfo.getCateid())
                    && StringUtils.isNotNull(mdModelInfo.getName())) {
                ptTreeCates.add(new PtTreeCate(mdModelInfo.getMdid(), mdModelInfo.getCateid(), mdModelInfo.getName(), "MODEL"));
            }
        }
        List<MdModelFlow> mdModelFlows = mdModelFlowService.selectMdModelFlowList(new MdModelFlow());
        for (MdModelFlow mdModelFlow : mdModelFlows) {
            if (StringUtils.isNotNull(mdModelFlow.getMdid()) && StringUtils.isNotNull(mdModelFlow.getFlowId())
                    && StringUtils.isNotNull(mdModelFlow.getFlowName())) {
                ptTreeCates.add(new PtTreeCate(mdModelFlow.getFlowId().toString(), mdModelFlow.getMdid(), mdModelFlow.getFlowName(), "FLOW"));
            }
        }



        return success(ptTreeCateService.buildDataTreeSelect(ptTreeCates));
    }


    /**
     * 新增树状目录
     */
    @Log(title = "树状目录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PtTreeCate ptTreeCate) {
        return toAjax(ptTreeCateService.insertPtTreeCate(ptTreeCate));
    }

    /**
     * 修改树状目录
     */
    @Log(title = "树状目录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PtTreeCate ptTreeCate) {
        return toAjax(ptTreeCateService.updatePtTreeCate(ptTreeCate));
    }

    /**
     * 删除树状目录
     */
    @Log(title = "树状目录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{treeIds}")
    public AjaxResult remove(@PathVariable String[] treeIds) {
        return toAjax(ptTreeCateService.deletePtTreeCateByTreeIds(treeIds));
    }


    /**
     * 构建模型树状列表
     *
     * @param ptTreeCate
     * @return
     */
    @GetMapping(value = "/modelTreeSelect")
    public AjaxResult modelTreeSelect(PtTreeCate ptTreeCate)    {
        ptTreeCate.setTreeType("MODEL");
        String level = StringUtils.isNull(ptTreeCate.getParams().get("level")) ? "3" : ptTreeCate.getParams().get("level").toString();

        List<PtTreeCate> ptTreeCates = ptTreeCateService.selectPtTreeCateList(ptTreeCate).stream().peek(item -> item.setNodeType("TREE")).collect(Collectors.toList());
        if (level.equals("2") || level.equals("3")) {
            List<MdModelInfo> mdModelInfos = mdModelInfoService.selectMdModelInfoList(new MdModelInfo());
            for (MdModelInfo mdModelInfo : mdModelInfos) {
                if (StringUtils.isNotNull(mdModelInfo.getMdid()) && StringUtils.isNotNull(mdModelInfo.getCateid())
                        && StringUtils.isNotNull(mdModelInfo.getName())) {
                    ptTreeCates.add(new PtTreeCate(mdModelInfo.getMdid(), mdModelInfo.getCateid(), mdModelInfo.getName(), "MODEL",null));
                }
            }
        }
        if (level.equals("3")) {
            List<PtService> ptServices = ptServiceService.selectAllColumns(new PtService());
            for (PtService ptService : ptServices) {
                if (StringUtils.isNotNull(ptService.getSrvId()) &&
                        StringUtils.isNotNull(ptService.getMdid()) &&
                        StringUtils.isNotNull(ptService.getName())) {
                    ptTreeCates.add(new PtTreeCate(ptService.getSrvId(), ptService.getMdid(), ptService.getName(), "SERVICE",ptService));
                }
            }
        }
        return success(ptTreeCateService.buildDataTreeSelect(ptTreeCates));
    }

}
