<?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.pnkx.mapper.PxPhotoMapper">

    <resultMap type="com.pnkx.domain.po.PxPhoto" id="PxPhotoResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="photoBase64" column="photo_base64"/>
        <result property="thumbnail" column="thumbnail"/>
        <result property="photoAddress" column="photo_address"/>
        <result property="type" column="type"/>
        <result property="version" column="version"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectPxPhotoVo">
        select id,
               name,
               photo_base64,
               thumbnail,
               photo_address,
               type,
               version,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from px_photo
    </sql>

    <select id="selectPxPhotoList" parameterType="com.pnkx.domain.po.PxPhoto" resultMap="PxPhotoResult">
        <include refid="selectPxPhotoVo"/>
        <where>
            <if test="name != null  and name != ''">and name like concat('%', #{name}, '%')</if>
            <if test="photoAddress != null  and photoAddress != ''">and photo_address = #{photoAddress}</if>
            <if test="thumbnail != null  and thumbnail != ''">and thumbnail = #{thumbnail}</if>
            <if test="type != null  and type != ''">and type = #{type}</if>
            <if test="remark != null  and remark != ''">and remark like concat('%', #{remark}, '%')</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectPxPhotoById" parameterType="String" resultMap="PxPhotoResult">
        <include refid="selectPxPhotoVo"/>
        where id = #{id}
    </select>

    <insert id="insertPxPhoto" parameterType="com.pnkx.domain.po.PxPhoto">
        insert into px_photo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="name != null">name,</if>
            <if test="photoBase64 != null">photo_base64,</if>
            <if test="thumbnail != null">thumbnail,</if>
            <if test="photoAddress != null">photo_address,</if>
            <if test="type != null">type,</if>
            <if test="version != null">version,</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">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="name != null">#{name},</if>
            <if test="photoBase64 != null">#{photoBase64},</if>
            <if test="thumbnail != null">#{thumbnail},</if>
            <if test="photoAddress != null">#{photoAddress},</if>
            <if test="type != null">#{type},</if>
            <if test="version != null">#{version},</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">#{remark},</if>
        </trim>
    </insert>

    <update id="updatePxPhoto" parameterType="com.pnkx.domain.po.PxPhoto">
        update px_photo
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="photoBase64 != null">photo_base64 = #{photoBase64},</if>
            <if test="thumbnail != null">thumbnail = #{thumbnail},</if>
            <if test="photoAddress != null">photo_address = #{photoAddress},</if>
            <if test="type != null">type = #{type},</if>
            <if test="version != null">version = #{version},</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">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePxPhotoById" parameterType="String">
        delete
        from px_photo
        where id = #{id}
    </delete>

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

    <!--获取相册列表-->
    <select id="getAlbumList" resultType="map">
        SELECT distinct dict_code                  dictCode,
                        dict_value                 dictValue,
                        dict_label                 dictLabel,
                        d.create_time              createTime,
                        d.remark,
                        count(dict_value)          number,
                        (select if(thumbnail is null or thumbnail = '', photo_base64, thumbnail)
                         from px_photo
                         where type = dict_value
                         order BY create_time desc limit 1) photo
        FROM
            sys_dict_data d left join px_photo p
        on d.dict_value = p.type
        WHERE
            dict_type = 'px_album_name'
          and status = 0
        group by dict_code, dict_value, dict_label, d.create_time, d.remark
    </select>
</mapper>
