package com.rf.psychological.plan.dao.repository;


import com.rf.psychological.base.repository.BaseRepository;
import com.rf.psychological.plan.dao.model.TestPlanUserEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lpf
 * @description:
 * @date 2022/1/6 12:42
 */
public interface TestPlanUserRepository extends BaseRepository<TestPlanUserEntity, String> {

    @Transactional
    @Modifying(clearAutomatically = true)
    void deleteById(String id);

    @Transactional
    @Modifying(clearAutomatically = true)
    void deleteByuIdAndTestPlanId(String uId, String testPlanId);

    @Transactional
    @Modifying(clearAutomatically = true)
    void deleteAllByuId(String uId);


    /**
     * 查询某个测试计划下的用户信息
     *
     * @param testId
     * @param pageable
     * @return
     */
    @Query(value = "select * from t_test_plan_user join t_user_info on t_user_info.id = u_id\n" +
            "where test_plan_id=?1 and if(?2 is not null and ?2 !='',(t_user_info.pet_name like CONCAT('%',?2,'%') or phone like CONCAT('%',?2,'%')),1=1)\n" +
            "  order by create_time desc ",
            countQuery = "select count(*) from t_test_plan_user join t_user_info on t_user_info.id = u_id\n" +
                    "where test_plan_id=?1 and if(?2 is not null and ?2 !='',(t_user_info.pet_name like CONCAT('%',?2,'%') or phone like CONCAT('%',?2,'%')),1=1)\n",
            nativeQuery = true)
    Page<TestPlanUserEntity> findAllByTestPlanId(String testId, String searchKey, Pageable pageable);


    /**
     * 查看测试用户数量
     *
     * @param testId
     * @return
     */
    int countByTestPlanId(String testId);


    @Query(value = "select * from t_test_plan_user tu  join t_test_plan tp  ON tu.test_plan_id = tp.id  and tu.u_id=?1 and tu.is_complete = ?3 AND if(?2 is not null and ?2 !='',(status = ?2),1=1) ", nativeQuery = true)
    List<TestPlanUserEntity> findTestPlanByUId(String uId, String status, String isComplete, Pageable pageable);

    @Transactional
    @Modifying(clearAutomatically = true)
    void deleteAllByTestPlanId(String testPlanId);

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update t_test_plan_user set is_complete = ?3 where test_plan_id=?1 and u_id=?2", nativeQuery = true)
    void updateIsComplete(String testPlanId, String userId, String isComplete);

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "update t_test_plan_user set is_complete = ?2 where test_plan_id=?1 ", nativeQuery = true)
    void updateIsComplete(String testPlanId, String isComplete);

    @Query(value = "select count(t_test_plan_user.id)\n" +
            "from t_test_plan_user\n" +
            "         join t_user_info\n" +
            "         join t_user_record on t_user_info.id = u_id and t_user_info.phone = t_user_record.phone and\n" +
            "                               t_user_record.test_plan_id = t_test_plan_user.test_plan_id\n" +
            "where u_id = ?3\n" +
            "  and t_test_plan_user.test_plan_id = ?1 and type=?2\n", nativeQuery = true)
    int countHavingDoByTypeAndUserId(String testPlanId, String type, String userId);

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value="delete from t_test_plan_user where test_plan_id =:planId and u_id in (select id from t_user_info where g_id = :groupId)",nativeQuery = true)
    void deletePlanIdAndGroupId(@Param("planId") String planId,@Param("groupId") String groupId);

    @Query(value="select u_id from t_test_plan_user where test_plan_id =:planId",nativeQuery = true)
    List<String> findUserIdByPlanId(String planId);

    @Query(value = "select id,create_time,test_plan_id,u_id,update_time,is_complete from t_test_plan_user where u_id = :userId and test_plan_id = :planId",nativeQuery = true)
    TestPlanUserEntity findByUserIdAndPlanId(@Param("userId") String userId, @Param("planId") String planId);

    @Query(value = "select id,create_time,test_plan_id,u_id,update_time,is_complete from t_test_plan_user where is_complete = '0' and test_plan_id = :planId",nativeQuery = true)
    List<TestPlanUserEntity> findUncompleteUser(@Param("planId") String planId);
}
