package com.crm.manager.service.impl;

import com.crm.manager.dao.repository.RiskLeverageGroupRuleRepository;
import com.crm.manager.service.RiskLeverageGroupRuleService;
import com.crm.rely.backend.core.dto.risk.leverage.group.rule.RiskLeverageGroupRuleDto;
import com.crm.rely.backend.core.entity.risk.leverage.group.rule.RiskLeverageGroupRuleSaveEntity;
import com.crm.rely.backend.core.pojo.table.RiskLeverageGroupRuleTable;
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 RiskLeverageGroupRuleServiceImpl implements RiskLeverageGroupRuleService {

    @Autowired
    private RiskLeverageGroupRuleRepository riskLeverageGroupRuleRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Long leverageGroupId, List<RiskLeverageGroupRuleSaveEntity> entities) throws ServiceException {
        List<RiskLeverageGroupRuleTable> riskLeverageGroupRuleTables = riskLeverageGroupRuleRepository.findAllByLeverageGroupId(leverageGroupId);

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

        List<RiskLeverageGroupRuleTable> leverageGroupRuleTables = new ArrayList<>(entities.size());

        for (RiskLeverageGroupRuleSaveEntity riskLeverageGroupRuleSaveEntity : entities) {
            RiskLeverageGroupRuleTable riskLeverageGroupRuleTable = new RiskLeverageGroupRuleTable();

            BeanUtils.copyProperties(riskLeverageGroupRuleSaveEntity, riskLeverageGroupRuleTable);

            riskLeverageGroupRuleTable.setLeverageGroupId(leverageGroupId);
            leverageGroupRuleTables.add(riskLeverageGroupRuleTable);

        }
        riskLeverageGroupRuleRepository.save(leverageGroupRuleTables);
    }

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

    @Override
    public List<RiskLeverageGroupRuleDto> getRuleDto(Long leverageGroupId) throws ServiceException {
        List<RiskLeverageGroupRuleTable> riskLeverageGroupRuleTables
                = riskLeverageGroupRuleRepository.getAllByLeverageGroupId(leverageGroupId);

        return transform(riskLeverageGroupRuleTables);
    }

    private List<RiskLeverageGroupRuleDto> transform(List<RiskLeverageGroupRuleTable> riskLeverageGroupRuleTables) {
        if (riskLeverageGroupRuleTables == null || riskLeverageGroupRuleTables.size() <= 0) {
            return null;
        }
        List<RiskLeverageGroupRuleDto> riskLeverageGroupRuleDtos = new ArrayList<>(riskLeverageGroupRuleTables.size());
        for (RiskLeverageGroupRuleTable riskLeverageGroupRuleTable : riskLeverageGroupRuleTables) {
            RiskLeverageGroupRuleDto riskLeverageGroupRuleDto = transform(riskLeverageGroupRuleTable);
            riskLeverageGroupRuleDtos.add(riskLeverageGroupRuleDto);
        }
        return riskLeverageGroupRuleDtos;
    }

    private RiskLeverageGroupRuleDto transform(RiskLeverageGroupRuleTable riskLeverageGroupRuleTable) {
        RiskLeverageGroupRuleDto riskLeverageGroupRuleDto = new RiskLeverageGroupRuleDto();
        BeanUtils.copyProperties(riskLeverageGroupRuleTable, riskLeverageGroupRuleDto);
        return riskLeverageGroupRuleDto;
    }
}
