package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.Keyword;
import com.yami.shop.bean.param.KeywordParam;
import com.yami.shop.dao.KeywordMapper;
import com.yami.shop.service.IKeywordService;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author wy
 */
@Service
public class KeywordServiceImpl extends ServiceImpl<KeywordMapper, Keyword> implements IKeywordService {

    @Resource
    private KeywordMapper keywordMapper;

    @Override
    public ResponseEntity<Void> addOrUpdate(KeywordParam keywordParam) {
        Keyword keyword = new Keyword();
        LocalDateTime now = LocalDateTime.now();
        BeanUtils.copyProperties(keywordParam, keyword);
        keyword.setCreateTime(now);
        keyword.setUpdateTime(now);
        if (Objects.isNull(keywordParam.getId())) {
            keywordMapper.insert(keyword);
        } else {
            keyword.setUpdateTime(LocalDateTime.now());
            keywordMapper.updateById(keyword);
        }
        return ResponseEntity.ok().build();
    }
}
