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

import com.zfire.jiasm.syncdata.service.SMSService;
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 SMSServiceImpl implements SMSService {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<Map<String, Object>> getTaskData(String tabName) {
		List<Map<String, Object>> result = jdbcTemplate.queryForList("SELECT * FROM " + tabName
				+ " where syn_status=0 and (fjid is not null) and fjid<>'0' and fjid<>'' order by syn_req_time ");
		return result;
	}

	@Override
	public List<Map<String, Object>> getTaskData4Self() {
		List<Map<String, Object>> result = jdbcTemplate
				.queryForList("SELECT * FROM fa_order_sms where syn_status=0 and "
						+ "(syn_times is null  or syn_times<3) order by syn_req_time ");
		return result;
	}

	@Override
	public void successUpdate(String tabName, String synTaskNo) {
		jdbcTemplate.update("update " + tabName + " set syn_status=1,syn_time=? where syn_task_no=?",
				new Object[] { new Date(), synTaskNo });
	}

	@Override
	public void errorUpdate(String tabName, int synTimes, String synTaskNo, String errorMessage) {
		jdbcTemplate.update("update " + tabName + " set syn_times=?,syn_time=?,syn_err_msg=? where syn_task_no=?",
				new Object[] { synTimes, new Date(), errorMessage, synTaskNo });
	}

	@Override
	public List<Map<String, Object>> getTaskMarkeginData4Self(){
		List<Map<String, Object>> result = jdbcTemplate
				.queryForList("SELECT * FROM fa_marketing_sms where syn_status=0 and "
						+ "(syn_times is null  or syn_times<3) order by syn_req_time ");
		return result;
	}

	@Override
	public List<Map<String, Object>> getTaskExceptionData(){
		List<Map<String, Object>> result = jdbcTemplate
				.queryForList("SELECT * FROM fa_exception_mobile ");
		return result;
	}

	@Override
	public void stopSendUpdate(String tabName,int synStatus,String synTaskNo,String errorMessage){
		jdbcTemplate.update("update " + tabName + " set syn_status=?,syn_time=?,syn_err_msg=? where syn_task_no=?",
				new Object[] { synStatus, new Date(), errorMessage, synTaskNo });

	}

}
