package cn.com.ty.lift.ud.appmenu.controller;

import cn.com.ty.lift.ud.appmenu.mapper.entity.Appmenu;
import cn.com.ty.lift.ud.appmenu.mapper.entity.AppmenuTree;
import cn.com.ty.lift.ud.appmenu.service.IAppmenuService;
import cn.com.ty.lift.ud.common.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.service.IService;

import cn.com.ty.lift.ud.appmenu.controller.query.AppmenuQuery;
import cn.com.xwy.boot.web.dto.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * app权限API
 * 
 * @author Xson
 * @date 2020-06-04
 */
@Api(tags = "app权限API")
@RestController
@RequestMapping("/api/appmenus")
public class AppmenuController extends BaseController<Appmenu, AppmenuQuery> {

    private final Logger log = LoggerFactory.getLogger(AppmenuController.class);

    private @Autowired IAppmenuService appmenuService;

    @Override
    protected IService<Appmenu> getService() {
        return appmenuService;
    }

    // 新建
    @PostMapping("/insert")
    @ApiOperation(value = "新增")
    @Override
    public RestResponse<Appmenu> create(@RequestBody Appmenu entity) {
        if (StringUtils.isBlank(entity.getName())) {
            return RestResponse.fail("名称不能为空");
        }
        if (entity.getParentId() != 1 && StringUtils.isBlank(entity.getRouter())) {
            return RestResponse.fail("路由不能为空");
        }
        if(entity.getParentId() != 1 && StringUtils.isNotBlank(entity.getRouter())){
            Appmenu findByRouter = appmenuService.findByRouter(entity.getRouter());
            if (findByRouter != null) {
                return RestResponse.fail("不能添加相同权限");
            }
        }

        boolean saveFlag = getService().save(entity);

        if (saveFlag)
            return RestResponse.success(null, "Created successfully");
        else
            return RestResponse.fail("Created unsuccessfully");
    }

    @GetMapping("/queryAll")
    @ApiOperation(value = "查询所有")
    public RestResponse<Appmenu> queryAll() {
        return appmenuService.queryAll();
    }

    @GetMapping("/queryAllTree")
    @ApiOperation(value = "查询所有菜单-树形结构")
    public RestResponse queryAllTree() {
        return appmenuService.queryAllTree();
    }

    @GetMapping("/queryAllParent")
    @ApiOperation(value = "查询所有父级菜单")
    public RestResponse queryAllParent() {
        return appmenuService.queryAllParent();
    }
}