<?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.accounting.unified.operator.AccountingUnifiedOperatorMapperEx">

    <select id="getAccountingList"
            parameterType="cn.superdesk.uniorder.dto.param.accounting.unified.operator.AccountingUnifiedOperatorQueryParam"
            resultType="cn.superdesk.uniorder.dto.vo.accounting.unified.operator.AccountingUnifiedOperatorVo">
        SELECT
            t.id,
            t.accounting_order_sn,
            t.customer_name,
            t.customer_code,
            t.accounting_status,
            t.cancel_reason,
            t.order_credit_count,
            t.create_time,
            t.update_time,
            t.submit_time,
            t.start_time,
            t.end_time,
            DATE_FORMAT(t.create_time,'%Y-%m-%d %H:%i:%s') createDateTime,
            DATE_FORMAT(t.update_time,'%Y-%m-%d %H:%i:%s') updateDateTime,
            DATE_FORMAT(t.submit_time,'%Y-%m-%d %H:%i:%s') submitDateTime,
            CONCAT(t.start_time,'至',t.end_time) accountingCycle,
            t.remark,
            t.biz_personnel_name,
            t.biz_personnel_mobile,
            t.email_status,
            t.rel_operators,
            t.rel_projects,
            t.rel_order_types,
            t.service_charge,
            t.share_profit,
            t.accounting_amount,
            t.order_amount,
            t.order_settlement_amoun,
            t.order_non_settlement_amount,
            t.order_count,
            t.order_goods_count,
            t.order_credit_count,
            t.reject_reson,
            t.invoice_title_id,
            t.settlement_plan_id,
            t3.fullname submitUserName
        <if test="id == null">
            ,t1.fullname createName
            ,t2.fullname updateName
        </if>
        FROM
            accounting_unified_operator t
            LEFT JOIN sys_user t3 ON t3.id = t.submit_user_id
            <if test="id == null">
                LEFT JOIN sys_user t1 ON t1.id = t.create_user_id
                LEFT JOIN sys_user t2 ON t2.id = t.update_user_id
            </if>
        WHERE
        t.deleted = 0
        <if test="id != null">
            AND t.id = #{id}
        </if>
        <if test="ids != null and ids != ''">
            AND t.id  in (${ids})
        </if>
        <choose>
            <when test="accountingType == 1 and (accountingStatus == null or accountingStatus == '') ">
                AND ( t.accounting_status = 1 OR t.accounting_status = 2 OR t.accounting_status = 7 )
            </when>
            <when test="accountingType == 2 and (accountingStatus == null or accountingStatus == '') ">
                AND ( t.accounting_status = 3 OR t.accounting_status = 4 OR t.accounting_status = 5 OR t.accounting_status = 6 )
            </when>
            <when test="accountingStatus != null and accountingStatus != ''">
                AND t.accounting_status in (${accountingStatus})
            </when>
        </choose>
        <if test="customerId != null and customerId != ''">
            AND t.customer_id IN (${customerId})
        </if>
        <if test="emailStatus != null">
            AND t.email_status = #{emailStatus}
        </if>
        <if test="accountingOrderSn != null and accountingOrderSn != ''">
            AND t.accounting_order_sn = #{accountingOrderSn}
        </if>
        <if test="bizPersonnelName != null and bizPersonnelName != ''">
            AND t.biz_personnel_name LIKE CONCAT('%',#{bizPersonnelName},'%')
        </if>
        <if test="createName != null and createName != ''">
            AND t1.fullname LIKE CONCAT('%',#{createName},'%')
        </if>
        <if test="beginTime != null and beginTime != ''">
            AND t.create_time >= CONCAT(#{beginTime},' 00:00:00')
        </if>
        <if test="endTime != null and endTime != ''">
            AND t.create_time  <![CDATA[  <= ]]> concat(#{endTime},' 23:59:59')
        </if>
        <if test="waterNumber != null and waterNumber != ''">

        </if>
        <if test="remark != null and remark != ''">
            AND t.remark LIKE CONCAT('%',#{remark},'%')
        </if>

        <if test="outgoingName != null and outgoingName != ''">
            AND t3.fullname LIKE CONCAT('%',#{outgoingName},'%')
        </if>
        <if test="outgoingBeginTime != null and outgoingBeginTime != ''">
            AND t.submit_time >= CONCAT(#{outgoingBeginTime},' 00:00:00')
        </if>
        <if test="outgoingEndTime != null and outgoingEndTime != ''">
            AND t.submit_time  <![CDATA[  <= ]]> concat(#{outgoingEndTime},' 23:59:59')
        </if>
        ORDER BY
            t.update_time DESC
    </select>
</mapper>