package com.yida.data.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.message.EduMessageStrategy;
import com.yida.data.user.mapper.EduMessageStrategyMapper;
import com.yida.data.user.service.EduMessageStrategyService;
import com.yida.data.user.vo.EduMessageStrategyVo;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;

/**
 * 消息策略 Service实现
 *
 * @author wjm
 * @date 2021-08-23 19:39:20
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class EduMessageStrategyServiceImpl extends ServiceImpl
        <EduMessageStrategyMapper, EduMessageStrategy> implements EduMessageStrategyService {

    private final EduMessageStrategyMapper eduMessageStrategyMapper;

    @Override
    public boolean saveOrUpdateMessage(EduMessageStrategyVo eduMessageStrategyVo, CurrentUser currentUser) {
        EduMessageStrategy eduMessageStrategy = new EduMessageStrategy();
        eduMessageStrategy.setId(eduMessageStrategyVo.getId());
        eduMessageStrategy.setSchoolId(eduMessageStrategyVo.getSchoolId());
        eduMessageStrategy.setContent(eduMessageStrategyVo.getContent());
        eduMessageStrategy.setType(eduMessageStrategyVo.type);
        eduMessageStrategy.setSubType(eduMessageStrategyVo.getSubType());

        if (ObjectUtil.isNotEmpty(eduMessageStrategyVo.getId())) {
            eduMessageStrategy.setCreateDate(LocalDateTime.now());
            eduMessageStrategy.setCreateDate(LocalDateTime.now());
            eduMessageStrategy.setCreateId(currentUser.getUserId());
        } else {
            eduMessageStrategy.setUpdateDate(LocalDateTime.now());
            eduMessageStrategy.setUpdateId(currentUser.getUserId());
        }
        return this.saveOrUpdate(eduMessageStrategy);
    }
}
