package com.liangjian.dataplatform.member.controller;

import com.liangjian.dataplatform.member.entity.response.ResultObject;
import com.liangjian.dataplatform.member.entity.vo.LayoutConfigVo;
import com.liangjian.dataplatform.member.service.ILayoutConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;

/**
 * @Author: 黄子奇
 * @Date: 2019/9/26 14:34
 */
@RestController
@RequestMapping("/layout")
@Api(value = "首页布局设置的控制器", tags = "布局设置", description = "首页布局设置的控制器")
public class LayoutConfigController {

    @Autowired
    private ILayoutConfigService iLayoutConfigService;

    @GetMapping("/layout")
    @ApiOperation(value = "获取布局配置信息")
    public ResultObject<LayoutConfigVo> getLayoutConfig(@ApiParam(value = "布局名称") @RequestParam(required = false) String name) {
        LayoutConfigVo vo = (name == null || name.isEmpty() ) ? iLayoutConfigService.queryOne() : iLayoutConfigService.getConfig(name);
        if(vo == null) {
            vo = new LayoutConfigVo(null,null,new ArrayList<>(),new Date(),new Date(),null);
        }
        return ResultObject.success(vo);
    }

    @PutMapping("/layout")
    @ApiOperation(value = "保存布局配置信息")
    public ResultObject<Object> updateLayoutConfig(@ApiParam(value = "布局信息结构", required = true) @RequestBody LayoutConfigVo layoutConfig) {
        if(iLayoutConfigService.updateOne(layoutConfig)) {
            return ResultObject.success("保存成功",null);
        } else {
            return ResultObject.error("保存失败");
        }
    }
}