package com.zcloud.primeport.gatewayimpl;

import com.zcloud.primeport.domain.gateway.MkmjGateway;
import com.zcloud.primeport.domain.model.MkmjE;
import com.zcloud.primeport.persistence.dataobject.MkmjDO;
import com.zcloud.primeport.persistence.repository.MkmjApprovalUserRepository;
import com.zcloud.primeport.persistence.repository.MkmjGateRepository;
import com.zcloud.primeport.persistence.repository.MkmjPassageRepository;
import com.zcloud.primeport.persistence.repository.MkmjRepository;
import com.zcloud.primeport.persistence.repository.VideoRepository;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Service
@AllArgsConstructor
public class MkmjGatewayImpl implements MkmjGateway {
    private final MkmjRepository mkmjRepository;
    private final MkmjPassageRepository mkmjPassageRepository;
    private final MkmjGateRepository mkmjGateRepository;
    private final VideoRepository videoRepository;
    private final MkmjApprovalUserRepository mkmjApprovalUserRepository;

    @Override
    public Boolean add(MkmjE mkmjE) {
        MkmjDO d = new MkmjDO();
        BeanUtils.copyProperties(mkmjE, d);
        mkmjRepository.save(d);
        return true;
    }

    @Override
    public Boolean update(MkmjE mkmjE) {
        MkmjDO d = new MkmjDO();
        BeanUtils.copyProperties(mkmjE, d);
        mkmjRepository.updateById(d);
        return true;
    }

    @Override
    public Boolean deletedMkmjById(Long id) {
        return mkmjRepository.removeById(id);
    }

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

    @Override
    public Boolean checkName(String mkmjName, Long excludeId) {
        return mkmjRepository.checkName(mkmjName, excludeId);
    }

    @Override
    public Map<String, Integer> countRelatedData(Long mkmjId) {
        Map<String, Integer> result = new HashMap<>();
        result.put("passageCount", mkmjPassageRepository.countByMkmjId(mkmjId));
        result.put("gateCount", mkmjGateRepository.countByMkmjId(mkmjId));
        result.put("videoCount", videoRepository.countByForeignIdAndDeviceType(mkmjId, 1));
        return result;
    }
}

