package com.crm.manager.service.impl;

import com.crm.manager.dao.repository.CustomGroupHideRepository;
import com.crm.manager.service.CustomGroupHideService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.custom.group.hide.CustomGroupHideDto;
import com.crm.rely.backend.core.dto.custom.group.leverage.CustomGroupLeverageDto;
import com.crm.rely.backend.core.entity.custom.group.hide.CustomGroupHideSaveEntity;
import com.crm.rely.backend.core.pojo.table.CustomGroupHideTable;
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 CustomGroupHideServiceImpl implements CustomGroupHideService {

    @Autowired
    private CustomGroupHideRepository customGroupHideRepository;

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

        CustomGroupHideDto customGroupHideDto = new CustomGroupHideDto();
        customGroupHideDto.setGroupId(groupId);
        List<Integer> leverages = new ArrayList<>(customGroupLeverageTables.size());
        for (CustomGroupHideTable customGroupHideTable : customGroupLeverageTables) {
            leverages.add(customGroupHideTable.getHide());
        }
        customGroupHideDto.setHides(leverages);

        return customGroupHideDto;
    }

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

        if (customGroupLeverageTables != null) {
            for (int l = 0; l < customGroupLeverageTables.size(); ) {
                CustomGroupHideTable customGroupHideTable = customGroupLeverageTables.get(l);
                boolean isDelete = false;
                for (int i = 0; i < entity.getHides().size(); ) {
                    Integer leverage = entity.getHides().get(i);
                    if (leverage.equals(customGroupHideTable.getHide())) {
                        entity.getHides().remove(leverage);
                        isDelete = true;
                        break;
                    } else {
                        i++;
                    }
                }
                if (isDelete) {
                    customGroupLeverageTables.remove(customGroupHideTable);
                } else {
                    l++;
                }
            }
        }
        if (entity.getHides().size() > 0) {
            List<CustomGroupHideTable> customGroupHideTables = new ArrayList<>();
            for (Integer leverage : entity.getHides()) {
                CustomGroupHideTable customGroupHideTable = new CustomGroupHideTable();
                customGroupHideTable.setGroupId(entity.getGroupId());
                customGroupHideTable.setHide(leverage);
                customGroupHideTable.setAddIp(entity.getIp());
                customGroupHideTable.setAddTime(entity.getTime());
                customGroupHideTable.setAddUser(entity.getUserId());
                customGroupHideTables.add(customGroupHideTable);
            }
            customGroupHideRepository.save(customGroupHideTables);
        }
        if (customGroupLeverageTables != null) {
            customGroupHideRepository.delete(customGroupLeverageTables);
        }
    }

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

        customGroupHideRepository.delete(customGroupHideTables);
    }

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

        customGroupHideRepository.delete(customGroupLeverageTables);
    }

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


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

            Long groupId = customGroupLeverageTable.getGroupId();
            CustomGroupHideDto customGroupHideDto = customGroupLeverageDtoMap.getOrDefault(groupId, null);

            if (customGroupHideDto == null) {
                customGroupHideDto = new CustomGroupHideDto();
                customGroupHideDto.setGroupId(groupId);
            }
            List<Integer> hides = customGroupHideDto.getHides();
            if (hides == null) {
                hides = new ArrayList<>();
            }
            hides.add(customGroupLeverageTable.getHide());
            customGroupHideDto.setHides(hides);
            customGroupLeverageDtoMap.put(groupId, customGroupHideDto);
        }
        List<CustomGroupHideDto> customGroupHideDtos = new ArrayList<>();
        customGroupLeverageDtoMap.forEach((k, v) -> {
            customGroupHideDtos.add(v);
        });

        return customGroupHideDtos;
    }
}
