<?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.czg.service.product.mapper.ShopVendorMapper">

    <select id="summary" resultType="com.czg.product.vo.ShopVendorSummaryVO">
        SELECT vendor_id,
               sum(amount_payable)                                          AS amountPayable,
               sum(CASE
                       WHEN create_time BETWEEN DATE_FORMAT(NOW(), '%Y-%m-01') AND LAST_DAY(NOW())
                           THEN amount_payable END)                         AS mouthAmountPayable,
               sum(actual_payment_amount)                                   AS actualPaymentAmount,
               sum(CASE
                       WHEN create_time BETWEEN DATE_FORMAT(NOW(), '%Y-%m-01') AND LAST_DAY(NOW())
                           THEN actual_payment_amount END)                  AS mouthActualPaymentAmount,
               sum(amount_payable - actual_payment_amount)                  AS unPaidAmount,
               sum(CASE
                       WHEN create_time BETWEEN DATE_FORMAT(NOW(), '%Y-%m-01') AND LAST_DAY(NOW())
                           THEN amount_payable - actual_payment_amount END) AS mouthUnPaidAmount,
               remark
        FROM tb_cons_stock_flow
        WHERE shop_id = #{shopId}
    </select>
    <select id="bill" resultType="com.czg.product.vo.ShopVendorBillVO">
        select tb_cons_stock_flow.vendor_id as vendorId,
        tb_shop_vendor.`name`,
        sum(tb_cons_stock_flow.amount_payable) as amountPayable,
        sum(tb_cons_stock_flow.actual_payment_amount) as actualPaymentAmount,
        sum(tb_cons_stock_flow.amount_payable) - sum(tb_cons_stock_flow.actual_payment_amount) as unPaidAmount,
        tb_cons_stock_flow.remark
        from tb_cons_stock_flow
        left join tb_shop_vendor on tb_cons_stock_flow.vendor_id = tb_shop_vendor.id
        where tb_cons_stock_flow.shop_id = #{shopId}
        <if test="key != null and key != ''">
            and tb_shop_vendor.`name` like concat('%', #{key}, '%')
        </if>
        GROUP BY tb_cons_stock_flow.vendor_id
    </select>
</mapper>
