package com.zcloud.primeport.gatewayimpl;

import com.alibaba.cola.exception.BizException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zcloud.primeport.domain.gateway.ClosedAreaGateway;
import com.zcloud.primeport.domain.model.ClosedAreaE;
import com.zcloud.primeport.persistence.dataobject.ClosedAreaDO;
import com.zcloud.primeport.persistence.repository.ClosedAreaRepository;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
* web-infrastructure
* @Author dearLin
* @Date 2026-03-19 10:27:51
*/
@Service
@AllArgsConstructor
public class ClosedAreaGatewayImpl implements ClosedAreaGateway {
    private final ClosedAreaRepository closedAreaRepository;

    @Override
    public Boolean add(ClosedAreaE closedAreaE) {
        ClosedAreaDO d = new ClosedAreaDO();
        BeanUtils.copyProperties(closedAreaE, d);
        closedAreaRepository.save(d);
        return true;
    }

    @Override
    public Boolean update(ClosedAreaE closedAreaE) {
        ClosedAreaDO d = new ClosedAreaDO();
        BeanUtils.copyProperties(closedAreaE, d);
        closedAreaRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean deletedClosedAreaById(Long id) {
        QueryWrapper<ClosedAreaDO> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("parent_id",id);
        if (!closedAreaRepository.list(objectQueryWrapper).isEmpty()) {
            throw new BizException("当前区域有子区域禁止删除");
        }
        return closedAreaRepository.removeById(id);
    }

    @Override
    public Boolean deletedClosedAreaByIds(Long[] ids) {
        return closedAreaRepository.removeByIds(Arrays.asList(ids));
    }
}

