/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.platform.service.impl;

import com.crunii.micro.common.constants.CommonConstants;
import com.crunii.micro.domain.entitysql.main.TBusiLogS;
import com.crunii.micro.platform.jdbc.log.BusiLogJdbcDao;
import com.crunii.micro.platform.service.BusiLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 田平 create 2018年8月7日下午4:31:48
 */
public class BusiLogServiceImpl implements BusiLogService {

	@Autowired
	private BusiLogJdbcDao busiLogJdbcDao;

	@Override
	public int createTBusiLog(TBusiLogS busi) {
		busi.setCreateTime(new Date());
		checkColumns(busi);
		return busiLogJdbcDao.insertTBusiLog(busi);
	}

	@Override
	public int createTBusiLogs(List<TBusiLogS> busiLogs) {
		Date date = new Date();
		busiLogs.stream().forEach(busi -> {
			busi.setCreateTime(date);
			checkColumns(busi);
		});
		return busiLogJdbcDao.insertTBusiLogs(busiLogs);
	}

	private void checkColumns(TBusiLogS busiLog) {
		if (ObjectUtils.isEmpty(busiLog.getLogType())) {
			busiLog.setLogType(CommonConstants.TYPE_CLIENT);
		}
		if (ObjectUtils.isEmpty(busiLog.getCallResult())) {
			busiLog.setLogType(CommonConstants.RESULT_FAILURE);
		}
		if (busiLog.getCallTime() == null) {
			busiLog.setCallTime(new Date());
		}
	}
}
