package com.cyksj.service.corp.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.common.util.Jsons;
import com.cyksj.mapper.corp.CorpWelcomeTemplateMapper;
import com.cyksj.model.entity.CorpWelcomeTemplate;
import com.cyksj.model.request.corp.Attachments;
import com.cyksj.service.corp.CorpWelcomeTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 项目名: yhlxj
 * 文件名: CorpWelcomeTemplateServiceImpl
 * 创建者: JavaZou
 * 创建时间:2023/12/5 18:22
 */
@Service
@RequiredArgsConstructor
public class CorpWelcomeTemplateServiceImpl extends ServiceImpl<CorpWelcomeTemplateMapper, CorpWelcomeTemplate> implements CorpWelcomeTemplateService {
	@Override
	public void saveOrUpdateTemplate(CorpWelcomeTemplate template) throws Exception {
		handleParams(template);
		this.saveOrUpdate(template);
	}


	public void handleParams(CorpWelcomeTemplate template) throws Exception {
		List<Attachments> attachments = template.getAttachments();
		if (CollUtil.isNotEmpty(attachments)) {
			String attachmentsStr = Jsons.toJson(attachments);
			template.setAttachmentsStr(attachmentsStr);
		} else {
			template.setAttachmentsStr(null);
		}
		if (!CorpWelcomeTemplate.Type.checkType(template.getType())) {
			throw BusinessRuntimeException.getInstance("请选择正确的类型");
		}
	}
}
