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

import com.huiju.estateagents.center.taUser.entity.TaUser;
import com.huiju.estateagents.property.common.UserElement;
import com.huiju.estateagents.property.dao.IndexCountMapper;
import com.huiju.estateagents.property.model.*;
import com.huiju.estateagents.property.service.IndexCountServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FXF
 * @date 2019-05-28
 */
@Service("indexCountService")
public class IndexCountServiceImpl implements IndexCountServiceI {
	
	@Autowired
	private IndexCountMapper indexCountMapper;
	
	
	/**
	 * 获取首页统计所有数据
	 *
	 * @param userElement
	 * @return
	 */
	@Override
	public Map<String, Object> getIndexCount(TaUser userElement) {
		Map<String,Object> map = new HashMap<>();
		Integer communityId = userElement.getOrgId();
		
		//历史工单统计和今日工单统计
		Map<String,Object> ticketMap = getTicketMap(communityId);
		
		//历史工单统计表
		Map<String,Object> historyMap = new HashMap<>();
		historyMap.put("totalTicket",ticketMap.get("totalTicket"));
		historyMap.put("solvedTicket",ticketMap.get("solvedTicket"));
		historyMap.put("unsolvedTicket",ticketMap.get("unsolvedTicket"));
		
		//今日新增工单统计表
		Map<String,Object> todayMap = new HashMap<>();
		todayMap.put("todayRepairTicket",ticketMap.get("todayRepairTicket"));
		todayMap.put("todayComplaintTicket",ticketMap.get("todayComplaintTicket"));
		todayMap.put("todayContactTicket",ticketMap.get("todayContactTicket"));
		
		//今日小区智能硬件数据统计
		Map<String,Object> hardwareMap = getHardwareMap(communityId);
		
		//获取收费组总缴费统计
		List<TpBillInvoiceCount> billInvoiceCountList = indexCountMapper.getBillInvoiceCountList(communityId);
		
			//获取月租车总续费统计
		List<TaUserLicenseCount> taUserLicenseCountList = indexCountMapper.getUserLicenseCountList(communityId);
		
		//获取临时车总缴费统计
		List<TaTemporaryLicenseCount> taTemporaryLicenseCountList = indexCountMapper.getTemporaryLicenseCountList(communityId);
		
		//获取注册用户数数据
		Map<String,Object> registerUserMap = getRegisterUserMap(communityId);
		
		//获取日活跃用户
		List<TaBriskUser> taBriskUserList = indexCountMapper.getBriskUserList(communityId);
		
		//获取已发布内容统计
		Map<String,Object> publishContentMap = getPublishContentMap(communityId);
		
		map.put("historyMap",historyMap);
		map.put("todayMap",todayMap);
		map.put("hardwareMap",hardwareMap);
		map.put("billInvoiceCountList",billInvoiceCountList);
		map.put("taUserLicenseCountList",taUserLicenseCountList);
		map.put("taTemporaryLicenseCountList",taTemporaryLicenseCountList);
		map.put("registerUserMap",registerUserMap);
		map.put("taBriskUserList",taBriskUserList);
		map.put("publishContentMap",publishContentMap);
		return map;
	}
	
	/**
	 * 获取已发布内容统计
	 * @param communityId
	 * @return
	 */
	private Map<String, Object> getPublishContentMap(Integer communityId) {
		Map<String,Object> map = new HashMap<>();
		//获取公告数量
		Integer announcementCount = indexCountMapper.getAnnouncementCount(communityId);
		//获取活动数量
		Integer activityCount = indexCountMapper.getActivityCount(communityId);
		//获取话题数量
		Integer topicCount = indexCountMapper.getTopicCount(communityId);
		
		map.put("announcementCount",announcementCount);
		map.put("activityCount",activityCount);
		map.put("topicCount",topicCount);
		return map;
	}
	
	/**
	 * 获取注册用户数据
	 * @param communityId
	 * @return
	 */
	private Map<String, Object> getRegisterUserMap(Integer communityId) {
		Map<String,Object> map = new HashMap<>();
		// 1 是户主 2 是租客 3 是家属
		Integer houseOwnerCount = indexCountMapper.getRegisterUserCount(communityId,1);
		Integer houseTenantCount = indexCountMapper.getRegisterUserCount(communityId,2);
		Integer houseRelationCount = indexCountMapper.getRegisterUserCount(communityId,3);
		map.put("houseOwnerCount",houseOwnerCount);
		map.put("houseTenantCount",houseTenantCount);
		map.put("houseRelationCount",houseRelationCount);
		return map;
	}
	
	/**
	 * 获取硬件数据统计
	 * @param communityId
	 * @return
	 */
	private Map<String, Object> getHardwareMap(Integer communityId) {
		Map<String,Object> map = new HashMap<>();
		Integer result = indexCountMapper.selectCommunityOpenDoorCount();
		
		// 刷卡开门次数
		Integer brushCardUnitsOpenDoorCount = indexCountMapper.selectUnitsOpenDoorCount(983299);
		// 刷脸开门次数
		Integer brushFaceUunitsOpenDoorCount = indexCountMapper.selectUnitsOpenDoorCount(983309);
		
		Integer openCount = brushCardUnitsOpenDoorCount + brushFaceUunitsOpenDoorCount;
		
		//公区摄像头查看次数
		Integer publicCameraCount = indexCountMapper.getPublicCameraCount(communityId);
		//单元门远程监控查看
		Integer remoteMonitoringCount = indexCountMapper.getRemoteMonitoringCount(communityId);
		//单元门远程开门
		Integer remoteUnitCount = indexCountMapper.getRemoteUnitCount(communityId);
		
		map.put("communityCount",result);
		map.put("unitCount",openCount);
		map.put("visitorCount",0);
		map.put("publicCameraCount",publicCameraCount);
		map.put("remoteMonitoringCount",remoteMonitoringCount);
		map.put("remoteUnitCount",remoteUnitCount);
		return map;
	}
	
	/**
	 * 历史工单统计
	 */
	private Map<String, Object> getTicketMap(Integer communityId) {
		Map<String,Object> Map = new HashMap<>();
		List<TpTicket> ticketList = indexCountMapper.getTicketList(communityId);
		//已解决：状态为待评价+已评价+拒绝受理+被终止的工单总和
		List<TpTicket> solvedTicketList = new ArrayList<>();
		//未解决：等待分配 + 待处理 +正在处理
		List<TpTicket> unsolvedTicketList = new ArrayList<>();
		//今日新增报修单
		List<TpTicket> todayRepairTicketList = new ArrayList<>();
		//今日新增投诉单
		List<TpTicket> todayComplaintTicketList = new ArrayList<>();
		//今日新增联系单
		List<TpTicket> todayContactTicketList = new ArrayList<>();
		//格式化当天时间
		LocalDate todayData = LocalDate.now();
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		String todayDataStr= dtf.format(todayData);
		
		for (TpTicket ticket : ticketList) {
			//已解决：状态为待评价+已评价+拒绝受理+被终止的工单总和
			if (ticket.getStatus().equals("4") || ticket.getStatus().equals("5")
					|| ticket.getStatus().equals("1") || ticket.getStatus().equals("6")){
				solvedTicketList.add(ticket);
			}
			//未解决：等待分配 + 待处理 +正在处理
			if (ticket.getStatus().equals("0") || ticket.getStatus().equals("2") || ticket.getStatus().equals("3")){
				unsolvedTicketList.add(ticket);
			}
			//获取当天时间的数据
			if (dtf.format(ticket.getCreateDate()).equals(todayDataStr)){
				//1:投诉 2:报修 3:联系物业
				switch(ticket.getType()){
					case "1" :
						todayComplaintTicketList.add(ticket);
						break;
					case "2" :
						todayRepairTicketList.add(ticket);
						break;
					case "3" :
						todayContactTicketList.add(ticket);
						break;
				}
			}
		}
		Map.put("totalTicket",ticketList.size());
		Map.put("solvedTicket",solvedTicketList.size());
		Map.put("unsolvedTicket",unsolvedTicketList.size());
		Map.put("todayRepairTicket",todayRepairTicketList.size());
		Map.put("todayComplaintTicket",todayComplaintTicketList.size());
		Map.put("todayContactTicket",todayContactTicketList.size());
		return Map;
	}
}
