package com.crm.manager.service.impl;

import com.crm.manager.dao.repository.SysChannelBankRepository;
import com.crm.manager.service.SysChannelBankService;
import com.crm.rely.backend.core.dto.system.remittance.channel.bank.SysChannelBankDto;
import com.crm.rely.backend.core.entity.system.channel.bank.SysChannelBankSaveEntity;
import com.crm.rely.backend.core.pojo.table.SysChannelBankTable;
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 SysChannelBankServiceImpl implements SysChannelBankService {

    @Autowired
    private SysChannelBankRepository sysChannelBankRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(String channelCode, List<SysChannelBankSaveEntity> entities) throws ServiceException {

        if (entities == null || entities.size() <= 0) {
            List<SysChannelBankTable> sysChannelBankTables = sysChannelBankRepository.findAllByChannelCode(channelCode);
            if (sysChannelBankTables != null && sysChannelBankTables.size() > 0) {
                sysChannelBankRepository.delete(sysChannelBankTables);
            }
            return;
        }

        List<SysChannelBankTable> sysBankSaveTables = new ArrayList<>(entities.size());
        List<SysChannelBankTable> sysChannelBankTables = sysChannelBankRepository.findAllByChannelCode(channelCode);
        List<SysChannelBankTable> sysBankDeleteTables;
        //判断通道code银行是否存在 如果存在 进行判断是否修改和删除 如果不存在则直接进入添加银行实体环节
        if (sysChannelBankTables != null || sysChannelBankTables.size() > 0) {
            sysBankDeleteTables = new ArrayList<>(sysChannelBankTables.size());
            for (SysChannelBankTable sysChannelBankTable : sysChannelBankTables) {
                //标识数据库数据是否需要删除
                boolean isDelete = true;
                int size = entities.size();
                for (int i = 0; i < size; i++) {
                    if (entities.get(i).getId() != null && sysChannelBankTable.getId().equals(entities.get(i).getId())) {
                        BeanUtils.copyProperties(entities.get(i), sysChannelBankTable);
                        sysChannelBankTable.setModifyIp(entities.get(i).getIp());
                        sysChannelBankTable.setModifyTime(entities.get(i).getTime());
                        sysChannelBankTable.setModifyUser(entities.get(i).getUser());
                        sysBankSaveTables.add(sysChannelBankTable);
                        isDelete = false;
                        //移除需要修改的实体 并结束循环
                        entities.remove(i);
                        break;
                    }
                }
                //循环判断中 原数据库数据不在这次修改或者添加范围 则这个记录是需要删除 添加到删除记录中
                if (isDelete) {
                    sysBankDeleteTables.add(sysChannelBankTable);
                }
            }
        } else {
            sysBankDeleteTables = null;
        }
        //剩下的这些为需要添加的实体
        for (SysChannelBankSaveEntity sysChannelBankSaveEntity : entities) {
            sysChannelBankSaveEntity.setId(null);
            SysChannelBankTable sysChannelBankTable = new SysChannelBankTable();
            BeanUtils.copyProperties(sysChannelBankSaveEntity, sysChannelBankTable);
            sysChannelBankTable.setAddIp(sysChannelBankSaveEntity.getIp());
            sysChannelBankTable.setAddTime(sysChannelBankSaveEntity.getTime());
            sysChannelBankTable.setAddUser(sysChannelBankSaveEntity.getUser());
            sysBankSaveTables.add(sysChannelBankTable);
        }

        if (sysBankSaveTables != null && sysBankSaveTables.size() > 0) {
            sysChannelBankRepository.save(sysBankSaveTables);
        }

        if (sysBankDeleteTables != null && sysBankDeleteTables.size() > 0) {
            sysChannelBankRepository.delete(sysBankDeleteTables);
        }
    }

    @Override
    public List<SysChannelBankDto> getByChannelCode(String channelCode) throws ServiceException {
        List<SysChannelBankTable> sysChannelBankTables = sysChannelBankRepository.getAllByChannelCode(channelCode);

        return transform(sysChannelBankTables);
    }

    @Override
    public List<SysChannelBankDto> getByChannelCodes(List<String> channelCodes) throws ServiceException {
        List<SysChannelBankTable> sysChannelBankTables = sysChannelBankRepository.getAllByChannelCodeIn(channelCodes);

        return transform(sysChannelBankTables);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByChannelCodes(List<String> channelCodes) throws ServiceException {
        List<SysChannelBankTable> sysChannelBankTables = sysChannelBankRepository.findAllByChannelCodeIn(channelCodes);
        if (sysChannelBankTables != null && sysChannelBankTables.size() > 0) {
            sysChannelBankRepository.delete(sysChannelBankTables);
        }
    }

    private List<SysChannelBankDto> transform(List<SysChannelBankTable> sysChannelBankTables) {

        if (sysChannelBankTables == null || sysChannelBankTables.size() <= 0) {
            return null;
        }

        List<SysChannelBankDto> sysChannelBankDtos = new ArrayList<>(sysChannelBankTables.size());
        sysChannelBankTables.forEach(value -> sysChannelBankDtos.add(transform(value)));

        return sysChannelBankDtos;
    }

    private SysChannelBankDto transform(SysChannelBankTable sysChannelBankTable) {
        SysChannelBankDto sysChannelBankDto = new SysChannelBankDto();
        BeanUtils.copyProperties(sysChannelBankTable, sysChannelBankDto);
        return sysChannelBankDto;
    }
}
