package com.zfire.jiasm.syncdata.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

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

@Service
public class AddJdueServiceImpl {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public List<Map<String, Object>> getTaskData(String tabName, boolean isFilterOrderNo) {
		List<Map<String, Object>> result = jdbcTemplate.queryForList("SELECT * FROM " + tabName + " where syn_status=0 "
				+ (isFilterOrderNo ? " and (orderNo is not null and orderNo<>'') " : "") + " order by syn_req_id ");
		return result;
	}

	public List<Map<String, Object>> getTaskData4Tmall() {
		List<Map<String, Object>> result = jdbcTemplate.queryForList(
				"SELECT * FROM " + "itf_tbl_tmall_fkmx_req" + " where syn_status=0 " + " order by syn_req_id ");
		return result;
	}

	public void updateError(String tabName, Long synReqId, String synErrMsg) throws Exception {
		jdbcTemplate.update("update " + tabName + " set syn_err_msg=? where syn_req_id=?",
				new Object[] { synErrMsg, synReqId });
	}

	public void updateSuccess(Long synReqId, String tabName) {
		jdbcTemplate.update("update " + tabName + " set syn_status=1,syn_time=? where syn_req_id=?",
				new Object[] { new Date(System.currentTimeMillis()), synReqId });
	}
	
	public void updateJDFinish(Long synReqId, String tabName,String errorMessage) {
		jdbcTemplate.update("update " + tabName + " set syn_status=2,syn_time=?,syn_err_msg=? where syn_req_id=?",
				new Object[] { new Date(System.currentTimeMillis()), errorMessage,synReqId });
	}

	public void updateTmallSuccess(Long synReqId, Long fkguid) {
		jdbcTemplate.update("update itf_tbl_tmall_fkmx_req set syn_status=1,syn_time=?,fkguid=? where syn_req_id=?",
				new Object[] { new Date(System.currentTimeMillis()), fkguid, synReqId });
	}

}
