<?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="cn.reghao.tnb.user.app.db.mapper.UserContactRecordMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into user_contact_record
        (`apply_user`,`applied_user`,`apply_status`,`remark`)
        values 
        (#{applyUser},#{appliedUser},#{applyStatus},#{remark})
    </insert>

    <update id="updateContactApply">
        update user_contact_record
        set update_time=now(),apply_status=#{applyStatus}
        where id=#{applyId} and applied_user=#{appliedUser}
    </update>

    <select id="findByUserIdAndFriendId" resultType="cn.reghao.tnb.user.app.model.po.UserContactRecord">
        select *
        from user_contact_record
        where apply_status=1 and apply_user=#{applyUser} and applied_user=#{appliedUser}
    </select>

    <select id="findByApplyId" resultType="cn.reghao.tnb.user.app.model.po.UserContactRecord">
        select *
        from user_contact_record
        where id=#{applyId} and applied_user=#{appliedUser}
    </select>
    <select id="countNewApply" resultType="java.lang.Integer">
        select count(*)
        from user_contact_record
        where apply_status=1 and applied_user=#{userId}
    </select>
    <select id="findByNewApply" resultType="cn.reghao.tnb.user.app.model.dto.ContactApplyInfo">
        select id,apply_user as user_id,remark,date_format(create_time, '%Y-%m-%d %H:%i:%s') as createdAt
        from user_contact_record addRequest
        where apply_status=1 and applied_user=#{userId}
    </select>
</mapper>
