package com.persagy.proxy.report.controller;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.persagy.proxy.adm.constant.AdmObjectType;
import com.persagy.proxy.adm.constant.AdmRelationTypeEnum;
import com.persagy.proxy.adm.constant.SpaceTypeEnum;
import com.persagy.proxy.adm.request.AdmCountResponse;
import com.persagy.proxy.adm.request.AdmQueryCriteria;
import com.persagy.proxy.adm.request.AdmResponse;
import com.persagy.proxy.adm.utils.AdmContextUtil;
import com.persagy.proxy.common.entity.InstanceUrlParam;
import com.persagy.proxy.object.model.AdmEquipment;
import com.persagy.proxy.object.model.AdmShaft;
import com.persagy.proxy.object.service.IAdmEquipmentService;
import com.persagy.proxy.object.service.IAdmShaftService;
import com.persagy.proxy.report.service.IRelationReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统计接口
 * 
 * @version 1.0.0
 * @company persagy 
 * @author zhangqiankun
 * @date 2021年9月8日 下午7:51:56
 */
@RestController
@RequestMapping("/statistics")
public class ComponentStatisticsController {

	@Autowired
	private IRelationReportService relationReportService;
	@Autowired
	private IAdmShaftService shaftService;
	
	/**
	 * 台账中待删除的对象
	 * <p>条件："filters": "taskState = -1"</p>
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/object-delete-count")
	public AdmCountResponse objectDeleteCount(@RequestBody JSONObject params, HttpServletRequest request) {
		return AdmCountResponse.success(0L);
	}
	
	/**
	 * 不能同步到物理世界的关系数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/unsyn-relation-count")
	public AdmCountResponse unsynRelationCount(HttpServletRequest request) {
		return AdmCountResponse.success(0L);
	}
	
	/**
	 * 岗位和关联的资产信息点值不一致
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/eq2pe-count")
	public AdmCountResponse eq2peCount(HttpServletRequest request) {
		return AdmCountResponse.success(0L);
	}
	
	/**
	 * 岗位和关联的资产信息点值不一致的数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/eq2pe-different")
	public AdmCountResponse eq2peDifferent(HttpServletRequest request) {
		return AdmCountResponse.success(0L);
	}
	
	/**
	 * 设备-未关联资产的设备，这里返回所有的设备
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/uneq2pe-count")
	public AdmCountResponse uneq2peCount(HttpServletRequest request) {
		InstanceUrlParam context = AdmContextUtil.toDmpContext();
		long count = this.relationReportService.countObject(context.getGroupCode(), context.getProjectId(), AdmObjectType.EQUIPMENT.getIndex(), null);
		return AdmCountResponse.success(count);
	}
	
	/**
	 * 业务空间-各分区占比
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/sp-objecttype-count")
	public AdmResponse spObjectTypeCount(HttpServletRequest request) {
		InstanceUrlParam context = AdmContextUtil.toDmpContext();
		List<Map<String,Object>> list = this.relationReportService.queryObjectCountByObjInfos(context.getGroupCode(), null,
				Sets.newHashSet(AdmObjectType.SPACE.getIndex()), Lists.newArrayList("classCode"));
		for (Map<String, Object> map : list) {
//			map.put("name", map.get("classCode"));
			map.put("name", SpaceTypeEnum.valueOf((String)map.get("classCode")).getDesc());
		}
		return AdmResponse.success(list);
	}
	
	/**
	 * 业务空间-默认分区功能类型占比
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/general-sp-functype-count")
	public AdmResponse generalSpFunctypeCount(HttpServletRequest request) {
		InstanceUrlParam context = AdmContextUtil.toDmpContext();
		List<Map<String,Object>> list = this.relationReportService.queryObjectCountByObjInfos(context.getGroupCode(),
				Sets.newHashSet(SpaceTypeEnum.GeneralZone.getCode()),
				Sets.newHashSet(AdmObjectType.SPACE.getIndex()), Lists.newArrayList("roomFuncType"));
		for (Map<String, Object> map : list) {
			map.put("name", map.get("roomFuncType"));
		}
		return AdmResponse.success(list);
	}
	
	/**
	 * 租户-关联业务空间完成度
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/tenant2sp-count")
	public AdmCountResponse tenant2spCount(HttpServletRequest request) {
		InstanceUrlParam context = AdmContextUtil.toDmpContext();
		long count = this.relationReportService.countRelationObject(context.getGroupCode(), context.getProjectId(),
				AdmRelationTypeEnum.TN2SP.getGraphCode(),
				AdmRelationTypeEnum.TN2SP.getRelCode(), null);
		return AdmCountResponse.success(count);
	}
	
	/**
	 * 竖井-关联业务空间完成度
	 * @return
	 */
	@RequestMapping(value = "/shaft2sp-count")
	public AdmResponse shaft2spCount() {
		AdmQueryCriteria request = new AdmQueryCriteria();
		request.setName("shaft");
		request.setOnlyCount(true);
		request.resetRelationCond(true, AdmRelationTypeEnum.SH2SP.getGraphCode(),
				AdmRelationTypeEnum.SH2SP.getRelCode(), null);
		return shaftService.doQuery(AdmContextUtil.toDmpContext(), request, AdmShaft.class);
	}
	
	/**
	 * 竖井-按功能类型占比
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/shaft-functype-count")
	public AdmResponse shaftFunctypeCount(HttpServletRequest request) {
		InstanceUrlParam context = AdmContextUtil.toDmpContext();
		List<Map<String,Object>> list = this.relationReportService.queryObjectCountByObjInfos(context.getGroupCode(), null,
				Sets.newHashSet(AdmObjectType.SHAFT.getIndex()), Lists.newArrayList("shaftFuncType"));
		for (Map<String, Object> map : list) {
			map.put("name", map.get("shaftFuncType"));
		}
		return AdmResponse.success(list);
	}

	@Autowired
	private IAdmEquipmentService equipmentService;

	/**
	 * 设备所在空间占比统计
	 * @return
	 */
	@RequestMapping(value = "/eq2sp")
	public AdmResponse queryEquip2Space() {
		InstanceUrlParam context = AdmContextUtil.toDmpContext();
		// 查询设备数量
		AdmQueryCriteria criteria = new AdmQueryCriteria();
		criteria.setName(AdmEquipment.OBJ_TYPE);
		criteria.setOnlyCount(true);
		AdmResponse rs = equipmentService.query(criteria);
		long equipCount = rs.getCount() == null ? 0L : rs.getCount().longValue();
		// 查询关系数量
		long relCount = relationReportService.countRelationObject(context.getGroupCode(), context.getProjectId(),
				AdmRelationTypeEnum.EQ2SP_IN.getGraphCode(),AdmRelationTypeEnum.EQ2SP_IN.getRelCode(), "GeneralZone");
		// 组装返回结果
		Map<String, Long> countMap = new HashMap<>(2);
		countMap.put("equipCount", equipCount);
		countMap.put("relCount", relCount);
		return AdmResponse.success(CollUtil.newArrayList(countMap));
	}
}
