package com.huiju.estateagents.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.entity.*;
import com.huiju.estateagents.excel.ConsultantKPIExport;
import com.huiju.estateagents.mapper.TaRecommendCustomerMapper;
import com.huiju.estateagents.mapper.TsConsultantKpiMapper;
import com.huiju.estateagents.service.ITsConsultantKpiService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 置业顾问KPI  服务实现类
 * </p>
 *
 * @author yansen
 * @since 2020-04-14
 */
@Service
public class TsConsultantKpiServiceImpl extends ServiceImpl<TsConsultantKpiMapper, TsConsultantKpi> implements ITsConsultantKpiService {
    @Autowired
    TsConsultantKpiMapper tsConsultantKpiMapper;

    @Autowired
    TaRecommendCustomerMapper taRecommendCustomerMapper;

    @Override
    public IPage<TsConsultantKpi> stsKPIDaily(Integer pageNum, Integer pageSize, Integer orgId, String buildingId, String startDate, String endDate, String asc, String desc) {
        if (StringUtils.isEmpty(asc) && StringUtils.isEmpty(desc)) {
            desc = "new_persons";
        }

        IPage<TsConsultantKpi> page = new Page<>(pageNum, pageSize);
        return tsConsultantKpiMapper.stsKPIDaily(page, orgId, buildingId, startDate, endDate, asc, desc);
    }

    @Override
    public TsConsultantKpi stsKPITotalByOrg(Integer orgId, String buildingId, String startDate, String endDate) {
        // 合计数据, 但是不包含 客户总计 列
        TsConsultantKpi result = tsConsultantKpiMapper.stsKPITotalByOrg(orgId, buildingId, startDate, endDate);

        // 统计当前时间段内的已经固化的置业的客户
        Integer totalPersons = tsConsultantKpiMapper.stsAllCustomersByOrg(orgId, buildingId, startDate, endDate);
        result.setTotalPersons(totalPersons);

        return result;
    }

    @Override
    public List<ConsultantKPIExport> stsKPIDailyExport(Integer orgId, String buildingId, String startDate, String endDate) {
        return tsConsultantKpiMapper.stsKPIDailyExport(orgId, buildingId, startDate, endDate);
    }

    @Override
    public IPage<TaRecommendCustomer> getCustomerListOfConsultant(IPage<TaRecommendCustomer> page, Integer orgId, String userId, String buildingId, LocalDateTime startDate, LocalDateTime endDate) {
        return taRecommendCustomerMapper.getCustomerListOfConsultant(page, orgId, userId, buildingId, startDate, endDate);
    }

    @Override
    public IPage<TaPerson> getConsultantShareCustomers(IPage<TaPerson> page, Integer orgId, String userId, String buildingId, LocalDateTime startDate, LocalDateTime endDate) {
        // buildingId 在表里不一定有数据, 实际执行 SQL 中没有这个条件
        return tsConsultantKpiMapper.getConsultantShareCustomers(page, orgId, userId, buildingId, startDate, endDate);
    }

    @Override
    public IPage<TaPerson> getConsultantHomePagePersons(IPage<TaPerson> page, Integer orgId, String userId, String buildingId, LocalDateTime startDate, LocalDateTime endDate) {
        return tsConsultantKpiMapper.getConsultantHomePagePersons(page, orgId, userId, buildingId, startDate, endDate);
    }

    @Override
    public IPage<TaPerson> getConsultantHomePageTimes(IPage<TaPerson> page, Integer orgId, String userId, String buildingId, LocalDateTime startDate, LocalDateTime endDate, String personId) {
        return tsConsultantKpiMapper.getConsultantHomePageTimes(page, orgId, userId, buildingId, startDate, endDate, personId);
    }

    @Override
    public IPage<TaPerson> getConsultantChatPersons(IPage<TaPerson> page, Integer orgId, String userId, String buildingId, LocalDateTime startDate, LocalDateTime endDate) {
        // buildingId 在聊天表不存在, 因此没有用到这个字段
        return tsConsultantKpiMapper.getConsultantChatPersons(page, orgId, userId, buildingId, startDate, endDate);
    }

    @Override
    public IPage<TaPerson> getConsultantFavor(IPage<TaPerson> page, Integer orgId, String userId, String buildingId, LocalDateTime startDate, LocalDateTime endDate) {
        // buildingId 在点赞表不存在, 因此没有用到这个字段
        return tsConsultantKpiMapper.getConsultantFavor(page, orgId, userId, buildingId, startDate, endDate);
    }

    @Override
    public IPage<TaPerson> getConsultantShareTargets(IPage<TaPerson> page, Integer orgId, String userId, String buildingId, LocalDateTime startDate, LocalDateTime endDate, String targetType, String targetName) {
        return tsConsultantKpiMapper.getConsultantShareTargets(page, orgId, userId, buildingId, startDate, endDate, targetType, targetName);
    }

    @Override
    public IPage<TaPerson> getConsultantSharePersons(IPage<TaPerson> page, Integer orgId, String userId, String buildingId, LocalDateTime startDate, LocalDateTime endDate) {
        // buildingId 在表里不一定有数据, 实际执行 SQL 中没有这个条件
        return tsConsultantKpiMapper.getConsultantSharePersons(page, orgId, userId, buildingId, startDate, endDate);
    }

    @Override
    public IPage<TaPerson> getConsultantShareTimes(IPage<TaPerson> page, Integer orgId, String userId, String buildingId, LocalDateTime startDate, LocalDateTime endDate, String targetType, String targetName, String userName, String personId) {
        // buildingId 在表里不一定有数据, 实际执行 SQL 中没有这个条件
        return tsConsultantKpiMapper.getConsultantShareTimes(page, orgId, userId, buildingId, startDate, endDate, targetType, targetName, userName, personId);
    }

}
