package com.qmth.themis.business.service.impl;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javax.annotation.Resource;

import com.google.gson.Gson;
import com.qmth.themis.business.dto.cache.TEStudentCacheDto;
import com.qmth.themis.business.util.RedisUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.themis.business.bean.backend.StudentPhotoUploadResponseBean;
import com.qmth.themis.business.config.SystemConfig;
import com.qmth.themis.business.dao.TEStudentMapper;
import com.qmth.themis.business.dto.response.TEStudentDto;
import com.qmth.themis.business.dto.response.TEStudentExamRecordDto;
import com.qmth.themis.business.entity.TEStudent;
import com.qmth.themis.business.service.TEStudentService;
import com.qmth.themis.business.util.OssUtil;
import com.qmth.themis.common.exception.BusinessException;

/**
 * @Description: 学生档案 服务实现类
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/6/25
 */
@Service
public class TEStudentServiceImpl extends ServiceImpl<TEStudentMapper, TEStudent> implements TEStudentService {

    @Resource
    TEStudentMapper teStudentMapper;
    
    @Resource
    SystemConfig systemConfig;

    @Resource
    RedisUtil redisUtil;

    /**
     * 学生档案查询
     *
     * @param iPage
     * @param identity
     * @param name
     * @param enable
     * @return
     */
    @Override
    public IPage<TEStudentDto> studentQuery(IPage<Map> iPage, String identity, String name, Integer enable) {
        return teStudentMapper.studentQuery(iPage, identity, name, enable);
    }

    /**
     * 学生考试记录查询
     *
     * @param iPage
     * @param studentId
     * @param examId
     * @return
     */
    @Override
    public IPage<TEStudentExamRecordDto> studentExamRecordQuery(IPage<Map> iPage, Long studentId, Long examId) {
        return teStudentMapper.studentExamRecordQuery(iPage, studentId, examId);
    }
    
    
    @Transactional
    @Override
    public StudentPhotoUploadResponseBean photoUpload(Long orgId, String identity, MultipartFile file, String md5) {
    	QueryWrapper<TEStudent> wrapper = new QueryWrapper<>();
    	wrapper.lambda().eq(TEStudent::getOrgId, orgId);
    	wrapper.lambda().eq(TEStudent::getIdentity, identity);
    	TEStudent student=this.getOne(wrapper);
    	if(student==null) {
    		throw new BusinessException("学生不存在");
    	}
    	String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf(".")+1).toLowerCase();
        String filePath = "base_photo/"+orgId+"/"+student.getId()+"."+suffix;
        InputStream in = null;
        try {
        	String fileMd5 = DigestUtils.md5Hex(file.getBytes());
        	if (!md5.equals(fileMd5)) {
        		throw new BusinessException("文件md5不一致");
        	}
        	student.setBasePhotoPath(filePath);
        	this.saveOrUpdate(student);
            Gson gson = new Gson();
            TEStudentCacheDto teStudentCacheDto = gson.fromJson(gson.toJson(student), TEStudentCacheDto.class);
            redisUtil.setStudent(student.getId(), teStudentCacheDto);
            in = file.getInputStream();
            OssUtil.ossUploadStream(systemConfig.getOssEnv(3), filePath, in);
            String url = systemConfig.getProperty("aliyun.oss.url") + File.separator + filePath;
            StudentPhotoUploadResponseBean ret=new StudentPhotoUploadResponseBean();
            ret.setPhotoUrl(url);
            return ret;
        } catch (IOException e) {
        	throw new BusinessException("文件读取出错");
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
    }
}
