<?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.customer.mapper.customer.CustomerMapper">
    <update id="customerAllocation">
        update customer set user_id     = #{userId},
                            update_time = now()
        where id = #{id}
    </update>
    <select id="getList" resultType="com.fjhx.customer.entity.customer.vo.CustomerVo">
        select
            c.id,
            c.customer_code,
            c.code,
            c.country_id,
            c.province_id,
            c.city_id,
            c.address,
            c.zip_code,
            c.name,
            c.status,
            c.source,
            c.user_id,
            c.create_user,
            c.create_time,
            c.update_user,
            c.update_time,
            c.tag
        from customer c
            ${ew.customSqlSegment}
    </select>

    <select id="getPage" resultType="com.fjhx.customer.entity.customer.vo.CustomerVo">
        select c.id,
               c.customer_code,
               c.code,
               c.country_id,
               c.province_id,
               c.city_id,
               c.address,
               c.zip_code,
               c.name,
               c.status,
               c.source,
               c.user_id,
               c.create_user,
               c.create_time,
               c.update_user,
               c.update_time,
               c.tag
        from customer c
            ${ew.customSqlSegment}
    </select>


    <select id="sourceStock" resultType="com.fjhx.customer.entity.customer.vo.CustomerVo">
        SELECT
            COUNT(*) count,
	        ifnull(c.source,-1) source,
            ifnull(DATE_FORMAT(c.create_time,'%Y-%m'),-1) time
        FROM
            customer c
            ${ew.customSqlSegment}
    </select>

    <select id="sourceIncrement" resultType="com.fjhx.customer.entity.customer.vo.CustomerVo">
        SELECT COUNT(*) count,
	        ifnull(c.source,-1) source,
            ifnull(DATE_FORMAT(c.create_time,'%Y-%m'),-1) time
        FROM
            customer c
            ${ew.customSqlSegment}
    </select>
    <select id="getFollowUp" resultType="com.fjhx.customer.entity.customer.vo.CustomerFollowRecordsVo">
        SELECT t1.*
        FROM ((SELECT id,
                      amount,
                      create_time `date`,
                      NULL AS     contractCode,
                      code,
                      10   AS     type,
                      NULL AS     content,
                      create_user
               FROM bytesailing_sale.sale_quotation sq
               WHERE sq.buy_corporation_id = #{id}
                 and sq.tenant_id = #{tenantId}
                 and sq.del_flag = 0)
              UNION
              (SELECT id,
                      amount,
                      create_time `date`,
                      CODE AS     contractCode,
                      null as     code,
                      20   AS     type,
                      NULL AS     content,
                      create_user
               FROM bytesailing_sale.contract c
               WHERE c.status = 30
                 and c.buy_corporation_id = #{id}
                 and c.tenant_id = #{tenantId}
                 and c.del_flag = 0)
              UNION
              (SELECT id,
                      NULL AS amount,
                      `date`,
                      NULL AS contractCode,
                      null as code,
                      30   AS type,
                      content,
                      create_user
               FROM customer_follow_records cfr
               WHERE cfr.customer_id = #{id})) t1
        ORDER BY t1.date DESC LIMIT 3
    </select>

</mapper>
