package com.persagy.proxy.report.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.persagy.proxy.object.model.AdmBuilding;
import com.persagy.proxy.object.model.AdmFloor;
import com.persagy.proxy.adm.request.AdmQueryCriteria;
import com.persagy.proxy.adm.request.AdmResponse;
import com.persagy.proxy.object.service.IAdmBuildingService;
import com.persagy.proxy.object.service.IAdmFloorService;
import com.persagy.proxy.adm.utils.AdmContextUtil;
import com.persagy.proxy.common.entity.InstanceUrlParam;
import com.persagy.proxy.object.service.impl.AdmFloorServiceImpl;
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 java.util.ArrayList;
import java.util.List;

/**
 * 建筑报表Controller
 * @version 1.0.0
 * @company persagy 
 * @author zhangqiankun
 * @date 2021年9月8日 下午7:28:00
 */
@RestController
@SuppressWarnings("unchecked")
public class ComponentBuildingController {

	@Autowired
	private IAdmBuildingService service;
	@Autowired
	private IAdmFloorService floorService;
	
	/**
	 * 建筑查询，这里查楼层都是从属关系，而非服务关系
	 * @param request
	 * @return
	 */
	@RequestMapping("/building/query")
	public AdmResponse queryBuilding(@RequestBody AdmQueryCriteria request) {
		InstanceUrlParam context = AdmContextUtil.toDmpContext();
		request.setName(AdmBuilding.OBJ_TYPE);
		// 级联查询有模型图的楼层
		AdmQueryCriteria cascade = new AdmQueryCriteria();
		cascade.setName("floor");
		List<AdmQueryCriteria> cascades = request.getCascade();
		if(CollUtil.isEmpty(cascades)) {
			cascades = CollUtil.newArrayList(cascade);
			request.setCascade(cascades);
		} else {
			cascades.add(cascade);
		}
		AdmResponse result = service.doQuery(context, request, AdmBuilding.class);
		calculateModelUsedCount(context, (List<AdmBuilding>) result.getContent());
		return result;
	}

	/**
	 * 统计建筑里所有楼层模型总计被使用次数
	 * @param context
	 * @param buildings
	 */
	private void calculateModelUsedCount(InstanceUrlParam context, List<AdmBuilding> buildings) {
		if(CollUtil.isEmpty(buildings)) {
			return;
		}
		// 按建筑计算模型数量 - 这里先这样写，这种统计不太合理，统计建筑里所有楼层模型总计被使用次数。确认是否需要后再考虑是否移除
		// 收集建筑下的楼层，处理信息点
		List<AdmFloor> admFloors = new ArrayList<>(16);
		for(AdmBuilding building:buildings) {
			List<AdmFloor> floors = building.getFloor();
			if(CollUtil.isEmpty(floors)) {
				continue;
			}
			admFloors.addAll(floors);
			// 收集所有模型Id
			List<Object> modelIds = CollUtil.getFieldValues(floors, "modelId", true);
			if(CollUtil.isEmpty(modelIds)) {
				continue;
			}
			// 查询模型使用次数
			AdmQueryCriteria floorCriteria = new AdmQueryCriteria();
			String inSql = CollUtil.join(modelIds, StrUtil.COMMA,"'","'");
			floorCriteria.setFilters(" modelId in (" + inSql + ") ");
			floorCriteria.setOnlyCount(true);
			AdmResponse floorRs = floorService.query(floorCriteria);
			// 设置次数
			building.setCount(floorRs.getCount());
		}
		floorService.ensureSpecialValues(AdmContextUtil.toDmpContext(), admFloors, AdmFloorServiceImpl.QUERY);
	}
}
