package com.yfd.platform.system.controller;


import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.system.domain.SysConfig;
import com.yfd.platform.system.service.ISysConfigService;
import com.yfd.platform.system.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.IOException;
import java.sql.Timestamp;


/**
 * <p>
 * 系统全局配置 前端控制器
 * </p>
 *
 * @author zhengsl
 * @since 2022-01-19
 */
@RestController
    @RequestMapping("/system/config")
@Api(tags = "系统全局配置")
public class SysConfigController {
    @Resource
    private ISysConfigService configService;

    @Resource
    private IUserService userService;

    @PostMapping("/getOneById")
    @ApiOperation("根据id查询全局配置详情记录")
    @ResponseBody
    public SysConfig getOneById(String id){
        return configService.getById(id);
    }

    @PostMapping("/addConfig")
    @ApiOperation("根据id查询全局配置详情记录")
    @ResponseBody
    public ResponseResult addConfig(@RequestBody SysConfig  config ) throws IOException, UnsupportedAudioFileException {
        if (StrUtil.isEmpty(config.getId())){
            config.setId(IdUtil.fastSimpleUUID());        }
        config.setLastmodifier(userService.getUsername());
        config.setLastmodifydate(new Timestamp(System.currentTimeMillis()));
        boolean ok=configService.save(config);
        return ResponseResult.success();
    }

    @PostMapping("/updateById")
    @ApiOperation("根据id修改全局配置记录")
    @ResponseBody
    public ResponseResult updateById(@RequestBody SysConfig config) throws IOException, UnsupportedAudioFileException {
        config.setLastmodifier(userService.getUsername());
        config.setLastmodifydate(new Timestamp(System.currentTimeMillis()));
        boolean ok=configService.updateById(config);
        return ResponseResult.success();
    }


}
