package com.crm.manager.service.impl;

import com.crm.manager.dao.repository.CustomGroupPointRepository;
import com.crm.manager.service.CustomGroupPointService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.custom.group.print.CustomGroupPointDto;
import com.crm.rely.backend.core.entity.custom.group.point.CustomGroupPointSaveEntity;
import com.crm.rely.backend.core.pojo.table.CustomGroupPointTable;
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 CustomGroupPointServiceImpl implements CustomGroupPointService {

    @Autowired
    private CustomGroupPointRepository customGroupPointRepository;

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

        CustomGroupPointDto customGroupHideDto = new CustomGroupPointDto();
        customGroupHideDto.setGroupId(groupId);
        List<Float> points = new ArrayList<>(customGroupLeverageTables.size());
        for (CustomGroupPointTable customGroupHideTable : customGroupLeverageTables) {
            points.add(customGroupHideTable.getPoint());
        }
        customGroupHideDto.setPoints(points);

        return customGroupHideDto;
    }

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

        if (customGroupLeverageTables != null) {
            for (int l = 0; l < customGroupLeverageTables.size(); ) {
                CustomGroupPointTable customGroupHideTable = customGroupLeverageTables.get(l);
                boolean isDelete = false;
                for (int i = 0; i < entity.getPoints().size(); ) {
                    Float point = entity.getPoints().get(i);
                    if (point.equals(customGroupHideTable.getPoint())) {
                        entity.getPoints().remove(point);
                        isDelete = true;
                        break;
                    } else {
                        i++;
                    }
                }
                if (isDelete) {
                    customGroupLeverageTables.remove(customGroupHideTable);
                } else {
                    l++;
                }
            }
        }
        if (entity.getPoints().size() > 0) {
            List<CustomGroupPointTable> customGroupHideTables = new ArrayList<>();
            for (Float leverage : entity.getPoints()) {
                CustomGroupPointTable customGroupPointTable = new CustomGroupPointTable();
                customGroupPointTable.setGroupId(entity.getGroupId());
                customGroupPointTable.setPoint(leverage);
                customGroupPointTable.setAddIp(entity.getIp());
                customGroupPointTable.setAddTime(entity.getTime());
                customGroupPointTable.setAddUser(entity.getUserId());
                customGroupHideTables.add(customGroupPointTable);
            }
            customGroupPointRepository.save(customGroupHideTables);
        }
        if (customGroupLeverageTables != null) {
            customGroupPointRepository.delete(customGroupLeverageTables);
        }
    }

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

        customGroupPointRepository.delete(customGroupHideTables);
    }

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

        customGroupPointRepository.delete(customGroupLeverageTables);
    }

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


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

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

            if (customGroupHideDto == null) {
                customGroupHideDto = new CustomGroupPointDto();
                customGroupHideDto.setGroupId(groupId);
            }
            List<Float> points = customGroupHideDto.getPoints();
            if (points == null) {
                points = new ArrayList<>();
            }
            points.add(customGroupLeverageTable.getPoint());
            customGroupHideDto.setPoints(points);
            customGroupLeverageDtoMap.put(groupId, customGroupHideDto);
        }
        List<CustomGroupPointDto> customGroupHideDtos = new ArrayList<>();
        customGroupLeverageDtoMap.forEach((k, v) -> {
            customGroupHideDtos.add(v);
        });

        return customGroupHideDtos;
    }
}
