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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.biz.enums.DeviceResponseEnum;
import com.njcn.device.line.mapper.LineDetailMapper;
import com.njcn.device.pq.enums.PvDeviceResponseEnum;
import com.njcn.device.pq.mapper.NewStationMapper;
import com.njcn.device.pq.pojo.param.NewStationQueryParam;
import com.njcn.device.pq.pojo.po.NewStation;
import com.njcn.device.pq.service.INewStationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  新能源场站高低电压穿越表 服务实现类
 * </p>
 * @author guofeihu
 * @since 2024-08-14
 */
@Service
@RequiredArgsConstructor
public class NewStationServiceImpl extends ServiceImpl<NewStationMapper, NewStation> implements INewStationService {

    private final LineDetailMapper lineDetailMapper;

    @Override
    public Page<NewStation> queryPage(NewStationQueryParam newStationQueryParam) {
        Page<NewStation> returnpage = new Page<> (newStationQueryParam.getPageNum(),newStationQueryParam.getPageSize());
        returnpage  = this.getBaseMapper().queryPage(returnpage,newStationQueryParam);
        return returnpage;
    }

    @Override
    public Boolean editNewStation(NewStationQueryParam.NewStationEdit newStationEdit) {
        NewStation newStation = new NewStation();
        BeanUtil.copyProperties(newStationEdit, newStation);
        newStation.setState(1);
        LambdaQueryWrapper<NewStation> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(NewStation::getName,newStation.getName()).eq(NewStation::getState,1);
        boolean result ;
        if(newStation.getId() != null){
            lambdaQueryWrapper.ne(NewStation::getId,newStation.getId());
            if(!this.baseMapper.selectList(lambdaQueryWrapper).isEmpty()){
                throw new BusinessException(PvDeviceResponseEnum.NEWSTATION_REPEAT.getMessage());
            }
            result = this.updateById(newStation);
        }else{
            if(!this.baseMapper.selectList(lambdaQueryWrapper).isEmpty()){
                throw new BusinessException(PvDeviceResponseEnum.NEWSTATION_REPEAT.getMessage());
            }
            newStation.setId(IdUtil.simpleUUID());
            result = this.save(newStation);
        }
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delNewStation(String ids) {
        List<String> listIds = Arrays.asList(ids.split(","));
        //判断该新能源场站信息是否绑定了测点ID
        for(String id : listIds){
            if(lineDetailMapper.checkExistsLineByNewStationId(id)!=0){
                LambdaUpdateWrapper<NewStation> queryWrapper = new LambdaUpdateWrapper<>();
                queryWrapper.eq(NewStation::getId,id);
                NewStation newStation = this.baseMapper.selectOne(queryWrapper);
                throw new BusinessException(newStation == null?"":newStation.getName()+DeviceResponseEnum.NEWSTATION_IS_BIND_LINE.getMessage());
            }
        }
        LambdaUpdateWrapper<NewStation> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(NewStation::getState,0)
                .in(NewStation::getId, listIds);
        boolean update = this.update(lambdaUpdateWrapper);
        return update;
    }

    @Override
    public List<Map> selectDown(String name) {
        return this.baseMapper.selectDown(name);
    }

    @Override
    public NewStation selectById(String id) {
        LambdaQueryWrapper<NewStation> lambdaUpdateWrapper = new LambdaQueryWrapper<>();
        lambdaUpdateWrapper.eq(NewStation::getId,id).eq(NewStation::getState,1);
        return this.baseMapper.selectOne(lambdaUpdateWrapper);
    }

}
