package com.aegis.web.controller.resource.material;

import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.aegis.common.annotation.Log;
import com.aegis.common.core.controller.BaseController;
import com.aegis.common.core.domain.AjaxResult;
import com.aegis.common.enums.BusinessType;
import com.aegis.common.utils.StringUtils;
import com.aegis.resource.material.domain.MaterialCategory;
import com.aegis.resource.material.service.IResourceMaterialCategoryService;

/**
 * 物资分类Controller
 * 
 * @author aegis
 */
@RestController
@RequestMapping("/resources/material-categories")
public class ResourceMaterialCategoryController extends BaseController {

    @Autowired
    private IResourceMaterialCategoryService categoryService;

    /**
     * 查询物资分类列表
     */
    @PreAuthorize("@ss.hasPermi('resource:material:category:list')")
    @GetMapping("/list")
    public AjaxResult list(MaterialCategory category) {
        List<MaterialCategory> list = categoryService.listByCondition(category);
        return success(categoryService.buildCategoryTree(list));
    }

    /**
     * 获取物资分类下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(MaterialCategory category) {
        List<MaterialCategory> categories = categoryService.listByCondition(category);
        return success(categoryService.buildCategoryTreeSelect(categories));
    }

    /**
     * 查询物资分类列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('resource:material:category:list')")
    @GetMapping("/list/exclude/{id}")
    public AjaxResult excludeChild(@PathVariable(value = "id", required = false) Long id) {
        List<MaterialCategory> categories = categoryService.listByCondition(new MaterialCategory());
        categories.removeIf(c -> c.getId().intValue() == id || ArrayUtils.contains(StringUtils.split(c.getAncestors(), ","), id + ""));
        return success(categoryService.buildCategoryTree(categories));
    }

    /**
     * 根据ID获取物资分类详情
     */
    @PreAuthorize("@ss.hasPermi('resource:material:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(categoryService.getById(id));
    }

    /**
     * 新增物资分类
     */
    @PreAuthorize("@ss.hasPermi('resource:material:category:add')")
    @Log(title = "物资分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody MaterialCategory category) {
        return toAjax(categoryService.save(category));
    }

    /**
     * 修改物资分类
     */
    @PreAuthorize("@ss.hasPermi('resource:material:category:edit')")
    @Log(title = "物资分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody MaterialCategory category) {
        Long categoryId = category.getId();
        if (category.getParentId() != null && category.getParentId().equals(categoryId)) {
            return error("修改分类'" + category.getCategoryName() + "'失败，上级分类不能是自己");
        }
        return toAjax(categoryService.updateById(category));
    }

    /**
     * 删除物资分类
     */
    @PreAuthorize("@ss.hasPermi('resource:material:category:remove')")
    @Log(title = "物资分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(categoryService.removeById(id));
    }
}