<?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.yiqi.app.mapper.AppCouponItemMapper">

    <resultMap type="AppCouponItem" id="AppCouponItemResult">
        <result property="id" column="id"/>
        <result property="orderNo" column="order_no"/>
        <result property="couponTypeId" column="coupon_type_id"/>
        <result property="couponTypeTitle" column="coupon_type_title"/>
        <result property="activityInfoId" column="activity_info_id"/>
        <result property="storeId" column="store_id"/>
        <result property="appUserId" column="app_user_id"/>
        <result property="isUsed" column="is_used"/>
        <result property="usedStoreId" column="used_store_id"/>
        <result property="usedTime" column="used_time"/>
        <result property="startDate" column="start_date"/>
        <result property="endDate" column="end_date"/>
        <result property="isUnique" column="is_unique"/>
        <result property="minAmount" column="min_amount"/>
        <result property="deductNo" column="deduct_no"/>
        <result property="deductAmount" column="deduct_amount"/>
        <result property="deductOrderNo" column="deduct_order_no"/>
        <result property="createById" column="create_by_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateById" column="update_by_id"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectAppCouponItemVo">
        select * from app_coupon_item
    </sql>

    <sql id="selectAppCouponItemListVo">
        select c.*,ct.title as couponTypeTitle,a.title as activityTitle,s.name as orgName,
               us.name as usedOrgName,u.nick_name as appUserNickName,u.real_name as appUserName, u.phone_number as  appUserPhone
        from app_coupon_item as c
        left join app_user as u on u.id=c.app_user_id
        left join app_coupon_type as ct on ct.id=c.coupon_type_id
        left join activity_info as a on a.id=c.activity_info_id
        left join sys_store as s on  s.id=c.store_id
        left join sys_store as us on us.id=c.used_store_id
    </sql>

    <select id="selectAppCouponItemList" parameterType="com.yiqi.app.domain.dto.AppCouponItemQueryDTO" resultType="com.yiqi.app.domain.vo.AppCouponItemVO">
        <include refid="selectAppCouponItemListVo"/>
        <where>
            1 = 1
            <if test="isUsed != null  and isUsed != ''">
                and c.is_used = #{isUsed}
            </if>
            <if test="orgId != null  and orgId != ''">
                and c.store_id = #{orgId}
            </if>
            <if test="startDate != null">
                and c.start_date = #{startDate}
            </if>
            <if test="endDate != null">
                and c.end_date = #{endDate}
            </if>
            <if test="appUserPhoneNumber != null  and appUserPhoneNumber != ''">
                and u.phone_number = #{appUserPhoneNumber}
            </if>
            <if test="discountWay != null and discountWay != ''">
                and c.discount_way = #{discountWay}
            </if>
            <if test="appUserId != null">
                and c.app_user_id = #{appUserId}
            </if>
        </where>
        order by c.create_time desc
    </select>

    <select id="selectAppCouponItemById" parameterType="Long"
            resultMap="AppCouponItemResult">
        <include refid="selectAppCouponItemVo"/>
        where id = #{id}
    </select>
    <select id="getlistByAppUserId" resultType="com.yiqi.app.domain.vo.AppCouponItemVO">
        select c.*
        from app_coupon_item as c
        where (c.use_scope = 1 or (c.use_scope = 2 and c.coupon_type_id in
        (select coupon_type_id from app_coupon_type_apply_cloth
            where cloth_item_id in
            <foreach item="clothId" collection="appCouponItemQueryByAppUserIdDTO.clothIds" open="(" separator="," close=")">
                #{clothId}
            </foreach>
        )))
        and FIND_IN_SET(#{appCouponItemQueryByAppUserIdDTO.storeId}, c.store_ids)
        <![CDATA[and c.start_date <= now()]]>
        <![CDATA[and c.end_date >= now()]]>
        <if test="appCouponItemQueryByAppUserIdDTO.appUserId != null">
            and c.app_user_id=#{appCouponItemQueryByAppUserIdDTO.appUserId}
        </if>
        <if test="appCouponItemQueryByAppUserIdDTO.discountWay != null">
            and c.discount_way = #{appCouponItemQueryByAppUserIdDTO.discountWay}
        </if>
    </select>

    <insert id="insertAppCouponItem" parameterType="AppCouponItem" useGeneratedKeys="true"
            keyProperty="id">
        insert into app_coupon_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderNo != null">order_no,
            </if>
            <if test="couponTypeId != null">coupon_type_id,
            </if>
            <if test="couponTypeTitle != null">coupon_type_title,
            </if>
            <if test="activityInfoId != null">activity_info_id,
            </if>
            <if test="orgId != null">org_id,
            </if>
            <if test="sourceType != null">source_type,
            </if>
            <if test="appUserId != null">app_user_id,
            </if>
            <if test="isUsed != null">is_used,
            </if>
            <if test="usedOrgId != null">used_org_id,
            </if>
            <if test="usedSourceType != null">used_source_type,
            </if>
            <if test="usedTime != null">used_time,
            </if>
            <if test="startDate != null">start_date,
            </if>
            <if test="endDate != null">end_date,
            </if>
            <if test="isUnique != null">is_unique,
            </if>
            <if test="canManual != null">can_manual,
            </if>
            <if test="minAmount != null">min_amount,
            </if>
            <if test="deductNo != null">deduct_no,
            </if>
            <if test="deductAmount != null">deduct_amount,
            </if>
            <if test="deductOrderNo != null">deduct_order_no,
            </if>
            <if test="canDeductRecharge != null">can_deduct_recharge,
            </if>
            <if test="deductRechargeAmount != null">deduct_recharge_amount,
            </if>
            <if test="createById != null">create_by_id,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateById != null">update_by_id,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
            <if test="remark != null">remark,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderNo != null">#{orderNo},
            </if>
            <if test="couponTypeId != null">#{couponTypeId},
            </if>
            <if test="couponTypeTitle != null">#{couponTypeTitle},
            </if>
            <if test="activityInfoId != null">#{activityInfoId},
            </if>
            <if test="orgId != null">#{orgId},
            </if>
            <if test="sourceType != null">#{sourceType},
            </if>
            <if test="appUserId != null">#{appUserId},
            </if>
            <if test="isUsed != null">#{isUsed},
            </if>
            <if test="usedOrgId != null">#{usedOrgId},
            </if>
            <if test="usedSourceType != null">#{usedSourceType},
            </if>
            <if test="usedTime != null">#{usedTime},
            </if>
            <if test="startDate != null">#{startDate},
            </if>
            <if test="endDate != null">#{endDate},
            </if>
            <if test="isUnique != null">#{isUnique},
            </if>
            <if test="canManual != null">#{canManual},
            </if>
            <if test="minAmount != null">#{minAmount},
            </if>
            <if test="deductNo != null">#{deductNo},
            </if>
            <if test="deductAmount != null">#{deductAmount},
            </if>
            <if test="deductOrderNo != null">#{deductOrderNo},
            </if>
            <if test="canDeductRecharge != null">#{canDeductRecharge},
            </if>
            <if test="deductRechargeAmount != null">#{deductRechargeAmount},
            </if>
            <if test="createById != null">#{createById},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateById != null">#{updateById},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
            <if test="remark != null">#{remark},
            </if>
        </trim>
    </insert>

    <update id="updateAppCouponItem" parameterType="AppCouponItem">
        update app_coupon_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderNo != null">order_no =
                #{orderNo},
            </if>
            <if test="couponTypeId != null">coupon_type_id =
                #{couponTypeId},
            </if>
            <if test="couponTypeTitle != null">coupon_type_title =
                #{couponTypeTitle},
            </if>
            <if test="activityInfoId != null">activity_info_id =
                #{activityInfoId},
            </if>
            <if test="orgId != null">org_id =
                #{orgId},
            </if>
            <if test="sourceType != null">source_type =
                #{sourceType},
            </if>
            <if test="appUserId != null">app_user_id =
                #{appUserId},
            </if>
            <if test="isUsed != null">is_used =
                #{isUsed},
            </if>
            <if test="usedOrgId != null">used_org_id =
                #{usedOrgId},
            </if>
            <if test="usedSourceType != null">used_source_type =
                #{usedSourceType},
            </if>
            <if test="usedTime != null">used_time =
                #{usedTime},
            </if>
            <if test="startDate != null">start_date =
                #{startDate},
            </if>
            <if test="endDate != null">end_date =
                #{endDate},
            </if>
            <if test="isUnique != null">is_unique =
                #{isUnique},
            </if>
            <if test="canManual != null">can_manual =
                #{canManual},
            </if>
            <if test="minAmount != null">min_amount =
                #{minAmount},
            </if>
            <if test="deductNo != null">deduct_no =
                #{deductNo},
            </if>
            <if test="deductAmount != null">deduct_amount =
                #{deductAmount},
            </if>
            <if test="deductOrderNo != null">deduct_order_no =
                #{deductOrderNo},
            </if>
            <if test="canDeductRecharge != null">can_deduct_recharge =
                #{canDeductRecharge},
            </if>
            <if test="deductRechargeAmount != null">deduct_recharge_amount =
                #{deductRechargeAmount},
            </if>
            <if test="createById != null">create_by_id =
                #{createById},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateById != null">update_by_id =
                #{updateById},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
            <if test="remark != null">remark =
                #{remark},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAppCouponItemById" parameterType="Long">
        delete from app_coupon_item where id = #{id}
    </delete>

    <delete id="deleteAppCouponItemByIds" parameterType="String">
        delete from app_coupon_item where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="orgListByCouponId" resultType="com.yiqi.system.domain.SysOrg">
        select *
        from sys_org
        where (id, source_type) in (select org_id, source_type
                                    from app_coupon_type_apply_org

                                    where coupon_type_id = (select coupon_type_id from app_coupon_item where id = #{couponItemId}))
          and status = '0'
    </select>
    <select id="getMemberCouponInfoByRecharge" resultType="com.yiqi.app.domain.AppCouponItem">
        select aci.*
        from app_coupon_item aci
        left join app_coupon_type_apply_org org on aci.coupon_type_id = org.coupon_type_id
        where aci.app_user_id = #{memberCouponItemQueryDTO.appUserId}
              and aci.is_used = 'N' and aci.del_flag = '0'
          <![CDATA[and aci.end_date >= #{NowDateTime}]]>
            and org.org_id = #{orgId}
            order by aci.create_time desc
            limit #{memberCouponItemQueryDTO.startRow},#{memberCouponItemQueryDTO.pageSize}
    </select>
    <select id="countMemberCouponInfoByRecharge" resultType="java.lang.Integer">

    </select>
    <select id="couponSendListByStore" resultType="com.yiqi.app.domain.vo.AppCouponItemVO">
        <include refid="selectAppCouponItemListVo"/>
        <where>
            <if test="orgId != null  and orgId != ''">
                and c.store_id = #{orgId}
            </if>
<!--            <if test="discountWay != null and discountWay != ''">-->
<!--                and c.discount_way = #{discountWay}-->
<!--            </if>-->
        </where>
        order by c.create_time desc
        limit #{startRow},#{pageSize}
    </select>
    <select id="countCouponSendListByStore" resultType="java.lang.Integer">
        select count(*)
        from app_coupon_item as c
        left join app_user as u on u.id=c.app_user_id
        left join app_coupon_type as ct on ct.id=c.coupon_type_id
        left join activity_info as a on a.id=c.activity_info_id
        left join sys_store as s on  s.id=c.store_id
        left join sys_store as us on us.id=c.used_store_id
        <where>
            <if test="orgId != null  and orgId != ''">
                and c.store_id = #{orgId}
            </if>
        </where>
    </select>
    <select id="countTodaySendNumber" resultType="java.lang.Integer">
        select count(*) from app_coupon_item
        where store_id = #{orgId}  and activity_info_id is not null
        <if test="dateTimeNow != null and dateTimeNow !=''">
            <![CDATA[and create_time >= concat(#{dateTimeNow}, ' 00:00:00')]]>
            <![CDATA[and create_time <= concat(#{dateTimeNow}, ' 23:59:59')]]>
        </if>
    </select>
    <select id="countTodayHxNumber" resultType="java.lang.Integer">
        select count(*) from app_coupon_item
        where used_store_id = #{orgId}
        <if test="dateTimeNow != null and dateTimeNow !=''">
            <![CDATA[and update_time >= concat(#{dateTimeNow}, ' 00:00:00')]]>
            <![CDATA[and update_time <= concat(#{dateTimeNow}, ' 23:59:59')]]>
        </if>
    </select>
</mapper>