package com.crm.manager.service.impl;

import com.crm.manager.dao.repository.CustomGroupDemoLeverageRepository;
import com.crm.manager.service.CustomGroupDemoLeverageService;
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.CustomGroupDemoLeverageTable;
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 CustomGroupDemoLeverageServiceImpl implements CustomGroupDemoLeverageService {

    @Autowired
    private CustomGroupDemoLeverageRepository customGroupDemoLeverageRepository;

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

        CustomGroupLeverageDto customGroupLeverageDto = new CustomGroupLeverageDto();
        customGroupLeverageDto.setGroupId(groupId);
        List<Integer> leverages = new ArrayList<>(customGroupDemoLeverageTables.size());
        for (CustomGroupDemoLeverageTable customGroupDemoLeverageTable : customGroupDemoLeverageTables) {
            leverages.add(customGroupDemoLeverageTable.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<CustomGroupDemoLeverageTable> customGroupLeverageTables = customGroupDemoLeverageRepository.findAllByGroupId(entity.getGroupId());

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


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

        customGroupDemoLeverageRepository.delete(customGroupDemoLeverageTables);
    }

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

        customGroupDemoLeverageRepository.delete(customGroupDemoLeverageTables);
    }

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


        Map<Long, CustomGroupLeverageDto> customGroupLeverageDtoMap = new HashMap<>();
        for (CustomGroupDemoLeverageTable customGroupDemoLeverageTable : customGroupDemoLeverageTables) {

            Long groupId = customGroupDemoLeverageTable.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(customGroupDemoLeverageTable.getLeverage());
            customGroupLeverageDto.setLeverages(leverages);
            customGroupLeverageDtoMap.put(groupId, customGroupLeverageDto);
        }
        List<CustomGroupLeverageDto> customGroupLeverageDtos = new ArrayList<>();
        customGroupLeverageDtoMap.forEach((k, v) -> {
            customGroupLeverageDtos.add(v);
        });

        return customGroupLeverageDtos;
    }
}
