package com.ruoyi.base.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.base.domain.BaseCategory;
import com.ruoyi.base.service.IBaseCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分类树-各种分类属性结构Controller
 *
 * @author vinjor-m
 * @date 2025-03-17
 */
@RestController
@RequestMapping("/base/category")
public class BaseCategoryController extends BaseController {
    @Autowired
    private IBaseCategoryService baseCategoryService;

    /**
     * 查询分类树-各种分类属性结构列表
     */
    @PreAuthorize("@ss.hasPermi('base:category:list')")
    @GetMapping("/list")
    public AjaxResult list(BaseCategory baseCategory) {
        LambdaQueryWrapper<BaseCategory> queryWrapper = new LambdaQueryWrapper<BaseCategory>();
        if (StringUtils.isNotEmpty(baseCategory.getCode())) {
            queryWrapper.like(BaseCategory::getCode, baseCategory.getCode());
        }
        if (StringUtils.isNotEmpty(baseCategory.getTitle())) {
            queryWrapper.like(BaseCategory::getTitle, baseCategory.getTitle());
        }
        queryWrapper.orderByAsc(BaseCategory::getSort);
        List<BaseCategory> list = baseCategoryService.list(queryWrapper);
        return success(list);
    }

    /**
     * 查询分类树
     *
     * @param code 分类编码
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 10:49 2025/4/14
     **/
    @GetMapping("/treeData")
    public AjaxResult treeData(@RequestParam("code") String code) {
        return success(baseCategoryService.treeData(code));
    }

    /**
     * 导出分类树-各种分类属性结构列表
     */
    @PreAuthorize("@ss.hasPermi('base:category:export')")
    @Log(title = "分类树-各种分类属性结构", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseCategory baseCategory) {
        List<BaseCategory> list = baseCategoryService.list();
        ExcelUtil<BaseCategory> util = new ExcelUtil<BaseCategory>(BaseCategory.class);
        util.exportExcel(response, list, "分类树-各种分类属性结构数据");
    }

    /**
     * 获取分类树-各种分类属性结构详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(baseCategoryService.getById(id));
    }

    /**
     * 获取分类树-根据code查询
     */
    @GetMapping(value = "/getByCodeInfo")
    public AjaxResult getByCodeInfo(@RequestParam("code") String code) {
        LambdaQueryWrapper<BaseCategory> queryWrapper = new LambdaQueryWrapper<BaseCategory>()
                .eq(BaseCategory::getCode,code);
        List<BaseCategory> list = baseCategoryService.list(queryWrapper);
        return success(list.isEmpty()?null:list.get(0));
    }

    /**
     * 新增分类树-各种分类属性结构
     */
    @PreAuthorize("@ss.hasPermi('base:category:add')")
    @Log(title = "分类树-各种分类属性结构", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseCategory baseCategory) {
        List<BaseCategory> list = baseCategoryService.selectByCode(baseCategory.getCode());
        if (!list.isEmpty()) {
            return error("唯一编码重复！");
        }
        return toAjax(baseCategoryService.save(baseCategory));
    }

    /**
     * 修改分类树-各种分类属性结构
     */
    @PreAuthorize("@ss.hasPermi('base:category:edit')")
    @Log(title = "分类树-各种分类属性结构", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseCategory baseCategory) {
        return toAjax(baseCategoryService.updateById(baseCategory));
    }

    /**
     * 删除分类树-各种分类属性结构
     */
    @PreAuthorize("@ss.hasPermi('base:category:remove')")
    @Log(title = "分类树-各种分类属性结构", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        List<String> list = new ArrayList<>(Arrays.asList(ids));
        return toAjax(baseCategoryService.removeByIds(list));
    }

    /**
     * 根据code查分类字典，list
     *
     * @param code     code
     * @param isSystem 是否系统级
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author vinjor-M
     * @date 11:31 2025/3/18
     **/
    @GetMapping("/listByCode")
    @Anonymous
    public AjaxResult listByCode(@RequestParam(value = "code") String code,
                                 @RequestParam(value = "isSystem", required = false) Integer isSystem) {
        try {
            return success(baseCategoryService.listByParentCode(code, isSystem));
        } catch (ServiceException e) {
            return error("未查询到该字典项！");
        }
    }

    /**
     * uniapp下拉列表使用
     *
     * @param code     code
     * @param isSystem 是否系统级
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author PQZ
     * @date 14:47 2025/4/3
     **/
    @GetMapping("/uniListByParentCode")
    @Anonymous
    public AjaxResult uniListByParentCode(@RequestParam(value = "code") String code,
                                          @RequestParam(value = "isSystem", required = false) Integer isSystem) {
        try {
            return success(baseCategoryService.uniListByParentCode(code, isSystem));
        } catch (ServiceException e) {
            return error("未查询到该字典项！");
        }
    }
}
