package com.ym.mec.web.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.dto.CourseReviewDto;
import com.ym.mec.biz.dal.dto.MusicGroupStudentsDto;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.page.CourseReviewQueryInfo;
import com.ym.mec.biz.dal.page.MusicGroupStudentQueryInfo;
import com.ym.mec.biz.service.CourseReviewService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.util.date.DateUtil;
import com.ym.mec.util.excel.POIUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RequestMapping("courseReview")
@Api(tags = "课程评价")
@RestController
public class CourseReviewController extends BaseController {

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private EmployeeDao employeeDao;

    @Autowired
    private CourseReviewService courseReviewService;


    @ApiOperation(value = "网管课课程评论管理")
    @GetMapping("getPracticeGroup")
    @PreAuthorize("@pcs.hasPermissions('courseReview/getPracticeGroup')")
    public Object getPracticeGroup(CourseReviewQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        if (!sysUser.getIsSuperAdmin()) {
            Employee employee = employeeDao.get(sysUser.getId());
            if (StringUtils.isEmpty(queryInfo.getOrganId())) {
                queryInfo.setOrganId(employee.getOrganIdList());
            } else if (StringUtils.isEmpty(employee.getOrganIdList())) {
                return failed("用户所在分部异常");
            } else {
                List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
                if (!list.containsAll(Arrays.asList(queryInfo.getOrganId().split(",")))) {
                    return failed("非法请求");
                }
            }
        }
        return succeed(courseReviewService.findPracticeGroupReviews(queryInfo));
    }

    @ApiOperation(value = "网管课评论详情")
    @GetMapping("getReviewInfo")
    @PreAuthorize("@pcs.hasPermissions('courseReview/getReviewInfo')")
    public HttpResponseResult getReviewInfo(int id) {
        return succeed(courseReviewService.getReviewInfo(id));
    }

    @ApiOperation(value = "获取老师评价和学生评价")
    @GetMapping("getStuAndTeaReview")
    @PreAuthorize("@pcs.hasPermissions('courseReview/getStuAndTeaReview')")
    public HttpResponseResult getStuAndTeaReview(Long courseId) {
        return succeed(courseReviewService.getStuAndTeaReview(courseId));
    }

    @ApiOperation(value = "VIP课课程评论管理")
    @GetMapping("getVipGroup")
    @PreAuthorize("@pcs.hasPermissions('courseReview/getVipGroup')")
    public Object getVipGroup(CourseReviewQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        if (!sysUser.getIsSuperAdmin()) {
            Employee employee = employeeDao.get(sysUser.getId());
            if (StringUtils.isEmpty(queryInfo.getOrganId())) {
                queryInfo.setOrganId(employee.getOrganIdList());
            } else if (StringUtils.isEmpty(employee.getOrganIdList())) {
                return failed("用户所在分部异常");
            } else {
                List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
                if (!list.containsAll(Arrays.asList(queryInfo.getOrganId().split(",")))) {
                    return failed("非法请求");
                }
            }
        }
        return succeed(courseReviewService.getVipCourseReviews(queryInfo));
    }

}
