package com.liangjian.dataplatform.member.controller;

import com.liangjian.dataplatform.member.entity.po.secondary.UserLevelDesc;
import com.liangjian.dataplatform.member.entity.po.secondary.UserLevelUnit;
import com.liangjian.dataplatform.member.entity.response.ResultObject;
import com.liangjian.dataplatform.member.entity.vo.request.LevelListAndDesc;
import com.liangjian.dataplatform.member.service.IUserLevelService;
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.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 黄子奇
 * @Date: 2019/11/15
 */
@RestController
@RequestMapping("/userLevelSetting")
@Api(value = "会员等级设置控制器", tags = "会员等级设置控制器", description = "会员等级设置控制器")
public class UserLevelSettingController {
    @Autowired
    private IUserLevelService iUserLevelService;

    @GetMapping("/desc")
    @ApiOperation(value = "获取等级说明")
    public ResultObject<UserLevelDesc> getDesc() {
        UserLevelDesc desc = iUserLevelService.getLevelDesc();
        return ResultObject.success(desc);
    }

    @PostMapping("/desc")
    @ApiOperation(value = "保存等级说明")
    public ResultObject<Object> saveDesc(@ApiParam(value = "等级说明对象", required = true) @RequestBody UserLevelDesc desc) {
        if(iUserLevelService.setLevelDesc(desc)) {
            return ResultObject.success("保存成功",null);
        } else {
            return ResultObject.error("保存失败");
        }
    }

    @GetMapping("/levels")
    @ApiOperation(value = "获取所有等级")
    public ResultObject<List<UserLevelUnit>> getAllLevel() {
        List<UserLevelUnit> levels = iUserLevelService.getAllLevelUnit();
        return ResultObject.success(levels);
    }

    @PostMapping("/level")
    @ApiOperation(value = "添加会员等级")
    public ResultObject<Object> addLevel(@ApiParam(value = "会员等级", required = true) @RequestBody UserLevelUnit level) {
        if(iUserLevelService.addUserLevelUnit(level)) {
            return ResultObject.success("保存成功",null);
        } else {
            return ResultObject.error("保存失败");
        }
    }

    @PutMapping("/level")
    @ApiOperation(value = "修改会员等级")
    public ResultObject<Object> saveLevel(@ApiParam(value = "会员等级", required = true) @RequestBody UserLevelUnit level) {
        if(iUserLevelService.saveUserLevelUnit(level)) {
            return ResultObject.success("保存成功",null);
        } else {
            return ResultObject.error("保存失败");
        }
    }

    @DeleteMapping("/level")
    @ApiOperation(value = "删除会员等级")
    public ResultObject<Object> deleteLevel(@ApiParam(value = "会员等级编号", required = true) @RequestBody String id) {
        if(iUserLevelService.deleteUserLevelUnit(id)) {
            return ResultObject.success("删除成功",null);
        } else {
            return ResultObject.error("删除失败");
        }
    }

    @PostMapping("/saveAll")
    @ApiOperation("保存所有")
    public ResultObject<Object> saveAll(@ApiParam(value = "等级列表与等级说明", required = true) @RequestBody LevelListAndDesc levelListAndDesc) {
        if(iUserLevelService.saveAll(levelListAndDesc)) {
            return ResultObject.success("保存成功",null);
        } else {
            return ResultObject.error("保存失败");
        }
    }
}
