package com.persagy.proxy.object.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.dmp.basic.model.QueryCriteria;
import com.persagy.dmp.common.helper.SpringHelper;
import com.persagy.proxy.adm.constant.GraphCodeEnum;
import com.persagy.proxy.adm.constant.RelCodeEnum;
import com.persagy.proxy.adm.request.AdmQueryCriteria;
import com.persagy.proxy.adm.request.AdmResponse;
import com.persagy.proxy.adm.service.impl.AbstractAdmBaseServiceImpl;
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.model.AdmSystem;
import com.persagy.proxy.object.service.IAdmEquipmentService;
import com.persagy.proxy.object.service.IAdmShaftService;
import com.persagy.proxy.object.service.IAdmSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * ADM 竖井 实现类
 * @author lvxy
 * @date 2021/8/18
 */
@Service
public class AdmShaftServiceImpl extends AbstractAdmBaseServiceImpl<AdmShaft> implements IAdmShaftService {
    @Autowired
    private IAdmEquipmentService equipmentService;
    @Autowired
    private IAdmSystemService systemService;

    @Override
    public void ensureSpecialValues(InstanceUrlParam context, List<AdmShaft> vos) {
        if(CollUtil.isEmpty(vos)) {
            return;
        }
        //收集设备对象、系统对象
        List<AdmEquipment> equipmentList = new ArrayList<>();
        List<AdmSystem> systemList = new ArrayList<>();
        vos.stream().forEach(admShaft -> {
            if(CollUtil.isNotEmpty(admShaft.getEquipmentList())){
                equipmentList.addAll(admShaft.getEquipmentList());
            }
            if(CollUtil.isNotEmpty(admShaft.getSystemList())){
                systemList.addAll(admShaft.getSystemList());
            }
        });
        equipmentService.ensureSpecialValues(AdmContextUtil.toDmpContext(), equipmentList);
        systemService.ensureSpecialValues(AdmContextUtil.toDmpContext(), systemList);
    }

    @Override
    public void processDmpCriteria(QueryCriteria dmpRequest) {
        ObjectNode criteria = dmpRequest.getCriteria();
        if (null==criteria){
            return;
        }
        if (criteria.has("buildingId")){
            // 有建筑id过滤
            ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
            ObjectNode relationTo = objectMapper.createObjectNode();
            relationTo.put("graphCode", GraphCodeEnum.ArchSubset.name());
            relationTo.put("relCode", RelCodeEnum.Bd2Sh.name());
            if(!criteria.get("buildingId").toString().contains("$null")) {
                relationTo.set("objFrom",criteria.get("buildingId"));
            }else{
                relationTo.put("$bindFlag", false);
            }
            criteria.set("relationTo",relationTo);
            criteria.remove("buildingId");
        }
    }

    @Override
    public AdmResponse doQuery(InstanceUrlParam context, AdmQueryCriteria request, Class<AdmShaft> clazz) {
        AdmResponse response = super.doQuery(context, request, clazz);
        // 系统需要补充分类名称属性
        ensureSpecialValues(context, (List<AdmShaft>) response.getContent());
        return response;
    }
}
