package com.zcloud.primeport.gatewayimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zcloud.primeport.domain.gateway.MkmjPassageGateway;
import com.zcloud.primeport.domain.model.MkmjPassageE;
import com.zcloud.primeport.persistence.dataobject.MkmjPassageDO;
import com.zcloud.primeport.persistence.repository.MkmjGateRepository;
import com.zcloud.primeport.persistence.repository.MkmjPassageRepository;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;

@Service
@AllArgsConstructor
public class MkmjPassageGatewayImpl implements MkmjPassageGateway {
    private final MkmjPassageRepository mkmjPassageRepository;
    private final MkmjGateRepository mkmjGateRepository;

    @Override
    public Boolean add(MkmjPassageE mkmjPassageE) {
        MkmjPassageDO d = new MkmjPassageDO();
        BeanUtils.copyProperties(mkmjPassageE, d);
        mkmjPassageRepository.save(d);
        return true;
    }

    @Override
    public Boolean update(MkmjPassageE mkmjPassageE) {
        MkmjPassageDO d = new MkmjPassageDO();
        BeanUtils.copyProperties(mkmjPassageE, d);
        mkmjPassageRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean deletedMkmjPassageById(Long id) {
        return mkmjPassageRepository.removeById(id);
    }

    @Override
    public Boolean deletedMkmjPassageByIds(Long[] ids) {
        return mkmjPassageRepository.removeByIds(Collections.singletonList(ids));
    }

    @Override
    public Boolean checkName(String passageName, Long mkmjId, Long excludeId) {
        QueryWrapper<MkmjPassageDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("passage_name", passageName);
        queryWrapper.eq("mkmj_id", mkmjId);
        queryWrapper.eq("delete_enum", "false");
        if (excludeId != null) {
            queryWrapper.ne("id", excludeId);
        }
        return mkmjPassageRepository.count(queryWrapper) == 0;
    }

    @Override
    public Integer countByPassageId(String passageId) {
        return mkmjGateRepository.countByPassageId(passageId);
    }
}