<?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="jnpf.cultivate.mapper.FtbCultivatePromotionMemberMapper">

    <!--根据用户ID查询用户的推广信息-->
    <select id="queryPromotionByUserOrPostId" resultMap="jnpf.cultivate.mapper.FtbCultivatePromotionMapper.promotionMap">
        SELECT
        DISTINCT
        pr.F_Id AS id,
        pr.F_Promotion AS promotion,
        pr.F_PromotionBusinessId AS promotionBusinessId,
        pr.F_LastModifyTime AS lastModifyTime,
        pr.F_CreatorTime AS creatorTime,
        po.F_PostId channelIniPoId,
        po.F_PostName channelIniName
        FROM
        ftb_cultivate_promotion AS pr
        <if test="userId != null ">
            inner join ftb_cultivate_promotion_member AS me ON pr.F_Id = me.F_PromotionId
        </if>
        <if test="postId != null ">
            inner join ftb_cultivate_promotion_post AS po ON pr.F_Id = po.F_PromotionId
        </if>
        <where>
            pr.F_EnableMark=0
            <if test="postId != null ">
                and  po.F_PostId =#{postId}
            </if>
            <if test="userId != null ">
                AND me.F_UserId =#{userId}
                and me.F_EnableStatus=1
            </if>
        </where>
        ORDER BY
        pr.F_Promotion
    </select>
    <!--根据用户ID和帖子ID查询推广信息-->
    <select id="queryPromotionByUser" resultMap="jnpf.cultivate.mapper.FtbCultivatePromotionMapper.promotionMap">
        SELECT
        DISTINCT
        pr.F_Id AS id,
        pr.F_Promotion AS promotion,
        pr.F_PromotionBusinessId AS promotionBusinessId,
        pr.F_LastModifyTime AS lastModifyTime,
        pr.F_CreatorTime AS creatorTime,
        po.F_PostId channelIniPoId,
        po.F_PostName channelIniName
        FROM
        ftb_cultivate_promotion AS pr
        left JOIN ftb_cultivate_promotion_member AS me ON pr.F_Id = me.F_PromotionId
        inner join ftb_cultivate_promotion_post AS po ON pr.F_Id = po.F_PromotionId
        <where>
            pr.F_EnableMark=0
            and  po.F_PostId =#{postId}
            AND me.F_UserId =#{userId}
            and me.F_EnableStatus=1
        </where>
        ORDER BY
        pr.F_Promotion
    </select>

    <!--此方法用于根据特定的用户ID-->
    <select id="queryUserInfo" resultType="jnpf.model.cultivate.vo.promotion.FtbCultivatePromotionMemberVO">
        SELECT
        DISTINCT
        me.F_PromotionId promotionId,
        pr.F_Promotion promotionName,
        me.F_state state
        FROM
        ftb_cultivate_promotion AS pr
        inner JOIN ftb_cultivate_promotion_member AS me ON pr.F_Id = me.F_PromotionId
        inner join ftb_cultivate_promotion_post AS po ON pr.F_Id = po.F_PromotionId
        <where>
            pr.F_EnableMark=0
            AND me.F_UserId =#{userId}
        </where>
    </select>
    <!--查询已存在的帖子列表-->
    <select id="queryPostHasExist" resultType="jnpf.model.cultivate.vo.promotion.FtbCultivatePromotionPostVO">
        SELECT post.F_PostId      as postId,
               post.F_PostName    as postName,
               post.F_Level       as channelLevel,
               post.F_PromotionId as promotionId
        FROM ftb_cultivate_promotion AS pro
                 INNER JOIN ftb_cultivate_promotion_post AS post ON pro.F_Id = post.F_PromotionId
        WHERE pro.F_EnableMark = 0
        ORDER BY post.F_PromotionId, post.F_Level
    </select>
    <!--根据用户ID查询用户的推广信息-->
    <select id="queryPromotionByUserOrOrgId" resultType="jnpf.model.cultivate.vo.promotion.FtbCultivatePromotionVO">
        SELECT
        DISTINCT
        pr.F_Id AS id,
        pr.F_Promotion AS promotion,
        pr.F_PromotionBusinessId AS promotionBusinessId,
        pr.F_LastModifyTime AS lastModifyTime,
        pr.F_CreatorTime AS creatorTime,
        po.F_PostId channelIniPoId,
        po.F_PostName channelIniName
        FROM
        ftb_cultivate_promotion AS pr
        inner join ftb_cultivate_promotion_member AS me ON pr.F_Id = me.F_PromotionId
        inner join ftb_cultivate_promotion_post AS po ON pr.F_Id = po.F_PromotionId
        <where>
            pr.F_EnableMark=0
             and  me.F_OrgId =#{OrgId}
            <if test="userId != null ">
                AND me.F_UserId =#{userId}
                and me.F_EnableStatus=1
            </if>
        </where>
        ORDER BY
        pr.F_Promotion
    </select>
</mapper>