package com.mortals.xhx.base.system.param.service.impl;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.impl.AbstractCRUDCacheServiceImpl;
import com.mortals.xhx.base.system.param.dao.ParamDao;
import com.mortals.xhx.base.system.param.model.ParamEntity;
import com.mortals.xhx.base.system.param.model.ParamQuery;
import com.mortals.xhx.base.system.param.service.ParamService;

import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>Title: 参数信息</p>
 * <p>Description: ParamServiceImpl service接口 </p>
 * <p>Copyright: Copyright &reg;  </p>
 * <p>Company: </p>
 *
 * @author
 * @version 1.0.0
 */
@Service("paramService")
public class ParamServiceImpl extends AbstractCRUDCacheServiceImpl<ParamDao, ParamEntity, Long>
        implements ParamService {

    @Override
    protected String getExtKey(ParamEntity data) {
        return data.getParamKey();
    }
    
    @Override
    protected String getCacheName() {
        return "ParamEntity.paramKey";
    }
    
    @Override
    public void putCache(String key, ParamEntity data) {
        cacheService.set(data.getParamKey(), data.getParamValue());
    }

    @Override
    public void removeCache(String key) {
    }

    @Override
    public int remove(Long[] ids, Context context) throws AppException {
        // 先删缓存
        for (Long id : ids) {
            ParamEntity paramEntity = get(id, null);
            cacheService.del(paramEntity.getParamKey());
        }
        removeBefore(ids, context);
        int iRet = dao.delete(ids);
        removeAfter(ids, context, iRet);
        return iRet;
    }

    @Override
    public String getValueByKey(String key) {
        return cacheService.get(key);
    }

    @Override
    public Map<String, String> getParamByFirstOrganize(String firstOrganize, String... excludeParamKeys) {
        ParamQuery query = new ParamQuery();
        query.setFirstOrganize(firstOrganize);
        List<ParamEntity> list = this.getDao().getList(query);
        return list.stream().filter(s -> {
            return !Arrays.asList(excludeParamKeys).contains(s.getParamKey());
        }).collect(Collectors.toMap(ParamEntity::getParamKey, ParamEntity::getParamValue));
    }
}