/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

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.HotSearch;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.platform.util.SecurityUtils;
import com.yami.shop.service.HotSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import com.yami.shop.common.util.R;
/**
 * 热门搜索管理实现
 *
 * @author lgh
 * @version 1.0.0
 * @since 2019-03-27
 */
@RestController
@RequestMapping("/platform/hotSearch")
public class HotSearchController {

    @Autowired
    private HotSearchService hotSearchService;

	/**
	 * 分页获取
	 */
    @GetMapping("/page")
//	@PreAuthorize("@pms.hasPermission('platform:hotSearch:page')")
	public R<IPage<HotSearch>> page(HotSearch hotSearch, PageParam<HotSearch> page){
		IPage<HotSearch> hotSearchs = hotSearchService.page(page,new LambdaQueryWrapper<HotSearch>()
			.eq(HotSearch::getShopId, Constant.PLATFORM_SHOP_ID)
			.like(StrUtil.isNotBlank(hotSearch.getContent()), HotSearch::getContent,hotSearch.getContent())
				.like(StrUtil.isNotBlank(hotSearch.getTitle()), HotSearch::getTitle,hotSearch.getTitle())
			.eq(hotSearch.getStatus()!=null, HotSearch::getStatus,hotSearch.getStatus())
		);
		return R.SUCCESS(hotSearchs);
	}

    /**
	 * 获取信息
	 */
	@GetMapping("/info/{id}")
//	@PreAuthorize("@pms.hasPermission('platform:hotSearch:info')")
	public R<HotSearch> info(@PathVariable("id") Long id){
		HotSearch hotSearch = hotSearchService.getById(id);
		return R.SUCCESS(hotSearch);
	}

	/**
	 * 保存
	 */
	@PostMapping
//	@PreAuthorize("@pms.hasPermission('platform:hotSearch:save')")
	public R<Void> save(@RequestBody @Valid HotSearch hotSearch){
		hotSearch.setRecDate(new Date());
		hotSearch.setShopId(Constant.PLATFORM_SHOP_ID);
		hotSearchService.save(hotSearch);
		//清除缓存
		hotSearchService.removeHotSearchDtoCacheByshopId(Constant.PLATFORM_SHOP_ID);
		return R.SUCCESS();
	}

	/**
	 * 修改
	 */
	@PutMapping
//	@PreAuthorize("@pms.hasPermission('platform:hotSearch:update')")
	public R<Void> update(@RequestBody @Valid HotSearch hotSearch){
		hotSearchService.updateById(hotSearch);
		//清除缓存
		hotSearchService.removeHotSearchDtoCacheByshopId(Constant.PLATFORM_SHOP_ID);
		return R.SUCCESS();
	}

	/**
	 * 删除
	 */
	@DeleteMapping
//	@PreAuthorize("@pms.hasPermission('platform:hotSearch:delete')")
	public R<Void> delete(@RequestBody List<Long> ids){
		hotSearchService.removeByIds(ids);
		//清除缓存
		hotSearchService.removeHotSearchDtoCacheByshopId(Constant.PLATFORM_SHOP_ID);
		return R.SUCCESS();
	}
}
