package com.huiju.estateagents.statistic.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.entity.TaPersonBuilding;
import com.huiju.estateagents.excel.ActivityStatistics.*;
import com.huiju.estateagents.statistic.mapper.TaActicityStaticMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TaActivityStaticServiceImpl {
    @Autowired
    private TaActicityStaticMapper taActicityStaticMapper;

    public ResponseBean activityStatistical(LocalDate startDate, LocalDate endDate, String building, String targetType, Integer orgId, List<TaPersonBuilding> taPersonBuildings, Integer userId){

        ResponseBean responseBean = new ResponseBean();
        Map<String, Object> map = new HashMap<>();

        Integer sharePersonNum = taActicityStaticMapper.selectSharePersonNum(LocalDateTime.of(startDate, LocalTime.MIN), LocalDateTime.of(endDate, LocalTime.MAX.withNano(0)), building, targetType, orgId, taPersonBuildings, userId);

        Integer shareNum = taActicityStaticMapper.selectShareNum(LocalDateTime.of(startDate, LocalTime.MIN), LocalDateTime.of(endDate, LocalTime.MAX.withNano(0)), building, targetType, orgId, taPersonBuildings, userId);

        Integer visitPersonNum = taActicityStaticMapper.selectVisitPersonNum(LocalDateTime.of(startDate, LocalTime.MIN), LocalDateTime.of(endDate, LocalTime.MAX.withNano(0)), building, targetType, orgId, taPersonBuildings, userId);

        Integer visitNum = taActicityStaticMapper.selectVisitNum(LocalDateTime.of(startDate, LocalTime.MIN), LocalDateTime.of(endDate, LocalTime.MAX.withNano(0)), building, targetType, orgId, taPersonBuildings, userId);

        map.put("sharePersonNum", sharePersonNum);
        map.put("shareNum", shareNum);
        map.put("visitPersonNum", visitPersonNum);
        map.put("visitNum", visitNum);

        responseBean.addSuccess(map);

        return responseBean;
    }

    public ResponseBean selectAllActivityUser(Integer orgId, LocalDate startDate, LocalDate endDate, String buildingId, String targetType, List<TaPersonBuilding> taPersonBuildings, Integer userId){
        ResponseBean responseBean = new ResponseBean();

        // 新增用户数
        List<Map<String, Object>> selectShareCount = taActicityStaticMapper.selectShareCount(orgId, LocalDateTime.of(startDate, LocalTime.MIN), LocalDateTime.of(endDate, LocalTime.MAX.withNano(0)), buildingId, targetType, taPersonBuildings, userId);
        List<Map<String, Object>> selectSharePersonCount = taActicityStaticMapper.selectSharePersonCount(orgId, LocalDateTime.of(startDate, LocalTime.MIN), LocalDateTime.of(endDate, LocalTime.MAX.withNano(0)), buildingId, targetType, taPersonBuildings, userId);
        List<Map<String, Object>> selectVisitCount = taActicityStaticMapper.selectVisitCount(orgId, LocalDateTime.of(startDate, LocalTime.MIN), LocalDateTime.of(endDate, LocalTime.MAX.withNano(0)), buildingId, targetType, taPersonBuildings);
        List<Map<String, Object>> selectVisitPersonCount = taActicityStaticMapper.selectVisitPersonCount(orgId, LocalDateTime.of(startDate, LocalTime.MIN), LocalDateTime.of(endDate, LocalTime.MAX.withNano(0)), buildingId, targetType, taPersonBuildings);
        List<Map<String, Object>> selectAddRegistCount = taActicityStaticMapper.selectAddRegistCount(orgId, LocalDateTime.of(startDate, LocalTime.MIN), LocalDateTime.of(endDate, LocalTime.MAX.withNano(0)), buildingId, targetType, taPersonBuildings);


        for (Map<String, Object> ssc : selectShareCount){
            //如果时间相等，那么原来子查询的数据塞到统一的数据处理map中
            for (Map<String, Object> sspc : selectSharePersonCount){
                if (sspc.get("date").equals(ssc.get("date"))){
                    ssc.put("sharePersonCount", sspc.get("sharePersonCount"));
                }
                continue;
            }
            for (Map<String, Object> svc : selectVisitCount){
                if (svc.get("date").equals(ssc.get("date"))){
                    ssc.put("visitCount", svc.get("visitCount"));
                }
                continue;
            }
            for (Map<String, Object> svpc : selectVisitPersonCount){
                if (svpc.get("date").equals(ssc.get("date"))){
                    ssc.put("visitPersonCount", svpc.get("visitPersonCount"));
                }
                continue;
            }
            for (Map<String, Object> sarc : selectAddRegistCount){
                if (sarc.get("date").equals(ssc.get("date"))){
                    ssc.put("addregistCount", sarc.get("addregistCount"));
                }
                continue;
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("selectAllActivityUser", selectShareCount);
        responseBean.addSuccess(map);
        return responseBean;
    }

    public ResponseBean selectTableData(Integer pageNum, Integer pageSize, Integer orgId, LocalDate startDate, LocalDate endDate, String buildingId, String targetType, String sort, String colKey, List<TaPersonBuilding> taPersonBuildings, Integer userId){
        IPage<StatisicAttractUser> iPage = new Page<>(pageNum, pageSize);
        String sortType = "ascend".equals(sort) ? "asc" : "desc";
        IPage<StatisicAttractUser> data = taActicityStaticMapper.selectTableData(iPage, orgId, LocalDateTime.of(startDate, LocalTime.MIN), LocalDateTime.of(endDate, LocalTime.MAX.withNano(0)), buildingId, targetType, sortType, colKey, taPersonBuildings, userId);
        return ResponseBean.success(data);
    }

    public List<StatisicAttractUser> selectExportTableData(Integer orgId, LocalDate startDate, LocalDate endDate, String buildingId, String targetType, String sort, String colKey, List<TaPersonBuilding> taPersonBuildings, Integer userId){
        String sortType = "ascend".equals(sort) ? "asc" : "desc";
        return taActicityStaticMapper.selectExportTableData(orgId, LocalDateTime.of(startDate, LocalTime.MIN), LocalDateTime.of(endDate, LocalTime.MAX.withNano(0)), buildingId, targetType, sortType, colKey, taPersonBuildings, userId);
    }

    public ResponseBean selectActivityStatisDetail(Integer pageNum, Integer pageSize, Integer orgId, LocalDate startDate, LocalDate endDate, String buildingId, String targetType, String activityName, String sort, String colKey, List<TaPersonBuilding> taPersonBuildings, Integer userId){
        IPage<ActivityDetailShareRecord> iPage = new Page<>(pageNum, pageSize);
        String sortType = "ascend".equals(sort) ? "asc" : "desc";
        IPage<ActivityDetailShareRecord> data = taActicityStaticMapper.selectActivityStatisDetail(iPage, orgId, LocalDateTime.of(startDate, LocalTime.MIN), LocalDateTime.of(endDate, LocalTime.MAX.withNano(0)), buildingId, targetType, activityName, sortType, colKey, taPersonBuildings, userId);
        return ResponseBean.success(data);
    }

    public List<ActivityDetailShareRecord> selectActivityStatisDetailExport(Integer orgId, LocalDate startDate, LocalDate endDate, String buildingId, String targetType, String activityName, String sort, String colKey, List<TaPersonBuilding> taPersonBuildings, Integer userId){
        String sortType = "ascend".equals(sort) ? "asc" : "desc";
        return taActicityStaticMapper.selectActivityStatisDetailExport(orgId, startDate, endDate, buildingId, targetType, activityName, sortType, colKey, taPersonBuildings, userId);
    }

    public ResponseBean selectActivitySharePersonNum(Integer pageNum, Integer pageSize, Integer orgId, LocalDate startDate, LocalDate endDate, String sharePersonType, String sharePersonName, String sharePhone, String activityId, String activityType, String buildingId, String sort, String colKey, List<TaPersonBuilding> taPersonBuildings, Integer userId){
        IPage<SharePersonNum> iPage = new Page<>(pageNum, pageSize);
        String sortType = "ascend".equals(sort) ? "asc" : "desc";

        IPage<SharePersonNum> data = taActicityStaticMapper.selectActivitySharePersonNum(iPage, orgId, LocalDateTime.of(startDate, LocalTime.MAX), LocalDateTime.of(endDate, LocalTime.MAX), sharePersonType, sharePersonName, sharePhone, activityId, activityType, buildingId, sortType, colKey, taPersonBuildings, userId);
        return ResponseBean.success(data);
    }

    public List<SharePersonNum> selectActivitySharePersonNumExport(Integer orgId, LocalDate startDate, LocalDate endDate, String sharePersonType, String sharePersonName, String sharePhone, String activityId, String activityType, String buildingId, String sort, String colKey, List<TaPersonBuilding> taPersonBuildings, Integer userId){
        String sortType = "ascend".equals(sort) ? "asc" : "desc";
        return taActicityStaticMapper.selectActivitySharePersonNumExport(orgId, LocalDateTime.of(startDate, LocalTime.MAX), LocalDateTime.of(endDate, LocalTime.MAX), sharePersonType, sharePersonName, sharePhone, activityId, activityType, buildingId, sortType, colKey, taPersonBuildings, userId);
    }

    public ResponseBean selectActivityShareNum(Integer pageNum, Integer pageSize, Integer orgId, LocalDate startDate, LocalDate endDate, String sharePersonType, String sharePersonName, String sharePhone, String activityId, String activityType, String personId, String buildingId, String sort, String colKey, List<TaPersonBuilding> taPersonBuildings, Integer userId){
        IPage<ShareNum> iPage = new Page<>(pageNum, pageSize);
        String sortType = "ascend".equals(sort) ? "asc" : "desc";

        IPage<ShareNum> data = taActicityStaticMapper.selectActivityShareNum(iPage, orgId, LocalDateTime.of(startDate, LocalTime.MAX), LocalDateTime.of(endDate, LocalTime.MAX), sharePersonType, sharePersonName, sharePhone, activityId, activityType, personId, buildingId, sortType, colKey, taPersonBuildings, userId);
        return ResponseBean.success(data);
    }

    public List<ShareNum> selectActivityShareNumExport(Integer orgId, LocalDate startDate, LocalDate endDate, String sharePersonType, String sharePersonName, String sharePhone, String activityId, String activityType, String personId, String buildingId, String sort, String colKey, List<TaPersonBuilding> taPersonBuildings, Integer userId){
        String sortType = "ascend".equals(sort) ? "asc" : "desc";
        return taActicityStaticMapper.selectActivityShareNumExport( orgId, LocalDateTime.of(startDate, LocalTime.MAX), LocalDateTime.of(endDate, LocalTime.MAX), sharePersonType, sharePersonName, sharePhone, activityId, activityType, personId, buildingId, sortType, colKey, taPersonBuildings, userId);
    }

    public ResponseBean selectActivityAddRegistNum(Integer pageNum, Integer pageSize, Integer orgId, LocalDate startDate, LocalDate endDate, String personFrom, String activityId, String activityType, String buildingId, String province, List<TaPersonBuilding> taPersonBuildings, Integer userId){
        IPage<AddRegistNum> iPage = new Page<>(pageNum, pageSize);
        IPage<AddRegistNum> data = taActicityStaticMapper.selectActivityAddRegistNum(iPage, orgId, LocalDateTime.of(startDate, LocalTime.MAX), LocalDateTime.of(endDate, LocalTime.MAX), personFrom, activityId, activityType, buildingId, province, taPersonBuildings, userId);
        return ResponseBean.success(data);
    }

    public List<AddRegistNum> selectActivityAddRegistNumExport(Integer orgId, LocalDate startDate, LocalDate endDate, String personFrom, String activityId, String activityType, String buildingId, String province, List<TaPersonBuilding> taPersonBuildings, Integer userId){
        return taActicityStaticMapper.selectActivityAddRegistNumExport(orgId, LocalDateTime.of(startDate, LocalTime.MAX), LocalDateTime.of(endDate, LocalTime.MAX), personFrom, activityId, activityType, buildingId, province, taPersonBuildings, userId);
    }

    public ResponseBean selectActivityVisitNum(Integer pageNum, Integer pageSize, Integer orgId, LocalDate startDate, LocalDate endDate, String personFrom, String provice, String realtyConsultant, String realtyConsultantPhone, String activityId, String activityType, String personId, String buildingId){
        IPage<VisitNum> iPage = new Page<>(pageNum, pageSize);
        IPage<VisitNum> data;
        if (StringUtils.isEmpty(personId)){
            data = taActicityStaticMapper.selectActivityVisitNum(iPage, orgId, LocalDateTime.of(startDate, LocalTime.MAX), LocalDateTime.of(endDate, LocalTime.MAX), personFrom, provice, realtyConsultant, realtyConsultantPhone, activityId, activityType, buildingId);
        }else{
            data = taActicityStaticMapper.selectActivityVisitNumByPersonId(iPage, orgId, LocalDateTime.of(startDate, LocalTime.MAX), LocalDateTime.of(endDate, LocalTime.MAX), personId, activityId, activityType, buildingId);
        }

        return ResponseBean.success(data);
    }

    public List<VisitNum> selectActivityVisitNumExport(Integer orgId, LocalDate startDate, LocalDate endDate, String personFrom, String provice, String realtyConsultant, String realtyConsultantPhone, String activityId, String activityType, String personId, String buildingId){
        List<VisitNum> list = new ArrayList<>();
        if (StringUtils.isEmpty(personId)){
            list = taActicityStaticMapper.selectActivityVisitNumExport(orgId, LocalDateTime.of(startDate, LocalTime.MAX), LocalDateTime.of(endDate, LocalTime.MAX), personFrom, provice, realtyConsultant, realtyConsultantPhone, activityId, activityType, buildingId);
        }else{
            list = taActicityStaticMapper.selectActivityVisitNumExportByPersonId(orgId, LocalDateTime.of(startDate, LocalTime.MAX), LocalDateTime.of(endDate, LocalTime.MAX), personId, activityId, activityType, buildingId);
        }
        return list;
    }

    public ResponseBean selectActivityVisitPersonNum(Integer pageNum, Integer pageSize, Integer orgId, LocalDate startDate, LocalDate endDate, String personFrom, String provice, String realtyConsultant, String realtyConsultantPhone, String activityId, String activityType, String buildingId, String personId){
        IPage<VisitPersonNum> iPage = new Page<>(pageNum, pageSize);
        IPage<VisitPersonNum> data;
        if (StringUtils.isEmpty(personId)){
            data = taActicityStaticMapper.selectActivityVisitPersonNum(iPage, orgId, LocalDateTime.of(startDate, LocalTime.MAX), LocalDateTime.of(endDate, LocalTime.MAX), personFrom, provice, realtyConsultant, realtyConsultantPhone, activityId, activityType, buildingId);
        }else{
            data = taActicityStaticMapper.selectActivityVisitPersonNumByPersonId(iPage, orgId, LocalDateTime.of(startDate, LocalTime.MAX), LocalDateTime.of(endDate, LocalTime.MAX), personId, activityId, activityType, buildingId);
        }

        return ResponseBean.success(data);
    }

    public List<VisitPersonNum> selectActivityVisitPersonNumExport(Integer orgId, LocalDate startDate, LocalDate endDate, String personFrom, String provice, String realtyConsultant, String realtyConsultantPhone, String activityId, String activityType, String buildingId, String personId){
        List<VisitPersonNum> list = new ArrayList<>();
        if (StringUtils.isEmpty(personId)){
            list = taActicityStaticMapper.selectActivityVisitPersonNumExport(orgId, LocalDateTime.of(startDate, LocalTime.MAX), LocalDateTime.of(endDate, LocalTime.MAX), personFrom, provice, realtyConsultant, realtyConsultantPhone, activityId, activityType, buildingId);
        }else{
            list = taActicityStaticMapper.selectActivityVisitPersonNumExportByPersonId(orgId, LocalDateTime.of(startDate, LocalTime.MAX), LocalDateTime.of(endDate, LocalTime.MAX), personId, activityId, activityType, buildingId);
        }
        return list;
    }

}
