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.center.taUser.entity.TaUser;
import com.huiju.estateagents.center.taUser.mapper.TaUserMapper;
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.ArrayList;
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
    TaUserMapper taUserMapper;

    @Autowired
    TaRecommendCustomerMapper taRecommendCustomerMapper;

    /**
     * 获取用户授权楼盘
     * 如果 buildingId 参数存在, 则校验参数是否在授权范围内
     *
     * @param userId
     * @param buildingId
     * @return List 长度 0 代表未授权楼盘, null 代表管理员, 不校验楼盘授权
     */
    public List<String> getBuildingListOf(Integer userId, String buildingId) {
        List<String> unAuthrized = new ArrayList<String>() {{
            add("*");   // 构造SQL  building_id in ('*') , 目的是为了阻止检索到数据
        }};

        TaUser taUser = taUserMapper.selectById(userId);
        if (taUser == null) {
            return unAuthrized;
        }

        if (null != taUser.getIsAdmin() && taUser.getIsAdmin()) {
            if (!StringUtils.isEmpty(buildingId)) {
                return new ArrayList<String>() {{
                    add(buildingId);
                }};
            }
            return null;
        }

        List<String> buildings = taUserMapper.getBuildingIdsOf(userId, buildingId);
        return null == buildings || buildings.size() == 0 ? unAuthrized : buildings;
    }

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

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

    @Override
    public TsConsultantKpi stsKPITotalByOrg(Integer orgId, Integer userId, String buildingId, String startDate, String endDate) {
        List<String> buildingIds = getBuildingListOf(userId, buildingId);

        // 合计数据, 但是不包含 客户总计 列
        TsConsultantKpi result = tsConsultantKpiMapper.stsKPITotalByOrg(orgId, buildingIds, startDate, endDate);

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

        return result;
    }

    @Override
    public List<ConsultantKPIExport> stsKPIDailyExport(Integer orgId, Integer userId, String buildingId, String startDate, String endDate) {
        List<String> buildingIds = getBuildingListOf(userId, buildingId);
        return tsConsultantKpiMapper.stsKPIDailyExport(orgId, buildingIds, 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) {
        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) {
        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) {
        return tsConsultantKpiMapper.getConsultantShareTimes(page, orgId, userId, buildingId, startDate, endDate, targetType, targetName, userName, personId);
    }

}
