package com.ruoyi.web.controller.front;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.syzy.domain.BBusiSyghVideo;
import com.ruoyi.syzy.service.IBBusiSyghVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "生涯课程")
@RequestMapping("front/sykc/")
public class FrontSyKcController extends BaseController {

    @Autowired
    private IBBusiSyghVideoService syghVideoService;

    @Autowired
    private ISysDictDataService dictDataService;

    @GetMapping("types")
    @ApiOperation("00    生涯课程类型")
    public TableDataInfo types() {
        if (!new Integer(1).equals(SecurityUtils.getLoginUser().getUser().getIsBind())) {
            throw new CustomException("未授权", 401);
        }
        List<SysDictData> arr = dictDataService.selectDictDataByType("sykc_type");
        List<JSONObject> list = new ArrayList<JSONObject>(arr.size());
        arr.forEach(row -> {
            JSONObject item = new JSONObject(true);
            item.put("value", row.getDictValue());
            item.put("label", row.getDictLabel());
            list.add(item);
        });
        return getDataTable(list);
    }

    @GetMapping("list")
    @ApiOperation("01 生涯课程列表")
    public TableDataInfo list(@ApiParam(value = "分类 ", example = "1") @RequestParam Integer type,
                              @ApiParam(value = "页数", example = "1") @RequestParam Integer pageNum,
                              @ApiParam(value = "页大小", example = "15") @RequestParam Integer pageSize) {
        if (!new Integer(1).equals(SecurityUtils.getLoginUser().getUser().getIsBind())) {
            throw new CustomException("未授权", 401);
        }
        return llist(type,pageNum,pageSize);
    }

    private TableDataInfo llist (Integer type,Integer pageNum,Integer pageSize){
        startPage();
        BBusiSyghVideo exam = new BBusiSyghVideo();
        if (type > 0) {
            exam.setType(type);
        }
        List<BBusiSyghVideo> arr = syghVideoService.selectBBusiSyghVideoList(exam);
        return getDataTable(arr);
    }


    @GetMapping("listNoToken")
    @ApiOperation("01-1 生涯课程列表")
    public TableDataInfo listNoToken(@ApiParam(value = "分类 ", example = "1") Integer type,
                                     @ApiParam(value = "页数", example = "1") Integer pageNum,
                                     @ApiParam(value = "页大小", example = "15")  Integer pageSize) {
        return llist(type,pageNum,pageSize);
    }

    @GetMapping("info")
    @ApiOperation("02 生涯课程详情")
    public AjaxResult videoInfo(@ApiParam("课程id") Long id) {
        BBusiSyghVideo info = syghVideoService.selectBBusiSyghVideoById(id);
        return AjaxResult.success(info);
    }
}
