package com.fs.course.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.DateUtils;
import com.fs.course.domain.FsVideoResource;
import com.fs.course.service.IFsVideoResourceService;
import com.fs.course.vo.FsVideoResourceVO;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 资源库管理
 */
@RestController
@RequestMapping("/course/videoResource")
@AllArgsConstructor
public class FsVideoResourceController extends BaseController {

    private final IFsVideoResourceService fsVideoResourceService;

    /**
     * 查询视频素材库列表
     */
    @PreAuthorize("@ss.hasPermi('course:videoResource:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(required = false) String resourceName,
                              @RequestParam(required = false) String fileName,
                              @RequestParam(required = false) Integer typeId,
                              @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                              @RequestParam(required = false, defaultValue = "10") Integer pageSize)
    {
        Map<String, Object> params = new HashMap<>();
        params.put("resourceName", resourceName);
        params.put("fileName", fileName);
        params.put("typeId", typeId);

        PageHelper.startPage(pageNum, pageSize);
        List<FsVideoResourceVO> list = fsVideoResourceService.selectVideoResourceListByMap(params);
        return getDataTable(list);
    }


    /**
     * 获取视频素材库详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:videoResource:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(fsVideoResourceService.getById(id));
    }

    /**
     * 新增视频素材库
     */
    @PreAuthorize("@ss.hasPermi('course:videoResource:add')")
    @Log(title = "视频素材库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsVideoResource fsVideoResource)
    {
        fsVideoResource.setCreateTime(LocalDateTime.now());
        fsVideoResourceService.save(fsVideoResource);
        return AjaxResult.success();
    }

    /**
     * 修改视频素材库
     */
    @PreAuthorize("@ss.hasPermi('course:videoResource:edit')")
    @Log(title = "视频素材库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsVideoResource fsVideoResource)
    {
        fsVideoResourceService.updateById(fsVideoResource);
        return AjaxResult.success();
    }

    /**
     * 删除视频素材库
     */
    @PreAuthorize("@ss.hasPermi('course:videoResource:remove')")
    @Log(title = "视频素材库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        Wrapper<FsVideoResource> updateWrapper = Wrappers.<FsVideoResource>lambdaUpdate()
                .set(FsVideoResource::getIsDel, 1)
                .in(FsVideoResource::getId, Arrays.asList(ids));
        fsVideoResourceService.update(updateWrapper);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('course:videoResource:add')")
    @Log(title = "视频素材库", businessType = BusinessType.INSERT)
    @PostMapping("/batchAddVideoResource")
    public AjaxResult batchAddVideoResource(@RequestBody List<FsVideoResource> list) {
        if (Objects.isNull(list) || list.isEmpty()) {
            return AjaxResult.error("数据不能为空");
        }
        list.forEach(v -> v.setCreateTime(LocalDateTime.now()));
        fsVideoResourceService.saveBatch(list);
        return AjaxResult.success();
    }
}
