<?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.corewing.app.mapper.ParamsCenterMapper">

    <!-- 结果映射 -->
    <resultMap id="VOResultMap" type="com.corewing.app.vo.ParamsCenterVO">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="username" property="username"/>
        <result column="param_name" property="paramName"/>
        <result column="device_name" property="deviceName"/>
        <result column="description" property="description"/>
        <result column="fc_model" property="fcModel"/>
        <result column="fc_type" property="fcType"/>
        <result column="param_version" property="paramVersion"/>
        <result column="param_detail" property="paramDetail"/>
        <result column="download_count" property="downloadCount"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="audit_status" property="auditStatus"/>
        <result column="audit_error_msg" property="auditErrorMsg"/>
    </resultMap>

    <!-- 基础查询SQL片段 -->
    <sql id="selectVOSql">
        SELECT
            p.id,
            p.user_id,
            u.username,
            p.param_name,
            p.device_name,
            p.description,
            p.fc_model,
            p.fc_type,
            p.param_version,
            p.param_detail,
            p.download_count,
            p.create_time,
            p.update_time,
            p.status,
            p.audit_status,
            p.audit_error_msg
        FROM app_params_center p
        LEFT JOIN app_user u ON p.user_id = u.id
    </sql>

    <!-- 根据ID查询带用户名的VO -->
    <select id="selectVOById" resultMap="VOResultMap">
        <include refid="selectVOSql"/>
        WHERE p.id = #{id}
    </select>

    <!-- 查询所有参数列表（带用户名） -->
    <select id="selectAllVOList" resultMap="VOResultMap">
        <include refid="selectVOSql"/>
        <where>
            <if test="fcModel != null and fcModel != ''">
                AND p.fc_model = #{fcModel}
            </if>
        </where>
        ORDER BY p.update_time DESC
    </select>

    <!-- 分页查询所有公共参数列表（带用户名） -->
    <select id="selectAllVOPage" resultMap="VOResultMap">
        <include refid="selectVOSql"/>
        <where>
            and p.status = '2'
            <if test="fcModel != null and fcModel != ''">
                AND p.fc_model = #{fcModel}
            </if>
        </where>
        ORDER BY p.update_time DESC
    </select>

    <!-- 查询当前用户的参数列表（带用户名） -->
    <select id="selectVOListByUserId" resultMap="VOResultMap">
        <include refid="selectVOSql"/>
        <where>
            p.user_id = #{userId}
            <if test="fcModel != null and fcModel != ''">
                AND p.fc_model = #{fcModel}
            </if>
        </where>
        ORDER BY p.update_time DESC
    </select>

    <!-- 分页查询当前用户的参数列表（带用户名） -->
    <select id="selectVOPageByUserId" resultMap="VOResultMap">
        <include refid="selectVOSql"/>
        <where>
            p.user_id = #{userId}
            <if test="fcModel != null and fcModel != ''">
                AND p.fc_model = #{fcModel}
            </if>
        </where>
        ORDER BY p.update_time DESC
    </select>

</mapper>
