package ink.wgink.module.menu.controller.app;

import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.menu.pojo.vos.MenuVO;
import ink.wgink.module.menu.service.IMenuService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.ZTreeDTO;
import ink.wgink.pojo.dtos.menu.MenuDTO;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: MenuController
 * @Description: 菜单
 * @Author: wenc
 * @Date: 2018/12/27 10:09 PM
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_APP_PREFIX + "菜单")
@RestController
@RequestMapping(ISystemConstant.APP_PREFIX + "/menu")
public class MenuAppController extends DefaultBaseController {

    public static final String SLASH = "/";
    @Autowired
    private IMenuService menuService;

    @ApiOperation(value = "保存菜单", notes = "保存菜单接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("save")
    public SuccessResult saveMenu(@RequestHeader("token") String token, @RequestBody MenuVO menuVO) {
        checkParams(menuVO);
        menuService.save(token, menuVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "删除菜单", notes = "通过id列表批量删除菜单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "菜单ID列表，用下划线分隔", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("remove/{ids}")
    public SuccessResult removeMenu(@RequestHeader("token") String token, @PathVariable("ids") String ids) {
        menuService.remove(Arrays.asList(ids.split("\\_")));
        return new SuccessResult();
    }

    @ApiOperation(value = "修改菜单", notes = "修改菜单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("update/{menuId}")
    public SuccessResult updateMenu(@RequestHeader("token") String token, @PathVariable("menuId") String menuId, @RequestBody MenuVO menuVO) {
        checkParams(menuVO);
        menuService.update(menuId, menuVO);
        return new SuccessResult();
    }

    /**
     * 参数校验
     *
     * @param menuVO
     */
    private void checkParams(MenuVO menuVO) {
        if (!StringUtils.equals(menuVO.getMenuUrl(), "javascript:void(0);")) {
            menuVO.setMenuUrl(addSlash(menuVO.getMenuUrl()));
        }
    }

    /**
     * 添加斜杠
     *
     * @param prefix
     * @return
     */
    private String addSlash(String prefix) {
        if (!StringUtils.isEmpty(prefix)) {
            if (prefix.toLowerCase().startsWith("http://") || prefix.toLowerCase().startsWith("https://")) {
                return prefix;
            }
            if (!StringUtils.startsWith(prefix, SLASH)) {
                prefix = SLASH + prefix;
            }
            if (StringUtils.endsWith(prefix, SLASH)) {
                prefix = prefix.substring(0, prefix.length() - 1);
            }
        }
        return prefix;
    }

    @ApiOperation(value = "菜单详情", notes = "菜单详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get/{menuId}")
    public MenuDTO get(@RequestHeader("token") String token, @PathVariable("menuId") String menuId) {
        Map<String, Object> params = getParams();
        params.put("menuId", menuId);
        return menuService.get(params);
    }

    @ApiOperation(value = "菜单列表", notes = "菜单列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuParentId", value = "菜单父ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-all/{menuParentId}")
    public List<MenuDTO> listAllByParentId(@RequestHeader("token") String token, @PathVariable("menuParentId") String menuParentId) {
        return menuService.listAllByParentId(menuParentId);
    }

    @ApiOperation(value = "菜单列表", notes = "菜单列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuParentId", value = "菜单父ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list/{menuParentId}")
    public List<MenuDTO> list(@RequestHeader("token") String token, @PathVariable("menuParentId") String menuParentId) {
        return menuService.listByParentId(menuParentId);
    }

    @ApiOperation(value = "菜单zTree列表", notes = "菜单zTree列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "父ID", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listztree")
    public List<ZTreeDTO> listZTree() {
        Map<String, Object> params = requestParams();
        String menuParentId = "0";
        if (!StringUtils.isBlank(params.get(ISystemConstant.PARAMS_ID) == null ? null : params.get(ISystemConstant.PARAMS_ID).toString())) {
            menuParentId = params.get("id").toString();
        }
        params.put("menuParentId", menuParentId);
        return menuService.listZTree(params);
    }

    @ApiOperation(value = "菜单分页列表", notes = "菜单分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "上级ID", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "rows", value = "显示数量", paramType = "query", dataType = "int", defaultValue = "20"),
            @ApiImplicitParam(name = "keywords", value = "关键字", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage")
    public SuccessResultList<List<MenuDTO>> listPage(@RequestHeader("token") String token, ListPage page) {
        Map<String, Object> params = requestParams();
        String menuParentId = "0";
        if (!StringUtils.isBlank(params.get("parentId") == null ? null : params.get("parentId").toString())) {
            menuParentId = params.get("parentId").toString();
        }
        params.put("menuParentId", menuParentId);
        page.setParams(params);
        return menuService.listPage(page);
    }

}
