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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hys.app.converter.erp.MsgConfigConverter;
import com.hys.app.framework.database.mybatisplus.base.BaseServiceImpl;
import com.hys.app.framework.exception.ServiceException;
import com.hys.app.mapper.erp.MsgConfigMapper;
import com.hys.app.model.erp.dos.MsgConfig;
import com.hys.app.model.erp.dto.MsgConfigDTO;
import com.hys.app.model.erp.enums.MsgTypeEnum;
import com.hys.app.model.erp.vo.MsgConfigVO;
import com.hys.app.model.system.dos.SmsMailTemplate;
import com.hys.app.service.erp.MsgConfigManager;
import com.hys.app.service.erp.SmsMailManager;
import com.hys.app.util.ValidateUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 订单业务层实现
 *
 * @author 张崧
 * 2024-01-24 15:58:31
 */
@Service
@AllArgsConstructor
public class MsgConfigManagerImpl extends BaseServiceImpl<MsgConfigMapper, MsgConfig> implements MsgConfigManager {

    private SmsMailManager smsMailManager;

    @Override
    public Long addMsg(MsgConfigDTO msgConfigDTO) {
        checkSmsStatus(msgConfigDTO.getTemplateCode());
        MsgConfig msgConfig = MsgConfigConverter.INSTANCE.convert(msgConfigDTO);
        checkParams(msgConfig);
        save(msgConfig);
        return msgConfig.getId();
    }

    @Override
    public void changeTemplate(String id, String templateCode) {
        checkSmsStatus(templateCode);
        MsgConfig msgConfig = getById(id);
        msgConfig.setTemplateCode(templateCode);
        updateById(msgConfig);
    }

    @Override
    public void getAllMsg() {
        List<MsgConfig> list = list();
        List<MsgConfigVO> convert = MsgConfigConverter.INSTANCE.convert(list);
        for (MsgConfigVO msgConfig : convert) {
            msgConfig.setChildMsg(list.stream().filter(item -> item.getParentId().equals(msgConfig.getId())).collect(Collectors.toList()));
        }
    }

    @Override
    public void deleteMsg(String id) {
        removeById(id);
    }

    private void checkParams(MsgConfig msgConfig) {
        if (ValidateUtil.checkRepeat(baseMapper, MsgConfig::getTitle, msgConfig.getTitle(), MsgConfig::getId, null)) {
            throw new ServiceException("模板名称不能重复");
        }
        if (ValidateUtil.checkRepeat(baseMapper, MsgConfig::getCode, msgConfig.getCode(), MsgConfig::getId, null)) {
            throw new ServiceException("模板标识不能重复");
        }
    }

    /**
     * 校验短信模板启用了没有有没有审核通过
     *
     * @param code
     */
    private void checkSmsStatus(String code) {
        SmsMailTemplate one = smsMailManager.getOne(new LambdaQueryWrapper<SmsMailTemplate>()
                .eq(SmsMailTemplate::getCode, code)
                .eq(SmsMailTemplate::getEnableStatus, true));
        if (ObjectUtil.isNull(one)) {
            throw new ServiceException("未找到模板信息");
        }
        if (one.getType().equals(MsgTypeEnum.SMS) && !one.getAuditStatus()) {
            throw new ServiceException("该短信模板暂未审核通过，不能使用");
        }
    }
}

