package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.biz.dal.dto.search.MemberPriceSettingsSearch;
import com.yonge.toolset.mybatis.support.PageUtil;
import com.yonge.cooleshow.biz.dal.vo.MemberPriceSettingsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.MemberPriceSettings;
import com.yonge.cooleshow.biz.dal.service.MemberPriceSettingsService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;

import javax.validation.Valid;

@RequestMapping("memberPriceSettings")
@Api(tags = "会员价格设置")
@RestController
public class MemberPriceSettingsController extends BaseController {
	@Autowired
	private MemberPriceSettingsService memberPriceSettingsService;
	@Autowired
	private SysUserFeignService sysUserFeignService;

	/**
	 * 查询单条
	 */
	@GetMapping("/detail/{id}")
	@ApiOperation(value = "详情", notes = "传入id")
	public HttpResponseResult<MemberPriceSettingsVo> detail(@PathVariable("id") Long id) {
		return succeed(memberPriceSettingsService.detail(id));
	}

	/**
	 * 查询分页
	 */
	@GetMapping("/list")
	@ApiOperation(value = "查询分页", notes = "传入memberPriceSettingsSearch")
	@PreAuthorize("@pcs.hasPermissions('memberPriceSettings/list')")
	public HttpResponseResult<PageInfo<MemberPriceSettingsVo>> page(MemberPriceSettingsSearch query) {
		IPage<MemberPriceSettingsVo> pages = memberPriceSettingsService.selectPage(PageUtil.getPage(query), query);
		return succeed(PageUtil.pageInfo(pages));
	}

	/**
	 * 修改
	 */
	@PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ApiOperation(value = "修改", notes = "传入memberPriceSettings")
	@PreAuthorize("@pcs.hasPermissions('memberPriceSettings/update')")
	public HttpResponseResult<Boolean> update(@Valid @RequestBody MemberPriceSettings memberPriceSettings) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || user.getId() == null) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		memberPriceSettings.setUpdateBy(user.getId());
		memberPriceSettings.setUpdateTime(new Date());
		return status(memberPriceSettingsService.updateById(memberPriceSettings));
	}
}
