package com.ym.mec.teacher.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dto.CashAccountDetail;
import com.ym.mec.biz.dal.entity.SysSuggestion;
import com.ym.mec.biz.dal.entity.SysUserBankCard;
import com.ym.mec.biz.service.ClassGroupService;
import com.ym.mec.biz.service.StudentRegistrationService;
import com.ym.mec.biz.service.SysSuggestionService;
import com.ym.mec.biz.service.SysUserBankCardService;
import com.ym.mec.biz.service.SysUserCashAccountDetailService;
import com.ym.mec.biz.service.SysUserCashAccountService;
import com.ym.mec.biz.service.UploadFileService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.UploadReturnBean;
import com.ym.mec.util.upload.UploadUtil;

@Api(tags = "教师管理")
@RestController
public class TeacherManageController extends BaseController {

    @Autowired
    private SysSuggestionService suggestionService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private SysUserCashAccountService sysUserCashAccountService;
    @Autowired
    private SysUserCashAccountDetailService sysUserCashAccountDetailService;
    @Autowired
    private ClassGroupService classGroupService;
    @Autowired
    private SysUserBankCardService sysUserBankCardService;
    @Autowired
    private StudentRegistrationService studentRegistrationService;

    private final static Logger LOGGER = LoggerFactory.getLogger(TeacherManageController.class);

    @Autowired
    private UploadFileService uploadFileService;

    @PostMapping(value = "uploadFile")
    public Object uploadFile(@ApiParam(value = "上传的文件", required = true) @RequestParam("file") MultipartFile file) {
        try {
            if (file != null && StringUtils.isNotBlank(file.getOriginalFilename())) {
                UploadReturnBean bean = uploadFileService.uploadFile(file.getInputStream(), UploadUtil.getExtension(file.getOriginalFilename()));
                if (bean.isStatus()) {
                    return succeed(bean);
                }
                return failed(bean.getMessage());
            }
        } catch (Exception e) {
            LOGGER.error("上传失败", e);
        }
        return failed("上传失败");
    }

    @ApiOperation(value = "新增建议")
    @PostMapping("suggestion/add")
    public Object add(SysSuggestion sysSuggestion) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("请重新登录");
        }
        sysSuggestion.setUserId(sysUser.getId().longValue());
    	sysSuggestion.setClientType("TEACHER");
        suggestionService.insert(sysSuggestion);
        return succeed();
    }

    @ApiOperation(value = "根据群编号，获取群组基本信息")
    @GetMapping("/course/findGroupById")
    public Object findGroupById(Integer groupId){
        if(null == groupId){
            return failed("参数校验错误");
        }
        return succeed(classGroupService.findGroupById(groupId));
    }

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

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

    @ApiOperation(value = "获取用户账户信息")
    @GetMapping("userCashAccount/get")
    public Object add() {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null){
            return failed("请重新登录");
        }
        return succeed(sysUserCashAccountService.get(sysUser.getId()));
    }

    @ApiOperation(value = "分页查询用户交易明细")
    @GetMapping("userCashAccountDetail/queryPage")
    public Object queryPage(CashAccountDetail queryInfo) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if(user == null || user.getId() != null){
            return failed("请重新登录");
        }
        queryInfo.setUserId(user.getId());
        return succeed(sysUserCashAccountDetailService.queryPage(queryInfo));
    }

    @ApiOperation(value = "新增银行卡信息")
    @PostMapping("userBankCard/add")
    public Object add(SysUserBankCard sysUserBankCard) {
        if(sysUserBankCard == null){
            return failed("参数校验异常");
        }
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null || sysUser.getId() == null){
            return failed("获取用户失败");
        }
        sysUserBankCard.setUserId(sysUser.getId());
        return succeed(sysUserBankCardService.add(sysUserBankCard));
    }

    @ApiOperation(value = "删除银行卡信息")
    @PostMapping("userBankCard/del")
    public Object del(Long id) {
        if(id == null){
            return failed("参数校验失败");
        }
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null || sysUser.getId() == null){
            return failed("获取用户信息失败");
        }
        sysUserBankCardService.del(id,sysUser.getId());
        return succeed();
    }

    @ApiOperation(value = "分页查询银行卡信息列表")
    @GetMapping("userBankCard/queryPage")
    public Object queryPage() {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser == null || sysUser.getId() == null){
            return failed("获取用户失败");
        }
        return succeed(sysUserBankCardService.findByUser(sysUser.getId()));
    }

    @ApiOperation(value = "调剂学生报名专业")
    @PostMapping("studentRegistration/updateSubject")
    public Object batchUpdateSubject(Integer userId,Integer subId,String musicGroupId) throws Exception {
        if (null == userId || subId == null || StringUtils.isEmpty(musicGroupId)) {
            return failed("参数校验异常");
        }
        return succeed(studentRegistrationService.batchUpdateSubject(userId, subId,musicGroupId));
    }

    @ApiOperation(value = "获取学员详情页，包含所在乐团信息")
    @PostMapping("studentManage/detail")
    public Object getStudentDetail(Integer studentId){
        return succeed(studentRegistrationService.getStudentDetail(studentId));
    }
}
