package com.liangjian.dataplatform.member.controller;

import com.liangjian.dataplatform.member.entity.po.primary.FeatureInfo;
import com.liangjian.dataplatform.member.entity.po.primary.FeatureLog;
import com.liangjian.dataplatform.member.entity.response.ResultObject;
import com.liangjian.dataplatform.member.service.IFeatureInfoService;
import com.liangjian.dataplatform.member.service.IFeatureLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 黄子奇
 * @Date: 2019/9/24 9:14
 */
@RestController
@RequestMapping("/feature")
@Api(value = "功能管理的控制器", tags = "功能管理", description = "功能管理的控制器")
public class FeatureInfoController {
    @Autowired
    private IFeatureInfoService iFeatureInfoService;
    @Autowired
    private IFeatureLogService iFeatureLogService;

    @GetMapping("/features")
    @ApiOperation(value = "分页获取功能")
    public Page<FeatureInfo> getFeatures(@ApiParam(value = "功能名称") @RequestParam(required = false) String name,
                                         @ApiParam(value = "开始页数", required = true) @RequestParam Integer page,
                                         @ApiParam(value = "每页数量", required = true) @RequestParam Integer size) {
        if(name == null) {
            name = "";
        }
        return iFeatureInfoService.queryByNameLike(name,page-1,size);
    }

    @GetMapping("/feature")
    @ApiOperation(value = "根据id获取功能")
    public ResultObject<FeatureInfo> getFeature(@ApiParam(value = "功能编号", required = true) @RequestParam String id) {
        FeatureInfo featureInfo = iFeatureInfoService.queryById(id);
        return ResultObject.success(featureInfo);
    }

    @PostMapping("/feature")
    @ApiOperation(value = "新增功能")
    public ResultObject<Object> addFeature(@ApiParam(value = "功能对象", required = true) @RequestBody FeatureInfo featureInfo) {
        if(featureInfo.getId() != null) {
            return ResultObject.error("提交的对象不合规");
        }

        if(iFeatureInfoService.addFeaturesInfo(featureInfo)) {
            return ResultObject.success("保存成功",null);
        } else {
            return ResultObject.error("保存失败");
        }
    }

    @PutMapping("/feature")
    @ApiOperation(value = "更新功能")
    public ResultObject<Object> saveFeature(@ApiParam(value = "功能对象", required = true) @RequestBody FeatureInfo featureInfo) {
        if(featureInfo.getId() == null) {
            return ResultObject.error("提交的对象不合规");
        }

        if(iFeatureInfoService.saveFeaturesInfo(featureInfo)) {
            return ResultObject.success("保存成功",null);
        } else {
            return ResultObject.error("保存失败");
        }
    }

    @DeleteMapping("/feature")
    @ApiOperation(value = "删除功能")
    public ResultObject<Object> deleteFeature(@ApiParam(value = "功能编号", required = true) @RequestBody String id) {
        if(iFeatureInfoService.deleteFeaturesInfo(id)) {
            return ResultObject.success("删除成功",null);
        } else {
            return ResultObject.error("删除失败");
        }
    }

    @GetMapping("/log")
    @ApiOperation(value = "分页获取功能更新信息")
    public Page<FeatureLog> getFeatureLog(@ApiParam(value = "功能编号") @RequestParam(required = false) String infoId,
                                                        @ApiParam(value = "开始页数", required = true) @RequestParam Integer page,
                                                        @ApiParam(value = "每页数量", required = true) @RequestParam Integer size) {
        return iFeatureLogService.queryByInfoId(infoId, page-1, size);
    }

    @PostMapping("/log")
    @ApiOperation(value = "添加功能更新信息")
    public ResultObject<Object> addUpdateLog(@ApiParam(value = "更新信息对象", required = true) @RequestBody FeatureLog featureLog) {
        if(featureLog.getInfoId() == null || featureLog.getInfoId().isEmpty()) {
            return ResultObject.error("提交的对象不合规");
        }

        if(iFeatureLogService.addFeatureLog(featureLog)) {
            return ResultObject.success("保存成功",null);
        } else {
            return ResultObject.error("保存失败");
        }
    }
}
