<?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="vip.xiaonuo.biz.modular.record.mapper.BizRecordMapper">

    <select id="sevenDaysTotalNetWeight" resultType="java.util.Map">
        WITH RECURSIVE DateRange AS (
            SELECT #{startDay} AS date
            UNION ALL
            SELECT DATE_ADD(date, INTERVAL 1 DAY)
            FROM DateRange
            WHERE date &lt; #{endDay}
        )
        SELECT
            dr.date AS day,
            '总净重' AS 'name' ,
            ROUND(COALESCE(SUM(br.NET_WEIGHT), 0) / 1000.0, 3) AS num
        FROM
            DateRange dr
        LEFT JOIN
            biz_record br ON DATE_FORMAT(br.gross_time, '%Y-%m-%d') = dr.date AND  br.DELETE_FLAG = 'NOT_DELETE'
        WHERE 1 = 1
        <if test="licensePlate != null and '' != licensePlate">
            AND br.LICENSE_PLATE LIKE CONCAT('%', #{licensePlate}, '%')
        </if>
        <if test="goodsName != null and '' != goodsName">
            AND br.GOODS_NAME LIKE CONCAT('%', #{goodsName}, '%')
        </if>
        <if test="receiptCompany != null and '' != receiptCompany">
            AND br.RECEIPT_COMPANY LIKE CONCAT('%', #{receiptCompany}, '%')
        </if>
        <if test="shippingCompany != null and '' != shippingCompany">
            AND br.SHIPPING_COMPANY LIKE CONCAT('%', #{shippingCompany}, '%')
        </if>
        <if test="transportCompany != null and '' != transportCompany">
            AND br.TRANSPORT_COMPANY LIKE CONCAT('%', #{transportCompany}, '%')
        </if>
        GROUP BY dr.date
        ORDER BY dr.date
    </select>
    <select id="sevenDaysTotalTareWeight" resultType="java.util.Map">
        WITH RECURSIVE DateRange AS (
            SELECT #{startDay} AS date
            UNION ALL
            SELECT DATE_ADD(date, INTERVAL 1 DAY)
            FROM DateRange
            WHERE date &lt; #{endDay}
        )
        SELECT
            dr.date AS day,
            '总皮重' AS 'name' ,
            ROUND(COALESCE(SUM(br.TARE_WEIGHT), 0) / 1000.0, 3) AS num
        FROM
            DateRange dr
        LEFT JOIN
            biz_record br ON DATE_FORMAT(br.gross_time, '%Y-%m-%d') = dr.date AND  br.DELETE_FLAG = 'NOT_DELETE'
        WHERE 1 = 1
        <if test="goodsName != null and '' != goodsName">
            AND br.GOODS_NAME LIKE CONCAT('%', #{goodsName}, '%')
        </if>
        <if test="licensePlate != null and '' != licensePlate">
            AND br.LICENSE_PLATE LIKE CONCAT('%', #{licensePlate}, '%')
        </if>
        <if test="receiptCompany != null and '' != receiptCompany">
            AND br.RECEIPT_COMPANY LIKE CONCAT('%', #{receiptCompany}, '%')
        </if>
        <if test="shippingCompany != null and '' != shippingCompany">
            AND br.SHIPPING_COMPANY LIKE CONCAT('%', #{shippingCompany}, '%')
        </if>
        <if test="transportCompany != null and '' != transportCompany">
            AND br.TRANSPORT_COMPANY LIKE CONCAT('%', #{transportCompany}, '%')
        </if>
        GROUP BY dr.date
        ORDER BY dr.date
    </select>
    <select id="sevenDaysTotalGrossWeight" resultType="java.util.Map">
        WITH RECURSIVE DateRange AS (
            SELECT #{startDay} AS date
            UNION ALL
            SELECT DATE_ADD(date, INTERVAL 1 DAY)
            FROM DateRange
            WHERE date &lt; #{endDay}
        )
        SELECT
            dr.date AS day,
            '总毛重' AS 'name' ,
            ROUND(COALESCE(SUM(br.GROSS_WEIGHT), 0) / 1000.0, 3) AS num
        FROM
            DateRange dr
        LEFT JOIN
            biz_record br ON DATE_FORMAT(br.gross_time, '%Y-%m-%d') = dr.date AND  br.DELETE_FLAG = 'NOT_DELETE'
        WHERE 1 = 1
        <if test="goodsName != null and '' != goodsName">
            AND br.GOODS_NAME LIKE CONCAT('%', #{goodsName}, '%')
        </if>
        <if test="licensePlate != null and '' != licensePlate">
            AND br.LICENSE_PLATE LIKE CONCAT('%', #{licensePlate}, '%')
        </if>
        <if test="receiptCompany != null and '' != receiptCompany">
            AND br.RECEIPT_COMPANY LIKE CONCAT('%', #{receiptCompany}, '%')
        </if>
        <if test="shippingCompany != null and '' != shippingCompany">
            AND br.SHIPPING_COMPANY LIKE CONCAT('%', #{shippingCompany}, '%')
        </if>
        <if test="transportCompany != null and '' != transportCompany">
            AND br.TRANSPORT_COMPANY LIKE CONCAT('%', #{transportCompany}, '%')
        </if>
        GROUP BY dr.date
        ORDER BY dr.date
    </select>
    <select id="sevenDaysTotalCar" resultType="java.util.Map">
        WITH RECURSIVE DateRange AS (
            SELECT #{startDay} AS date
            UNION ALL
            SELECT DATE_ADD(date, INTERVAL 1 DAY)
            FROM DateRange
            WHERE date &lt; #{endDay}
        )
        SELECT
            dr.date AS day,
            '车次' AS 'name' ,
            COUNT(br.CAR_NUMBER) AS num
        FROM
            DateRange dr
        LEFT JOIN
            biz_record br ON DATE_FORMAT(br.gross_time, '%Y-%m-%d') = dr.date AND  br.DELETE_FLAG = 'NOT_DELETE'
        WHERE 1 = 1
        <if test="goodsName != null and '' != goodsName">
            AND br.GOODS_NAME LIKE CONCAT('%', #{goodsName}, '%')
        </if>
        <if test="licensePlate != null and '' != licensePlate">
            AND br.LICENSE_PLATE LIKE CONCAT('%', #{licensePlate}, '%')
        </if>
        <if test="receiptCompany != null and '' != receiptCompany">
            AND br.RECEIPT_COMPANY LIKE CONCAT('%', #{receiptCompany}, '%')
        </if>
        <if test="shippingCompany != null and '' != shippingCompany">
            AND br.SHIPPING_COMPANY LIKE CONCAT('%', #{shippingCompany}, '%')
        </if>
        <if test="transportCompany != null and '' != transportCompany">
            AND br.TRANSPORT_COMPANY LIKE CONCAT('%', #{transportCompany}, '%')
        </if>
        GROUP BY dr.date
        ORDER BY dr.date
    </select>
    <select id="sevenDaysCountWeight" resultType="java.util.Map">
        SELECT
            COALESCE(ROUND(COALESCE(SUM(TARE_WEIGHT), 0) / 1000.0, 3), 0) AS totalTareWeight,
            COALESCE(ROUND(COALESCE(SUM(GROSS_WEIGHT), 0) / 1000.0, 3), 0) AS totalGrossWeight,
            COALESCE(ROUND(COALESCE(SUM(NET_WEIGHT), 0) / 1000.0, 3), 0) AS totalNetWeight,
            COUNT(CAR_NUMBER) AS totalCar
        FROM
            biz_record
        WHERE DELETE_FLAG = 'NOT_DELETE'
        <if test="licensePlate != null and '' != licensePlate">
            AND LICENSE_PLATE LIKE CONCAT('%', #{licensePlate}, '%')
        </if>
        <if test="startDay != null and '' != startDay">
            AND DATE_FORMAT(gross_time, '%Y-%m-%d') &gt;= #{startDay}
        </if>
        <if test="endDay != null and '' != endDay">
            AND DATE_FORMAT(gross_time, '%Y-%m-%d') &lt;= #{endDay}
        </if>
        <if test="goodsName != null and '' != goodsName">
            AND GOODS_NAME LIKE CONCAT('%', #{goodsName}, '%')
        </if>
        <if test="receiptCompany != null and '' != receiptCompany">
            AND RECEIPT_COMPANY LIKE CONCAT('%', #{receiptCompany}, '%')
        </if>
        <if test="shippingCompany != null and '' != shippingCompany">
            AND SHIPPING_COMPANY LIKE CONCAT('%', #{shippingCompany}, '%')
        </if>
        <if test="transportCompany != null and '' != transportCompany">
            AND TRANSPORT_COMPANY LIKE CONCAT('%', #{transportCompany}, '%')
        </if>
    </select>
    <select id="receiptCompanyCountList" resultType="vip.xiaonuo.biz.modular.record.result.CompanyResult">
        SELECT
            RECEIPT_COMPANY AS companyName,
            COUNT(CAR_NUMBER) AS totalCarNum,
            COALESCE(ROUND(COALESCE(SUM(TARE_WEIGHT), 0) / 1000.0, 3), 0) AS totalTareWeight,
            COALESCE(ROUND(COALESCE(SUM(GROSS_WEIGHT), 0) / 1000.0, 3), 0) AS totalGrossWeight,
            COALESCE(ROUND(COALESCE(SUM(NET_WEIGHT), 0) / 1000.0, 3), 0) AS totalNetWeight
        FROM
            biz_record
        WHERE DELETE_FLAG = 'NOT_DELETE'
        <include refid="queryparam"/>
        <if test="companyName != null and '' != companyName">
            AND RECEIPT_COMPANY LIKE CONCAT('%', #{companyName}, '%')
        </if>
        GROUP BY RECEIPT_COMPANY
    </select>
    <sql id="queryparam">
        <if test="licensePlate != null and '' != licensePlate">
            AND LICENSE_PLATE LIKE CONCAT('%', #{licensePlate}, '%')
        </if>
        <if test="startDay != null and '' != startDay">
            AND DATE_FORMAT(gross_time, '%Y-%m-%d') &gt;= #{startDay}
        </if>
        <if test="endDay != null and '' != endDay">
            AND DATE_FORMAT(gross_time, '%Y-%m-%d') &lt;= #{endDay}
        </if>
        <if test="goodsName != null and '' != goodsName">
            AND GOODS_NAME LIKE CONCAT('%', #{goodsName}, '%')
        </if>

    </sql>
    <select id="shippingCompanyCountList" resultType="vip.xiaonuo.biz.modular.record.result.CompanyResult">
        SELECT
            SHIPPING_COMPANY AS companyName,
            COUNT(CAR_NUMBER) AS totalCarNum,
            COALESCE(ROUND(COALESCE(SUM(TARE_WEIGHT), 0) / 1000.0, 3), 0) AS totalTareWeight,
            COALESCE(ROUND(COALESCE(SUM(GROSS_WEIGHT), 0) / 1000.0, 3), 0) AS totalGrossWeight,
            COALESCE(ROUND(COALESCE(SUM(NET_WEIGHT), 0) / 1000.0, 3), 0) AS totalNetWeight
        FROM
            biz_record
        WHERE DELETE_FLAG = 'NOT_DELETE'
        <include refid="queryparam"/>
        <if test="companyName != null and '' != companyName">
            AND SHIPPING_COMPANY LIKE CONCAT('%', #{companyName}, '%')
        </if>
        GROUP BY SHIPPING_COMPANY
    </select>
    <select id="transportCompanyCountList" resultType="vip.xiaonuo.biz.modular.record.result.CompanyResult">
        SELECT
            TRANSPORT_COMPANY AS companyName,
            COUNT(CAR_NUMBER) AS totalCarNum,
            COALESCE(ROUND(COALESCE(SUM(TARE_WEIGHT), 0) / 1000.0, 3), 0) AS totalTareWeight,
            COALESCE(ROUND(COALESCE(SUM(GROSS_WEIGHT), 0) / 1000.0, 3), 0) AS totalGrossWeight,
            COALESCE(ROUND(COALESCE(SUM(NET_WEIGHT), 0) / 1000.0, 3), 0) AS totalNetWeight
        FROM
            biz_record
        WHERE DELETE_FLAG = 'NOT_DELETE'
        <include refid="queryparam"/>
        <if test="companyName != null and '' != companyName">
            AND TRANSPORT_COMPANY LIKE CONCAT('%', #{companyName}, '%')
        </if>
        GROUP BY TRANSPORT_COMPANY
    </select>
    <select id="goodsCountList" resultType="vip.xiaonuo.biz.modular.record.result.GoodsResult">
        SELECT
            GOODS_NAME AS goodsName,
            GOODS_MODEL AS goodsModel,
            COUNT(CAR_NUMBER) AS totalCarNum,
            COALESCE(ROUND(COALESCE(SUM(TARE_WEIGHT), 0) / 1000.0, 3), 0) AS totalTareWeight,
            COALESCE(ROUND(COALESCE(SUM(GROSS_WEIGHT), 0) / 1000.0, 3), 0) AS totalGrossWeight,
            COALESCE(ROUND(COALESCE(SUM(NET_WEIGHT), 0) / 1000.0, 3), 0) AS totalNetWeight
        FROM
            biz_record
        WHERE DELETE_FLAG = 'NOT_DELETE'
        <include refid="queryparam"/>
        GROUP BY GOODS_NAME, GOODS_MODEL
    </select>
    <select id="carCountList" resultType="vip.xiaonuo.biz.modular.record.result.CarResult">
        SELECT
            LICENSE_PLATE AS lecensePlate,
            PLATE_COLOR AS plateColor,
            COUNT(CAR_NUMBER) AS totalCarNum,
            COALESCE(ROUND(COALESCE(SUM(TARE_WEIGHT), 0) / 1000.0, 3), 0) AS totalTareWeight,
            COALESCE(ROUND(COALESCE(SUM(GROSS_WEIGHT), 0) / 1000.0, 3), 0) AS totalGrossWeight,
            COALESCE(ROUND(COALESCE(SUM(NET_WEIGHT), 0) / 1000.0, 3), 0) AS totalNetWeight
        FROM
            biz_record
        WHERE DELETE_FLAG = 'NOT_DELETE'
        <include refid="queryparam"/>
        GROUP BY LICENSE_PLATE, PLATE_COLOR
    </select>

    <select id="getPage" resultType="vip.xiaonuo.biz.modular.record.entity.BizRecord">
        select
            br.ID,
            br.RELATION_ID,
            br.LICENSE_PLATE,
            br.PLATE_COLOR,
            br.CAR_NUMBER,
            br.GROSS_TIME,
            br.TARE_TIME,
            br.TARE_TYPE,
            br.GROSS_WEIGHT/1000 GROSS_WEIGHT,
            br.TARE_WEIGHT/1000 TARE_WEIGHT,
            br.NET_WEIGHT/1000 NET_WEIGHT,
            br.KF_WEIGHT/1000 KF_WEIGHT,
            br.DRIVER_NAME,
            br.DRIVER_MOBILE,
            br.GOODS_NAME,
            br.GOODS_MODEL,
            br.RECEIPT_COMPANY,
            br.SHIPPING_COMPANY,
            br.TRANSPORT_COMPANY,
            br.TRANSPORT_ROUTE,
            br.GROSS_PLATE_NAME,
            br.GROSS_LICENSE_NAME,
            br.GROSS_CAPTURE_HEAD,
            br.GROSS_CAPTURE_TAIL,
            br.GROSS_CAPTURE_WARE,
            br.GROSS_CAPTURE_BODY,
            br.GROSS_CAPTURE_POUND_ROOM,
            br.TARE_PLATE_NAME,
            br.TARE_LICENSE_NAME,
            br.TARE_CAPTURE_HEAD,
            br.TARE_CAPTURE_TAIL,
            br.TARE_CAPTURE_WARE,
            br.TARE_CAPTURE_BODY,
            br.TARE_CAPTURE_POUND_ROOM,
            br.EXT_KEY1,
            br.EXT_KEY2,
            br.DELETE_FLAG,
            br.CREATE_TIME,
            br.CREATE_USER,
            br.UPDATE_TIME,
            br.UPDATE_USER,
            br.weighing_type,
            br.appointment_id,
            br.order_id,
            br.confirm_user,
            br.appointment_user,
            br.driver_sign,
            br.unload_weight/1000 unload_weight,
            br.unload_img,
            br.unload_name,
            br.audit_sign,
            bo.order_name,
            bo.order_number,
            bo.order_type,
            bc.name customerName,
            bar.status
        from biz_record br
         left join biz_appointment_record bar on bar.id = br.appointment_id
         left join biz_order bo on bo.id = br.order_id
         left join biz_customer bc on bo.customer_id = bc.id
        ${ew.customSqlSegment}
    </select>
</mapper>
