<?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.bonus.canteen.core.device.mapper.DeviceAppVersionMapper">
    <resultMap type="com.bonus.canteen.core.device.domain.DeviceAppVersion" id="DeviceAppVersionResult">
        <result property="id"    column="id"    />
        <result property="version"    column="version"    />
        <result property="versionName"    column="version_name"    />
        <result property="apkName"    column="apk_name"    />
        <result property="apkPath"    column="apk_path"    />
        <result property="updateContent"    column="update_content"    />
        <result property="deployTime"    column="deploy_time"    />
        <result property="deployUser"    column="deploy_user"    />
        <result property="canteenType"    column="canteen_type"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectDeviceAppVersionVo">
        select id, version, version_name, apk_name, apk_path, update_content, deploy_time, deploy_user, canteen_type, create_by, create_time, update_by, update_time from device_app_version
    </sql>

    <select id="selectDeviceAppVersionList" parameterType="com.bonus.canteen.core.device.domain.DeviceAppVersion" resultMap="DeviceAppVersionResult">
        <include refid="selectDeviceAppVersionVo"/>
        <where>  
            <if test="versionName != null  and versionName != ''"> and version_name like concat('%', #{versionName}, '%')</if>
            <if test="apkName != null  and apkName != ''"> and apk_name like concat('%', #{apkName}, '%')</if>
            <if test="apkPath != null  and apkPath != ''"> and apk_path = #{apkPath}</if>
            <if test="updateContent != null  and updateContent != ''"> and update_content = #{updateContent}</if>
            <if test="deployTime != null "> and deploy_time = #{deployTime}</if>
            <if test="deployUser != null  and deployUser != ''"> and deploy_user = #{deployUser}</if>
        </where>
    </select>
    
    <select id="selectDeviceAppVersionById" parameterType="Integer" resultMap="DeviceAppVersionResult">
        <include refid="selectDeviceAppVersionVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertDeviceAppVersion" parameterType="com.bonus.canteen.core.device.domain.DeviceAppVersion">
        insert into device_app_version
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="version != null">version,</if>
            <if test="versionName != null">version_name,</if>
            <if test="apkName != null">apk_name,</if>
            <if test="apkPath != null">apk_path,</if>
            <if test="updateContent != null">update_content,</if>
            <if test="deployTime != null">deploy_time,</if>
            <if test="deployUser != null">deploy_user,</if>
            <if test="canteenType != null">canteen_type,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="version != null">#{version},</if>
            <if test="versionName != null">#{versionName},</if>
            <if test="apkName != null">#{apkName},</if>
            <if test="apkPath != null">#{apkPath},</if>
            <if test="updateContent != null">#{updateContent},</if>
            <if test="deployTime != null">#{deployTime},</if>
            <if test="deployUser != null">#{deployUser},</if>
            <if test="canteenType != null">#{canteenType},</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>
         </trim>
    </insert>

    <update id="updateDeviceAppVersion" parameterType="com.bonus.canteen.core.device.domain.DeviceAppVersion">
        update device_app_version
        <trim prefix="SET" suffixOverrides=",">
            <if test="version != null">version = #{version},</if>
            <if test="versionName != null">version_name = #{versionName},</if>
            <if test="apkName != null">apk_name = #{apkName},</if>
            <if test="apkPath != null">apk_path = #{apkPath},</if>
            <if test="updateContent != null">update_content = #{updateContent},</if>
            <if test="deployTime != null">deploy_time = #{deployTime},</if>
            <if test="deployUser != null">deploy_user = #{deployUser},</if>
            <if test="canteenType != null">canteen_type = #{canteenType},</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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDeviceAppVersionById" parameterType="Integer">
        delete from device_app_version where id = #{id}
    </delete>

    <delete id="deleteDeviceAppVersionByIds" parameterType="String">
        delete from device_app_version where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="getApkByType" resultType="com.bonus.canteen.core.device.domain.ApkDTO">
        select *
        from device_app_version
        where 1 = 1
        <if test="version != null and version != ''">
            and version = #{version}
        </if>
        <if test="type != null and type != ''">
            and canteen_type = #{type}
        </if>
        order by version desc
        limit 1
    </select>
    <update id="updateApk">
        update device_app_version
        <set>
            <if test="version != null and version != ''">
                version = #{version},
            </if>
            <if test="versionName != null and versionName != ''">
                version_name = #{versionName},
            </if>
            <if test="apkName != null and apkName != ''">
                apk_name = #{apkName},
            </if>
            <if test="apkPath != null and apkPath != ''">
                apk_path = #{apkPath},
            </if>
            <if test="updateContent != null and updateContent != ''">
                update_content = #{updateContent},
            </if>
            <if test="deployUser != null and deployUser != ''">
                deploy_user = #{deployUser},
            </if>
            <if test="type != null and type != ''">
                canteen_type = #{type},
            </if>
        </set>
        where version = #{version}
    </update>
    <insert id="insertApk">
        insert into device_app_version(version, version_name, apk_name, apk_path, update_content, deploy_user, canteen_type)
        values (#{version}, #{versionName}, #{apkName}, #{apkPath}, #{updateContent}, #{deployUser}, #{type})
    </insert>
</mapper>