<?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.MangDrawDao">


    <insert id="addPrize" parameterType="Map">
       INSERT INTO TD_AQYDRAW_PRIZELIST(
			ID,USERID,PRIZENAME,USERSEQ,PRIZEID,PRIZEMONTH,PRIZESEC,SPID)
 		VALUES(TO_CHAR(SYSDATE,'yyyyMMddhh24miss')||SEQ_COMMON6.nextval,#{userid},#{prizename},#{userseq},#{prizeid},#{prizemonth},#{prizesec},#{spid})
    </insert>

    <delete id="deleteDraw">
        delete from TD_AQYDRAW_PRIZELIST where USERID=#{userid} and  PRIZEMONTH=#{month} and spid=#{spid}
    </delete>

    <select id="getNo" resultType="String">
       SELECT TO_CHAR(SYSDATE,'yyyyMMddhh24miss')||SEQ_COMMON6.nextval SENO FROM DUAL       
    </select>


    <select id="hasDraw" resultType="java.util.Map" parameterType="java.lang.String">
        <![CDATA[
		select * from TD_AQYDRAW_PRIZEREC where  INSERTTIME>=to_date(#{startTime},'yyyyMMddHH24miss') and   INSERTTIME<=to_date(#{endTime},'yyyyMMddHH24miss')
	    and  USERID=#{userid} and SPID = #{spid}
		order by inserttime desc
		  ]]>
	</select>

    <select id="hasDrawPrizelist" resultType="java.util.Map" parameterType="java.lang.String">
		select * from TD_AQYDRAW_PRIZELIST where 1=1
	    and  USERID=#{userid} and (PRIZEMONTH = #{prizemonth} or PRIZEMONTH='202108' or PRIZEMONTH='202109') and spid=#{spid}
		order by inserttime desc
	</select>

    <select id="hasDrawPrizeDetail" resultType="java.util.Map" parameterType="java.lang.String">
		select * from TD_AQYDRAW_PRIZELIST_DETAIL where 1=1
	    and  USERID=#{userid} and PRIZEMONTH = #{prizemonth} and  SPID=#{spid}
		order by inserttime desc
	</select>

    <insert id="addPrizeRec" parameterType="Map">
       INSERT INTO TD_AQYDRAW_PRIZEREC(
			ID,USERID,PRIZEMONTH,SPID)
 		VALUES(#{ID},#{USERID},#{PRIZEMONTH},#{SPID})
    </insert>

    <select id="getPrizeList" resultType="java.util.Map">
		select * from TB_AQYDRAW_PRIZECONFIG  where status='0'  and spid = #{spid}
	</select>
    <select id="isActive" resultType="java.lang.Integer">
        select  count(1)
        from TD_ORDER_RELATIONS where  userid=#{userid} and  spid=#{spid} and (endtime is  null  or  endtime >sysdate )
    </select>

    <insert id="addDrawLog" parameterType="Map">
       INSERT INTO TL_AQYDRAW_DRAWLOG (
			ID,USERID)
 		VALUES(#{ID},#{USERID})
    </insert>

    <update id="upDrawLog" parameterType="Map">
		UPDATE TL_AQYDRAW_DRAWLOG SET detail = #{DETAIL},resultcode = #{RESULTCODE}
		WHERE ID = #{ID}
	</update>

    <update id="updatePrizeRec" parameterType="Map">
		UPDATE TD_AQYDRAW_PRIZEREC SET detail = #{DETAIL}
		WHERE ID = #{ID}
	</update>

    <update id="addPrizeDetail" parameterType="Map">


    merge into TD_AQYDRAW_PRIZELIST_DETAIL t1
    using (select  #{userid} as USERID , #{prizemonth} as  PRIZEMONTH ,#{spid} as SPID from dual) t2
    on (t1.USERID=t2.USERID and t1.PRIZEMONTH=t2.PRIZEMONTH and t1.SPID=t2.SPID)
    when matched then update set ADDRESS=#{address},PHONE=#{phone},USERNAME=#{username}
    when  not matched then  INSERT  (ID,USERID,USERNAME,GOODSCODE,ADDRESS,PRIZEMONTH,PHONE,SPID)
 	VALUES (TO_CHAR(SYSDATE,'yyyyMMddhh24miss')||SEQ_COMMON6.nextval,#{userid},#{username},#{goodscode},#{address},#{prizemonth},#{phone},#{spid})
    </update>

</mapper>