package com.yami.shop.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Keyword;
import com.yami.shop.bean.param.KeywordParam;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.IKeywordService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import com.yami.shop.common.util.R;
/**
 * 关键词管理实现
 *
 * @author wy
 * @version 1.0.0
 * @since 2025-01-01
 */
@Controller
@RequestMapping("/keyword")
public class KeywordController {

    @Resource
    private IKeywordService keywordService;

    @ApiOperation("添加编辑")
    @PostMapping("/addOrUpdate")
    public R<Void> addOrUpdate(@RequestBody KeywordParam keywordParam) {
         keywordService.addOrUpdate(keywordParam);
         return R.SUCCESS();
    }

    @ApiOperation("列表")
    @GetMapping("/search")
    public R<IPage<Keyword>> search(String name, PageParam<Keyword> page) {
        IPage<Keyword> result = keywordService.page(page, new LambdaQueryWrapper<Keyword>()
                .like(StringUtils.isNotEmpty(name), Keyword::getName, name)
                .orderByDesc(Keyword::getId));
        return R.SUCCESS(result);
    }

    @ApiOperation("查看")
    @GetMapping("/get/{id}")
    public R<Keyword> get(@PathVariable("id") Integer id) {
        Keyword keyword = keywordService.getById(id);
        return R.SUCCESS(keyword);
    }

    @ApiOperation("获取关键词类型下关键词")
    @GetMapping("/list")
    public R<List<Keyword>> list(@RequestParam("type") Integer type) {
        LocalDateTime now = LocalDateTime.now();
        List<Keyword> list = keywordService.list(new LambdaQueryWrapper<Keyword>()
                .eq(Keyword::getType, type)
                .ge(Keyword::getDeadTime, now));
        return R.SUCCESS(list);
    }

    @ApiOperation("获取关键词类型下关键词")
    @GetMapping("/remove/{id}")
    public R<Void> remove(@PathVariable("id") Integer id) {
        keywordService.removeById(id);
        return R.SUCCESS();
    }

}
