
package com.crm.ucard.service.impl;

import com.crm.rely.backend.core.constant.ConfigConstants;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.pojo.table.SysConfigTable;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.service.RedisService;
import com.crm.ucard.dao.repository.SysConfigRepository;
import com.crm.ucard.service.SysConfigService;
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.List;

@Service
public class SysConfigServiceImpl implements SysConfigService {

    @Autowired
    private SysConfigRepository sysConfigRepository;

    @Autowired
    private RedisService redisService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(String code, String value) throws ServiceException {
        SysConfigTable sysConfigTable = sysConfigRepository.findFirstByCode(code);

        if (sysConfigTable == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }

        sysConfigTable.setValue(value);
        sysConfigRepository.save(sysConfigTable);

        saveCache(sysConfigTable);
    }

    @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 = sysConfigRepository.getByCodeIn(codes);
            if (sysConfigTables == null || supplementTables.size() <= 0) {
                throw ServiceException.exception(Constants.INFO_NOT_FOUND);
            }

            saveCache(supplementTables);

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

    private void saveCache(List<SysConfigTable> sysConfigTables) throws ServiceException {
        List<String> configListKey = redisService.getList(ConfigConstants.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(ConfigConstants.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(ConfigConstants.CONFIG_LIST, configListKey);
    }

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

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

        }
        return sysConfigTable;
    }

}
