package com.crm.manager.service.impl;

import com.crm.manager.dao.repository.RiskLeverageLoginRuleRepository;
import com.crm.manager.service.RiskLeverageLoginRuleService;
import com.crm.rely.backend.core.dto.risk.leverage.login.rule.RiskLeverageLoginRuleDto;
import com.crm.rely.backend.core.entity.risk.leverage.login.rule.RiskLeverageLoginRuleSaveEntity;
import com.crm.rely.backend.core.pojo.table.RiskLeverageLoginRuleTable;
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 RiskLeverageLoginRuleServiceImpl implements RiskLeverageLoginRuleService {

    @Autowired
    private RiskLeverageLoginRuleRepository riskLeverageLoginRuleRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Long leverageGroupId, List<RiskLeverageLoginRuleSaveEntity> entities) throws ServiceException {
        List<RiskLeverageLoginRuleTable> riskLeverageLoginRuleTables =
                riskLeverageLoginRuleRepository.findAllByLeverageLoginId(leverageGroupId);

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

        List<RiskLeverageLoginRuleTable> leverageLoginRuleTables = new ArrayList<>(entities.size());

        for (RiskLeverageLoginRuleSaveEntity riskLeverageLoginRuleSaveEntity : entities) {
            RiskLeverageLoginRuleTable riskLeverageLoginRuleTable = new RiskLeverageLoginRuleTable();

            BeanUtils.copyProperties(riskLeverageLoginRuleSaveEntity, riskLeverageLoginRuleTable);

            riskLeverageLoginRuleTable.setLeverageLoginId(leverageGroupId);
            leverageLoginRuleTables.add(riskLeverageLoginRuleTable);

        }
        riskLeverageLoginRuleRepository.save(leverageLoginRuleTables);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> leverageGroupIds) throws ServiceException {
        List<RiskLeverageLoginRuleTable> riskLeverageLoginRuleTables
                = riskLeverageLoginRuleRepository.findAllByLeverageLoginIdIn(leverageGroupIds);
        if (riskLeverageLoginRuleTables == null || riskLeverageLoginRuleTables.size() <= 0) {
            return;
        }
        riskLeverageLoginRuleRepository.delete(riskLeverageLoginRuleTables);
    }

    @Override
    public List<RiskLeverageLoginRuleDto> getRuleDto(Long leverageGroupId) throws ServiceException {
        List<RiskLeverageLoginRuleTable> riskLeverageLoginRuleTables
                = riskLeverageLoginRuleRepository.getAllByLeverageLoginId(leverageGroupId);

        return transform(riskLeverageLoginRuleTables);
    }

    private List<RiskLeverageLoginRuleDto> transform(List<RiskLeverageLoginRuleTable> riskLeverageLoginRuleTables) {
        if (riskLeverageLoginRuleTables == null || riskLeverageLoginRuleTables.size() <= 0) {
            return null;
        }
        List<RiskLeverageLoginRuleDto> riskLeverageLoginRuleDtos = new ArrayList<>(riskLeverageLoginRuleTables.size());
        for (RiskLeverageLoginRuleTable riskLeverageLoginRuleTable : riskLeverageLoginRuleTables) {
            RiskLeverageLoginRuleDto riskLeverageLoginRuleDto = transform(riskLeverageLoginRuleTable);
            riskLeverageLoginRuleDtos.add(riskLeverageLoginRuleDto);
        }
        return riskLeverageLoginRuleDtos;
    }

    private RiskLeverageLoginRuleDto transform(RiskLeverageLoginRuleTable riskLeverageLoginRuleTable) {
        RiskLeverageLoginRuleDto riskLeverageLoginRuleDto = new RiskLeverageLoginRuleDto();
        BeanUtils.copyProperties(riskLeverageLoginRuleTable, riskLeverageLoginRuleDto);
        return riskLeverageLoginRuleDto;
    }
}
