package com.xinyilu.web.controller.motionprescriptionlibrary;

import com.xinyilu.common.annotation.Log;
import com.xinyilu.common.core.controller.BaseController;
import com.xinyilu.common.core.domain.AjaxResult;
import com.xinyilu.common.core.page.TableDataInfo;
import com.xinyilu.common.enums.BusinessType;
import com.xinyilu.common.utils.file.FileUtils;
import com.xinyilu.system.domain.dto.MotionPrescriptionVideoDTO;
import com.xinyilu.system.domain.motionprescriptionlibrary.MotionPrescriptionLibrary;
import com.xinyilu.system.domain.vo.motionprescriptionlibrary.MotionPrescriptionLibraryVO;
import com.xinyilu.system.service.motionprescriptionlibrary.IMotionPrescriptionLibraryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 运动处方库数据Controller
 *
 * @author xinyilu
 * @date 2023-04-03
 */
@RestController
@RequestMapping("/system/motionPrescriptionLibrary")
public class MotionPrescriptionLibraryController extends BaseController {
    @Resource
    private IMotionPrescriptionLibraryService motionPrescriptionLibraryService;

    /**
     * 查询运动处方库数据列表
     */
    @PreAuthorize("@ss.hasPermi('system:motionPrescriptionLibrary:list')")
    @GetMapping("/list")
    public TableDataInfo list(MotionPrescriptionLibraryVO motionPrescriptionLibrary) {
        startPage();
        List<MotionPrescriptionLibraryVO> list = motionPrescriptionLibraryService.selectMotionPrescriptionLibraryList(motionPrescriptionLibrary);
        return getDataTable(list);
    }

    /**
     * 获取运动处方库数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:motionPrescriptionLibrary:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(motionPrescriptionLibraryService.selectMotionPrescriptionLibraryById(id));
    }

    /**
     * 新增运动处方库数据
     */
    @PreAuthorize("@ss.hasPermi('system:motionPrescriptionLibrary:add')")
    @Log(title = "运动处方库数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody MotionPrescriptionLibrary motionPrescriptionLibrary) {
        //校验运动程度得分起始值不能大于结束值
        if (Objects.nonNull(motionPrescriptionLibrary.getDegreeStartFraction()) && Objects.nonNull(motionPrescriptionLibrary.getDegreeEndFraction())) {
            if (motionPrescriptionLibrary.getDegreeStartFraction().compareTo(motionPrescriptionLibrary.getDegreeEndFraction()) > 0) {
                return AjaxResult.error("运动程度开始值不能大于运动程度结束值，请重新输入！");
            }
        }
        return toAjax(motionPrescriptionLibraryService.insertMotionPrescriptionLibrary(motionPrescriptionLibrary));
    }

    /**
     * 修改运动处方库数据
     */
    @PreAuthorize("@ss.hasPermi('system:motionPrescriptionLibrary:edit')")
    @Log(title = "运动处方库数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody MotionPrescriptionLibrary motionPrescriptionLibrary) {
        //校验运动程度得分起始值不能大于结束值
        if (Objects.nonNull(motionPrescriptionLibrary.getDegreeStartFraction()) && Objects.nonNull(motionPrescriptionLibrary.getDegreeEndFraction())) {
            if (motionPrescriptionLibrary.getDegreeStartFraction().compareTo(motionPrescriptionLibrary.getDegreeEndFraction()) > 0) {
                return AjaxResult.error("运动程度开始值不能大于运动程度结束值，请重新输入！");
            }
        }
        return toAjax(motionPrescriptionLibraryService.updateMotionPrescriptionLibrary(motionPrescriptionLibrary));
    }

    /**
     * 删除运动处方库数据
     */
    @PreAuthorize("@ss.hasPermi('system:motionPrescriptionLibrary:remove')")
    @Log(title = "运动处方库数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return motionPrescriptionLibraryService.deleteMotionPrescriptionLibraryByIds(ids);
    }

    /**
     * 运动处方库视频上传方法
     */
    @PreAuthorize("@ss.hasPermi('system:motionPrescriptionLibrary:prescriptionVideoUpload')")
    @GetMapping(value = "/prescriptionVideoUpload")
    public AjaxResult prescriptionVideoUpload(Long id) {
        return AjaxResult.success(motionPrescriptionLibraryService.getMotionPrescriptionVideoInfo(id));
    }

    /**
     * 处方视频上传功能
     *
     * @param multipartFile        视频文件
     * @param motionPrescriptionId 处方库表id
     * @return 上传结果
     * @throws Exception 异常信息
     */
    @PostMapping("/uploadMotionPrescriptionVideo")
    public AjaxResult uploadMotionPrescriptionVideo(@RequestParam("file") MultipartFile multipartFile, Long motionPrescriptionId) throws Exception {
        if (Objects.isNull(multipartFile)) {
            return AjaxResult.error("请选择视频！");
        }
        if (Objects.isNull(motionPrescriptionId)) {
            return AjaxResult.error("请选择处方！");
        }
        return motionPrescriptionLibraryService.uploadMotionPrescriptionVideo(multipartFile, motionPrescriptionId);
    }

    /**
     * 处方上传视频
     *
     * @param prescriptionVideoDTO 输入参数
     * @return 上传结果
     * @throws Exception 错误信息
     */
    @PostMapping("/confirmUploadVideo")
    public AjaxResult confirmUploadVideo(@RequestBody MotionPrescriptionVideoDTO prescriptionVideoDTO) throws Exception {
        if (Objects.isNull(prescriptionVideoDTO) || Objects.isNull(prescriptionVideoDTO.getMotionPrescriptionId())) {
            return AjaxResult.error("请选择需要上传的运动处方！");
        }
        return motionPrescriptionLibraryService.confirmUploadVideo(prescriptionVideoDTO);
    }

    /**
     * 查询运动处方干预视频查看方法
     *
     * @param motionPrescriptionId 运动处方库表id
     * @return 干预视频信息集合
     */
    @GetMapping("/getPrescriptionVideoInfo")
    public AjaxResult getPrescriptionVideoList(Long motionPrescriptionId) {
        if (Objects.isNull(motionPrescriptionId)) {
            return AjaxResult.error("请选择要查看的运动处方信息！");
        }
        return motionPrescriptionLibraryService.getPrescriptionVideoList(motionPrescriptionId);
    }

    /**
     * 根据视频路径删除运动处方干预视频
     *
     * @param prescriptionVideoUrl 视频路径
     * @return 删除结果
     */
    @PostMapping("/deletePrescriptionVideo")
    public AjaxResult deletePrescriptionVideo(String prescriptionVideoUrl) {
        if (StringUtils.isBlank(prescriptionVideoUrl)) {
            return AjaxResult.error("请选择要删除的视频信息！");
        }
        FileUtils.deleteSystemFile(prescriptionVideoUrl);
        return AjaxResult.success();
    }
}
