package com.lutao.cloud.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lutao.cloud.admin.api.entity.SysPublicParam;
import com.lutao.cloud.admin.service.SysPublicParamService;
import com.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.common.log.annotation.SysLog;
import com.lutao.cloud.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 公共参数
 *
 * @author Lucky
 * @date 2019-04-29
 */
@Api(value = "/param", tags ={"公共参数"})
@ApiIgnore
@RestController
@AllArgsConstructor
@RequestMapping("/param")
public class SysPublicParamController {

	private final SysPublicParamService sysPublicParamService;

	/**
	 * 通过key查询公共参数值
	 * @param publicKey
	 * @return
	 */
	@ApiOperation(value = "查询公共参数值", notes = "根据key查询公共参数值", httpMethod = "GET")
	@Inner(value = false)
	@GetMapping("/publicValue/{publicKey}")
	public MessageInfo<String> publicKey(@PathVariable("publicKey") String publicKey){
		return MessageInfo.ok(sysPublicParamService.getSysPublicParamKeyToValue(publicKey));
	}

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param sysPublicParam 公共参数
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询", httpMethod = "GET")
	@GetMapping("/page")
	public MessageInfo<IPage<SysPublicParam>> getSysPublicParamPage(Page page, SysPublicParam sysPublicParam){
		return MessageInfo.ok(sysPublicParamService.page(page, Wrappers.query(sysPublicParam)));
	}

	/**
	 * 通过id查询公共参数
	 * @param publicId id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询公共参数", notes = "通过id查询公共参数", httpMethod = "GET")
	@GetMapping("/{publicId}")
	public MessageInfo<SysPublicParam> getById(@PathVariable("publicId") Long publicId){
		return MessageInfo.ok(sysPublicParamService.getById(publicId));
	}

	/**
	 * 新增公共参数
	 * @param sysPublicParam 公共参数
	 * @return R
	 */
	@ApiOperation(value = "新增公共参数", notes = "新增公共参数", httpMethod = "POST")
	@SysLog("新增公共参数")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('admin_syspublicparam_add')")
	public MessageInfo<Boolean> save(@RequestBody SysPublicParam sysPublicParam){
		return MessageInfo.ok(sysPublicParamService.save(sysPublicParam));
	}

	/**
	 * 修改公共参数
	 * @param sysPublicParam 公共参数
	 * @return R
	 */
	@ApiOperation(value = "修改公共参数", notes = "修改公共参数", httpMethod = "PUT")
	@SysLog("修改公共参数")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('admin_syspublicparam_edit')")
	public MessageInfo<Boolean> updateById(@RequestBody SysPublicParam sysPublicParam){
		return sysPublicParamService.updateParam(sysPublicParam);
	}

	/**
	 * 通过id删除公共参数
	 * @param publicId id
	 * @return R
	 */
	@ApiOperation(value = "删除公共参数", notes = "删除公共参数", httpMethod = "DELETE")
	@SysLog("删除公共参数")
	@DeleteMapping("/{publicId}")
	@PreAuthorize("@pms.hasPermission('admin_syspublicparam_del')")
	public MessageInfo<Boolean> removeById(@PathVariable Long publicId){
		return sysPublicParamService.removeParam(publicId);
	}

}
