<?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.sckw.transport.dao.KwtLogisticsOrderGoodsUnitMapper">
    <resultMap id="BaseResultMap" type="com.sckw.transport.model.KwtLogisticsOrderGoodsUnit">
        <!--@mbg.generated-->
        <!--@Table kwt_logistics_order_goods_unit-->
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="l_order_id" jdbcType="BIGINT" property="lOrderId" />
        <result column="goods_id" jdbcType="BIGINT" property="goodsId" />
        <result column="from_unit" jdbcType="VARCHAR" property="fromUnit" />
        <result column="to_unit" jdbcType="VARCHAR" property="toUnit" />
        <result column="conversion_value" jdbcType="DECIMAL" property="conversionValue" />
        <result column="unit" jdbcType="VARCHAR" property="unit" />
        <result column="unload_amount" jdbcType="DECIMAL" property="unloadAmount" />
        <result column="load_amount" jdbcType="DECIMAL" property="loadAmount" />
        <result column="total_load_amount" jdbcType="DECIMAL" property="totalLoadAmount" />
        <result column="total_unload_amount" jdbcType="DECIMAL" property="totalUnloadAmount" />
        <result column="remark" jdbcType="VARCHAR" property="remark" />
        <result column="status" jdbcType="INTEGER" property="status" />
        <result column="create_by" jdbcType="BIGINT" property="createBy" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="update_by" jdbcType="BIGINT" property="updateBy" />
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
        <result column="del_flag" jdbcType="INTEGER" property="delFlag" />
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id, l_order_id, goods_id, from_unit, to_unit, conversion_value, unit, unload_amount,
        load_amount, total_load_amount, total_unload_amount, remark, `status`, create_by,
        create_time, update_by, update_time, del_flag
    </sql>

    <select id="findList" resultType="com.sckw.transport.model.KwtLogisticsOrderGoodsUnit" parameterType="java.util.Map" >
        SELECT
        id, l_order_id lOrderId, goods_id goodsId, from_unit fromUnit, to_unit toUnit,
        conversion_value conversionValue, unit,unload_amount unloadAmount , load_amount loadAmount,
        total_load_amount totalLoadAmount, total_unload_amount totalUnloadAmount,
        remark, status, create_by createBy, create_time createTime, update_by updateBy, update_time updateTime
        from kwt_logistics_order_goods_unit
        where del_flag = 0
        <if test="lOrderId != null and goodsId != ''">
            and l_order_id = #{lOrderId, jdbcType=BIGINT}
        </if>
        <if test="unit != null and unit != ''">
            and unit = #{unit, jdbcType=INTEGER}
        </if>
        ORDER BY create_time desc
    </select>

    <select id="findGoodsUnit" resultType="com.sckw.transport.model.KwtLogisticsOrderGoodsUnit" parameterType="java.util.Map" >
        SELECT
            id, l_order_id lOrderId, goods_id goodsId, from_unit fromUnit, to_unit toUnit,
            conversion_value conversionValue, unit,
            if(unit is null, null, IFNULL(SUM(unload_amount), 0.0)) unloadAmount,
            if(unit is null, null, IFNULL(SUM(load_amount), 0.0)) loadAmount,
            if(unit is null, null, IFNULL(SUM(total_load_amount), 0.0)) totalLoadAmount,
            if(unit is null, null, IFNULL(SUM(total_unload_amount), 0.0)) totalUnloadAmount,
            remark, status, create_by createBy, create_time createTime, update_by updateBy, update_time updateTime
        from kwt_logistics_order_goods_unit
        where del_flag = 0
        <if test="lOrderId != null and goodsId != ''">
            and l_order_id = #{lOrderId, jdbcType=BIGINT}
        </if>
        <if test="unit != null and unit != ''">
            and unit = #{unit, jdbcType=INTEGER}
        </if>
        ORDER BY create_time desc
    </select>

    <select id="findGoodsUnitCount" resultType="com.sckw.transport.model.KwtLogisticsOrderGoodsUnit" parameterType="java.util.Map" >
        SELECT
        IFNULL(SUM(logu.load_amount), 0.0) loadAmount,
        IFNULL(SUM(logu.unload_amount), 0.0) unloadAmount,
        IFNULL(SUM(logu.total_load_amount), 0.0) totalLoadAmount,
        IFNULL(SUM(logu.total_unload_amount), 0.0) totalUnloadAmount
        from kwt_logistics_order_goods_unit logu
        left join kwt_logistics_order lo on lo.id = logu.l_order_id
        where logu.del_flag = 0 and lo.del_flag = 0
        <if test="belowlOrderId != null and belowlOrderId != ''">
            and FIND_IN_SET(#{belowlOrderId, jdbcType=BIGINT}, lo.pids)
            and logu.l_order_id != #{belowlOrderId, jdbcType=BIGINT}
        </if>
        <if test="upperlOrderId != null and upperlOrderId != ''">
            and FIND_IN_SET(#{upperlOrderId, jdbcType=BIGINT}, lo.pids)
        </if>
        <if test="unit != null and unit != ''">
            and logu.unit = #{unit}
        </if>
    </select>
</mapper>