<?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.FtbCultivatePromotionMapper">
    <resultMap id="promotionMap" type="jnpf.model.cultivate.vo.promotion.FtbCultivatePromotionVO">
        <id property="id" column="id" jdbcType="VARCHAR"  javaType="java.lang.String"/>
        <result property="promotion" column="promotion" jdbcType="VARCHAR"  javaType="java.lang.String"/>
        <result property="promotionBusinessId" column="promotionBusinessId" jdbcType="VARCHAR"  javaType="java.lang.String"/>
        <result property="channelIniPoId" column="channelIniPoId" jdbcType="VARCHAR"  javaType="java.lang.String"/>
        <result property="channelIniName" column="channelIniName" jdbcType="VARCHAR"  javaType="java.lang.String"/>
        <result property="lastModifyTime" column="lastModifyTime" jdbcType="DATETIMEOFFSET"  javaType="java.util.Date"/>
        <result property="creatorTime" column="creatorTime" jdbcType="DATETIMEOFFSET"  javaType="java.util.Date"/>
        <collection select="promoPostId" property="postChannelLevel" column="id" javaType="java.util.ArrayList"/>
    </resultMap>
    <!--查询通道层级-->
    <select id="getList" resultMap="promotionMap">
        SELECT
        a.*,b.channelIniPoId,b.channelIniName
        FROM
        (
        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
        FROM
        ftb_cultivate_promotion AS pr
        LEFT JOIN ftb_cultivate_promotion_post AS po ON pr.F_Id = po.F_PromotionId
        <where>
            pr.F_EnableMark=0
            <if test="dto.postId != null and dto.postId !=''">
                and  po.F_PostId =#{dto.postId}
            </if>
            <if test="dto.keyWord != null and dto.keyWord !=''">
                and   pr.F_Promotion like CONCAT('%',#{dto.keyWord},'%')
            </if>
            <if test="dto.promotionId != null and dto.promotionId !=''">
                AND pr.F_Id =#{dto.promotionId}
            </if>
        </where>
        ) AS a INNER JOIN (
        SELECT
        pr.F_Id,
        po.F_PostId channelIniPoId,
        po.F_PostName channelIniName
        FROM
        ftb_cultivate_promotion AS pr
        LEFT JOIN ftb_cultivate_promotion_post AS po ON pr.F_Id = po.F_PromotionId
        where
        pr.F_EnableMark=0
        AND po.F_Level = 1
        ) AS b ON a.id = b.F_Id
        ORDER BY
        a.creatorTime DESC,
        a.lastModifyTime DESC
    </select>
    <!--根据促销活动ID列表查询对应的促销活动信息-->
    <select id="promoPostId" resultType="jnpf.model.cultivate.vo.promotion.FtbCultivatePromotionPostVO">
        SELECT
            F_Id promotionPostId,
            F_PromotionId AS promotionId,
            F_PostId AS postId,
            F_PostName AS postName,
            F_Level AS channelLevel
        FROM
            ftb_cultivate_promotion_post where F_PromotionId = #{id}
        order by F_Level asc
    </select>
    <!--查询已经开启的成员列表-->
    <select id="getPromotionMbeList" resultType="jnpf.model.cultivate.vo.promotion.FtbCultivatePromotionMemberVO">
        SELECT
            me.F_PromotionId promotionId,
            pr.F_Promotion promotionName,
            me.F_UserId userId,
            me.F_UserName userName,
            me.F_OrgId orgId,
            me.F_OrgName orgName,
            me.F_PositionId positionId,
            me.F_PositionName positionName,
            me.F_GradeId positionGradesId,
            me.F_GradeName positionGradesName,
            count( pr.F_Id ) as numOfChannelsEnabled
        FROM
            ftb_cultivate_promotion AS pr
                INNER JOIN ftb_cultivate_promotion_member AS me ON pr.F_Id = me.F_PromotionId
        WHERE pr.F_Id=#{dto.promotionId}
          AND me.F_EnableStatus=1
        GROUP BY me.F_UserId
    </select>
    <!--查询通道层级-->
    <select id="getPromotionChannel" resultMap="promotionMap">
        SELECT
        pr.F_Id AS id,
        pr.F_Promotion AS promotion,
        pr.F_PromotionBusinessId AS promotionBusinessId,
        pr.F_LastModifyTime AS lastModifyTime
        FROM
        ftb_cultivate_promotion AS pr
        <where>
            pr.F_EnableMark=0
            <if test="id != null">
                AND pr.F_Id =#{id}
            </if>
        </where>
        ORDER BY
        pr.F_Promotion
    </select>
    <!--根据促销活动ID列表查询对应的促销活动信息-->
    <select id="queryListByIds" resultMap="promotionMap">
        SELECT
        a.*,b.channelIniPoId,b.channelIniName
        FROM
        (
        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
        FROM
        ftb_cultivate_promotion AS pr
        LEFT JOIN ftb_cultivate_promotion_post AS po ON pr.F_Id = po.F_PromotionId
        <where>
            pr.F_EnableMark=0
            AND pr.F_Id  in
            <foreach collection="promotionIds" item="promotionId" open="(" separator="," close=")">
                #{promotionId}
            </foreach>
        </where>
        ) AS a INNER JOIN (
        SELECT
        pr.F_Id,
        po.F_PostId channelIniPoId,
        po.F_PostName channelIniName
        FROM
        ftb_cultivate_promotion AS pr
        LEFT JOIN ftb_cultivate_promotion_post AS po ON pr.F_Id = po.F_PromotionId
        <where>
            pr.F_EnableMark=0
            AND po.F_Level = 1
        </where>
        ) AS b ON a.id = b.F_Id
        ORDER BY
        a.creatorTime DESC,
        a.lastModifyTime DESC
    </select>
    <!--查询组织机构列表的智慧统计信息，并进行分页显示-->
    <select id="organizationListStatistics"
            resultType="jnpf.model.cultivate.vo.promotion.FtbCultivateMapsOrgWisdomStatisticVO">
        SELECT
        *
        FROM
        (
        SELECT
        a.*,
        b.F_PostId iniPostId,
        b.F_PostName iniPosition,
        COUNT( me.F_UserId ) AS numberOfEmplo,
        me.F_OrgId enableOrgId,
        me.F_OrgName enableOrgName
        FROM
        (
        SELECT
        pr.F_Id AS id,
        pr.F_Promotion mapName,
        COUNT( DISTINCT post.F_PostId ) AS mapLevel,
        pr.F_CreatorTime creationTime,
        F_PromotionBusinessId AS mapID
        FROM
        ftb_cultivate_promotion_new AS pr
        LEFT JOIN ftb_cultivate_promotion_post_new AS post ON pr.F_Id = post.F_PromotionId
        WHERE pr.F_EnableMark = 0
        GROUP BY
        pr.F_Id
        ) a
        LEFT JOIN ftb_cultivate_promotion_post_new AS b ON a.id = b.F_PromotionId
        AND b.F_Level = 1
        LEFT JOIN (
        SELECT DISTINCT
        me.F_UserId,
        me.F_PromotionId,
        me.F_OrgId,
        me.F_OrgName
        FROM
        ftb_cultivate_position_cource_learning a
        JOIN ftb_cultivate_position_course b ON a.F_CourceId = b.F_CourseId
        JOIN ftb_cultivate_promotion_member_new AS me ON me.F_UserId = a.F_UserId
        JOIN ftb_cultivate_course as course ON b.F_CourseId = course.F_Id
        WHERE
        b.F_EnabledMark = 0 and (me.F_DeleteMark is null or me.F_DeleteMark = 0 ) and a.F_EnabledMark = 0
        AND course.F_EnabledMark = 0
        AND a.F_state IN ( 1, 2 )
        <if test="dto.selectOrganization != null and dto.selectOrganization.size() > 0">
            AND me.F_OrgId IN
            <foreach collection="dto.selectOrganization" item="orgId" open="(" separator="," close=")">
                #{orgId}
            </foreach>
        </if>
        GROUP BY
        me.F_UserId,
        me.F_PromotionId
        ) AS me ON me.F_PromotionId = a.id
        <where>
            a.mapLevel != 0
            <if test="dto.startTime != null and dto.endTime != null">
                AND a.creationTime BETWEEN #{dto.startTime} AND #{dto.endTime}
            </if>
            <if test="dto.keyWords != null and dto.keyWords != ''">
                AND(
                b.F_PostName like CONCAT('%',#{dto.keyWords},'%') or
                a.mapName like CONCAT('%',#{dto.keyWords},'%') or
                a.mapID like CONCAT('%',#{dto.keyWords},'%')
                )
            </if>
        </where>
        GROUP BY
        a.id,me.F_OrgId
        ORDER BY
        a.creationTime DESC
        ) b
        WHERE
        b.enableOrgName IS NOT NULL
    </select>
    <!--查询个人列表的智慧统计信息，并进行分页显示-->
    <select id="personListStatistics"
            resultType="jnpf.model.cultivate.vo.promotion.FtbCultivateMapsPersonWisdomStatisticVO">
        SELECT
        me.F_Id,
        me.F_PromotionId promotionId,
        me.F_TenantId,
        me.F_CreatorUserId,
        me.F_CreatorTime creationTime,
        me.F_LastModifyUserId,
        me.F_LastModifyTime,
        me.F_DeleteUserId,
        me.F_DeleteTime,
        me.F_OrgId,
        me.F_OrgName organization,
        me.F_PositionName  initialPosition,
        CASE
        WHEN me.F_GradeName IS NOT NULL AND me.F_GradeName != ''
        THEN concat(me.F_PositionName,"_",me.F_GradeName)
        ELSE me.F_PositionName
        END employeeSCurrentPosition,
        me.F_UserId employeeID,
        me.F_UserName fullName,
        pr.F_PromotionBusinessId as mapID,
        pr.F_Promotion currentlyEnabledMapName
        FROM
        ftb_cultivate_promotion_member_new AS me
        INNER JOIN ftb_cultivate_promotion_new AS pr ON pr.F_Id = me.F_PromotionId
        INNER JOIN  (
        SELECT DISTINCT
        me.F_UserId,
        me.F_PromotionId,
        me.F_OrgId,
        me.F_OrgName
        FROM
        ftb_cultivate_position_cource_learning a
        JOIN ftb_cultivate_position_course b ON a.F_CourceId = b.F_CourseId
        JOIN ftb_cultivate_promotion_member_new AS me ON me.F_UserId = a.F_UserId
        JOIN ftb_cultivate_course as course ON b.F_CourseId = course.F_Id
        WHERE
        (me.F_DeleteMark is null or me.F_DeleteMark = 0) and
        b.F_EnabledMark = 0
        AND a.F_EnabledMark = 0
        AND course.F_EnabledMark = 0
        AND a.F_state IN ( 1, 2 )
        GROUP BY
        me.F_UserId,
        me.F_PromotionId
        ) a on a.F_UserId =me.F_UserId
        <where>
            pr.F_EnableMark=0
            <if test="dto.mapId != null">
                and  pr.F_Id =#{dto.mapId}
            </if>
            <if test="dto.keyWords != null and dto.keyWords != ''">
                and
                (
                me.F_UserName like CONCAT('%',#{dto.keyWords},'%') or
                me.F_PositionName like CONCAT('%',#{dto.keyWords},'%') or
                me.F_GradeName like CONCAT('%',#{dto.keyWords},'%') or
                me.F_OrgName like CONCAT('%',#{dto.keyWords},'%') or
                pr.F_Promotion like CONCAT('%',#{dto.keyWords},'%') or
                pr.F_PromotionBusinessId like CONCAT('%',#{dto.keyWords},'%')
                )
            </if>
            <if test="dto.startTime != null and dto.endTime != null">
                and  me.F_CreatorTime between #{dto.startTime} and #{dto.endTime}
            </if>

            <if test="dto.selectPeoples != null and dto.selectPeoples.size() > 0">
                and  me.F_UserId in
                <foreach collection="dto.selectPeoples" item="userId" open="(" separator="," close=")">
                    #{userId}
                </foreach>
            </if>
            <if test="dto.iniPostId != null and  dto.iniPostId !=''">
                AND me.F_PositionId =#{dto.iniPostId}
            </if>

        </where>
        GROUP BY me.F_UserId,pr.F_Id
    </select>
    <select id="queryStudyMapList" resultType="jnpf.model.cultivate.vo.promotion.FtbCultivateMapSimpleDto">
        select cpn.F_id as promotionId,cpn.F_Promotion as promotionName, cpn.F_CreatorTime as creatorTime,cppn.F_PostId as postId,cpn.F_PromotionBusinessId as promotionBusinessId
        from ftb_cultivate_promotion_new as cpn join  ftb_cultivate_promotion_post_new as cppn on cpn.F_id = cppn.F_PromotionId WHERE
            cpn.F_EnableMark=0 and cppn.F_Level = 1
        <if test="dto.keyWords != null and dto.keyWords != ''">
            AND(
            cpn.F_Promotion like CONCAT('%',#{dto.keyWords},'%') or
            cpn.F_PromotionBusinessId = #{dto.keyWords}
            )
        </if>

    </select>
    <select id="queryStudyUser" resultType="jnpf.model.cultivate.vo.promotion.FtbCultivateMapStudyUserDto">
        SELECT DISTINCT
            me.F_UserId as userId,
            me.F_PromotionId as promotionId,
            me.F_PositionId as  postId

        FROM
            ftb_cultivate_position_cource_learning a
                JOIN ftb_cultivate_position_course b ON a.F_CourceId = b.F_CourseId
                JOIN ftb_cultivate_promotion_member_new AS me ON me.F_UserId = a.F_UserId
                JOIN ftb_cultivate_course as course ON b.F_CourseId = course.F_Id
        WHERE
            b.F_EnabledMark = 0 and (me.F_DeleteMark is null or me.F_DeleteMark = 0 ) and a.F_EnabledMark = 0
          AND course.F_EnabledMark = 0
          AND a.F_state IN ( 1, 2 )

        <if test="queryPromotionIds != null and queryPromotionIds.size() > 0">
            and  me.F_PromotionId in
            <foreach collection="queryPromotionIds" item="promotionId" open="(" separator="," close=")">
                #{promotionId}
            </foreach>
        </if>

    </select>
    <select id="queryMapPostNum" resultType="jnpf.model.cultivate.vo.promotion.FtbCultivatePromotionLevelMapVO">
        select F_PromotionId as promotionId,F_PostId as postId from ftb_cultivate_promotion_post_new where  F_PromotionId in
                                                     <foreach collection="queryPromotionIds" item="promotionId" open="(" separator="," close=")">
                                                         #{promotionId}
                                                     </foreach>
    </select>
</mapper>