package com.fs.company.mapper;

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

import com.alibaba.fastjson.JSONObject;
import com.fs.company.domain.CompanyVoiceLogs;
import com.fs.company.param.CompanyStatisticsParam;
import com.fs.company.param.CompanyVoiceLogsListQueryParam;
import com.fs.company.param.CompanyVoiceLogsParam;
import com.fs.company.vo.CompanyVoiceLogsExportVO;
import com.fs.company.vo.CompanyVoiceLogsListVO;
import com.fs.company.vo.CompanyVoiceLogsStatisticsVO;
import com.fs.company.vo.CompanyVoiceLogsVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 通话记录Mapper接口
 *
 * @author fs
 * @date 2021-10-04
 */
public interface CompanyVoiceLogsMapper
{
    /**
     * 查询通话记录
     *
     * @param voiceId 通话记录ID
     * @return 通话记录
     */
    public CompanyVoiceLogs selectCompanyVoiceLogsById(Long voiceId);

    /**
     * 查询通话记录列表
     *
     * @param companyVoiceLogs 通话记录
     * @return 通话记录集合
     */
    public List<CompanyVoiceLogs> selectCompanyVoiceLogsList(CompanyVoiceLogs companyVoiceLogs);

    /**
     * 新增通话记录
     *
     * @param companyVoiceLogs 通话记录
     * @return 结果
     */
    public int insertCompanyVoiceLogs(CompanyVoiceLogs companyVoiceLogs);

    /**
     * 修改通话记录
     *
     * @param companyVoiceLogs 通话记录
     * @return 结果
     */
    public int updateCompanyVoiceLogs(CompanyVoiceLogs companyVoiceLogs);

    /**
     * 删除通话记录
     *
     * @param voiceId 通话记录ID
     * @return 结果
     */
    public int deleteCompanyVoiceLogsById(Long voiceId);

    /**
     * 批量删除通话记录
     *
     * @param voiceIds 需要删除的数据ID
     * @return 结果
     */
    public int deleteCompanyVoiceLogsByIds(Long[] voiceIds);
    @Select({"<script> " +
            "select l.*,u.nick_name as user_nick_name,c.company_name,crm.source,crm.create_time as createTime, crm.customer_name as customerName " +
            "from company_voice_logs l left join company c on c.company_id=l.company_id left join  company_voice_api a  on a.api_id=c.voice_api_id  left join company_user u on u.user_id=l.company_user_id " +
            " left join crm_customer crm on l.customer_id=crm.customer_id " +
            "where 1=1 " +
            "<if test = 'maps.companyId != null  '> " +
            "and l.company_id = #{maps.companyId}" +
            "</if>" +
            "<if test = 'maps.customerId != null  '> " +
            "and l.customer_id = #{maps.customerId}" +
            "</if>" +
            "<if test = 'maps.userNickName != null and maps.userNickName != \"\" '> " +
            "and u.nick_name like '%${maps.userNickName}%' " +
            "</if>" +
            "<if test = 'maps.callerPhone != null and maps.callerPhone != \"\" '> " +
            "and l.caller_phone like '%${maps.callerPhone}%' " +
            "</if>" +
            "<if test = 'maps.calleePhone != null and maps.calleePhone != \"\" '> " +
            "and l.callee_phone like '%${maps.calleePhone}%' " +
            "</if>" +
            "<if test = 'maps.status != null '> " +
            "and l.status = #{maps.status}" +
            "</if>" +
            "<if test = 'maps.source != null '> " +
            "and crm.source = #{maps.source}" +
            "</if>" +
            "<if test = 'maps.times != null '> " +
            "and l.times > #{maps.times}" +
            "</if>" +
            "<if test = 'maps.startTimeList != null    '> " +
            " AND date_format(l.start_time,'%y%m%d') &gt;= date_format(#{maps.startTimeList[0]},'%y%m%d') " +
            " AND date_format(l.start_time,'%y%m%d') &lt;= date_format(#{maps.startTimeList[1]},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.createTimeList != null    '> " +
            " AND date_format(crm.create_time,'%y%m%d') &gt;= date_format(#{maps.createTimeList[0]},'%y%m%d') " +
            " AND date_format(crm.create_time,'%y%m%d') &lt;= date_format(#{maps.createTimeList[1]},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.deptId != null    '> " +
            "  AND (u.dept_id = #{maps.deptId} OR u.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{maps.deptId}, ancestors) )) " +
            "</if>" +
            "${maps.params.dataScope}"+
            "order by l.voice_id desc " +
            "</script>"})
    List<CompanyVoiceLogsVO> selectCompanyVoiceLogsVOList(@Param("maps")CompanyVoiceLogsParam param);

    @Select({"<script> " +
            "select l.*,u.nick_name as user_nick_name,c.company_name,crm.source,crm.create_time as createTime " +
            "from company_voice_logs l left join company c on c.company_id=l.company_id left join  company_voice_api a  on a.api_id=c.voice_api_id  left join company_user u on u.user_id=l.company_user_id " +
            " left join crm_customer crm on l.customer_id=crm.customer_id " +
            "where 1=1 " +
            "<if test = 'maps.companyId != null  '> " +
            "and l.company_id = #{maps.companyId}" +
            "</if>" +
            "<if test = 'maps.customerId != null  '> " +
            "and l.customer_id = #{maps.customerId}" +
            "</if>" +
            "<if test = 'maps.userNickName != null and maps.userNickName != \"\" '> " +
            "and u.nick_name like '%${maps.userNickName}%' " +
            "</if>" +
            "<if test = 'maps.callerPhone != null and maps.callerPhone != \"\" '> " +
            "and l.caller_phone like '%${maps.callerPhone}%' " +
            "</if>" +
            "<if test = 'maps.calleePhone != null and maps.calleePhone != \"\" '> " +
            "and l.callee_phone like '%${maps.calleePhone}%' " +
            "</if>" +
            "<if test = 'maps.status != null '> " +
            "and l.status = #{maps.status}" +
            "</if>" +
            "<if test = 'maps.source != null '> " +
            "and crm.source = #{maps.source}" +
            "</if>" +
            "<if test = 'maps.times != null '> " +
            "and l.times > #{maps.times}" +
            "</if>" +
            "<if test = 'maps.startTimeList != null    '> " +
            " AND date_format(l.start_time,'%y%m%d') &gt;= date_format(#{maps.startTimeList[0]},'%y%m%d') " +
            " AND date_format(l.start_time,'%y%m%d') &lt;= date_format(#{maps.startTimeList[1]},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.createTimeList != null    '> " +
            " AND date_format(crm.create_time,'%y%m%d') &gt;= date_format(#{maps.createTimeList[0]},'%y%m%d') " +
            " AND date_format(crm.create_time,'%y%m%d') &lt;= date_format(#{maps.createTimeList[1]},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.deptId != null    '> " +
            "  AND (u.dept_id = #{maps.deptId} OR u.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{maps.deptId}, ancestors) )) " +
            "</if>" +
            "${maps.params.dataScope}"+
            "order by l.voice_id desc limit 30000 " +
            "</script>"})
    List<CompanyVoiceLogsExportVO> selectCompanyVoiceLogsExportVOList(@Param("maps")CompanyVoiceLogsParam param);

    @Select("select * from company_voice_logs where session_id=#{sessionId}")
    CompanyVoiceLogs selectCompanyVoiceLogsBySessionId(String sessionId);
    @Select("select count(1) from company_voice_logs  ")
    Integer selectCompanyVoiceLogsCount();
    @Select({"<script> " +
            "select count(1) from company_voice_logs  " +
            "where callee_phone=#{mobile} " +
            "<if test = 'type != null and type ==1 '> " +
            "and timestampdiff(MINUTE,start_time,now()) &lt; 1 " +
            "</if>" +
            "<if test = 'type != null and type ==2 '> " +
            "and timestampdiff(HOUR,start_time,now()) &lt; 1 " +
            "</if>" +
            "<if test = 'type != null and type ==3 '> " +
            "and timestampdiff(DAY ,start_time,now()) &lt; 1 " +
            "</if>" +
            "<if test = 'type != null and type ==4 '> " +
            "and timestampdiff(WEEK ,start_time,now()) &lt; 1 " +
            "</if>" +
            "<if test = 'type != null and type ==5 '> " +
            "and timestampdiff(MONTH,start_time,now()) &lt; 1 " +
            "</if>" +
            "</script>"})
    Integer selectCompanyVoiceLogsCountByCalleeMobile(@Param("mobile") String calleeNbr,@Param("type")Integer type);
    @Select({"<script> " +
            "select count(1) from company_voice_logs  " +
            "where caller_phone=#{mobile} " +
            "<if test = 'type != null and type ==1 '> " +
            "and timestampdiff(MINUTE,start_time,now()) &lt; 1 " +
            "</if>" +
            "<if test = 'type != null and type ==2 '> " +
            "and timestampdiff(HOUR,start_time,now()) &lt; 1 " +
            "</if>" +
            "<if test = 'type != null and type ==3 '> " +
            "and timestampdiff(DAY ,start_time,now()) &lt; 1 " +
            "</if>" +
            "<if test = 'type != null and type ==4 '> " +
            "and timestampdiff(WEEK ,start_time,now()) &lt; 1 " +
            "</if>" +
            "<if test = 'type != null and type ==5 '> " +
            "and timestampdiff(MONTH,start_time,now()) &lt; 1 " +
            "</if>" +
            "</script>"})
    Integer selectCompanyVoiceLogsCountByCallerMobile(@Param("mobile")String callerMobile,@Param("type")Integer type);
    @Select({"<script> " +
            "select u.nick_name, count(l.company_user_id) as call_count,count(ifnull(l.times &gt; 0,null)) as call_success_count ,sum(l.times) as times,sum(l.billing_time) as billing_time from  company_voice_logs l left join company_user u on l.company_user_id=u.user_id  " +
            "where 1=1 " +
            "<if test = 'maps.type != null and maps.type ==1 '> " +
            "and TO_DAYS(l.start_time) = TO_DAYS(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==2 '> " +
            "and TO_DAYS(NOW()) - TO_DAYS(l.start_time) &lt;= 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==3 '> " +
            "and YEARWEEK(DATE_FORMAT(l.start_time,'%Y-%m-%d')) = YEARWEEK(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==4 '> " +
            "and YEARWEEK(DATE_FORMAT(l.start_time,'%Y-%m-%d')) = YEARWEEK(NOW())-1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==5 '> " +
            "and DATE_FORMAT(l.start_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(l.start_time,'%Y%m')) = 1 " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==7 '> " +
            "and QUARTER(l.start_time) = QUARTER(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==8 '> " +
            "and QUARTER(l.start_time) = QUARTER(DATE_SUB(NOW(),INTERVAL 1 QUARTER)) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==9 '> " +
            "and YEAR(l.start_time)=YEAR(NOW()) " +
            "</if>" +
            "<if test = 'maps.type != null and maps.type ==10 '> " +
            "and  YEAR(l.start_time) = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR))" +
            "</if>" +
            "<if test = 'maps.startTime != null and maps.startTime != \"\" '> " +
            "AND date_format(l.start_time,'%y%m%d') &gt;= date_format(#{maps.startTime},'%y%m%d')"+
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\" '> " +
            "AND date_format(l.start_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d')"+
            "</if>" +
            "and  l.company_user_id IN " +
            "<foreach  item='item' index='index' collection='maps.users' open='(' separator=',' close=')'> #{item}    </foreach>"+
            " group by l.company_user_id "+
            "</script>"})
    List<CompanyVoiceLogsStatisticsVO> selectVoiceLogsStatisticsList(@Param("maps") CompanyStatisticsParam param);

    List<JSONObject> selectVoiceLogsTotalCount(Map<String, Object> toMap);
    @Select({"<script> " +
            "select l.* from company_voice_logs l " +
            "where 1=1 " +
            "<if test = 'maps.companyId != null  '> " +
            "and l.company_id = #{maps.companyId}" +
            "</if>" +
            "<if test = 'maps.customerId != null  '> " +
            "and l.customer_id = #{maps.customerId}" +
            "</if>" +
            "order by l.voice_id desc " +
            "</script>"})
    List<CompanyVoiceLogsListVO> selectCompanyVoiceLogsListQuery(@Param("maps")CompanyVoiceLogsListQueryParam param);


    @Select("select count(*) from company_voice_logs where caller_no = #{callerNo} and date_format(start_time,'%y%m%d') >= date_format(#{bindTime},'%y%m%d') ")
    Long selectCompanyVoiceLogsCountByCallerNo(@Param("callerNo")String callerNo,@Param("bindTime") Date bindTime);
}
