<?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.pangu.application.mapper.ApplicationMapper">

    <!-- 应用VO结果映射 -->
    <resultMap id="ApplicationVOResult" type="com.pangu.application.domain.vo.ApplicationVO">
        <id property="appId" column="app_id"/>
        <result property="appCode" column="app_code"/>
        <result property="appName" column="app_name"/>
        <result property="appSecret" column="app_secret"/>
        <result property="contactPerson" column="contact_person"/>
        <result property="contactPhone" column="contact_phone"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
        <result property="remark" column="remark"/>
        <collection property="apiList" ofType="com.pangu.application.domain.vo.ApplicationVO$ApiVO">
            <id property="apiId" column="api_id"/>
            <result property="apiCode" column="api_code"/>
            <result property="apiName" column="api_name"/>
            <result property="apiPath" column="api_path"/>
            <result property="apiMethod" column="api_method"/>
        </collection>
    </resultMap>

    <!-- 查询应用列表 -->
    <select id="selectApplicationVOList" resultMap="ApplicationVOResult">
        SELECT DISTINCT
            a.app_id,
            a.app_code,
            a.app_name,
            a.app_secret,
            a.contact_person,
            a.contact_phone,
            a.status,
            a.create_time,
            a.remark,
            api.api_id,
            api.api_code,
            api.api_name,
            api.api_path,
            api.api_method
        FROM pg_application a
        LEFT JOIN pg_app_api aa ON a.app_id = aa.app_id
        LEFT JOIN pg_api_dict api ON aa.api_id = api.api_id AND api.status = '0'
        WHERE a.del_flag = '0'
        <if test="dto.appName != null and dto.appName != ''">
            AND a.app_name LIKE CONCAT('%', #{dto.appName}, '%')
        </if>
        <if test="dto.appCode != null and dto.appCode != ''">
            AND a.app_code = #{dto.appCode}
        </if>
        <if test="dto.status != null and dto.status != ''">
            AND a.status = #{dto.status}
        </if>
        <if test="dto.beginTime != null and dto.beginTime != ''">
            AND DATE_FORMAT(a.create_time,'%Y-%m-%d') &gt;= #{dto.beginTime}
        </if>
        <if test="dto.endTime != null and dto.endTime != ''">
            AND DATE_FORMAT(a.create_time,'%Y-%m-%d') &lt;= #{dto.endTime}
        </if>
        ORDER BY a.create_time DESC
    </select>

    <!-- 根据ID查询应用详情 -->
    <select id="selectApplicationVOById" resultMap="ApplicationVOResult">
        SELECT
            a.app_id,
            a.app_code,
            a.app_name,
            a.app_secret,
            a.contact_person,
            a.contact_phone,
            a.status,
            a.create_time,
            a.remark,
            api.api_id,
            api.api_code,
            api.api_name,
            api.api_path,
            api.api_method
        FROM pg_application a
        LEFT JOIN pg_app_api aa ON a.app_id = aa.app_id
        LEFT JOIN pg_api_dict api ON aa.api_id = api.api_id AND api.status = '0'
        WHERE a.app_id = #{appId} AND a.del_flag = '0'
    </select>

    <!-- 检查应用编码是否唯一 -->
    <select id="countByAppCode" resultType="int">
        SELECT COUNT(1)
        FROM pg_application
        WHERE app_code = #{appCode}
        AND del_flag = '0'
        <if test="appId != null">
            AND app_id != #{appId}
        </if>
    </select>

    <!-- 获取下一个应用编码序号 -->
    <select id="getNextCodeSeq" resultType="int">
        SELECT COALESCE(MAX(CAST(SUBSTRING(app_code, 3) AS UNSIGNED)), 0) + 1
        FROM pg_application
        WHERE app_code LIKE 'YY%'
        AND del_flag = '0'
    </select>

</mapper>
