/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.security.trans.settings.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.security.common.Constant;
import org.security.common.util.DataFormatUtil;
import org.security.trans.common.service.IFileService;
import org.security.ht.vo.common.UploadVO;
import org.security.trans.settings.entity.FileTemplate;
import org.security.trans.settings.mapper.FileTemplateMapper;
import org.security.trans.settings.service.IFileTemplateService;
import org.security.trans.settings.vo.FileTemplateVO;
import org.springblade.core.secure.utils.AuthUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;
import java.util.Objects;

/**
 * 文件模板 服务实现类
 *
 * @author BladeX
 * @since 2022-03-03
 */
@AllArgsConstructor
@Service
@Slf4j
public class FileTemplateServiceImpl extends ServiceImpl<FileTemplateMapper, FileTemplate> implements IFileTemplateService {

	private final IFileService fileService;

	@Override
	public IPage<FileTemplateVO> selectFileTemplatePage(IPage<FileTemplateVO> page, FileTemplateVO fileTemplate) {
		log.info("a===========");
		return page.setRecords(baseMapper.selectFileTemplatePage(page, fileTemplate));
	}

	@Override
	public boolean saveOrUpdateFileTemplate(FileTemplateVO fileTemplate) throws ParseException {
		fileTemplate.setDelFlag(Constant.DELETE_FLAG_0);
		fileTemplate.setCreatedBy(String.valueOf(AuthUtil.getUserId()));
		fileTemplate.setCreatedTime(DataFormatUtil.getNowDate());
		fileTemplate.setDeptId(AuthUtil.getDeptId());
		this.saveOrUpdate(fileTemplate);
		//		存模板附件
		List<UploadVO> fileList = fileTemplate.getFileList();
		if(Objects.nonNull(fileList) && !fileList.isEmpty()){
			fileService.saveAttachments(fileList, fileTemplate.getId());
		}
		//		存说明附件
		List<UploadVO> noteFileList = fileTemplate.getNoteFileList();
		if (Objects.nonNull(noteFileList) && !noteFileList.isEmpty()) {
			fileService.saveAttachments(noteFileList, fileTemplate.getId());
		}
		return true;
	}

	@Override
	public FileTemplateVO getDetail(String id) {
		FileTemplate detail = super.getById(id);
		FileTemplateVO fileTemplateVO = new FileTemplateVO();
		BeanUtils.copyProperties(detail, fileTemplateVO);
		fileTemplateVO.setFileList(fileService.getAttachments(detail.getId(),detail.getGroupName()));
		fileTemplateVO.setNoteFileList(fileService.getAttachments(detail.getId(),detail.getOtherGroupName()));
		return fileTemplateVO;
	}
}
