package net.educoder.ecsonar.services;

import net.educoder.ecsonar.config.DynamicDataSourceConfig;
import net.educoder.ecsonar.config.DynamicDataSourceContextHolder;
import net.educoder.ecsonar.dao.*;
import net.educoder.ecsonar.model.GameCodes;
import net.educoder.ecsonar.model.GraduationProjectTaskInfoDetail;
import net.educoder.ecsonar.model.Project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author: youys
 * @Date: 2021/12/31
 * @Description:
 */
@Service
public class DbOperateService {

    @Autowired
    private GameCodesDao gameCodesDao;


    @Autowired
    private CeActivityDao ceActivityDao;

    @Autowired
    private ProjectDao projectDao;

    @Autowired
    private TaskInfoDetailDao taskInfoDetailDao;

    @Autowired
    private CloudTaskInfoDetailDao cloudTaskInfoDetailDao;

    @Resource
    private GraduationProjectTaskInfoDetailDao graduationProjectTaskInfoDetailDao;

    /**
     * 根据id查询用户传的code
     *
     * @param id
     * @return
     */
    public GameCodes queryGameCodesById(Long id) {
        DynamicDataSourceContextHolder.setContextKey(DynamicDataSourceConfig.READONLY);
        GameCodes gameCode = gameCodesDao.queryOriginalCodeById(id);
        DynamicDataSourceContextHolder.removeContextKey();
        return gameCode;
    }

    /**
     * 查询任务执行状态
     *
     * @param projectName
     * @return
     */
    public String queryCeActivityStatus(String projectName) {
        Project project = projectDao.findByName(projectName);
        if (project == null) {
            return null;
        }
        return ceActivityDao.queryActivityStatus(project.getProject_uuid());
    }


    public void updateTaskInfoDetail(String id, Integer status) {
        taskInfoDetailDao.updateTaskInfoDetailStatus(id, status);
    }

    public void updateGraduationProjectTaskInfoDetail(String id, Integer status) {
        graduationProjectTaskInfoDetailDao.updateGraduationProjectTaskInfoDetailStatus(id, status);
    }

    public void updateCloudTaskInfoDetail(String id, Integer status) {
        cloudTaskInfoDetailDao.updateCloudTaskInfoDetailStatus(id, status);
    }


}
