package com.ym.mec.web.controller;

import com.ym.mec.biz.dal.dto.HighClassGroupDto;
import com.ym.mec.biz.dal.entity.ClassGroup;
import com.ym.mec.biz.dal.entity.ClassGroupTeacherMapper;
import com.ym.mec.biz.dal.enums.SalarySettlementTypeEnum;
import com.ym.mec.biz.service.ClassGroupService;
import com.ym.mec.biz.service.ClassGroupTeacherMapperService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.QueryInfo;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RequestMapping("classGroup")
@Api(tags = "班级服务")
@RestController
public class ClassGroupController extends BaseController {

    @Autowired
    private ClassGroupService classGroupService;
    @Autowired
    private ClassGroupTeacherMapperService classGroupTeacherMapperService;

    @ApiOperation(value = "新增单技班班级")
    @PostMapping("/add")
    public Object add(@RequestBody ClassGroup classGroup) throws Exception {
        return succeed(classGroupService.addClassGroup(classGroup));
    }

    @ApiOperation(value = "新增合奏班")
    @PostMapping("/addMixClass")
    public Object addMixClass(@ApiParam(value = "乐团编号", required = true) @RequestParam String musicGroupId,
                              @ApiParam(value = "班级名称", required = true) String name,
                              @ApiParam(value = "班级编号,号分割", required = true) String classGroupIds) throws Exception {
        return succeed(classGroupService.addMixClassGroup(musicGroupId, name, classGroupIds));
    }

    @ApiOperation(value = "新增提高班")
    @PostMapping("/addHighClass")
    public Object addHighClass(@ApiParam(value = "乐团提高班json", required = true) @RequestParam List<HighClassGroupDto> highClassGroupDtoList) throws Exception {
        if (highClassGroupDtoList.size() <= 0) {
            return failed("参数不合法");
        }
        return succeed(classGroupService.addHighClassGroup(highClassGroupDtoList));
    }

    @ApiOperation(value = "删除单技班")
    @PostMapping("/delSingle")
    public Object delSingle(Integer classGroupId) {
        classGroupService.delete(classGroupId);
        return succeed();
    }

    @ApiOperation(value = "删除合奏班")
    @PostMapping("/delMix")
    public Object delMix(Integer classGroupId) {
        classGroupService.delete(classGroupId);
        return succeed();
    }

    @ApiOperation(value = "修改班级")
    @PostMapping("/update")
    public Object update(ClassGroup classGroup) {
        classGroup.setUpdateTime(new Date());
        classGroupService.update(classGroup);
        return succeed();
    }

    @ApiOperation(value = "分页查询班级列表")
    @GetMapping("/queryPage")
    public Object queryPage(QueryInfo queryInfo) {
        return succeed(classGroupService.queryPage(queryInfo));
    }

    @ApiOperation(value = "合奏班相关班级获取")
    @GetMapping("/findClassGroupAboutMix")
    public HttpResponseResult findClassGroupAboutMix(@ApiParam(value = "乐团编号", required = true) @RequestParam String musicGroupId,
                                         @ApiParam(value = "班级编号", required = false) Integer mixClassGroupId) {
        return succeed(classGroupService.findClassGroup(musicGroupId, mixClassGroupId));
    }

    @ApiOperation(value = "乐团单技班列表")
    @GetMapping("/findMusicGroupClass")
    public HttpResponseResult findMusicGroupClass(@ApiParam(value = "乐团编号", required = true) @RequestParam String musicGroupId) {
        return succeed(classGroupService.findAllNormalClassGroupByMusicGroupId(musicGroupId));
    }

    @ApiOperation(value = "获取未分班的单技班列表")
    @GetMapping("/findNoClassSubjects")
    public HttpResponseResult findNoClassSubjects(@ApiParam(value = "乐团编号", required = true) @RequestParam String musicGroupId) {
        return succeed(classGroupService.findNoClassSubjects(musicGroupId));
    }

    @ApiOperation(value = "乐团合奏班列表")
    @GetMapping("/findMixMusicGroupClass")
    public HttpResponseResult findMixMusicGroupClass(@ApiParam(value = "乐团编号", required = true) @RequestParam String musicGroupId) {
        return succeed(classGroupService.findAllMixClassGroupByMusicGroupId(musicGroupId));
    }

    @ApiOperation(value = "乐团所有班列表")
    @GetMapping("/findAllClassGroupByMusicGroup")
    public HttpResponseResult findAllClassGroupByMusicGroup(@ApiParam(value = "乐团编号", required = true) @RequestParam String musicGroupId) {
        return succeed(classGroupService.findAllClassGroupByMusicGroup(musicGroupId));
    }

    @ApiOperation(value = "乐团班级老师设置")
    @PostMapping("/addClassGroupTeacher")
    @ApiParam(value = "乐团班级老师json", required = true)
    public HttpResponseResult addClassGroupTeacher(@RequestBody List<ClassGroupTeacherMapper> classGroupTeacherMapperList) {
        if (classGroupTeacherMapperList.size() <= 0) {
            return failed("参数不合法");
        }
        return succeed(classGroupTeacherMapperService.classGroupTeachersInsert(classGroupTeacherMapperList));
    }

    @ApiOperation(value = "获取乐团班级老师")
    @GetMapping("/findMusicGroupClassTeacher")
    public HttpResponseResult findMusicGroupClassTeacher(@ApiParam(value = "乐团编号", required = true) @RequestParam String musicGroupId) {
        return succeed(classGroupService.getClassGroupAndTeachers(musicGroupId,"NORMAL,MIX"));
    }

    @ApiOperation(value = "获取乐团班级老师课酬")
    @GetMapping("/findMusicGroupClassTeacherSalary")
    @ApiImplicitParams({@ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "type", value = "结算类型（1-基准课酬，4-梯度课酬）", required = true, dataType = "Integer")})
    public HttpResponseResult findMusicGroupClassTeacherSalary(String musicGroupId, SalarySettlementTypeEnum type) {
        try {
            return succeed(classGroupService.getClassGroupAndTeacherSalary(musicGroupId, type));
        } catch (Exception e) {
            return failed(e.getMessage());
        }
    }

    @ApiOperation(value = "乐团班级老师课酬确认")
    @PostMapping("/setClassGroupTeacherSalary")
    @ApiParam(value = "乐团班级老师<包含相应课酬>json", required = true)
    public Object setClassGroupTeacherSalary(@RequestBody List<ClassGroupTeacherMapper> classGroupTeacherMapperList) throws Exception {
        if (classGroupTeacherMapperList.size() <= 0) {
            return failed("参数不合法");
        }
        return succeed(classGroupTeacherMapperService.classGroupTeacherMapperUpdate(classGroupTeacherMapperList));
    }

    @ApiOperation(value = "根据群编号，获取群组所有成员基本信息")
    @GetMapping("/findGroupUsers")
    public Object findGroupUsers(String groupId) {
        if (StringUtils.isEmpty(groupId)) {
            return failed("参数校验错误");
        }
        return succeed(classGroupService.findGroupUsers(groupId));
    }

    @ApiOperation(value = "乐团班级设置，成团确认")
    @PostMapping("/addMusicGroupTeam")
    @ApiImplicitParams({@ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "teacherId", value = "老师编号", required = true, dataType = "Integer")})
    public Object addMusicGroupTeam(Integer teacherId,String musicGroupId) throws Exception {
        if(teacherId == null || StringUtils.isEmpty(musicGroupId)){
            return failed("参数校验错误");
        }
        classGroupService.addMusicGroupTeam(teacherId,musicGroupId);
        return succeed();
    }


    @ApiOperation(value = "合并班级")
    @PostMapping("/mergeClassGroup")
    @ApiImplicitParams({@ApiImplicitParam(name = "classGroupIds", value = "班级编号,号分割", required = true, dataType = "String")})
    public HttpResponseResult mergeClassGroup(String classGroupIds) throws Exception {
        //软删除班级

        return succeed();
    }

}