<?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.FtbCultivateCaseBaseMapper">

    <!-- 用于Web端展示的案例列表查询 -->
    <select id="listDisplay" resultType="jnpf.model.cultivate.vo.casebase.FtbCultivateCaseBaseVO">
        SELECT
            ca.F_Id AS id,
            ca.F_CreatorUserId AS creatorUserId,
            ca.F_CaseDescription as caseDescription,
            ca.F_CreatorTime AS creatorTime,
            ca.F_LastModifyTime AS lastModifyTime,
            ca.F_CaseName caseName,
            ca.F_Status `status`,
            ca.F_SystemCaseId AS systemCaseId,
            sa.F_Name AS creatorUserName,
            ca.F_AuditUserId AS auditUserId,
            ca.F_AuditTime AS auditTime,
            IFNULL( ( SELECT COUNT( F_Id ) FROM ftb_cultivate_case_base_like WHERE F_GainedId = ca.F_Id ), 0)   AS numberOfLikes,
            IFNULL( ( SELECT COUNT( F_Id ) FROM ftb_cultivate_case_base_like WHERE F_GainedId = ca.F_Id and F_LikeUserId = #{dto.userId} ), 0)   AS haveYouLikedIt
        FROM
            ftb_cultivate_case_base AS ca
                LEFT JOIN ftb_personnels_staff_roster AS sa ON ca.F_CreatorUserId = sa.F_UserId
        <where>
            <if test="dto.flag != null and dto.flag != '' and  dto.flag == 0">
                AND ca.F_Status != 0
            </if>
            <if test="dto.flag != null and dto.flag != '' and  dto.flag == 1">
                AND ca.F_Status = 2
            </if>
            <if test="dto.keyWords != null and dto.keyWords != ''">
                AND
                (
                ca.F_CaseName LIKE CONCAT('%',#{dto.keyWords},'%')
                or ca.F_SystemCaseId LIKE CONCAT('%',#{dto.keyWords},'%')
                )
            </if>
            <if test="dto.approvalStatus != null and dto.approvalStatus != ''">
                AND ca.F_Status = #{dto.approvalStatus}
            </if>
            <if test="dto.flag != null and dto.flag != '' and  dto.flag == 2">
                AND ca.F_CreatorUserId = #{dto.userId}
            </if>
        </where>
        ORDER BY
            ca.F_CreatorTime DESC
    </select>

    <!-- 用于移动端展示的案例列表查询 -->
    <select id="listDisplayForApp" resultType="jnpf.model.cultivate.vo.casebase.FtbCultivateCaseBaseVO">
        SELECT
        ca.F_Id AS id,
        ca.F_CreatorUserId AS creatorUserId,
        ca.F_CaseDescription as caseDescription,
        ca.F_CreatorTime AS creatorTime,
        ca.F_LastModifyTime AS lastModifyTime,
        ca.F_CaseName caseName,
        ca.F_Status `status`,
        ca.F_SystemCaseId AS systemCaseId,
        sa.F_Name AS creatorUserName,
        ca.F_AuditUserId AS auditUserId,
        ca.F_AuditTime AS auditTime,
        IFNULL( ( SELECT COUNT( F_Id ) FROM ftb_cultivate_case_base_like WHERE F_GainedId = ca.F_Id ), 0)   AS numberOfLikes,
        IFNULL( ( SELECT COUNT( F_Id ) FROM ftb_cultivate_case_base_like WHERE F_GainedId = ca.F_Id and F_LikeUserId = #{dto.userId} ), 0)   AS haveYouLikedIt
        FROM
        ftb_cultivate_case_base AS ca
        LEFT JOIN ftb_personnels_staff_roster AS sa ON ca.F_CreatorUserId = sa.F_UserId
        <where>
            <if test="dto.flag != null and dto.flag != '' and  dto.flag == 0">
                AND ca.F_Status != 0
            </if>
            <if test="dto.flag != null and dto.flag != '' and  dto.flag == 1">
                AND ca.F_Status = 2
            </if>
            <if test="dto.keyWords != null and dto.keyWords != ''">
                AND ca.F_CaseName LIKE CONCAT('%',#{dto.keyWords},'%')
            </if>
            <if test="dto.approvalStatus != null and dto.approvalStatus != ''">
                AND ca.F_Status = #{dto.approvalStatus}
            </if>
            <if test="dto.flag != null and dto.flag != '' and  dto.flag == 2">
                AND ca.F_CreatorUserId = #{dto.userId}
            </if>
        </where>
        ORDER BY
        ca.F_LastModifyTime desc
    </select>

</mapper>