package cn.teammodel.repository;

import cn.teammodel.model.entity.appraise.Appraise;
import cn.teammodel.model.entity.appraise.AppraiseTreeNode;
import com.azure.cosmos.models.PartitionKey;
import com.azure.spring.data.cosmos.repository.CosmosRepository;
import com.azure.spring.data.cosmos.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

/**
 * @author winter
 * @create 2023-11-20 14:39 <br/>
 * Page<User> findByLastname(String lastname, Pageable pageable); <br/>
 * Slice<User> findByLastname(String lastname, Pageable pageable);<br/>
 * Window<User> findTop10ByLastname(String lastname, ScrollPosition position, Sort sort);<br/>
 * List<User> findByLastname(String lastname, Sort sort);<br/>
 * List<User> findByLastname(String lastname, Sort sort, Limit limit);<br/>
 * List<User> findByLastname(String lastname, Pageable pageable);<br/>
 */
@Repository
public interface AppraiseRepository extends CosmosRepository<Appraise, String> {
    Appraise findBySchoolId(String schoolId, PartitionKey partitionKey);
    Appraise findAppraiseBySchoolIdAndPeriodIdAndCode(String schoolId, String periodId, String code);
    Appraise findAppraiseById(String id, PartitionKey partitionKey);
    /**
    * 获取模板树
    */
    @Query("SELECT * FROM School AS s where s.code = 'Appraise' and s.schoolId = 'template'")
    List<Appraise> findTemplateTree();
    @Query("SELECT value n FROM School AS s join n in s.nodes where s.code = @code and n.id = @nodeId and s.periodId = @periodId")
    List<AppraiseTreeNode> findNodeById(@Param("code") String code, @Param("nodeId") String nodeId,@Param("periodId") String periodId);
    @Query("select value n  from School as c join n in c.nodes where c.code = @code and n.id in (@ids) and c.periodId = @periodId")
    List<AppraiseTreeNode> findAllByCodeAndIdIn(String code, Set<String> ids,String periodId);
    @Query("SELECT c.id, c.achievementRules FROM School AS c where c.code = 'Appraise' and c.schoolId = @schoolId and c.periodId = @periodId")
    List<Appraise> findRulesById(String schoolId, String periodId);
}
