package com.zd.exam.controller;

import cn.hutool.core.collection.ListUtil;
import com.zd.common.core.domain.R;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.exam.domain.ElCategory;
import com.zd.exam.service.IElCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学习分类Controller
 *
 * @author liubo
 * @date 2021-12-07
 */
@RestController
@Api(tags = "【学习分类】")
@RequestMapping("/el_category")
public class ElCategoryController extends BaseController {
    @Autowired
    private IElCategoryService elCategoryService;

    /**
     * 查询学习分类列表 "exam:el_category:list"
     */
    @PreAuthorize(hasPermi = PerPrefix.EXAM_EL_CATEGORY+ PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询学习分类列表" ,notes="权限字符："+PerPrefix.EXAM_EL_CATEGORY+ PerFun.LIST)
    public List<ElCategory> list(ElCategory elCategory) {
        if(elCategory.getType() == null){
            return ListUtil.empty();
        }
        List<ElCategory> list = elCategoryService.selectElCategoryList(elCategory);
        return list;
    }

    /**
     * 获取学习分类树状结构
     * id 默认 0 查全部
     * @param type 1 资源，2 课程
     */
    @ApiOperation(value = "获取学习分类详细信息")
    @GetMapping(value = "/treeList/{id}/{type}")
    public AjaxResult treeList(@PathVariable("id") Long id, @PathVariable("type") Integer type) {
        if(id == null){
            return error("ID 不能为空！");
        }
        ElCategory elCategory = new ElCategory();
        elCategory.setType(type);
        elCategory.setParentId(id);
        return AjaxResult.success(elCategoryService.treeList(elCategory));
    }

    /**
     * 新增学习分类 "exam:el_category:add"
     */
    @ApiOperation(value = "新增学习分类" ,notes="权限字符："+PerPrefix.EXAM_EL_CATEGORY+ PerFun.ADD)
    @PreAuthorize(hasPermi =PerPrefix.EXAM_EL_CATEGORY+ PerFun.ADD)
    @Log(title = "学习分类", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody ElCategory elCategory) {
        if(elCategory.getType() == null){
            return R.fail("分类类型不能为空！");
        }
        return R.ok(elCategoryService.insertElCategory(elCategory));
    }

    /**
     * 修改学习分类 "exam:el_category:edit"
     */
    @ApiOperation(value = "修改学习分类" ,notes="权限字符："+PerPrefix.EXAM_EL_CATEGORY+ PerFun.EDIT)
    @PreAuthorize(hasPermi =PerPrefix.EXAM_EL_CATEGORY+ PerFun.EDIT)
    @Log(title = "学习分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ElCategory elCategory) {
        if(elCategory.getId() == null){
            return error("ID 不能为空！");
        }
        return AjaxResult.success(elCategoryService.updateElCategory(elCategory));
    }

    /**
     * 删除学习分类 "exam:el_category:remove"
     */
    @ApiOperation(value = "删除学习分类" ,notes="权限字符："+PerPrefix.EXAM_EL_CATEGORY+ PerFun.REMOVE)
    @PreAuthorize(hasPermi = PerPrefix.EXAM_EL_CATEGORY+ PerFun.REMOVE )
    @Log(title = "学习分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return AjaxResult.success(elCategoryService.deleteElCategoryByIds(ids));
    }
}
