/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.user.platform.controller;

import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.user.comment.dto.UserLevelDto;
import com.yami.shop.user.comment.model.UserLevel;
import com.yami.shop.user.comment.service.UserLevelService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 用户等级信息管理实现
 *
 * @author LGH
 * @version 1.0.0
 * @since 2020-02-26
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user/userLevel" )
public class UserLevelController {

    private final UserLevelService userLevelService;


    /**
     * 用户等级列表
     */
    @GetMapping("/list" )
    public ResponseEntity<List<UserLevelDto>> listUserLevel(Integer userLevelType) {
        return ResponseEntity.ok(userLevelService.listUserLevelsByUserLevelType(userLevelType));
    }


    /**
     * 通过id查询会员等级
     * @param id id
     * @return 单个数据
     */
    @GetMapping("/info/{id}" )
    public ResponseEntity<UserLevel> getById(@PathVariable("id") Long id) {
        UserLevel userLevel = userLevelService.getUserLevelById(id);
        return ResponseEntity.ok(userLevel);
    }

    /**
     * 新增会员等级
     * @param userLevel 会员等级
     * @return 是否新增成功
     */
    @SysLog("新增会员等级" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('user:userLevel:save')" )
    public ResponseEntity<Boolean> save(@RequestBody @Valid UserLevel userLevel) {
        return ResponseEntity.ok(userLevelService.save(userLevel));
    }

    /**
     * 修改会员等级
     * @param userLevel 会员等级
     * @return 是否修改成功
     */
    @SysLog("修改会员等级" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('user:userLevel:update')" )
    public ResponseEntity<Void> updateById(@RequestBody @Valid UserLevel userLevel) {
        userLevelService.updateUserLevelList(userLevel);
        return ResponseEntity.ok().build();
    }

    /**
     * 通过id删除会员等级
     * @param userLevel 会员等级
     * @return 是否删除成功
     */
    @SysLog("删除会员等级" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('user:userLevel:delete')" )
    public ResponseEntity<Boolean> removeById(@RequestBody @Valid UserLevel userLevel) {
        return ResponseEntity.ok(userLevelService.deleteUserLevel(userLevel));
    }

    /**
     * 更新用户会员等级
     * @return 是否修改成功
     */
    @PutMapping("/updateUserLevel")
    public ResponseEntity<Boolean> updateUserLevel(@RequestBody @Valid UserLevel userLevel) {
        return ResponseEntity.ok(userLevelService.updateUserLevel(userLevel));
    }

}
