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.entity.Employee;
import com.ym.mec.biz.dal.enums.GroupType;
import com.ym.mec.biz.dal.page.PracticeGroupQueryInfo;
import com.ym.mec.biz.dal.page.StudentBuyPracticeQueryInfo;
import com.ym.mec.biz.service.CourseScheduleEvaluateService;
import com.ym.mec.biz.service.GroupClassService;
import com.ym.mec.biz.service.PracticeGroupService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @Author Joburgess
 * @Date 2019/9/21
 */
@Api(tags = "陪练课")
@RequestMapping("practiceGroupManage")
@RestController
public class PracticeGroupManageController extends BaseController {

    @Autowired
    private PracticeGroupService practiceGroupService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private GroupClassService groupService;
    @Autowired
    private CourseScheduleEvaluateService courseScheduleEvaluateService;

    @ApiOperation(value = "全查询")
    @GetMapping("/queryAll")
    @PreAuthorize("@pcs.hasPermissions('practiceGroupManage/queryAll')")
    public Object queryAll(PracticeGroupQueryInfo 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(practiceGroupService.findPracticeGroups(queryInfo));
    }

    @ApiOperation(value = "获取陪练课课程计划")
    @GetMapping(value = "/findPracticeGroupCourseSchedules")
    @PreAuthorize("@pcs.hasPermissions('practiceGroupManage/findPracticeGroupCourseSchedules')")
    public Object findPracticeGroupCourseSchedules(PracticeGroupQueryInfo queryInfo) {
        return succeed(practiceGroupService.findPracticeGroupCourseSchedules(queryInfo));
    }

    @ApiOperation(value = "修改网管课备注")
    @PostMapping(value = "/updateMemo")
    @PreAuthorize("@pcs.hasPermissions('practiceGroupManage/updateMemo')")
    public Object updateMemo(Long groupId,String memo) {
        practiceGroupService.updateMemo(groupId,memo);
        return succeed();
    }

    @ApiOperation(value = "获取陪练课可预约声部")
    @GetMapping(value = "/getPracticeApplySubjects")
    @PreAuthorize("@pcs.hasPermissions('practiceGroupManage/getPracticeApplySubjects')")
    public HttpResponseResult getPracticeApplySubjects(){
        return succeed(practiceGroupService.getPracticeApplySubjects());
    }

    @ApiOperation(value = "关闭课程组")
    @PostMapping(value = "/cancelGroup")
    @PreAuthorize("@pcs.hasPermissions('practiceGroupManage/cancelGroup')")
    public HttpResponseResult cancelGroup(Long groupId, GroupType groupType, BigDecimal refundAmount) {
        groupService.cancelGroup(groupId, groupType, refundAmount);
        return succeed();
    }

    @ApiOperation(value = "评论管理")
    @GetMapping("getReviews")
    @PreAuthorize("@pcs.hasPermissions('practiceGroupManage/getReviews')")
    public Object getReviews(PracticeGroupQueryInfo 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(practiceGroupService.findPracticeGroupReviews(queryInfo));
    }

    @ApiOperation(value = "课程组评论列表")
    @GetMapping("getGroupReviews")
    @PreAuthorize("@pcs.hasPermissions('practiceGroupManage/getGroupReviews')")
    public Object getGroupReviews(Integer groupId) {
        return succeed(courseScheduleEvaluateService.findByGroupId(groupId,1));
    }

    @ApiOperation(value = "评论详情")
    @GetMapping("getReviewsInfo")
    @PreAuthorize("@pcs.hasPermissions('practiceGroupManage/getReviewsInfo')")
    public Object getReviewsInfo(Integer id) {
        return succeed(courseScheduleEvaluateService.findById(id));
    }

    @ApiOperation(value = "网管课购买列表")
    @GetMapping("studentBuys")
    @PreAuthorize("@pcs.hasPermissions('practiceGroupManage/studentBuys')")
    public Object studentBuys(StudentBuyPracticeQueryInfo queryInfo) {
        return succeed(practiceGroupService.studentBuys(queryInfo));
    }
}
