package com.fs.store.mapper;

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

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.nickname,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.log_type =1 or l.log_type =2 or l.log_type =3)  " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type==2       '> " +
            "and (l.log_type =4 or l.log_type =5)  " +
            "</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);
}
