CouponTemplateDao.xml 7.81 KB
<?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.diligrp.etrade.coupon.dao.CouponTemplateDao">
    <resultMap id="templateResultMap" type="com.diligrp.etrade.coupon.model.CouponTemplateDo">
        <id property="id" column="id"/>
        <result property="templateNo" column="template_no"/>
        <result property="templateName" column="template_name"/>
        <result property="validType" column="valid_type"/>
        <result property="fixedDays" column="fixed_days"/>
        <result property="type" column="type"/>
        <result property="extendCount" column="extend_count"/>
        <result property="discountAmount" column="discount_amount"/>
        <result property="discountRatio" column="discount_ratio"/>
        <result property="upperLimitAmount" column="upper_limit_amount"/>
        <result property="useScope" column="use_scope"/>
        <result property="applyStartTime" column="apply_start_time"/>
        <result property="applyEndTime" column="apply_end_time"/>
        <result property="issueStartTime" column="issue_start_time"/>
        <result property="issueEndTime" column="issue_end_time"/>
        <result property="notes" column="notes"/>
        <result property="marketId" column="market_id"/>
        <result property="version" column="version"/>
        <result property="createdTime" column="created_time"/>
        <result property="modifiedTime" column="modified_time"/>
        <result property="createrId" column="creater_id"/>
        <result property="createrCode" column="creater_code"/>
        <result property="createrName" column="creater_name"/>
        <result property="state" column="state"/>
    </resultMap>

    <select id="getMaxTemplateNo" resultType="integer">
        SELECT
            IFNULL( CAST( RIGHT ( MAX( template_no ), 5 ) AS SIGNED ), 0 ) AS templateNo
        FROM
            coupon_template
        WHERE
            template_no LIKE concat(#{key},'%')
    </select>
    <insert id="insert" parameterType="com.diligrp.etrade.coupon.domain.CouponTemplateDto">
        INSERT INTO `coupon_template` (
            `template_no`,
            `template_name`,
            `valid_type`,
            `fixed_days`,
            `type`,
            `extend_count`,
            `discount_amount`,
            `discount_ratio`,
            `upper_limit_amount`,
            `use_scope`,
            `apply_start_time`,
            `apply_end_time`,
            `issue_start_time`,
            `issue_end_time`,
            `notes`,
            `market_id`,
            `version`,
            `created_time`,
            `modified_time`,
            `creater_id`,
            `creater_code`,
            `creater_name`,
            `state`
        )
        VALUES
            (#{templateNo}, #{templateName}, #{validType},
             #{fixedDays}, #{type}, #{extendCount},
             #{discountAmount}, #{discountRatio}, #{upperLimitAmount},
             #{useScope}, #{applyStartTime}, #{applyEndTime},
             #{issueStartTime}, #{issueEndTime}, #{notes},
             #{marketId}, #{version}, #{createdTime},
             #{modifiedTime}, #{createrId}, #{createrCode},
             #{createrName},#{state} )
    </insert>
    <select id="findById" resultMap="templateResultMap">
        SELECT
            ct.`id`,
            ct.`template_no`,
            ct.`template_name`,
            ct.`valid_type`,
            ct.`fixed_days`,
            ct.`state`,
            ct.`type`,
            ct.`extend_count`,
            ct.`discount_amount`,
            ct.`discount_ratio`,
            ct.`upper_limit_amount`,
            ct.`use_scope`,
            CASE WHEN ct.`apply_start_time` = '1001-01-01 00:00:01' THEN NULL ELSE ct.`apply_start_time` END AS apply_start_time,
            CASE WHEN ct.`apply_end_time` = '9999-12-31 23:59:59' THEN NULL ELSE ct.`apply_end_time` END AS apply_end_time,
            CASE WHEN ct.`issue_start_time` = '1001-01-01 00:00:01' THEN NULL ELSE ct.`issue_start_time` END AS issue_start_time,
            CASE WHEN ct.`issue_end_time` = '9999-12-31 23:59:59' THEN NULL ELSE ct.`issue_end_time` END AS issue_end_time,
            ct.`notes`,
            ct.`market_id`,
            ct.`version`,
            ct.`created_time`,
            ct.`modified_time`,
            ct.`creater_id`,
            ct.`creater_code`,
            ct.`creater_name`,
            cr.`satisfied_amount`
        FROM
            coupon_template ct
                JOIN coupon_ruler cr ON ct.template_no = cr.template_no
        WHERE
            ct.id = #{id}
    </select>
    <update id="updateById" parameterType="com.diligrp.etrade.coupon.model.CouponTemplateDo">
        UPDATE `coupon_template`
        SET
            `state` = #{state},
            `version` = #{version}+1,
            `modified_time` = #{modifiedTime}
        WHERE
            `id` = #{id}
             and
             `version` = #{version}
    </update>
    <select id="page" parameterType="com.diligrp.etrade.coupon.domain.TemplateQueryDto" resultMap="templateResultMap">
        SELECT
        ct.id,
        ct.template_no,
        ct.template_name,
        ct.state,
        ct.creater_name,
        ct.created_time,
        CASE WHEN ct.`issue_start_time` = '1001-01-01 00:00:01' THEN NULL ELSE ct.`issue_start_time` END AS
        issue_start_time,
        CASE WHEN ct.`issue_end_time` = '9999-12-31 23:59:59' THEN NULL ELSE ct.`issue_end_time` END AS issue_end_time,
        ct.extend_count,
        SUM( CASE WHEN ciu.active = 1 THEN 1 ELSE 0 END ) bindCount,
        SUM(CASE WHEN ciu.state = 3 THEN 1 ELSE 0 END) usedCount
        FROM
        coupon_template ct
        LEFT JOIN coupon_bind_user ciu ON ct.template_no = ciu.template_no
        WHERE
         1=1
        <if test="marketId != null"><![CDATA[ AND ct.market_id = #{marketId}]]></if>
        <if test="templateNo != null and templateNo != ''">
            AND ct.template_no LIKE concat('%',#{templateNo},'%')
        </if>
        <if test="templateName != null and templateName != ''">
            AND ct.template_name LIKE concat('%',#{templateName},'%')
        </if>
        <if test="state != null"><![CDATA[ AND ct.state = #{state}]]></if>
        GROUP BY
        ct.template_no
        ORDER BY ct.id DESC
    </select>
    <select id="selectForTask" resultMap="templateResultMap">
        select
            `id`,
            `template_no`,
            `template_name`,
            `issue_start_time`,
            `issue_end_time`,
            `version`,
            `created_time`,
            `modified_time`,
            `state`
        from coupon_template
        where state in (0,1)
    </select>
    <update id="batchUpdateTemplate" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index">
            update
            coupon_template
            SET
            state = #{item.state},
            modified_time = now(),
            version = version+1
            WHERE id = #{item.id} and version = #{item.version};
        </foreach>
    </update>
    <select id="getTemplateByNo" resultMap="templateResultMap">
        select
            `id`,
            `fixed_days`,
            `template_no`,
            `template_name`,
            `state`,
            `valid_type`,
            `type`,
            `extend_count`,
            `discount_amount`,
            `discount_ratio`,
            `upper_limit_amount`,
            `use_scope`,
            `apply_start_time`,
            `apply_end_time`,
            `issue_start_time`,
            `issue_end_time`,
            `notes`,
            `market_id`,
            `version`,
            `created_time`,
            `modified_time`,
            `creater_id`,
            `creater_code`,
            `creater_name`
        from coupon_template
        where template_no = #{templateNo}
    </select>
</mapper>