<?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">
<!--  Copyright (c) 京北方信息技术股份有限公司 Corporation 2022 . All rights reserved. -->
<!--  Mybatis数据库持久层（业务层映射文件）：应用表 -->
<!--  version: 1.0  -->
<!--  author:  maven-cctp-plugin    -->
<!--  date:  2022-10-13 10:00:44  -->
<mapper namespace="net.northking.cctp.scriptcase.db.dao.AppDao">


  <!-- 请在本文件内添加自定义SQL语句 -->

  <resultMap id="DetailMap" type="net.northking.cctp.scriptcase.dto.app.AppDetailDto">
      <!-- 应用id -->
    <id column="id" jdbcType="VARCHAR" property="id"/>
                                              <!-- 应用名称 -->
    <result column="app_name" jdbcType="VARCHAR" property="appName"/>
        <!-- 应用大小 -->
    <result column="app_size" jdbcType="VARCHAR" property="appSize"/>
        <!-- 包名称 -->
    <result column="package_name" jdbcType="VARCHAR" property="packageName"/>
        <!-- 构建版本 -->
    <result column="build_version" jdbcType="VARCHAR" property="buildVersion"/>
        <!-- 平台类型$$$0-android,1-IOS -->
    <result column="platform" jdbcType="VARCHAR" property="platform"/>
        <!-- 脚本文件地址 -->
    <result column="app_url" jdbcType="VARCHAR" property="appUrl"/>
        <!-- 版本说明 -->
    <result column="version_mark" jdbcType="VARCHAR" property="versionMark"/>
        <!-- 设备归属对象ID -->
    <result column="owner_id" jdbcType="VARCHAR" property="ownerId"/>
        <!-- 租户ID -->
    <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <!-- app图标 -->
    <result column="app_icon" jdbcType="BLOB" property="appIcon"/>
        <!-- 备注 -->
    <result column="remarks" jdbcType="VARCHAR" property="remarks"/>
        <!-- 版本编号 -->
    <result column="version_code" jdbcType="INTEGER" property="versionCode"/>
        <!-- activity -->
    <result column="activity" jdbcType="VARCHAR" property="activity"/>
        <!-- 上传人 -->
    <result column="created_by" jdbcType="VARCHAR" property="createdBy"/>
        <!-- 创建时间 -->
    <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
        <!-- 更新人 -->
    <result column="updated_by" jdbcType="VARCHAR" property="updatedBy"/>
        <!-- 更新时间 -->
    <result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime"/>
    </resultMap>

  <select id="pagingQuery" resultMap="DetailMap" parameterType="net.northking.cctp.scriptcase.db.entity.App">
    select
    id
    ,app_name
    ,app_size
    ,package_name
    ,build_version
    ,platform
    ,app_url
    ,version_mark
    ,owner_id
    ,tenant_id
    ,app_icon
    ,remarks
    ,version_code
    ,activity
    ,created_by
    ,created_time
    ,updated_by
    ,updated_time
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND" >
      <if test="id != null">
        AND id = #{id,jdbcType=VARCHAR}
      </if>
      <if test="appName != null">
        AND app_name = #{appName,jdbcType=VARCHAR}
      </if>
      <if test="appSize != null">
        AND app_size = #{appSize,jdbcType=VARCHAR}
      </if>
      <if test="packageName != null">
        AND package_name = #{packageName,jdbcType=VARCHAR}
      </if>
      <if test="buildVersion != null">
        AND build_version = #{buildVersion,jdbcType=VARCHAR}
      </if>
      <if test="platform != null">
        AND platform = #{platform,jdbcType=VARCHAR}
      </if>
      <if test="appUrl != null">
        AND app_url = #{appUrl,jdbcType=VARCHAR}
      </if>
      <if test="versionMark != null">
        AND version_mark = #{versionMark,jdbcType=VARCHAR}
      </if>
      <if test="ownerId != null">
        AND owner_id = #{ownerId,jdbcType=VARCHAR}
      </if>
      <if test="tenantId != null">
        AND tenant_id = #{tenantId,jdbcType=VARCHAR}
      </if>
      <if test="appIcon != null">
        AND app_icon = #{appIcon,jdbcType=BLOB}
      </if>
      <if test="remarks != null">
        AND remarks = #{remarks,jdbcType=VARCHAR}
      </if>
      <if test="versionCode != null">
        AND version_code = #{versionCode,jdbcType=INTEGER}
      </if>
      <if test="activity != null">
        AND activity = #{activity,jdbcType=VARCHAR}
      </if>
      <if test="createdBy != null">
        AND created_by = #{createdBy,jdbcType=VARCHAR}
      </if>
      <if test="createdTime != null">
        AND created_time = #{createdTime,jdbcType=TIMESTAMP}
      </if>
      <if test="updatedBy != null">
        AND updated_by = #{updatedBy,jdbcType=VARCHAR}
      </if>
      <if test="updatedTime != null">
        AND updated_time = #{updatedTime,jdbcType=TIMESTAMP}
      </if>
    </trim>
  </select>
  <select id="queryCreator" resultType="net.northking.cctp.scriptcase.dto.app.AppCreator"
          parameterType="net.northking.cctp.scriptcase.dto.app.AppQueryDto">
    select created_by as creator from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND">
      <if test="id != null">
        AND ID = #{id,jdbcType=VARCHAR}
      </if>
      <if test="appName != null">
        AND LOCATE(#{appName,jdbcType=VARCHAR},APP_NAME) &gt; 0
<!--        AND APP_NAME like "%"#{appName,jdbcType=VARCHAR}"%"-->
      </if>
      <if test="packageName != null">
        AND binary PACKAGE_NAME = #{packageName,jdbcType=VARCHAR}
      </if>
      <if test="buildVersion != null">
        AND LOCATE(#{buildVersion,jdbcType=VARCHAR},BUILD_VERSION) &gt; 0
      </if>
      <if test="platform != null">
        AND PLATFORM = #{platform,jdbcType=VARCHAR}
      </if>
      <if test="tenantId != null">
        AND TENANT_ID = #{tenantId,jdbcType=VARCHAR}
      </if>
      <if test="remarks != null">
<!--        AND REMARKS like "%"#{remarks,jdbcType=VARCHAR}"%"-->
        AND LOCATE(#{remarks,jdbcType=VARCHAR},REMARKS) &gt; 0
      </if>
      <if test="createdBy != null">
        AND CREATED_BY = #{createdBy,jdbcType=VARCHAR}
      </if>
      <if test="creatorList != null and creatorList.size() > 0">
        and CREATED_BY in
        <foreach collection="creatorList" item="creator" open="(" separator="," close=")">
          #{creator,jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="createdTime != null">
        AND CREATED_TIME like concat('%',left(#{createdTime,jdbcType=TIMESTAMP},10),'%')
      </if>
      <if test="ownerIdList != null and ownerIdList.size() > 0">
        and OWNER_ID in
        <foreach collection="ownerIdList" item="owner" open="(" separator="," close=")">
          #{owner,jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="ownerId != null">
        AND OWNER_ID = #{ownerId,jdbcType=VARCHAR}
      </if>
    </trim>
    group by CREATED_BY
  </select>

  <select id="myPagingQuery" parameterType="net.northking.cctp.scriptcase.dto.app.AppQueryDto"
          resultType="net.northking.cctp.scriptcase.dto.app.AppDetailDto">
    select
    ID
    ,APP_NAME
    ,APP_SIZE
    ,PACKAGE_NAME
    ,BUILD_VERSION
    ,PLATFORM
    ,APP_URL
    ,VERSION_MARK
    ,OWNER_ID
    ,TENANT_ID
    ,APP_ICON
    ,REMARKS
    ,VERSION_CODE
    ,ACTIVITY
    ,CREATED_BY
    ,CREATED_TIME
    ,UPDATED_BY
    ,UPDATED_TIME
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND">
      <if test="id != null">
        AND ID = #{id,jdbcType=VARCHAR}
      </if>
      <if test="appName != null">
<!--        AND APP_NAME like "%"#{appName,jdbcType=VARCHAR}"%"-->
        AND LOCATE(#{appName,jdbcType=VARCHAR},APP_NAME) &gt; 0
      </if>
      <if test="packageName != null">
        AND binary PACKAGE_NAME = #{packageName,jdbcType=VARCHAR}
      </if>
      <if test="buildVersion != null">
        AND LOCATE(#{buildVersion,jdbcType=VARCHAR},BUILD_VERSION) &gt; 0
      </if>
      <if test="platform != null">
        AND PLATFORM = #{platform,jdbcType=VARCHAR}
<!--        <choose>-->
<!--&lt;!&ndash;          ios应用&ndash;&gt;-->
<!--          <when test="platform == 1">-->
<!--            AND PLATFORM = #{platform,jdbcType=VARCHAR}-->
<!--          </when>-->
<!--&lt;!&ndash;          android和鸿蒙&ndash;&gt;-->
<!--          <otherwise>-->
<!--            AND PLATFORM IN('0','2')-->
<!--          </otherwise>-->
<!--        </choose>-->
      </if>
      <if test="tenantId != null">
        AND TENANT_ID = #{tenantId,jdbcType=VARCHAR}
      </if>
      <if test="remarks != null">
<!--        AND REMARKS like "%"#{remarks,jdbcType=VARCHAR}"%"-->
        AND LOCATE(#{remarks,jdbcType=VARCHAR},REMARKS) &gt; 0
      </if>
      <if test="createdBy != null">
        AND CREATED_BY = #{createdBy,jdbcType=VARCHAR}
      </if>
      <if test="creatorList != null and creatorList.size() > 0">
        and CREATED_BY in
        <foreach collection="creatorList" item="creator" open="(" separator="," close=")">
          #{creator,jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="createdTime != null">
        AND CREATED_TIME like concat('%',left(#{createdTime,jdbcType=TIMESTAMP},10),'%')
      </if>
      <if test="ownerIdList != null and ownerIdList.size() > 0">
        and OWNER_ID in
        <foreach collection="ownerIdList" item="owner" open="(" separator="," close=")">
          #{owner,jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="ownerId != null">
        AND OWNER_ID = #{ownerId,jdbcType=VARCHAR}
      </if>
    </trim>
    order by CREATED_TIME desc
  </select>


  <select id="queryAppList" resultMap="BaseResultMap" parameterType="net.northking.cctp.scriptcase.db.entity.App">
    select
    *
    from
    <include refid="Table_Name"/>
    where
    package_name =  #{packageName, jdbcType=VARCHAR}
    and platform = #{platform, jdbcType=VARCHAR}
    and owner_id = #{ownerId, jdbcType=VARCHAR}
    order by CREATED_TIME desc
  </select>

  <select id="checkAppAlive" parameterType="list" resultMap="BaseResultMap">
    select * from
    <include refid="Table_Name"/>
    where id in
    <foreach collection="ids" item="id" open="(" separator="," close=")">
      #{id,jdbcType=VARCHAR}
    </foreach>
  </select>
  <select id="PagingQueryGroupByPackage" resultMap="BaseResultMap">
    select
    package_name
    ,platform
    ,owner_id
    from
    <include refid="Table_Name"/>
    <where>
      <if test="appName != null">
        AND LOCATE(#{appName,jdbcType=VARCHAR},APP_NAME) &gt; 0
      </if>
      <if test="packageName != null">
        AND binary package_name = #{packageName,jdbcType=VARCHAR}
      </if>
      <if test="platforms != null">
        and platform in
        <foreach collection="platforms" item="platform" open="(" separator="," close=")">
          #{platform}
        </foreach>
      </if>
      <if test="ownerId != null">
        and owner_id = #{ownerId, jdbcType=VARCHAR}
      </if>
    </where>
    group by package_name
  </select>
    <select id="selectCreatorByProject" resultType="java.lang.String">
      SELECT created_by FROM app
      WHERE owner_id = #{projectId}
      GROUP BY created_by
    </select>


</mapper>