package com.maintain.business.controller;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.tree.Tree;
import com.maintain.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.maintain.common.annotation.RepeatSubmit;
import com.maintain.common.annotation.Log;
import com.maintain.common.core.controller.BaseController;
import com.maintain.common.core.domain.PageQuery;
import com.maintain.common.core.domain.R;
import com.maintain.common.core.validate.AddGroup;
import com.maintain.common.core.validate.EditGroup;
import com.maintain.common.enums.BusinessType;
import com.maintain.common.utils.poi.ExcelUtil;
import com.maintain.business.domain.vo.ErpMaterialTypeVo;
import com.maintain.business.domain.bo.ErpMaterialTypeBo;
import com.maintain.business.service.IErpMaterialTypeService;

/**
 * 物资分类
 *
 * @author liushuai
 * @date 2025-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/materialType")
public class ErpMaterialTypeController extends BaseController {

    private final IErpMaterialTypeService iErpMaterialTypeService;

    /**
     * 查询物资分类列表
     */
    @SaCheckPermission("business:materialType:list")
    @GetMapping("/list")
    public R<List<ErpMaterialTypeVo>> list(ErpMaterialTypeBo bo, PageQuery pageQuery) {
        return R.ok(iErpMaterialTypeService.queryList(bo));
    }

    /**
     * 查询物资分类树
     */
    @SaCheckPermission("business:materialType:list")
    @GetMapping("/typeTree")
    public R<List<Tree<Long>>> tree(ErpMaterialTypeBo bo) {
        return R.ok(iErpMaterialTypeService.queryTreeList(bo));
    }

    /**
     * 查询分类列表（排除节点）
     *
     * @param typeId 部门ID
     */
    @SaCheckPermission("business:materialType:list")
    @GetMapping("/list/exclude/{typeId}")
    public R<List<ErpMaterialTypeVo>> excludeChild(@PathVariable(value = "typeId", required = false) Long typeId) {
        List<ErpMaterialTypeVo> typeVoList = iErpMaterialTypeService.queryList(new ErpMaterialTypeBo());
        typeVoList.removeIf(d -> d.getTypeId().equals(typeId)
            || StringUtils.splitList(d.getAncestors()).contains(Convert.toStr(typeId)));
        return R.ok(typeVoList);
    }

    /**
     * 导出物资分类列表
     */
    @SaCheckPermission("business:materialType:export")
    @Log(title = "物资分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpMaterialTypeBo bo, HttpServletResponse response) {
        List<ErpMaterialTypeVo> list = iErpMaterialTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "物资分类", ErpMaterialTypeVo.class, response);
    }

    /**
     * 获取物资分类详细信息
     *
     * @param typeId 主键
     */
    @SaCheckPermission("business:materialType:query")
    @GetMapping("/{typeId}")
    public R<ErpMaterialTypeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long typeId) {
        return R.ok(iErpMaterialTypeService.queryById(typeId));
    }

    /**
     * 新增物资分类
     */
    @SaCheckPermission("business:materialType:add")
    @Log(title = "物资分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpMaterialTypeBo bo) {
        return toAjax(iErpMaterialTypeService.insertByBo(bo));
    }

    /**
     * 修改物资分类
     */
    @SaCheckPermission("business:materialType:edit")
    @Log(title = "物资分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpMaterialTypeBo bo) {
        return toAjax(iErpMaterialTypeService.updateByBo(bo));
    }

    /**
     * 删除物资分类
     *
     * @param typeIds 主键串
     */
    @SaCheckPermission("business:materialType:remove")
    @Log(title = "物资分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] typeIds) {
        return toAjax(iErpMaterialTypeService.deleteWithValidByIds(Arrays.asList(typeIds), true));
    }
}
