package com.yami.shop.platform.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.SearchTerm;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.R;
import com.yami.shop.service.SearchTermService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/platform/SearchTerm")
public class SearchTermController {

    @Autowired
    private SearchTermService searchTermService;

    @GetMapping("/page")
    @ApiOperation("分页查询搜索词")
    public R<IPage<SearchTerm>> page(SearchTerm searchTerm, PageParam<SearchTerm> page) {
        IPage<SearchTerm> SearchTerms = searchTermService.page(page, new LambdaQueryWrapper<SearchTerm>()
                .like(StrUtil.isNotBlank(searchTerm.getSearchName()), SearchTerm::getSearchName, searchTerm.getSearchName())
                .eq(Objects.nonNull(searchTerm.getType()), SearchTerm::getType, searchTerm.getType())
                .orderByDesc(SearchTerm::getCreateTime)
        );
        return R.SUCCESS(SearchTerms);
    }

    @GetMapping("/info/{id}")
    @ApiOperation("主键查询")
    public R<SearchTerm> info(@PathVariable("id") Long id) {
        return R.SUCCESS(searchTermService.getById(id));
    }

    @PostMapping
    @ApiOperation("新增搜索词")
    public R<Void> save(@RequestBody @Valid SearchTerm searchTerm) {
        searchTerm.setCreateTime(new Date());
        searchTermService.save(searchTerm);
        return R.SUCCESS();
    }

    @PutMapping
    @ApiOperation("修改搜索词")
    public R<Void> update(@RequestBody @Valid SearchTerm searchTerm) {
        searchTermService.updateById(searchTerm);
        return R.SUCCESS();
    }

    @DeleteMapping
    @ApiOperation("删除搜索词")
    public R<Void> delete(@RequestBody List<Long> ids) {
        searchTermService.removeByIds(ids);
        return R.SUCCESS();
    }
}
