package com.koobietech.eas.controller;

import com.github.pagehelper.PageHelper;
import com.koobietech.eas.common.constant.Disabled;
import com.koobietech.eas.common.result.JsonPageResult;
import com.koobietech.eas.common.result.JsonResult;
import com.koobietech.eas.common.result.PageDataResult;
import com.koobietech.eas.mbg.model.EasEduCategory;
import com.koobietech.eas.service.EasEduCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Tag(name = "课程分类控制器")
@RequestMapping("/category")
public class EasEduCategoryController {

    @Resource
    EasEduCategoryService easEduCategoryService;

    /**
     * 添加类别
     * @param easEduCategory
     * @return
     */
    @PostMapping(value = "/add")
    @Operation(summary = "添加课程类别" , description = "用于添加课程类别")
    public JsonResult addEasEduCategory(@RequestBody EasEduCategory easEduCategory ){
        int add = easEduCategoryService.addEasEduCategory( easEduCategory );
        System.out.println( add );
        return JsonResult.ok("添加成功");
    }

    /**
     * 更新
     * @param easEduCategory
     * @return
     */
    @PostMapping(value = "/update")
    @Operation(summary = "课程类别更新" , description = "用于课程类别更新")
    public JsonResult updateEasEduCategory(@RequestBody EasEduCategory easEduCategory){
        int i = easEduCategoryService.updateEasEduCategory( easEduCategory );
        if (i > 0){
            return JsonResult.ok();
        }else {
            return JsonResult.fail();
        }
    }

    /**
     * 根据 Id 删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete/{id}")
    @Operation(summary = "根据Id删除课程类别" , description = "用于根据Id删除课程类别")
    public JsonResult deleteById(@PathVariable Integer id){
        int i = easEduCategoryService.deleteById(id);
        if (i > 0){
            return JsonResult.ok();
        }else {
            return JsonResult.fail();
        }
    }

    /**
     * id 查询
     * @param id
     * @return
     */
    @GetMapping(value = "/selectById")
    @Operation(summary = "根据Id查询对应课程类别" , description = "用于根据Id查询课程类别")
    public JsonResult selectById(Integer id){
        EasEduCategory easEduCategory = easEduCategoryService.selectById(id);
        System.out.println(
                Disabled.valueOf(easEduCategory.getDisabled())
        );
        return JsonResult.data( easEduCategory );
    }

    /**
     * 通过条件查询课程
     * @param easEduCategory
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping(value = "/query")
    @Operation(summary = "根据条件进行查询课程类别", description = "用于根据条件查询课程类别")
    public JsonPageResult selectByCondition(@RequestBody EasEduCategory easEduCategory,
                                            @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageDataResult pageDataResult = easEduCategoryService.selectByCondition(easEduCategory);
        return JsonPageResult.data(pageDataResult);
    }
}
