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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.device.line.service.LineService;
import com.njcn.device.pq.enums.LineBaseEnum;
import com.njcn.device.pq.mapper.PqSubstationExpendMapper;
import com.njcn.device.pq.pojo.param.PqSubstationExpendParam;
import com.njcn.device.pq.pojo.po.Line;
import com.njcn.device.pq.pojo.po.PqSubstationExpend;
import com.njcn.device.pq.pojo.po.Substation;
import com.njcn.device.pq.pojo.vo.PqSubstationExpendVO;
import com.njcn.device.pq.service.ISubstationService;
import com.njcn.device.pq.service.PqSubstationExpendService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: cdf
 * @CreateTime: 2026-07-29
 * @Description: 变电站扩展信息
 */
@Service
@RequiredArgsConstructor
public class PqSubstationExpendServiceImpl extends ServiceImpl<PqSubstationExpendMapper, PqSubstationExpend>
        implements PqSubstationExpendService {

    private final LineService lineService;

    private final ISubstationService iSubstationService;

    @Override
    public Page<PqSubstationExpendVO> pageJoin(IPage<PqSubstationExpendVO> page, PqSubstationExpendParam param) {
        return baseMapper.pageJoin(page, param);
    }

    @Override
    public PqSubstationExpendVO getByIdVO(String id) {
        return baseMapper.getByIdVO(id);
    }

    @Override
    public List<PqSubstationExpendVO> listVO() {
        return baseMapper.listVO();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean save(PqSubstationExpendParam param) {
        Line line = new Line();
        line.setId(param.getId());
        line.setName(param.getName());
        line.setPid(param.getPid());
        line.setLevel(LineBaseEnum.SUB_LEVEL.getCode());

        Line gd = lineService.lambdaQuery().eq(Line::getId, param.getPid()).eq(Line::getState, DataStateEnum.ENABLE.getCode()).one();
        line.setPids(gd.getPids()+","+param.getId());
        line.setSort(0);
        line.setState(DataStateEnum.ENABLE.getCode());
        lineService.saveOrUpdate(line);

        Substation substation = new Substation();
        substation.setId(param.getId());
        substation.setLng(param.getLng());
        substation.setLat(param.getLat());
        substation.setScale(param.getScale());
        iSubstationService.saveOrUpdate(substation);


        PqSubstationExpend expend = new PqSubstationExpend();
        expend.setId(param.getId());
        expend.setLoadCap(param.getLoadCap());
        expend.setAddress(param.getAddress());
        expend.setOperateDate(param.getOperateDate());
        expend.setPlantType(param.getPlantType());
        expend.setOperateState(param.getOperateState());
        expend.setReportUrl(param.getReportUrl());
        this.saveOrUpdate(expend);
        return true;
    }
}