<?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="edu.whut.infrastructure.dao.IGroupBuyOrderDao">

    <resultMap id="dataMap" type="edu.whut.infrastructure.dao.po.GroupBuyOrder">
        <id column="id" property="id"/>
        <result column="team_id" property="teamId"/>
        <result column="activity_id" property="activityId"/>
        <result column="source" property="source"/>
        <result column="channel" property="channel"/>
        <result column="original_price" property="originalPrice"/>
        <result column="deduction_price" property="deductionPrice"/>
        <result column="pay_price" property="payPrice"/>
        <result column="target_count" property="targetCount"/>
        <result column="complete_count" property="completeCount"/>
        <result column="lock_count" property="lockCount"/>
        <result column="status" property="status"/>
        <result column="valid_start_time" property="validStartTime"/>
        <result column="valid_end_time" property="validEndTime"/>
        <result column="notify_url" property="notifyUrl"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <insert id="insert" parameterType="edu.whut.infrastructure.dao.po.GroupBuyOrder">
        insert into group_buy_order(
        team_id, activity_id, source, channel, original_price,
        deduction_price, pay_price, target_count, complete_count,
        lock_count, status, valid_start_time, valid_end_time, notify_url, create_time, update_time
        ) values(
        #{teamId}, #{activityId}, #{source}, #{channel}, #{originalPrice},
        #{deductionPrice}, #{payPrice}, #{targetCount}, #{completeCount}, #{lockCount}, 0, #{validStartTime}, #{validEndTime} ,#{notifyUrl},now(), now()
        )
    </insert>

    <update id="updateAddLockCount" parameterType="java.lang.String">
        <![CDATA[
            update group_buy_order
            set lock_count = lock_count + 1, update_time= now()
            where team_id = #{teamId} and lock_count < target_count
        ]]>
    </update>

    <update id="updateAddCompleteCount" parameterType="java.lang.String">
        <![CDATA[
        update group_buy_order
        set complete_count = complete_count + 1, update_time= now()
        where team_id = #{teamId} and complete_count < target_count
        ]]>
    </update>

    <update id="updateSubtractionLockCount" parameterType="java.lang.String">
        update group_buy_order
        set lock_count = lock_count - 1, update_time= now()
        where team_id = #{teamId} and lock_count > 0
    </update>

    <update id="updateOrderStatus2COMPLETE" parameterType="java.lang.String">
        update group_buy_order
        set status = 1, update_time= now()
        where team_id = #{teamId} and status = 0
    </update>

    <select id="queryGroupBuyProgress" parameterType="java.lang.String" resultMap="dataMap">
        select target_count, complete_count, lock_count from group_buy_order
        where team_id = #{teamId}
    </select>

    <select id="queryGroupBuyTeamByTeamId" parameterType="java.lang.String" resultMap="dataMap">
        select team_id, activity_id, target_count, complete_count, lock_count, status, valid_start_time, valid_end_time,notify_url
        from group_buy_order where team_id = #{teamId}
    </select>

    <select id="queryGroupBuyProgressByTeamIds" parameterType="java.util.Set" resultMap="dataMap">
        select team_id, activity_id, target_count, complete_count, lock_count, status, valid_start_time, valid_end_time,
        notify_url
        from group_buy_order
        where status = 0 and target_count > lock_count and valid_end_time > now() and team_id in
        <foreach item="teamId" collection="teamIds" open="(" separator="," close=")">
            #{teamId}
        </foreach>
    </select>

    <select id="queryAllTeamCount" parameterType="java.util.Set" resultType="java.lang.Integer">
        select count(id) from group_buy_order
        where team_id in
        <foreach item="teamId" collection="teamIds" open="(" separator="," close=")">
            #{teamId}
        </foreach>
    </select>

    <select id="queryAllTeamCompleteCount" parameterType="java.util.Set" resultType="java.lang.Integer">
        select count(id) from group_buy_order
        where status = 1 and team_id in
        <foreach item="teamId" collection="teamIds" open="(" separator="," close=")">
            #{teamId}
        </foreach>
    </select>

    <select id="queryAllUserCount" parameterType="java.util.Set" resultType="java.lang.Integer">
        select sum(lock_count) from group_buy_order
        where team_id in
        <foreach item="teamId" collection="teamIds" open="(" separator="," close=")">
            #{teamId}
        </foreach>
    </select>

    <select id="queryActiveTeamIdsByActivityId" parameterType="java.lang.Long" resultType="java.lang.String">
        SELECT DISTINCT team_id
        FROM group_buy_order_list
        WHERE activity_id = #{activityId} and status IN (0, 1)
    </select>

</mapper>
