package com.quantgroup.asset.distribution.service.distribute.impl;


import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.quantgroup.asset.distribution.constant.StatusConstants;
import com.quantgroup.asset.distribution.exception.QGException;
import com.quantgroup.asset.distribution.exception.QGExceptionType;
import com.quantgroup.asset.distribution.model.entity.DistributeRecord;
import com.quantgroup.asset.distribution.service.distribute.IAssetDistributeRecordService;
import com.quantgroup.asset.distribution.service.jpa.entity.Asset;
import com.quantgroup.asset.distribution.service.jpa.entity.AssetDistributeRecord;
import com.quantgroup.asset.distribution.service.jpa.repository.IAssetDistributeRecordRepository;
import com.quantgroup.asset.distribution.service.redis.IRedisService;

import lombok.extern.slf4j.Slf4j;

/**
 * 资产分发日志记录表
 * @author liwenbin
 *
 */
@Slf4j
@Service
public class AssetDistributeRecordServiceImpl implements IAssetDistributeRecordService{
	
	@Autowired
	private IAssetDistributeRecordRepository assetDistributeRecordRepository;
	@Autowired
	private IRedisService<Integer> redisService;
	@Value("${isDebug}")
	private Boolean isDebug;
		
	/**
	 * 不能改成异步，防止save的时候record被清空
	 */
	@Override
	public AssetDistributeRecord saveDistributeRecord(Asset asset, DistributeRecord distributeRecord, int distributeStatus, int ruleType) {
		AssetDistributeRecord record = null;
		try {
			AssetDistributeRecord assetDistributeRecord = new AssetDistributeRecord();
			assetDistributeRecord.setAssetNo(asset.getAssetNo());
			assetDistributeRecord.setUuid(asset.getUuid());
			assetDistributeRecord.setUserLoanType(asset.getUserLoanType());
			assetDistributeRecord.setBizChannel(asset.getBizChannel());
			assetDistributeRecord.setFinanceProductType(asset.getFinanceProductType());
			assetDistributeRecord.setBizNo(asset.getBizNo());
			assetDistributeRecord.setAssetDistributeTravel(JSON.toJSONString(distributeRecord));
			assetDistributeRecord.setAssetDistributeTarget(ruleType);
			assetDistributeRecord.setAssetDistributeStatus(distributeStatus);
			assetDistributeRecord.setEnable(true);
			record = assetDistributeRecordRepository.save(assetDistributeRecord);
			// 完成一次分发，清除分发记录
			distributeRecord.clearRecords();
		} catch (Exception e) {
			log.error("用户记录分发日志异常, uuid : {}", asset.getUuid(), e);
		}
		return record;
	}
	
	@Override
	public List<AssetDistributeRecord> getDistributeRecord(String assetNo) {
		return assetDistributeRecordRepository.findByAssetNoAndEnableIsTrue(assetNo);
	}

	@Override
	public void updateAssetDistributeStatus(String bizNo, int status) {
		AssetDistributeRecord assetDistributeRecord = assetDistributeRecordRepository.findByBizNoOrderByCreatedAtDescLimitOne(bizNo);
		if (assetDistributeRecord == null) {
			log.info("资产分发记录更改状态未找到订单, bizNo : {}, status : {}", bizNo, status);
			if (isDebug) { return; }
			throw new QGException(QGExceptionType.NOT_FOUND_FUND_SERVER_RESULT_BIZNO, bizNo, status);
		} else {
			assetDistributeRecord.setAssetDistributeStatus(status);
			assetDistributeRecordRepository.save(assetDistributeRecord);
		}
	}

	@Async
	@Override
	public void updateAssetDistribute(AssetDistributeRecord assetDistributeRecord) {
		try {
			assetDistributeRecordRepository.save(assetDistributeRecord);
		} catch (Exception e) {
			log.error("更新分发记录异常!", e);
		}
	}
}
