package com.crm.manager.service.impl;

import com.crm.manager.dao.repository.RiskRevokeCreditRuleExcludeLoginRepository;
import com.crm.manager.service.RiskRevokeCreditRuleExcludeLoginService;
import com.crm.rely.backend.core.dto.risk.revoke.credit.rule.exclude.login.RiskRevokeCreditRuleExcludeLoginDto;
import com.crm.rely.backend.core.entity.risk.revoke.credit.rule.exclude.login.RiskRevokeCreditRuleExcludeLoginSaveEntity;
import com.crm.rely.backend.core.pojo.table.RiskRevokeCreditRuleExcludeLoginTable;
import com.crm.rely.backend.exception.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class RiskRevokeCreditRuleExcludeLoginServiceImpl implements RiskRevokeCreditRuleExcludeLoginService {

    @Autowired
    private RiskRevokeCreditRuleExcludeLoginRepository riskRevokeCreditRuleExcludeLoginRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Long revokeCreditRuleId, List<RiskRevokeCreditRuleExcludeLoginSaveEntity> entities) throws ServiceException {
        List<RiskRevokeCreditRuleExcludeLoginTable> riskRevokeCreditRuleExcludeLoginTables = riskRevokeCreditRuleExcludeLoginRepository.findAllByRevokeCreditRuleId(revokeCreditRuleId);

        if (riskRevokeCreditRuleExcludeLoginTables != null && riskRevokeCreditRuleExcludeLoginTables.size() > 0) {
            riskRevokeCreditRuleExcludeLoginRepository.deleteInBatch(riskRevokeCreditRuleExcludeLoginTables);
        }
        if (entities == null || entities.size() <= 0) {
            return;
        }

        List<RiskRevokeCreditRuleExcludeLoginTable> riskRevokeCreditRuleExcludeLoginSaveTables = new ArrayList<>(entities.size());

        for (RiskRevokeCreditRuleExcludeLoginSaveEntity riskRevokeCreditRuleExcludeLoginSaveEntity : entities) {
            RiskRevokeCreditRuleExcludeLoginTable riskRevokeCreditRuleExcludeLoginTable = new RiskRevokeCreditRuleExcludeLoginTable();

            BeanUtils.copyProperties(riskRevokeCreditRuleExcludeLoginSaveEntity, riskRevokeCreditRuleExcludeLoginTable);

            riskRevokeCreditRuleExcludeLoginTable.setRevokeCreditRuleId(revokeCreditRuleId);
            riskRevokeCreditRuleExcludeLoginSaveTables.add(riskRevokeCreditRuleExcludeLoginTable);

        }
        riskRevokeCreditRuleExcludeLoginRepository.save(riskRevokeCreditRuleExcludeLoginSaveTables);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> revokeCreditRuleIds) throws ServiceException {
        List<RiskRevokeCreditRuleExcludeLoginTable> riskRevokeCreditRuleExcludeLoginTables
                = riskRevokeCreditRuleExcludeLoginRepository.findAllByRevokeCreditRuleIdIn(revokeCreditRuleIds);
        if (riskRevokeCreditRuleExcludeLoginTables == null || riskRevokeCreditRuleExcludeLoginTables.size() <= 0) {
            return;
        }
        riskRevokeCreditRuleExcludeLoginRepository.delete(riskRevokeCreditRuleExcludeLoginTables);
    }

    @Override
    public List<Long> getExcludeLoginDto(Long revokeCreditRuleId) throws ServiceException {
        List<RiskRevokeCreditRuleExcludeLoginTable> riskRevokeCreditRuleExcludeLoginTables
                = riskRevokeCreditRuleExcludeLoginRepository.getAllByRevokeCreditRuleId(revokeCreditRuleId);

        if (riskRevokeCreditRuleExcludeLoginTables == null || riskRevokeCreditRuleExcludeLoginTables.size() <= 0) {
            return null;
        }
        List<Long> excludeLogins = new ArrayList<>(riskRevokeCreditRuleExcludeLoginTables.size());
        for (RiskRevokeCreditRuleExcludeLoginTable riskRevokeCreditRuleExcludeLoginTable : riskRevokeCreditRuleExcludeLoginTables) {

            excludeLogins.add(riskRevokeCreditRuleExcludeLoginTable.getLogin());
        }
        return excludeLogins;
    }

    private RiskRevokeCreditRuleExcludeLoginDto transform(RiskRevokeCreditRuleExcludeLoginTable riskRevokeCreditRuleExcludeLoginTable) {
        RiskRevokeCreditRuleExcludeLoginDto riskRevokeCreditRuleExcludeLoginDto = new RiskRevokeCreditRuleExcludeLoginDto();
        BeanUtils.copyProperties(riskRevokeCreditRuleExcludeLoginTable, riskRevokeCreditRuleExcludeLoginDto);
        return riskRevokeCreditRuleExcludeLoginDto;
    }
}
