<?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.bonus.canteen.core.supply.purchase.mapper.DrpInspectGoodsMapper">
    <select id="pageInspectGoods" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpInspectGoodsPageVO">
        SELECT a.inspect_goods_id,
               a.inspect_date,
               a.inspector,
               a.relate_delivery_goods_id,
               a.delivery_supplier_id,
               b.supplier_name  deliverySupplierName,
               a.delivery_man,
               a.delivery_phone,
               a.delivery_warehouse_id,
               a.delivery_date,
               c.warehouse_name deliveryWarehouseName,
               a.delivery_total_num,
               a.total_qualified_num,
               a.inspect_qualified_num,
               a.relate_order_goods_id,
               a.create_by,
               a.create_time,
               a.update_by,
               a.update_time,
               a.if_into,
               c.area_id,
               d.area_name,
               a.signature_pic_url
        FROM drp_inspect_goods a
                 LEFT JOIN drp_supplier b ON b.supplier_id = a.delivery_supplier_id
                 LEFT JOIN drp_warehouse c ON c.warehouse_id = a.delivery_warehouse_id
                 left join alloc_area d on d.area_id = c.area_id
        <where>
            <if test="content.areaIdList != null and content.areaIdList.size() != 0">
                AND c.area_id in
                <foreach collection="content.areaIdList" item="areaId" separator="," open="(" close=")">
                    #{areaId}
                </foreach>
            </if>
<!--            &lt;!&ndash;区域权限&ndash;&gt;-->
<!--            <if test="content.areaAuth != null and content.areaAuth.size() > 0">-->
<!--                and c.area_id in-->
<!--                <foreach collection="content.areaAuth" item="areaId" separator="," open="(" close=")">-->
<!--                    #{areaId}-->
<!--                </foreach>-->
<!--            </if>-->
            <if test="content.inspectGoodsId != null and content.inspectGoodsId != ''">
                AND (
                    a.inspect_goods_id LIKE concat('%', #{content.inspectGoodsId}, '%')
                        OR a.relate_delivery_goods_id LIKE concat('%', #{content.inspectGoodsId}, '%')
                        OR a.delivery_man LIKE concat('%', #{content.inspectGoodsId}, '%')
                    )
            </if>
            <if test="content.startTime != null">
                AND a.inspect_date <![CDATA[ >= ]]> #{content.startTime}
            </if>
            <if test="content.endTime != null">
                AND a.inspect_date <![CDATA[ <= ]]> #{content.endTime}
            </if>
            <if test="content.inspector != null and content.inspector != ''">
                AND a.inspector LIKE concat('%', #{content.inspector}, '%')
            </if>
            <if test="content.relateOrderGoodsId != null and content.relateOrderGoodsId != ''">
                AND a.relate_order_goods_id LIKE concat('%', #{content.relateOrderGoodsId}, '%')
            </if>
            <if test="content.supplierId != null">
                AND a.delivery_supplier_id = #{content.supplierId}
            </if>
            <if test="content.deliveryWarehouseId != null">
                AND a.delivery_warehouse_id = #{content.deliveryWarehouseId}
            </if>
            <if test="content.createBy != null and content.createBy != ''">
                and a.create_by like concat('%', #{content.createBy}, '%')
            </if>
            <if test="content.crtimeStart != null">
                and a.create_time <![CDATA[ >= ]]> #{content.crtimeStart}
            </if>
            <if test="content.crtimeEnd != null">
                and a.create_time <![CDATA[ <= ]]> #{content.crtimeEnd}
            </if>
            <if test="content.ifInto != null">
                and a.if_into = #{content.ifInto}
            </if>
            <if test="content.deliveryStartDate != null">
                and a.delivery_date <![CDATA[ >= ]]> #{content.deliveryStartDate}
            </if>
            <if test="content.deliveryEndDate != null">
                and a.delivery_date <![CDATA[ <= ]]> #{content.deliveryEndDate}
            </if>
            <if test="content.warehouseAuthority != null and content.warehouseAuthority.size() > 0">
                and a.delivery_warehouse_id in
                <foreach collection="content.warehouseAuthority" item="warehouseId" separator="," open="(" close=")">
                    #{warehouseId}
                </foreach>
            </if>
        </where>
        ORDER BY a.create_time DESC
    </select>
<!--    <select id="sumInspectQualifiedNum" resultType="java.math.BigDecimal">-->
<!--        SELECT sum(inspect_qualified_num)-->
<!--        FROM drp_inspect_goods-->
<!--        WHERE relate_delivery_goods_id = #{deliveryGoodsId}-->
<!--    </select>-->
<!--    <select id="getDeliveryOnTimeNum" resultType="java.lang.Integer">-->
<!--        SELECT count(DISTINCT a.relate_delivery_goods_id)-->
<!--        FROM drp_inspect_goods a-->
<!--                 JOIN drp_delivery_goods b ON b.delivery_goods_id = a.relate_delivery_goods_id-->
<!--        WHERE a.`status` = #{status}-->
<!--          AND a.delivery_supplier_id = #{supplierId}-->
<!--          AND a.inspect_date <![CDATA[ >= ]]> #{startTime}-->
<!--          AND a.inspect_date <![CDATA[ <= ]]> #{endTime}-->
<!--          AND a.relate_delivery_goods_id IS NOT NULL-->
<!--        <if test="ifOnTime != null">-->
<!--            AND DATE_FORMAT(a.inspect_date, "%Y-%m-%d") <![CDATA[ <= ]]> a.delivery_date-->
<!--        </if>-->
<!--    </select>-->
<!--    <select id="queryTotal" resultType="net.xnzn.core.drp.vo.DrpInspectGoodsTotalVO">-->
<!--        SELECT (SELECT COUNT(*)-->
<!--                FROM drp_inspect_goods-->
<!--                WHERE DATE(crtime) = CURDATE()-->
<!--                  AND status = 2-->
<!--                  and inspect_date between #{startTime} and #{endTime})                                       AS 'today',-->
<!--               (SELECT COUNT(*) FROM drp_inspect_goods WHERE status = 2)                                      AS 'total',-->
<!--               (SELECT COALESCE(SUM(unit_price * total_qualified_num), '0.00')-->
<!--                FROM drp_inspect_goods_detail)                                                                AS 'amount';-->
<!--    </select>-->
<!--    <select id="queryTotal" resultType="net.xnzn.core.drp.vo.DrpInspectGoodsTotalVO" databaseId="kb">-->
<!--        SELECT (SELECT COUNT(*)-->
<!--                FROM drp_inspect_goods-->
<!--                WHERE crtime::date = CURRENT_DATE AND status = 2 and inspect_date between #{startTime}-->
<!--                  and #{endTime})                                                                             AS "today",-->
<!--               (SELECT COUNT(*) FROM drp_inspect_goods WHERE status = 2)                                      AS "total",-->
<!--               (SELECT COALESCE(SUM(unit_price * total_qualified_num), '0.00')-->
<!--                FROM drp_inspect_goods_detail)                                                                AS "amount";-->
<!--    </select>-->
</mapper>
