package com.zksy.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.base.domain.GasPipePoint;
import com.zksy.base.dto.GasPipePointPageInDTO;
import com.zksy.base.mapper.GasPipePointMapper;
import com.zksy.base.service.GasPipePointService;
import com.zksy.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class GasPipePointServiceImpl extends ServiceImpl<GasPipePointMapper, GasPipePoint>
        implements GasPipePointService {

    @Override
    public Page<GasPipePoint> queryByPage(GasPipePointPageInDTO dto) {
        Page<GasPipePoint> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<GasPipePoint> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(dto.getPointName() != null && !dto.getPointName().isEmpty(), GasPipePoint::getPointName, dto.getPointName());
        wrapper.like(dto.getPointCode() != null && !dto.getPointCode().isEmpty(), GasPipePoint::getPointCode, dto.getPointCode());
        wrapper.eq(dto.getNetworkId() != null && !dto.getNetworkId().isEmpty(), GasPipePoint::getNetworkId, dto.getNetworkId());
        wrapper.eq(dto.getPointType() != null && !dto.getPointType().isEmpty(), GasPipePoint::getPointType, dto.getPointType());
        wrapper.eq(dto.getStatus() != null && !dto.getStatus().isEmpty(), GasPipePoint::getStatus, dto.getStatus());
        wrapper.orderByDesc(GasPipePoint::getCreateTime);
        return this.page(page, wrapper);
    }

    @Override
    public List<GasPipePoint> listByNetworkId(String networkId) {
        LambdaQueryWrapper<GasPipePoint> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GasPipePoint::getNetworkId, networkId);
        wrapper.orderByAsc(GasPipePoint::getPointCode);
        return this.list(wrapper);
    }

    @Override
    public List<GasPipePoint> getOptions() {
        LambdaQueryWrapper<GasPipePoint> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(GasPipePoint::getPointId, GasPipePoint::getPointName, GasPipePoint::getPointCode);
        wrapper.orderByAsc(GasPipePoint::getPointName);
        return this.list(wrapper);
    }

    @Override
    public boolean saveWithCheck(GasPipePoint entity) {
        LambdaQueryWrapper<GasPipePoint> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GasPipePoint::getPointCode, entity.getPointCode());
        if (this.count(wrapper) > 0) {
            throw new ServiceException("管点编码已存在：" + entity.getPointCode());
        }
        return this.save(entity);
    }

    @Override
    @Transactional
    public boolean updateWithCheck(GasPipePoint entity) {
        Long pointId = entity.getPointId();
        if (this.getById(pointId) == null) {
            throw new ServiceException("管点不存在：" + pointId);
        }
        String newCode = entity.getPointCode();
        if (newCode != null) {
            LambdaQueryWrapper<GasPipePoint> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(GasPipePoint::getPointCode, newCode)
                    .ne(GasPipePoint::getPointId, pointId);
            if (this.count(wrapper) > 0) {
                throw new ServiceException("管点编码已被使用：" + newCode);
            }
        }
        return this.updateById(entity);
    }

    @Override
    @Transactional
    public boolean removeWithCheck(Long pointId) {
        GasPipePoint point = this.getById(pointId);
        if (point == null) {
            throw new ServiceException("管点不存在：" + pointId);
        }
        return this.removeById(pointId);
    }
}
