package com.njcn.device.pms.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.device.pms.mapper.SubstationRelationMapper;
import com.njcn.device.pms.pojo.param.SubstationExpendParam;
import com.njcn.device.pms.pojo.po.SubstationExpend;
import com.njcn.device.pms.pojo.po.SubstationRelation;
import com.njcn.device.pms.pojo.vo.SubstationRelationVO;
import com.njcn.device.pms.service.ISubstationExpendService;
import com.njcn.device.pms.service.ISubstationRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 2.电网拓扑图关系表 服务实现类
 * </p>
 *
 * @author zhuxinyu
 * @since 2023-05-08
 */
@Service
@RequiredArgsConstructor
public class SubstationRelationServiceImpl extends ServiceImpl<SubstationRelationMapper, SubstationRelation> implements ISubstationRelationService {

    private final ISubstationExpendService substationExpendService;
    @Override
    public Boolean saveSubstationRelation(SubstationRelation param) {
        if(StrUtil.isNotBlank(param.getId())){
            return this.updateById(param);
        }
        return this.save(param);
    }

    @Override
    public SubstationRelationVO getRelation(SubstationExpendParam.Relation param) {
        SubstationRelationVO relationVO=new SubstationRelationVO();
        SubstationRelation one = this.getOne(new LambdaQueryWrapper<SubstationRelation>()
                .and(w->w
                        .eq(SubstationRelation::getStartStationId, param.getStartStationId())
                        .eq(SubstationRelation::getEndStationId, param.getEndStationId())
                )
                .or(w -> w
                    .eq(SubstationRelation::getStartStationId, param.getEndStationId())
                    .eq(SubstationRelation::getEndStationId, param.getStartStationId())
                )
        );
        if(ObjectUtil.isNotNull(one)){
            BeanUtil.copyProperties(one, relationVO);
            relationVO.setStartStation(getSubstationExpend(param.getStartStationId()));
            relationVO.setEndStation(getSubstationExpend(param.getEndStationId()));
        }
        return relationVO;
    }

    public SubstationExpend getSubstationExpend(String id){
      return   substationExpendService.getOne(new LambdaQueryWrapper<SubstationExpend>()
                .eq(SubstationExpend::getSubstationId, id)
        );
    }
}
