package com.yonge.cooleshow.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.dayaedu.cbs.openfeign.client.CoursewareFeignService;
import com.dayaedu.cbs.openfeign.wrapper.courseware.CbsLessonCoursewareDetailWrapper;
import com.dayaedu.cbs.openfeign.wrapper.courseware.CbsLessonCoursewareWrapper;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.PageUtil;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.service.TenantAlbumMusicService;
import com.yonge.cooleshow.biz.dal.wrapper.LessonCoursewareWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 专辑表 web 控制层
 * @author yzp
 * @date 2022-03-26 00:21:46
 * @version v1.0
 **/
@RestController
@RequestMapping("${app-config.url.admin:}/tenantAlbumMusic")
@Api(tags = "机构专辑曲目 API接口")
public class TenantAlbumSheetController extends BaseController {

    @Autowired
    private CoursewareFeignService coursewareFeignService;

    @Autowired
    private TenantAlbumMusicService tenantAlbumMusicService;

    @Autowired
    private SysUserFeignService sysUserFeignService;


    @ApiOperation(value = "获取内容平台课件教材")
    @PostMapping("/queryPage")
    public HttpResponseResult<PageInfo<CbsLessonCoursewareWrapper.LessonCourseware>> queryPage(@RequestBody CbsLessonCoursewareWrapper.LessonCoursewareQuery query) throws Exception {
//        query.setEnableFlag(true);
        return succeed(PageUtil.pageInfo(coursewareFeignService.lessonCoursewarePage(query).feignData()));
    }

    @ApiOperation(value = "获取课件详情")
    @PostMapping("/getLessonCoursewareDetail")
    public HttpResponseResult<LessonCoursewareWrapper.LessonCourseware> getLessonCoursewareDetail(@RequestBody @Valid LessonCoursewareWrapper.LessonCourseQuery query){
        query.setClient(ClientEnum.SYSTEM);
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null || null == sysUser.getId()) {
            return failed("用户信息获取失败");
        }
        query.setUserId(sysUser.getId());
        LessonCoursewareWrapper.LessonCourseware lessonCourseware = tenantAlbumMusicService.getLessonCoursewareDetail(query);
        return succeed(lessonCourseware);
    }

    @ApiOperation(value = "获取课件关联的课程列表")
    @GetMapping("/getLessonCoursewareCourseList/{id}")
    public HttpResponseResult<List<LessonCoursewareWrapper.CourseScheduleCoursewareDetail>> getLessonCoursewareCourseList(@PathVariable("id") Long id){
        List<CbsLessonCoursewareWrapper.CourseScheduleCoursewareDetail> courseScheduleCoursewareDetails = coursewareFeignService.coursewareDetail(id).feignData();
        if (CollectionUtils.isNotEmpty(courseScheduleCoursewareDetails)) {
            String jsonString = JSONObject.toJSONString(courseScheduleCoursewareDetails);
            List<LessonCoursewareWrapper.CourseScheduleCoursewareDetail> courseScheduleCoursewareDetailList = JSONObject.parseArray(jsonString, LessonCoursewareWrapper.CourseScheduleCoursewareDetail.class);
            return succeed(courseScheduleCoursewareDetailList);
        }
        return succeed(new ArrayList<>());
    }

    @ApiOperation(value = "获取课程详情")
    @GetMapping("/getLessonCourseDetail/{id}")
    public HttpResponseResult<CbsLessonCoursewareDetailWrapper.LessonCoursewareDetail> getLessonCourseDetail(@PathVariable("id") String id){
        return succeed(coursewareFeignService.lessonCoursewareDetailDetail(Long.parseLong(id)).feignData());
    }

}
