package com.cyksj.service.corp.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.common.util.Jsons;
import com.cyksj.common.util.StringUtil;
import com.cyksj.mapper.corp.CorpInteractiveRadarMapper;
import com.cyksj.model.entity.CorpInteractiveRadar;
import com.cyksj.model.entity.CorpInteractiveReaderLink;
import com.cyksj.service.corp.CorpInteractiveRadarService;
import org.springframework.stereotype.Service;

/*
 *项目名: netflix
 *文件名: CorpInteractiveRadarServiceImpl
 *创建者: JavaZou
 *创建时间:2023/8/4 17:34
 */
@Service
public class CorpInteractiveRadarServiceImpl extends ServiceImpl<CorpInteractiveRadarMapper, CorpInteractiveRadar> implements CorpInteractiveRadarService {
	private final static String RADAR_MARK = "radarId";

	@Override
	public void saveOrUpdateRadar(CorpInteractiveRadar radar) {
		Integer type = radar.getType();
		//企业微信类型
		if (type == 1) {
			CorpInteractiveReaderLink linkInfo = radar.getLinkInfo();
			if (linkInfo == null) {
				throw BusinessRuntimeException.getInstance("链接信息为空");
			}
			try {
				if (ObjectUtil.hasEmpty(linkInfo.getTitle(), linkInfo.getDesc(), linkInfo.getImg())) {
					throw BusinessRuntimeException.getInstance("请完善链接信息");
				}
				radar.setLinkInfoStr(Jsons.toJson(linkInfo));
			} catch (Exception e) {
				throw BusinessRuntimeException.getInstance("链接信息格式错误");
			}
		}
		if (type == 2) {
			radar.setLinkInfoStr(null);
		}
		if (radar.getWxCorpId() == null) {
			radar.setWxCorpId(2);
		}
		this.saveOrUpdate(radar);
		String url = radar.getUrl();
		if (!url.contains(RADAR_MARK)) {
			String connect = StringUtil.QM;
			if (url.contains(StringUtil.QM)) {
				connect = StringUtil.AD;
			}
			url = String.format("%s%s%s", url, connect, RADAR_MARK + "=" + radar.getId());
			radar.setUrl(url);
			this.updateById(radar);
		}
	}
}
