package com.zfire.jiasm.syncdata.service.impl;

import com.zfire.jiasm.syncdata.service.CommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CommonServiceImpl implements CommonService {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public void updateError(String tabName, Long synReqId, Integer synTimes, String synErrMsg) throws Exception {
		
		if(synTimes==null) {
			synTimes=1;
		}else {
			synTimes++;
		}
		
		jdbcTemplate.update("update " + tabName + " set syn_times=?,syn_err_msg=? where syn_req_id=?",
				new Object[] { synTimes, synErrMsg, synReqId });

	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int backup(String tabName) throws Exception{
		jdbcTemplate.update("insert into "+tabName+"_bak select * from "+tabName+" where syn_status=1 ");
		return jdbcTemplate.update("delete from "+tabName+" where syn_status=1 ");
	}

}
