package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.search.HotSearchSearch;
import com.yonge.cooleshow.biz.dal.entity.HotSearch;
import com.yonge.cooleshow.biz.dal.service.HotSearchService;
import com.yonge.cooleshow.common.enums.YesOrNoEnum;
import com.yonge.toolset.mybatis.support.PageUtil;
import com.yonge.cooleshow.biz.dal.vo.HotSearchVo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;
import io.swagger.annotations.Api;
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;

@RestController
@RequestMapping("/hotSearch")
@Api(value = "热门搜索表", tags = "热门搜索表")
public class HotSearchController extends BaseController {

    @Autowired
    private HotSearchService hotSearchService;

	@Autowired
	private SysUserFeignService sysUserFeignService;
	/**
     * 查询单条
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "详情", notes = "传入id")
    public HttpResponseResult<HotSearch> detail(@PathVariable("id") Long id) {
		HotSearch hotSearch = hotSearchService.getById(id);
		if (hotSearch == null) {
			return failed("未找到对应信息");
		}
		return succeed(hotSearch);
	}
    
    /**
     * 查询分页
     */
    @PostMapping("/page")
    @ApiOperation(value = "查询分页", notes = "传入hotSearchSearch")
    public HttpResponseResult<PageInfo<HotSearchVo>> page(@RequestBody HotSearchSearch query) {
		IPage<HotSearchVo> pages = hotSearchService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
	}
    
    /**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "传入hotSearch")
	public HttpResponseResult save(@Valid @RequestBody HotSearch hotSearch) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || user.getId() == null) {
			return failed("用户信息不存在");
		}

		hotSearch.setCreateOn(new Date());
		hotSearch.setModifyOn(new Date());
		hotSearch.setCreateBy(user.getId());
		hotSearch.setUpdateBy(user.getId());
		hotSearch.setStatus(YesOrNoEnum.NO);

		return status(hotSearchService.save(hotSearch));
	}
    
    /**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "传入hotSearch")
	public HttpResponseResult update(@Valid @RequestBody HotSearch hotSearch) {
		if (hotSearch.getId() == null) {
			return failed("热门搜索id不能为空");
		}
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || user.getId() == null) {
			return failed("用户信息不存在");
		}
		hotSearch.setModifyOn(new Date());
		hotSearch.setUpdateBy(user.getId());

		return status(hotSearchService.update(hotSearch));
	}

 	/**
	 * 删除
	 */
	@PostMapping("/remove/{id}")
	@ApiOperation(value = "删除", notes = "传入ids")
	public HttpResponseResult remove(@PathVariable Long id) {

		if (id == null) {
			return failed("参数错误");
		}
		return status(hotSearchService.removeById(id));
	}


	@PostMapping("/status/{id}")
	@ApiOperation(value = "修改状态", notes = "传入ids")
	public HttpResponseResult status(@PathVariable Long id) {

		if (id == null) {
			return failed("参数错误");
		}
		return status(hotSearchService.status(id));
	}
}