package com.hotent.portal.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotent.base.util.BeanUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.hotent.sys.persistence.manager.SysAppMenuManager;
import com.hotent.sys.persistence.model.SysAppMenu;
import com.hotent.base.controller.BaseController;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.util.StringUtil;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * <pre> 
 * 描述：应用菜单 控制器类
 * 构建组：x7
 * 作者:qiuxd
 * 邮箱:qiuxd@jee-soft.cn
 * 日期:2020-08-21 10:12:59
 * 版权：广州宏天软件股份有限公司
 * </pre>
 */
@RestController
@RequestMapping(value="/portal/sysAppMenu/v1")
@Api(tags="sysAppMenuController")
public class SysAppMenuController extends BaseController<SysAppMenuManager,SysAppMenu>{
	@Resource
	SysAppMenuManager sysAppMenuManager;
	
    /**
	 * 新增应用菜单
	 * @param sysAppMenu
	 * @throws Exception 
	 * @return
	 * @exception 
	 */
	@PostMapping(value="save")
	@ApiOperation(value = "新增,更新应用菜单数据", httpMethod = "POST", notes = "新增,更新应用菜单数据")
	public CommonResult<String> save(@ApiParam(name="sysAppMenu",value="应用菜单业务对象", required = true)@RequestBody SysAppMenu sysAppMenu) throws Exception{
		String msg = "添加应用菜单成功";
		if(StringUtil.isEmpty(sysAppMenu.getId())){
			sysAppMenuManager.create(sysAppMenu);
		}else{
			sysAppMenuManager.update(sysAppMenu);
			 msg = "更新应用菜单成功";
		}
		return new CommonResult<String>(msg);
	}
	
	/**
	 * 批量删除应用菜单记录
	 * @param ids
	 * @throws Exception 
	 * @return
	 * @exception 
	 */
	@DeleteMapping(value="/remove")
	@ApiOperation(value = "批量删除应用菜单记录", httpMethod = "DELETE", notes = "批量删除应用菜单记录")
	public CommonResult<String> removes(@ApiParam(name="ids",value="业务主键数组,多个业务主键之间用逗号分隔", required = true)@RequestParam String...ids) throws Exception{
		sysAppMenuManager.removeByIds(ids);
		return new CommonResult<String>(true, "删除成功");
	}

	@GetMapping(value="/getTree")
	@ApiOperation(value = "获取APP菜单树", httpMethod = "GET", notes = "获取APP菜单树")
	public List<SysAppMenu> getTree(@RequestParam("appId") String appId){
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.eq("app_id_",appId);
		List<SysAppMenu> menus =baseService.list(wrapper);
        menus = menus.stream().sorted(Comparator.comparing(o -> Integer.valueOf(o.getSn()))).collect(Collectors.toList());
		return BeanUtils.listToTree(menus);
	}
}
