package com.crm.custom.service.impl;

import com.crm.custom.dao.repository.SysConfigRepository;
import com.crm.custom.service.SysConfigService;
import com.crm.rely.backend.core.constant.GlobalConfigConstants;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.pojo.table.SysConfigTable;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class SysConfigServiceImpl implements SysConfigService {

    @Autowired
    private SysConfigRepository configRepository;

    @Autowired
    private RedisService redisService;

    @Override
    public List<SysConfigTable> getByCodes(List<String> codes) throws ServiceException {
        List<SysConfigTable> sysConfigTables = new ArrayList<>(codes.size());
        for (int i = 0; i < codes.size(); ) {
            SysConfigTable sysConfigTable = redisService.getEntity(codes.get(i), SysConfigTable.class);
            if (sysConfigTable != null) {
                sysConfigTables.add(sysConfigTable);
                codes.remove(i);
            } else {
                i++;
            }
        }
        if (codes != null && codes.size() > 0) {
            List<SysConfigTable> supplementTables = configRepository.getByCodeIn(codes);
            if (sysConfigTables == null || supplementTables.size() <= 0) {
                throw ServiceException.exception(Constants.INFO_NOT_FOUND);
            }

            saveCache(supplementTables);

            sysConfigTables.addAll(supplementTables);
        }
        return sysConfigTables;
    }

    @Override
    public SysConfigTable getByCode(String code) throws ServiceException {
        SysConfigTable sysConfigTable = redisService.getEntity(code, SysConfigTable.class);

        if (sysConfigTable == null) {
            sysConfigTable = configRepository.getByCode(code);
            if (sysConfigTable == null) {
                throw ServiceException.exception(Constants.INFO_NOT_FOUND);
            } else {
                redisService.save(sysConfigTable.getCode(), sysConfigTable);
            }

        }
        return sysConfigTable;
    }


    private void saveCache(List<SysConfigTable> sysConfigTables) throws ServiceException {
        List<String> configListKey = redisService.getList(GlobalConfigConstants.CONFIG_LIST, String.class);
        sysConfigTables.forEach(v -> {
            try {
                saveCache(v, configListKey);
            } catch (ServiceException e) {
                e.printStackTrace();
            }
        });
    }

    private void saveCache(SysConfigTable sysConfigTable) throws ServiceException {
        saveCache(sysConfigTable, null);
    }

    private void saveCache(SysConfigTable sysConfigTable, List<String> configListKey) throws ServiceException {
        if (configListKey == null) {
            configListKey = redisService.getList(GlobalConfigConstants.CONFIG_LIST, String.class);
        }
        if (configListKey == null) {
            configListKey = new ArrayList<>(10);
        }
        if (!configListKey.contains(sysConfigTable.getCode())) {
            configListKey.add(sysConfigTable.getCode());
        }
        redisService.save(sysConfigTable.getCode(), sysConfigTable);
        redisService.save(GlobalConfigConstants.CONFIG_LIST, configListKey);
    }
}
