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.PageData;
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.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

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

    @Resource
    EasEduCategoryService easEduCategoryService;

    /**
     * 添加类别
     * @param easEduCategory
     * @return
     */
    @PostMapping(value = "/addEasEduCategory")
    @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 = "/updateEasEduCategory")
    @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 = "/deleteById/{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 );
    }

    /**
     * 查询全部课程
     * @return
     */
    @GetMapping(value = "/selectAll")
    @Operation(summary = "查询全部课程类别" , description = "用于查询全部课程类别")
    public JsonResult selectAll(){
        List<EasEduCategory> easEduCategories = easEduCategoryService.selectAll();
        return JsonResult.data( easEduCategories );
    }

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