package com.crm.custom.service.impl;

import com.crm.custom.dao.repository.CustomBankRepository;
import com.crm.custom.service.CustomBankService;
import com.crm.custom.service.impl.base.BaseUploadServiceImpl;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.model.dto.custom.bank.CustomBankListDto;
import com.crm.rely.backend.model.entity.custom.bank.CustomBankAddEntity;
import com.crm.rely.backend.model.entity.custom.bank.CustomBankDeleteEntity;
import com.crm.rely.backend.model.entity.custom.bank.CustomBankUpdateEntity;
import com.crm.rely.backend.model.pojo.table.CustomBankTable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

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

@Service
public class CustomBankServiceImpl extends BaseUploadServiceImpl implements CustomBankService {

    @Autowired
    private CustomBankRepository repository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(CustomBankAddEntity entity) throws ServiceException {

        CustomBankTable table = repository.getFirstByCustomId(entity.getCustomId());

        if (table == null) {
            table = new CustomBankTable();
        }
        BeanUtils.copyProperties(entity, table);

        repository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CustomBankUpdateEntity entity) throws ServiceException {
        if (entity.getCustomId() == null) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        CustomBankTable table;
        if (entity.getId() != null) {
            table = repository.findFirstById(entity.getId());
        } else {
            table = null;
            entity.setId(null);
        }
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        if (entity.getDefaultBank() == null) {
            entity.setDefaultBank(0);
        }
        if (entity.getType() == null) {
            entity.setType(1);
        }


        if (table.getDefaultBank() != null && !table.getDefaultBank().equals(1)
                && entity.getDefaultBank() != null && entity.getDefaultBank().equals(1)) {

            List<CustomBankTable> tables = repository.findAllByCustomIdAndDefaultBankAndType(entity.getCustomId(), 1, entity.getType());

            if (tables != null && tables.size() > 0) {
                for (CustomBankTable updateTable :
                        tables) {
                    updateTable.setDefaultBank(0);
                }

                repository.save(tables);
            }

        }
        BeanUtils.copyProperties(entity, table);

        repository.save(table);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(CustomBankDeleteEntity entity) throws ServiceException {

        List<CustomBankTable> tables = repository.findAllByCustomIdAndIdIn(entity.getCustomId(), entity.getIds());

        if (tables == null && tables.size() <= 0) {
            return;
        }
        repository.delete(tables);
    }

    @Override
    public String uploadBankFile(Long customId, MultipartFile file) throws Exception {

        String middlePath = (customId == null ? "" : customId.toString()) + "/bank";
        String path = saveFile(middlePath, file);

        return path;
    }

    @Override
    public CustomBankTable getByCustomId(Long customId) throws ServiceException {
        CustomBankTable table = repository.getFirstByCustomId(customId);
        return table;
    }

    private List<CustomBankListDto> transform(List<CustomBankTable> tables) {
        if (tables == null && tables.size() <= 0) {
            return null;
        }
        List<CustomBankListDto> dtos = new ArrayList<>(tables.size());
        for (CustomBankTable table : tables) {
            CustomBankListDto dto = transform(table);
            dtos.add(dto);
        }

        return dtos;
    }

    private CustomBankListDto transform(CustomBankTable customBankTable) {
        CustomBankListDto customBankListDto = new CustomBankListDto();
        BeanUtils.copyProperties(customBankTable, customBankListDto);
        //todo 其他判断转化

        return customBankListDto;
    }
}
