package com.ktg.iscs.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.StringUtils;
import com.ktg.iscs.domain.IsMailNotifyConfig;
import com.ktg.iscs.domain.IsMailTemplate;
import com.ktg.iscs.mapper.IsMailTemplateMapper;
import com.ktg.iscs.service.IIsMailNotifyConfigService;
import com.ktg.iscs.service.IIsMailTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 系统邮件模版Service业务层处理
 *
 * @author cgj
 * @date 2025-01-15
 */
@Service
public class IsMailTemplateServiceImpl extends ServiceImpl<IsMailTemplateMapper, IsMailTemplate> implements IIsMailTemplateService {

    @Autowired
    private IsMailTemplateMapper isMailTemplateMapper;
    @Autowired
    private IIsMailNotifyConfigService isMailNotifyConfigService;

    @Override
    public Page<IsMailTemplate> getIsMailTemplatePage(Page<IsMailTemplate> page, IsMailTemplate isMailTemplate) {
        Page<IsMailTemplate> result = page(page, Wrappers.<IsMailTemplate>lambdaQuery()
                .like(StringUtils.isNotBlank(isMailTemplate.getTemplateCode()), IsMailTemplate::getTemplateCode, isMailTemplate.getTemplateCode())
                .like(StringUtils.isNotBlank(isMailTemplate.getTemplateName()), IsMailTemplate::getTemplateName, isMailTemplate.getTemplateName())
                .like(StringUtils.isNotBlank(isMailTemplate.getTemplateTitle()), IsMailTemplate::getTemplateTitle, isMailTemplate.getTemplateTitle())
                .like(StringUtils.isNotBlank(isMailTemplate.getTemplateContent()), IsMailTemplate::getTemplateContent, isMailTemplate.getTemplateContent())
                .orderByDesc(IsMailTemplate::getCreateTime));
        return result;
    }

    @Override
    public Boolean insertIsMailTemplate(IsMailTemplate isMailTemplate) {
        Assert.notBlank(isMailTemplate.getTemplateCode(), "模板编码不能为空！");
        List<IsMailTemplate> list = list(Wrappers.<IsMailTemplate>lambdaQuery().eq(IsMailTemplate::getTemplateCode, isMailTemplate.getTemplateCode()));
        Assert.isTrue(list.isEmpty(), "该模板编码已存在！");
        return save(isMailTemplate);
    }

    @Override
    public Boolean updateIsMailTemplate(IsMailTemplate isMailTemplate) {
        Assert.notNull(isMailTemplate.getTemplateId(), "模板id不能为空！");
        Assert.notBlank(isMailTemplate.getTemplateCode(), "模板编码不能为空！");
        List<IsMailTemplate> list = list(Wrappers.<IsMailTemplate>lambdaQuery()
                .eq(IsMailTemplate::getTemplateCode, isMailTemplate.getTemplateCode())
                .ne(IsMailTemplate::getTemplateId, isMailTemplate.getTemplateId()));
        Assert.isTrue(list.isEmpty(), "该模板编码已存在！");
        return updateById(isMailTemplate);
    }

    @Override
    public Boolean deleteIsMailTemplateByTemplateCodes(String templateIds) {
        Assert.notBlank(templateIds, "请选择需要删除的数据!");
        List<Long> ids = Arrays.asList(Convert.toLongArray(templateIds));
        List<IsMailTemplate> isMailTemplates = listByIds(ids);
        List<String> codes = isMailTemplates.stream().map(IsMailTemplate::getTemplateCode).collect(Collectors.toList());
        // 检测下面有没有被配置使用，使用了就不能删除
        List<IsMailNotifyConfig> notifyConfigs = isMailNotifyConfigService.list(Wrappers.<IsMailNotifyConfig>lambdaQuery()
                .in(IsMailNotifyConfig::getTemplateCode, codes));
        Assert.isTrue(notifyConfigs.isEmpty(), "存在正在使用的模板，不可删除！");
        return removeBatchByIds(ids);
    }


}
