package cn.axzo.msg.center.message.service.impl;

import cn.axzo.basics.common.util.AssertUtil;
import cn.axzo.msg.center.common.exception.ServiceException;
import cn.axzo.msg.center.dal.MessageBaseTemplateDao;
import cn.axzo.msg.center.domain.entity.MessageBaseTemplate;
import cn.axzo.msg.center.message.domain.param.MessageTemplateSaveOrUpdateParam;
import cn.axzo.msg.center.message.service.MessageTemplateNewSyncService;
import cn.axzo.msg.center.service.enums.MessageCategoryEnum;
import cn.axzo.msg.center.service.template.request.MessageTemplateSyncDto;
import cn.axzo.msg.center.utils.JSONObjectUtil;
import cn.axzo.msg.center.utils.MessageTemplateCategoryUtil;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author syl
 * @date 2023/12/15
 */
@Slf4j
@Service
public class MessageTemplateNewSyncServiceImpl implements MessageTemplateNewSyncService {

    @Autowired
    private MessageBaseTemplateDao messageBaseTemplateDao;

    @Autowired
    private MessageTemplateNewServiceImpl messageTemplateNewService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void syncTemplate(MessageTemplateSyncDto param) {
        // 检查模板
        checkTemplate(param);
        // 同步模板基础信息
        String templateCode = syncBaseTemplate(param);
        // 同步模板的路由信息
        messageTemplateNewService.saveTemplateRouters(MessageTemplateSaveOrUpdateParam.fromSync(param), templateCode);
    }

    private String syncBaseTemplate(MessageTemplateSyncDto param) {
        MessageBaseTemplate template = convertBaseTemplate(param);
        messageTemplateNewService.doSaveTemplate(template, param.getTemplateCode());
        return template.getCode();
    }

    private MessageBaseTemplate convertBaseTemplate(MessageTemplateSyncDto param) {
        MessageCategoryEnum category = MessageTemplateCategoryUtil
                .checkAndReturnSubCategory(param.getCategory(), param.getSubCategory())
                .orElseThrow(() -> new ServiceException("无效的参数"));
        MessageBaseTemplate template = new MessageBaseTemplate();
        template.setCode(param.getTemplateCode());
        template.setName(param.getTemplateName());
        template.setMsgCategory(category);
        template.setTitle(param.getMsgTitle());
        template.setContent(param.getMsgContent());
        template.setCardContent(JSONObjectUtil.toJSONString(param.getCardContentItems()));
        template.setIcon(param.getMsgIcon());
        template.setPushTerminal(JSONObjectUtil.toJSONString(param.getPushTerminals()));
        template.setMinAppVersion(param.getMinAppVersion());
        template.setCreatorId(param.getOperatorId());
        template.setUpdaterId(param.getOperatorId());
        return template;
    }

    private void checkTemplate(MessageTemplateSyncDto param) {
        Optional<MessageBaseTemplate> msgBaseTemplateOpt = messageBaseTemplateDao.lambdaQuery()
                .eq(MessageBaseTemplate::getCode, param.getTemplateCode())
                .eq(MessageBaseTemplate::getIsDelete, 0)
                .oneOpt();
        AssertUtil.isFalse(msgBaseTemplateOpt.isPresent(),
                String.format("%s环境模版code已存在，本次同步操作取消", param.getEnv()));
    }
}
