<?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.fzm.mall.server.admin.marketing.mapper.PromotionMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.fzm.mall.server.admin.marketing.model.Promotion">
        <id column="id" property="id" />
        <result column="promotion_id" property="promotionId" />
        <result column="type" property="type" />
        <result column="name" property="name" />
        <result column="scope" property="scope" />
        <result column="range_type" property="rangeType" />
        <result column="remark" property="remark" />
        <result column="start_time" property="startTime" />
        <result column="end_time" property="endTime" />
        <result column="status" property="status" />
        <result column="merchant_id" property="merchantId" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <resultMap id="ResultMap" type="com.fzm.mall.server.admin.marketing.entity.dto.PromotionDTO">
        <id column="id" property="id" />
        <result column="promotion_id" property="promotionId" />
        <result column="type" property="type" />
        <result column="name" property="name" />
        <result column="scope" property="scope" />
        <result column="range_type" property="rangeType" />
        <result column="remark" property="remark" />
        <result column="start_time" property="startTime" />
        <result column="end_time" property="endTime" />
        <result column="status" property="status" />
        <result column="merchant_id" property="merchantId" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />

        <association property="promotionRule" column="promotion_id">
            <result column="condition_val" property="conditionVal" />
            <result column="promotion_val" property="promotionVal" />
        </association>
    </resultMap>
    <select id="page" resultMap="ResultMap">
        select p.id, p.promotion_id, p.`type`, p.`name`, p.`scope`, p.`remark`, p.start_time, p.end_time, p.status,
               p.create_time, p.update_time, p.merchant_id, p.range_type, pr.condition_val, pr.promotion_val
        from marketing_promotion p
        left join marketing_promotion_rule pr on p.promotion_id = pr.promotion_id
        <where>
            p.merchant_id = #{merchantId}
            <if test="promotionId != null">
                and p.promotion_id like CONCAT('%', #{promotionId},'%')
            </if>
            <if test="name != null">
                and p.`name` like CONCAT('%', #{name},'%')
            </if>
            <if test="status != null">
                and p.status = #{status}
            </if>
        </where>
        order by p.create_time desc
    </select>

    <select id="read" resultMap="BaseResultMap">
        select id, promotion_id, `type`, `name`, `scope`, `remark`, start_time, end_time, status, create_time, update_time, merchant_id, range_type
        from marketing_promotion
        where merchant_id = #{merchantId} and promotion_id = #{promotionId}
    </select>

    <select id="listTimeRepetition" resultMap="BaseResultMap">
        select id, promotion_id, `type`, `name`, `scope`, `remark`, start_time, end_time, status, create_time, update_time, merchant_id, range_type
        from marketing_promotion
        where merchant_id = #{merchantId} and status in('1', '2')
        order by create_time desc
    </select>
</mapper>
