package com.gic.haoban.manage.service.service.notify.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gic.haoban.manage.service.dao.mapper.NoticeMessageTemplateMapper;
import com.gic.haoban.manage.service.entity.TabNoticeMessageTemplate;
import com.gic.haoban.manage.service.pojo.bo.TemplateContentBO;
import com.gic.haoban.manage.service.service.notify.NoticeMessageTemplateService;
import com.gic.redis.data.util.RedisUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created 2021/12/17.
 *
 * @author hua
 */
@Service
public class NoticeMessageTemplateServiceImpl implements NoticeMessageTemplateService {

    private static final String QYWX_TEMPLATE_KEY = "qywx-notice-message-template:";
    private static final String HAOBAN_TEMPLATE_KEY = "haoban-notice-message-template:";

    private static final Long TIME_OUT = 24 * 60 * 60L;

    @Autowired
    private NoticeMessageTemplateMapper noticeMessageTemplateMapper;

    @Override
    public List<TemplateContentBO> getQywxTemplateMessage(String enterpriseId, String templateCode) {
        String key = QYWX_TEMPLATE_KEY + templateCode + ":" + enterpriseId;
        String content = (String) RedisUtil.getCache(key);
        if (StringUtils.isNotBlank(content)) {
            return JSONArray.parseArray(content, TemplateContentBO.class);
        }
        String lockKey = QYWX_TEMPLATE_KEY + templateCode + ":" + enterpriseId + ";lock";
        RedisUtil.lock(lockKey, 2L);

        TabNoticeMessageTemplate messageTemplate = noticeMessageTemplateMapper.getByCode(enterpriseId, templateCode);
        if (messageTemplate == null) {
            messageTemplate = noticeMessageTemplateMapper.getByCode("-1", templateCode);
        }
        if (messageTemplate == null) {
            RedisUtil.unlock(lockKey);
            return null;
        }
        content = messageTemplate.getContent();
        RedisUtil.setCache(key, content, TIME_OUT);
        RedisUtil.unlock(lockKey);
        return JSONArray.parseArray(content, TemplateContentBO.class);
    }

    @Override
    public String getHaobanTemplateMessage(String enterpriseId, String templateCode) {
        String key = HAOBAN_TEMPLATE_KEY + templateCode + ":" + enterpriseId;
        String content = (String) RedisUtil.getCache(key);
        if (StringUtils.isNotBlank(content)) {
            return content;
        }
        String lockKey = HAOBAN_TEMPLATE_KEY + templateCode + ":" + enterpriseId + ";lock";
        RedisUtil.lock(lockKey, 2L);

        TabNoticeMessageTemplate messageTemplate = noticeMessageTemplateMapper.getByCode(enterpriseId, templateCode);
        if (messageTemplate == null) {
            messageTemplate = noticeMessageTemplateMapper.getByCode("-1", templateCode);
        }
        if (messageTemplate == null) {
            RedisUtil.unlock(lockKey);
            return null;
        }
        content = messageTemplate.getNoticeContent();
        RedisUtil.setCache(key, content, TIME_OUT);
        RedisUtil.unlock(lockKey);
        return content;
    }
}
