<?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.logistics.mapper.BizWaybillCostDetailsMapper">
    
    <resultMap type="com.ruoyi.logistics.domain.BizWaybillCostDetails" id="BizWaybillCostDetailsResult">
        <result property="waybillDetailId"    column="waybill_detail_id"    />
        <result property="waybillId"    column="waybill_id"    />
        <result property="externalWaybillNo"    column="external_waybill_no"    />
        <result property="feeItemCode"    column="fee_item_code"    />
        <result property="feeItemName"    column="fee_item_name"    />
        <result property="feeName"    column="fee_name"    />
        <result property="amount"    column="amount"    />
        <result property="rateAmount"    column="rate_amount"    />
        <result property="adjustAmount"    column="adjust_amount"    />
        <result property="purchaseAmount"    column="purchase_amount"    />
        <result property="rate"    column="rate"    />
        <result property="remark"    column="remark"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="deptId"    column="dept_id"    />
        <result property="userId"    column="user_id"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="orderType"    column="order_type"    />
        <result property="senderCity"    column="sender_city"    />
        <result property="receiverCity"    column="receiver_city"    />
        <result property="signTime"    column="sign_time"    />
        <result property="goodsWeight"    column="goods_weight"    />
        <result property="productCode"    column="product_code"    />
        <result property="productName"    column="product_name"    />
        <result property="userName"    column="user_name"    />
        <result property="summaryMonth"    column="summaryMonth"    />




    </resultMap>

    <sql id="selectBizWaybillCostDetailsVo">
        SELECT
            a.waybill_detail_id,
            b.sign_time,
            b.external_waybill_no,
            b.sender_city,
            b.receiver_city,
            b.goods_weight,
            b.order_type,
            CASE b.order_type
        WHEN 1 THEN (SELECT dict_label	FROM sys_dict_data WHERE dict_type = 'jd_logistics_product_code' AND dict_value = b.product_code LIMIT 1)
        WHEN 2 THEN (SELECT dict_label FROM sys_dict_data WHERE dict_type = 'sf_logistics_product_code' AND dict_value = b.product_code LIMIT 1)
        ELSE NULL END AS product_name,
            b.product_code,
            u.nick_name,
            a.waybill_id,
            a.external_waybill_no,
            a.fee_item_code,
            a.fee_item_name,
            a.amount,
            ifnull( a.adjust_amount, a.rate_amount ) rate_amount,
            ifnull(TRIM(TRAILING '.' FROM TRIM(TRAILING '0' FROM rate)),'--') rate,
            a.purchase_amount,
            DATE_FORMAT( b.sign_time, '%Y.%m' )  summaryMonth,
            a.remark,
            a.create_time,
            a.update_time,
            a.dept_id,
            a.del_flag,
            a.create_by,
            a.update_by,u.nick_name  user_name
        FROM
            biz_waybill_cost_details a
                LEFT JOIN biz_waybill_order b ON a.waybill_id = b.waybill_id
                LEFT JOIN sys_user u ON b.user_id = u.user_id
                LEFT JOIN sys_dept d ON a.dept_id = d.dept_id
                </sql>

    <select id="selectBizWaybillCostDetailsList" parameterType="com.ruoyi.logistics.domain.BizWaybillCostDetails" resultMap="BizWaybillCostDetailsResult">
        <include refid="selectBizWaybillCostDetailsVo"/>
        <where>
            b.order_status = 5
            <if test="summaryMonth != null "> AND DATE_FORMAT( b.sign_time, '%Y.%m' ) = #{summaryMonth}</if>
            <if test="deptId != null "> AND b.dept_id = #{deptId}</if>
            <if test="externalWaybillNo != null  and externalWaybillNo != ''"> and a.external_waybill_no = #{externalWaybillNo}</if>
            ${params.dataScope}

           <!-- <if test="waybillId != null "> and waybill_id = #{waybillId}</if>

            <if test="feeItemCode != null  and feeItemCode != ''"> and fee_item_code = #{feeItemCode}</if>
            <if test="feeItemName != null  and feeItemName != ''"> and fee_item_name like concat('%', #{feeItemName}, '%')</if>
            <if test="feeName != null  and feeName != ''"> and fee_name like concat('%', #{feeName}, '%')</if>
            <if test="amount != null "> and amount = #{amount}</if>
            <if test="rateAmount != null "> and rate_amount = #{rateAmount}</if>
            <if test="adjustAmount != null "> and adjust_amount = #{adjustAmount}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>-->
        </where>
    </select>


    <select id="detaillist" parameterType="com.ruoyi.logistics.domain.BizWaybillCostDetails" resultMap="BizWaybillCostDetailsResult">
        <include refid="selectBizWaybillCostDetailsVo"/>
        <where>

            <if test="waybillId != null "> and a.waybill_id = #{waybillId}</if>

        </where>
    </select>

    <select id="selectBizWaybillCostDetailsListByWx" parameterType="com.ruoyi.logistics.domain.BizWaybillCostDetails" resultMap="BizWaybillCostDetailsResult">
        select waybill_detail_id, waybill_id, external_waybill_no, fee_item_code, fee_item_name, fee_name, amount, rate_amount, adjust_amount, remark, create_time, update_time, dept_id, user_id, del_flag, create_by, update_by from biz_waybill_cost_details

    <where>

     <if test="waybillId != null "> and waybill_id = #{waybillId}</if>
        <!--  <if test="externalWaybillNo != null  and externalWaybillNo != ''"> and external_waybill_no = #{externalWaybillNo}</if>
         <if test="feeItemCode != null  and feeItemCode != ''"> and fee_item_code = #{feeItemCode}</if>
         <if test="feeItemName != null  and feeItemName != ''"> and fee_item_name like concat('%', #{feeItemName}, '%')</if>
         <if test="feeName != null  and feeName != ''"> and fee_name like concat('%', #{feeName}, '%')</if>
         <if test="amount != null "> and amount = #{amount}</if>
         <if test="rateAmount != null "> and rate_amount = #{rateAmount}</if>
         <if test="adjustAmount != null "> and adjust_amount = #{adjustAmount}</if>
         <if test="deptId != null "> and dept_id = #{deptId}</if>
         <if test="userId != null "> and user_id = #{userId}</if>-->
    </where>
    </select>




    <select id="getAccountAdjustmentById" parameterType="com.ruoyi.logistics.domain.BizWaybillCostDetails" resultMap="BizWaybillCostDetailsResult">
        SELECT
        a.waybill_detail_id,
        b.sign_time,
        b.external_waybill_no,
        b.sender_city,
        b.receiver_city,
        b.goods_weight,
        b.order_type,
        b.product_code,
        a.waybill_id,
        a.external_waybill_no,
        a.fee_item_code,
        a.fee_item_name,
        a.amount,
        ifnull( a.adjust_amount, a.rate_amount ) rate_amount,
        DATE_FORMAT( b.sign_time, '%Y.%m' )  summaryMonth,
        a.remark,
        a.create_time,
        a.update_time,
        a.dept_id,
        a.update_by,u.nick_name  user_name
        FROM
        biz_waybill_cost_details a
        LEFT JOIN biz_waybill_order b ON a.waybill_id = b.waybill_id
        LEFT JOIN sys_user u ON b.user_id = u.user_id
        LEFT JOIN sys_dept d ON a.dept_id = d.dept_id

    <where>

        <if test="waybillDetailId != null "> and a.waybill_detail_id = #{waybillDetailId}</if>

    </where>
    </select>


    <select id="selectBizWaybillCostDetailsByWaybillDetailId" parameterType="Long" resultMap="BizWaybillCostDetailsResult">
        <include refid="selectBizWaybillCostDetailsVo"/>
        where a.waybill_detail_id = #{waybillDetailId}
    </select>

    <insert id="upsertBizWaybillCostDetails" parameterType="com.ruoyi.logistics.domain.BizWaybillCostDetails" useGeneratedKeys="true" keyProperty="waybillDetailId">
        INSERT INTO biz_waybill_cost_details (
            waybill_id,
            external_waybill_no,
            fee_item_code,
            fee_item_name,
            fee_name,
            amount,
            rate_amount,
            adjust_amount,
            remark,
            create_time,
            update_time,
            dept_id,
            user_id,
            del_flag,
            create_by,
            update_by,
            purchase_amount
        ) VALUES (
            #{waybillId},
            #{externalWaybillNo},
            #{feeItemCode},
            #{feeItemName},
            #{feeName},
            #{amount},
            #{rateAmount},
            #{adjustAmount},
            #{remark},
            #{createTime},
            #{updateTime},
            #{deptId},
            #{userId},
            #{delFlag},
            #{createBy},
            #{updateBy},
            #{purchaseAmount}
        )
        ON DUPLICATE KEY UPDATE
            amount = #{amount},
            rate_amount = #{rateAmount},
            adjust_amount = #{adjustAmount},
            purchase_amount = #{purchaseAmount},
            del_flag = #{delFlag},
            update_time = #{updateTime},
            update_by = #{updateBy}
    </insert>

    <insert id="insertBizWaybillCostDetails" parameterType="com.ruoyi.logistics.domain.BizWaybillCostDetails" useGeneratedKeys="true" keyProperty="waybillDetailId">
        insert into biz_waybill_cost_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="waybillId != null">waybill_id,</if>
            <if test="externalWaybillNo != null">external_waybill_no,</if>
            <if test="feeItemCode != null">fee_item_code,</if>
            <if test="feeItemName != null">fee_item_name,</if>
            <if test="feeName != null">fee_name,</if>
            <if test="amount != null">amount,</if>
            <if test="rateAmount != null">rate_amount,</if>
            <if test="adjustAmount != null">adjust_amount,</if>
            <if test="remark != null">remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="waybillId != null">#{waybillId},</if>
            <if test="externalWaybillNo != null">#{externalWaybillNo},</if>
            <if test="feeItemCode != null">#{feeItemCode},</if>
            <if test="feeItemName != null">#{feeItemName},</if>
            <if test="feeName != null">#{feeName},</if>
            <if test="amount != null">#{amount},</if>
            <if test="rateAmount != null">#{rateAmount},</if>
            <if test="adjustAmount != null">#{adjustAmount},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
         </trim>
    </insert>

    <update id="updateBizWaybillCostDetails" parameterType="com.ruoyi.logistics.domain.BizWaybillCostDetails">
        update biz_waybill_cost_details
        <trim prefix="SET" suffixOverrides=",">
            <if test="waybillId != null">waybill_id = #{waybillId},</if>
            <if test="externalWaybillNo != null">external_waybill_no = #{externalWaybillNo},</if>
            <if test="feeItemCode != null">fee_item_code = #{feeItemCode},</if>
            <if test="feeItemName != null">fee_item_name = #{feeItemName},</if>
            <if test="feeName != null">fee_name = #{feeName},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="rateAmount != null">rate_amount = #{rateAmount},</if>
            <if test="adjustAmount != null">adjust_amount = #{adjustAmount},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where waybill_detail_id = #{waybillDetailId}
    </update>

    <delete id="deleteBizWaybillCostDetailsByWaybillDetailId" parameterType="Long">
        delete from biz_waybill_cost_details where waybill_detail_id = #{waybillDetailId}
    </delete>

    <delete id="deleteBizWaybillCostDetailsByWaybillDetailIds" parameterType="String">
        delete from biz_waybill_cost_details where waybill_detail_id in 
        <foreach item="waybillDetailId" collection="array" open="(" separator="," close=")">
            #{waybillDetailId}
        </foreach>
    </delete>

    <select id="selectBizWaybillCostDetailsSum" parameterType="BizWaybillCostDetails" resultType="java.math.BigDecimal">
        select coalesce(sum(coalesce(adjust_amount, rate_amount)), 0)
        from biz_waybill_cost_details bwcd
        left join sys_user u on bwcd.user_id = u.user_id
        left join sys_dept d on bwcd.dept_id = d.dept_id
        <where>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
    </select>
</mapper>