<?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="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderMapperExCustomerDimOrder">

    <!-- TODO 要清除 错误标志 20201022 -->
    <select id="getCustomerUnAccountingDimOrderHeadSum" resultMap="cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderMapperEx.UnifiedOrderCommonHeadSumResultMap">
        SELECT
        count(a.id) as total,
        IFNULL(sum(a.amount),0) order_amount,
        IFNULL(sum(a.cost_amount),0) cost_amount,
        IFNULL(sum(a.transport_amount),0) transport_amount
        from
        unified_order a
        <where>
            and a.complet_state != 3

            <if test="orderSn != null and orderSn != '' ">
                and a.order_sn like CONCAT('%',#{orderSn},'%')
            </if>

            <if test="amount != null and amount != '' ">
                and a.amount = #{amount}
            </if>
            <if test="completState != null and completState != '' ">
                and a.complet_state = #{completState}
            </if>
            <if test="accountingStateCustomer != null and accountingStateCustomer != '' ">
                and a.accounting_state_customer = #{accountingStateCustomer}
            </if>



            <if test="virtId != null and virtId != '' ">
                and a.purchase_virt_id = #{virtId}
            </if>
            <if test="virtName != null and virtName != '' ">
                and a.purchase_virt_name like CONCAT('%',#{virtName},'%')
            </if>

            <if test="supplierId != null and supplierId != '' ">
                and a.sale_id = #{supplierId}
            </if>
            <if test="supplierName != null and supplierName != '' ">
                and a.sale_name like CONCAT('%',#{supplierName},'%')
            </if>

            <if test="purchaseUserId != null and purchaseUserId != '' ">
                and a.purchase_user_id = #{purchaseUserId}
            </if>
            <if test="purchaseUserName != null and purchaseUserName != '' ">
                and a.purchase_user_name like CONCAT('%',#{purchaseUserName},'%')
            </if>

            <if test="serviceNo != null and serviceNo != ''">
                and a.service_no in
                <foreach collection="serviceNo" index="index" item="ot" open="(" close=")" separator=",">
                    #{ot}
                </foreach>
            </if>



            <if test="startTime != null and startTime != ''">
                and DATE_FORMAT(a.create_time,'%Y-%m-%d') &gt;= #{startTime,jdbcType=VARCHAR}
            </if>
            <if test="endTime != null and endTime != ''">
                and DATE_FORMAT(a.create_time,'%Y-%m-%d') &lt;= #{endTime,jdbcType=VARCHAR}
            </if>

            <if test="outVirtIds != null and outVirtIds != ''"><!-- 已修正 数据权限 by linjinyu at 2020090818 -->
                <!-- project_id -->
                and a.purchase_virt_id in
                <foreach collection="outVirtIds" index="index" item="pid" open="(" close=")" separator=",">
                    #{pid}
                </foreach>
            </if>
            <if test="outServices != null and outServices != ''">
                and a.service_no in
                <foreach collection="outServices" index="index" item="service_no" open="(" close=")" separator=",">
                    #{service_no}
                </foreach>
            </if>
        </where>

    </select>

    <resultMap id="UnifiedOrderCustomerNotCheckResultMap" type="cn.superdesk.uniorder.dto.vo.accounting.unified.customer.notyet.UnifiedOrderCustomerNotCheckVo">
        <id column="id" property="id" jdbcType="BIGINT"/>

        <result column="order_sn" property="orderSn" jdbcType="VARCHAR"/>

        <result column="purchase_virt_name" property="purchaseVirtName" jdbcType="VARCHAR"/>
        <result column="purchase_user_name" property="purchaseUserName" jdbcType="VARCHAR"/>
        <result column="purchase_phone" property="purchasePhone" jdbcType="VARCHAR"/>
        <result column="sale_name" property="saleName" jdbcType="VARCHAR"/>
        <result column="amount" property="amount" jdbcType="DECIMAL"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="other_params" property="otherParams" jdbcType="VARCHAR"/>
        <result column="pay_type" property="payType" jdbcType="TINYINT"/>
        <result column="service_name" property="serviceName" jdbcType="VARCHAR"/>
        <result column="order_state_name" property="orderStateName" jdbcType="VARCHAR"/>

        <result column="remarks" property="remarks" jdbcType="VARCHAR"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!-- 客户结算 -> 未对账订单 -->
    <select id="getCustomerUnAccountingDimOrderList" resultMap="UnifiedOrderCustomerNotCheckResultMap">
        SELECT
            id,
            order_sn,
            service_no,
            service_name,
            purchase_virt_name,
            purchase_user_name,
            purchase_phone,
            sale_name,
            amount,
            pay_type,
            order_state_name,
            other_params,
            create_time,
            update_time,
            remarks
        FROM
            unified_order
        <where>
            and complet_state != 3
        <if test="orderSn != null and orderSn != '' ">
            and order_sn like CONCAT('%',#{orderSn},'%')
        </if>

        <if test="amount != null and amount != '' ">
            and amount = #{amount}
        </if>

        <if test="completState != null and completState != '' ">
            and complet_state = #{completState}
        </if>

        <if test="accountingStateCustomer != null and accountingStateCustomer != '' ">
            and accounting_state_customer = #{accountingStateCustomer}
        </if>

        <if test="virtId != null and virtId != '' ">
            and purchase_virt_id = #{virtId}
        </if>
        <if test="virtName != null and virtName != '' ">
            and purchase_virt_name like CONCAT('%',#{virtName},'%')
        </if>

        <if test="supplierId != null and supplierId != '' ">
            and sale_id = #{supplierId}
        </if>
        <if test="supplierName != null and supplierName != '' ">
            and sale_name like CONCAT('%',#{supplierName},'%')
        </if>

        <if test="purchaseUserId != null and purchaseUserId != '' ">
            and purchase_user_id = #{purchaseUserId}
        </if>
        <if test="purchaseUserName != null and purchaseUserName != '' ">
            and purchase_user_name like CONCAT('%',#{purchaseUserName},'%')
        </if>

        <if test="serviceNo != null and serviceNo != ''">
            and service_no in
            <foreach collection="serviceNo" index="index" item="ot" open="(" close=")" separator=",">
                #{ot}
            </foreach>
        </if>

        <if test="startTime != null and startTime != ''">
            and DATE_FORMAT(create_time,'%Y-%m-%d') &gt;= #{startTime,jdbcType=VARCHAR}
        </if>
        <if test="endTime != null and endTime != ''">
            and DATE_FORMAT(create_time,'%Y-%m-%d') &lt;= #{endTime,jdbcType=VARCHAR}
        </if>

        <if test="outVirtIds != null and outVirtIds != ''"><!-- 已修正 数据权限 by linjinyu at 2020090818 -->
            <!-- project_id -->
            and purchase_virt_id in
            <foreach collection="outVirtIds" index="index" item="pid" open="(" close=")" separator=",">
                #{pid}
            </foreach>
        </if>
        <if test="outServices != null and outServices != ''">
            and service_no in
            <foreach collection="outServices" index="index" item="serviceno" open="(" close=")" separator=",">
                #{serviceno}
            </foreach>
        </if>

        </where>
        order by create_time desc
    </select>

</mapper>