<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.cai.mapper.GuardTotalMapper">

    <resultMap type="com.ruoyi.cai.domain.GuardTotal" id="CaiGuardTotalResult">
        <result property="id" column="id"/>
        <result property="fromUserId" column="from_user_id"/>
        <result property="toUserId" column="to_user_id"/>
        <result property="guardNum" column="guard_num"/>
        <result property="guardValue" column="guard_value"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>
    <update id="incs">
        update cai_guard_total
        set guard_num = guard_num + #{guardNum},
            guard_value = guard_value + #{guardValue}
        where to_user_id = #{toUserId} and from_user_id = #{fromUserId}
    </update>
    <select id="selectGuardTotal" resultType="com.ruoyi.cai.dto.app.dto.GuardTotalDTO">
        select t1.from_user_id,t1.to_user_id,t2.avatar,t1.guard_num
        from cai_guard_total t1
        join cai_user t2 on t1.from_user_id = t2.id
        where t1.to_user_id = #{toUserId}
        order by guard_num desc
        <if test="limit != null">
            limit #{limit}
        </if>
    </select>
    <select id="guardCount" resultType="java.lang.Long">
        select ifnull(sum(guard_num),0)
        from cai_guard_total t1
        join cai_user t2 on t1.from_user_id = t2.id
        where t1.to_user_id = #{toUserId}
    </select>
    <select id="guardPersonCount" resultType="java.lang.Long">
        select ifnull(count(1),0)
        from cai_guard_total t1
        join cai_user t2 on t1.from_user_id = t2.id
        where t1.to_user_id = #{toUserId}
    </select>
    <select id="userGuardPage" resultType="com.ruoyi.cai.dto.app.vo.index.GuardListPageVo">
        select t1.from_user_id as user_id, t2.nickname,t2.avatar,t2.usercode,t1.guard_num,t1.guard_value
        from cai_guard_total t1
        join cai_user t2 on t1.from_user_id = t2.id
        where t1.to_user_id = #{toUserId}
        order by guard_num desc
    </select>


</mapper>
