package com.fzm.mall.server.admin.opration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.admin.opration.entity.dto.KeywordDTO;
import com.fzm.mall.server.admin.opration.entity.dto.KeywordSaveDTO;
import com.fzm.mall.server.admin.opration.mapper.KeywordMapper;
import com.fzm.mall.server.admin.opration.model.Keyword;
import com.fzm.mall.server.admin.opration.service.IKeywordService;
import com.fzm.mall.server.admin.util.CopyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 运营中心-关键字 服务实现类
 * </p>
 *
 * @author wulixian
 * @since 2021-03-22
 */
@Service
@Transactional
public class KeywordServiceImpl extends ServiceImpl<KeywordMapper, Keyword> implements IKeywordService {

    @Override
    public void save(KeywordSaveDTO keywordSaveDTO) {
        Keyword keyword = CopyUtil.to(keywordSaveDTO, Keyword.class);
        keyword.init();
        this.save(keyword);
    }

    /**
     * 逻辑删除
     *
     * @param operationKeywordId
     */
    @Override
    public void delete(String operationKeywordId) {
        UpdateWrapper<Keyword> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("operation_keyword_id", operationKeywordId)
                .set("status", true);
        this.update(updateWrapper);
    }

    @Override
    public List<KeywordDTO> listAll() {
        QueryWrapper<Keyword> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", false)
                .orderByAsc("display_order")
                .orderByDesc("update_time");
        List<Keyword> list = this.list(queryWrapper);
        return CopyUtil.toList(list, KeywordDTO.class);
    }
}
