package com.cirle.scientific.dao;

import com.cirle.scientific.po.QkjMemberTaskDetail;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;

public interface QkjMemberTaskDetailDao extends PagingAndSortingRepository<QkjMemberTaskDetail, Long>, JpaSpecificationExecutor<QkjMemberTaskDetail> {

    @Query(value = "SELECT * FROM qkj_member_task_detail WHERE mem_card = ?1 AND task_id = ?2 And type=0" ,nativeQuery = true)
    List<QkjMemberTaskDetail> findMemberAndTaskId(String memCard, Integer taskId);

    @Query(value = "SELECT * FROM qkj_member_task_detail WHERE mem_card = ?1 AND task_id = ?2 And type=0 AND TO_DAYS(complete_time) = TO_DAYS(NOW())" ,nativeQuery = true)
    List<QkjMemberTaskDetail> findDailyTask(String memCard, Integer taskId);

    @Query(value = "select DATE_FORMAT(complete_time, '%Y-%m-%d') as date from qkj_member_task_detail where DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-%d'), interval 6 DAY) <= DATE_FORMAT(complete_time, '%Y-%m-%d') AND mem_card =?1 AND complete_time>=?2 AND task_id = 18 And type=0 group by date" ,nativeQuery = true)
    List<Object> sevenDayCheckIn(String memCard,String time);

    @Query(value = "select  DATE_FORMAT(complete_time, '%Y-%m-%d') as date from qkj_member_task_detail where DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-%d'), interval 7 DAY) <= DATE_FORMAT(complete_time, '%Y-%m-%d') AND mem_card =?1 AND complete_time>=?2 AND task_id = 18 And type=0 group by date " ,nativeQuery = true)
    List<Object> sevenDayCheckInEn(String memCard, String time);

    @Query(value = "select  DATE_FORMAT(complete_time, '%Y-%m-%d') as date from qkj_member_task_detail where DATE_SUB(DATE_FORMAT(NOW(), '%Y-%m-%d'), interval 6 DAY) <= DATE_FORMAT(complete_time, '%Y-%m-%d') AND mem_card =?1 AND complete_time>=?2 AND task_id = 18 And type=0 AND points=10 group by date" ,nativeQuery = true)
    List<Object> sevenDayCheckInFirst(String memCard, String time);

    @Query(value = "SELECT qmt.task_desc,qmtd.points,qmtd.change_type,qmtd.complete_time FROM qkj_member_task_detail qmtd left join qkj_member_task qmt on qmtd.task_id = qmt.id WHERE mem_card = ?1 order by qmtd.complete_time desc" ,nativeQuery = true)
    List<Object[]> findUserTaskInfo(String memCard);
}
