package com.yami.shop.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.common.bean.SysConfig;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author chenlin
 * @DateTime: 2020/3/19 8:29
 * @description: 支付配置、文件上传配置、短信配置、快递配置、小程序配置、公众号配置
 */
@RestController
@RequestMapping("/sys/pconfig")
public class SysPconfigController {


    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 获取保存支付宝支付配置信息
     */
    @SysLog("获取配置信息")
    @GetMapping("/info/{key}")
    public ResponseEntity<String> info(@PathVariable("key")String key){
        return ResponseEntity.ok(sysConfigService.getValue(key));
    }

    /**
     * 保存配置
     */
    @SysLog("保存配置")
    @PostMapping("/save")
    public ResponseEntity<Void> save(@RequestBody @Valid SysConfig sysConfig){
        String paramValue = sysConfig.getParamValue();
        String paramKey = sysConfig.getParamKey();
        int count = sysConfigService.count(new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getParamKey,paramKey));
        if (count>0){
            sysConfigService.updateValueByKey(paramKey,paramValue);
        }else {
            sysConfigService.save(sysConfig);
        }
        sysConfigService.removeSysConfig(paramKey);
        return ResponseEntity.ok().build();
    }




}
