package com.bs.ct.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.bs.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.bs.common.annotation.Log;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.hutool.core.lang.Validator;
import com.bs.ct.domain.CtGalleryCata;
import com.bs.ct.service.ICtGalleryCataService;
import javax.annotation.Resource;

/**
 * 图库目录Controller
 *
 * @author bs
 * @date 2025-02-22
 */
@Api(tags = "图库目录")
@RestController
@RequestMapping("/gallery/cata")
public class CtGalleryCataController extends BaseController {
    @Resource
    private ICtGalleryCataService ctGalleryCataService;

    /**
     * 分页查询图库目录列表
     */
    @ApiOperation("分页查询图库目录列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CtGalleryCata ctGalleryCata) {
        startPage();
        LambdaQueryWrapper<CtGalleryCata> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctGalleryCata);
        List<CtGalleryCata> list = ctGalleryCataService.list(queryWrapper);
        List<CtGalleryCata> ctGalleryCatas = buildTree(list);
        return getDataTable(ctGalleryCatas);
    }

    /**
     * 查询图库目录列表
     */
    @ApiOperation("查询图库目录列表")
    @GetMapping("/list")
    public AjaxResult list(CtGalleryCata ctGalleryCata) {
        LambdaQueryWrapper<CtGalleryCata> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctGalleryCata);
        List<CtGalleryCata> list = ctGalleryCataService.list(queryWrapper);
        List<CtGalleryCata> ctGalleryCatas = buildTree(list);
        return success(ctGalleryCatas);
    }

    public List<CtGalleryCata> buildTree(List<CtGalleryCata> list) {
        Map<Long, CtGalleryCata> map = new HashMap<>();
        List<CtGalleryCata> tree = new ArrayList<>();
        // 先将所有节点放入 map 中
        for (CtGalleryCata node : list) {
            map.put(node.getId(), node);
        }
        // 构建树结构
        for (CtGalleryCata node : list) {
            Long parentId = node.getParentId();
            if (parentId == null ||!map.containsKey(parentId)) {
                tree.add(node);
            } else {
                CtGalleryCata parent = map.get(parentId);
                // 假设 CtGalleryCata 类中有一个 children 属性来存储子节点
                // 你需要在 CtGalleryCata 类中添加相应的属性和方法
                // 这里简单模拟添加子节点
                List<CtGalleryCata> children = parent.getChildren();
                if (children == null) {
                    children = new ArrayList<>();
                    parent.setChildren(children);
                }
                children.add(node);
            }
        }
        return tree;
    }

    /**
     * 导出图库目录列表
     */
    @ApiOperation("导出图库目录列表")
    @Log(title = "图库目录导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CtGalleryCata ctGalleryCata) {
        LambdaQueryWrapper<CtGalleryCata> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctGalleryCata);
        List<CtGalleryCata> list = ctGalleryCataService.list(queryWrapper);
        ExcelUtil<CtGalleryCata> util = new ExcelUtil<CtGalleryCata>(CtGalleryCata. class);
        util.exportExcel(response, list, "图库目录数据");
    }

    /**
     * 获取图库目录详细信息
     */
    @ApiOperation("获取图库目录详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ctGalleryCataService.getById(id));
    }

    /**
     * 新增图库目录
     */
    @ApiOperation("新增图库目录")
    @Log(title = "图库目录新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CtGalleryCata ctGalleryCata) {
        return toAjax(ctGalleryCataService.save(ctGalleryCata));
    }

    /**
     * 修改图库目录
     */
    @ApiOperation("修改图库目录")
    @Log(title = "图库目录修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CtGalleryCata ctGalleryCata) {
        return toAjax(ctGalleryCataService.updateById(ctGalleryCata));
    }

    /**
     * 删除图库目录
     */
    @ApiOperation("删除图库目录")
    @Log(title = "图库目录删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(ctGalleryCataService.removeBatchByIds(ids));
    }

    /**
      * 条件设置
      */
    private void condition (LambdaQueryWrapper<CtGalleryCata> queryWrapper,CtGalleryCata ctGalleryCata){


        //id
        if(Validator.isNotEmpty(ctGalleryCata.getId())){
            queryWrapper.eq(CtGalleryCata::getId,ctGalleryCata.getId());
        }

        //目录名称
        if(Validator.isNotEmpty(ctGalleryCata.getCataName())){
            queryWrapper.eq(CtGalleryCata::getCataName,ctGalleryCata.getCataName());
        }

        //父级id
        if(Validator.isNotEmpty(ctGalleryCata.getParentId())){
            queryWrapper.eq(CtGalleryCata::getParentId,ctGalleryCata.getParentId());
        }

        //类型
        if(Validator.isNotEmpty(ctGalleryCata.getType())){
            queryWrapper.eq(CtGalleryCata::getType,ctGalleryCata.getType());
        }

        // 查询私人目录
        if ("private".equals(ctGalleryCata.getType())) {
            queryWrapper.eq(CtGalleryCata::getCreateBy, SecurityUtils.getUserId());
        }

        //备注
        if(Validator.isNotEmpty(ctGalleryCata.getRemarks())){
            queryWrapper.eq(CtGalleryCata::getRemarks,ctGalleryCata.getRemarks());
        }

        //删除标志（0代表存在 2代表删除）
        if(Validator.isNotEmpty(ctGalleryCata.getDelFlag())){
            queryWrapper.eq(CtGalleryCata::getDelFlag,ctGalleryCata.getDelFlag());
        }

        //创建部门
        if(Validator.isNotEmpty(ctGalleryCata.getCreateDept())){
            queryWrapper.eq(CtGalleryCata::getCreateDept,ctGalleryCata.getCreateDept());
        }

        //创建人
        if(Validator.isNotEmpty(ctGalleryCata.getCreateBy())){
            queryWrapper.eq(CtGalleryCata::getCreateBy,ctGalleryCata.getCreateBy());
        }

        //创建时间
        if(Validator.isNotEmpty(ctGalleryCata.getCreateTime())){
            queryWrapper.eq(CtGalleryCata::getCreateTime,ctGalleryCata.getCreateTime());
        }

        //修改人
        if(Validator.isNotEmpty(ctGalleryCata.getUpdateBy())){
            queryWrapper.eq(CtGalleryCata::getUpdateBy,ctGalleryCata.getUpdateBy());
        }

        //修改时间
        if(Validator.isNotEmpty(ctGalleryCata.getUpdateTime())){
            queryWrapper.eq(CtGalleryCata::getUpdateTime,ctGalleryCata.getUpdateTime());
        }

        //租户ID
        if(Validator.isNotEmpty(ctGalleryCata.getTenantId())){
            queryWrapper.eq(CtGalleryCata::getTenantId,ctGalleryCata.getTenantId());
        }

        }

}
