package com.yonge.cooleshow.auth.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yonge.cooleshow.auth.api.dto.MenuQueryInfo;
import com.yonge.cooleshow.auth.api.entity.SysMenu;
import com.yonge.cooleshow.auth.service.SysMenuService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.security.AuthUser;
import com.yonge.cooleshow.common.security.SecurityUtils;

@RestController()
@RequestMapping("menu")
@Api(tags = "菜单服务")
public class MenuController extends BaseController {

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation("新增菜单")
    @PutMapping(value = "/add",consumes = MediaType.APPLICATION_JSON_VALUE)
    @PreAuthorize("@pcs.hasPermissions('menu/add')")
    public Object getMenu(@RequestBody SysMenu sysMenu) {
//        SysMenu menuByPermission = sysMenuService.findMenuByPermission(sysMenu.getPermission());
//        if(menuByPermission != null){
//            return failed("权限标识不能重复");
//        }
        if (sysMenu.getParentId() == null) {
            sysMenu.setParentId(0L);
        }
        return succeed(sysMenuService.insert(sysMenu));
    }

    @ApiOperation("根据菜单id删除菜单")
    @DeleteMapping("/del/{id}")
    @PreAuthorize("@pcs.hasPermissions('menu/del')")
    public Object delMenu(@ApiParam(value = "菜单编号", required = true) @PathVariable("id") Long id) {
        sysMenuService.recursiveDel(id);
        return succeed();
    }

    @ApiOperation("根据菜单id修改菜单")
    @PutMapping(value = "/update",consumes = MediaType.APPLICATION_JSON_VALUE)
    @PreAuthorize("@pcs.hasPermissions('menu/update')")
    public Object updateMenu(@RequestBody SysMenu sysMenu) {
//        SysMenu menuByPermission = sysMenuService.findMenuByPermission(sysMenu.getPermission());
//        if(menuByPermission != null && !menuByPermission.getId().equals(sysMenu.getId())){
//            return failed("权限标识不能重复");
//        }
        Date date = new Date();
        sysMenu.setUpdateTime(date);
        return succeed(sysMenuService.update(sysMenu));
    }

    @ApiOperation("根据菜单id查询菜单")
    @GetMapping(value = "/{id}",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @PreAuthorize("@pcs.hasPermissions('menu')")
    public Object getMenu(@ApiParam(value = "菜单编号", required = true) @PathVariable("id") Long id) {
        return succeed(sysMenuService.get(id));
    }

    @ApiOperation("查询用户可访问菜单树状结构")
    @GetMapping(value = "/findByUser")
    public Object findByUser(@RequestBody(required = false) MenuQueryInfo menuQueryInfo) {
        AuthUser user = SecurityUtils.getUser();
        if(user != null){
            if(menuQueryInfo == null){
                menuQueryInfo = new MenuQueryInfo();
            }
            menuQueryInfo.setUserId(user.getUserId());
            return succeed(sysMenuService.findByUser(menuQueryInfo));
        }
        return failed();
    }

}
