<?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.upyuns.platform.rs.website.mapper.TModelMapper">

    <resultMap id="BaseResultMap" type="com.upyuns.platform.rs.website.entity.TModel">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="modelCn" column="model_cn" jdbcType="VARCHAR"/>
        <result property="modelEn" column="model_en" jdbcType="VARCHAR"/>
        <result property="modelUnitName" column="model_unit_name" jdbcType="VARCHAR"/>
        <result property="favorites" column="favorites" jdbcType="BIGINT"/>
        <result property="views" column="views" jdbcType="BIGINT"/>
        <result property="parameters" column="parameters" jdbcType="BIGINT"/>
        <result property="releasedBy" column="released_by" jdbcType="VARCHAR"/>
        <result property="modelPosition" column="model_position" jdbcType="VARCHAR"/>
        <result property="releaseTime" column="release_time" jdbcType="TIMESTAMP"/>
        <result property="isOnlineExperience" column="is_online_experience" jdbcType="VARCHAR"/>
        <result property="onlineExperienceApi" column="online_experience_api" jdbcType="VARCHAR"/>
        <result property="details" column="details" jdbcType="VARCHAR"/>
        <result property="modelCersion" column="model_cersion" jdbcType="INTEGER"/>
        <result property="softwareFramework" column="software_framework" jdbcType="VARCHAR"/>
        <result property="hardwareType" column="hardware_type" jdbcType="VARCHAR"/>
        <result property="examineStatus" column="examine_status" jdbcType="TINYINT"/>
        <result property="status" column="status" jdbcType="TINYINT"/>
        <result property="isDel" column="is_del" jdbcType="TINYINT"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="createUser" column="create_user" jdbcType="BIGINT"/>
        <result property="lastUpdateTime" column="last_update_time" jdbcType="TIMESTAMP"/>
        <result property="lastUpdateUser" column="last_update_user" jdbcType="BIGINT"/>
    </resultMap>

    <sql id="Base_Column_List">
        id
        ,model_cn,model_en,
        model_unit_name,favorites,views,intro,
        parameters,released_by,model_position,
        release_time,is_online_experience,online_experience_api,
        details,model_version,software_framework,
        hardware_type,examine_status,status,
        is_del,create_time,create_user,
        last_update_time,last_update_user
    </sql>
    <insert id="insertTModelTypeRelevance">
       INSERT INTO t_model_type_relevance (model_id, model_type_id)
     VALUES
       <foreach collection="ids" item="id" separator=",">
            (#{modelId}, #{id})
       </foreach>
    </insert>
    <insert id="insertTModel" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO `rscp_website`.`t_model`( `model_cn`, `model_en`, `intro`, `model_unit_name`, `favorites`, `views`, `parameters`, `released_by`, `model_position`, `release_time`, `is_online_experience`, `online_experience_api`, `details`, `model_version`, `software_framework`, `hardware_type`, `examine_status`,  `create_user`,  `last_update_user`)
        VALUES (#{modelCn}, #{modelEn}, #{intro}, #{modelUnitName}, #{favorites}, #{views}, #{parameters}, #{releasedBy}, #{modelPosition}, #{releaseTime}, #{isOnlineExperience}, #{onlineExperienceApi}, #{details}, #{modelVersion}, #{softwareFramework}, #{hardwareType}, #{examineStatus}, #{createUser},  #{lastUpdateUser});
    </insert>
    <select id="selectList" resultType="com.upyuns.platform.rs.website.entity.TModel">
        SELECT
        t.id,
        t.model_cn,
        t.model_en,
        t.intro,
        t.model_unit_name,
        t.favorites,
        t.views,
        t.parameters,
        t.released_by,
        t.model_position,
        t.release_time,
        t.is_online_experience,
        t.online_experience_api,
        t.details,
        t.model_version,
        t.software_framework,
        t.hardware_type,
        t.examine_status,
        t.`status`,
        t.create_time,
        t.create_user,
        t.last_update_time,
        t.last_update_user
        FROM
        t_model t
        left join t_model_type_relevance ttr on t.id=ttr.model_id
        WHERE
        t.is_del = 0
        <if test="modelCn != null and modelCn != ''">
            AND t.model_cn like concat('%',#{modelCn},'%')
            OR t.model_en like concat('%',#{modelCn},'%')
        </if>
<!--        <if test="modelEn != null and modelEn != ''">-->
<!--            AND t.model_en like concat('%',#{modelEn},'%')-->
<!--        </if>-->
        <if test="releasedBy != null and releasedBy != ''">
            AND t.released_by like concat('%',#{releasedBy},'%')
        </if>
        <if test="softwareFramework != null and softwareFramework != ''">
            AND t.software_framework in
            <foreach item="item" index="index" collection="softwareFramework.split(',')"  open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="status != null">
            AND t.`status` = #{status}
        </if>
        <if test="examineStatus != null">
            AND t.`examine_status` = #{examineStatus}
        </if>
        <if test="modelVersion != null">
            AND t.`model_version` = #{modelVersion}
        </if>
        <if test="hardwareType != null">
            AND t.`hardware_type` = #{hardwareType}
        </if>
        <if test="modelTypeIds != null">
            AND ttr.`model_type_id` in (${modelTypeIds})
        </if>
        GROUP BY t.`id`
        <choose>
            <when test="orderBy == 1">
                ORDER BY t.`views` desc
            </when>
            <when test="orderBy == 2">
                ORDER BY t.create_time,t.id desc
            </when>
            <when test="orderBy == 3">
                ORDER BY ttr.`model_type_id`,t.id desc
            </when>
            <when test="orderBy == 4">
                ORDER BY t.released_by desc
            </when>
        </choose>
    </select>
    <select id="selectTypeIds" resultType="java.lang.Long">
        SELECT ttr.`model_type_id`
        FROM t_model_type_relevance ttr
        WHERE ttr.is_del = 0
          AND ttr.`model_id` = #{id}
    </select>
</mapper>
