package cn.superdesk.uniorder.redis.model001.channel;

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.enums.common.CommonProcessResultEnum;
import cn.superdesk.uniorder.common.excel.model.ExcelRow;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.mq.redis.dto.ExcelImportTaskMessage;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.operation.ExcelImportLog;
import cn.superdesk.uniorder.dto.param.invoice.excel.InvoiceInvoicedHistoryExcelRow;
import cn.superdesk.uniorder.dto.param.invoice.excel.InvoiceInvoicedNormalExcelRow;
import cn.superdesk.uniorder.service.invoice.customer.invoiced.InvoiceInvoicedService;
import cn.superdesk.uniorder.service.operation.ExcelImportLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Description: 继承方式
 * @Author: linjinyu
 * @CreateDate: 2021/2/4 下午3:02
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/2/4 下午3:02
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Component
public class ProcessMessagesChannel003 implements MessageListener {

	@Resource
	private StringRedisTemplate stringRedisTemplate;

	@Autowired
	private ExcelImportLogService  excelImportLogService;
	@Autowired
	private InvoiceInvoicedService invoiceInvoicedService;

//	@Autowired
//	private StringRedisSerializer              stringRedisSerializer;
//	@Autowired
//	private GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer;
//	@Autowired
//	private JdkSerializationRedisSerializer    jdkSerializationRedisSerializer;

	@Override
	public void onMessage(Message message, byte[] pattern) {

//		test0
		RedisSerializer<String> valueSerializer = stringRedisTemplate.getStringSerializer();
		String                  deserialize     = valueSerializer.deserialize(message.getBody());
		log.info("ProcessMessagesChannel003 onMessage 消息 {}", deserialize);

		ExcelImportTaskMessage excelImportTaskMessage = AnJsonUtils.toObject(deserialize, ExcelImportTaskMessage.class);


		// 有空再优化写法
		if (excelImportTaskMessage.getTopic().equals("cn.superdesk.uniorder.dto.param.invoice.excel.InvoiceInvoicedHistoryExcelRow")) {

			InvoiceInvoicedHistoryExcelRow excelRow     = AnJsonUtils.toObject(excelImportTaskMessage.getContent(), InvoiceInvoicedHistoryExcelRow.class);
			LoginSysUser                   loginSysUser = AnJsonUtils.toObject(excelImportTaskMessage.getLoginSysUser(), LoginSysUser.class);

			if (null != excelRow && null != excelRow.getValidateCode() && CommonProcessResultEnum.正常.getDesc().equals(excelRow.getValidateCode())) {
				invoiceInvoicedService.saveOrUpdate4InvoiceInvoicedHistoryExcelRowByInvoiceNumber(excelRow, loginSysUser);
			}

			excelLog(excelImportTaskMessage, excelRow, loginSysUser);


		} else if (excelImportTaskMessage.getTopic().equals("cn.superdesk.uniorder.dto.param.invoice.excel.InvoiceInvoicedNormalExcelRow")) {

			InvoiceInvoicedNormalExcelRow excelRow     = AnJsonUtils.toObject(excelImportTaskMessage.getContent(), InvoiceInvoicedNormalExcelRow.class);
			LoginSysUser                  loginSysUser = AnJsonUtils.toObject(excelImportTaskMessage.getLoginSysUser(), LoginSysUser.class);

			if (null != excelRow && null != excelRow.getValidateCode() && CommonProcessResultEnum.正常.getDesc().equals(excelRow.getValidateCode())) {
				invoiceInvoicedService.saveOrUpdate4InvoiceInvoicedNormalExcelRowByInvoiceNumber(excelRow, loginSysUser);
			}

			excelLog(excelImportTaskMessage, excelRow, loginSysUser);

		}


//		StringRedisSerializer valueSerializer = stringRedisTemplate.getStringSerializer();
//		String                        deserialize     = valueSerializer.deserialize(message.getBody());
//		log.info("ProcessMessagesChannel003 onMessage 消息 {}", deserialize);

//		test1
//		byte[] body    = message.getBody();//请使用valueSerializer
//		byte[] channel = message.getChannel();
//		String msg     = (String) stringRedisSerializer.deserialize(body);
//		String topic   = (String) stringRedisSerializer.deserialize(channel);
//		System.out.println("我是sub,监听" + topic + ",我收到消息:" + msg);


//		test2
//		byte[] body    = message.getBody();//请使用valueSerializer
//		byte[] channel = message.getChannel();
//		String msg     = (String) stringRedisSerializer.deserialize(body);
//		String topic   = (String) stringRedisSerializer.deserialize(channel);
//		System.out.println("我是sub2,监听" + topic + ",我收到消息:" + msg);


//		test3
//		byte[] body    = message.getBody();//请使用valueSerializer
//		byte[] channel = message.getChannel();
//		User   u       = jackson2JsonRedisSerializer.deserialize(body, User.class);
//
//		String topic = (String) stringRedisSerializer.deserialize(channel);
//		System.out.println("我是sub3,监听" + topic + ",我收到消息:" + u.getId() + "--" + u.getName());

	}

	private void excelLog(ExcelImportTaskMessage excelImportTaskMessage, ExcelRow excelRow, LoginSysUser loginSysUser) {

		/****************************************** 统一入库 start ******************************************/
		ExcelImportLog excelImportLog = new ExcelImportLog();
		excelImportLog.setBatchId(excelImportTaskMessage.getBatchId());
		excelImportLog.setOperationType(excelImportTaskMessage.getTopic());

		excelImportLog.setCreateTime(DateUtil.date());
		excelImportLog.setCreateUserId(loginSysUser.getSysUserId());
		excelImportLog.setCreateUserName(loginSysUser.getSysUserName());

		excelImportLog.setValidateCode(excelRow.getValidateCode());
		excelImportLog.setValidateMessage(excelRow.getValidateMessage());

		if (null == excelRow.getProcessResult()) {
			excelImportLog.setProcessResult(CommonProcessResultEnum.初始.getKey());
		} else {
			excelImportLog.setProcessResult(excelRow.getProcessResult());
		}

		if (null == excelRow.getProcessReport()) {
			excelImportLog.setProcessReport("");
		} else {
			excelImportLog.setProcessReport(excelRow.getProcessReport());
		}


		excelImportLog.setRowData(excelImportTaskMessage.getContent());

		excelImportLogService.addExcelImportLog(excelImportLog);
	}

}
