<?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.testor.module.fives.dao.TFivesToolUseDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <select id="selectToolUserLists" resultType="com.testor.module.fives.model.vo.TFivesToolUseListVO"
            parameterType="com.testor.module.fives.model.dto.TFivesToolUseParam">
        SELECT
        tu.*
        FROM
        (
        SELECT
        tftu.id,
        tftu.use_type,
        tfto.theme_name,
        tftu.create_by,
        tftu.create_date,
        tftu.grain_org_id,
        tftu.area_org_id,
        tftu.org_id,
        tftu.host_post,
        tfto.id AS tool_id,
        CASE
        tftu.score_type
        WHEN '7' THEN
        '7'
        WHEN '6' THEN
        '6' ELSE tftu.score
        END AS score
        FROM
        t_fives_tool_use AS tftu
        LEFT JOIN t_fives_tool_opl AS tfto ON tftu.id = tfto.parent_id
        WHERE
        tftu.use_type = 0 AND tftu.status='0' UNION
        SELECT
        tftu.id,
        tftu.use_type,
        tftc.case_name AS theme_name,
        tftu.create_by,
        tftu.create_date,
        tftu.grain_org_id,
        tftu.area_org_id,
        tftu.org_id,
        tftu.host_post,
        tftc.id AS tool_id,
        CASE
        tftu.score_type
        WHEN '7' THEN
        '7'
        WHEN '6' THEN
        '6' ELSE tftu.score
        END AS score
        FROM
        t_fives_tool_use AS tftu
        LEFT JOIN t_fives_tool_case AS tftc ON tftu.id = tftc.parent_id
        WHERE
        tftu.use_type = 1 AND tftu.status='0' UNION
        SELECT
        tftu.id,
        tftu.use_type,
        tftf.course_name AS theme_name,
        tftu.create_by,
        tftu.create_date,
        tftu.grain_org_id,
        tftu.area_org_id,
        tftu.org_id,
        tftu.host_post,
        tftf.id AS tool_id,
        CASE
        tftu.score_type
        WHEN '7' THEN
        '7'
        WHEN '6' THEN
        '6' ELSE tftu.score
        END AS score
        FROM
        t_fives_tool_use AS tftu
        LEFT JOIN t_fives_tool_focus AS tftf ON tftu.id = tftf.parent_id
        WHERE
        tftu.use_type = 2 AND tftu.status='0'
        ) AS tu
        where (tu.org_id in(
        <foreach separator="," collection="param.companyIds" item="item" index="index">
            #{item}
        </foreach>
        )
        <if test="param.toolList!=null and param.toolList!=''">
            or tu.host_post in(
            <foreach separator="," collection="param.companyIds" item="item" index="index">
                #{item}
            </foreach>
            )
        </if>)
        <if test="param.useType!=null and param.useType!=''">
            AND tu.use_type=#{param.useType}
        </if>
        <if test="param.grainOrgId!=null and param.grainOrgId!=''">
            AND tu.grain_org_id=#{param.grainOrgId}
        </if>
        <if test="param.areaOrgId!=null and param.areaOrgId!=''">
            AND tu.area_org_id=#{param.areaOrgId}
        </if>
        <if test="param.themeName!=null and param.themeName!=''">
            AND tu.theme_name LIKE CONCAT('%',#{param.themeName},'%')
        </if>
        <if test="param.startDate!=null and param.startDate!=''">
            AND tu.create_date BETWEEN #{param.startDate} AND #{param.endDate}
        </if>
        <if test="param.score == '6'.toString()" >
            AND tu.score = #{param.score}
        </if>
        <if test="param.score == '7'.toString()" >
            AND tu.score = #{param.score}
        </if>
        <if test="param.score == '8'.toString()" >
            AND tu.score = #{param.score}
        </if>
        <if test="param.score!=null and param.score!='' and param.score!=6 and param.score!=7 and param.score != 8 " >
            AND tu.score &gt;= #{param.score} and tu.score not in (6,7,8)
        </if>
        <if test="param.brightPost!=null and param.brightPost!=''">
            AND tu.score >= 4 and tu.score not in (6,7,8)
        </if>
        ORDER BY tu.score DESC
    </select>

    <resultMap id="BaseResultMap" type="com.testor.module.fives.model.domain.TFivesToolUse">
        <id column="id" property="id"/>
        <id column="ref_id" property="refId"/>
        <id column="parent_id" property="parentId"/>
        <id column="tenant_id" property="tenantId"/>
        <id column="process_id" property="processId"/>
        <id column="process_status" property="processStatus"/>
        <id column="grain_org_id" property="grainOrgId"/>
        <id column="area_org_id" property="areaOrgId"/>
        <id column="use_type" property="useType"/>
        <id column="serial_number" property="serialNumber"/>
    </resultMap>
</mapper>