package com.xinelu.applet.controller.nurseappletpersontraining;

import com.xinelu.applet.service.nurseappletpersontraining.NurseAppletPersonTrainingService;
import com.xinelu.common.annotation.MobileRequestAuthorization;
import com.xinelu.common.annotation.RepeatSubmit;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.custominterface.Insert;
import com.xinelu.manage.domain.trainingitemwatchrecord.TrainingItemWatchRecord;
import com.xinelu.manage.dto.trainingitem.NurseAppletPersonTrainingItemDTO;
import com.xinelu.manage.dto.trainingitem.TrainingItemDTO;
import com.xinelu.manage.dto.trainingitemwatchrecord.TrainingItemWatchRecordDTO;
import com.xinelu.manage.dto.trainingorder.NurseAppletPersonTrainingItemOrderDTO;
import com.xinelu.manage.vo.trainingitem.TrainingItemVO;
import com.xinelu.manage.vo.trainingorderdetails.NurseAppletPersonTrainingOrderDetailsVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 护理员学习培训控制器
 *
 * @author ljh
 * @version 1.0
 * Create by 2023/4/21 8:56
 */
@RestController
@RequestMapping("/nurseApplet/personLearn")
public class NurseAppletPersonTrainingController extends BaseController {
    @Resource
    private NurseAppletPersonTrainingService nurseAppletPersonTrainingService;

    /**
     * 学习培训-查询学习培训课程组信息（App和小程序共用）
     *
     * @param trainingItem 学习培训项目组
     * @return com.xinyilu.common.core.page.TableDataInfo
     **/
    @MobileRequestAuthorization
    @GetMapping("/getTrainingItemGroupInfo")
    public TableDataInfo getTrainingItemGroupInfo(TrainingItemDTO trainingItem) {
        startPage();
        List<TrainingItemVO> trainingItemList = nurseAppletPersonTrainingService.getTrainingItemGroupInfo(trainingItem);
        return getDataTable(trainingItemList);
    }

    /**
     * 学习培训-查询查询学习培训课程下属视频和图文信息（App和小程序共用）
     *
     * @param trainingItem 学习培训项目
     * @return com.xinyilu.common.core.page.TableDataInfo
     **/
    @MobileRequestAuthorization
    @GetMapping("/getTrainingVideoAndContentList")
    public AjaxResult getTrainingVideoAndContentList(TrainingItemDTO trainingItem) {
        if (Objects.isNull(trainingItem) || Objects.isNull(trainingItem.getTrainingParentId())) {
            return AjaxResult.error("学习培训课程信息不存在！");
        }
        return nurseAppletPersonTrainingService.getTrainingVideoAndContentList(trainingItem);
    }

    /**
     * 学习培训-查询学习培训课程详情信息（App和小程序共用）
     *
     * @param trainingItem 学习培训项目
     * @return com.xinyilu.common.core.page.TableDataInfo
     **/
    @MobileRequestAuthorization
    @GetMapping("/getTrainingItemDetailsInfo")
    public AjaxResult getTrainingItemDetailsInfo(NurseAppletPersonTrainingItemDTO trainingItem) {
        if (Objects.isNull(trainingItem) || Objects.isNull(trainingItem.getTrainingItemId())) {
            return AjaxResult.error("课程详情信息不存在！");
        }
        if (Objects.isNull(trainingItem.getNurseStationPersonId())) {
            return AjaxResult.error("护理人员id不能为空！");
        }
        return nurseAppletPersonTrainingService.getTrainingItemDetailsInfo(trainingItem);
    }

    /**
     * 学习培训-下单方法（App和小程序共用）
     *
     * @param trainingItemOrderDTO 培训订单
     * @return com.xinyilu.common.core.domain.AjaxResult
     **/
    @MobileRequestAuthorization
    @RepeatSubmit
    @PostMapping("/buyTrainingItemOrder")
    public AjaxResult buyTrainingItemOrder(@RequestBody @Validated(Insert.class) NurseAppletPersonTrainingItemOrderDTO trainingItemOrderDTO) {
        return nurseAppletPersonTrainingService.buyTrainingItemOrder(trainingItemOrderDTO);
    }

    /**
     * 我的学习-查询学习培训项目订单集合组
     *
     * @param trainingItem 学习培训项目
     * @return java.util.List<com.xinyilu.base.vo.trainingitem.NurseAppletPersonTrainingItemVO>
     **/
    @MobileRequestAuthorization
    @GetMapping("/selectTrainingOrderItemGroupList")
    public TableDataInfo selectTrainingOrderItemGroupList(NurseAppletPersonTrainingItemDTO trainingItem) {
        startPage();
        List<NurseAppletPersonTrainingOrderDetailsVO> getTrainingOrderItemGroupList = nurseAppletPersonTrainingService.selectTrainingOrderItemGroupList(trainingItem);
        return getDataTable(getTrainingOrderItemGroupList);
    }

    /**
     * 我的学习-查询学习培训项目订单集合
     *
     * @param trainingItem 学习培训项目
     * @return java.util.List<com.xinyilu.base.vo.trainingitem.NurseAppletPersonTrainingItemVO>
     **/
    @MobileRequestAuthorization
    @GetMapping("/selectTrainingOrderItemList")
    public AjaxResult selectTrainingOrderItemList(NurseAppletPersonTrainingItemDTO trainingItem) {
        if (Objects.isNull(trainingItem) || Objects.isNull(trainingItem.getTrainingParentId()) || Objects.isNull(trainingItem.getTrainingOrderNo())) {
            return AjaxResult.error("当前培训学习课程订单信息不存在，请联系管理员！");
        }
        if (Objects.isNull(trainingItem.getNurseStationPersonId())) {
            return AjaxResult.error("当前护理人员信息不存在，请联系管理员！");
        }
        return nurseAppletPersonTrainingService.selectTrainingOrderItemList(trainingItem);
    }

    /**
     * 我的学习-查询已购买学习培训项目详情
     *
     * @param trainingItem 学习培训项目
     * @return com.xinyilu.common.core.page.TableDataInfo
     **/
    @MobileRequestAuthorization
    @GetMapping("/selectTrainingOrderVideoDetails")
    public AjaxResult selectTrainingOrderVideoDetails(NurseAppletPersonTrainingItemDTO trainingItem) {
        if (Objects.isNull(trainingItem) || Objects.isNull(trainingItem.getTrainingItemId())) {
            return AjaxResult.error("当前培训学习课程订单信息不存在，请联系管理员！");
        }
        if (StringUtils.isBlank(trainingItem.getTrainingOrderNo())) {
            return AjaxResult.error("当前培训学习课程订单信息不存在，请联系管理员！");
        }
        return nurseAppletPersonTrainingService.selectTrainingOrderVideoDetails(trainingItem);
    }

    /**
     * 查询学习培训课程观看时间点记录信息（观看视频用，App和小程序共用）
     *
     * @param trainingItemWatchRecord 学习培训项目观看时间点记录信息
     * @return com.xinyilu.common.core.domain.AjaxResult
     **/
    @MobileRequestAuthorization
    @GetMapping("/getTrainingItemWatchRecord")
    public AjaxResult getTrainingItemWatchRecord(TrainingItemWatchRecord trainingItemWatchRecord) {
        if (Objects.isNull(trainingItemWatchRecord) || Objects.isNull(trainingItemWatchRecord.getTrainingItemId()) || Objects.isNull(trainingItemWatchRecord.getTrainingItemDirectoryId())) {
            return AjaxResult.error("当前课程信息不出在，无法观看！");
        }
        if (Objects.isNull(trainingItemWatchRecord.getTrainingOrderId())) {
            return AjaxResult.error("当前课程暂未购买，请先进行购买，无法观看！");
        }
        if (Objects.isNull(trainingItemWatchRecord.getNurseStationPersonId())) {
            return AjaxResult.error("当前护理人员信息不存在，无法观看！");
        }
        return nurseAppletPersonTrainingService.getTrainingWatchRecord(trainingItemWatchRecord);
    }

    /**
     * 新增修改学习培训课程观看时间点记录信息（退出视频用，App和小程序共用）
     *
     * @param trainingItemWatchRecord 学习培训项目观看时间点记录信息
     * @return com.xinyilu.common.core.domain.AjaxResult
     **/
    @MobileRequestAuthorization
    @PostMapping("/insertTrainingItemWatchRecord")
    public AjaxResult insertTrainingItemWatchRecord(@RequestBody @Validated(Insert.class) TrainingItemWatchRecordDTO trainingItemWatchRecord) {
        if (Objects.isNull(trainingItemWatchRecord.getWatchTime()) || Objects.isNull(trainingItemWatchRecord.getItemDirectoryVideoDuration())) {
            return AjaxResult.error("当前视频播放时长有误，无法播放，请联系管理员！");
        }
        return nurseAppletPersonTrainingService.insertTrainingWatchRecord(trainingItemWatchRecord);
    }

    /**
     * 查询待支付的订单详情信息
     *
     * @param trainingOrderNo 学习培训订单编号
     * @return 详情信息
     */
    @MobileRequestAuthorization
    @GetMapping("/getWaitPayTrainingOrderDetails")
    public AjaxResult getWaitPayTrainingOrderDetails(String trainingOrderNo) {
        if (StringUtils.isBlank(trainingOrderNo)) {
            return AjaxResult.error("当前订单信息不存在，请联系管理员！");
        }
        return nurseAppletPersonTrainingService.getWaitPayTrainingOrderDetails(trainingOrderNo);
    }
}
