<?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="org.dromara.work.mapper.TpWechatMapper">

    <select id="selectVoPageList" parameterType="org.dromara.work.domain.bo.TpWechatBo" resultType="org.dromara.work.domain.vo.TpWechatVo">
        SELECT
            w.id,
            w.`code`,
            w.nikename,
            w.uid,
            w.`user`,
            w.phone,
            DATEDIFF( CURDATE(), w.user_time ) AS userDay,
            w.`name`,
            w.card_id,
            w.bank,
            w.number,
            w.num,
            w.allnum,
            w.add_user,
            w.addname,
            w.add_time,
            w.now_price,
            DATEDIFF( CURDATE(), w.now_time ) AS nowDay,
            w.deal_num,
            w.total_sum,
            w.deal_order_num,
            w.deal_ratio,
            w.month_amount,
            (
                SELECT
                    sum(o.price)
                FROM
                    tp_order AS o
                WHERE
                    o.wid = w.id and o.sid = w.uid
            ) AS nowPrice,
            (
                SELECT
                sum(o.price)
                FROM
                tp_order AS o
                WHERE
                o.wid = w.id
            ) AS totalSum,
            (
                SELECT
                count(1)
                FROM
                tp_order AS o
                WHERE
                o.wid = w.id
            ) AS dealOrderNum,
            (
                SELECT
                COUNT(DISTINCT o.kid)
                FROM
                tp_order AS o
                WHERE
                o.wid = w.id
            ) AS dealNum,
            (
                SELECT
                concat(round(COUNT(DISTINCT o.kid) / w.allnum * 100),'%')
                FROM
                tp_order AS o
                WHERE
                o.wid = w.id
            ) AS dealRatio,
            (
                WITH MonthlySales AS (
                SELECT
                DATE_FORMAT(add_time, '%Y-%m') AS month,
                SUM(price) AS monthly_amount
                FROM
                tp_order where wid = w.id
                GROUP BY
                DATE_FORMAT(add_time, '%Y-%m')
                )
                SELECT
                AVG(monthly_amount) AS monthAmount
                FROM
                MonthlySales
            ) AS monthAmount,
            (
                SELECT
                    sum( n.num )
                FROM
                    tp_wechat_num AS n
                WHERE
                    n.wid = w.id
                  AND n.add_time >= CONCAT( YEAR ( CURDATE()), '-01-01' )
            ) AS yearNum,
            (
                SELECT
                    sum( n.num )
                FROM
                    tp_wechat_num AS n
                WHERE
                    n.wid = w.id
                  AND n.add_time >= CONCAT( YEAR ( CURRENT_DATE ()), '-', MONTH ( CURRENT_DATE ()), '-01' )
            ) AS monthNum,
            (
                SELECT
                    sum( n.num )
                FROM
                    tp_wechat_num AS n
                WHERE
                    n.wid = w.id
                  AND n.add_time = ( CURDATE() - INTERVAL 1 DAY )
            ) AS dayNum
        FROM
            tp_wechat AS w
        WHERE
            w.is_del = 1
        <if test="bo.code != null  and bo.code != ''"> and w.code like concat('%', #{bo.code}, '%')</if>
        <if test="bo.nikename != null  and bo.nikename != ''"> and w.nikename like concat('%', #{bo.nikename}, '%')</if>
        <if test="bo.user != null  and bo.user != ''"> and w.user like concat('%', #{bo.user}, '%')</if>
        <if test="bo.phone != null  and bo.phone != ''"> and w.phone = #{bo.phone}</if>
        <if test="bo.uid != null and bo.uid != ''"> and w.uid = #{bo.uid}</if>
        <if test="bo.name != null  and bo.name != ''"> and w.name like concat('%', #{bo.name}, '%')</if>
        <if test="bo.addname != null  and bo.addname != ''"> and w.addname like concat('%', #{bo.addname}, '%')</if>
        <if test="bo.cardId != null  and bo.cardId != ''"> and w.card_id like concat('%', #{bo.cardId}, '%')</if>
        <if test="bo.number != null  and bo.number != ''"> and w.number like concat('%', #{bo.number}, '%')</if>
        <if test="bo.beginUserTime != null  and bo.beginUserTime != ''"> and w.user_time between #{bo.beginUserTime} and #{bo.endUserTime}</if>
        <if test="bo.deptIds != null and bo.deptIds.size() > 0"> and w.uid in (SELECT user_id from sys_user where dept_id in
            <foreach collection="bo.deptIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        )</if>
    </select>


    <select id="selectVoList" parameterType="org.dromara.work.domain.bo.TpWechatBo" resultType="org.dromara.work.domain.vo.TpWechatVo">
        SELECT
        w.id,
        w.`code`,
        w.nikename,
        w.uid,
        w.`user`,
        w.phone,
        DATEDIFF( CURDATE(), w.user_time ) AS userDay,
        w.`name`,
        w.card_id,
        w.bank,
        w.number,
        w.num,
        w.allnum,
        w.add_user,
        w.addname,
        w.add_time,
        w.now_price,
        DATEDIFF( CURDATE(), w.now_time ) AS nowDay,
        w.deal_num,
        w.total_sum,
        w.deal_order_num,
        w.deal_ratio,
        w.month_amount,
        (
        SELECT
        sum(o.price)
        FROM
        tp_order AS o
        WHERE
        o.wid = w.id and o.sid = w.uid
        ) AS nowPrice,
        (
        SELECT
        sum(o.price)
        FROM
        tp_order AS o
        WHERE
        o.wid = w.id
        ) AS totalSum,
        (
        SELECT
        count(1)
        FROM
        tp_order AS o
        WHERE
        o.wid = w.id
        ) AS dealOrderNum,
        (
        SELECT
        COUNT(DISTINCT o.kid)
        FROM
        tp_order AS o
        WHERE
        o.wid = w.id
        ) AS dealNum,
        (
        SELECT
        concat(round(COUNT(DISTINCT o.kid) / w.allnum * 100),'%')
        FROM
        tp_order AS o
        WHERE
        o.wid = w.id
        ) AS dealRatio,
        (
        WITH MonthlySales AS (
        SELECT
        DATE_FORMAT(add_time, '%Y-%m') AS month,
        SUM(price) AS monthly_amount
        FROM
        tp_order where wid = w.id
        GROUP BY
        DATE_FORMAT(add_time, '%Y-%m')
        )
        SELECT
        AVG(monthly_amount) AS monthAmount
        FROM
        MonthlySales
        ) AS monthAmount,
        (
        SELECT
        sum( n.num )
        FROM
        tp_wechat_num AS n
        WHERE
        n.wid = w.id
        AND n.add_time >= CONCAT( YEAR ( CURDATE()), '-01-01' )
        ) AS yearNum,
        (
        SELECT
        sum( n.num )
        FROM
        tp_wechat_num AS n
        WHERE
        n.wid = w.id
        AND n.add_time >= CONCAT( YEAR ( CURRENT_DATE ()), '-', MONTH ( CURRENT_DATE ()), '-01' )
        ) AS monthNum,
        (
        SELECT
        sum( n.num )
        FROM
        tp_wechat_num AS n
        WHERE
        n.wid = w.id
        AND n.add_time = ( CURDATE() - INTERVAL 1 DAY )
        ) AS dayNum
        FROM
        tp_wechat AS w
        WHERE
        w.is_del = 1
        <if test="bo.code != null  and bo.code != ''"> and w.code like concat('%', #{bo.code}, '%')</if>
        <if test="bo.nikename != null  and bo.nikename != ''"> and w.nikename like concat('%', #{bo.nikename}, '%')</if>
        <if test="bo.user != null  and bo.user != ''"> and w.user like concat('%', #{bo.user}, '%')</if>
        <if test="bo.phone != null  and bo.phone != ''"> and w.phone = #{bo.phone}</if>
        <if test="bo.uid != null and bo.uid != ''"> and w.uid = #{bo.uid}</if>
        <if test="bo.name != null  and bo.name != ''"> and w.name like concat('%', #{bo.name}, '%')</if>
        <if test="bo.addname != null  and bo.addname != ''"> and w.addname like concat('%', #{bo.addname}, '%')</if>
        <if test="bo.cardId != null  and bo.cardId != ''"> and w.card_id like concat('%', #{bo.cardId}, '%')</if>
        <if test="bo.number != null  and bo.number != ''"> and w.number like concat('%', #{bo.number}, '%')</if>
        <if test="bo.beginUserTime != null  and bo.beginUserTime != ''"> and w.user_time between #{bo.beginUserTime} and #{bo.endUserTime}</if>
        <if test="bo.deptIds != null and bo.deptIds.size() > 0"> and w.uid in (SELECT user_id from sys_user where dept_id in
            <foreach collection="bo.deptIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
            )</if>
    </select>

    <select id="selectVoListExport" parameterType="org.dromara.work.domain.bo.TpWechatBo" resultType="org.dromara.work.domain.vo.TpWechatVo">
        SELECT
        w.id,
        w.`code`,
        w.nikename,
        w.uid,
        w.`user`,
        w.phone,
        DATEDIFF( CURDATE(), w.user_time ) AS userDay,
        w.`name`,
        w.card_id,
        w.bank,
        w.number,
        w.num,
        w.allnum,
        w.add_user,
        w.addname,
        w.add_time,
        w.now_price,
        DATEDIFF( CURDATE(), w.now_time ) AS nowDay,
        w.deal_num,
        w.total_sum,
        w.deal_order_num,
        w.deal_ratio,
        w.month_amount,
        w.is_del,
        w.user_time,
        w.now_time
        FROM
        tp_wechat AS w
        WHERE
        w.is_del = 1
        <if test="bo.code != null  and bo.code != ''"> and w.code like concat('%', #{bo.code}, '%')</if>
        <if test="bo.nikename != null  and bo.nikename != ''"> and w.nikename like concat('%', #{bo.nikename}, '%')</if>
        <if test="bo.user != null  and bo.user != ''"> and w.user like concat('%', #{bo.user}, '%')</if>
        <if test="bo.phone != null  and bo.phone != ''"> and w.phone = #{bo.phone}</if>
        <if test="bo.uid != null and bo.uid != ''"> and w.uid = #{bo.uid}</if>
        <if test="bo.name != null  and bo.name != ''"> and w.name like concat('%', #{bo.name}, '%')</if>
        <if test="bo.addname != null  and bo.addname != ''"> and w.addname like concat('%', #{bo.addname}, '%')</if>
        <if test="bo.cardId != null  and bo.cardId != ''"> and w.card_id like concat('%', #{bo.cardId}, '%')</if>
        <if test="bo.number != null  and bo.number != ''"> and w.number like concat('%', #{bo.number}, '%')</if>
        <if test="bo.beginUserTime != null  and bo.beginUserTime != ''"> and w.user_time between #{bo.beginUserTime} and #{bo.endUserTime}</if>
        <if test="bo.deptIds != null and bo.deptIds.size() > 0"> and w.uid in (SELECT user_id from sys_user where dept_id in
            <foreach collection="bo.deptIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
            )</if>
    </select>

    <select id="wxDayList" resultType="org.dromara.work.domain.vo.WxNumDayListVo">
        SELECT
        o.wid,
        DATE_FORMAT(o.add_time, '%Y-%m-%d' ) AS date,
        SUM(o.num) AS num
        FROM
        tp_wechat_num as o
        WHERE DATE_FORMAT(o.add_time, '%Y-%m') = #{bo.month}
        <if test="wxIds != null and wxIds.size() > 0"> and o.wid in
            <foreach collection="wxIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY
        DATE_FORMAT(o.add_time, '%Y-%m-%d'),o.wid
        ORDER BY o.wid,date
    </select>

    <select id="wxMonthList" resultType="org.dromara.work.domain.vo.WxNumDayListVo">
        SELECT
        o.wid,
        DATE_FORMAT(o.add_time, '%Y-%m' ) AS date,
        SUM(o.num) AS num
        FROM
        tp_wechat_num as o
        WHERE DATE_FORMAT(o.add_time, '%Y') = #{bo.year}
        <if test="wxIds != null and wxIds.size() > 0"> and o.wid in
            <foreach collection="wxIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY
        DATE_FORMAT(o.add_time, '%Y-%m'),o.wid
        ORDER BY o.wid,date
    </select>

</mapper>
