<?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.njzscloud.supervisory.money.mapper.MoneyBillMapper">

    <!-- 分页查询对账单（关联用户、站点、产品信息） -->
    <select id="selectMoneyBillWithNames" resultType="com.njzscloud.supervisory.money.pojo.entity.MoneyBillEntity">
        SELECT 
            mb.id,
            mb.station_id as stationId,
            mb.user_id as userId,
            mb.invoice_status as invoiceStatus,
            mb.bill_status as billStatus,
            mb.start_time as startTime,
            mb.end_time as endTime,
            mb.bill_period as billPeriod,
            mb.goods_id as goodsId,
            mb.order_count as orderCount,
            mb.total_weight as totalWeight,
            mb.discount_money as discountMoney,
            mb.total_money as totalMoney,
            mb.creator_id as creatorId,
            mb.modifier_id as modifierId,
            mb.create_time as createTime,
            mb.modify_time as modifyTime,
            mb.deleted,
            u.nickname,
            bc.company_name as companyName,
            mb.goods_name as goodsName,
            mb.car_count
        FROM money_bill mb
        LEFT JOIN sys_user u ON mb.user_id = u.id
        LEFT JOIN biz_company bc ON mb.station_id = bc.id AND bc.station = 1
        <where>
            <if test="entity.userId != null">
                AND mb.user_id = #{entity.userId}
            </if>
            <if test="entity.stationId != null">
                AND mb.station_id = #{entity.stationId}
            </if>
            <if test="entity.goodsId != null">
                AND mb.goods_id = #{entity.goodsId}
            </if>
            <if test="entity.invoiceStatus != null">
                AND mb.invoice_status = #{entity.invoiceStatus}
            </if>
            <if test="entity.billStatus != null">
                AND mb.bill_status = #{entity.billStatus}
            </if>
            <if test="entity.billPeriod != null">
                AND mb.bill_period = #{entity.billPeriod}
            </if>
            <if test="entity.nickname != null and entity.nickname != ''">
                AND u.nickname LIKE CONCAT('%', #{entity.nickname}, '%')
            </if>
            <if test="entity.companyName != null and entity.companyName != ''">
                AND bc.company_name LIKE CONCAT('%', #{entity.companyName}, '%')
            </if>
            <if test="entity.goodsName != null and entity.goodsName != ''">
                AND og.goods_name LIKE CONCAT('%', #{entity.goodsName}, '%')
            </if>
        </where>
        ORDER BY mb.modify_time DESC
    </select>

    <select id="selectMoneyBillList" resultType="com.njzscloud.supervisory.money.pojo.result.MoneyBillResult">
        SELECT
            oi.trans_company_id,
            bc.user_id,
            bc.company_name,
            su.nickname,
            oei.expense_item_name,
            oei.money_strategy,
            oei.origin_expense_item_id,
            oei.order_id,
            oei.discount_money,
            oei.settle_money,
            oei.quantity
        FROM
            order_expense_items oei
                LEFT JOIN order_info oi ON oi.id = oei.order_id
                LEFT JOIN biz_company bc ON oi.trans_company_id = bc.id
                LEFT JOIN sys_user su ON bc.user_id = su.id
        WHERE
            oi.order_status = 'YiWanCheng'
            <if test="entity.startTime != null">
                AND oi.create_time &gt;= #{entity.startTime}
            </if>
            <if test="entity.endTime != null">
                AND oi.create_time &lt;= #{entity.endTime}
            </if>
        order by oei.create_time desc
    </select>
</mapper>