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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.security.trans.common.service.IFileService;
import org.security.trans.projectBudgetManagement.dto.ProjectContractDTO;
import org.security.trans.projectBudgetManagement.entity.ProjectContract;
import org.security.trans.projectBudgetManagement.vo.ProjectContractVO;
import org.security.trans.projectBudgetManagement.mapper.ProjectContractMapper;
import org.security.trans.projectBudgetManagement.service.IProjectContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目管理-项目合同 服务实现类
 *
 * @author BladeX
 * @since 2022-08-05
 */
@Service
@Slf4j
@AllArgsConstructor
public class ProjectContractServiceImpl extends ServiceImpl<ProjectContractMapper, ProjectContract> implements IProjectContractService {

	private final IFileService fileService;

	@Override
	public List<ProjectContractVO> getDaya(String id) {
		log.info("==============获取项目合同数据开始==============");
		List<ProjectContractVO> vos = new ArrayList<>();
		List<ProjectContract> list = this.list(new QueryWrapper<ProjectContract>().lambda().eq(Func.isNotEmpty(id), ProjectContract::getProjectLinkId, id));
		if (Func.isNotEmpty(list)){
			list.forEach(item ->{
				ProjectContractVO vo = Func.copy(item, ProjectContractVO.class);
				//获取相关附件
				vo.setContractAttachments(fileService.getAttachments(item.getId(), item.getOriginalContract()));
				vo.setSupplementAttachments(fileService.getAttachments(item.getId(), item.getSupplementMaterials()));
				vo.setLegalAttachments(fileService.getAttachments(item.getId(), item.getAfterLegalDevice()));
				vo.setOtherAttachments(fileService.getAttachments(item.getId(), item.getOtherMaterials()));
				vos.add(vo);
			});
		}
		log.info("==============获取项目合同数据结束==============");
		return vos;
	}

	@Override
	public IPage<ProjectContractVO> selectProjectContractPage(IPage<ProjectContractVO> page, ProjectContractDTO projectContractDTO) {
		log.info("==============获取项目合同数据开始projectContractDTO:==============",projectContractDTO);
		List<ProjectContractVO> vos = baseMapper.selectProjectContractPage(page, projectContractDTO);
		log.info("==============获取项目合同数据结束==============");
		return page.setRecords(vos);
	}
}
