package org.springblade.gateway.common_gateway.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.gateway.common_gateway.enums.PlatformSettingEnum;
import org.springblade.gateway.common_gateway.util.PlatformSettingUtil;
import org.springblade.sing.desk.entity.PlatformSetting;
import org.springblade.sing.desk.service.IPlatformSettingService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Silent
 * @Description
 * @Date: Created in 15:00 2021/11/9
 * @Modified By:
 */
@RestController
@AllArgsConstructor
@RequestMapping("app/platformsetting")
@Api(value = "平台设置", tags = "平台设置接口")
public class AppPlatFormSettingController extends BladeController {
	private IPlatformSettingService platformSettingService;

	/**
	 * 将平台设置放入缓存(预热)
	 */
	@PostConstruct
	public void init(){
		for (PlatformSetting platformSetting : platformSettingService.list()) {
			PlatformSettingUtil.setPlatformSettingToCache(platformSetting);
		}
	}

	/**
	 * 获取平台设置
	 */
	@GetMapping("/getPlatformValue")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "获取平台配置", notes = "传入key")
	public R<String> getPlatformSetting(@RequestParam PlatformSettingEnum platformSettingEnum,
										@ApiParam(value = "活动ID") @RequestParam(required = false) Long activeId){
		return R.data(PlatformSettingUtil.getPlatformSettingByCache(platformSettingEnum.name(),activeId));
	}

	/**
	 * 获取平台配置
	 */
	@GetMapping("/getPlatformValueByList")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "获取平台配置", notes = "传入key")
	public R<Map<String,String>> getPlatformValueByList(@ApiParam(value = "平台配置key",required = true) @RequestParam String keys,
														@ApiParam(value = "活动ID") @RequestParam(required = false) Long activeId){
		String[] split = keys.split(",");
		Map<String, String> result = new HashMap<>();
		for (String key : split) {
			String value = PlatformSettingUtil.getPlatformSettingByCache(key,activeId);
			if(StringUtils.isNotEmpty(value)){
				result.put(key,value);
			}
		}
		return R.data(result);
	}

	/**
	 * 修改平台设置
	 */
	@PostMapping("/updatePlatformValue")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "更新平台配置", notes = "传入key")
	public R<Boolean> getPlatformSetting(@Valid @RequestBody PlatformSetting platformSetting){
		PlatformSettingUtil.setPlatformSettingToCache(platformSetting);
		LambdaUpdateWrapper<PlatformSetting> lambdaUpdateWrapper = Wrappers.<PlatformSetting>lambdaUpdate()
			.eq(PlatformSetting::getKey, platformSetting.getKey());
		if(ObjectUtils.isNotEmpty(platformSetting.getActiveId())){
			lambdaUpdateWrapper.eq(PlatformSetting::getActiveId,platformSetting.getActiveId());
			platformSetting.setId(null);
		}
		return R.data(platformSettingService.saveOrUpdate(platformSetting, lambdaUpdateWrapper));
	}
}
