package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeClassManage;
import shop.alien.store.service.PlatformClassService;

import java.io.IOException;

@Api(tags = {"平台-课程管理"})
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/platformClass")
@RequiredArgsConstructor
public class PlatformClassController {

    private final PlatformClassService platformClassService;

    @ApiOperation("课程列表")
    @ApiOperationSupport(order = 1)
    @GetMapping("/getClassManageList")
    public R<IPage<LifeClassManage>> getClassManageList(int page, int size, String className, String classTeacher, String classBigType) {
        log.info("PlatformClassController.getClassManageList?page={}, size={}, className={}, classTeacher={}, classBigType={}", page, size, className, classTeacher, classBigType);
        return R.data(platformClassService.getClassManageList(page, size, className, classTeacher, classBigType));
    }

    @ApiOperation("新增课程")
    @ApiOperationSupport(order = 2)
    @PostMapping("/addOrUpdateClassManage")
    public R<String> addOrUpdateClassManage(@RequestBody LifeClassManage lifeClassManage) throws IOException, InterruptedException {
        log.info("PlatformClassController.addOrUpdateClassManage?lifeClassManage={}", lifeClassManage.toString());
        int num = platformClassService.addOrUpdateClassManage(lifeClassManage);
        if (0 == num) {
            return R.fail("新增失败");
        } else {
            return R.data("新增成功");
        }
    }

    @ApiOperation("获取课程详情")
    @ApiOperationSupport(order = 3)
    @GetMapping("/getClassManageById")
    public R<LifeClassManage> getClassManageById(Integer id) {
        log.info("PlatformClassController.getClassManageById?id={}", id);
        return R.data(platformClassService.getClassManageById(id));
    }

    @ApiOperation("修改课程状态")
    @ApiOperationSupport(order = 4)
    @GetMapping("/setClassState")
    public R<String> setClassState(Integer id, String classState) {
        log.info("PlatformClassController.setClassState?id={}, classState={}", id, classState);
        int num = platformClassService.setClassState(id, classState);
        if (0 == num) {
            return R.fail("修改失败");
        } else {
            return R.data("修改成功");
        }
    }

    @ApiOperation("删除课程")
    @ApiOperationSupport(order = 5)
    @GetMapping("/deleteClassById")
    public R<String> deleteClassById(Integer id) {
        log.info("PlatformClassController.deleteClassById?id={}", id);
        int num = platformClassService.deleteClassById(id);
        if (0 == num) {
            return R.fail("修改失败");
        } else {
            return R.data("修改成功");
        }
    }

    @ApiOperation("导出Excel")
    @ApiOperationSupport(order = 6)
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportToExcel(String className, String classTeacher, String classBigType) {
        log.info("StoreInfoController.exportToExcel?className={}, classTeacher={}, classBigType={}", className, classTeacher, classBigType);
        return platformClassService.exportToExcel(className, classTeacher, classBigType);
    }
}
