package com.fs.his.mapper;

import com.fs.his.domain.FsUserIntegralLogs;
import com.fs.his.param.FsUserIntegralLogsListUParam;
import com.fs.his.param.FsUserIntegralLogsParam;
import com.fs.his.vo.FsUserIntegralLogsListUVO;
import com.fs.his.vo.FsUserIntegralLogsListVO;
import com.fs.his.vo.SubIntegralVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

/**
 * 积分记录Mapper接口
 *
 * @author fs
 * @date 2023-11-02
 */
public interface FsUserIntegralLogsMapper
{
    /**
     * 查询积分记录
     *
     * @param id 积分记录主键
     * @return 积分记录
     */
    public FsUserIntegralLogs selectFsUserIntegralLogsById(Long id);

    /**
     * 查询积分记录列表
     *
     * @param fsUserIntegralLogs 积分记录
     * @return 积分记录集合
     */
    public List<FsUserIntegralLogs> selectFsUserIntegralLogsList(FsUserIntegralLogs fsUserIntegralLogs);

    /**
     * 新增积分记录
     *
     * @param fsUserIntegralLogs 积分记录
     * @return 结果
     */
    public int insertFsUserIntegralLogs(FsUserIntegralLogs fsUserIntegralLogs);

    /**
     * 修改积分记录
     *
     * @param fsUserIntegralLogs 积分记录
     * @return 结果
     */
    public int updateFsUserIntegralLogs(FsUserIntegralLogs fsUserIntegralLogs);

    /**
     * 删除积分记录
     *
     * @param id 积分记录主键
     * @return 结果
     */
    public int deleteFsUserIntegralLogsById(Long id);

    /**
     * 批量删除积分记录
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsUserIntegralLogsByIds(Long[] ids);
    @Select({"<script> " +
            "select l.*,u.nick_name,u.phone from fs_user_integral_logs l LEFT JOIN fs_user u ON u.user_id=l.user_id "+
            " <where>  \n" +
            "            <if test=\"userId != null \"> and l.user_id = #{userId}</if>\n" +
            "            <if test=\"logType != null  and logType != ''\"> and log_type = #{logType}</if>\n" +
            "            <if test=\"phone != null \"> and u.phone = #{phone}</if>\n" +
            "            <if test=\"businessId != null  and businessId != ''\"> and business_id = #{businessId}</if>\n" +
            "            <if test=\"createTime != null \"> and create_time = #{createTime}</if>\n" +
            "            <if test=\"sTime != null \">  and DATE(l.create_time) &gt;= DATE(#{sTime})</if>\n" +
            "            <if test=\"eTime != null \">  and DATE(l.create_time) &lt;= DATE(#{eTime})</if>\n" +
            "        </where> order by id desc"+
            "</script>"})
    List<FsUserIntegralLogsListVO> selectFsUserIntegralLogsListVO(FsUserIntegralLogsParam fsUserIntegralLogs);
    @Select({"<script> " +
            "select l.*  from fs_user_integral_logs l  " +
            "where 1=1  " +
            "<if test = 'maps.userId != null       '> " +
            "and l.user_id = #{maps.userId}  " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type==1       '> " +
            "and l.integral &gt;= 0  " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type==2       '> " +
            "and l.integral &lt; 0  " +
            "</if>" +
            " order by l.id desc "+
            "</script>"})
    List<FsUserIntegralLogsListUVO> selectFsUserIntegralLogsListUVO(@Param("maps")FsUserIntegralLogsListUParam param);
    @Select("select user_id  from fs_user_integral_logs WHERE DATE_FORMAT(create_time, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m') and log_type=2 and `status`=0  and business_type=2 GROUP BY user_id HAVING count(user_id) > 5;")
    List<Long> subFsUserIntegralLogsByOrder5();

    @Select("select * from fs_user_integral_logs where user_id = #{id} AND `status`=0 and DATE_FORMAT(create_time, '%Y-%m') = DATE_FORMAT(CURDATE(), '%Y-%m') and ((business_type=2 and log_type=2) or log_type=6)")
    List<FsUserIntegralLogs> selectFsUserIntegralByStatus0(Long id);

    @Select("select * from fs_user_integral_logs where log_type=10 and user_id = #{userId} and business_id = #{videoId} limit 1")
    FsUserIntegralLogs selectFsUserIntegralByCourseVideo(@Param("videoId") Long videoId,@Param("userId")Long userId);

    @Select("select * from fs_user_integral_logs where log_type=11 and user_id = #{userId} limit 1")
    FsUserIntegralLogs selectFsUserIntegralLogsAddPatient(@Param("userId")Long userId);


    @Select("select * from fs_user_integral_logs where log_type=12 and user_id = #{userId} limit 1")
    FsUserIntegralLogs selectFsUserIntegralLogsAddUserAddress(@Param("userId")Long userId);

    @Select({"<script> " +
            "SELECT SUM(integral) AS todayTotalIntegral FROM fs_user_integral_logs " +
            "WHERE user_id = #{userId} AND log_type IN " +
            "<foreach item='type' collection='types' open='(' separator=',' close=')'> " +
            "            #{type} " +
            "</foreach>"+
            " AND DATE(create_time) = CURDATE()"+
            "</script>"})
    Integer selectTodayTotalIntegral(@Param("types") Integer[] integralTypeByOneDay, @Param("userId") Long userId);

    @Select("select count(1) from fs_user_integral_logs where log_type = 14 and user_id =#{userId} AND DATE(create_time) = CURDATE()")
    Integer selectTodayVideoIntegralCount(@Param("userId") Long userId);

    @Select("SELECT user_id, COUNT(*) AS num \n" +
            "FROM fs_user_integral_logs\n" +
            "WHERE log_type = 12\n" +
            "GROUP BY user_id\n" )
    List<SubIntegralVO> selectSubIntegralByLogType12();

    @Delete("delete from fs_user_integral_logs where log_type = #{logType} and user_id = #{userId}")
    void deleteFsUserIntegralLogsByType(@Param("logType") int i,@Param("userId") Long userId);


    @Select("select count(1) from fs_user_integral_logs where log_type = 16 and user_id =#{userId} and business_id = #{videoId} ")
    Long selectH5VideoIntegralCount(@Param("userId") Long userId,@Param("videoId") Long videoId);

    List<FsUserIntegralLogs> selectFsUserIntegralLogsByUserIdAndLogType(@Param("userId") Long userId, @Param("logType") Integer logType, @Param("date") LocalDate date);

    /**
     * 查询用户最新的积分记录
     */
    FsUserIntegralLogs selectLatestIntegralLogByUserId(@Param("userId") Long userId);

    /**
     * 查询用户当天获得的积分总数（只统计正积分）
     */
    @Select("SELECT IFNULL(SUM(integral),0) FROM fs_user_integral_logs " +
            "WHERE user_id = #{userId} AND integral > 0 AND DATE(create_time) = CURDATE()")
    Long selectTodayIntegralTotal(@Param("userId") Long userId);
}
