package com.ym.mec.teacher.controller;

import com.ksyun.ks3.dto.PostObjectFormFields;
import com.ym.mec.biz.dal.entity.CooperationOrgan;
import com.ym.mec.biz.dal.enums.SuggestionType;
import com.ym.mec.biz.service.*;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.thirdparty.entity.UploadSign;
import io.swagger.annotations.*;

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.*;
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.common.controller.BaseController;
import com.ym.mec.common.entity.UploadReturnBean;
import com.ym.mec.util.upload.UploadUtil;

import java.util.List;
import java.util.Objects;

@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;
    @Autowired
    private CooperationOrganService cooperationOrganService;

    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("上传失败");
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "bucketName", dataType = "String", value = "为空时默认使用daya"),
            @ApiImplicitParam(name = "fileName", dataType = "String", value = "要上传的文件名称,不包含路径信息"),
            @ApiImplicitParam(name = "postData", dataType = "Map", value = "1.如果使用js sdk上传的时候设置了ACL请设置,例\"acl\":\"public-read\"值要与SDK中一致,没有则删除该项</br>" +
                    "2.提供js sdk中的key值,例\"key\":\"20150115/中文/${filename}\""),
            @ApiImplicitParam(name = "unknowValueField", dataType = "List", value = "对于用户无法确定表单值的放在unknownValueField中(比如有的上传控件会添加一些表单项,但表单项的值可能是随机的)"),
    })
    @ApiOperation(value = "获取上传文件签名", notes = "{\n" +
            "    \"bucketName\":\"\",\n" +
            "    \"filename\":\"test.png\",\n" +
            "    \"postData\":{\n" +
            "        \"acl\":\"public-read\",\n" +
            "        \"key\":\"20150115/中文/${filename}\"\n" +
            "    },\n" +
            "    \"unknowValueField\":[\"test\"]\n" +
            "}")
    @PostMapping("/getUploadSign")
    public HttpResponseResult<PostObjectFormFields> getUploadSign(@RequestBody UploadSign uploadSign) {
        return succeed(uploadFileService.getUploadSign(uploadSign));
    }

    @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");
        if(StringUtils.isEmpty(sysSuggestion.getMobileNo())){
            sysSuggestion.setMobileNo(sysUser.getPhone());
        }
        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));
    }


    @ApiOperation(value = "根据分部id获取合作单位(学校)列表")
    @GetMapping("cooperation/getOrganCooperation")
    public HttpResponseResult<List<CooperationOrgan>> getOrganCooperation(String organId) {
        return succeed(cooperationOrganService.queryByOrganId(organId));
    }
}
