<?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.fdage.dao.cust.TbCollectionMapperCust">
  <select id="timeList" resultType="com.fdage.pojo.TbCollectionTime">
    select id, name
    from tb_collection_time
    order by order_num
  </select>

  <select id="typeList" resultType="com.fdage.pojo.TbCollectionType">
    select id, name
    from tb_collection_type
    WHERE 1 = 1
    <if test="state != null">
      and state = #{state}
    </if>
  </select>

  <select id="findList" parameterType="com.fdage.request.RequestCollection" resultType="com.fdage.respon.ResponCollection">
    select a.id, a.name, a.type_id as typeId, a.time_id as timeId, a.discovery_time as discoveryTime,
    a.repair_time as repairTime, a.venue, a.model_url as modelUrl, a.content_url as contentUrl, a.state,
    a.create_time as createTime, a.description, b.name as timeName, c.name as typeName, a.pic, a.num,
    a.like_num as likeNum, a.open_num as openNum, a.search_num as searchNum, download_num as downloadNum,
    a.unity_pic as unityPic, a.unity_url as unityUrl, a.icon, a.dir_code as dirCode, a.type, a.url_list, a.qr_code
    from tb_collection a LEFT JOIN tb_collection_time b on a.time_id = b.id
    LEFT JOIN tb_collection_type c ON a.type_id = c.id
    WHERE 1 = 1
    <if test="typeId != null">
      and a.type_id = #{typeId}
    </if>
    <if test="timeId != null">
      and a.time_id = #{timeId}
    </if>
    <if test="name != null and name != ''">
      and a.name LIKE CONCAT('%', #{name}, '%')
    </if>
    <if test="state != null">
      and a.state = #{state}
    </if>
  </select>

  <select id="findListByExhibitionId" parameterType="java.lang.Long" resultType="com.fdage.respon.ResponCollection">
    select a.id, a.name, a.type_id as typeId, a.time_id as timeId, a.discovery_time as discoveryTime,
    a.repair_time as repairTime, a.venue, a.model_url as modelUrl, a.content_url as contentUrl, a.state,
    a.create_time as createTime, a.description, b.name as timeName, c.name as typeName, a.pic, a.num,
    a.like_num as likeNum, a.open_num as openNum, a.search_num as searchNum, download_num as downloadNum,
     a.unity_pic as unityPic, a.unity_url as unityUrl, a.type, a.url_list, a.qr_code
    from tb_collection a LEFT JOIN tb_collection_time b on a.time_id = b.id
    LEFT JOIN tb_collection_type c ON a.type_id = c.id
    LEFT JOIN tb_exhibition_collection d ON a.id = d.collection_id
    WHERE d.exhibition_id = #{id}
  </select>

  <select id="searchListByName" parameterType="com.fdage.request.RequestCollection" resultType="com.fdage.respon.ResponCollection">
    select a.id, a.name, a.type_id as typeId, a.time_id as timeId, a.discovery_time as discoveryTime,
    a.repair_time as repairTime, a.venue, a.model_url as modelUrl, a.content_url as contentUrl, a.state,
    a.create_time as createTime, a.description, b.name as timeName, c.name as typeName, a.pic, a.num,
    a.like_num as likeNum, a.open_num as openNum, a.search_num as searchNum, download_num as downloadNum,
    a.unity_pic as unityPic, a.unity_url as unityUrl, a.type, a.url_list, a.qr_code
    from tb_collection a LEFT JOIN tb_collection_time b on a.time_id = b.id
    LEFT JOIN tb_collection_type c ON a.type_id = c.id
    WHERE 1 = 1
    <if test="name != null and name != ''">
      and a.name LIKE CONCAT('%', #{name}, '%')
    </if>

    <if test="typeId != null and typeId != ''">
      and a.type_id = #{typeId}
    </if>

  </select>

  <update id="addLikeNumById" parameterType="java.lang.Long">
    UPDATE tb_collection SET like_num = like_num + 1 WHERE id = #{id}
  </update>

  <update id="addOpenNumById" parameterType="java.lang.Long">
    UPDATE tb_collection SET open_num = open_num + 1 WHERE id = #{id}
  </update>

  <update id="addSearchNumById" parameterType="java.lang.Long">
    UPDATE tb_collection SET search_num = search_num + 1 WHERE id = #{id}
  </update>

  <update id="addDownloadNumById" parameterType="java.lang.Long">
    UPDATE tb_collection SET download_num = download_num + 1 WHERE id = #{id}
  </update>

  <select id="findByName" parameterType="java.lang.String" resultMap="com.fdage.dao.base.TbCollectionMapper.BaseResultMap">
    select
    <include refid="com.fdage.dao.base.TbCollectionMapper.Base_Column_List" />
    ,
    <include refid="com.fdage.dao.base.TbCollectionMapper.Blob_Column_List" />
    from tb_collection
    where name = #{name}
  </select>

  <select id="collectionTotal" resultType="com.fdage.respon.ResponStatistics">
    SELECT COUNT(*) AS total, SUM(like_num) AS likeTotal, SUM(download_num) AS downloadNum
    FROM `tb_collection`
  </select>

  <select id="findListOrderBy" parameterType="List" resultType="com.fdage.respon.ResponCollection">
    select a.id, a.name, a.type_id as typeId, a.time_id as timeId, a.discovery_time as discoveryTime,
    a.repair_time as repairTime, a.venue, a.model_url as modelUrl, a.content_url as contentUrl, a.state,
    a.create_time as createTime, a.description, b.name as timeName, c.name as typeName, a.pic, a.num,
    a.like_num as likeNum, a.open_num as openNum, a.search_num as searchNum, download_num as downloadNum,
    a.unity_pic as unityPic, a.unity_url as unityUrl, a.type, a.url_list, a.qr_code
    from tb_collection a LEFT JOIN tb_collection_time b on a.time_id = b.id
    LEFT JOIN tb_collection_type c ON a.type_id = c.id
    WHERE 1 = 1
    <choose>
      <when test="sidx != null and sidx.trim() != ''">
        order by ${sidx} ${order}
      </when>
      <otherwise>
        order by a.id desc
      </otherwise>
    </choose>

  </select>

  <select id="typeTotal" resultType="Map">
    SELECT SUM(a.type_id) AS typeNum, b.name AS typeName
    FROM `tb_collection` a LEFT JOIN `tb_collection_type` b ON a.type_id = b.id
    GROUP BY a.type_id
  </select>
</mapper>