<?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.younike.system.mapper.VideoMapper">
  <resultMap id="BaseResultMap" type="com.younike.system.domain.Video">
    <id column="video_id" property="videoId" />
    <result column="video_url" property="videoUrl" />
    <result column="video_face_url" property="videoFaceUrl" />
    <result column="video_category_id" property="videoCategoryId" />
    <result column="video_desc" property="videoDesc" />
    <result column="server_id" property="serverId" />
    <result column="merchant_id" property="merchantId" />
    <result column="is_merchant_top" property="isMerchantTop" />
    <result column="is_operation_top" property="isOperationTop" />
    <result column="del_flag" property="delFlag" />
    <result column="create_by" property="createBy" />
    <result column="create_time" property="createTime" />
    <result column="update_by" property="updateBy" />
    <result column="update_time" property="updateTime" />
    <result column="remark" property="remark" />
  </resultMap>
  <resultMap id="VideoVoResultMap" type="com.younike.system.domain.vo.VideoVo">
    <id column="video_id" property="videoId" />
    <result column="video_url" property="videoUrl" />
    <result column="video_face_url" property="videoFaceUrl" />
    <result column="video_category_id" property="videoCategoryId" />
    <result column="video_desc" property="videoDesc" />
    <result column="server_id" property="serverId" />
    <result column="merchant_id" property="merchantId" />
    <result column="is_merchant_top" property="isMerchantTop" />
    <result column="is_operation_top" property="isOperationTop" />
    <result column="del_flag" property="delFlag" />
    <result column="create_by" property="createBy" />
    <result column="create_time" property="createTime" />
    <result column="update_by" property="updateBy" />
    <result column="update_time" property="updateTime" />
    <result column="remark" property="remark" />
    <result column="real_name" property="serverName" />
    <result column="merchant_name" property="merchantName" />
    <result column="merchant_type" property="merchantType" />
    <result column="merchantAvatar" property="merchantAvatar" />
    <result column="serverAvatar" property="serverAvatar" />
    <result column="admin_id" property="adminId" />
    <result column="videoCategoryName" property="videoCategoryName" />
    <result column="parentCategoryName" property="parentCategoryName" />
    <result column="parent_id" property="parentId" />
  </resultMap>

  <sql id="Base_Column_List">
    video_id, video_url, video_face_url, video_category_id, video_desc, server_id, merchant_id,
    is_merchant_top, is_operation_top, del_flag, create_by, create_time, update_by, update_time, remark
  </sql>
  <sql id="VideoVoList">
    v.video_id, v.video_url, v.video_face_url, v.video_category_id, v.video_desc, v.server_id, v.merchant_id,
    v.is_merchant_top, v.is_operation_top, v.del_flag, v.create_by, v.create_time, v.update_by, v.update_time, v.remark,
    m.merchant_type,m.merchant_name,m.admin_id,m.avatar as merchantAvatar, su.real_name,su.avatar as serverAvatar,
    cg.category_name as videoCategoryName,cy.category_name as parentCategoryName,cg.parent_id
  </sql>

  <insert id="insertVideo" parameterType="com.younike.system.domain.Video">
    insert into video
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="videoUrl != null and videoUrl != ''">video_url,</if>
      <if test="videoFaceUrl != null and videoFaceUrl != ''">video_face_url,</if>
      <if test="videoCategoryId != null ">video_category_id,</if>
      <if test="videoDesc != null and videoDesc != ''">video_desc,</if>
      <if test="serverId != null">server_id,</if>
      <if test="merchantId != null">merchant_id,</if>
      <if test="isMerchantTop != null and isMerchantTop != ''">is_merchant_top,</if>
      <if test="isOperationTop != null and isOperationTop != ''">is_operation_top,</if>
      <if test="delFlag != null">del_flag,</if>
      <if test="createBy != null">create_by,</if>
      <if test="createTime != null">create_time,</if>
      <if test="updateBy != null">update_by,</if>
      <if test="updateTime != null">update_time,</if>
      <if test="remark != null and remark != ''">remark,</if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="videoUrl != null and videoUrl != ''">#{videoUrl},</if>
      <if test="videoFaceUrl != null and videoFaceUrl != ''">#{videoFaceUrl},</if>
      <if test="videoCategoryId != null ">#{videoCategoryId},</if>
      <if test="videoDesc != null and videoDesc != ''">#{videoDesc},</if>
      <if test="serverId != null">#{serverId},</if>
      <if test="merchantId != null">#{merchantId},</if>
      <if test="isMerchantTop != null and isMerchantTop != ''">#{isMerchantTop},</if>
      <if test="isOperationTop != null and isOperationTop != ''">#{isOperationTop},</if>
      <if test="delFlag != null">#{delFlag},</if>
      <if test="createBy != null">#{createBy},</if>
      <if test="createTime != null">#{createTime},</if>
      <if test="updateBy != null">#{updateBy},</if>
      <if test="updateTime != null">#{updateTime},</if>
      <if test="remark != null and remark != ''">#{remark},</if>
    </trim>
  </insert>

  <delete id="deleteByVideoId" parameterType="java.lang.Long">
    delete from video
    where video_id = #{videoId}
  </delete>

  <update id="updateVideo" parameterType="com.younike.system.domain.Video">
    update video
    <trim prefix="SET" suffixOverrides=",">
      <if test="videoUrl != null and videoUrl != ''">video_url = #{videoUrl},</if>
      <if test="videoFaceUrl != null and videoFaceUrl != ''">video_face_url = #{videoFaceUrl},</if>
      <if test="videoCategoryId != null">video_category_id = #{videoCategoryId},</if>
      <if test="videoDesc != null and videoDesc != ''">video_desc = #{videoDesc},</if>
      <if test="serverId != null">server_id = #{serverId},</if>
      <if test="merchantId != null">merchant_id = #{merchantId},</if>
      <if test="isMerchantTop != null and isMerchantTop != ''">is_merchant_top = #{isMerchantTop},</if>
      <if test="isOperationTop != null and isOperationTop != ''">is_operation_top = #{isOperationTop},</if>
      <if test="delFlag != null">del_flag = #{delFlag},</if>
      <if test="createBy != null">create_by = #{createBy},</if>
      <if test="createTime != null">create_time = #{createTime},</if>
      <if test="updateBy != null">update_by = #{updateBy},</if>
      <if test="updateTime != null">update_time = #{updateTime},</if>
      <if test="remark != null and remark != ''">remark = #{remark},</if>
    </trim>
    where video_id = #{videoId}
  </update>

  <select id="selectByVideoId" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from video
    where video_id = #{videoId}
  </select>
  <delete id="deleteVideoByVideoIds" parameterType="String">
      delete from video where video_id in
      <foreach close=")" separator="," open="(" collection="array" item="videoId">#{videoId} </foreach>
    </delete>

<!--  查询列表数据-->
  <select id="getVideoList" resultMap="VideoVoResultMap" parameterType="com.younike.system.param.VideoParam">
    select
        <include refid="VideoVoList"/>
    from video v
      LEFT JOIN server_user su on v.server_id=su.server_id and su.del_flag =0
      LEFT JOIN merchant m on m.merchant_id = v.merchant_id
      LEFT JOIN category cg ON cg.category_id = v.video_category_id AND cg.del_flag = '0'
      LEFT JOIN category cy ON cg.parent_id = cy.category_id AND cy.del_flag = '0'
     where
      v.del_flag=0
      and m.del_flag = 0
      <if test="bizType != null "> and cg.biz_type = #{bizType}</if>
      <if test="videoCategoryId != null "> and v.video_category_id = #{videoCategoryId}</if>
      <if test="parentId != null and parentId != ''">
        AND (cg.parent_id =#{parentId} or cg.category_id=#{parentId})
      </if>
      <if test="videoDesc != null and videoDesc != ''"> and v.video_desc = #{videoDesc}</if>
      <if test="merchantId != null "> and v.merchant_id = #{merchantId}</if>
      <if test="serverId != null "> and v.server_id = #{serverId}</if>
      <if test="isMerchantTop != null and isMerchantTop != ''"> and v.is_merchant_top = #{isMerchantTop}</if>
      <if test="isOperationTop != null and isOperationTop != ''"> and v.is_operation_top = #{isOperationTop}</if>
      <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
        AND date_format(v.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
      </if>
      <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
        AND date_format(v.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
      </if>
      <if test="serverName != null and serverName != ''">
        AND su.real_name like concat('%', #{serverName}, '%')
      </if>
    <if test="merchantId != null "> order by v.is_merchant_top  desc,v.create_time desc </if>
    <if test="merchantId == null or merchantId == ''">  order by v.is_operation_top desc,v.create_time desc </if>
  </select>

  <select id="getInfo" parameterType="java.lang.Long" resultMap="VideoVoResultMap">
    select
    <include refid="VideoVoList"/>
    from video v
    LEFT JOIN server_user su on v.server_id=su.server_id
    LEFT JOIN merchant m on m.merchant_id = v.merchant_id
    LEFT JOIN category cg ON cg.category_id = v.video_category_id AND cg.del_flag = '0'
    LEFT JOIN category cy ON cg.parent_id = cy.category_id AND cy.del_flag = '0'
    where
    su.del_flag =0
    and v.del_flag=0
    and m.del_flag = 0
    and v.video_id = #{videoId}
  </select>
<!--  会员端：获取首页运营推荐的视频、商家视频、全部视频列表-->
<select id="memberVideoList" resultMap="VideoVoResultMap" resultType="com.younike.system.param.VideoParam">
  select
  <include refid="VideoVoList"/>
  from video v
  LEFT JOIN server_user su on v.server_id=su.server_id
  LEFT JOIN merchant m on m.merchant_id = v.merchant_id
  LEFT JOIN category cg ON cg.category_id = v.video_category_id AND cg.del_flag = '0'
  LEFT JOIN category cy ON cg.parent_id = cy.category_id AND cy.del_flag = '0'
  where
   v.del_flag=0
  and m.del_flag = 0
  and m.shop_state ='002'
  and m.business_state ='001'
  and (su.del_flag = 0 or v.server_id  is null)
  <if test="merchantId != null "> and v.merchant_id = #{merchantId}</if>
  <if test="bizType != null "> and cg.biz_type = #{bizType}</if>
  <if test="parentId != null and parentId != ''">
    AND  (cg.parent_id =#{parentId} or cg.category_id=#{parentId})
  </if>
  <if test="isOperationTop != null and isOperationTop != ''"> and v.is_operation_top = #{isOperationTop}</if>
  <if test="isMerchantTop != null and isMerchantTop != ''"> and v.is_merchant_top = #{isMerchantTop}</if>
  <if test="merchantId != null ">
    order by v.is_merchant_top desc,v.is_operation_top desc,v.create_time desc
  </if>
  <if test="merchantId == null ">
    order by v.is_operation_top desc ,v.is_merchant_top desc,v.create_time desc
  </if>


</select>
<!--  会员端 ：服务人员视频列表-->
  <select id="serverVideoList" resultMap="VideoVoResultMap" resultType="com.younike.system.param.VideoParam">
    select
    <include refid="VideoVoList"/>
    from video v
    LEFT JOIN server_user su on v.server_id=su.server_id
    LEFT JOIN merchant m on m.merchant_id = v.merchant_id
    LEFT JOIN category cg ON cg.category_id = v.video_category_id AND cg.del_flag = '0'
    LEFT JOIN category cy ON cg.parent_id = cy.category_id AND cy.del_flag = '0'
    where
    su.del_flag =0
    and v.del_flag=0
    and m.del_flag = 0
    and m.shop_state ='002'
    and m.business_state ='001'
    and su.del_flag = 0
    <if test="isOperationTop != null and isOperationTop != ''"> and v.is_operation_top = #{isOperationTop}</if>
    <if test="isMerchantTop != null and isMerchantTop != ''"> and v.is_merchant_top = #{isMerchantTop}</if>
    <if test="bizType != null "> and cg.biz_type = #{bizType}</if>
    <if test="merchantId != null "> and v.merchant_id = #{merchantId}</if>
    <if test="serverId != null "> and v.server_id = #{serverId}</if>
    <if test="parentId != null and parentId != ''">
      AND (cg.parent_id =#{parentId} or cg.category_id=#{parentId})
    </if>
    order by v.is_operation_top desc ,v.is_merchant_top desc  ,v.create_time desc
  </select>

</mapper>
