<?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="MdeExchangeHisMapper" >
  <resultMap id="BaseResultMap" type="com.wechat.model.dbEntity.MdeExchangeHis" >
    <id column="HIS_ID" property="hisId" jdbcType="INTEGER" />
    <result column="USER_CODE" property="userCode" jdbcType="BIGINT" />
    <result column="PRIZE_ID" property="prizeId" jdbcType="VARCHAR" />
    <result column="ACT_ID" property="actId" jdbcType="INTEGER" />
    <result column="PRIZE_NAME_CN" property="prizeNameCn" jdbcType="VARCHAR" />
    <result column="PRIZE_NAME_EN" property="prizeNameEn" jdbcType="VARCHAR" />
    <result column="EXCHANGE_QUALITY" property="exchangeQuality" jdbcType="INTEGER" />
    <result column="EXCHANGE_NORMAL" property="exchangeNormal" jdbcType="INTEGER" />
    <result column="ACTUAL_QUALITY" property="actualQuality" jdbcType="INTEGER" />
    <result column="ACTUAL_NORMAL" property="actualNormal" jdbcType="INTEGER" />
    <result column="STATUS" property="status" jdbcType="CHAR" />
    <result column="CHECK_BY" property="checkBy" jdbcType="VARCHAR" />
    <result column="CHECK_TIME" property="checkTime" jdbcType="TIMESTAMP" />
    <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" >
    HIS_ID, USER_CODE, PRIZE_ID, ACT_ID, PRIZE_NAME_CN, PRIZE_NAME_EN, EXCHANGE_QUALITY, EXCHANGE_NORMAL,ACTUAL_QUALITY,
    ACTUAL_NORMAL, STATUS, CHECK_BY, CHECK_TIME, VERSION_NO, REMARKS, DEL_FLAG, CREATE_BY, 
    CREATE_DATE, UPDATE_BY, UPDATE_DATE
  </sql>

  <delete id="deleteExchangeRequest" parameterType="com.wechat.model.dbEntity.MdeExchangeHis" >
    delete from mde_exchange_his
    where HIS_ID = #{hisId}
    and USER_CODE = #{userCode}
    and STATUS = #{status}
  </delete>
  
  <insert id="insertExchangeRequest" parameterType="com.wechat.model.dbEntity.MdeExchangeHis" >
    insert into mde_exchange_his (USER_CODE, PRIZE_ID, 
      ACT_ID, PRIZE_NAME_CN, PRIZE_NAME_EN, 
      EXCHANGE_QUALITY, EXCHANGE_NORMAL, STATUS, CREATE_BY, 
      CREATE_DATE, UPDATE_BY, UPDATE_DATE
      )
    (select  #{userCode}, #{prizeId}, 
      ACT_ID, PRIZE_NAME_CN, PRIZE_NAME_EN,
      EXCHANGE_QUALITY, EXCHANGE_NORMAL, #{status}, #{createBy}, 
      #{createDate}, #{updateBy}, #{updateDate} 
       from MDE_ACTIVITY_DETAIL where PRIZE_ID = #{prizeId} and DEL_FLAG= 0 )
  </insert>
  
  <select id="getExchangeHis" resultType="com.wechat.model.dto.ExchangeHisDto">
		SELECT 
			a.his_id AS "hisId",
			a.prize_name_cn AS "prizeNameCn",
			a.prize_name_en AS "prizeNameEn",
			d.IMG_PC as imgPc,
			d.IMG_MOBILE as imgMobile,
			a.status AS "status",
			sd.label as statusName,
			a.CREATE_DATE as exchangeDate,
			CASE a.STATUS WHEN '2' THEN a.ACTUAL_QUALITY ELSE a.EXCHANGE_QUALITY END  AS exchangeQuality,
			CASE a.STATUS WHEN '2' THEN a.ACTUAL_NORMAL ELSE a.EXCHANGE_NORMAL END  AS exchangeNormal 
		FROM mde_exchange_his a
		left JOIN MDE_ACTIVITY_DETAIL d
		on a.PRIZE_ID = d.PRIZE_ID
		INNER JOIN sys_dict sd on sd.VALUE = a.`STATUS` and sd.type='exchange_status'
		<where>
				a.del_flag = '0'
			AND a.user_code = #{userCode}
			AND a.act_id = #{actId}
		</where>
		order by a.CREATE_DATE desc
		
	</select>
  
</mapper>