<?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.pluss.platform.mapper.NoticeReadRecordMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.pluss.platform.entity.NoticeReadRecord">
        <id column="id" property="id" />
        <result column="userId" property="userId" />
        <result column="type" property="type" />
        <result column="noticeId" property="noticeId" />
        <result column="createTime" property="createTime" />
    </resultMap>

    <insert id="updateBillNoticeRead">
        INSERT INTO tb_pluss_notice_read_record(`userId`, `type`, `noticeId`)
        SELECT #{userId} `userId`, 1 `type`, mm.id noticeId FROM tb_pluss_make_money mm
            LEFT JOIN tb_pluss_merchant_channel_status mcs ON mm.merchantId = mcs.merchantId
            LEFT JOIN tb_pluss_user_app ua ON ua.merchantCode = mcs.merchantCode
            LEFT JOIN tb_pluss_notice_read_record nrr ON nrr.noticeId = mm.id AND nrr.type = '1' AND nrr.userId = ua.userId
        WHERE nrr.id IS NULL AND ua.userId = #{userId}
    </insert>

    <insert id="updateNoticeRead">
        INSERT INTO tb_pluss_notice_read_record(`userId`, `type`, `noticeId`)
        SELECT #{userId},2 type, id FROM tb_pluss_notice
        WHERE id NOT IN(SELECT noticeId FROM tb_pluss_notice_read_record WHERE type='2' AND userId=#{userId})
        AND userId IN(-1,#{userId})
    </insert>

    <insert id="updateEventInfoRead">
        INSERT INTO tb_pluss_notice_read_record(`userId`, `type`, `noticeId`)
        SELECT #{userId} `userId`, 3 `type`, bna.id noticeId FROM tb_pluss_best_new_activity bna
            LEFT JOIN tb_pluss_notice_read_record nrr ON nrr.noticeId = bna.id AND nrr.type = '3' AND nrr.userId = #{userId}
        WHERE FIND_IN_SET(#{type}, bna.type) AND nrr.id IS NULL
    </insert>

    <select id="selectBillCount" resultType="java.lang.Integer">
        SELECT count(1) FROM tb_pluss_make_money mm
         LEFT JOIN tb_pluss_merchant_channel_status mcs ON mm.merchantId = mcs.merchantId
         LEFT JOIN tb_pluss_user_app ua ON ua.merchantCode = mcs.merchantCode
         LEFT JOIN tb_pluss_notice_read_record nrr ON nrr.noticeId = mm.id AND nrr.type = '1' AND nrr.userId = ua.userId
        WHERE nrr.id IS NULL AND ua.userId = #{userId}
    </select>

    <select id="selectNoticeCount" resultType="java.util.Map">
        SELECT COUNT(*) `count`,typeFirst `type` FROM tb_pluss_notice
        WHERE id NOT IN(select noticeId from tb_pluss_notice_read_record WHERE type='2' and userId=#{userId})
        AND userId IN(-1,#{userId}) GROUP BY typeFirst
    </select>

    <select id="selectArticleCount" resultType="java.util.Map">
        SELECT count(1) `count`, 'recommendCount' `type` FROM tb_pluss_best_new_activity bna
           LEFT JOIN tb_pluss_notice_read_record nrr ON nrr.noticeId = bna.id AND nrr.type = '3' AND nrr.userId = #{userId}
        WHERE FIND_IN_SET(1, bna.type) AND nrr.id IS NULL
        UNION
        SELECT count(1) `count`, 'recommendCount2' `type` FROM tb_pluss_best_new_activity bna
           LEFT JOIN tb_pluss_notice_read_record nrr ON nrr.noticeId = bna.id AND nrr.type = '3' AND nrr.userId = #{userId}
        WHERE FIND_IN_SET(4, bna.type) AND nrr.id IS NULL
        UNION
        SELECT count(1) `count`, 'informationCount' `type` FROM tb_pluss_best_new_activity bna
           LEFT JOIN tb_pluss_notice_read_record nrr ON nrr.noticeId = bna.id AND nrr.type = '3' AND nrr.userId = #{userId}
        WHERE FIND_IN_SET(2, bna.type) AND nrr.id IS NULL
        UNION
        SELECT count(1) `count`, 'activityCount' `type` FROM tb_pluss_best_new_activity bna
           LEFT JOIN tb_pluss_notice_read_record nrr ON nrr.noticeId = bna.id AND nrr.type = '3' AND nrr.userId = #{userId}
        WHERE FIND_IN_SET(3, bna.type) AND nrr.id IS NULL
    </select>

</mapper>
