<?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="MdePhotoAlbumMapper" >
  <resultMap id="BaseResultMap" type="com.wechat.model.dbEntity.MdePhotoAlbum" >
    <id column="PHO_ID" property="phoId" jdbcType="INTEGER" />
    <result column="USER_CODE" property="userCode" jdbcType="BIGINT" />
    <result column="PHOTO_DESC" property="photoDesc" jdbcType="VARCHAR" />
    <result column="PHOTO_URL" property="photoUrl" jdbcType="VARCHAR" />
    <result column="SORT" property="sort" jdbcType="INTEGER" />
    <result column="VERSION_NO" property="versionNo" jdbcType="INTEGER" />
    <result column="REMARKS" property="remarks" jdbcType="VARCHAR" />
    <result column="DEL_FLAG" property="delFlag" jdbcType="INTEGER" />
    <result column="CREATE_BY" property="createBy" jdbcType="VARCHAR" />
    <result column="CREATE_DATE" property="createDate" jdbcType="TIMESTAMP" />
    <result column="UPDATE_BY" property="updateBy" jdbcType="VARCHAR" />
    <result column="UPDATE_DATE" property="updateDate" jdbcType="TIMESTAMP" />
  </resultMap>
  <sql id="Base_Column_List" >
    PHO_ID, USER_CODE, PHOTO_DESC, PHOTO_URL, SORT, VERSION_NO, REMARKS, DEL_FLAG, CREATE_BY, CREATE_DATE, 
    UPDATE_BY, UPDATE_DATE
  </sql>
  <select id="selectByUserCode" resultType="com.wechat.model.responseDto.PhotoAlbumResp" parameterType="java.lang.String" >
    select 
    	PHO_ID     phoId,
    	PHOTO_DESC photoDesc,
    	PHOTO_URL  photoUrl,
    	SORT       sort
    from mde_photo_album
    where USER_CODE = #{userCode}
   	  and DEL_FLAG = 0
   	  order by SORT asc,PHO_ID asc
  </select>

  <insert id="insertSelective" parameterType="com.wechat.model.dbEntity.MdePhotoAlbum" >
    insert into mde_photo_album
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="userCode != null" >
        USER_CODE,
      </if>
      <if test="photoUrl != null" >
        PHOTO_URL,
      </if>
      <if test="photoDesc != null" >
        PHOTO_DESC,
      </if>
      <if test="sort != null" >
        SORT,
      </if>
      <if test="versionNo != null" >
        VERSION_NO,
      </if>
      <if test="remarks != null" >
        REMARKS,
      </if>
      <if test="delFlag != null" >
        DEL_FLAG,
      </if>
      <if test="createBy != null" >
        CREATE_BY,
      </if>
      <if test="createDate != null" >
        CREATE_DATE,
      </if>
      <if test="updateBy != null" >
        UPDATE_BY,
      </if>
      <if test="updateDate != null" >
        UPDATE_DATE,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="userCode != null" >
        #{userCode},
      </if>
      <if test="photoUrl != null" >
        #{photoUrl},
      </if>
      <if test="photoDesc != null" >
        #{photoDesc},
      </if>
      <if test="sort != null" >
        #{sort},
      </if>
      <if test="versionNo != null" >
        #{versionNo,jdbcType=INTEGER},
      </if>
      <if test="remarks != null" >
        #{remarks,jdbcType=VARCHAR},
      </if>
      <if test="delFlag != null" >
        #{delFlag,jdbcType=INTEGER},
      </if>
      <if test="createBy != null" >
        #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="createDate != null" >
        #{createDate,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null" >
        #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="updateDate != null" >
        #{updateDate,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  
  <update id="update" parameterType="com.wechat.model.dbEntity.MdePhotoAlbum" >
    update mde_photo_album
    <set >
      <if test="sort != null" >
        SORT = #{sort,jdbcType=INTEGER},
      </if>
      <if test="photoDesc != null" >
        PHOTO_DESC = #{photoDesc},
      </if>
      <if test="versionNo != null" >
        VERSION_NO = #{versionNo,jdbcType=INTEGER},
      </if>
      <if test="delFlag != null" >
        DEL_FLAG = #{delFlag},
      </if>
      <if test="updateBy != null" >
        UPDATE_BY = #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="updateDate != null" >
        UPDATE_DATE = #{updateDate,jdbcType=TIMESTAMP},
      </if>
      version_no = version_no+1 
    </set>
    where PHO_ID = #{phoId} and USER_CODE = #{userCode}
  </update>
  
  <select id="getSortByUserCode" resultType="java.lang.Integer" parameterType="java.lang.String" >
    select 
    	Max(SORT)
    from mde_photo_album
    where USER_CODE = #{userCode}
   	  and DEL_FLAG = 0
   	  group by USER_CODE
  </select>
 
</mapper>