package com.softwarebr.nunu.web.admin;

import com.brframework.commoncms.annatotion.AdminMenu;
import com.brframework.commoncms.annatotion.layout.EditLayout;
import com.brframework.commoncms.core.Icons;
import com.brframework.commonweb.json.JSONResult;
import com.brframework.commonwebadmin.aop.annotation.AOLog;
import com.brframework.commonwebbase.service.DictionaryService;
import com.softwarebr.nunu.config.domain.ConfigService;
import com.softwarebr.nunu.domain.PayConfig;
import com.softwarebr.nunu.domain.SMSConfig;
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.*;

/**
 * 系统配置管理
 * @author xu
 * @date 2019/10/18 16:46
 */
@RestController
@Api(tags = "配置中心")
@AdminMenu(menuName = "配置中心", icon = Icons.DEVELOPMENT_MANAGE)
@RequestMapping("/admin/access")
public class AdminSystemConfigController {
    @Autowired
    ConfigService<PayConfig> payConfigService;
    @Autowired
    ConfigService<SMSConfig> smsConfigService;
    @Autowired
    DictionaryService dictionaryService;

    @ApiOperation(value = "支付配置", notes = "支付配置")
    @PostMapping("/v1/system-config/pay/config")
    @EditLayout(paddingMappingMethod = "getPay")
    @AdminMenu(menuName = "支付配置", order = 1)
    @PreAuthorize("hasRole('system-config-pay-config')")
    @AOLog
    public JSONResult postPay(@RequestBody PayConfig param) {
        payConfigService.set(param);
        return JSONResult.ok();
    }

    @ApiOperation(value = "获取支付配置", notes = "获取支付配置")
    @PreAuthorize("hasRole('system-config-pay-config')")
    @GetMapping("/v1/system-config/pay/config")
    public JSONResult<PayConfig> getPay() {
        return JSONResult.ok(payConfigService.get());
    }

    @ApiOperation(value = "短信配置", notes = "短信配置")
    @PostMapping("/v1/system-config/sms/config")
    @EditLayout(paddingMappingMethod = "getSms")
    @AdminMenu(menuName = "短信配置", order = 2)
    @PreAuthorize("hasRole('system-config-sms-config')")
    @AOLog
    public JSONResult postSms(@RequestBody SMSConfig param) {
        smsConfigService.set(param);
        return JSONResult.ok();
    }

    @ApiOperation(value = "获取短信配置", notes = "获取短信配置")
    @PreAuthorize("hasRole('system-config-sms-config')")
    @GetMapping("/v1/system-config/sms/config")
    public JSONResult<SMSConfig> getSms() {
        return JSONResult.ok(smsConfigService.get());
    }

}
