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.dao.PracticeGroupSellPriceDao;
import com.ym.mec.biz.dal.dao.TeacherDao;
import com.ym.mec.biz.dal.dto.*;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.entity.PracticeGroupSellPrice;
import com.ym.mec.biz.dal.entity.Teacher;
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.OrganizationService;
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.*;

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

import static com.ym.mec.biz.dal.enums.PracticeGroupType.CHARGE;

/**
 * @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 OrganizationService organizationService;
    @Autowired
    private GroupClassService groupService;
    @Autowired
    private CourseScheduleEvaluateService courseScheduleEvaluateService;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private PracticeGroupSellPriceDao practiceGroupSellPriceDao;

    @ApiOperation(value = "全查询")
    @GetMapping("/queryAll")
    @PreAuthorize("@pcs.hasPermissions('practiceGroupManage/queryAll')")
    public Object queryAll(PracticeGroupQueryInfo queryInfo) {
        queryInfo.setOrganId(organizationService.getEmployeeOrgan(queryInfo.getOrganId()));
        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(){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        if (sysUser.getTenantId() == null) {
            return failed("用户机构信息异常");
        }
        return succeed(practiceGroupService.getPracticeApplySubjects(sysUser.getTenantId()));
    }

    @ApiOperation(value = "关闭课程组")
    @PostMapping(value = "/cancelGroup")
    @PreAuthorize("@pcs.hasPermissions('practiceGroupManage/cancelGroup')")
    public HttpResponseResult cancelGroup(ReturnFeeDto returnFeeDto) {
        return groupService.cancelGroup(returnFeeDto);
    }

    @ApiOperation(value = "评论管理")
    @GetMapping("getReviews")
    @PreAuthorize("@pcs.hasPermissions('practiceGroupManage/getReviews')")
    public Object getReviews(PracticeGroupQueryInfo queryInfo) {
        queryInfo.setOrganId(organizationService.getEmployeeOrgan(queryInfo.getOrganId()));
        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) {
        queryInfo.setOrganId(organizationService.getEmployeeOrgan(queryInfo.getOrganId()));
        return succeed(practiceGroupService.studentBuys(queryInfo));
    }

    @ApiOperation("课酬总费用")
    @PostMapping("/getPracticeGroupCostCount")
    @PreAuthorize("@pcs.hasPermissions('practiceGroupManage/getPracticeGroupCostCount')")
    public Object getPracticeGroupCostCount(@RequestBody PracticeGroupApplyBaseInfoDto practice){
        Map results = practiceGroupService.countPracticeGroupPredictFee(practice,practice.getUserId());
        return succeed(results);
    }

    @ApiOperation("获取网管课分部价格")
    @RequestMapping("/queryPracticeGroupSellPrice")
    @PreAuthorize("@pcs.hasPermissions('practiceGroupManage/queryPracticeGroupSellPrice')")
    public Object queryPracticeGroupSellPrice(Integer organId){
        return succeed(practiceGroupSellPriceDao.get(organId));
    }

    @ApiOperation(value = "网管课申请")
    @PostMapping("/practiceGroupApply")
    @PreAuthorize("@pcs.hasPermissions('practiceGroupManage/practiceGroupApply')")
    public Object practiceGroupApply(@RequestBody PracticeGroupApplyDto practiceGroupApplyDto){
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        PracticeGroupApplyBaseInfoDto applyBaseInfoDto = practiceGroupApplyDto.getPracticeGroupApplyBaseInfoDto();
        Integer userId = applyBaseInfoDto.getUserId();
        Teacher teacher = teacherDao.get(userId);
        if(Objects.isNull(teacher)){
            return failed("请指定指导老师!");
        }
        if(Objects.isNull(applyBaseInfoDto.getOrganId())){
            SysUser student = teacherDao.getUser(applyBaseInfoDto.getStudentId());
            if(Objects.isNull(student)||Objects.isNull(student.getOrganId())){
                return failed("学员信息异常");
            }
            applyBaseInfoDto.setOrganId(student.getOrganId());
        }
        applyBaseInfoDto.setType(CHARGE);
        applyBaseInfoDto.setMemo("指导老师申请网管课");
        return practiceGroupService.createPracticeGroup(practiceGroupApplyDto);
    }

    @ApiOperation(value = "获取学生指定网管课的剩余课时费用")
    @GetMapping(value = "/getStudentSurplusCourseFee")
    public Object getStudentSurplusCourseFee(Long practiceGroupId){
        return succeed(practiceGroupService.getStudentSurplusCourseFee(practiceGroupId));
    }
}
