package com.ym.mec.web.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.SysConfig;
import com.ym.mec.biz.service.SysTenantConfigService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.tenant.TenantContextHolder;
import com.yonge.log.model.AuditLogAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

	@Autowired
	private SysTenantConfigService sysTenantConfigService;
	@Autowired
	private SysUserFeignService sysUserFeignService;

	@ApiOperation(value = "参数列表")
	@GetMapping(value = "queryAll")
    @PreAuthorize("@pcs.hasPermissions('sysTenantConfig/queryAll')")
	public Object queryAll(String group) {
		Map<String,Object> params = new HashMap<String, Object>(2);
		params.put("group", group);
		params.put("tenantId", TenantContextHolder.getTenantId());
		return succeed(sysTenantConfigService.queryAll(params));
	}

	@ApiOperation(value = "批量调整配置文件")
	@PostMapping(value = "batchUpSet")
    @PreAuthorize("@pcs.hasPermissions('sysTenantConfig/batchUpSet')")
	@AuditLogAnnotation(operateName = "批量调整配置文件")
	public Object batchUpSet(@RequestBody List<SysConfig> sysConfigs) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		sysTenantConfigService.batchUpSet(sysConfigs,sysUser.getTenantId());
		return succeed();
	}

	@ApiOperation(value = "获取单个配置")
	@PostMapping(value = "findByName")
    @PreAuthorize("@pcs.hasPermissions('sysTenantConfig/findByName')")
	public Object findByName(String configName) {
		return succeed(sysTenantConfigService.getTenantConfigValue(configName,TenantContextHolder.getTenantId()));
	}
}
