<?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.fjhx.form.mapper.EmployeeProductivityMapper">

    <select id="getList" resultType="com.fjhx.form.entity.EmployeeProductivity">
        SELECT
        su.user_id,
        su.nick_name as userName,
        ( SELECT
        count( 1 )
        FROM
        sale_quotation sq
        WHERE create_user = su.user_id
        <if test="dto.beginTime != null">
            AND create_time >= #{dto.beginTime}
        </if>
        <if test="dto.endTime != null">
            AND create_time &lt;= #{dto.endTime}
        </if>
        ) AS quotationCount,
        ( SELECT
        count( 1 )
        FROM
        customer
        WHERE create_user = su.user_id
        <if test="dto.beginTime != null">
            AND create_time >= #{dto.beginTime}
        </if>
        <if test="dto.endTime != null">
            AND create_time &lt;= #{dto.endTime}
        </if>
        ) AS newCustomerCount,
        ( SELECT
        COUNT( DISTINCT buy_corporation_id )
        FROM
        sample
        WHERE create_user = su.user_id
        <if test="dto.beginTime != null">
            AND create_time >= #{dto.beginTime}
        </if>
        <if test="dto.endTime != null">
            AND create_time &lt;= #{dto.endTime}
        </if>
        ) AS sampleCustomerCount,
        (
        SELECT
        count( DISTINCT c.buy_corporation_id )
        FROM
        contract c
        WHERE
        c.buy_corporation_id IN ( SELECT c.id FROM customer c WHERE c.user_id = su.user_id
        <if test="dto.beginTime != null">AND c.create_time >= #{dto.beginTime}</if>
        <if test="dto.endTime != null">AND c.create_time &lt;= #{dto.endTime}</if>
        )
        <if test="dto.beginTime != null">
            AND c.create_time >= #{dto.beginTime}
        </if>
        <if test="dto.endTime != null">
            AND c.create_time &lt;= #{dto.endTime}
        </if>
        AND NOT c.`status` IN ( 0, 70, 88 )
        AND c.del_flag=0
        and c.create_user = su.user_id
        ) AS newDealCustomerCount,
        (
        SELECT
        count( DISTINCT c.buy_corporation_id )
        FROM
        contract c
        WHERE
        c.create_user = su.user_id
        <if test="dto.beginTime != null">
            AND c.create_time >= #{dto.beginTime}
        </if>
        <if test="dto.endTime != null">
            AND c.create_time &lt;= #{dto.endTime}
        </if>
        AND NOT c.`status` IN ( 0, 70, 88 )
        AND c.del_flag = 0
        ) as contractCustomerCount,
        ( SELECT count( 1 ) FROM customer WHERE user_id = su.user_id AND del_flag = 0 ) AS
        customerCount,
        ( SELECT
        count( 1 )
        FROM sample
        WHERE create_user = su.user_id
        <if test="dto.beginTime != null">
            AND create_time >= #{dto.beginTime}
        </if>
        <if test="dto.endTime != null">
            AND create_time &lt;= #{dto.endTime}
        </if>
        AND del_flag = 0
        ) AS sampleCount,
        (
        SELECT
        count( 1 )
        FROM
        contract c
        WHERE
        c.buy_corporation_id IN ( SELECT c.id FROM customer c WHERE c.user_id = su.user_id
        <if test="dto.beginTime != null">AND c.create_time >= #{dto.beginTime}</if>
        <if test="dto.endTime != null">AND c.create_time &lt;= #{dto.endTime}</if>
        )
        <if test="dto.beginTime != null">
            AND c.create_time >= #{dto.beginTime}
        </if>
        <if test="dto.endTime != null">
            AND c.create_time &lt;= #{dto.endTime}
        </if>
        AND NOT c.`status` IN ( 0, 70, 88 )
        AND c.del_flag = 0
        ) AS newCustomerContractCount,
        (
        SELECT
        IFNULL( sum( c.amount * c.rate ), 0 )
        FROM
        contract c
        WHERE
        c.buy_corporation_id IN ( SELECT c.id FROM customer c WHERE c.user_id = su.user_id
        <if test="dto.beginTime != null">AND c.create_time >= #{dto.beginTime}</if>
        <if test="dto.endTime != null">AND c.create_time &lt;= #{dto.endTime}</if>
        )
        <if test="dto.beginTime != null">
            AND c.create_time >= #{dto.beginTime}
        </if>
        <if test="dto.endTime != null">
            AND c.create_time &lt;= #{dto.endTime}
        </if>
        AND NOT c.`status` IN ( 0, 70, 88 )
        AND c.del_flag = 0
        ) AS newCustomerContractCnyAmount,
        (
        SELECT
        count( 1 )
        FROM
        contract c
        WHERE
        NOT c.buy_corporation_id IN ( SELECT c.id FROM customer c WHERE c.user_id = su.user_id
        <if test="dto.beginTime != null">AND c.create_time >= #{dto.beginTime}</if>
        <if test="dto.endTime != null">AND c.create_time &lt;= #{dto.endTime}</if>
        )
        <if test="dto.beginTime != null">
            AND c.create_time >= #{dto.beginTime}
        </if>
        <if test="dto.endTime != null">
            AND c.create_time &lt;= #{dto.endTime}
        </if>
        AND NOT c.`status` IN ( 0, 70, 88 )
        AND c.del_flag = 0
        ) AS oldCustomerContractCount,
        (
        SELECT
        IFNULL( sum( c.amount * c.rate ), 0 )
        FROM
        contract c
        WHERE
        NOT c.buy_corporation_id IN ( SELECT c.id FROM customer c WHERE c.user_id = su.user_id
        <if test="dto.beginTime != null">AND c.create_time >= #{dto.beginTime}</if>
        <if test="dto.endTime != null">AND c.create_time &lt;= #{dto.endTime}</if>
        )
        <if test="dto.beginTime != null">
            AND c.create_time >= #{dto.beginTime}
        </if>
        <if test="dto.endTime != null">
            AND c.create_time &lt;= #{dto.endTime}
        </if>
        AND NOT c.`status` IN ( 0, 70, 88 )
        AND c.del_flag = 0
        ) AS oldCustomerContractCnyAmount,
        (
        SELECT
        count(1)
        FROM
        contract c
        WHERE
        <if test="dto.beginTime != null">
            c.create_time >= #{dto.beginTime}
        </if>
        <if test="dto.endTime != null">
            AND c.create_time &lt;= #{dto.endTime}
        </if>
        AND NOT c.`status` IN ( 0, 70, 88 )
        AND c.del_flag = 0
        ) AS contractCount
        FROM
        sys_user su
        JOIN sys_user_role sur ON sur.user_id = su.user_id
        JOIN sys_role sr ON sr.role_id = sur.role_id
        ${ew.customSqlSegment}
    </select>
    <select id="getUserEmailCount" resultType="com.fjhx.form.entity.EmailCount">
        SELECT count(1) count,
	t1.user_id
        FROM
            (
            SELECT
            em.id, em.send_date, em.from_email, emb.user_id, CONCAT( emb.mail_user_prefix, '@', ed.domain_name ) AS mail_user
            FROM
            enterprise_message em
            JOIN enterprise_mailbox emb ON em.mailbox_id = emb.id
            JOIN enterprise_domain ed ON emb.domain_id = ed.id UNION ALL
            SELECT
            pm.id, pm.send_date, pm.from_email, pmb.user_id, pmb.mail_user
            FROM
            personal_message pm
            JOIN personal_mailbox pmb ON pm.mailbox_id = pmb.id
            ) t1
            ${ew.customSqlSegment}
    </select>
</mapper>
