/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.system.custommenu.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.node.INode;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.service.IMenuService;
import org.springblade.modules.system.vo.MenuVO;
import org.springblade.modules.system.wrapper.MenuWrapper;
import org.springblade.system.custommenu.entity.CustomMenu;
import org.springblade.system.custommenu.service.ICustomMenuService;
import org.springblade.system.custommenu.vo.CustomMenuVO;
import org.springblade.system.tree.TreeParser;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

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

/**
 *  控制器
 *
 * @author BladeX
 * @since 2020-11-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("system/custommenu")
@Api(value = "", tags = "接口")
public class CustomMenuController extends BladeController {

	private ICustomMenuService customMenuService;

	private IMenuService menuService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入customMenu")
	public R<CustomMenu> detail(CustomMenu customMenu) {
		CustomMenu detail = customMenuService.getOne(Condition.getQueryWrapper(customMenu));
		return R.data(detail);
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入customMenu")
	public R<IPage<CustomMenu>> list(CustomMenu customMenu, Query query) {
		IPage<CustomMenu> pages = customMenuService.page(Condition.getPage(query), Condition.getQueryWrapper(customMenu));

		customMenuService.list(Condition.getQueryWrapper(customMenu));

		return R.data(pages);
	}

	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入customMenu")
	public R page(CustomMenuVO customMenu, Query query) {
//		IPage<CustomMenuVO> pages = customMenuService.selectCustomMenuPage(Condition.getPage(query), customMenu);

		List<CustomMenuVO> list = customMenuService.getAllList();

		List<Map<String, Object>> treeList = TreeParser.getTreeList(0L, list);

		return R.data(treeList);
	}
	/**
	 * 获取自定义菜单
	 */
	@GetMapping("/getCustomRoutes")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入customMenu")
	public R getCustomRoutes(BladeUser user, Long topMenuId) {
		List<MenuVO> list = this.menuService.routes(user == null ? null : user.getRoleId(), topMenuId);
		customMenuService.recoverWithCustomMenu(list, user);
		return R.data(list);
	}

	/**
	 * 自定义菜单懒加载
	 */
	@GetMapping("/lazy-list")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入customMenu")
	public R lazyList(@RequestParam(required = true) Long parentId, @ApiIgnore @RequestParam Map<String, Object> menu, BladeUser bladeUser) {
		List<Map<String, Object>> resultList = new ArrayList<>();
		List<MenuVO> list = MenuWrapper.build().listNodeLazyVO(menuService.lazyList(parentId, menu));
		String tenantId = bladeUser.getTenantId();
		CustomMenuVO customMenu = new CustomMenuVO();
		customMenu.setTenantId(tenantId);
		list.forEach(item -> {
			Map<String, Object> map = BeanUtil.beanToMap(item);
			if (item.getCategory() == 1){
				//菜单类型过滤
				customMenu.setMenuId(item.getId());
				CustomMenu one = customMenuService.getOne(Condition.getQueryWrapper(customMenu));
				if (one != null){
					map.put("customMenuId", one.getId());
					map.put("customMenuName", one.getMenuName());
				}
				resultList.add(map);
			}
		});


		return R.data(resultList);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "customMenu")
	public R save(@RequestBody CustomMenu customMenu) {
		return R.status(customMenuService.save(customMenu));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入customMenu")
	public R update(@Valid @RequestBody CustomMenu customMenu) {
		return R.status(customMenuService.updateById(customMenu));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入customMenu")
	public R submit(@Valid @RequestBody Map<String, Object> map, BladeUser user) {
		CustomMenu customMenu = new CustomMenu();
		CustomMenu menu = new CustomMenu();
		if (map.get("customMenuId") != null){
			customMenu.setId(Long.parseLong(map.get("customMenuId").toString()));
		}
		if (map.get("id") != null){
			customMenu.setMenuId(Long.parseLong(map.get("id").toString()));
			menu.setMenuId(Long.parseLong(map.get("id").toString()));
		}
		if (map.get("customMenuName") != null){
			customMenu.setMenuName(map.get("customMenuName").toString());
		}

		menu.setTenantId(user.getTenantId());
		CustomMenu one = customMenuService.getOne(Condition.getQueryWrapper(menu));
		if (one != null){
			customMenu.setId(one.getId());
		}

		return R.status(customMenuService.saveOrUpdate(customMenu));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(customMenuService.deleteLogic(Func.toLongList(ids)));
	}


}
