package com.fs.course.mapper;

import com.fs.company.vo.RedPacketMoneyVO;
import com.fs.course.domain.FsCourseRedPacketLog;
import com.fs.course.param.FsCourseRedPacketLogParam;
import com.fs.course.vo.FsCourseRedPacketLogListPVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

/**
 * 短链课程看课记录Mapper接口
 *
 * @author fs
 * @date 2024-10-24
 */
public interface FsCourseRedPacketLogMapper
{
    /**
     * 查询短链课程看课记录
     *
     * @param logId 短链课程看课记录主键
     * @return 短链课程看课记录
     */
    public FsCourseRedPacketLog selectFsCourseRedPacketLogByLogId(Long logId);

    /**
     * 查询短链课程看课记录列表
     *
     * @param fsCourseRedPacketLog 短链课程看课记录
     * @return 短链课程看课记录集合
     */
    public List<FsCourseRedPacketLog> selectFsCourseRedPacketLogList(FsCourseRedPacketLog fsCourseRedPacketLog);

    /**
     * 新增短链课程看课记录
     *
     * @param fsCourseRedPacketLog 短链课程看课记录
     * @return 结果
     */
    public int insertFsCourseRedPacketLog(FsCourseRedPacketLog fsCourseRedPacketLog);

    /**
     * 修改短链课程看课记录
     *
     * @param fsCourseRedPacketLog 短链课程看课记录
     * @return 结果
     */
    public int updateFsCourseRedPacketLog(FsCourseRedPacketLog fsCourseRedPacketLog);

    /**
     * 删除短链课程看课记录
     *
     * @param logId 短链课程看课记录主键
     * @return 结果
     */
    public int deleteFsCourseRedPacketLogByLogId(Long logId);

    /**
     * 批量删除短链课程看课记录
     *
     * @param logIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsCourseRedPacketLogByLogIds(Long[] logIds);

    @Select("select * from fs_course_red_packet_log where out_batch_no = #{outBatchNo}")
    FsCourseRedPacketLog selectFsCourseRedPacketLogByBatchNo(@Param("outBatchNo") String outBatchNo);


    @Select({"<script> " +
            "select o.*,v.title as video_name,u.phone as user_name  from fs_course_red_packet_log l " +
            " left join fs_user_course_video v on v.video_id = o.course_video_id" +
            " left join fs_user u on u.user_id = o.user_id " +
            "where 1=1   " +
            "<if test = ' maps.userId !=null '> " +
            "and o.user_id = #{maps.userId} " +
            "</if>" +
            "<if test = ' maps.orderCode !=null and maps.orderCode!=\"\" '> " +
            "and o.order_code = #{maps.orderCode} " +
            "</if>" +
            "<if test = ' maps.courseName !=null and maps.courseName!=\"\" '> " +
            "and o.course_name = #{maps.courseName} " +
            "</if>" +
            "<if test = ' maps.videoName !=null and maps.videoName!=\"\" '> " +
            "and v.title = #{maps.videoName} " +
            "</if>" +
            "<if test = ' maps.payType !=null '> " +
            "and o.pay_type = #{maps.payType} " +
            "</if>" +
            "<if test = ' maps.createType !=null '> " +
            "and o.create_type = #{maps.createType} " +
            "</if>" +
            "<if test = ' maps.status !=null '> " +
            "and o.status = #{maps.status} " +
            "</if>" +
            " order by o.order_id desc  "+
            "</script>"})
    List<FsCourseRedPacketLogListPVO> selectRedPacketLogListVO(@Param("maps") FsCourseRedPacketLogParam param);

    @Select({"<script> " +
            "select l.*,v.title,u.nick_name as fsNickName,u.avatar as fsAvatar,u.phone,cu.nick_name company_user_name,c.company_name,qu.qw_user_name  from fs_course_red_packet_log l  \n" +
            "left join fs_user_course_video v on v.video_id = l.video_id \n" +
            "left join fs_user u on u.user_id = l.user_id \n" +
            "left join company_user cu on cu.user_id=l.company_user_id \n" +
            "left join company c on c.company_id=l.company_id \n" +
            "LEFT JOIN qw_user qu on qu.id= l.qw_user_id  " +
            "where 1=1   " +
            "<if test = ' maps.userId !=null '> and l.user_id = #{maps.userId} </if>" +
            "<if test = ' maps.watchLogId !=null '> and l.watch_log_id = #{maps.watchLogId} </if>" +
            "<if test = ' maps.companyId !=null '> and l.company_id = #{maps.companyId} </if>" +
            "<if test = ' maps.companyUserName !=null '> and cu.nick_name  like concat('%', #{maps.companyUserName}, '%') </if>" +
            "<if test = ' maps.nickName !=null '> and u.nick_name  like concat('%', #{maps.nickName}, '%') </if>" +
            "<if test = ' maps.courseId !=null '> and l.course_id = #{maps.courseId} </if>" +
            "<if test = ' maps.videoId !=null '> and l.video_id = #{maps.videoId} </if>" +
            "<if test = ' maps.status !=null '> and l.status = #{maps.status} </if>" +
            "<if test = ' maps.phone !=null '> and u.phone = #{maps.phone} </if>" +
            "<if test = ' maps.qwUserId !=null '> and l.qw_user_id = #{maps.qwUserId} </if>" +
            "<if test=\"maps.sTime != null \">  and DATE(l.create_time) &gt;= DATE(#{maps.sTime})</if>\n" +
            "<if test=\"maps.eTime != null \">  and DATE(l.create_time) &lt;= DATE(#{maps.eTime})</if>\n" +
            " order by l.log_id desc  "+
            "</script>"})
    List<FsCourseRedPacketLogListPVO> selectFsCourseRedPacketLogListVO(@Param("maps")FsCourseRedPacketLogParam fsCourseRedPacketLog);

    @Select("SELECT company_id,sum(amount) as money from fs_course_red_packet_log  WHERE status = 1 and  DATE(create_time) = DATE_SUB(CURDATE(), INTERVAL 1 DAY) GROUP BY company_id")
    List<RedPacketMoneyVO> selectFsCourseRedPacketLogByCompany();

    List<FsCourseRedPacketLogListPVO> selectFsCourseRedPacketLogListVONew(FsCourseRedPacketLogParam fsCourseRedPacketLog);

    @Select("select * from fs_course_red_packet_log where video_id = #{videoId} and user_id = #{userId} limit 1")
    FsCourseRedPacketLog selectFsCourseRedPacketLogByTemporary(@Param("videoId") Long videoId, @Param("userId")Long userId);

    /**
     * 查询红包数量
     * @param companyUserId 销售ID
     * @return count
     */
    @Select("select count(fcrpl.log_id) from fs_course_red_packet_log fcrpl where fcrpl.company_user_id = #{companyUserId}")
    int getCountByCompanyUserIdId(@Param("companyUserId") Long companyUserId);

    /**
     * 查询新用户红包金额总和
     * @param companyUserId 销售ID
     * @return amount
     */
    BigDecimal getNewVipRedPackAmountByCompanyUserIdId(@Param("companyUserId") Long companyUserId);

    /**
     * 查询红包金额数
     * @param companyUserId 销售ID
     * @return amount
     */
    @Select("select ifnull(sum(fcrpl.amount), 0) from fs_course_red_packet_log fcrpl where fcrpl.company_user_id = #{companyUserId}")
    BigDecimal getSumByCompanyUserIdId(@Param("companyUserId") Long companyUserId);
}
