<?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.ruoyi.web.modules.freight.mapper.BizShopFreightTemplateMapper">

    <resultMap id="BizShopFreightTemplateResult" type="com.ruoyi.web.modules.freight.domain.BizShopFreightTemplate">
        <id property="templateId" column="template_id"/>
        <result property="shopId" column="shop_id"/>
        <result property="templateName" column="template_name"/>
        <result property="shipTime" column="ship_time"/>
        <result property="freeFlag" column="free_flag"/>
        <result property="pricingType" column="pricing_type"/>
        <result property="isDefault" column="is_default"/>
        <result property="firstUnit" column="first_unit"/>
        <result property="firstFee" column="first_fee"/>
        <result property="addUnit" column="add_unit"/>
        <result property="addFee" column="add_fee"/>
        <result property="freeThreshold" column="free_threshold"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectColumns">
        template_id, shop_id, template_name, ship_time, free_flag, pricing_type, is_default,
        first_unit, first_fee, add_unit, add_fee, free_threshold, del_flag,
        create_by, create_time, update_by, update_time
    </sql>

    <select id="selectList" resultMap="BizShopFreightTemplateResult">
        select <include refid="selectColumns"/>
        from biz_shop_freight_template
        where shop_id = #{shopId} and del_flag = '0'
        <if test="keyword != null and keyword != ''">
            and template_name like concat('%', #{keyword}, '%')
        </if>
        order by is_default desc, create_time desc
    </select>

    <select id="selectById" resultMap="BizShopFreightTemplateResult">
        select <include refid="selectColumns"/>
        from biz_shop_freight_template
        where template_id = #{templateId} and shop_id = #{shopId} and del_flag = '0'
    </select>

    <select id="selectDefaultByShopId" resultMap="BizShopFreightTemplateResult">
        select <include refid="selectColumns"/>
        from biz_shop_freight_template
        where shop_id = #{shopId} and del_flag = '0' and is_default = '1'
        limit 1
    </select>

    <select id="countByName" resultType="int">
        select count(1) from biz_shop_freight_template
        where shop_id = #{shopId} and template_name = #{templateName} and del_flag = '0'
        <if test="excludeTemplateId != null">
            and template_id != #{excludeTemplateId}
        </if>
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="templateId">
        insert into biz_shop_freight_template (
            shop_id, template_name, ship_time, free_flag, pricing_type, is_default,
            first_unit, first_fee, add_unit, add_fee, free_threshold, del_flag, create_by, create_time
        ) values (
            #{shopId}, #{templateName}, #{shipTime}, #{freeFlag}, #{pricingType}, #{isDefault},
            #{firstUnit}, #{firstFee}, #{addUnit}, #{addFee}, #{freeThreshold}, #{delFlag}, #{createBy}, sysdate()
        )
    </insert>

    <update id="update">
        update biz_shop_freight_template
        set template_name = #{templateName},
            ship_time = #{shipTime},
            free_flag = #{freeFlag},
            pricing_type = #{pricingType},
            is_default = #{isDefault},
            first_unit = #{firstUnit},
            first_fee = #{firstFee},
            add_unit = #{addUnit},
            add_fee = #{addFee},
            free_threshold = #{freeThreshold},
            update_by = #{updateBy},
            update_time = sysdate()
        where template_id = #{templateId} and shop_id = #{shopId} and del_flag = '0'
    </update>

    <update id="clearDefaultByShopId">
        update biz_shop_freight_template
        set is_default = '0', update_time = sysdate()
        where shop_id = #{shopId} and del_flag = '0' and is_default = '1'
        <if test="excludeTemplateId != null">
            and template_id != #{excludeTemplateId}
        </if>
    </update>

    <update id="updateIsDefault">
        update biz_shop_freight_template
        set is_default = #{isDefault}, update_by = #{updateBy}, update_time = sysdate()
        where template_id = #{templateId} and shop_id = #{shopId} and del_flag = '0'
    </update>

    <update id="logicDeleteByIds">
        update biz_shop_freight_template
        set del_flag = '2', update_by = #{updateBy}, update_time = sysdate()
        where shop_id = #{shopId} and del_flag = '0'
          and template_id in
        <foreach collection="templateIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>
