package com.fs.store.mapper;

import java.util.List;
import com.fs.store.domain.FsUserTuiMoneyRank;
import com.fs.store.vo.FsUserShareVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 用户佣金排行Mapper接口
 * 
 * @author fs
 * @date 2024-07-17
 */
public interface FsUserTuiMoneyRankMapper 
{
    /**
     * 查询用户佣金排行
     * 
     * @param id 用户佣金排行ID
     * @return 用户佣金排行
     */
    public FsUserTuiMoneyRank selectFsUserTuiMoneyRankById(Long id);

    /**
     * 查询用户佣金排行列表
     * 
     * @param fsUserTuiMoneyRank 用户佣金排行
     * @return 用户佣金排行集合
     */
    public List<FsUserTuiMoneyRank> selectFsUserTuiMoneyRankList(FsUserTuiMoneyRank fsUserTuiMoneyRank);

    /**
     * 新增用户佣金排行
     * 
     * @param fsUserTuiMoneyRank 用户佣金排行
     * @return 结果
     */
    public int insertFsUserTuiMoneyRank(FsUserTuiMoneyRank fsUserTuiMoneyRank);

    /**
     * 修改用户佣金排行
     * 
     * @param fsUserTuiMoneyRank 用户佣金排行
     * @return 结果
     */
    public int updateFsUserTuiMoneyRank(FsUserTuiMoneyRank fsUserTuiMoneyRank);

    /**
     * 删除用户佣金排行
     * 
     * @param id 用户佣金排行ID
     * @return 结果
     */
    public int deleteFsUserTuiMoneyRankById(Long id);

    /**
     * 批量删除用户佣金排行
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteFsUserTuiMoneyRankByIds(Long[] ids);

    @Select({"<script> " +
            "SELECT SUM(CASE WHEN b.bill_type = 1 THEN b.number ELSE 0 END)-SUM(CASE WHEN b.bill_type = 0 THEN b.number ELSE 0 END) AS tui_money, u.user_id,u.phone,u.avatar,u.nickname FROM fs_user_bill b LEFT JOIN fs_user u ON u.user_id= b.user_id WHERE b.category = 'brokerage_price' " +
            "<if test = 'type != null   and type==2   '> " +
            "and  b.create_time &gt;= DATE_FORMAT(NOW() ,'%Y-%m-01') " +
            "</if>" +
            "<if test = 'type != null   and type==1   '> " +
            "and  YEARWEEK(b.create_time, 1) = YEARWEEK(CURDATE(), 1) " +
            "</if>" +
            " GROUP BY b.user_id HAVING tui_money > 0 ORDER BY  tui_money DESC limit 100" +
            "</script>"})
    List<FsUserShareVO> selectFsUserTuiMoneyRankByType(@Param("type") Integer type);
    @Delete("DELETE FROM fs_user_tui_money_rank WHERE type = #{type}")
    int deleteFsUserTuiMoneyRankByType(@Param("type") Integer type);
    @Select("select  id,sort,type,user_id,nickname,tui_money,avatar from fs_user_tui_money_rank where type=#{type} ORDER BY sort ")
    List<FsUserShareVO> selectFsUserShareList(Integer type);
}
