package cn.com.qmth.stmms.ms.admin.service;

import cn.com.qmth.stmms.ms.admin.utils.BultInAccountUtil;
import cn.com.qmth.stmms.ms.commons.config.ImageConfig;
import cn.com.qmth.stmms.ms.core.cache.ParamCache;
import cn.com.qmth.stmms.ms.core.domain.Level;
import cn.com.qmth.stmms.ms.core.domain.MarkSubject;
import cn.com.qmth.stmms.ms.core.domain.ParamSetting;
import cn.com.qmth.stmms.ms.core.domain.Work;
import cn.com.qmth.stmms.ms.core.domain.enums.TrialEnum;
import cn.com.qmth.stmms.ms.core.repository.*;
import cn.com.qmth.stmms.ms.core.vo.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 评卷工作服务
 */
@Service
public class WorkService {

    @Autowired
    private WorkRepo workRepo;

    @Autowired
    private PaperRepo paperRepo;

    @Autowired
    private MarkSubjectRepo markSubjectRepo;

    @Autowired
    private ParamSettingRepo paramSettingRepo;

    @Autowired
    private LevelRepo levelRepo;

    @Resource
    ImageConfig imageConfig;

    @Autowired
    BultInAccountUtil bultInAccountUtil;

    /**
     * 删除评卷工作
     *
     * @param workId
     */
    @Transactional
    public void delete(Long workId) {
        Long paperCount = paperRepo.countByWorkIdAndTest(workId, TrialEnum.DEFAULT.getId());
        if (paperCount > 0) {
            throw new RuntimeException("该评卷工作已有数据，不能删除");
        }
        workRepo.delete(workId);

    }

    /**
     * 创建评卷工作
     *
     * @param work
     */
    @Transactional
    public void save(Work work) {
        Work workTemp = workRepo.findByName(work.getName());
        if(workTemp != null){
            throw new RuntimeException("工作任务名称不能重复");
        }
        workRepo.save(work);
        Subject[] subjects = Subject.values();
        if (!imageConfig.isCustomSubject()) {
            for (int i = 0; i < subjects.length - 1; i++) {
                MarkSubject markSubject = new MarkSubject(subjects[i], work.getId());
                markSubject.setWorkId(work.getId());
                markSubject.setEnable(true);
                markSubjectRepo.save(markSubject);
            }
        } else {
            MarkSubject markSubject = new MarkSubject(work.getName(), Subject.CUSTOM, work.getId());
            markSubject.setWorkId(work.getId());
            markSubject.setEnable(true);
            markSubjectRepo.save(markSubject);
        }

        //新增工作的同时，初始化该工作的全局参数
        ParamSetting paramSetting = ParamSetting.init();
        paramSetting.setWorkId(work.getId());
        paramSettingRepo.save(paramSetting);
        ParamCache.resetParam(paramSetting);

        //默认增加档位
        Level A = new Level(work.getId(), "A",0, 100, 90, Level.LevelType.ADMITED, 95);
        Level B = new Level(work.getId(), "B",1, 89, 85, Level.LevelType.ADMITED, 87);
        Level C = new Level(work.getId(), "C",2, 84, 80, Level.LevelType.ADMITED, 82);
        Level D = new Level(work.getId(), "D",3, 79, 75, Level.LevelType.ADMITED, 77);
        Level E = new Level(work.getId(), "E",4, 74, 70, Level.LevelType.ADMITED, 72);
        Level F = new Level(work.getId(), "F",5, 69, 65, Level.LevelType.ADMITED, 67);
        Level G = new Level(work.getId(), "G",6, 64, 60, Level.LevelType.ADMITED, 62);
        Level H = new Level(work.getId(), "H",7, 59, 55, Level.LevelType.ADMITED, 57);
        Level I = new Level(work.getId(), "I",8, 54, 50, Level.LevelType.ADMITED, 52);
        Level J = new Level(work.getId(), "J",9, 49, 45, Level.LevelType.ADMITED, 47);
        Level K = new Level(work.getId(), "K",10, 44, 0, Level.LevelType.ADMITED, 40);
        List<Level> levels = Arrays.asList(A, B, C, D, E, F, G, H, I, J, K);
        levelRepo.save(levels);

        //生成内置账号
        bultInAccountUtil.createAccout(work);
    }

}
