<?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.diagbot.mapper.UserRenewalsMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.UserRenewals">
        <id column="id" property="id" />
        <result column="is_deleted" property="isDeleted" />
        <result column="gmt_create" property="gmtCreate" />
        <result column="gmt_modified" property="gmtModified" />
        <result column="creator" property="creator" />
        <result column="modifier" property="modifier" />
        <result column="product_id" property="productId" />
        <result column="user_id" property="userId" />
        <result column="renewals_status" property="renewalsStatus" />
        <result column="apply_time" property="applyTime" />
        <result column="renewals_no" property="renewalsNo"/>
        <result column="cancel_renewals" property="cancelRenewals"/>
    </resultMap>

    <select id="selectProductIdByUserId"  parameterType="java.util.Map" resultType="java.lang.Long">
        SELECT r.product_id FROM `diag_user_renewals` r WHERE r.is_deleted = 'N'
        AND r.user_id =#{map.userId}
        AND r.product_id = #{map.productId}
        AND r.renewals_status = 0
        AND r.cancel_renewals = 0
    </select>

    <select id="selectUserRenewals" resultType="com.diagbot.dto.RenewalsInfosDTO">
        SELECT * FROM `diag_user_renewals` WHERE is_deleted = 'N'
        <if test="renewalsInfosVO.renewalsStatus != null">
              AND renewals_status = #{renewalsInfosVO.renewalsStatus}
        </if>
        <if test="renewalsInfosVO.startTime != null and renewalsInfosVO.endTime != null">
            AND apply_time BETWEEN #{renewalsInfosVO.startTime} and #{renewalsInfosVO.endTime}
        </if>
        <if test="renewalsInfosVO.cancelRenewals != null and renewalsInfosVO.cancelRenewals != null">
            AND cancel_renewals = #{renewalsInfosVO.cancelRenewals}
        </if>
        <if test="renewalsInfosVO.userId != null and renewalsInfosVO.userId.size > 0">
            <foreach collection="renewalsInfosVO.userId" item="renewalsInfosVO.userId" open="and(" separator="or" close=")">
                user_id = #{renewalsInfosVO.userId}
            </foreach>
        </if>
        ORDER BY apply_time DESC
    </select>

    <update id="updateRenewalStatus" parameterType="com.diagbot.entity.UserRenewals">
	UPDATE 
	 diag_user_renewals a 
	  <trim prefix="set" suffixOverrides=",">
	  <if test="gmtModified != null">a.gmt_modified = #{gmtModified} ,</if>
	  <if test="modifier != null">a.modifier = #{modifier},</if> 
	  <if test="renewalsStatus != null">a.renewals_status = #{renewalsStatus} </if>
	  </trim>
	  WHERE a.renewals_no = #{renewalsNo}
</update>
</mapper>
