package com.zt.plat.module.qms.core.code;

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

import org.springframework.stereotype.Component;

import com.zt.plat.framework.common.exception.ServiceException;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.module.system.api.sequence.SequenceApi;
import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class SequenceUtil {
	
	private final SequenceApi sequenceApi;
	
	private final CodeGenUtil codeGenUtil;
	
	/**
	 * 样品编号生成
	 * @param sequenceCode 序列号编码
	 * @return
	 */
	public String genCode(String sequenceCode) {
		if (sequenceApi == null) {
			throw new ServiceException(1_032_100_000, "序列号服务为空");
		}
		String circulationValue = null;
		List<String> inputStrs = null;
		
		if (sequenceCode.lastIndexOf("_CFY") > 0) {
			circulationValue = codeGenUtil.getFinancialYearAndMonth("yyyyMM");
			inputStrs = new ArrayList<>();
			inputStrs.add(circulationValue);
		}

		CommonResult<String> result = sequenceApi.getNextSequence(sequenceCode, circulationValue, inputStrs);
		if (result != null && result.isSuccess() && result.getData() != null) {
            return result.getData();
        }
		throw new ServiceException(1_032_100_000, "调用远程服务获取序列号异常");
	}

}
