<?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.goods.mapper.BizGoodsFreightMapper">

    <resultMap id="BizGoodsFreightResult" type="com.ruoyi.web.modules.goods.domain.BizGoodsFreight">
        <id property="goodsId" column="goods_id"/>
        <result property="freightType" column="freight_type"/>
        <result property="fixedAmount" column="fixed_amount"/>
        <result property="freightTemplateId" column="freight_template_id"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <select id="selectByGoodsId" resultMap="BizGoodsFreightResult">
        select goods_id, freight_type, fixed_amount, freight_template_id, del_flag, create_time, update_time
        from biz_goods_freight
        where goods_id = #{goodsId} and del_flag = '0'
    </select>

    <select id="countByFreightTemplateId" resultType="int">
        select count(1)
        from biz_goods_freight f
        inner join biz_goods g on g.goods_id = f.goods_id and g.del_flag = '0'
        where f.freight_type = '2'
          and f.freight_template_id = #{templateId}
          and f.del_flag = '0'
    </select>

    <insert id="insert">
        insert into biz_goods_freight (
            goods_id, freight_type, fixed_amount, freight_template_id, del_flag, create_time, update_time
        ) values (
            #{goodsId}, #{freightType}, #{fixedAmount}, #{freightTemplateId}, #{delFlag}, sysdate(), sysdate()
        )
    </insert>

    <update id="update">
        update biz_goods_freight
        set freight_type = #{freightType},
            fixed_amount = #{fixedAmount},
            freight_template_id = #{freightTemplateId},
            update_time = sysdate()
        where goods_id = #{goodsId} and del_flag = '0'
    </update>

    <delete id="deleteByGoodsId">
        delete from biz_goods_freight where goods_id = #{goodsId}
    </delete>

</mapper>
