package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.admin.io.request.course.CourseRelationVo;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.AdjustModel;
import com.yonge.cooleshow.biz.dal.dto.search.VideoGroupSearch;
import com.yonge.cooleshow.biz.dal.dto.search.VideoLessonSearch;
import com.yonge.cooleshow.biz.dal.entity.VideoLessonGroup;
import com.yonge.cooleshow.biz.dal.entity.VideoLessonGroupDetail;
import com.yonge.cooleshow.biz.dal.enums.AuthStatusEnum;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.enums.course.CourseTypeEnum;
import com.yonge.cooleshow.biz.dal.queryInfo.CourseRelationQueryInfo;
import com.yonge.cooleshow.biz.dal.service.VideoLessonGroupDetailService;
import com.yonge.cooleshow.biz.dal.service.VideoLessonGroupService;
import com.yonge.cooleshow.biz.dal.valid.AddGroup;
import com.yonge.cooleshow.biz.dal.valid.SelectGroup;
import com.yonge.cooleshow.biz.dal.vo.CountVideoGroupVo;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonAuthGroup;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonExamineVo;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonGroupDetailVo;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonPurchaseVo;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonShelvesVo;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonStudentDetailVo;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonStudentVo;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonTeacherVo;
import com.yonge.cooleshow.biz.dal.vo.VideoLessonVo;
import com.yonge.cooleshow.biz.dal.wrapper.course.CourseRelationWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.common.enums.YesOrNoEnum;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.base.util.StringUtil;
import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * @Author: cy
 * @Date: 2022/4/2
 */
@RestController
@RequestMapping("${app-config.url.admin:}/videoLesson")
@Api(tags = "视频课")
@Validated
public class VideoLessonController extends BaseController {
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private VideoLessonGroupService lessonGroupService;
    @Autowired
    private VideoLessonGroupDetailService videoLessonGroupDetailService;

    @ApiOperation(value = "根据视频课组id查详情")
    @GetMapping(value = "/selectGroupById")
    public HttpResponseResult<VideoLessonGroup> selectGroupById(@NotNull Long groupId) {
        return succeed(lessonGroupService.selectGroup(groupId));
    }

    /**
     * @Description: 学员详情-查询视频课列表
     * @Author: cy
     * @Date: 2022/4/2
     */
    @ApiOperation(value = "学员详情-查询视频课列表")
    @PostMapping(value = "/student")
    public HttpResponseResult<PageInfo<VideoLessonStudentDetailVo>> selectStudentPage(@Validated(AddGroup.class) @RequestBody VideoLessonSearch search) {
        return succeed(PageUtil.pageInfo(lessonGroupService.selectStudentPage(PageUtil.getPage(search), search)));
    }

    /**
     * @Description: 老师详情-查询视频课列表
     * @Author: cy
     * @Date: 2022/4/6
     */
    @ApiOperation(value = "老师详情-查询视频课列表")
    @PostMapping(value = "/teacher")
    public HttpResponseResult<PageInfo<VideoLessonTeacherVo>> selectTeacherPage(@Validated(AddGroup.class) @RequestBody VideoLessonSearch search) {
        return succeed(PageUtil.pageInfo(lessonGroupService.selectTeacherPage(PageUtil.getPage(search), search)));
    }

    /**
     * @Description: 老师详情-视频课详情-查询购买学员列表
     * @Author: cy
     * @Date: 2022/4/6
     */
    @ApiOperation(value = "老师详情-视频课详情-查询购买学员列表", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/teacher/purchaseStudent", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<PageInfo<VideoLessonPurchaseVo>> selectPurchaseStudent(@Validated(SelectGroup.class) @RequestBody VideoLessonSearch search) {
        return succeed(PageUtil.pageInfo(lessonGroupService.selectPurchaseStudent(PageUtil.getPage(search), search)));
    }

    /**
     * @Description: 老师详情-视频课详情-教学计划
     * @Author: cy
     * @Date: 2022/4/6
     */
    @ApiOperation(value = "老师详情-视频课详情-教学计划", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/teacher/plan", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<PageInfo<VideoLessonGroupDetailVo>> page(@Validated(SelectGroup.class) @RequestBody VideoLessonSearch search) {
        return succeed(PageUtil.pageInfo(videoLessonGroupDetailService.selectPage(PageUtil.getPage(search), search)));
    }

    /**
     * @Description: 审核-视频课组列表
     * @Author: cy
     * @Date: 2022/4/20
     */
    @ApiOperation(value = "审核-视频课组列表", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/queryGroupList", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<PageInfo<VideoLessonAuthGroup>> queryGroupList(@RequestBody VideoGroupSearch search) {
        return succeed(PageUtil.pageInfo(lessonGroupService.queryGroupList(PageUtil.getPage(search), search)));
    }

    @ApiOperation(value = "审核-视频课组历史记录列表", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/queryGroupHistoryList", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<PageInfo<VideoLessonAuthGroup>> queryGroupHistoryList(@RequestBody VideoGroupSearch search) {
        return succeed(PageUtil.pageInfo(lessonGroupService.queryGroupHistoryList(PageUtil.getPage(search), search)));
    }

    @ApiOperation(value = "老师详情-视频课详情-推荐/赠送内容", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/queryRelationMusicAlbum", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<PageInfo<CourseRelationWrapper.MusicAlbumInfo>> queryRelationMusicAlbum(@RequestBody CourseRelationVo.CourseRelationQuery query) {

        query.setCourseType(CourseTypeEnum.VIDEO);
        IPage<CourseRelationWrapper.MusicAlbumInfo> iPage = lessonGroupService
                .queryRelationMusicAlbum(PageUtil.getPage(query), CourseRelationQueryInfo.from(query.jsonString()));
        return succeed(PageUtil.pageInfo(iPage));

    }


    /**
     * @Description: 审核-根据视频课组id查视频课详情
     * @Author: cy
     * @Date: 2022/4/20
     */
    @ApiOperation(value = "审核-根据视频课组id查视频课详情")
    @GetMapping("/authGroupDetil")
    public HttpResponseResult<VideoLessonAuthGroup> authGroupDetil(
            @ApiParam(value = "当前视频组id", required = true) @RequestParam(value = "videoGroupId") Long videoGroupId) {
        return succeed(lessonGroupService.authGroupDetil(videoGroupId));
    }

    /**
     * @Description: 审核-根据视频课组id查视频课详情
     * @Author: cy
     * @Date: 2022/4/20
     */
    @ApiOperation(value = "审核-根据视频课组id查视频课详情")
    @GetMapping("/queryLessonInfo")
    public HttpResponseResult<List<VideoLessonGroupDetail>> queryLessonInfo(Integer groupId) {
        QueryWrapper<VideoLessonGroupDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("video_lesson_group_id_", groupId);
        return succeed(videoLessonGroupDetailService.list(queryWrapper));
    }



    /**
     * @Description: 审核-修改视频课组审核状态
     * @Author: cy
     * @Date: 2022/4/25
     */
    @ApiOperation(value = "审核-修改视频课组审核状态", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/updateGroup", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<Object> update(@Validated @RequestBody VideoLessonExamineVo examineVo) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        examineVo.setAuditId(user.getId());
        examineVo.setAuditName(user.getUsername());
        lessonGroupService.updateGroup(examineVo);
        return succeed();
    }

    /**
     * @Description: 更新上架状态
     * @Author: cy
     * @Date: 2022/4/25
     */
    @ApiOperation(value = "更新上架状态", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/updateShelves", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<Object> updateShelves(@Validated @RequestBody VideoLessonShelvesVo shelvesVo) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if (shelvesVo.getShelvesFlag() == 0 && StringUtil.isEmpty(shelvesVo.getShelvesReason())) {
            return failed( "下架必须要有下架原因");
        }
        VideoLessonGroup videoLessonGroup = lessonGroupService.getById(shelvesVo.getId());
        if (videoLessonGroup.getAuditStatus() ==null || !videoLessonGroup.getAuditStatus().equals(AuthStatusEnum.PASS)) {
            return failed("审核通过才能上架");
        }

        shelvesVo.setShelvesId(user.getId());
        if (shelvesVo.getShelvesFlag() == 1) {
            shelvesVo.setShelvesTime(new Date());
        }
        shelvesVo.setClientType(ClientEnum.SYSTEM);
        lessonGroupService.updateShelves(shelvesVo);
        return succeed();
    }

    @ApiOperation(value = "首页-视频课统计")
    @GetMapping("/countVideoGroup")
    public HttpResponseResult<CountVideoGroupVo> countVideoGroup() {
        return succeed(lessonGroupService.countVideoGroup());
    }


    /**
     * @Description: 新增视频课组&视频课
     * @Author: cy
     * @Date: 2022/4/1
     */
    @ApiOperation(value = "新增视频课组&视频课", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/add", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<Object> add( @RequestBody VideoLessonVo lessonVo) {

        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        lessonVo.setUserBy(user.getId());
        if (lessonVo.getLessonGroup().getTeacherId() == null) {
            return failed("请设置所属老师");
        }
        SysUser sysUser = sysUserFeignService.queryUserById(lessonVo.getLessonGroup().getTeacherId());

        if (sysUser == null || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        if (lessonVo.getLessonGroup().getDraftFlag().equals(YesOrNoEnum.NO) && CollectionUtils.isEmpty(lessonVo.getLessonList())) {
            return failed("课程不能为空");
        }
        Long add = lessonGroupService.add(lessonVo, sysUser);
        return succeed(add);
    }

    /**
     * @Description: 修改视频课组&视频课
     * @Author: cy
     * @Date: 2022/4/7
     */
    @ApiOperation(value = "修改视频课组&视频课", httpMethod = "POST", consumes = "application/json", produces = "application/json")
    @PostMapping(value = "/update", consumes = "application/json", produces = "application/json")
    public HttpResponseResult<Object> update(@RequestBody VideoLessonVo lessonVo) {

        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        lessonVo.setUserBy(user.getId());

        if (lessonVo.getLessonGroup().getTeacherId() == null) {
            return failed("请设置所属老师");
        }
        SysUser sysUser = sysUserFeignService.queryUserById(lessonVo.getLessonGroup().getTeacherId());

        if (sysUser == null || sysUser.getId() == null) {
            return failed("用户信息获取失败");
        }
        if (lessonVo.getLessonGroup().getDraftFlag().equals(YesOrNoEnum.NO) && CollectionUtils.isEmpty(lessonVo.getLessonList())) {
            return failed("课程不能为空");
        }
        return succeed(lessonGroupService.update(lessonVo, sysUser));
    }



    /**
     * 置顶
     */
    @PostMapping("/top/{id}")
    @ApiOperation(value = "置顶", notes = "传入id")
    public HttpResponseResult<Boolean> top(@ApiParam(value = "视频课编号", required = true)  @PathVariable Long id) {
        if (StringUtil.isEmpty(id)) {
            return failed("参数不能为空");
        }
        return status(lessonGroupService.top(id));
    }



    /**
     * @Description: 根据组id查询视频课&视频课组
     * @Author: cy
     * @Date: 2022/4/2
     */
    @ApiOperation(value = "根据组id查询视频课&视频课组")
    @GetMapping(value = "/selectVideoLesson")
    public HttpResponseResult<VideoLessonStudentVo> selectVideoLesson(@NotNull(message = "视频组id不能为空") Long groupId) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null || sysUser.getId() == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }

        VideoLessonStudentVo videoLessonStudentVo = lessonGroupService.selectVideoLesson(groupId);
        if (videoLessonStudentVo.getLessonGroup().getTeacherId().equals(sysUser.getId())) {
            videoLessonStudentVo.setMyself(true);
        } else {
            videoLessonStudentVo.setMyself(false);
        }
        if (videoLessonStudentVo.getLessonGroup() != null && sysUser.getId().equals(videoLessonStudentVo.getLessonGroup().getId())) {
            videoLessonStudentVo.setAlreadyBuy(true);
        } else {
            videoLessonStudentVo.setAlreadyBuy(false);
        }
        return succeed(videoLessonStudentVo);
    }

    /**
     * 设置排序值 和加精
     */
    @PostMapping("/adjust")
    @ApiOperation(value = "视频课调整排序")
    public HttpResponseResult<Boolean> adjust(@RequestBody @Valid AdjustModel model) {

        VideoLessonGroup videoLessonGroup = new VideoLessonGroup();
        videoLessonGroup.setId(model.getId());
        videoLessonGroup.setSortNumber(model.getSort());

        lessonGroupService.updateById(videoLessonGroup);

        return succeed();
    }

}
