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

import com.zfire.jiasm.syncdata.service.SMSRepairFinishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Service
public class SMSRepairFinishServiceImpl implements SMSRepairFinishService {

	@Value("${FAILURE_EXE_COUNT}")
	private String exeCount;


	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<Map<String, Object>> getTaskData() {
		List<Map<String, Object>> result = jdbcTemplate.queryForList(
				"SELECT * FROM itf_messagehuifangentity where fs_status=0 and sendtype = 1 order by syn_task_no");
		return result;
	}

	@Override
	public List<Map<String, Object>> getTaskData4Self() {
		List<Map<String, Object>> result = jdbcTemplate
				.queryForList("SELECT * FROM itf_messagehuifangentity where fs_status=0 and sendtype = 2 "
						+ " and exe_count<"+ exeCount +" and now()>plansendtime " + " order by syn_task_no");
		return result;
	}

	@Override
	public void successUpdate(String synTaskNo) {
		jdbcTemplate.update("update itf_messagehuifangentity set fs_status=1,sendtime=? where syn_task_no=?",
				new Object[] { new Timestamp(System.currentTimeMillis()),synTaskNo });
	}

	@Override
	public  void failureUpdate(String synTaskNo,String error_msg){
		jdbcTemplate.update("update itf_messagehuifangentity set exe_count=exe_count + 1,sendtime=?,error_msg=? where syn_task_no=?",
				new Object[] { new Timestamp(System.currentTimeMillis()),error_msg,synTaskNo});
	}

}
