package cn.pluss.platform.controller.helpVideo;

import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entity.HelpVideo;
import cn.pluss.platform.helpVideo.HelpVideoService;
import cn.pluss.platform.util.OssOperatUtil;
import cn.pluss.platform.util.PageUtils;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("helpVideo")
public class HelpVideoController extends BaseNoModelController {

    private Integer PAGE_SIZE = 12;

    @Autowired
    private HelpVideoService helpVideoService;

    /**
     * 引导页
     *
     * @return
     */
    @GetMapping("/index")
    public String index() {
        return "helpVideo/helpVideoList";
    }

    @RequestMapping("add")
    public ModelAndView add() {
        ModelAndView view = new ModelAndView("helpVideo/helpVideoBaseInfo");
        Map<String, String> uploadParam = OssOperatUtil.getUploadParam();
        view.addObject("uploadParam",uploadParam);
        return view;
    }

    /**
     * 分页查询
     */
    @GetMapping("/queryHelpVideoByPage")
    @ResponseBody
    public Result<PageInfo<HelpVideo>> queryHelpVideoByPage(Page<HelpVideo> page) {
        QueryWrapper<HelpVideo> helpVideoQueryWrapper=new QueryWrapper<HelpVideo>().orderByAsc("sort");
        page= helpVideoService.page(page, helpVideoQueryWrapper);
        return ResultGenerator.genSuccessResult(PageUtils.page(page));
    }

    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public Result<String> saveOrUpdate(@RequestBody HelpVideo helpVideo) {
        if (StringUtil.isNotEmpty(helpVideo.getId())){
            helpVideoService.updateById(helpVideo);
        }else {
            helpVideo.setCreateDt(new Date());
            helpVideoService.save(helpVideo);
        }
        return ResultGenerator.genSuccessResult("保存成功");
    }

    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable Integer id){
        ModelAndView view = new ModelAndView("helpVideo/helpVideoBaseInfo");
        HelpVideo helpVideo = helpVideoService.getById(id);
        Map<String, String> uploadParam = OssOperatUtil.getUploadParam();
        view.addObject("helpVideo",helpVideo);
        view.addObject("uploadParam",uploadParam);
        //OssOperatUtil.getUploadParam();
        return view;
    }

    @PostMapping("delete")
    @ResponseBody
    public Result<String> delete(@RequestBody HelpVideo helpVideo) {
        helpVideoService.removeById(helpVideo.getId());
        if(StringUtil.isNotEmpty(helpVideo.getVideoUrl())){
            OssOperatUtil.deleteFile(helpVideo.getVideoUrl());
        }
        return ResultGenerator.genSuccessResult("删除成功!");
    }
}


