package com.fs.crm.mapper;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fs.crm.domain.CrmCustomer;
import com.fs.crm.param.*;
import com.fs.crm.vo.*;
import com.fs.qwApi.param.QwCustomerDetailParam;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * 客户Mapper接口

 *
 * @author fs
 * @date 2022-12-21
 */
public interface CrmCustomerMapper extends BaseMapper<CrmCustomer> {
    /**
     * 查询客户
     *
     * @param customerId 客户ID
     * @return 客户
     */
    public CrmCustomer selectCrmCustomerById(Long customerId);

    /**
     * 查询公司内重复客户
     *
     *
     * @return 客户
     */
    public CrmCustomer selectCrmCustomerByMobile(CrmCustomer customer);

    /**
     * 查询客户列表
     * @param crmCustomer 客户
     * @return 客户集合
     */
    public List<CrmCustomer> selectCrmCustomerList(CrmCustomer crmCustomer);

    /**
     *  获取所有CRM里面关联了企业微信，然后未加为好友的客户列表
     */
    @Select("<script> " +
            "SELECT " +
            "cc.*, " +
            "qec.user_id AS from_user_id, " +
            "qec.name as qw_name, " +
            "qec.avatar as qw_avatar, " +
            "qec.type as qw_type, " +
            "qec.gender as qw_gender, " +
            "qec.remark as qw_remark, " +
            "qec.description as qw_description, " +
            "qec.remark_mobiles as qw_remark_mobiles, " +
            "qec.remark_corp_name as qw_remark_corp_name, " +
            "qec.transfer_status as qw_transfer_status, " +
            "qec.status as qw_status " +
            "FROM " +
            "crm_customer cc " +
            "INNER JOIN qw_external_contact qec ON cc.customer_id = qec.customer_id " +
            "where cc.is_add_wechat is NULL or cc.is_add_wechat=0 " +
            "</script>")
    public List<CrmCustomerListQueryFriendVO> getFriendAssistantListVO(CrmCustomerQueryFriendParam param);

    /**
     * 新增客户
     *
     * @param crmCustomer 客户
     * @return 结果
     */
    public int insertCrmCustomer(CrmCustomer crmCustomer);

    /**
     * 修改客户
     *
     * @param crmCustomer 客户
     * @return 结果
     */
    public int updateCrmCustomer(CrmCustomer crmCustomer);


    /**
     * 删除客户
     *
     * @param customerId 客户ID
     * @return 结果
     */
    public int deleteCrmCustomerById(Long customerId);

    /**
     * 批量删除客户
     *
     * @param customerIds 需要删除的数据ID
     * @return 结果
     */
    public int deleteCrmCustomerByIds(Long[] customerIds);
    @Select({"<script> " +
            "select c.*,d.dept_name from crm_customer c left join company_dept d on c.dept_id = d.dept_id " +
            "where c.is_line=0 and is_del=0 and c.is_pool=1 " +
            "<if test = 'maps.companyId != null     '> " +
            "and c.company_id =#{maps.companyId} " +
            "</if>" +
            "<if test = 'maps.address != null and  maps.address !=\"\"    '> " +
            "and c.address like CONCAT('%',#{maps.address},'%') " +
            "</if>" +
            "<if test = 'maps.customerCode != null and  maps.customerCode !=\"\"    '> " +
            "and c.customer_code like CONCAT(#{maps.customerCode},'%') " +
            "</if>" +
            "<if test = 'maps.customerName != null and  maps.customerName !=\"\"    '> " +
            "and c.customer_name like CONCAT('%',#{maps.customerName},'%') " +
            "</if>" +
            "<if test = 'maps.mobile != null and  maps.mobile !=\"\"    '> " +
            "and c.mobile like CONCAT('%',#{maps.mobile},'%') " +
            "</if>" +
            "<if test = 'maps.status != null and maps.status !=\"\"     '> " +
            "and c.status =#{maps.status} " +
            "</if>" +
            "<if test = 'maps.visitStatus != null and maps.visitStatus !=\"\"     '> " +
            "and c.visit_status IN " +
            "<foreach collection=\"maps.visitStatus.split(',')\"  item='item' index='index'  open='(' separator=',' close=')'> #{item} </foreach>"+
            "</if>" +
            "<if test = 'maps.customerType != null      '> " +
            "and c.customer_type IN " +
            "<foreach collection=\"maps.customerType.split(',')\"  item='item' index='index'  open='(' separator=',' close=')'> #{item} </foreach>"+
            "</if>" +
            "<if test = 'maps.companyId != null      '> " +
            "and c.company_id =#{maps.companyId} " +
            "</if>" +
            "<if test = 'maps.isReceive != null      '> " +
            "and c.is_receive =#{maps.isReceive} " +
            "</if>" +
            "<if test = 'maps.source != null and maps.source !=\"\"      '> " +
            "and c.source IN " +
            "<foreach collection=\"maps.source.split(',')\"  item='item' index='index'  open='(' separator=',' close=')'> #{item} </foreach>"+
            "</if>" +
            "<if test = 'maps.tags != null      '> " +
            "and  " +
            "<foreach collection=\"maps.tags.split(',')\" item=\"tag\"   open=\"(\" close=\")\" separator=\"OR\">" +
            "find_in_set(#{tag},c.tags)" +
            "</foreach> " +
            "</if>" +
            "<if test = 'maps.beginTime != null and maps.beginTime != \"\" '> " +
            "and date_format(c.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "and date_format(c.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.deptId != null  and maps.deptId != 0 '> " +
            "AND (c.dept_id = #{maps.deptId} OR c.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{maps.deptId}, ancestors) )) " +
            "</if>" +
            "${maps.params.dataScope}"+
            " order by c.customer_id desc "+
            "</script>"})
    List<CrmFullCustomerListQueryVO> selectCrmFullCustomerListQuery(@Param("maps") CrmFullCustomerListQueryParam param);
    @Select({"<script> " +
            "select c.* from crm_customer c where c.customer_id=#{customerId} " +
            "</script>"})
    CrmCustomerQueryVO selectCrmFullCustomerQueryByCustomerId(Long customerId);

    @Select({"<script> " +
            "select c.*,com.company_name,cu.nick_name as company_user_nick_name from crm_customer c left join company com on com.company_id=c.company_id left join company_user cu on cu.user_id=c.receive_user_id " +
            "where 1=1 " +
            "<if test = 'maps.companyId != null     '> " +
            "and c.company_id =#{maps.companyId} " +
            "</if>" +
            "<if test = 'maps.customerCode != null and  maps.customerCode !=\"\"    '> " +
            "and c.customer_code like CONCAT('%',#{maps.customerCode},'%') " +
            "</if>" +
            "<if test = 'maps.customerName != null and  maps.customerName !=\"\"    '> " +
            "and c.customer_name like CONCAT('%',#{maps.customerName},'%') " +
            "</if>" +
            "<if test = 'maps.mobile != null and  maps.mobile !=\"\"    '> " +
            "and c.mobile like CONCAT('%',#{maps.mobile},'%') " +
            "</if>" +
            "<if test = 'maps.trafficSource != null and  maps.trafficSource !=\"\"    '> " +
            "and c.traffic_source like CONCAT('%',#{maps.trafficSource},'%') " +
            "</if>" +
            "<if test = 'maps.tags != null and  maps.tags !=\"\"    '> " +
            "and find_in_set(#{maps.tags}, c.tags)   " +
            "</if>" +
            "<if test = 'maps.status != null      '> " +
            "and c.status =#{maps.status} " +
            "</if>" +
            "<if test = 'maps.visitStatus != null and maps.visitStatus !=\"\"     '> " +
            "and c.visit_status =#{maps.visitStatus} " +
            "</if>" +
            "<if test = 'maps.customerType != null      '> " +
            "and c.customer_type =#{maps.customerType}  " +
            "</if>" +
            "<if test = 'maps.companyId != null      '> " +
            "and c.company_id =#{maps.companyId} " +
            "</if>" +
            "<if test = 'maps.isLine != null      '> " +
            "and c.is_line =#{maps.isLine} " +
            "</if>" +
            "<if test = 'maps.source != null      '> " +
            "and c.source  =#{maps.source} " +
            "</if>" +
            "<if test = 'maps.beginTime != null and maps.beginTime != \"\" '> " +
            "and date_format(c.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "and date_format(c.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d') " +
            "</if>" +
            " order by c.customer_id desc "+
            "</script>"})
    List<CrmCustomerListVO> selectCrmCustomerListVO(@Param("maps") CrmCustomer crmCustomer);

    @Select({"<script> " +
            "select COUNT(mobile) as count from crm_customer " +
            "where 1=1 and is_line = 1 and mobile = #{mobile} and company_id = #{companyId} " +
            "</script>"})
    int selectCrmCustomerMobileNum(CrmCustomer crmCustomer);

    @Select({"<script> " +
            "select c.*,com.company_name,cu.nick_name as company_user_nick_name,ccu.last_time as lastTime from crm_customer c left join company com on com.company_id=c.company_id left join company_user cu on cu.user_id=c.receive_user_id left join crm_customer_user ccu on ccu.customer_user_id = c.customer_user_id " +
            "where 1=1 " +
            "<if test = 'maps.companyId != null     '> " +
            "and c.company_id =#{maps.companyId} " +
            "</if>" +
            "<if test = 'maps.customerCode != null and  maps.customerCode !=\"\"    '> " +
            "and c.customer_code like CONCAT('%',#{maps.customerCode},'%') " +
            "</if>" +
            "<if test = 'maps.customerName != null and  maps.customerName !=\"\"    '> " +
            "and c.customer_name like CONCAT('%',#{maps.customerName},'%') " +
            "</if>" +
            "<if test = 'maps.companyUserNickName != null and  maps.companyUserNickName !=\"\"    '> " +
            "and cu.nick_name like CONCAT(#{maps.companyUserNickName},'%') " +
            "</if>" +
            "<if test = 'maps.mobile != null and  maps.mobile !=\"\"    '> " +
            "and c.mobile like CONCAT('%',#{maps.mobile},'%') " +
            "</if>" +
            "<if test = 'maps.tags != null and  maps.tags !=\"\"    '> " +
            "and find_in_set(#{maps.tags}, c.tags)   " +
            "</if>" +
            "<if test = 'maps.status != null      '> " +
            "and c.status =#{maps.status} " +
            "</if>" +
            "<if test = 'maps.visitStatus != null and maps.visitStatus !=\"\"     '> " +
            "and c.visit_status =#{maps.visitStatus} " +
            "</if>" +
            "<if test = 'maps.customerType != null      '> " +
            "and c.customer_type IN " +
            "<foreach collection=\"maps.customerType.split(',')\"  item='item' index='index'  open='(' separator=',' close=')'> #{item} </foreach>"+
            "</if>" +
            "<if test = 'maps.companyId != null      '> " +
            "and c.company_id =#{maps.companyId} " +
            "</if>" +
            "<if test = 'maps.isLine != null      '> " +
            "and c.is_line =#{maps.isLine} " +
            "</if>" +
            "<if test = 'maps.isBuy != null and maps.isBuy==1      '> " +
            "and c.buy_count &gt; 0 " +
            "</if>" +
            "<if test = 'maps.isBuy != null and maps.isBuy==0      '> " +
            "and (c.buy_count = 0 or c.buy_count is null) " +
            "</if>" +
            "<if test = 'maps.source != null      '> " +
            "and c.source IN " +
            "<foreach collection=\"maps.source.split(',')\"  item='item' index='index'  open='(' separator=',' close=')'> #{item} </foreach>"+
            "</if>" +
            "<if test = 'maps.beginTime != null and maps.beginTime != \"\" '> " +
            "and date_format(c.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "and date_format(c.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.receiveTimeList != null    '> " +
            " AND date_format(c.receive_time,'%y%m%d') &gt;= date_format(#{maps.receiveTimeList[0]},'%y%m%d') " +
            " AND date_format(c.receive_time,'%y%m%d') &lt;= date_format(#{maps.receiveTimeList[1]},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.deptId != null  and maps.deptId != 0 '> " +
            "AND (c.dept_id = #{maps.deptId} OR c.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{maps.deptId}, ancestors) )) " +
            "</if>" +
            "${maps.params.dataScope}"+
            " order by c.customer_id desc "+
            "</script>"})
    List<CrmCustomerListVO> selectCrmCustomerListQueryParam(@Param("maps") CrmCustomerListQueryParam crmCustomer);


    @Select({"<script> " +
            "select cu.*,c.create_time as customer_create_time,c.visit_status,c.remark,c.register_desc,c.register_submit_time,c.customer_code,c.customer_name,c.mobile,c.sex,c.weixin,c.address,c.is_receive,c.customer_type,c.source,c.tags,c.receive_time from crm_customer_user cu inner join crm_customer c on c.customer_user_id=cu.customer_user_id " +
            "where cu.is_pool=0 " +
            "<if test = 'maps.companyId != null     '> " +
            "and cu.company_id =#{maps.companyId} " +
            "</if>" +
            "<if test = 'maps.companyUserId != null     '> " +
            "and cu.company_user_id =#{maps.companyUserId} " +
            "</if>" +
            "<if test = 'maps.address != null and  maps.address !=\"\"    '> " +
            "and c.address like CONCAT('%',#{maps.address},'%') " +
            "</if>" +
            "<if test = 'maps.customerCode != null and  maps.customerCode !=\"\"    '> " +
            "and c.customer_code like CONCAT('%',#{maps.customerCode},'%') " +
            "</if>" +
            "<if test = 'maps.customerName != null and  maps.customerName !=\"\"    '> " +
            "and c.customer_name like CONCAT('%',#{maps.customerName},'%') " +
            "</if>" +
            "<if test = 'maps.mobile != null and  maps.mobile !=\"\"    '> " +
            "and c.mobile like CONCAT('%',#{maps.mobile},'%') " +
            "</if>" +
            "<if test = 'maps.status != null and maps.status !=\"\"     '> " +
            "and cu.status IN " +
            "<foreach collection=\"maps.status.split(',')\"  item='item' index='index'  open='(' separator=',' close=')'> #{item} </foreach>"+
            "</if>" +
            "<if test = 'maps.isHisOrder != null and maps.isHisOrder==1      '> " +
            "and (select ifnull(count(1),0) from crm_customer_his_order h where h.customer_id=c.customer_id )  &gt; 0 " +
            "</if>" +
            "<if test = 'maps.isHisOrder != null and maps.isHisOrder==0      '> " +
            "and (select ifnull(count(1),0) from crm_customer_his_order h where h.customer_id=c.customer_id )  = 0 " +
            "</if>" +
            "<if test = 'maps.customerType != null      '> " +
            "and c.customer_type IN " +
            "<foreach collection=\"maps.customerType.split(',')\"  item='item' index='index'  open='(' separator=',' close=')'> #{item} </foreach>"+
            "</if>" +
            "<if test = 'maps.source != null and maps.source !=\"\"      '> " +
            "and c.source IN " +
            "<foreach collection=\"maps.source.split(',')\"  item='item' index='index'  open='(' separator=',' close=')'> #{item} </foreach>"+
            "</if>" +
            "<if test = 'maps.tags != null and maps.tags!=\"\"     '> " +
            "and  " +
            "<foreach collection=\"maps.tags.split(',')\" item=\"tag\"   open=\"(\" close=\")\" separator=\"OR\">" +
            "find_in_set(#{tag},c.tags)" +
            "</foreach> " +
            "</if>" +
            "<if test = 'maps.customerCreateTime != null    '> " +
            " AND date_format(c.create_time,'%y%m%d') &gt;= date_format(#{maps.customerCreateTime[0]},'%y%m%d') " +
            " AND date_format(c.create_time,'%y%m%d') &lt;= date_format(#{maps.customerCreateTime[1]},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.beginTime != null and maps.beginTime != \"\" '> " +
            "and date_format(c.receive_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "and date_format(c.receive_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d') " +
            "</if>" +
            " order by cu.customer_user_id desc "+
            "</script>"})
    List<CrmMyCustomerListQueryVO> selectCrmMyCustomerListQuery(@Param("maps") CrmMyCustomerListQueryParam param);
    @Select({"<script> " +
            "select c.*,u.nick_name as company_user_nick_name,ccu.start_time as startTime  from  crm_customer c left join company_user u on u.user_id=c.receive_user_id left join crm_customer_user ccu on c.customer_user_id = ccu.customer_user_id " +
//            "where is_line=0 " +
            "where 1=1 " +
            "<if test = 'maps.companyId != null     '> " +
            "and c.company_id =#{maps.companyId} " +
            "</if>" +
            "<if test = 'maps.companyUserNickName != null and  maps.companyUserNickName !=\"\"    '> " +
            "and u.nick_name like CONCAT(#{maps.companyUserNickName},'%') " +
            "</if>" +
            "<if test = 'maps.companyUserId != null     '> " +
            "and c.company_user_id =#{maps.companyUserId} " +
            "</if>" +
            "<if test = 'maps.customerCode != null and  maps.customerCode !=\"\"    '> " +
            "and c.customer_code like CONCAT('%',#{maps.customerCode},'%') " +
            "</if>" +
            "<if test = 'maps.customerName != null and  maps.customerName !=\"\"    '> " +
            "and c.customer_name like CONCAT('%',#{maps.customerName},'%') " +
            "</if>" +
            "<if test = 'maps.mobile != null and  maps.mobile !=\"\"    '> " +
            "and c.mobile like CONCAT('%',#{maps.mobile},'%') " +
            "</if>" +
            "<if test = 'maps.status != null and maps.status !=\"\"     '> " +
            "and c.status =#{maps.status} " +
            "</if>" +

            "<if test = 'maps.visitStatus != null       '> " +
            "and c.visit_status IN " +
            "<foreach collection=\"maps.visitStatus.split(',')\"  item='item' index='index'  open='(' separator=',' close=')'> #{item} </foreach>"+
            "</if>" +

            "<if test = 'maps.isBuy != null and maps.isBuy==1      '> " +
            "and c.buy_count &gt; 0 " +
            "</if>" +

            "<if test = 'maps.isBuy != null and maps.isBuy==0      '> " +
            "and (c.buy_count = 0 or c.buy_count is null) " +
            "</if>" +

            "<if test = 'maps.isHisOrder != null and maps.isHisOrder==1      '> " +
            "and (select ifnull(count(1),0) from crm_customer_his_order h where h.customer_id=c.customer_id )  &gt; 0 " +
            "</if>" +
            "<if test = 'maps.isHisOrder != null and maps.isHisOrder==0      '> " +
            "and (select ifnull(count(1),0) from crm_customer_his_order h where h.customer_id=c.customer_id )  = 0 " +
            "</if>" +


            "<if test = 'maps.customerType != null      '> " +
            "and c.customer_type IN " +
            "<foreach collection=\"maps.customerType.split(',')\"  item='item' index='index'  open='(' separator=',' close=')'> #{item} </foreach>"+
            "</if>" +

            "<if test = 'maps.source != null and maps.source !=\"\"      '> " +
            "and c.source IN " +
            "<foreach collection=\"maps.source.split(',')\"  item='item' index='index'  open='(' separator=',' close=')'> #{item} </foreach>"+
            "</if>" +

            "<if test = 'maps.isReceive != null      '> " +
            "and c.is_receive =#{maps.isReceive} " +
            "</if>" +
            "<if test = 'maps.isLine != null      '> " +
            "and c.is_line =#{maps.isLine} " +
            "</if>" +
            "<if test = 'maps.tags != null      '> " +
            "and  " +
            "<foreach collection=\"maps.tags.split(',')\" item=\"tag\"   open=\"(\" close=\")\" separator=\"OR\">" +
            "find_in_set(#{tag},c.tags)" +
            "</foreach> " +
            "</if>" +
            "<if test = 'maps.beginTime != null and maps.beginTime != \"\" '> " +
            "and date_format(c.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "and date_format(c.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.deptId != null  and maps.deptId != 0 '> " +
            "AND (c.dept_id = #{maps.deptId} OR c.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{maps.deptId}, ancestors) )) " +
            "</if>" +
            "${maps.params.dataScope}"+
            " order by c.customer_id desc "+
            "</script>"})
    List<CrmCustomerListQueryVO> selectCrmCustomerListQuery(@Param("maps") CrmCustomerListQueryParam param);
    @Select({"<script> " +
            "select c.*,u.nick_name as company_user_nick_name  from  crm_customer c left join company_user u on u.user_id=c.receive_user_id   " +
            "where is_line=1 " +
//            " select  c.*  " +
////            "<if test = 'maps.isDuplicate != null '> " +
////            " ,u.customer_id as dCustomerId,(case when u.count>1 and u.customer_id != c.customer_id and  LENGTH(c.mobile)>0 then 1 else 0 end) as isDuplicate " +
////            "</if>" +
//            " from crm_customer c  " +
////            "<if test = 'maps.isDuplicate != null '> " +
////              " LEFT JOIN  ( SELECT mobile,MIN(customer_id) as customer_id,  COUNT(*) as count FROM crm_customer GROUP BY mobile HAVING count >1) u on c.mobile=u.mobile  " +
////            "</if>" +
//            " where c.is_line=1  " +
//            "<if test = 'maps.isDuplicate != null and  maps.isDuplicate == 1  '> " +
//            " and u.customer_id != c.customer_id and  LENGTH(c.mobile)>0 " +
//            "</if>" +
//            "<if test = 'maps.isDuplicate != null and  maps.isDuplicate == 2  '> " +
//            "  and u.customer_id = c.customer_id and  LENGTH(c.mobile)>0 " +
//            "</if>" +
            "<if test = 'maps.customerCode != null and  maps.customerCode !=\"\"    '> " +
            "and c.customer_code like CONCAT('%',#{maps.customerCode},'%') " +
            "</if>" +
            "<if test = 'maps.customerName != null and  maps.customerName !=\"\"    '> " +
            "and c.customer_name like CONCAT('%',#{maps.customerName},'%') " +
            "</if>" +
            "<if test = 'maps.mobile != null and  maps.mobile !=\"\"    '> " +
            "and c.mobile like CONCAT('%',#{maps.mobile},'%') " +
            "</if>" +
            "<if test = 'maps.status != null and maps.status !=\"\"     '> " +
            "and c.status =#{maps.status} " +
            "</if>" +
            "<if test = 'maps.customerType != null      '> " +
            "and c.customer_type =#{maps.customerType} " +
            "</if>" +
            "<if test = 'maps.companyId != null      '> " +
            "and c.company_id =#{maps.companyId} " +
            "</if>" +
            "<if test = 'maps.source != null and maps.source !=\"\"      '> " +
            "and c.source =#{maps.source} " +
            "</if>" +
            "<if test = 'maps.tags != null      '> " +
            "and  " +
            "<foreach collection=\"maps.tags.split(',')\" item=\"tag\"   open=\"(\" close=\")\" separator=\"OR\">" +
            "find_in_set(#{tag},c.tags)" +
            "</foreach> " +
            "</if>" +
            "<if test = 'maps.createTime != null '> " +
            "and  DATE_FORMAT(c.create_time, '%Y-%m-%d')  = DATE_FORMAT(#{maps.createTime}, '%Y-%m-%d') " +
            "</if>" +
            "<if test = 'maps.beginTime != null and maps.beginTime != \"\" '> " +
            "and date_format(c.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "and date_format(c.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d') " +
            "</if>" +
            " order by c.customer_id desc "+
            "</script>"})
    List<CrmLineCustomerListQueryVO> selectCrmLineCustomerListQuery(@Param("maps") CrmLineCustomerListQueryParam param);
    @Select({"<script> " +
            "select ifnull(count(1),0) from crm_customer c  " +
            "where c.status=1 and c.is_del=0 " +
            "<if test = 'companyId != null     '> " +
            "and c.company_id =#{companyId} " +
            "</if>" +
            "<if test = 'type ==1      '> " +
            "and DATE_FORMAT(c.create_time, '%Y-%m-%d')  = DATE_FORMAT(now(), '%Y-%m-%d') " +
            "</if>" +
            "<if test = 'type ==2      '> " +
            "</if>" +
            "</script>"})
    Integer selectCrmCustomerCountByType(@Param("companyId") Long companyId, @Param("type") int type);
    @Select({"<script> " +
            "select ifnull(count(1),0)  from crm_customer  " +
            "where 1=1 " +
            "<if test = 'type != null and  type ==1  '> " +
            "and  DATE_FORMAT(create_time, '%Y-%m-%d')  = DATE_FORMAT(NOW(), '%Y-%m-%d') " +
            "</if>" +
            "<if test = 'companyId != null  '> " +
            "and  company_id=#{companyId} " +
            "</if>" +
            "</script>"})
    Long selectCrmCustomerCount(@Param("type") int type, @Param("companyId") Long companyId);

    List<JSONObject> selectCrmCustomerCounts(Map<String, Object> toMap);
    @Select({"<script> " +
            "select u.user_id, u.nick_name,d.dept_name, count( IF ( cu.is_pool =0, 1, NULL ))  as receive_count, count( IF ( cu.is_pool =1, 1, NULL )) as pool_count from crm_customer_user cu left join company_user u on u.user_id=cu.company_user_id left join company_dept d on d.dept_id=u.dept_id  " +
            "where 1=1 " +
            "<if test = 'maps.type != null and maps.type ==1 '> " +
            "and TO_DAYS(cu.create_time) = TO_DAYS(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==2 '> " +
            "and TO_DAYS(NOW()) - TO_DAYS(cu.create_time) &lt;= 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==3 '> " +
            "and YEARWEEK(DATE_FORMAT(cu.create_time,'%Y-%m-%d')) = YEARWEEK(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==4 '> " +
            "and YEARWEEK(DATE_FORMAT(cu.create_time,'%Y-%m-%d')) = YEARWEEK(NOW())-1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==5 '> " +
            "and DATE_FORMAT(cu.create_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==6 '> " +
            "and PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'),DATE_FORMAT(cu.create_time,'%Y%m')) = 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==7 '> " +
            "and QUARTER(cu.create_time) = QUARTER(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==8 '> " +
            "and QUARTER(cu.create_time) = QUARTER(DATE_SUB(NOW(),INTERVAL 1 QUARTER)) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==9 '> " +
            "and YEAR(cu.create_time)=YEAR(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==10 '> " +
            "and  YEAR(cu.create_time) = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR))" +
            "</if>" +
            "<if test = 'maps.startTime != null and maps.startTime != \"\" '> " +
            "AND date_format(cu.create_time,'%y%m%d') &gt;= date_format(#{maps.startTime},'%y%m%d')"+
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "AND date_format(cu.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d')"+
            "</if>" +
            "and  cu.company_user_id IN " +
            "<foreach  item='item' index='index' collection='maps.users' open='(' separator=',' close=')'> #{item}    </foreach>"+
            " group by cu.company_user_id "+
            "</script>"})
    List<CrmCustomerStatisticsVO> selectCrmCustomerStatisticsList(@Param("maps") CrmCustomerStatisticsParam param);

    @Update("update crm_customer set source=#{maps.source} where find_in_set(customer_id,#{maps.ids})")
    int updateCrmCustomerSource(@Param("maps") CrmCustomerEditSourceParam param);
    @Select("select * from crm_customer where find_in_set(customer_id,#{customerIds})")
    List<CrmCustomer> selectCrmCustomerListByIds(String customerIds);
    @Select({"<script> " +
            "select c.*,ru.nick_name as receive_user_nick_name ,cu.nick_name as create_user_nick_name from  crm_customer c left join company_user ru on ru.user_id=c.receive_user_id left join company_user cu on cu.user_id=c.create_user_id   " +
            "where 1=1 " +
            "<if test = 'maps.isLine != null     '> " +
            "and c.is_line =#{maps.isLine} " +
            "</if>" +
            "<if test = 'maps.companyId != null     '> " +
            "and c.company_id =#{maps.companyId} " +
            "</if>" +
            "<if test = 'maps.companyUserNickName != null and  maps.companyUserNickName !=\"\"    '> " +
            "and ru.nick_name like CONCAT(#{maps.companyUserNickName},'%') " +
            "</if>" +
            "<if test = 'maps.companyUserId != null     '> " +
            "and c.company_user_id =#{maps.companyUserId} " +
            "</if>" +
            "<if test = 'maps.customerCode != null and  maps.customerCode !=\"\"    '> " +
            "and c.customer_code like CONCAT('%',#{maps.customerCode},'%') " +
            "</if>" +
            "<if test = 'maps.customerName != null and  maps.customerName !=\"\"    '> " +
            "and c.customer_name like CONCAT('%',#{maps.customerName},'%') " +
            "</if>" +
            "<if test = 'maps.mobile != null and  maps.mobile !=\"\"    '> " +
            "and c.mobile like CONCAT('%',#{maps.mobile},'%') " +
            "</if>" +
            "<if test = 'maps.status != null and maps.status !=\"\"     '> " +
            "and c.status =#{maps.status} " +
            "</if>" +
            "<if test = 'maps.visitStatus != null       '> " +
            "and c.visit_status =#{maps.visitStatus} " +
            "</if>" +
            "<if test = 'maps.customerType != null      '> " +
            "and c.customer_type =#{maps.customerType} " +
            "</if>" +
            "<if test = 'maps.source != null and maps.source !=\"\"      '> " +
            "and c.source =#{maps.source} " +
            "</if>" +
            "<if test = 'maps.isReceive != null      '> " +
            "and c.is_receive =#{maps.isReceive} " +
            "</if>" +
            "<if test = 'maps.beginTime != null and maps.beginTime != \"\" '> " +
            "and date_format(c.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "and date_format(c.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.deptId != null  and maps.deptId != 0 '> " +
            "AND (c.dept_id = #{maps.deptId} OR c.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{maps.deptId}, ancestors) )) " +
            "</if>" +
            "${maps.params.dataScope}"+
            " order by c.customer_id desc limit 30000 "+
            "</script>"})
    List<CrmCustomerExportVO> selectCrmCustomerExportListQuery(@Param("maps") CrmCustomerListQueryParam param);
    @Select({"<script> " +
            "select c.*  from  crm_customer c " +
            "where 1=1 " +
            "<if test = 'maps.companyId != null     '> " +
            "and c.company_id =#{maps.companyId} " +
            "</if>" +
            "<if test = 'maps.mobile != null and  maps.mobile !=\"\"    '> " +
            "and c.mobile = #{maps.mobile} " +
            "</if>" +
            " order by c.customer_id desc "+
            "</script>"})
    List<CrmCustomer> selectCrmCustomerListBySearch(@Param("maps") CrmCustomerSearchParam param);
    @Select("select ifnull(count(1),0) from crm_customer where customer_code=#{customerCode} ")
    int check(String customerCode);
    @Update("update crm_customer set buy_count=(select ifnull(count(1),0) from fs_store_order o where o.customer_id=#{customerId} ) where customer_id=#{customerId} ")
    int updateBuyCount(Long customerId);

    @Select({"<script> " +
            "select ifnull(count(1),0) from crm_customer " +
            "where 1=1 " +
            "<if test = 'maps.mobile != null and  maps.mobile !=\"\"    '> " +
            "and mobile = #{maps.mobile} " +
            "</if>" +
            "<if test = 'maps.clueId != null and  maps.clueId !=\"\"    '> " +
            "and clue_id = #{maps.clueId} " +
            "</if>" +
            "<if test = 'maps.thirdAccount != null and  maps.thirdAccount !=\"\"    '> " +
            "and third_account = #{maps.thirdAccount} " +
            "</if>" +
            " order by customer_id desc "+
            "</script>"})
    int checkCrmThirdFeiyuClue(@Param("maps") CrmThirdFeiyuCluesSearchParam param);


    @Select({"<script> " +
            "select c.*,ru.nick_name as receive_user_nick_name ,cu.nick_name as create_user_nick_name from  crm_customer c left join company_user ru on ru.user_id=c.receive_user_id left join company_user cu on cu.user_id=c.create_user_id   " +
            "where c.customer_id in "+
            "<foreach item='customerId' collection='array' open='(' separator=',' close=')'> " +
            "#{customerId} " +
            "</foreach>" +
            " order by c.customer_id desc "+
            "</script>"})
    List<CrmCustomerExportVO> selectCrmCustomerExportListByIds(Long[] customerIds);

    @Select({"<script> " +
            "select * from crm_customer c  " +
            "where c.customer_code=#{crmCode} limit 1 " +
            "</script>"})
    CrmCustomer selectCrmByCode(@Param("crmCode") String customerCode);
    @Select({"<script> " +
            "select u.user_id, u.nick_name,d.dept_name, count( IF ( cu.visit_status =4, 1, NULL ))  as customerOkNum, count(*) as customerNum from crm_customer cu left join company_user u on u.user_id=cu.create_user_id left join company_dept d on d.dept_id=u.dept_id " +
            "where 1=1 " +
            "<if test = 'maps.type != null and maps.type ==1 '> " +
            "and TO_DAYS(cu.create_time) = TO_DAYS(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==2 '> " +
            "and TO_DAYS(NOW()) - TO_DAYS(cu.create_time) &lt;= 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==3 '> " +
            "and YEARWEEK(DATE_FORMAT(cu.create_time,'%Y-%m-%d')) = YEARWEEK(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==4 '> " +
            "and YEARWEEK(DATE_FORMAT(cu.create_time,'%Y-%m-%d')) = YEARWEEK(NOW())-1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==5 '> " +
            "and DATE_FORMAT(cu.create_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==6 '> " +
            "and PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'),DATE_FORMAT(cu.create_time,'%Y%m')) = 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==7 '> " +
            "and QUARTER(cu.create_time) = QUARTER(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==8 '> " +
            "and QUARTER(cu.create_time) = QUARTER(DATE_SUB(NOW(),INTERVAL 1 QUARTER)) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==9 '> " +
            "and YEAR(cu.create_time)=YEAR(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==10 '> " +
            "and  YEAR(cu.create_time) = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR))" +
            "</if>" +
            "<if test = 'maps.startTime != null and maps.startTime != \"\" '> " +
            "AND date_format(cu.create_time,'%y%m%d') &gt;= date_format(#{maps.startTime},'%y%m%d')"+
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "AND date_format(cu.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d')"+
            "</if>" +
            "and  cu.create_user_id IN " +
            "<foreach  item='item' index='index' collection='maps.users' open='(' separator=',' close=')'> #{item}    </foreach>"+
            " group by cu.create_user_id "+
            "</script>"})
    List<CrmCustomerNumStatisticsVO> selectCrmCustomerNumStatisticsList(@Param("maps") CrmCustomerStatisticsParam param);

    List<JSONObject> selectCrmCustomerNumCounts(Map<String, Object> toMap);
    @Select({"<script> " +
            "select u.user_id, u.nick_name,d.dept_name, count( IF ( cu.visit_status =4, 1, NULL ))  as customerOkNum, count(*) as customerNum ,(COUNT(IF(cu.visit_status = 4, 1, NULL)) / COUNT(*)) * 100 AS customerRate from crm_customer cu left join company_user u on u.user_id=cu.create_user_id left join company_dept d on d.dept_id=u.dept_id " +
            "where 1=1 " +
            "<if test = 'maps.type != null and maps.type ==1 '> " +
            "and TO_DAYS(cu.create_time) = TO_DAYS(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==2 '> " +
            "and TO_DAYS(NOW()) - TO_DAYS(cu.create_time) &lt;= 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==3 '> " +
            "and YEARWEEK(DATE_FORMAT(cu.create_time,'%Y-%m-%d')) = YEARWEEK(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==4 '> " +
            "and YEARWEEK(DATE_FORMAT(cu.create_time,'%Y-%m-%d')) = YEARWEEK(NOW())-1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==5 '> " +
            "and DATE_FORMAT(cu.create_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==6 '> " +
            "and PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'),DATE_FORMAT(cu.create_time,'%Y%m')) = 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==7 '> " +
            "and QUARTER(cu.create_time) = QUARTER(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==8 '> " +
            "and QUARTER(cu.create_time) = QUARTER(DATE_SUB(NOW(),INTERVAL 1 QUARTER)) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==9 '> " +
            "and YEAR(cu.create_time)=YEAR(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==10 '> " +
            "and  YEAR(cu.create_time) = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR))" +
            "</if>" +
            "<if test = 'maps.startTime != null and maps.startTime != \"\" '> " +
            "AND date_format(cu.create_time,'%y%m%d') &gt;= date_format(#{maps.startTime},'%y%m%d')"+
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "AND date_format(cu.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d')"+
            "</if>" +
            "and  cu.create_user_id IN " +
            "<foreach  item='item' index='index' collection='maps.users' open='(' separator=',' close=')'> #{item}    </foreach>"+
            " group by cu.create_user_id "+
            "</script>"})
    List<CrmCustomerRateStatisticsVO> selectCrmCustomerRateStatisticsList(@Param("maps")CrmCustomerStatisticsParam param);

    List<JSONObject> selectCrmCustomerRateCounts(Map<String, Object> map);
    @Select({"<script> " +
            "SELECT ANY_VALUE(cu.source) source , COUNT(*) AS `value`,ANY_VALUE(s.dict_label) name  FROM crm_customer cu left JOIN sys_dict_data s ON s.dict_type='crm_customer_source' and  s.dict_value=cu.source " +
            "where 1=1 " +
            "<if test = 'maps.isPay != null and maps.isPay ==1 '> " +
            "and cu.visit_status =4 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==1 '> " +
            "and TO_DAYS(cu.create_time) = TO_DAYS(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==2 '> " +
            "and TO_DAYS(NOW()) - TO_DAYS(cu.create_time) &lt;= 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==3 '> " +
            "and YEARWEEK(DATE_FORMAT(cu.create_time,'%Y-%m-%d')) = YEARWEEK(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==4 '> " +
            "and YEARWEEK(DATE_FORMAT(cu.create_time,'%Y-%m-%d')) = YEARWEEK(NOW())-1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==5 '> " +
            "and DATE_FORMAT(cu.create_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==6 '> " +
            "and PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'),DATE_FORMAT(cu.create_time,'%Y%m')) = 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==7 '> " +
            "and QUARTER(cu.create_time) = QUARTER(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==8 '> " +
            "and QUARTER(cu.create_time) = QUARTER(DATE_SUB(NOW(),INTERVAL 1 QUARTER)) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==9 '> " +
            "and YEAR(cu.create_time)=YEAR(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==10 '> " +
            "and  YEAR(cu.create_time) = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR))" +
            "</if>" +
            "<if test = 'maps.startTime != null and maps.startTime != \"\" '> " +
            "AND date_format(cu.create_time,'%y%m%d') &gt;= date_format(#{maps.startTime},'%y%m%d')"+
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "AND date_format(cu.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d')"+
            "</if>" +
            "and  cu.create_user_id IN " +
            "<foreach  item='item' index='index' collection='maps.users' open='(' separator=',' close=')'> #{item}    </foreach>"+
            " GROUP BY cu.source ORDER BY `value` desc "+
            "</script>"})
    List<CrmCustomerSourceStatisticsVO> selectCrmCustomerSourceStatisticsList(@Param("maps")CrmCustomerStatisticsParam param);
    @Select({"<script> " +
            "SELECT LEFT(cu.address, 2) `name`, COUNT(*) AS `value`   FROM crm_customer cu WHERE cu.address !='' " +
            "<if test = 'maps.isPay != null and maps.isPay ==1 '> " +
            "and cu.visit_status =4 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==1 '> " +
            "and TO_DAYS(cu.create_time) = TO_DAYS(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==2 '> " +
            "and TO_DAYS(NOW()) - TO_DAYS(cu.create_time) &lt;= 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==3 '> " +
            "and YEARWEEK(DATE_FORMAT(cu.create_time,'%Y-%m-%d')) = YEARWEEK(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==4 '> " +
            "and YEARWEEK(DATE_FORMAT(cu.create_time,'%Y-%m-%d')) = YEARWEEK(NOW())-1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==5 '> " +
            "and DATE_FORMAT(cu.create_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==6 '> " +
            "and PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'),DATE_FORMAT(cu.create_time,'%Y%m')) = 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==7 '> " +
            "and QUARTER(cu.create_time) = QUARTER(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==8 '> " +
            "and QUARTER(cu.create_time) = QUARTER(DATE_SUB(NOW(),INTERVAL 1 QUARTER)) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==9 '> " +
            "and YEAR(cu.create_time)=YEAR(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==10 '> " +
            "and  YEAR(cu.create_time) = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR))" +
            "</if>" +
            "<if test = 'maps.startTime != null and maps.startTime != \"\" '> " +
            "AND date_format(cu.create_time,'%y%m%d') &gt;= date_format(#{maps.startTime},'%y%m%d')"+
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "AND date_format(cu.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d')"+
            "</if>" +
            "and  cu.create_user_id IN " +
            "<foreach  item='item' index='index' collection='maps.users' open='(' separator=',' close=')'> #{item}    </foreach>"+
            "   GROUP BY `name` "+
            "</script>"})
    List<CrmCustomerMapStatisticsVO> selectCrmCustomerMapStatisticsList(@Param("maps")CrmCustomerStatisticsParam param);
    @Select({"<script> " +
            "select u.user_id, u.nick_name,d.dept_name, count( IF ( cu.is_pool =0, 1, NULL ))  as receive_count, count(1) as pool_count from crm_customer_user cu left join company_user u on u.user_id=cu.company_user_id left join company_dept d on d.dept_id=u.dept_id  " +
            "where 1=1 " +
            "<if test = 'maps.type != null and maps.type ==1 '> " +
            "and TO_DAYS(cu.create_time) = TO_DAYS(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==2 '> " +
            "and TO_DAYS(NOW()) - TO_DAYS(cu.create_time) &lt;= 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==3 '> " +
            "and YEARWEEK(DATE_FORMAT(cu.create_time,'%Y-%m-%d')) = YEARWEEK(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==4 '> " +
            "and YEARWEEK(DATE_FORMAT(cu.create_time,'%Y-%m-%d')) = YEARWEEK(NOW())-1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==5 '> " +
            "and DATE_FORMAT(cu.create_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==6 '> " +
            "and PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'),DATE_FORMAT(cu.create_time,'%Y%m')) = 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==7 '> " +
            "and QUARTER(cu.create_time) = QUARTER(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==8 '> " +
            "and QUARTER(cu.create_time) = QUARTER(DATE_SUB(NOW(),INTERVAL 1 QUARTER)) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==9 '> " +
            "and YEAR(cu.create_time)=YEAR(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==10 '> " +
            "and  YEAR(cu.create_time) = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR))" +
            "</if>" +
            "<if test = 'maps.startTime != null and maps.startTime != \"\" '> " +
            "AND date_format(cu.create_time,'%y%m%d') &gt;= date_format(#{maps.startTime},'%y%m%d')"+
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "AND date_format(cu.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d')"+
            "</if>" +
            "and  cu.company_user_id IN " +
            "<foreach  item='item' index='index' collection='maps.users' open='(' separator=',' close=')'> #{item}    </foreach>"+
            " group by cu.company_user_id "+
            "</script>"})
    List<CrmCustomerStatisticsVO> selectCrmCustomerFullStatisticsList(@Param("maps")CrmCustomerStatisticsParam param);

    List<JSONObject> selectCustomerFullCounts(Map<String, Object> map);
    @Select({"<script> " +
            "select u.nick_name,ROUND(AVG(DATEDIFF(o.create_time, cu.create_time)))  AS customerCycle,count(1) customerOkNum from fs_store_order o left join crm_customer cu ON o.customer_id = cu.customer_id LEFT JOIN company_user u on u.user_id=o.company_user_id  WHERE is_first=1 " +
            " " +
            "<if test = 'maps.type != null and maps.type ==1 '> " +
            "and TO_DAYS(cu.create_time) = TO_DAYS(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==2 '> " +
            "and TO_DAYS(NOW()) - TO_DAYS(cu.create_time) &lt;= 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==3 '> " +
            "and YEARWEEK(DATE_FORMAT(cu.create_time,'%Y-%m-%d')) = YEARWEEK(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==4 '> " +
            "and YEARWEEK(DATE_FORMAT(cu.create_time,'%Y-%m-%d')) = YEARWEEK(NOW())-1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==5 '> " +
            "and DATE_FORMAT(cu.create_time,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==6 '> " +
            "and PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'),DATE_FORMAT(cu.create_time,'%Y%m')) = 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==7 '> " +
            "and QUARTER(cu.create_time) = QUARTER(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==8 '> " +
            "and QUARTER(cu.create_time) = QUARTER(DATE_SUB(NOW(),INTERVAL 1 QUARTER)) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==9 '> " +
            "and YEAR(cu.create_time)=YEAR(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==10 '> " +
            "and  YEAR(cu.create_time) = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR))" +
            "</if>" +
            "<if test = 'maps.startTime != null and maps.startTime != \"\" '> " +
            "AND date_format(cu.create_time,'%y%m%d') &gt;= date_format(#{maps.startTime},'%y%m%d')"+
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "AND date_format(cu.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d')"+
            "</if>" +
            "and  cu.create_user_id IN " +
            "<foreach  item='item' index='index' collection='maps.users' open='(' separator=',' close=')'> #{item}    </foreach>"+
            " group by o.company_user_id "+
            "</script>"})
    List<CrmCustomerCycleStatisticsVO> selectCrmCustomerCycleStatisticsList(@Param("maps")CrmCustomerStatisticsParam param);

    List<JSONObject> selectCrmCustomerCycleCounts(Map<String, Object> map);

    @Select("select customer_id  from  crm_customer where mobile=#{remarkMobile} limit 1")
    Long selectCrmCustomerByCrmMobile(String remarkMobile);

    /**
     *   根据userid和外部联系人id获取到客户详情
     */

    @Select("SELECT cc.* FROM " +
            "crm_customer cc " +
            "LEFT JOIN qw_external_contact ec ON cc.customer_id = ec.customer_id  " +
            "WHERE " +
            "ec.user_id = #{map.userId}  " +
            "AND ec.external_user_id = #{map.externalUserId} ")
    public CrmCustomer getCustomerDetailsH5(@Param("map") QwCustomerDetailParam param);

    /**
     * 根据电话号码搜索小程序账号
     */
    @Select("select user_id  from  fs_user where phone=#{remarkMobile} limit 1")
    Long selectFsUserByCrmMobile(String remarkMobile);
}
