<?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.quotation.mapper.TSeapriceMapper">

    <resultMap type="TSeaprice" id="TSeapriceResult">
        <result property="fId" column="f_id"/>
        <result property="fBilltype" column="f_billtype"/>
        <result property="fBilltypeName" column="f_billtype_name"/>
        <result property="fCorpid" column="f_corpid"/>
        <result property="polId" column="pol_id"/>
        <result property="polName" column="pol_name"/>
        <result property="podId" column="pod_id"/>
        <result property="podName" column="pod_name"/>
        <result property="fValiddate" column="f_validdate"/>
        <result property="fDateChanged" column="f_date_changed"/>
        <result property="fBillstatus" column="f_billstatus"/>
        <result property="fBillstatusName" column="f_billstatus_name"/>
        <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"/>
        <result property="remarks" column="remarks"/>
        <result property="fLineid" column="f_lineid"/>
        <result property="fBookagentid" column="f_bookagentid"/>
        <result property="fLineName" column="f_line_name"/>
        <result property="fBookagentName" column="f_bookagent_name"/>
        <result property="fTransitPortId" column="f_transit_port_id"/>
        <result property="fSailingSchedule" column="f_sailing_schedule"/>
        <result property="fVoyage" column="f_voyage"/>
    </resultMap>

    <sql id="selectTSeapriceVo">
        select s.f_id,
               s.f_billtype,
               dic.dict_label as f_billtype_name,
               s.f_corpid,
               s.pol_id,
               ltd.f_name as pol_name,
               s.pod_id,
               dtd.f_name as pod_name,
               s.f_validdate,
               s.f_date_changed,
               CASE
                   WHEN s.f_billstatus=0 THEN '正常'
                   WHEN s.f_billstatus=1 THEN '停用'
                END as f_billstatus_name,
               s.f_billstatus,
               s.del_flag,
               s.create_by,
               s.create_time,
               s.update_by,
               s.update_time,
               s.remarks,
               s.f_lineid,
               s.f_bookagentid,
               s.f_transit_port_id,
               s.f_sailing_schedule,
               s.f_voyage,
               j.f_name  AS f_line_name,
               q.f_name  AS f_bookagent_name,
               tc.f_name AS fNamec
        from t_seaprice s
                 LEFT JOIN t_address j ON s.f_lineid = j.f_id
                 LEFT JOIN t_corps q ON s.f_bookagentid = q.f_id
                 LEFT JOIN t_corps tc ON s.f_corpid = tc.f_id
                 LEFT JOIN (
                    SELECT *
                    FROM sys_dict_data dic
                    WHERE dic.dict_label = IF(dic.dict_type = "data_billType", dic.dict_label, NULL)
                ) dic ON dic.dict_value = s.f_billtype
                 LEFT JOIN t_address ltd ON s.pol_id = ltd.f_id
                 LEFT JOIN t_address dtd ON s.pod_id = dtd.f_id
    </sql>

    <select id="selectTSeapriceList" parameterType="TSeaprice" resultMap="TSeapriceResult">
        <include refid="selectTSeapriceVo"/>
        <where>
            <if test="fBilltype != null ">and f_billtype = #{fBilltype}</if>
            <if test="fCorpid != null ">and f_corpid = #{fCorpid}</if>
            <if test="polId != null ">and pol_id = #{polId}</if>
            <if test="podId != null ">and pod_id = #{podId}</if>
            <if test="fBillstatus != null ">and f_billstatus = #{fBillstatus}</if>
            <if test="remarks != null  and remarks != ''">and remarks = #{remarks}</if>
            <if test='validdateList != null and validdateList[0] != null and validdateList[0]!= ""'>
                and f_validdate &gt;= #{validdateList[0]}
            </if>
            <if test='validdateList != null and validdateList[1] != null and validdateList[1]!= ""'>
                and f_validdate &lt;= #{validdateList[1]}
            </if>
            <if test="fLineid != null  and fLineid != ''">and f_lineid = #{fLineid}</if>
            <if test="fBookagentid != null  and fBookagentid != ''">and f_bookagentid = #{fBookagentid}</if>
            <if test="fTransitPortId != null ">and f_transit_port_id = #{fTransitPortId}</if>
            <if test="fSailingSchedule != null  and fSailingSchedule != ''">and f_sailing_schedule =
                #{fSailingSchedule}
            </if>
            <if test="fVoyage != null ">and f_voyage = #{fVoyage}</if>

        </where>
    </select>

    <select id="selectTSeapriceById" parameterType="Long" resultMap="TSeapriceResult">
        <include refid="selectTSeapriceVo"/>
        where s.f_id = #{fId}
    </select>

    <select id="selectTSeapriceMapList" parameterType="TSeaprice" resultType="Map">
        SELECT
        s.f_id fId,
        t.dict_label fBilltype,
        c.f_id fCorpid,
        c.f_cname fCorpName,
        CASE
        WHEN s.f_billstatus = '0' THEN
        '正常'
        WHEN s.f_billstatus = '1' THEN
        '停用'
        END AS fBillstatus,
        pol.f_name polId,
        pod.f_name podId,
        transit.f_name fTransitPortId,
        s.f_sailing_schedule fSailingSchedule,
        s.f_voyage fVoyage,
        s.create_by createBy,
        s.f_validdate fValiddate,
        s.remarks,
        s.f_lineid fLineid,
        s.f_bookagentid fBookagentid,
        j.f_name fLineName,
        q.f_name fBookagentName
        FROM
        t_seaprice s
        LEFT JOIN sys_dict_data t ON t.dict_value = s.f_billtype AND t.dict_type = 'data_billType'
        LEFT JOIN t_address pol ON s.pol_id = pol.f_id
        LEFT JOIN t_address pod ON s.pod_id = pod.f_id
        LEFT JOIN t_address transit ON s.f_transit_port_id = transit.f_id
        LEFT JOIN t_corps c ON c.f_id = s.f_corpid
        LEFT JOIN t_address j ON s.f_lineid = j.f_id
        LEFT JOIN t_corps q ON s.f_bookagentid = q.f_id
        <where>
            <if test="fBilltype != null ">and s.f_billtype = #{fBilltype}</if>
            <if test="fCorpid != null ">and s.f_corpid = #{fCorpid}</if>
            <if test="polId != null ">and s.pol_id = #{polId}</if>
            <if test="podId != null ">and s.pod_id = #{podId}</if>
            <if test="fValiddate != null ">and s.f_validdate = #{fValiddate}</if>
            <if test="fBillstatus != null ">and s.f_billstatus = #{fBillstatus}</if>
            <if test="remarks != null  and remarks != ''">and remarks = #{remarks}</if>
            <if test="fLineid != null  and fLineid != ''">and f_lineid = #{fLineid}</if>
            <if test="fBookagentid != null  and fBookagentid != ''">and f_bookagentid = #{fBookagentid}</if>
            <if test="fTransitPortId != null ">and f_transit_port_id = #{fTransitPortId}</if>
            <if test="fSailingSchedule != null  and fSailingSchedule != ''">and f_sailing_schedule =
                #{fSailingSchedule}
            </if>
            <if test="fVoyage != null ">and f_voyage = #{fVoyage}</if>
            <if test="fValiddateBegin != null and fValiddateBegin != '' and fValiddateEnd != null and fValiddateEnd!= ''">
                and s.f_validdate BETWEEN #{fValiddateBegin} AND #{fValiddateEnd}
            </if>
        </where>
        ORDER BY s.f_id DESC
    </select>

    <select id="checkSeapriceByValiddate" parameterType="TSeaprice" resultType="int">
        SELECT COUNT(f_id)
        FROM t_seaprice
        WHERE f_id != #{fId}
          AND f_billtype = #{fBilltype}
          AND f_corpid = #{fCorpid}
          AND pol_id = #{polId}
          AND pod_id = #{podId}
          AND pod_id = #{podId}
          AND f_validdate &gt;= #{fValiddate}
    </select>

    <select id="selectSeapriceItemAmt" parameterType="TSeaprice" resultType="BigDecimal">
        SELECT IFNULL(SUM(IF(#{fType} = '0', s.${fSpecification}, s.f_unitprice)), 0)
        FROM t_seaprice t
                 LEFT JOIN t_seapriceitem s ON s.f_pid = t.f_id
        WHERE t.f_billtype = #{fBilltype}
          AND t.f_corpid = #{fCorpid}
          AND t.f_validdate &gt;= #{fValiddate}
          AND t.f_billstatus = 0
          AND t.del_flag = '0'
          AND s.f_type = #{fType}
    </select>

    <select id="selectSeapriceItem" parameterType="TSeaprice" resultType="Map">
        SELECT f.f_name fFeeid, s.remark remark, IF(s.f_type = '0', s.${fSpecification}, s.f_unitprice) fUnitprice
        FROM t_seaprice t
                 LEFT JOIN t_seapriceitem s ON s.f_pid = t.f_id
                 LEFT JOIN t_fees f ON f.f_id = s.f_feeid
        WHERE t.f_billtype = #{fBilltype}
          AND t.f_corpid = #{fCorpid}
          AND t.f_validdate &gt;= #{fValiddate}
          AND t.f_billstatus = 0
          AND t.del_flag = '0'
          AND f.f_name != '海运费'
        ORDER BY s.f_type, s.f_id asc
    </select>

    <select id="selectShippingFee" parameterType="TSeaprice" resultType="com.ruoyi.quotation.domain.TSeapriceitem">
        SELECT s.f_id                                                            fId,
               s.f_feeid                                                         fFeeid,
               IFNULL(s.${fSpecification}, 0)                                    fUnitprice,
               IFNULL(s.f_exrate, 0)                                             fExrate,
               IFNULL(IFNULL(s.${fSpecification}, 0) * IFNULL(s.f_exrate, 0), 0) rmb
        FROM t_seaprice t
                 LEFT JOIN t_seapriceitem s ON s.f_pid = t.f_id
                 LEFT JOIN t_fees f ON f.f_id = s.f_feeid
        WHERE t.f_billtype = #{fBilltype}
          AND t.f_corpid = #{fCorpid}
          AND t.f_validdate &gt;= #{fValiddate}
          AND t.f_billstatus = 0
          AND t.del_flag = '0'
          AND f.f_name = '海运费'
        ORDER BY s.f_id asc LIMIT 0, 1
    </select>

    <insert id="insertTSeaprice" parameterType="TSeaprice" useGeneratedKeys="true" keyProperty="fId">
        insert into t_seaprice
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fBilltype != null">f_billtype,</if>
            <if test="fCorpid != null">f_corpid,</if>
            <if test="polId != null">pol_id,</if>
            <if test="podId != null">pod_id,</if>
            <if test="fValiddate != null">f_validdate,</if>
            <if test="fDateChanged != null">f_date_changed,</if>
            <if test="fBillstatus != null">f_billstatus,</if>
            <if test="delFlag != null">del_flag,</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="remarks != null">remarks,</if>
            <if test="fLineid != null">f_lineid,</if>
            <if test="fBookagentid != null">f_bookagentid,</if>
            <if test="fTransitPortId != null">f_transit_port_id,</if>
            <if test="fSailingSchedule != null">f_sailing_schedule,</if>
            <if test="fVoyage != null">f_voyage,</if>

        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="fBilltype != null">#{fBilltype},</if>
            <if test="fCorpid != null">#{fCorpid},</if>
            <if test="polId != null">#{polId},</if>
            <if test="podId != null">#{podId},</if>
            <if test="fValiddate != null">#{fValiddate},</if>
            <if test="fDateChanged != null">#{fDateChanged},</if>
            <if test="fBillstatus != null">#{fBillstatus},</if>
            <if test="delFlag != null">#{delFlag},</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="remarks != null">#{remarks},</if>
            <if test="fLineid != null">#{fLineid},</if>
            <if test="fBookagentid != null">#{fBookagentid},</if>
            <if test="fTransitPortId != null">#{fTransitPortId},</if>
            <if test="fSailingSchedule != null">#{fSailingSchedule},</if>
            <if test="fVoyage != null">#{fVoyage},</if>
        </trim>
    </insert>

    <update id="updateTSeaprice" parameterType="TSeaprice">
        update t_seaprice
        <trim prefix="SET" suffixOverrides=",">
            <if test="fBilltype != null">f_billtype = #{fBilltype},</if>
            <if test="fCorpid != null">f_corpid = #{fCorpid},</if>
            <if test="polId != null">pol_id = #{polId},</if>
            <if test="podId != null">pod_id = #{podId},</if>
            <if test="fValiddate != null">f_validdate = #{fValiddate},</if>
            <if test="fBillstatus != null">f_billstatus = #{fBillstatus},</if>
            <if test="fDateChanged != null">f_date_changed = #{fDateChanged},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</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="remarks != null">remarks = #{remarks},</if>
            <if test="fLineid != null">f_lineid = #{fLineid},</if>
            <if test="fBookagentid != null">f_bookagentid = #{fBookagentid},</if>
            <if test="fTransitPortId != null">f_transit_port_id = #{fTransitPortId},</if>
            <if test="fSailingSchedule != null">f_sailing_schedule = #{fSailingSchedule},</if>
            <if test="fVoyage != null">f_voyage = #{fVoyage},</if>
        </trim>
        where f_id = #{fId}
    </update>

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

    <delete id="deleteTSeapriceByIds" parameterType="String">
        delete from t_seaprice where f_id in
        <foreach item="fId" collection="array" open="(" separator="," close=")">
            #{fId}
        </foreach>
    </delete>

</mapper>