package com.crm.manager.service.impl;

import com.crm.manager.dao.repository.RiskLeverageGroupExcludeLoginRepository;
import com.crm.manager.service.risk.RiskLeverageGroupExcludeLoginService;
import com.crm.rely.backend.core.entity.risk.leverage.group.exclude.login.RiskLeverageRuleExcludeLoginSaveEntity;
import com.crm.rely.backend.core.pojo.table.RiskLeverageGroupExcludeLoginTable;
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 RiskLeverageGroupExcludeLoginServiceImpl implements RiskLeverageGroupExcludeLoginService {

    @Autowired
    private RiskLeverageGroupExcludeLoginRepository riskLeverageGroupExcludeLoginRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Long leverageGroupId, List<RiskLeverageRuleExcludeLoginSaveEntity> entities) throws ServiceException {
        List<RiskLeverageGroupExcludeLoginTable> riskLeverageGroupExcludeLoginTables = riskLeverageGroupExcludeLoginRepository.findAllByLeverageGroupId(leverageGroupId);

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

        List<RiskLeverageGroupExcludeLoginTable> leverageGroupExcludeLoginTables = new ArrayList<>(entities.size());

        for (RiskLeverageRuleExcludeLoginSaveEntity riskLeverageRuleExcludeLoginSaveEntity : entities) {
            RiskLeverageGroupExcludeLoginTable riskLeverageGroupExcludeLoginTable = new RiskLeverageGroupExcludeLoginTable();

            BeanUtils.copyProperties(riskLeverageRuleExcludeLoginSaveEntity, riskLeverageGroupExcludeLoginTable);

            riskLeverageGroupExcludeLoginTable.setLeverageGroupId(leverageGroupId);
            leverageGroupExcludeLoginTables.add(riskLeverageGroupExcludeLoginTable);

        }
        riskLeverageGroupExcludeLoginRepository.save(leverageGroupExcludeLoginTables);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> leverageGroupIds) throws ServiceException {
        List<RiskLeverageGroupExcludeLoginTable> riskLeverageGroupExcludeLoginTables
                = riskLeverageGroupExcludeLoginRepository.findAllByLeverageGroupIdIn(leverageGroupIds);
        if (riskLeverageGroupExcludeLoginTables == null || riskLeverageGroupExcludeLoginTables.size() <= 0) {
            return;
        }
        riskLeverageGroupExcludeLoginRepository.delete(riskLeverageGroupExcludeLoginTables);
    }

    @Override
    public List<Long> getExcludeLoginDto(Long leverageGroupId) throws ServiceException {
        List<RiskLeverageGroupExcludeLoginTable> riskLeverageGroupExcludeLoginTables
                = riskLeverageGroupExcludeLoginRepository.getAllByLeverageGroupId(leverageGroupId);

        if (riskLeverageGroupExcludeLoginTables == null || riskLeverageGroupExcludeLoginTables.size() <= 0) {
            return null;
        }
        List<Long> excludeLogins = new ArrayList<>(riskLeverageGroupExcludeLoginTables.size());
        for (RiskLeverageGroupExcludeLoginTable riskLeverageGroupExcludeLoginTable : riskLeverageGroupExcludeLoginTables) {

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