package com.ym.mec.student.controller;

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

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.ImGroupNoticeService;
import com.ym.mec.biz.service.StudentManageService;
import com.ym.mec.biz.service.SubjectService;
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.common.filters.TenantIdThreadLocal;
import com.ym.mec.common.page.QueryInfo;
import com.ym.mec.util.upload.UploadUtil;

@Api(tags = "学生管理")
@RestController
public class StudentManageController extends BaseController {

    @Autowired
    private SysSuggestionService suggestionService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private SysUserCashAccountService sysUserCashAccountService;
    @Autowired
    private SysUserCashAccountDetailService sysUserCashAccountDetailService;
    @Autowired
    private ClassGroupService classGroupService;
    @Autowired
    private ImGroupNoticeService imGroupNoticeService;
    @Autowired
    private SysUserBankCardService sysUserBankCardService;
    @Autowired
    private StudentManageService studentManageService;
    @Autowired
    private SubjectService subjectService;

    private final static Logger LOGGER = LoggerFactory.getLogger(StudentManageController.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.setClientType("STUDENT");
        sysSuggestion.setUserId(sysUser.getId().longValue());
        suggestionService.insert(sysSuggestion);
        return succeed();
    }

    @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(value = "/queryNoticePage")
    public Object queryNoticePage(QueryInfo queryInfo){
        return succeed(imGroupNoticeService.queryPage(queryInfo));
    }

    @ApiOperation(value = "根据群编号，获取用户群名片")
    @GetMapping("classGroup/getUserInfoWithUserId")
    public Object getUserInfoWithUserId(Integer groupId,Integer userId) {
        if (groupId == null || userId == null) {
            return failed("参数校验错误");
        }
        return succeed(classGroupService.getUserInfoWithUserId(groupId,userId));
    }

    @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(value = "user/update", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Object update(SysUser sysUser) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user != null) {
            Date date = new Date();
            sysUser.setId(user.getId());
            sysUser.setUpdateTime(date);
            studentManageService.updateUser(sysUser);
            return succeed();
        }
        return failed("获取用户失败");
    }

    /**
     * 学员注册激活比例列表
     * @return
     */
    @GetMapping(value = "studentManage/queryStudentPer")
    public Object queryStudentPer() {
        return succeed(studentManageService.queryStudentPer());
    }


    @ApiOperation(value = "获取子集科目列表")
    @GetMapping("subject/findSubSubjects")
    public Object findSubSubjects(Integer tenantId){
        if(tenantId == null){
            tenantId = TenantIdThreadLocal.get();
        }
        return succeed(subjectService.findSubSubjects(tenantId));
    }

}
