<?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.BiMapper">
    <select id="queryProductSell" resultType="com.kakarote.bi.entity.VO.ProductStatisticsVO">
        SELECT
        b.product_id,
        c.name as productName,
        d.name as categoryName,
        count(1) as contractNum,
        SUM(IFNULL(b.num,0)) as num,
        SUM(IFNULL(b.subtotal,0)) as total
        FROM
        wk_crm_contract AS a
        JOIN wk_crm_contract_product AS b ON a.contract_id = b.contract_id
        LEFT JOIN wk_crm_product as c on b.product_id = c.product_id
        LEFT JOIN wk_crm_product_category as d on c.category_id=d.category_id
        WHERE DATE_FORMAT(a.order_date,#{sqlDateFormat}) between #{beginTime} and #{finalTime}
        and a.owner_user_id in
        <for collection="userIds" item="item">
            #{item}
        </for>
        GROUP BY b.product_id
        limit ${page},${limit}
    </select>
    <select id="queryProductSellCount" resultType="com.alibaba.fastjson.JSONObject">
        SELECT COUNT(1) as count,sum(contractNum) as contractNum,sum(num) as num, sum(total) as total FROM (
        SELECT
        product_id,
        count(1) as contractNum,
        SUM(IFNULL(b.num,0)) as num,
        SUM(IFNULL(b.subtotal,0)) as total
        FROM
        wk_crm_contract AS a
        JOIN wk_crm_contract_product AS b ON a.contract_id = b.contract_id
        WHERE DATE_FORMAT(a.order_date,#{sqlDateFormat}) between #{beginTime} and #{finalTime}
        and a.owner_user_id in
        <for collection="userIds" item="item">
            #{item}
        </for>
        GROUP BY b.product_id
        ) as a
    </select>
    <select id="taskCompleteStatistics" resultType="com.alibaba.fastjson.JSONObject">
        <foreach collection="map" item="x" index="i" separator="union all">
            select '${x.remark}' as month,
            IFNULL(a.${x.name}, 0) as achievement,
            b.receivables as money,
            IFNULL(ROUND(b.receivables / IFNULL(a.${x.name}, 0) * 100, 2), 0) as rate
            from wk_crm_achievement as a,
            <choose>
                <when test="type == 3">
                    <choose>
                        <when test="status == 1">
                            (select IFNULL(SUM(money), 0) as receivables
                            from wk_crm_contract
                            where owner_user_id = #{userId}
                            and DATE_FORMAT(order_date, '%Y%m') = CONCAT('${year}', '${x.value}')
                            and check_status in ('1','10')) as b
                        </when>
                        <when test="status == 2">
                            (select IFNULL(SUM(money), 0) as receivables
                            from wk_crm_receivables
                            where owner_user_id = #{userId}
                            and DATE_FORMAT(return_time, '%Y%m') = CONCAT('${year}', '${x.value}')
                            and check_status in ('1','10'))       as b
                        </when>
                    </choose>
                </when>
                <when test="type == 2">
                    <choose>
                        <when test="status == 1">
                            (select IFNULL(SUM(a.money), 0) as receivables
                            from wk_crm_contract as a
                            inner join wk_admin_user as b
                            where a.owner_user_id = b.user_id
                            and b.dept_id = #{deptId}
                            and DATE_FORMAT(order_date, '%Y%m') = CONCAT('${year}', '${x.value}')
                            and check_status in ('1','10')) as b
                        </when>
                        <when test="status == 2">
                            (select IFNULL(SUM(a.money), 0)  as receivables
                            from wk_crm_receivables as a
                            inner join wk_admin_user as b
                            where a.owner_user_id = b.user_id
                            and b.dept_id = #{deptId}
                            and DATE_FORMAT(return_time, '%Y%m') = CONCAT('${year}', '${x.value}')
                            and check_status in ('1','10')) as b
                        </when>
                    </choose>
                </when>
            </choose>
            where a.obj_id = #{objId}
            and a.type = #{type}
            and a.year = ${year}
            and a.status = #{status}
        </foreach>
    </select>
</mapper>
