package cn.teammodel.repository;

import cn.teammodel.model.entity.weekDuty.WeekDuty;
import cn.teammodel.model.entity.weekDuty.WeekDutyRecord;
import cn.teammodel.model.vo.admin.DutyNodeRankVo;
import cn.teammodel.model.vo.admin.DutyRankPo;
import cn.teammodel.model.vo.admin.DutyIndexPo;
import cn.teammodel.model.vo.weekDuty.DutyRecordVo;
import com.azure.spring.data.cosmos.core.query.CosmosPageRequest;
import com.azure.spring.data.cosmos.repository.CosmosRepository;
import com.azure.spring.data.cosmos.repository.Query;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author winter
 * @create 2024-01-04 17:23
 */
@Repository
public interface DutyRecordRepository extends CosmosRepository<WeekDutyRecord, String> {
    WeekDutyRecord findByClassIdAndAcademicYearIdAndCode(String classId, String academicYearId, String code);

    @Query(value = "SELECT c.id as recordId, c.classId, c.className, n as info FROM c join n in c.nodes WHERE " +
            "c.code = @code " +
            "AND c.academicYearId = @academicYearId " +
            "AND (IS_NULL(@userId) or n.creatorId = @userId) " +
            "AND (IS_NULL(@classId) or c.classId = @classId) " +
            "AND (IS_NULL(@positive) or n.dutyTreeNode.positive = @positive) " +
            "AND (IS_NULL(@startTime) or n.createTime >= @startTime)  " +
            "AND (IS_NULL(@endTime) or n.createTime <= @endTime)")
    List<DutyRecordVo> findRecordsByConditions(String code, String academicYearId, String userId, String classId, Boolean positive, Long startTime, Long endTime);

    @Query("select n.id as dutyRecordNodeId, n.dutyTreeNode.id as dutyTreeNodeId, n.dutyTreeNode.name as dutyTreeNodeName, n.dutyTreeNode.score as dutyTreeNodeScore, c.classId, n.creatorId, n.createTime from School as c join n in c.nodes where c.academicYearId = @academicYearId and c.code = @code")
    Slice<DutyIndexPo> findAllByAcademicYearId(String code, String academicYearId, CosmosPageRequest pageRequest);

    @Query("select SUM(n.dutyTreeNode.score) as score, c.classId as id from School as c join n in c.nodes where " +
            "c.academicYearId = @academicYearId and " +
            "c.code = @code and " +
            "(IS_NULL(@startTime) or n.createTime >= @startTime) and " +
            "(IS_NULL(@endTime) or n.createTime <= @endTime) " +
            "group by c.classId")
    List<DutyRankPo> classRank(String code, String academicYearId, Long startTime, Long endTime);

    @Query("select count(n.creatorId) as score, n.creatorId as id from School as c join n in c.nodes where " +
            "c.academicYearId = @academicYearId and " +
            "c.code = @code and " +
            "(IS_NULL(@startTime) or n.createTime >= @startTime) and " +
            "(IS_NULL(@endTime) or n.createTime <= @endTime) " +
            "group by n.creatorId")
    List<DutyRankPo> teacherRank(String code, String academicYearId, Long startTime, Long endTime);

    @Query("select count(n.dutyTreeNode.name) as count, n.dutyTreeNode.name from School as c join n in c.nodes where " +
            "c.academicYearId = @academicYearId and " +
            "c.code = @code and " +
            "(IS_NULL(@startTime) or n.createTime >= @startTime) and " +
            "(IS_NULL(@endTime) or n.createTime <= @endTime) " +
            "group by n.dutyTreeNode.name")
    List<DutyNodeRankVo> dutyNodeRank(String code, String academicYearId, Long startTime, Long endTime);

    @Query("select n.dutyTreeNode.name, n.dutyTreeNode.path, n.dutyTreeNode.score from School as c join n in c.nodes where " +
            "c.academicYearId = @academicYearId and " +
            "c.code = @code and " +
            "n.dutyTreeNode.name in (@names)")
    List<WeekDuty.DutyTreeNode> findDutyRecordInNames(String code, String academicYearId, List<String> names);
}

