package com.persagy.adm.server.algorithm.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.adm.server.algorithm.constant.AdmServerConstant;
import com.persagy.adm.server.algorithm.dao.AdmModelFileMapper;
import com.persagy.adm.server.algorithm.entity.AdmModelFile;
import com.persagy.adm.server.algorithm.service.AdmModelService;
import com.persagy.dmp.basic.model.QueryCriteria;
import com.persagy.dmp.common.constant.CommonConstant;
import com.persagy.dmp.common.constant.DigitalObjectType;
import com.persagy.dmp.common.constant.GraphCodeEnum;
import com.persagy.dmp.common.constant.RelCodeEnum;
import com.persagy.dmp.common.context.AppContext;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.common.helper.SpringHelper;
import com.persagy.dmp.common.model.entity.AuditableEntity;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.delivery.client.DigitalObjectTypeCollectFacade;
import com.persagy.dmp.delivery.dto.ObjectTypeCollectDTO;
import com.persagy.dmp.delivery.entity.ObjectTypeCollect;
import com.persagy.dmp.digital.client.DigitalObjectFacade;
import com.persagy.dmp.digital.entity.ObjectDigital;
import com.persagy.dmp.digital.entity.ObjectRelation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 模型文件的逻辑处理类
 * @author : lijie
 * Update By 2022/1/12 14:08
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class AdmModelServiceImpl implements AdmModelService {

    private final AdmModelFileMapper admModelFileMapper;

    /**
     * 根据建筑id查询建筑下的所有楼层
     * @return : java.util.List<com.persagy.dmp.digital.entity.ObjectDigital>
     * @author : lijie
     * Update By 2022/1/19 10:04
     */
    @Override
    public List<ObjectDigital> queryBuildFloorsByProjectId() {
        // 构建查询参数,valid为true,并且是该建筑下的.且按floorSequenceID正序排序
        QueryCriteria queryCriteria = new QueryCriteria();
        ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
        ObjectNode objectNode = objectMapper.createObjectNode();
        objectNode.put(ObjectDigital.PROP_VALID,Boolean.TRUE);
        objectNode.putPOJO(ObjectDigital.OBJ_TYPE_HUM, CollUtil.newArrayList(DigitalObjectType.FLOOR.getIndex(),
                DigitalObjectType.BUILDING.getIndex()));
        queryCriteria.setCriteria(objectNode);
        CommonResult<List<ObjectDigital>> commonResult = DigitalObjectFacade.queryObject(AppContext.getContext().getGroupCode(),
                AppContext.getContext().getProjectId(), AppContext.getContext().getAppId(),
                AppContext.getContext().getAccountId(), queryCriteria);
        if (!CommonConstant.QUERY_SUCCESS.equals(commonResult.getResult())){
            throw new BusinessException(commonResult.getResult(),commonResult.getMessage());
        }
        if (CollUtil.isEmpty(commonResult.getData())){
            return new ArrayList<>();
        }
        return commonResult.getData();
    }
    /**
     * 根据楼层id集合查询当前的模型文件信息
     * @param floorIds : 楼层id集合
     * @return : java.util.Map<java.lang.String,com.persagy.adm.server.algorithm.entity.AdmModelFile>
     * @author : lijie
     * Update By 2022/1/19 14:34
     */
    @Override
    public Map<String, AdmModelFile> queryCurrentModelFileByFloorIds(List<String> floorIds) {
        LambdaQueryWrapper<AdmModelFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AdmModelFile::getFloorId,floorIds);
        queryWrapper.eq(AdmModelFile::getValid,Boolean.TRUE);
        queryWrapper.eq(AdmModelFile::getRemoved,Boolean.FALSE);
        List<AdmModelFile> admModelFiles = admModelFileMapper.queryCurrentModelFileByFloorIds(queryWrapper);
        if (CollUtil.isEmpty(admModelFiles)){
            return CollUtil.newHashMap();
        }
        return admModelFiles.stream().collect(Collectors.toMap(AdmModelFile::getFloorId,admModelFile -> admModelFile,(k1,k2)->k1));
    }
    /**
     * 新增模型文件
     * @param modelFile : 模型文件信息
     * return : void
     * @author : lijie
     * Update By 2022/1/19 17:54
     */
    @Override
    public void saveModelFile(AdmModelFile modelFile) {
        admModelFileMapper.insertEntity(modelFile);
    }
    /**
     * 根据正逆向交付查询对象类型交付范围
     * @param deliveryWay : 正逆向交付方式,0-逆向,1-正向
     * @return : java.util.List<com.persagy.dmp.delivery.dto.ObjectTypeCollectDTO>
     * @author : lijie
     * Update By 2022/1/20 18:26
     */
    @Override
    public List<ObjectTypeCollect> queryObjectTypeCollectByDeliveryWay(Integer deliveryWay) {
        if (null==deliveryWay){
            return new ArrayList<>();
        }
        QueryCriteria queryCriteria = new QueryCriteria();
        ObjectMapper objectMapper = SpringHelper.getBean(ObjectMapper.class);
        ObjectNode objectNode = objectMapper.createObjectNode();
        objectNode.put(AuditableEntity.PROP_VALID,Boolean.TRUE);
        objectNode.put(CommonConstant.QUERY_PROJECTID,AppContext.getContext().getProjectId());
        objectNode.put(CommonConstant.QUERY_GROUPCODE,AppContext.getContext().getGroupCode());
        objectNode.put(AdmServerConstant.DELIVERY_WAY,deliveryWay);
        objectNode.put(AdmServerConstant.DELIVERY_TYPE,"0");
        objectNode.put(AdmServerConstant.IS_DELIVERY,"1");
        queryCriteria.setCriteria(objectNode);
        CommonResult<List<ObjectTypeCollect>> commonResult = DigitalObjectTypeCollectFacade
                .queryPrototype(AppContext.getContext().getGroupCode(), AppContext.getContext().getProjectId(),
                        AppContext.getContext().getAppId(), AppContext.getContext().getAccountId(), queryCriteria);
        if (!CommonConstant.QUERY_SUCCESS.equals(commonResult.getResult())){
            throw new BusinessException(commonResult.getResult(),commonResult.getMessage());
        }
        if (CollUtil.isNotEmpty(commonResult.getData())){
            return commonResult.getData();
        }
        // 暂时加个逻辑,如果没有就查询0
        objectNode.put(CommonConstant.QUERY_PROJECTID,CommonConstant.DEFAULT_ID);
        objectNode.put(CommonConstant.QUERY_GROUPCODE,CommonConstant.DEFAULT_ID);
        CommonResult<List<ObjectTypeCollect>> zeroCommonResult = DigitalObjectTypeCollectFacade
                .queryPrototype(AppContext.getContext().getGroupCode(), AppContext.getContext().getProjectId(),
                        AppContext.getContext().getAppId(), AppContext.getContext().getAccountId(), queryCriteria);
        if (!CommonConstant.QUERY_SUCCESS.equals(zeroCommonResult.getResult())){
            throw new BusinessException(zeroCommonResult.getResult(),zeroCommonResult.getMessage());
        }
        return zeroCommonResult.getData();
    }
}
