package com.yunfeiyun.agmp.pts.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.yunfeiyun.agmp.pts.service.IPtsTutorialService;
import com.yunfeiyun.agmp.common.constant.ErrorCode;
import com.yunfeiyun.agmp.common.constant.ResConstants;
import com.yunfeiyun.agmp.common.framework.manager.ResManager;
import com.yunfeiyun.agmp.common.framework.web.domain.UploadParam;
import com.yunfeiyun.agmp.common.web.system.domain.SysRes;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yunfeiyun.agmp.common.annotation.Log;
import com.yunfeiyun.agmp.common.core.controller.BaseController;
import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.enums.BusinessType;
import com.yunfeiyun.agmp.pts.domain.PtsTutorial;
import com.yunfeiyun.agmp.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 教程Controller
 * 
 * @author 杨晓辉
 * @date 2023-05-23
 */
@RestController
@RequestMapping("/system/tutorial")
public class PtsTutorialController extends BaseController
{
    @Autowired
    private IPtsTutorialService ptsTutorialService;

    @Autowired
    ResManager resManager;


    /**
     * 查询教程帮助
     */
    @GetMapping("/help")
    public AjaxResult help(PtsTutorial ptsTutorial)
    {
        List<PtsTutorial> list = ptsTutorialService.selectPtsTutorialList(ptsTutorial);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("data" ,list );
        ajax.put("total" ,list.size() );
        return ajax;
    }


    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('system:tutorial:list')")
    @GetMapping("/list")
    public TableDataInfo list(PtsTutorial ptsTutorial)
    {
        startPage();
        List<PtsTutorial> list = ptsTutorialService.selectPtsTutorialList(ptsTutorial);
        return getDataTable(list);
    }



    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:tutorial:query')")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(String tutorialId)
    {
        return success(ptsTutorialService.selectPtsTutorialByTutorialId(tutorialId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('system:tutorial:add')")
    @Log(title = "教程", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PtsTutorial ptsTutorial)
    {
        return toAjax(ptsTutorialService.insertPtsTutorial(ptsTutorial,getLoginUser()));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('system:tutorial:edit')")
    @Log(title = "教程", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody PtsTutorial ptsTutorial)
    {
        return toAjax(ptsTutorialService.updatePtsTutorial(ptsTutorial,getLoginUser()));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('system:tutorial:remove')")
    @Log(title = "教程", businessType = BusinessType.DELETE)
	@DeleteMapping("/delete/{tutorialIds}")
    public AjaxResult remove(@PathVariable String[] tutorialIds)
    {
        return toAjax(ptsTutorialService.deletePtsTutorialByTutorialIds(tutorialIds));
    }


    /**
     * 资源图片和视频上传
     */
    @Log(title = "图片视频上传", businessType = BusinessType.UPDATE)
    @PostMapping("/preview/upload")
    public AjaxResult uploadTutorialPreview(MultipartFile file, HttpServletRequest request) throws Exception
    {
        if (!file.isEmpty())
        {
            UploadParam uploadParam = UploadParam.parse(request);
            if(uploadParam == null) return error(ErrorCode.UPLOAD_FAIL.getCode());
            SysRes sysRes  = resManager.uploadRes( ResConstants.BizType.TUTORIAL, uploadParam.getMediaType(), uploadParam.getResBusId() , file,getLoginUser());
            AjaxResult ajax = AjaxResult.success();
            ajax.put("data" ,sysRes );
            return ajax;
        }
        return error(ErrorCode.UPLOAD_FAIL.getCode());
    }

}
