package com.tourism.webadmin.ws.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.tourism.webadmin.app.website.dto.MessageDTO;
import com.tourism.webadmin.back.model.TourImMessage;
import com.tourism.webadmin.back.service.TourImMessageService;
import com.tourism.webadmin.ws.service.IMessageService;
//import com.tourism.webadmin.ws.utils.AESUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;

import java.util.Date;

public class IGroupMessageServiceImpl implements IMessageService {

    private ApplicationContext applicationContext;

    private TourImMessageService tourImMessageService;

//    private AESUtils aesUtil;

    public IGroupMessageServiceImpl(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
//        this.aesUtil=applicationContext.getBean(AESUtils.class);
        this.tourImMessageService=applicationContext.getBean(TourImMessageService.class);
    }
    @Override
    public boolean save(MessageDTO message) {
        TourImMessage bean = BeanUtil.toBean(message, TourImMessage.class);
        Long l = Long.valueOf(message.getSendUserId());
        if (StringUtils.isBlank(bean.getReadByUsers())){
            bean.setReadByUsers(message.getSendUserId());
        }else {
            bean.setReadByUsers(l.toString());
        }
        bean.setCreateUserId(l);
        bean.setCreateTime(new Date());
        bean.setUpdateTime(new Date());
        bean.setUpdateUserId(l);
        bean.setMessageContent(JSON.toJSONString(message));
        return tourImMessageService.save(bean);
    }
}
