<?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.sale.mapper.sale.SaleQuotationMapper">
    <select id="getPage" resultType="com.fjhx.sale.entity.sale.vo.SaleQuotationVo">
        select
         *
        from sale_quotation sq
            ${ew.customSqlSegment}
    </select>

    <select id="getCount" resultType="java.lang.Integer">
        <!-- 查询报价次数（报价审批已通过的）-->
        SELECT
        COUNT(*)
        FROM
        sale_quotation
        WHERE
        buy_corporation_id = #{id} and `status` = 30 and del_flag = 0
        GROUP BY
        buy_corporation_id
    </select>

    <select id="getAmount" resultType="com.fjhx.sale.entity.sale.vo.SaleQuotationVo">
        SELECT
            SUM( amount ) amount,
            DATE_FORMAT(create_time,'%Y-%m') time
        FROM
            sale_quotation
        WHERE buy_corporation_id = #{id}
                and (DATE_FORMAT(create_time,'%Y-%m')>= #{beginTime} and #{endTime} >= DATE_FORMAT(create_time,'%Y-%m'))
        GROUP BY
            time
    </select>
    <select id="saleDetail" resultType="com.fjhx.sale.entity.sale.vo.SaleQuotationVo">
        select *
        from (
                 SELECT t1.create_time,
                        t1.create_user,
                        IFNULL(t1.advance_ratio, 0)                            advanceRatio,
                        t1.amount,
                        ifnull(ROUND((t1.amount * t1.advance_ratio / 100), 2), 0) advanceCharge,
                        t1.buy_contact_name,
                        10 as                                               type,
                        t1.buy_corporation_id,
                        t1.status,
                        t1.code,
                        t1.id
                 FROM sale_quotation t1
                 UNION
                 SELECT t1.create_time,
                        t1.create_user,
                        IFNULL(t1.advance_ratio, 0)                            advanceRatio,
                        t1.amount,
                        ifnull(ROUND((t1.amount * t1.advance_ratio / 100), 2), 0) advanceCharge,
                        t1.buy_contact_name,
                        20 as                                               type,
                        t1.buy_corporation_id,
                        t1.status,
                        t1.code,
                        t1.id
                 FROM contract t1
             ) t1
            ${ew.customSqlSegment}
    </select>

    <select id="latestFollowUp" resultType="com.fjhx.sale.entity.sale.vo.SaleQuotationVo">
        select t1.*
        from ((SELECT id,
                      amount,
                      create_time,
                      NULL AS contractCode,
                      code,
                      10   as type,
                      NULL AS remark
               FROM sale_quotation sq
               where sq.buy_corporation_id = #{id})
              UNION
              (SELECT id,
                      amount,
                      create_time,
                      code as contractCode,
                      null as code,
                      20   as type,
                      NULL AS remark
               FROM contract c
               where c.status = 30
                 and c.buy_corporation_id = #{id})
              UNION
              (SELECT id,
                      NULL AS amount, date AS create_time, NULL AS contractCode, null as code, 30 AS type, content AS remark
               FROM
                   customer_follow_records cfr
               where cfr.customer_id = #{id}
                 and cfr.tenant_id = #{tenantId}
                 and cfr.del_flag = 0)) t1 ${ew.customSqlSegment}
    </select>

    <select id="saleStatistics" resultType="com.fjhx.sale.entity.sale.vo.SaleQuotationVo">
        SELECT
            COUNT(*) count,
            <!-- 防止出现null，代码报错-->
        ifnull(c.`status`,1000) status,
        ifnull(DATE_FORMAT(sq.create_time,'%Y-%m'),-1) time
        FROM
        customer c
        left JOIN sale_quotation sq ON sq.buy_corporation_id = c.id
        ${ew.customSqlSegment}
    </select>

</mapper>
