<?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.shipping.mapper.TCtnpriceMapper">

    <resultMap type="TCtnprice" id="TCtnpriceResult">
        <result property="fId" column="f_id"/>
        <result property="fPortofloadid" column="f_portofloadid"/>
        <result property="fDistinationid" column="f_distinationid"/>
        <result property="fPortoftransshipment" column="f_portoftransshipment"/>
        <result property="fEtd" column="f_ETD"/>
        <result property="fEta" column="f_ETA"/>
        <result property="fDays" column="f_days"/>
        <result property="fBegindate" column="f_begindate"/>
        <result property="fEnd" column="f_end"/>
        <result property="fTeu" column="f_teu"/>
        <result property="fDates" column="f_dates"/>
        <result property="fStatus" column="f_status"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectTCtnpriceVo">
        select f_id,
               f_portofloadid,
               f_distinationid,
               f_portoftransshipment,
               f_ETD,
               f_ETA,
               f_days,
               f_begindate,
               f_end,
               f_teu,
               f_dates,
               f_status,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from t_ctnprice
    </sql>

    <select id="selectTCtnpriceList" parameterType="TCtnprice" resultMap="TCtnpriceResult">
        SELECT
        tc.f_id,
        tc.f_portofloadid,
        ta.f_name portofloadidName,
        tc.f_distinationid,
        tr.f_name distinationidName,
        tc.f_portoftransshipment,
        ts.f_name portoftransshipmentName,
        tc.f_ETD,
        tc.f_ETA,
        tc.f_days,
        tc.f_begindate,
        tc.f_end,
        tc.f_teu,
        tc.f_dates,
        CASE
        WHEN tc.f_status = 'T' THEN
        '正常' ELSE '停用'
        END AS f_status,
        tc.create_by,
        tc.create_time,
        tc.update_by,
        tc.update_time,
        tc.remark
        FROM
        t_ctnprice tc
        LEFT JOIN t_address ta ON tc.f_portofloadid = ta.f_id
        LEFT JOIN t_address tr ON tc.f_distinationid = tr.f_id
        LEFT JOIN t_address ts ON tc.f_portoftransshipment = ts.f_id
        <where>
            <if test="fPortofloadid != null ">and tc.f_portofloadid = #{fPortofloadid}</if>
            <if test="fDistinationid != null ">and tc.f_distinationid = #{fDistinationid}</if>
            <if test="fPortoftransshipment != null ">and tc.f_portoftransshipment = #{fPortoftransshipment}</if>
            <if test="fEtd != null ">and tc.f_ETD = #{fEtd}</if>
            <if test="fEta != null ">and tc.f_ETA = #{fEta}</if>
            <if test="fDays != null ">and tc.f_days = #{fDays}</if>
            <if test="fBegindate != null ">and tc.f_begindate = #{fBegindate}</if>
            <if test="fEnd != null ">and tc.f_end = #{fEnd}</if>
            <if test="fTeu != null  and fTeu != ''">and tc.f_teu = #{fTeu}</if>
            <if test="fDates != null  and fDates != ''">and tc.f_dates = #{fDates}</if>
            <if test="fStatus != null  and fStatus != ''">and tc.f_status = #{fStatus}</if>
            <if test="createBy != null  and createBy != ''">and tc.create_by like concat('%', #{createBy}, '%')</if>
            <if test="remark != null  and remark != ''">and tc.remark like concat('%', #{remark}, '%')</if>
            <if test='cLoadDate != null and cLoadDate[0] != null and cLoadDate[0]!= ""'>
                and tc.create_time &gt;= #{cLoadDate[0]}
            </if>
            <if test='cLoadDate != null and cLoadDate[1] != null and cLoadDate[1]!= ""'>
                and tc.create_time &lt;= #{cLoadDate[1]}
            </if>
        </where>
    </select>

    <select id="selectTCtnpriceById" parameterType="Long" resultMap="TCtnpriceResult">
        <include refid="selectTCtnpriceVo"/>
        where f_id = #{fId}
    </select>

    <insert id="insertTCtnprice" parameterType="TCtnprice" useGeneratedKeys="true" keyProperty="fId">
        insert into t_ctnprice
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fPortofloadid != null">f_portofloadid,</if>
            <if test="fDistinationid != null">f_distinationid,</if>
            <if test="fPortoftransshipment != null">f_portoftransshipment,</if>
            <if test="fEtd != null">f_ETD,</if>
            <if test="fEta != null">f_ETA,</if>
            <if test="fDays != null">f_days,</if>
            <if test="fBegindate != null">f_begindate,</if>
            <if test="fEnd != null">f_end,</if>
            <if test="fTeu != null">f_teu,</if>
            <if test="fDates != null">f_dates,</if>
            <if test="fStatus != null">f_status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</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="fPortofloadid != null">#{fPortofloadid},</if>
            <if test="fDistinationid != null">#{fDistinationid},</if>
            <if test="fPortoftransshipment != null">#{fPortoftransshipment},</if>
            <if test="fEtd != null">#{fEtd},</if>
            <if test="fEta != null">#{fEta},</if>
            <if test="fDays != null">#{fDays},</if>
            <if test="fBegindate != null">#{fBegindate},</if>
            <if test="fEnd != null">#{fEnd},</if>
            <if test="fTeu != null">#{fTeu},</if>
            <if test="fDates != null">#{fDates},</if>
            <if test="fStatus != null">#{fStatus},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateTCtnprice" parameterType="TCtnprice">
        update t_ctnprice
        <trim prefix="SET" suffixOverrides=",">
            <if test="fPortofloadid != null">f_portofloadid = #{fPortofloadid},</if>
            <if test="fDistinationid != null">f_distinationid = #{fDistinationid},</if>
            <if test="fPortoftransshipment != null">f_portoftransshipment = #{fPortoftransshipment},</if>
            <if test="fPortoftransshipment == null">f_portoftransshipment = null,</if>
            <if test="fEtd != null">f_ETD = #{fEtd},</if>
            <if test="fEta != null">f_ETA = #{fEta},</if>
            <if test="fDays != null">f_days = #{fDays},</if>
            <if test="fBegindate != null">f_begindate = #{fBegindate},</if>
            <if test="fEnd != null">f_end = #{fEnd},</if>
            <if test="fTeu != null">f_teu = #{fTeu},</if>
            <if test="fDates != null">f_dates = #{fDates},</if>
            <if test="fStatus != null">f_status = #{fStatus},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</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 f_id = #{fId}
    </update>

    <delete id="deleteTCtnpriceById" parameterType="Long">
        delete
        from t_ctnprice
        where f_id = #{fId}
    </delete>

    <delete id="deleteTCtnpriceByIds" parameterType="String">
        delete from t_ctnprice where f_id in
        <foreach item="fId" collection="array" open="(" separator="," close=")">
            #{fId}
        </foreach>
    </delete>
    <select id="selectMessage" parameterType="TCtnprice" resultMap="TCtnpriceResult">
        SELECT
        t.f_id, t.f_portofloadid, t.f_distinationid, t.f_portoftransshipment, t.f_begindate, t.f_end
        FROM
        t_ctnprice t
        WHERE
        t.f_portofloadid = #{fPortofloadid}
        AND t.f_distinationid = #{fDistinationid}
        AND t.f_status = 'T'
        AND #{fBegindate} BETWEEN t.f_begindate AND t.f_end
        <if test="fPortoftransshipment != null">AND f_portoftransshipment = #{fPortoftransshipment}</if>
        <if test="fPortoftransshipment == null">AND f_portoftransshipment IS NULL</if>
    </select>

</mapper>