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.dto.ClassDateAdjustDto;
import com.ym.mec.biz.dal.dto.VipGroupApplyDto;
import com.ym.mec.biz.dal.entity.StudentApplyRefunds;
import com.ym.mec.biz.dal.entity.VipGroup;
import com.ym.mec.biz.dal.page.VipGroupAttendanceQueryInfo;
import com.ym.mec.biz.dal.page.VipGroupQueryInfo;
import com.ym.mec.biz.dal.page.VipGroupSalaryQueryInfo;
import com.ym.mec.biz.dal.page.VipGroupTeachingRecordQueryInfo;
import com.ym.mec.biz.service.CourseScheduleService;
import com.ym.mec.biz.service.VipGroupService;
import com.ym.mec.common.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Objects;

/**
 * @Author Joburgess
 * @Date 2019/9/21
 */
@Api(tags = "vip课")
@RequestMapping("vipGroupManage")
@RestController
public class VipGroupManageController extends BaseController {

    @Autowired
    private VipGroupService vipGroupService;
    @Autowired
    private CourseScheduleService scheduleService;
    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "vip课申请")
    @PostMapping("/vipGroupApply")
    public Object vipGroupApply(@RequestBody VipGroupApplyDto vipGroupApplyDto){
        SysUser sysUser = sysUserFeignService.queryUserById(vipGroupApplyDto.getVipGroupApplyBaseInfo().getUserId());
        if(Objects.isNull(sysUser)){
            return failed("请指定指导老师!");
        }
        vipGroupApplyDto.getVipGroupApplyBaseInfo().setOrganIdList(sysUser.getOrganId().toString());
        vipGroupService.createVipGroup(vipGroupApplyDto);
        return succeed();
    }

    @ApiOperation(value = "全查询")
    @GetMapping("/queryAll")
    public Object queryAll(VipGroupQueryInfo queryInfo) {
        return succeed(vipGroupService.findVipGroups(queryInfo));
    }

    @ApiOperation(value = "获取小课学员")
    @GetMapping(value = "/findVipGroupStudents")
    public Object findVipGroupStudents(VipGroupQueryInfo queryInfo){
        return succeed(vipGroupService.findVipGroupStudents(queryInfo));
    }

    @ApiOperation(value = "vip课详情")
    @GetMapping("/getVipGroupDetail")
    public Object getVipGroupDetail(Long vipGroupId){
        return succeed(vipGroupService.getVipGroupDetail(vipGroupId));
    }

    @ApiOperation(value = "获取vip课考勤记录")
    @PostMapping("/getVipGroupAttendances")
    public Object getVipGroupAttendances(@RequestBody VipGroupAttendanceQueryInfo queryInfo){
        return succeed(vipGroupService.findVipGroupAttendances(queryInfo));
    }

    @ApiOperation(value = "退课申请")
    @PostMapping("/applyRefundForStudent")
    public Object applyRefundForStudent(Long vipGroupId,Long studentId){
        vipGroupService.applyRefund(vipGroupId,studentId);
        return succeed();
    }

    @ApiOperation(value = "退课申请审核")
    @PostMapping("/applyRefundAudit")
    public Object applyRefundAudit(StudentApplyRefunds studentApplyRefunds){
        vipGroupService.applyRefundAudit(studentApplyRefunds);
        return succeed();
    }

    @ApiOperation(value = "获取VIP课教学记录")
    @GetMapping("/findVipGroupTeachingRecord")
    public Object findVipGroupTeachingRecord(VipGroupTeachingRecordQueryInfo queryInfo){
        return succeed(vipGroupService.findVipGroupTeachingRecord(queryInfo));
    }

    @ApiOperation(value = "获取vip课基本信息")
    @GetMapping("/findTeachingRecordBaseInfo")
    public Object findTeachingRecordBaseInfo(Long vipGroupId){
        return succeed(vipGroupService.findTeachingRecordBaseInfo(vipGroupId));
    }

    @ApiOperation("课酬总费用")
    @PostMapping("/getVipGroupCostCount")
    public Object getVipGroupCostCount(VipGroup vipGroup){
        Map results = vipGroupService.countVipGroupPredictFee(vipGroup, vipGroup.getOnlineClassesUnitPrice(), vipGroup.getOfflineClassesUnitPrice());
        return succeed(results);
    }

    @ApiOperation(value = "停止vip课")
    @PostMapping("/stopVipGroup")
    public Object stopVipGroup(Long vipGroupId){
        if(Objects.isNull(vipGroupId)){
            return failed(HttpStatus.FORBIDDEN,"请指定vip课");
        }
        vipGroupService.stopGroup(vipGroupId);
        return succeed();
    }

    @ApiOperation(value = "获取vip课财务信息")
    @GetMapping("/findVipGroupSalarys")
    public Object findVipGroupSalarys(VipGroupSalaryQueryInfo queryInfo){
        return succeed(vipGroupService.findVipGroupSalarys(queryInfo));
    }

    @ApiOperation(value = "获取当前课程上课学员")
    @GetMapping("/findVipGroupAttendanceStudents")
    public Object findVipGroupStudents(Long courseScheduleId){
        return succeed(vipGroupService.findVipGroupAttendanceStudents(courseScheduleId));
    }

    @ApiOperation(value = "课时调整")
    @PostMapping(value = "/classStartDateAdjust")
    public Object classStartDateAdjust(ClassDateAdjustDto classDateAdjustDto){
        if(Objects.isNull(classDateAdjustDto.getId())){
            return failed(HttpStatus.FORBIDDEN, "请指定课程!");
        }
        scheduleService.classStartDateAdjust(classDateAdjustDto);
        return succeed();
    }

}
