package com.corewing.app.modules.admin.biz;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.corewing.app.common.Result;
import com.corewing.app.dto.biz.tutorial.CategoryBatchDeleteRequest;
import com.corewing.app.entity.Tutorial;
import com.corewing.app.entity.TutorialCategory;
import com.corewing.app.service.TutorialCategoryService;
import com.corewing.app.service.TutorialService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 教程指南
 */
@Controller
@RequestMapping("/biz/tutorial")
public class BizTutorialController {

    @Resource
    private TutorialCategoryService tutorialCategoryService;

    @Resource
    private TutorialService tutorialService;


    /**
     * 教程分页
     * @param tutorial
     * @return
     */
    @GetMapping("/page")
    @ResponseBody
    public Result<Page<Tutorial>> page(Tutorial tutorial) {
        return Result.success(tutorialService.page(tutorial));
    }

    /**
     * 新增教程
     * @param tutorial
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public Result<String> save(@RequestBody Tutorial tutorial) {
        return Result.isBool(tutorialService.save(tutorial));
    }

    /**
     * 更新教程
     * @param tutorial
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public Result<String> update(@RequestBody Tutorial tutorial) {
        return Result.isBool(tutorialService.updateById(tutorial));
    }

    /**
     * 删除教程
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ResponseBody
    public Result<String> delete(Long id) {
        return Result.isBool(tutorialService.remove(id));
    }



    /**
     * 教程管理首页
     * @return
     */
    @GetMapping("/index")
    public String index() {
        return "admin/biz/tutorial/index";
    }

    /**
     * 教程分类分页
     * @param tutorialCategory
     * @return
     */
    @GetMapping("/category/page")
    @ResponseBody
    public Result<Page<TutorialCategory>> categoryPage(TutorialCategory tutorialCategory) {
        return Result.success(tutorialCategoryService.page(tutorialCategory));
    }

    /**
     * 新增教程分类
     * @param tutorialCategory
     * @return
     */
    @PostMapping("/category/save")
    @ResponseBody
    public Result<String> saveCategory(@RequestBody TutorialCategory tutorialCategory) {
        tutorialCategory.setType(TutorialCategory.typeCategory);
        return Result.isBool(tutorialCategoryService.save(tutorialCategory));
    }

    /**
     * 更新教程分类
     * @param tutorialCategory
     * @return
     */
    @PostMapping("/category/update")
    @ResponseBody
    public Result<String> updateCategory(@RequestBody TutorialCategory tutorialCategory) {
        return Result.isBool(tutorialCategoryService.updateById(tutorialCategory));
    }

    /**
     * 删除教程分类
     * @param id
     * @return
     */
    @DeleteMapping("/category/delete")
    @ResponseBody
    public Result<String> deleteCategory(Long id) {
        return Result.isBool(tutorialCategoryService.removeById(id));
    }

    /**
     * 批量删除教程分类
     * @param categoryBatchDeleteRequest
     * @return
     */
    @PostMapping("/category/batchDelete")
    @ResponseBody
    public Result<String> batchDeleteCategory(@RequestBody CategoryBatchDeleteRequest categoryBatchDeleteRequest) {
        return Result.isBool(tutorialCategoryService.removeBatchByIds(categoryBatchDeleteRequest.getIds()));
    }




}
