package cn.com.qmth.examcloud.core.questions.service.impl;

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

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.core.questions.dao.ExamFileRepo;
import cn.com.qmth.examcloud.core.questions.dao.entity.ExamFile;
import cn.com.qmth.examcloud.core.questions.service.ExamFileService;
import cn.com.qmth.examcloud.core.questions.service.bean.dto.ExportPaperInfoModel;
import cn.com.qmth.examcloud.support.filestorage.FileStorageUtil;

/**
 * @author chenken
 * @date 2017年7月13日 下午4:48:02
 * @company QMTH
 */
@Service("examFileService")
public class ExamFileServiceImpl implements ExamFileService {

    @Autowired
    private ExamFileRepo examFileRepo;

    @Autowired
    private MongoTemplate MongoTemplate;

//    @Autowired
//    private SysProperty sysProperty;

    @Override
    public void saveExamFiles(List<ExamFile> examFileList, User accessUser) {
        for (ExamFile examFile : examFileList) {
            saveExamFile(examFile, accessUser);
        }
    }

    @Override
    public List<ExamFile> findExamFileListByExamFile(ExamFile examFile) {
        return examFileRepo.findAll(Example.of(examFile));
    }

    @Override
    public void saveExamFile(ExamFile examFile, User accessUser) {
        examFile.setCreateTime(new Date());
        examFile.setCreateUser(accessUser.getDisplayName());
        examFileRepo.save(examFile);
    }

    @Override
    public List<ExamFile> findExamFileListByExportPaperInfoModel(ExportPaperInfoModel exportPaperInfoModel) {
        Query query = new Query();
        if (StringUtils.isBlank(exportPaperInfoModel.getExamId())) {
            throw new RuntimeException("考试ID不能为空");
        }
        query.addCriteria(Criteria.where("examId").is(exportPaperInfoModel.getExamId()));
        if (StringUtils.isNotBlank(exportPaperInfoModel.getCourseId())) {
            query.addCriteria(Criteria.where("courseId").is(exportPaperInfoModel.getCourseId()));
        }
        List<String> exportContentList = exportPaperInfoModel.getExportContentList();
        //批量导出时，不直接导出试卷结构
		/*if(exportPaperInfoModel.getExportWay()==ExportWay.BATCH){
			exportContentList.remove(ExamFileType.PAPER_STRUCTURE_OBJECTIVE.name());
			exportContentList.remove(ExamFileType.PAPER_STRUCTURE_SUBJECTIVE.name());
		}*/
        if (exportContentList.size() > 0) {
            List<String> examFileTypes = new ArrayList<>();
            for (int i = 0; i < exportContentList.size(); i++) {
                examFileTypes.add(exportContentList.get(i));
            }
            query.addCriteria(Criteria.where("examFileType").in(examFileTypes));
        } else {
            return null;
        }
        return this.MongoTemplate.find(query, ExamFile.class);
    }

    @Override
    public void deleteExamFile(ExamFile examFile) {
//		Example example=Example.of(examFile);
//		List<ExamFile> examFiles = examFileRepo.findAll(example);
        Query query = new Query();
        query.addCriteria(Criteria.where("orgId").is(examFile.getOrgId()));
        query.addCriteria(Criteria.where("examId").is(examFile.getExamId()));
        query.addCriteria(Criteria.where("courseId").is(examFile.getCourseId()));
        List<ExamFile> examFiles = MongoTemplate.find(query, ExamFile.class);
//        UpYun upyun = new UpYun(sysProperty.getBucketName(), sysProperty.getUserName(), sysProperty.getPassword());
        for (ExamFile examFile2 : examFiles) {
            //删除又拍云上的文件
//            upyun.deleteFile(examFile2.getFilePath());
        	//通用存储
        	FileStorageUtil.deleteFile(examFile2.getFilePath());
        }
        examFileRepo.deleteAll(examFiles);
    }

}

