package com.goldenwater.web.controller.system;

import java.util.List;
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.goldenwater.common.annotation.Log;
import com.goldenwater.common.core.controller.BaseController;
import com.goldenwater.common.core.domain.AjaxResult;
import com.goldenwater.common.core.page.TableDataInfo;
import com.goldenwater.common.enums.BusinessType;
import com.goldenwater.system.domain.SysCate;
import com.goldenwater.system.service.ISysCateService;

/**
 * 系统分类 信息操作处理
 * 
 * @author goldenwater
 */
@RestController
@RequestMapping("/system/cate")
public class SysCateController extends BaseController
{
    @Autowired
    private ISysCateService cateService;

    /**
     * 查询系统分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:cate:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCate cate)
    {
        startPage();
        List<SysCate> list = cateService.selectSysCateList(cate);
        return getDataTable(list);
    }

    /**
     * 根据分类编码获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:cate:query')")
    @GetMapping("/{cateCode}")
    public AjaxResult getInfo(@PathVariable String cateCode)
    {
        return success(cateService.selectSysCateById(cateCode));
    }

    /**
     * 新增系统分类
     */
    @PreAuthorize("@ss.hasPermi('system:cate:add')")
    @Log(title = "系统分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysCate cate)
    {
        cate.setCreateBy(getUsername());
        return toAjax(cateService.insertSysCate(cate));
    }

    /**
     * 修改系统分类
     */
    @PreAuthorize("@ss.hasPermi('system:cate:edit')")
    @Log(title = "系统分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysCate cate)
    {
        cate.setUpdateBy(getUsername());
        return toAjax(cateService.updateSysCate(cate));
    }

    /**
     * 删除系统分类
     */
    @PreAuthorize("@ss.hasPermi('system:cate:remove')")
    @Log(title = "系统分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cateCodes}")
    public AjaxResult remove(@PathVariable String[] cateCodes)
    {
        return toAjax(cateService.deleteSysCateByIds(cateCodes));
    }

    /**
     * 获取父分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:cate:list')")
    @GetMapping("/parentList")
    public AjaxResult parentList()
    {
        List<SysCate> list = cateService.findParent();
        return success(list);
    }

    /**
     * 获取子分类列表
     */
    @PreAuthorize("@ss.hasPermi('system:cate:list')")
    @GetMapping("/children")
    public AjaxResult childrenList(SysCate cate)
    {
        List<SysCate> list = cateService.findChildren(cate);
        return success(list);
    }

    /**
     * 修改系统分类状态
     */
    @PreAuthorize("@ss.hasPermi('system:cate:edit')")
    @Log(title = "系统分类", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public AjaxResult editStatus(@RequestBody SysCate cate)
    {
        return toAjax(cateService.updateStatus(cate));
    }
}
