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

import cn.com.qmth.examcloud.core.questions.base.Model;
import cn.com.qmth.examcloud.core.questions.dao.PaperStructRepo;
import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetailStruct;
import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetailUnitStruct;
import cn.com.qmth.examcloud.core.questions.dao.entity.PaperStruct;
import cn.com.qmth.examcloud.core.questions.dao.entity.dto.PaperDetailUnitStructDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

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

/**
 * @author weiwenhai
 * @date 2017.9.7
 * @company qmth
 * @describle 批量跟新线上精确试卷结构工具类
 */

@Service("updatePaperStructService")
public class UpdatePaperStruct {
    private static final Logger log = LoggerFactory.getLogger(UpdatePaperStruct.class);

    @Autowired
    PaperStructRepo paperStructRepo;

    @Autowired
    MongoTemplate mongoTemplate;

    public void updatePaperStruct(String orgId) {
        //根据orgId查询所有精确试卷结构
        //Query query = new Query();
        /*query.addCriteria(Criteria.where("orgId").is(orgId));
        query.addCriteria(Criteria.where("type").is("EXACT"));
        List<PaperStruct> paperStructList = this.mongoTemplate.find(query, PaperStruct.class);*/
        PaperStruct paSruct = Model.of(paperStructRepo.findById("5a03ca4e57853f6b94c8e526"));
        List<PaperStruct> paperStructList = new ArrayList<PaperStruct>();
        paperStructList.add(paSruct);
        int i = 0;
        if (paperStructList != null && paperStructList.size() > 0) {
            for (PaperStruct paperStruct : paperStructList) {
                //获取大题集合
                List<PaperDetailStruct> paperDetailStructs = paperStruct.getPaperDetailStructs();
                for (PaperDetailStruct paperDetailStruct : paperDetailStructs) {
                    //获取小题类型统计
                    List<PaperDetailUnitStructDto> unitStructs = paperDetailStruct.getUnitStructs();
                    for (PaperDetailUnitStructDto dto : unitStructs) {
                        dto.setPublicDifficulty(0);
                        dto.setPublicMedium(dto.getCount());
                        dto.setPublicSimple(0);
                        dto.setNoPublicDifficulty(0);
                        dto.setNoPublicMedium(0);
                        dto.setNoPublicSimple(0);
                    }
                    List<PaperDetailUnitStruct> units = paperDetailStruct.getPaperDetailUnitStructs();
                    for (PaperDetailUnitStruct unitStruct : units) {
                        unitStruct.setDifficulty("中");
                        unitStruct.setPublicity(true);
                        unitStruct.setPropertyGroup(buildGroup(unitStruct));
                    }
                }
                i++;
            }
        }
        log.debug("更新精确试卷结构的数量为：" + i + "------个");
        paperStructRepo.saveAll(paperStructList);
        log.debug("更新成功！----");
    }

    /**
     * 构建精确组建筛选条件
     *
     * @param unitStruct
     * @return
     */
    private String buildGroup(PaperDetailUnitStruct unitStruct) {
        return String.valueOf(unitStruct.getPublicity()) + "-" + unitStruct.getDifficulty();
    }
}
