<?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.chinacreator.video.restful.dao.RedeemUserDao">


    <!--    <insert id="insert" parameterType="com.chinacreator.video.bean.OrderInfo" useGeneratedKeys="true" keyProperty="id">
            insert into TD_REDEEM_USER(id,userid,cpid,spid,status)
             VALUES (TO_CHAR(SYSDATE,'yyyyMMddhh24miss')||SEQ_COMMON6.nextval,#{userid},#{cpid},#{spid},#{status})
        </insert>-->

    <update id="insert" parameterType="com.chinacreator.video.bean.OrderInfo">
    merge into TD_REDEEM_USER a

    using  (select #{userid} as userid,#{cpid} as cpid,#{spid} as spid from dual ) b

    on  (a.userid=b.userid and  a.cpid=b.cpid  and  a.spid =b.spid  and a.inserttime &lt;= sysdate
    and  a.inserttime &gt;= to_date(to_char(sysdate,'yyyyMMdd'),'yyyyMMdd'))

    when matched then update set status=#{status}, updatetime=sysdate

    when not matched then insert (id,userid,cpid,spid,status) VALUES (TO_CHAR(SYSDATE,'yyyyMMddhh24miss')||SEQ_COMMON6.nextval,#{userid},#{cpid},#{spid},#{status})
    </update>

    <select id="findUser" resultType="java.lang.Integer">
        select count(1) from td_order_relations where userid=#{userid} and cpid=#{cpid} and spid=#{spid} and status='0'
    </select>
</mapper>

