package com.yonge.cooleshow.admin.controller;

import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.utils.http.HttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yonge.cooleshow.biz.dal.entity.SysConfig;
import com.yonge.cooleshow.biz.dal.service.SysConfigService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.toolset.log.model.AuditLogAnnotation;

/** 
 * 系统配置控制层
 */
@RestController
@Api(tags = "系统参数设置")
@RequestMapping(value = "sysConfig")
public class SysConfigController extends BaseController {

	@Autowired
	private SysConfigService sysConfigService;

	@ApiOperation(value = "参数列表")
	@GetMapping(value = "list")
    @PreAuthorize("@pcs.hasPermissions('sysConfig/list')")
	public Object configList(String group) {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("group", group);
		List<SysConfig> configs = sysConfigService.findAll(params);
		return succeed(configs);
	}

	@ApiOperation(value = "修改参数")
	@PostMapping(value = "update")
    @PreAuthorize("@pcs.hasPermissions('sysConfig/update')")
	@AuditLogAnnotation(operateName = "修改参数")
	public Object update(SysConfig config) {
		config.setModifyOn(new Date());
		sysConfigService.updateConfig(config);
		return succeed();
	}

	@ApiOperation(value = "修改参数")
	@PostMapping(value = "batchUpdate")
    @PreAuthorize("@pcs.hasPermissions('sysConfig/batchUpdate')")
	@AuditLogAnnotation(operateName = "修改参数")
	public Object batchUpdate(@RequestBody List<SysConfig> configList) {
		sysConfigService.batchUpdate(configList);
		return succeed();
	}

	@ApiOperation(value = "新增参数")
	@PostMapping(value = "add")
    @PreAuthorize("@pcs.hasPermissions('sysConfig/add')")
	public Object addConfig(SysConfig config) {
		if (config == null)
			return failed("参数无效");
		if (StringUtils.isBlank(config.getParamName())) {
			return failed("参数名称不能为空");
		}
		if (StringUtils.isBlank(config.getParamValue())) {
			return failed("参数值不能为空");
		}
		config.setCreateOn(new Date());
		config.setModifyOn(new Date());
		return sysConfigService.insert(config) > 0 ? succeed() : failed("添加失败");
	}

	@ApiOperation(value = "查询参数")
	@GetMapping(value = "get")
    @PreAuthorize("@pcs.hasPermissions('sysConfig/get')")
	public Object getConfig(Long id) {
		if (id == null || id <= 0)
			return failed("请检查输入的ID");
		return succeed(sysConfigService.get(id));
	}

	@ApiOperation(value = "查询参数")
	@GetMapping(value = "queryByParamName")
    @PreAuthorize("@pcs.hasPermissions('sysConfig/queryByParamName')")
	public Object queryByParamName(String paramName) {
		if(StringUtils.isBlank(paramName)){
			return failed("参数不能为空");
		}
		return succeed(sysConfigService.findByParamName(paramName));
	}

	@GetMapping(value = "findConfigValue")
	public HttpResponseResult<String> findConfigValue(String paramName) {
		if(StringUtils.isBlank(paramName)){
			return failed("参数不能为空");
		}
		return succeed(sysConfigService.findConfigValue(paramName));
	}

	@ApiOperation(value = "url短链接")
	@PostMapping("shortURL")
	public HttpResponseResult<String> shortURL(String orginURL) {
		return succeed(HttpUtil.getSortUrl(orginURL));
	}
}
