package org.jeecg.modules.clockinlog.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.jeecg.modules.clockinlog.entity.ClockinLog;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.jeecg.modules.clockinlog.ressp.ClockInLogResp;

/**
 * @Description: clockin_log
 * @Author: jeecg-boot
 * @Date:   2024-10-24
 * @Version: V1.0
 */
public interface ClockinLogMapper extends BaseMapper<ClockinLog> {

    /**
     * 获取打卡详情
     * @param date
     * @param projectId
     * @return
     */
    @Select("SELECT a.id,a.clock_start_time,a.type,b.pic clock_in_pic,b.address clock_in_address,(SELECT id FROM clockin_log WHERE project_item_id = a.id AND clock_in_time like '%${date}%' AND user_id = #{userId}) log_id\n" +
            "     ,(SELECT pic FROM clockin_log WHERE project_item_id = a.id and clock_in_time like '%${date}%' AND user_id = #{userId}) pic\n" +
            "     ,(SELECT clock_in_time FROM clockin_log WHERE project_item_id = a.id AND clock_in_time like '%${date}%' AND user_id = #{userId}) clock_in_time\n" +
            "     ,(SELECT address FROM clockin_log WHERE project_item_id = a.id AND clock_in_time like '%${date}%' AND user_id = #{userId}) address\n" +
            "     ,(SELECT count(1) FROM clockin_log WHERE project_item_id = a.id AND clock_in_time like '%${date}%' AND user_id = #{userId}) clock_in_flag\n" +
            "FROM clock_in_project_item a left join clock_in_project b on a.project_id = b.id\n" +
            "WHERE a.del_flag = 0 AND a.type in (${type},2) AND a.project_id = #{projectId} ORDER BY a.sort,a.type")
    List<ClockInLogResp> getClockInLogResp(@Param("date") String date,@Param("projectId") String projectId,@Param("userId") String userId,@Param("type") Integer type);


    /**
     * 获取打卡统计
     * @param userId
     * @param date
     * @return
     */
    @Select("SELECT IFNULL(count(1),0) FROM clockin_log WHERE del_flag = 0 AND user_id = #{userId} AND clock_in_time like '%${date}%' AND type in (0,1)")
    Integer getClockInTotal(@Param("userId") String userId,@Param("date") String date);

    @Select("select clock_start_time,(select clock_in_time from clockin_log where project_item_id = a.id and del_flag = 0 and user_id = '${userId}' and clock_in_time like '%${date}%') time, a.type from clock_in_project_item a where project_id = '${projectId}'  AND a.del_flag = 0 AND type in (${type},2)\n")
    List<ClockInLogResp> getClockInLogExpeort(@Param("date") String date,@Param("projectId") String projectId,@Param("userId") String userId,@Param("type") Integer type);

}
