package net.northking.cctp.dw.api.atu.service;

import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.entity.Environment;
import net.northking.cctp.common.db.annotation.DatabaseWriteOnly;
import net.northking.cctp.dw.db.entity.DwAtuPlanEnvLink;
import net.northking.cctp.dw.db.service.DwAtuPlanEnvLinkService;
import net.northking.cctp.dw.feign.PlatformFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@ConditionalOnMissingBean(name = "DwAtuPlanEnvLinkApiServiceImpl")
public class DwAtuPlanEnvLinkApiServiceImpl implements DwAtuPlanEnvLinkApiService {

    @Autowired
    private DwAtuPlanEnvLinkService dwAtuPlanEnvLinkService;
    @Autowired
    private PlatformFeignClient platformFeignClient;

    @Override
    public Integer deleteBatch(List<Object> ids) {
        return this.dwAtuPlanEnvLinkService.deleteByPrimaryKeys(ids);
    }

    @Override
    public List<String> addBatch(List<DwAtuPlanEnvLink> dwAtuPlanEnvLinkList) {
        List<String> result = new ArrayList<>();
        for (DwAtuPlanEnvLink dto : dwAtuPlanEnvLinkList) {
            result.add(this.add(dto));
        }
        return result;
    }

    @Override
    @DatabaseWriteOnly
    public String add(DwAtuPlanEnvLink dto) {
        String uuid = dto.getId();
        List<String> ids = new ArrayList<>();
        ids.add(dto.getEnvId());
        Map<String, Environment> map = platformFeignClient.findEnvName(ids).getData();
        dto.setEnvName(map.get(dto.getEnvId()) != null ? map.get(dto.getEnvId()).getName() : "");
        this.dwAtuPlanEnvLinkService.insert(dto);
        return uuid;
    }

    @Override
    public Integer deleteByPlanIdAndEnvId(String planId, String envId) {
        return dwAtuPlanEnvLinkService.deleteByPlanIdAndEnvId(planId,envId);
    }
}
