/**
 * Copyright
 * All right reserved.
 * 项目名称:
 * 创建日期:2022/3/9
 */
package org.springblade.modules.business.notice.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.cache.DictCache;
import org.springblade.common.enums.DictEnum;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.business.notice.entity.NoticeAttachEntity;
import org.springblade.modules.business.notice.entity.NoticeEntity;
import org.springblade.modules.business.notice.service.INoticeAttachService;
import org.springblade.modules.business.notice.vo.NoticeVO;
import org.springblade.modules.resource.entity.Attach;
import org.springblade.modules.resource.service.IAttachService;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/***
 * Date:2022-03-08
 * Title:通知公告 包装类,返回视图层所需的字段
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author admin
 * @mender：（文件的修改者，文件创建者之外的人）
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
public class NoticeWrapper extends BaseEntityWrapper<NoticeEntity, NoticeVO> {

	public static NoticeWrapper build() {
		return new NoticeWrapper();
	}

	private static IAttachService attachService;

	private static IAttachService getAttachService() {
		if (attachService == null) {
			attachService = SpringUtil.getBean(IAttachService.class);
		}
		return attachService;
	}

	private static INoticeAttachService noticeAttachService;

	private static INoticeAttachService getNoticeAttachService() {
		if (noticeAttachService == null) {
			noticeAttachService = SpringUtil.getBean(INoticeAttachService.class);
		}
		return noticeAttachService;
	}

	@Override
	public NoticeVO entityVO(NoticeEntity noticeEntity) {
		NoticeVO noticeVO = Objects.requireNonNull(BeanUtil.copy(noticeEntity, NoticeVO.class));

		return entityVO(noticeVO);
	}

	public NoticeVO entityVO(NoticeVO noticeEntity) {

		String dictValue = DictCache.getValue(DictEnum.NOTICE, noticeEntity.getCategory());
		noticeEntity.setCategoryName(dictValue);
		LambdaQueryWrapper<NoticeAttachEntity> wrapper = Wrappers.<NoticeAttachEntity>query().lambda();
		wrapper.eq(NoticeAttachEntity::getIsDeleted, 0);
		wrapper.eq(NoticeAttachEntity::getGroupId, noticeEntity.getId());
		List<NoticeAttachEntity> attachEntities = getNoticeAttachService().list(wrapper);
		if (null != attachEntities && attachEntities.size() > 0) {
			String attachText = "";
			for (NoticeAttachEntity noticeAttachEntity : attachEntities) {
				Attach attach = getAttachService().getById(noticeAttachEntity.getAttachId());
				if (attachText.length() > 0) {
					attachText += ",";
				}
				attachText += attach.getOriginalName();
			}
			noticeEntity.setAttachText(attachText);
		}
		return noticeEntity;
	}
}
