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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zfire.jiasm.syncdata.plus.entity.SmsRecord;
import com.zfire.jiasm.syncdata.plus.service.SmsRecordService;
import com.zfire.jiasm.syncdata.service.PublicService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SMSSelfGetArrivedServiceImpl {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Resource
	SmsRecordService smsRecordService;
	@Resource
	PublicService publicService;

	@Transactional(rollbackFor = Exception.class)
	public void insertData(JSONObject datas) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		JSONArray reports = datas.getJSONArray("reports");
		for (int i = 0; i < reports.size(); i++) {
			JSONObject data = reports.getJSONObject(i);

			String sql = "insert into itf_receive_sms (smsType,fromNum,content,recvTime,"
					+ "dateSent,status,appendCode,action,deliverCode,reqId,smsCount,spCode,subappend) "
					+ "values (?,?,?,?,?,?,?,?,?,?,?,?,?) ";

			Object[] insertValue = new Object[13];
			insertValue[0] = data.getString("smsType");
			insertValue[1] = data.getString("fromNum");
			insertValue[2] = data.getString("content");

			insertValue[3] = sdf.parse(data.getString("recvTime"));

			if (data.containsKey("dateSent")) {
				insertValue[4] = sdf.parse(data.getString("dateSent"));
			} else {
				insertValue[4] = null;
			}

			insertValue[5] = data.getOrDefault("status", "0");
			insertValue[6] = data.getOrDefault("appendCode", "");
			insertValue[7] = data.getString("action");
			insertValue[8] = data.getOrDefault("deliverCode", "");
			insertValue[9] = data.getOrDefault("reqId", "");
			insertValue[10] = data.getOrDefault("smsCount", "0");
			insertValue[11] = data.getOrDefault("spCode", "");
			insertValue[12] = data.getOrDefault("subAppend", "");

			jdbcTemplate.update(sql, insertValue);
			// 同步回复内容信息smsrecord
			syncSmsRecord(data);
		}
	}

	private void syncSmsRecord(JSONObject data) {
		// 同步工单回复信息 这里使用手机号+发送码来确认 存在无对应的情况不处理
		// 最近两个月的一定只有一条，为防止出现问题，用list
		List<SmsRecord> rec = smsRecordService.lambdaQuery()
				.eq(SmsRecord::getPhone, data.getString("fromNum"))
				.eq(SmsRecord::getFsma, data.getOrDefault("subAppend", ""))
				.ge(SmsRecord::getCreateTime,publicService.stepMonth(new Date(),-2)).list();
		if (CollectionUtils.isNotEmpty(rec)){
			List<SmsRecord> u = new ArrayList<>();
			for (SmsRecord item:rec){
				item.setReplyContent(data.getString("content"));
				item.setReplyTime(new Date());
				u.add(item);
			}
			smsRecordService.updateBatchById(u);
			u.clear();


			// 差评时向申诉表插入数据


		}
	}

}
