<?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.mcwl.communityCenter.mapper.CommunityMapper">

    <resultMap id="CommunityJoinNumMap" type="java.util.HashMap">
        <id property="id" column="id" javaType="java.lang.Long" />  <!-- 键字段 -->
        <result property="joinNum" column="join_num" javaType="java.lang.Integer" /> <!-- 值字段 -->
    </resultMap>

    <resultMap id="CommunityPublishNumMap" type="java.util.HashMap">
        <id property="id" column="id" javaType="java.lang.Long" />  <!-- 键字段 -->
        <result property="publishNum" column="publish_num" javaType="java.lang.Integer" /> <!-- 值字段 -->
    </resultMap>
    <update id="quitCommunity">
        update cc_community_user
        set del_flag = '1'
        where tenant_id = #{tenantId}
          and community_id = #{communityId}
          and user_id = #{userId}
          and del_flag = '0';
    </update>

    <update id="deleteCommunity">
        update cc_community
        set del_flag = '1'
        where tenant_id = #{tenantId}
          and id = #{communityId}
          and del_flag = '0';
    </update>

    <select id="getByTenantIdAndCommunityId" resultType="com.mcwl.communityCenter.domain.Community">
        select id,
               tenant_id,
               community_name,
               type,
               price,
               validity_day
        from cc_community
        where tenant_id = #{tenantId}
          and id = #{communityId}
          and del_flag = '0';
    </select>

    <select id="selectCommunityJoinNum" resultMap="CommunityJoinNumMap">
        select cu.community_id as id, COALESCE(count(*), 0) as join_num
        from cc_community_user cu
        where cu.del_flag = '0'
        group by cu.community_id
    </select>



    <select id="selectCommunityPublishNum" resultMap="CommunityPublishNumMap">
        select p.community_id as id, COALESCE(count(*), 0) as publish_num
        from cc_community c
                 join cc_publish p on c.id = p.community_id
                 join cc_question q on c.id = q.community_id
        where c.del_flag = '0'
          and p.del_flag = '0'
          and q.del_flag = '0'
        group by p.community_id
    </select>
    <select id="selectPageByCommunityTag" resultType="com.mcwl.communityCenter.domain.Community">
        select
        id,
        tenant_id,
        image_url,
        community_name,
        description,
        community_tag,
        type,
        price,
        validity_day,
        create_by,
        create_time,
        update_by,
        update_time
        from cc_community
        <where>
            <if test="communityTag != null">
                and community_tag = #{communityTag}
            </if>
            <if test="searchContent != null and searchContent != ''">
                and (community_name like concat('%', #{searchContent}, '%')
                or `description` like concat('%', #{searchContent}, '%'))
            </if>
            and del_flag = '0'
        </where>

    </select>
    <select id="getMyJoinCommunity" resultType="com.mcwl.communityCenter.domain.Community">
        select c.id,
        c.tenant_id,
        c.image_url,
        c.community_name,
        c.description,
        c.community_tag,
        c.type,
        c.price,
        c.validity_day,
        c.create_by,
        c.create_time,
        c.update_by,
        c.update_time
        from cc_community c
        join cc_community_user cu on c.id = cu.community_id
        where cu.user_id = #{userId}
        and c.del_flag = '0'
        and cu.del_flag = '0'
        <if test="joinCommunityListPageRes.isMyCreate == 1">
            and c.tenant_id = #{userId}
        </if>
        <if test="joinCommunityListPageRes.isMyCreate == 0">
            and c.tenant_id != #{userId}
        </if>
        <if test="joinCommunityListPageRes.searchContent != null and joinCommunityListPageRes.searchContent != ''">
            and (c.community_name like concat('%', #{joinCommunityListPageRes.searchContent}, '%')
            or c.description like concat('%', #{joinCommunityListPageRes.searchContent}, '%'))
        </if>
    </select>
</mapper>