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

import cn.hutool.core.collection.CollUtil;
import com.persagy.dmp.basic.dto.DefineRequestData;
import com.persagy.dmp.common.constant.CommonConstant;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.define.client.DigitalEquipClassRelFacade;
import com.persagy.dmp.define.entity.DigitalEquipClassRel;
import com.persagy.proxy.calculation.model.AdmMepSourceTypeEntity;
import com.persagy.proxy.adm.request.AdmCreateRequest;
import com.persagy.proxy.adm.request.AdmResponse;
import com.persagy.proxy.calculation.service.AdmMepSystemCalcService;
import com.persagy.proxy.common.entity.InstanceUrlParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/***
 * Description: 源末端关系设置逻辑处理类
 * @author : lijie
 * @date :2021/9/14 23:21
 * Update By lijie 2021/9/14 23:21
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class AdmMepSystemCalcServiceImpl implements AdmMepSystemCalcService {
    /***
     * Description: 添加源末端关系
     * @param context : 请求上下文
     * @param request : 请求参数
     * @return : com.persagy.proxy.adm.request.AdmResponse
     * @author : lijie
     * @date :2021/9/14 23:23
     * Update By lijie 2021/9/14 23:23
     */
    @Override
    public AdmResponse add(InstanceUrlParam context, AdmCreateRequest<AdmMepSourceTypeEntity> request) {
        List<AdmMepSourceTypeEntity> content = request.getContent();
        if (CollUtil.isEmpty(content)){
            return AdmResponse.success(new ArrayList<>());
        }
        List<DigitalEquipClassRel> relList = new ArrayList<>();
        content.forEach(entity->{
            DigitalEquipClassRel rel = new DigitalEquipClassRel();
            rel.setProjectId(context.getProjectId());
            rel.setCalcName(entity.getCalcName());
            rel.setSourceEquipCode(entity.getSourceType());
            relList.add(rel);
        });
        DigitalEquipClassRelFacade
                .createBatch(context.getGroupCode(), context.getProjectId(),
                        context.getAppId(), CommonConstant.DEFAULT_ID, relList);
        return AdmResponse.success(content);
    }
    /***
     * Description: 查询源端设备类型
     * @param context : 参数
     * @param calcName : 关系名称
     * @return : com.persagy.proxy.adm.request.AdmResponse
     * @author : lijie
     * @date :2021/9/14 23:20
     * Update By lijie 2021/9/14 23:20
     */
    @Override
    public AdmResponse query(InstanceUrlParam context, String calcName) {
        // 先根据边类型名称查询图类型编码及边类型编码
        CommonResult<List<DigitalEquipClassRel>> result = DigitalEquipClassRelFacade
                .queryListByRelNames(context.getGroupCode(), context.getProjectId(), context.getAppId(),
                CommonConstant.DEFAULT_ID, DefineRequestData.builder().relNames(CollUtil.newHashSet(calcName)).build());
        List<DigitalEquipClassRel> data = result.getData();
        if (CollUtil.isEmpty(data)){
            return AdmResponse.success(new ArrayList<>());
        }
        // 组装ADM数据
        List<AdmMepSourceTypeEntity> resultData = new ArrayList<>();
        data.forEach(digitalEquipClassRel -> {
            resultData.add(AdmMepSourceTypeEntity.builder()
                .calcName(digitalEquipClassRel.getCalcName())
                .sourceType(digitalEquipClassRel.getSourceEquipCode())
                .projectId(digitalEquipClassRel.getProjectId())
                .createTime(digitalEquipClassRel.getCreationTime().getDate())
                .build());
        });
        return AdmResponse.success(resultData);
    }
}
