package cn.axzo.msg.center.notices.manager;

import cn.axzo.basics.common.constant.enums.TableIsDeleteEnum;
import cn.axzo.msg.center.dal.MNSChannelMessageTemplateDao;
import cn.axzo.msg.center.dal.MNSMessageTemplateDao;
import cn.axzo.msg.center.domain.entity.MNSChannelMessageTemplate;
import cn.axzo.msg.center.domain.entity.MNSMessageTemplate;
import cn.axzo.msg.center.domain.enums.YesNoEnum;
import cn.axzo.msg.center.notices.common.enums.AvailableStatusEnum;
import cn.axzo.msg.center.notices.common.enums.ReturnCodeEnum;
import cn.axzo.msg.center.notices.common.exception.BizException;
import cn.axzo.msg.center.notices.manager.api.MessageTemplateManager;
import cn.axzo.msg.center.notices.manager.api.MessageTemplateParamManager;
import cn.axzo.msg.center.notices.manager.api.dto.response.TemplateParamDto;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 消息模板处理实现类
 *
 * @author zhaoyong_sh
 * @see MessageTemplateManagerImpl
 * @since 2021-05-27 20:26
 */
@Service("messageTemplateManager")
public class MessageTemplateManagerImpl implements MessageTemplateManager {

  @Resource
  private MNSMessageTemplateDao mnsMessageTemplateDao;

  @Resource
  private MNSChannelMessageTemplateDao mnsChannelMessageTemplateDao;

  @Resource(name = "messageTemplateParamManager")
  private MessageTemplateParamManager messageTemplateParamManager;

  @Override
  public MNSMessageTemplate queryAndCheckInnerTemplate(String innerTemplateNo) {
    MNSMessageTemplate messageTemplate = mnsMessageTemplateDao.queryByTemplateNo(innerTemplateNo);
    BizException.error(messageTemplate != null, ReturnCodeEnum.MESSAGE_TEMPLATE_NOT_EXIST);
    BizException
        .error(AvailableStatusEnum.AVAILABLE.getStatus().equals(messageTemplate.getStatus()),
            ReturnCodeEnum.MESSAGE_TEMPLATE_NOT_VALID);
    return messageTemplate;
  }

  @Override
  public MNSChannelMessageTemplate queryChannelTemplate(String innerTemplateNo, String channelCode) {
    MNSChannelMessageTemplate channelMessageTemplate = mnsChannelMessageTemplateDao
        .queryByTemplateNo(innerTemplateNo, channelCode);
    BizException.error(channelMessageTemplate != null, ReturnCodeEnum.MESSAGE_TEMPLATE_NOT_EXIST);
    BizException
        .error(AvailableStatusEnum.AVAILABLE.getStatus().equals(channelMessageTemplate.getStatus()),
            ReturnCodeEnum.MESSAGE_TEMPLATE_NOT_VALID);
    return channelMessageTemplate;
  }

  @Override
  public List<TemplateParamDto> queryTemplateParam(String innerTemplateNo) {
    return messageTemplateParamManager.getByCode(innerTemplateNo);
  }

  @Override
  public List<String> listChannelCodes(String innerTemplateNo) {
    List<MNSChannelMessageTemplate> channelMessageTemplates = mnsChannelMessageTemplateDao.lambdaQuery()
            .eq(MNSChannelMessageTemplate::getInnerTemplateNo, innerTemplateNo)
            .eq(MNSChannelMessageTemplate::getIsDelete, YesNoEnum.NO.getCode())
            .list();
    return channelMessageTemplates.stream()
            // 过滤调不可用的数据
            .filter(e -> AvailableStatusEnum.AVAILABLE.getStatus().equals(e.getStatus()))
            .map(MNSChannelMessageTemplate::getChannelCode)
            .collect(Collectors.toList());
  }

  @Override
  public int changeChannelTemplateCode(String channelCode, String srcTemplateCode, String destTemplateCode) {
    if (Objects.equals(srcTemplateCode, destTemplateCode))
      return 0;
    List<MNSChannelMessageTemplate> templates = mnsChannelMessageTemplateDao.lambdaQuery()
            .eq(MNSChannelMessageTemplate::getTemplateNo, srcTemplateCode)
            .eq(MNSChannelMessageTemplate::getChannelCode, channelCode)
            .eq(MNSChannelMessageTemplate::getIsDelete, TableIsDeleteEnum.NORMAL.value)
            .list();
    if (templates.isEmpty())
      return 0;
    ArrayList<MNSChannelMessageTemplate> updates = new ArrayList<>();
    for (MNSChannelMessageTemplate template : templates) {
      MNSChannelMessageTemplate update = new MNSChannelMessageTemplate();
      update.setId(template.getId());
      update.setTemplateNo(destTemplateCode);
      updates.add(update);
    }
    mnsChannelMessageTemplateDao.updateBatchById(updates);
    return updates.size();
  }

}
