/*
 *      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.publicityReport.service.impl;

import cn.hutool.core.date.DateUtil;
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.trans.common.service.IFileService;
import org.security.trans.publicityReport.dto.PublicityDTO;
import org.security.trans.publicityReport.entity.Publicity;
import org.security.trans.publicityReport.mapper.PublicityMapper;
import org.security.trans.publicityReport.service.IPublicityService;
import org.security.trans.publicityReport.vo.PublicityVO;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 宣传报道-宣传报道信息表 服务实现类
 *
 * @author BladeX
 * @since 2022-03-16
 */
@Service
@Slf4j
@AllArgsConstructor
public class PublicityServiceImpl extends ServiceImpl<PublicityMapper, Publicity> implements IPublicityService {
	private final IFileService fileService;

	@Override
	public IPage<PublicityVO> selectPublicityPage(IPage<PublicityVO> page, PublicityDTO publicityDTO) {
		List<PublicityVO> vos = baseMapper.selectPublicityPage(page, publicityDTO);
		log.info("=======vos 1{}======",vos);
		vos.forEach(item ->{
			//获取宣传图
			item.setPicList(fileService.getAttachments(item.getId(), item.getReportImgRoute()));
			//获取附件
			item.setFileList(fileService.getAttachments(item.getId(), item.getReportEnclosureRoute()));
		});
		log.info("=======vos 2{}======",vos);
		return page.setRecords(vos);
	}

	@Override
	public PublicityVO getDetail(String id){
		Publicity publicity = this.getById(id);
		PublicityVO publicityVO = Func.copy(publicity,PublicityVO.class);
		//获取宣传图
		publicityVO.setPicList(fileService.getAttachments(publicityVO.getId(), publicityVO.getReportImgRoute()));
		//获取附件
		publicityVO.setFileList(fileService.getAttachments(publicityVO.getId(), publicityVO.getReportEnclosureRoute()));
		return publicityVO;
	}

	@Override
	public R savePublicity(PublicityDTO publicity) {
		if (Func.isNull(publicity.getDeptId()) || Func.isBlank(publicity.getDeptId())){
			publicity.setDeptId(AuthUtil.getDeptId());
		}
		if(Func.isNull(publicity.getReportDate()) || Func.isEmpty(publicity.getReportDate())){
			publicity.setReportDate(DateUtil.date());
		}
		publicity.setState(Constant.FORM_STATE_DRAFT);
		super.saveOrUpdate(publicity);
		//保存首页宣传图
		fileService.saveAttachments(publicity.getPicList(),publicity.getId());
		//保存相关附件
		fileService.saveAttachments(publicity.getFileList(),publicity.getId());
		return R.data(publicity);
	}

	@Override
	public boolean handin(PublicityDTO publicityDTO) {
		R<Publicity> res = savePublicity(publicityDTO);
		if (!res.isSuccess()) {
			throw new ServiceException("保存失败！");
		}
		Publicity publicity = res.getData();
		publicity.setState(Constant.FORM_STATE_VALID);
		publicity.setReportDate(DateUtil.date());
		return super.updateById(publicity);
	}
}
