package com.copy.trade.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.copy.trade.dao.MessageMapper;
import com.copy.trade.entity.po.Message;
import com.copy.trade.enums.MessageTypeEnum;
import com.copy.trade.service.IMessageService;

@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {

	@Override
	public boolean saveMessage(Long customerId, String symbol, MessageTypeEnum messageType, BigDecimal volume, BigDecimal triggerPrice, BigDecimal dealPrice,String content,Long date) {
		// TODO Auto-generated method stub
		Message message = new Message();
		message.setCustomerId(customerId);
		message.setSymbol(symbol);
		message.setMessageType(messageType);
		message.setVolume(volume);
		message.setTriggerPrice(triggerPrice);
		message.setDealPrice(dealPrice);
		message.setCreateTime(date == null? new Date(): longToDate(date));
		message.setIsRead(false);
		message.setContent(content);
		save(message);
		return true;
	}

	@Override
	public boolean saveMessage(Message message) {
		// TODO Auto-generated method stub
		if(message.getCreateTime() == null) {
			message.setCreateTime(new Date());
		}
		message.setIsRead(false);
		save(message);
		return false;
	}
	
	private static Date longToDate(Long lo) {
	    Date date = null;
		try {
			SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(sd.format(new Date(lo)));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return date;
	}
	
	public static void main(String[] args) {
	    Long time = 1673575798116L;
	    System.out.println("----->转换结果：" + longToDate(time));
	}

}
