package com.zt.plat.module.qms.business.bus.controller.vo;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
public class ReportedDataSource implements Serializable {

	private static final long serialVersionUID = 5854800501968008943L;

	/**
	 * 自动上报时间
	 */
	@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
	private LocalDateTime autoReportedTime;
	
	/** 已完成数据来源明细 **/
	private List<DataSourceDetail> details;
	
	/**
	 * 添加数据来源
	 * @param sourceCode 来源编码
	 * @param reportedOper 上报人
	 * @param reportedTime 上报时间
	 */
	public void addDataSource(String sourceCode, String reportedOper, LocalDateTime reportedTime) {
		if (this.details == null) {
			this.details = new ArrayList<>();
			this.details.add(new DataSourceDetail(sourceCode, reportedOper, reportedTime));
		} else {
			List<String> dataSources = this.details.stream().map(m -> m.getSourceCode()).collect(Collectors.toList());
			if (dataSources.contains(sourceCode)) {
				DataSourceDetail dataSourceDetail = this.details.stream().filter(f -> f.getSourceCode().equals(sourceCode)).findAny().get();
				dataSourceDetail.setReportedOper(reportedOper);
				dataSourceDetail.setReportedTime(reportedTime);
			} else {
				this.details.add(new DataSourceDetail(sourceCode, reportedOper, reportedTime));
			}
		}
	}
	
	/**
	 * 检查是否存在来源编码
	 * @param sourceCode 来源编码
	 * @return true 存在  false 不存在
	 */
	public boolean checkIsExist(String sourceCode) {
		if (StringUtils.isBlank(sourceCode)) {
			return false;
		}
		if (this.details == null) {
			return false;
		}
		return this.details.stream().anyMatch(f -> sourceCode.equals(f.getSourceCode()));
	}
	
	@Data
	@NoArgsConstructor
	@AllArgsConstructor
	public static class DataSourceDetail {
		/**
		 * 来源编码
		 */
		private String sourceCode;
		
		/**
		 * 上报人
		 */
		private String reportedOper;
		
		/**
		 * 上报时间
		 */
		@JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd HH:mm:ss")
		private LocalDateTime reportedTime;
	}
}
