package com.rf.psychological.param.rest;

import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.param.dao.model.ParamEntry;
import com.rf.psychological.param.service.ParamService;
import com.rf.psychological.security.AesEncryptUtils;
import com.rf.psychological.security.SafetyProcess;
import com.rf.psychological.utils.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:zzf
 * @Date:2022/7/1:18:12
 * @Description:系统参数控制器
 */
@RestController
@RequestMapping("/param")
@Api(tags = "系统参数")
public class ParamController extends BaseController {

    @Autowired
    private ParamService paramService;

    @PostMapping("/save")
    @SafetyProcess
    public Result save(@RequestBody String json) throws Exception {
        JSONObject jsonObject = JSONObject.parseObject(AesEncryptUtils.decrypt(JSONObject.parseObject(json).getString("data")));
        ParamEntry paramEntry = jsonObject.toJavaObject(ParamEntry.class);
        paramEntry = this.paramService.save(paramEntry);
        return success(paramEntry);
    }

    @GetMapping("/find/{paramName}")
    @SafetyProcess
    public Result find(@PathVariable String paramName){
        ParamEntry paramEntry = this.paramService.findByParamName(paramName);
        return success(paramEntry);
    }

}
