package com.crm.manager.service.impl;

import com.crm.manager.dao.repository.CustomGroupLeverageRepository;
import com.crm.manager.service.CustomGroupLeverageService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.custom.group.leverage.CustomGroupLeverageDto;
import com.crm.rely.backend.core.entity.custom.group.leverage.CustomGroupLeverageSaveEntity;
import com.crm.rely.backend.core.pojo.table.CustomGroupLeverageTable;
import com.crm.rely.backend.exception.ServiceException;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: houn
 * @Date: 2020/10/29 11:28
 * @Description:
 */
@Service
public class CustomGroupLeverageServiceImpl implements CustomGroupLeverageService {

    @Autowired
    private CustomGroupLeverageRepository customGroupLeverageRepository;

    @Override
    public CustomGroupLeverageDto getByGroupId(Long groupId) throws ServiceException {
        List<CustomGroupLeverageTable> customGroupLeverageTables = customGroupLeverageRepository.getAllByGroupId(groupId);
        if (customGroupLeverageTables == null || customGroupLeverageTables.size() <= 0) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        CustomGroupLeverageDto customGroupLeverageDto = new CustomGroupLeverageDto();
        customGroupLeverageDto.setGroupId(groupId);
        List<Integer> leverages = new ArrayList<>(customGroupLeverageTables.size());
        for (CustomGroupLeverageTable customGroupLeverageTable : customGroupLeverageTables) {
            leverages.add(customGroupLeverageTable.getLeverage());
        }
        customGroupLeverageDto.setLeverages(leverages);

        return customGroupLeverageDto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(CustomGroupLeverageSaveEntity entity) throws ServiceException {
        if (entity == null || entity.getGroupId() == null || entity.getLeverages() == null || entity.getLeverages().size() <= 0) {
            throw ServiceException.exception(Constants.SYSTEM_ERROR);
        }
        List<CustomGroupLeverageTable> customGroupLeverageTables = customGroupLeverageRepository.findAllByGroupId(entity.getGroupId());

        if (customGroupLeverageTables != null) {
            for (int l = 0; l < customGroupLeverageTables.size(); ) {
                CustomGroupLeverageTable customGroupLeverageTable = customGroupLeverageTables.get(l);
                boolean isDelete = false;
                for (int i = 0; i < entity.getLeverages().size(); ) {
                    Integer leverage = entity.getLeverages().get(i);
                    if (leverage.equals(customGroupLeverageTable.getLeverage())) {
                        entity.getLeverages().remove(leverage);
                        isDelete = true;
                        break;
                    } else {
                        i++;
                    }
                }
                if (isDelete) {
                    customGroupLeverageTables.remove(customGroupLeverageTable);
                } else {
                    l++;
                }
            }
        }
        if (entity.getLeverages().size() > 0) {
            List<CustomGroupLeverageTable> customGroupLeverageSaveTables = new ArrayList<>();
            for (Integer leverage : entity.getLeverages()) {
                CustomGroupLeverageTable customGroupLeverageTable = new CustomGroupLeverageTable();
                customGroupLeverageTable.setGroupId(entity.getGroupId());
                customGroupLeverageTable.setLeverage(leverage);
                customGroupLeverageTable.setAddIp(entity.getIp());
                customGroupLeverageTable.setAddTime(entity.getTime());
                customGroupLeverageTable.setAddUser(entity.getUserId());
                customGroupLeverageSaveTables.add(customGroupLeverageTable);
            }
            customGroupLeverageRepository.save(customGroupLeverageSaveTables);
        }
        if (customGroupLeverageTables != null) {
            customGroupLeverageRepository.delete(customGroupLeverageTables);
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long groupId) throws ServiceException {
        List<CustomGroupLeverageTable> customGroupLeverageTables = customGroupLeverageRepository.findAllByGroupId(groupId);
        if (customGroupLeverageTables == null || customGroupLeverageTables.size() <= 0) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        customGroupLeverageRepository.delete(customGroupLeverageTables);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteIn(List<Long> groupIds) throws ServiceException {
        List<CustomGroupLeverageTable> customGroupLeverageTables = customGroupLeverageRepository.findAllByGroupIdIn(groupIds);
        if (customGroupLeverageTables == null || customGroupLeverageTables.size() <= 0) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        customGroupLeverageRepository.delete(customGroupLeverageTables);
    }

    @Override
    public List<CustomGroupLeverageDto> getByGroupIdIn(List<Long> groupIds) throws ServiceException {
        List<CustomGroupLeverageTable> customGroupLeverageTables = customGroupLeverageRepository.getAllByGroupIdIn(groupIds);
        if (customGroupLeverageTables == null || customGroupLeverageTables.size() <= 0) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }


        Map<Long, CustomGroupLeverageDto> customGroupLeverageDtoMap = new HashMap<>();
        for (CustomGroupLeverageTable customGroupLeverageTable : customGroupLeverageTables) {

            Long groupId = customGroupLeverageTable.getGroupId();
            CustomGroupLeverageDto customGroupLeverageDto = customGroupLeverageDtoMap.getOrDefault(groupId, null);

            if (customGroupLeverageDto == null) {
                customGroupLeverageDto = new CustomGroupLeverageDto();
                customGroupLeverageDto.setGroupId(groupId);
            }
            List<Integer> leverages = customGroupLeverageDto.getLeverages();
            if (leverages == null) {
                leverages = new ArrayList<>();
            }
            leverages.add(customGroupLeverageTable.getLeverage());
            customGroupLeverageDto.setLeverages(leverages);
            customGroupLeverageDtoMap.put(groupId, customGroupLeverageDto);
        }
        List<CustomGroupLeverageDto> customGroupLeverageDtos = new ArrayList<>();
        customGroupLeverageDtoMap.forEach((k, v) -> {
            customGroupLeverageDtos.add(v);
        });

        return customGroupLeverageDtos;
    }
}
