<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://www.72crm.com/mybatis-3.5.3-mapper.dtd">
<mapper namespace="com.kakarote.bi.mapper.BiEmployeeMapper">
    <select id="contractNum" resultType="com.alibaba.fastjson.JSONObject">
        <foreach collection="timeList" item="x" separator="union all" index="i">
            select ${x} as month,
            count(contract_id) as thisMonth,
            (select count(contract_id)
            from wk_crm_contract
            where DATE_FORMAT(order_date,'%Y%m') =
            <choose>
                <when test="i==0">
                    ${x-89}
                </when>
                <otherwise>
                    ${x-1}
                </otherwise>
            </choose>
            and check_status in (1,10) and owner_user_id in
            <for collection="userIds" item="item">
                #{item}
            </for>) as lastMonth,
            (select count(contract_id)
            from wk_crm_contract
            where DATE_FORMAt(order_date,'%Y%m') = ${x-100} and check_status = 1  and owner_user_id in
            <for collection="userIds" item="item">
                #{item}
            </for>) as lastYear
            from wk_crm_contract
            where DATE_FORMAT(order_date,'%Y%m') = ${x} and check_status in (1,10) and owner_user_id in
            <for collection="userIds" item="item">
                #{item}
            </for>
        </foreach>
    </select>
    <select id="contractMoney" resultType="com.alibaba.fastjson.JSONObject">
        <foreach collection="timeList" item="x" separator="union all">
            select ${x} as month,
            IFNULL(SUM(money),0) as thisMonth,
            (select IFNULL(SUM(money),0)
            from wk_crm_contract
            where DATE_FORMAT(order_date,'%Y%m') =
            <choose>
                <when test="i==0">
                    ${x-89}
                </when>
                <otherwise>
                    ${x-1}
                </otherwise>
            </choose>
            and check_status in (1,10) and owner_user_id in
            <for collection="userIds" item="item">
                #{item}
            </for>) as lastMonth,
            (select IFNULL(SUM(money),0)
            from wk_crm_contract
            where DATE_FORMAt(order_date,'%Y%m') = ${x-100} and check_status in (1,10) and owner_user_id in
            <for collection="userIds" item="item">
                #{item}
            </for>) as lastYear
            from wk_crm_contract
            where DATE_FORMAT(order_date,'%Y%m') = ${x} and check_status in (1,10) and owner_user_id in
            <for collection="userIds" item="item">
                #{item}
            </for>
        </foreach>
    </select>
    <select id="receivables" resultType="com.alibaba.fastjson.JSONObject">
        <foreach collection="timeList" item="x" separator="union all">
            select ${x} as month,
            IFNULL(SUM(money),0) as thisMonth,
            (select IFNULL(SUM(money),0)
            from wk_crm_receivables
            where DATE_FORMAT(return_time,'%Y%m') =
            <choose>
                <when test="i==0">
                    ${x-89}
                </when>
                <otherwise>
                    ${x-1}
                </otherwise>
            </choose>
            and check_status in (1,10) and owner_user_id in
            <for collection="userIds" item="item">
                #{item}
            </for>) as lastMonth,
            (select IFNULL(SUM(money),0)
            from wk_crm_receivables
            where DATE_FORMAt(return_time,'%Y%m') = ${x-100} and check_status in (1,10)  and owner_user_id in
            <for collection="userIds" item="item">
                #{item}
            </for>) as lastYear
            from wk_crm_receivables
            where DATE_FORMAT(return_time,'%Y%m') = ${x} and check_status in (1,10) and owner_user_id in
            <for collection="userIds" item="item">
                #{item}
            </for>
        </foreach>
    </select>
    <select id="totalContractTable" resultType="com.alibaba.fastjson.JSONObject">
            select type,
            count(contract_id) as contractNum,
            IFNULL(SUM(IFNULL(money,0)),0) as contractMoney,
            IFNULL(SUM(receivablesMoney),0) as receivablesMoney
            from
            (select DATE_FORMAT( a.order_date, '%Y%m' )  type,a.contract_id,a.money,
            (select SUM(money)
            from wk_crm_receivables as b
            where b.contract_id = a.contract_id and b.check_status = 1) as receivablesMoney
            from wk_crm_contract as a
            where DATE_FORMAT(a.order_date,#{sqlDateFormat} between #{beginTime} and #{finalTime})
            and a.check_status = 1 and a.owner_user_id in
            <for collection="userIds" item="item">
                #{item}
            </for>
            ) as x GROUP BY x.type
    </select>
    <select id="totalContract" resultType="com.alibaba.fastjson.JSONObject">
        select count(contract_id) as contractNum,
        IFNULL(sum(money),0) as contractMoney,
        IFNULL(sum(receivablesMoney),0) as receivablesMoney,
        (IFNULL(sum(money),0) - IFNULL(sum(receivablesMoney),0)) as unreceivedMoney
        from (select a.money,a.contract_id,
        (select SUM(IFNULL(money,0)) from wk_crm_receivables as b
        where b.contract_id = a.contract_id and b.check_status = 1) as receivablesMoney
        from wk_crm_contract as a
        where DATE_FORMAT(a.order_date,#{sqlDateFormat}) between #{beginTime} and #{finalTime} and a.check_status = 1
        <if test="userIds.size()>0">
            and a.owner_user_id in
            <for collection="userIds" item="item">
                #{item}
            </for>
        </if>
        ) as x
    </select>

    <select id="totalInvoice" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            (
            SELECT IFNULL(sum(b.invoice_money),0) FROM wk_crm_invoice as b WHERE b.check_status in (1,10) and b.real_invoice_date BETWEEN #{startTime} and #{endTime}
            and b.owner_user_id in
            <for collection="userIds" item="item">
                #{item}
            </for>
             ) as invoiceMoney,
            (
            SELECT IFNULL(sum(c.money),0) FROM wk_crm_receivables as c WHERE c.check_status in (1,10) and c.return_time BETWEEN #{startTime} and #{endTime}
            and c.owner_user_id in
            <for collection="userIds" item="item">
                #{item}
            </for>
            ) as receivablesMoney
    </select>
    <select id="invoiceStatsTable" resultType="com.alibaba.fastjson.JSONObject">
        SELECT invoice.invoiceMoney as invoiceMoney,invoice.invoiceMoney - invoice.receivablesMoney as invoiceNoReceivables,
        receivables.receivablesMoney as receivablesMoney,receivables.receivablesMoney - receivables.invoiceMoney as receivablesNoInvoice
        FROM (
        SELECT
        ifnull(sum(a.invoice_money),0)  as invoiceMoney,
        ifnull(sum(b.money),0)  as receivablesMoney
        FROM
        wk_crm_invoice as a
        LEFT JOIN wk_crm_receivables as b on a.contract_id = b.contract_id and b.check_status in (1,10)
        WHERE a.check_status in (1,10) and a.real_invoice_date BETWEEN #{startTime} and #{endTime}
        and a.owner_user_id in
        <for collection="userIds" item="item">
            #{item}
        </for>
        ) as invoice,(
        SELECT
        ifnull(sum(a.money),0)  as receivablesMoney,
        ifnull(sum(b.invoice_money),0)  as invoiceMoney
        FROM
        wk_crm_receivables as a
        LEFT JOIN wk_crm_invoice as b on a.contract_id = b.contract_id
        WHERE a.check_status in (1,10) and a.return_time BETWEEN #{startTime} and #{endTime}
        and a.owner_user_id in
        <for collection="userIds" item="item">
            #{item}
        </for>
        ) as receivables
    </select>
</mapper>
