package com.hys.app.service.erp.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hys.app.mapper.erp.CompanyBandMsgMapper;
import com.hys.app.model.erp.entity.*;
import com.hys.app.service.erp.CompanyBandMsgManager;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

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

/**
 * 平台管理员业务类
 *
 * @author zh
 * @version v7.0
 * @since v7.0.0
 * 2018-06-20 20:38:26
 */
@Service
@AllArgsConstructor
public class CompanyBandMsgManagerImpl extends ServiceImpl<CompanyBandMsgMapper, CompanyBandMsg> implements CompanyBandMsgManager {

    private RedisTemplate redisTemplate;

    @Override
    public List<CompanyBandMsg> getCompanyBandMsg(Long companyId) {
        List<CompanyBandMsg> msgs = (List<CompanyBandMsg>) redisTemplate.opsForHash().get("companyTemplate", companyId.toString());
        if (CollUtil.isEmpty(msgs)) {
            List<CompanyBandMsg> list = list(new LambdaQueryWrapper<CompanyBandMsg>().eq(CompanyBandMsg::getCompanyId, companyId));
            msgs = list.stream().filter(CompanyBandMsg::getHasSelect).collect(Collectors.toList());
        }
        redisTemplate.opsForHash().put("companyTemplate", companyId.toString(), msgs);
        return msgs;
    }

    @Override
    public List<String> getCompanyBandMsgCode(Long companyId) {
        List<CompanyBandMsg> companyBandMsg = getCompanyBandMsg(companyId);
        return companyBandMsg.stream().map(CompanyBandMsg::getTemplateCode).collect(Collectors.toList());
    }
}
