package com.fs.his.mapper;

import com.fs.his.domain.RedPacketLog;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 红包发放日志表 Mapper接口
 */
@Mapper
public interface RedPacketLogMapper {

    /**
     * 根据ID查询红包发放日志
     *
     * @param id 主键ID
     * @return 红包发放日志
     */
    @Select("SELECT * FROM red_packet_log WHERE id = #{id}")
    RedPacketLog selectById(Long id);

    /**
     * 查询用户红包发放日志列表
     *
     * @param userId 用户ID
     * @return 红包发放日志列表
     */
    @Select("SELECT * FROM red_packet_log WHERE user_id = #{userId} ORDER BY create_time DESC")
    List<RedPacketLog> selectByUserId(Long userId);

    /**
     * 查询公司红包发放日志列表
     *
     * @param companyId 公司ID
     * @return 红包发放日志列表
     */
    @Select("SELECT * FROM red_packet_log WHERE company_id = #{companyId} ORDER BY create_time DESC")
    List<RedPacketLog> selectByCompanyId(Long companyId);

    /**
     * 查询指定状态的红包发放日志列表
     *
     * @param status 状态
     * @return 红包发放日志列表
     */
    @Select("SELECT * FROM red_packet_log WHERE status = #{status} ORDER BY create_time DESC")
    List<RedPacketLog> selectByStatus(Integer status);

    /**
     * 插入红包发放日志
     *
     * @param redPacketLog 红包发放日志
     * @return 影响行数
     */
    @Insert("INSERT INTO red_packet_log (user_id, amount, company_id, source, red_packet_mode, app_id, " +
            "acc_balance_before, acc_balance_after, status, error_msg, create_by, update_by, remark) " +
            "VALUES (#{userId}, #{amount}, #{companyId}, #{source}, #{redPacketMode}, #{appId}, " +
            "#{accBalanceBefore}, #{accBalanceAfter}, #{status}, #{errorMsg}, #{createBy}, #{updateBy}, #{remark})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(RedPacketLog redPacketLog);

    /**
     * 更新红包发放日志
     *
     * @param redPacketLog 红包发放日志
     * @return 影响行数
     */
    @Update("<script>" +
            "UPDATE red_packet_log SET " +
            "<if test='status != null'>status = #{status}, </if>" +
            "<if test='errorMsg != null'>error_msg = #{errorMsg}, </if>" +
            "<if test='updateBy != null'>update_by = #{updateBy}, </if>" +
            "<if test='remark != null'>remark = #{remark}, </if>" +
            "update_time = NOW() " +
            "WHERE id = #{id}" +
            "</script>")
    int update(RedPacketLog redPacketLog);

    /**
     * 根据ID删除红包发放日志
     *
     * @param id 主键ID
     * @return 影响行数
     */
    @Delete("DELETE FROM red_packet_log WHERE id = #{id}")
    int deleteById(Long id);
}
