//package jj.tech.paolu.biz.webadmin.controller;
//
//import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
//import static org.mybatis.dynamic.sql.SqlBuilder.isIn;
//import static org.mybatis.dynamic.sql.SqlBuilder.select;
//
//import java.util.List;
//import java.util.Map;
//import java.util.Optional;
//import java.util.stream.Collectors;
//
//import org.mybatis.dynamic.sql.render.RenderingStrategies;
//import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.validation.annotation.Validated;
//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 io.swagger.v3.oas.annotations.Operation;
//import jj.tech.paolu.biz.webadmin.dao.TreeMapper;
//import jj.tech.paolu.biz.webadmin.vo.Id;
//import jj.tech.paolu.biz.webadmin.vo.Ids;
//import jj.tech.paolu.biz.webadmin.vo.MenuTree;
//import jj.tech.paolu.config.security.user.WebAdminUtil;
//import jj.tech.paolu.repository.mybatis.dao.SysAdminRoleMapper;
//import jj.tech.paolu.repository.mybatis.dao.SysMenuMapper;
//import jj.tech.paolu.repository.mybatis.dao.SysRoleMapper;
//import jj.tech.paolu.repository.mybatis.dao.support.SysAdminRoleDynamicSqlSupport;
//import jj.tech.paolu.repository.mybatis.dao.support.SysMenuDynamicSqlSupport;
//import jj.tech.paolu.repository.mybatis.dao.support.SysRoleMenuDynamicSqlSupport;
//import jj.tech.paolu.repository.mybatis.dao.support.SysRoleUrlDynamicSqlSupport;
//import jj.tech.paolu.repository.mybatis.entity.SysAdmin;
//import jj.tech.paolu.repository.mybatis.entity.SysMenu;
//import jj.tech.paolu.utils.IDHelp;
//import jj.tech.paolu.utils.R;
//
//@RestController
//@RequestMapping(value = "/op/menu", name = "菜单管理")
//public class MenuManagerController {
//	@Autowired SysMenuMapper sysMenuMapper;
//	@Autowired SysRoleMapper sysRoleMapper;
//	@Autowired SysAdminRoleMapper sysAdminRoleMapper;
//
//	@Autowired TreeMapper treeMapper;
//
//	private final IDHelp ID_HELP = IDHelp.getInstance();
//
//	@Operation(summary = "所有一级和二级菜单", description = "")
//	@PostMapping("/all")
//	public Object all() {
//
//		var from = select(SysMenuMapper.selectList)
//				.from(SysMenuDynamicSqlSupport.sysMenu)
////				.where()
////				.and(SysMenuDynamicSqlSupport.types, isEqualTo(2))
////				.and(SysMenuDynamicSqlSupport.level, isEqualTo(2), or(SysMenuDynamicSqlSupport.level, isEqualTo(3)))
////				.and(SysMenuDynamicSqlSupport.name, isNotNull())
//				.orderBy(SysMenuDynamicSqlSupport.id,SysMenuDynamicSqlSupport.sort);
//
//
//		SelectStatementProvider provider = from
//				.build()
//				.render(RenderingStrategies.MYBATIS3);
//
//		List<MenuTree> list = treeMapper.selectManyMenu(provider);
//
//		Map<String, List<MenuTree>> map = list.stream().collect(Collectors.groupingBy(MenuTree::getParent_id));
//		list.stream().forEach(i->{
//			i.setChildren(map.get(i.getId()));
//		});
//
//		return R.SUCCESS(map.get("0"));
//	}
//
//	@Operation(summary = "获取管理员对应的菜单", description = "")
//	@PostMapping("/list")
//	public Object admin() {
//
//		SysAdmin admin = WebAdminUtil.getUser();
//
//		if(admin == null) {
//			return R.FALSE("admin can not find");
//		}
//
//		if(admin.getId().equals("1")) {
//			return this.all();
//		}
//
//		var adminrole = select(SysAdminRoleDynamicSqlSupport.roleid)
//				.from(SysAdminRoleDynamicSqlSupport.sysAdminRole)
//				.where(SysAdminRoleDynamicSqlSupport.adminid, isEqualTo(admin.getId()));
//
//		var roleurl = select(SysMenuDynamicSqlSupport.url)
//				.from(SysRoleUrlDynamicSqlSupport.sysRoleUrl)
//				.where(SysRoleUrlDynamicSqlSupport.roleid, isIn(adminrole));
//
//		var sysurl  = select(SysMenuMapper.selectList)
//				.from(SysMenuDynamicSqlSupport.sysMenu)
//				.where(SysMenuDynamicSqlSupport.url, isIn(roleurl))
//				.orderBy(SysMenuDynamicSqlSupport.url)
//				.build()
//	            .render(RenderingStrategies.MYBATIS3);
//
//		List<MenuTree> list = treeMapper.selectManyMenu(sysurl);
//
//		Map<String, List<MenuTree>> map = list.stream().collect(Collectors.groupingBy(MenuTree::getParent_id));
//		list.stream().forEach(i->{
//			i.setChildren(map.get(i.getId()));
//		});
//
//		return R.SUCCESS(map.get("0"));
//
//	}
//
//
//	@Operation(summary = "获取当前用户对应的菜单", description = "")
//	@PostMapping("/menuLit")
//	public Object menuLit() {
//
//		SysAdmin admin = WebAdminUtil.getUser();
//
//		if(admin == null) {
//			return R.FALSE("admin can not find");
//		}
//
//		if(admin.getId().equals("1")) {
//			return this.all();
//		}
//
//
//		var adminrole = select(SysAdminRoleDynamicSqlSupport.roleid)
//				.from(SysAdminRoleDynamicSqlSupport.sysAdminRole)
//				.where(SysAdminRoleDynamicSqlSupport.adminid, isEqualTo(admin.getId()));
//
//		var rolemenu = select(SysRoleMenuDynamicSqlSupport.menuid)
//				.from(SysRoleMenuDynamicSqlSupport.sysRoleMenu)
//				.where(SysRoleMenuDynamicSqlSupport.roleid, isIn(adminrole));
//
//		var sysmenu  = select(SysMenuMapper.selectList)
//				.from(SysMenuDynamicSqlSupport.sysMenu)
//				.where(SysMenuDynamicSqlSupport.id, isIn(rolemenu))
//				.orderBy(SysMenuDynamicSqlSupport.sort)
//				.build()
//				.render(RenderingStrategies.MYBATIS3);
//
//		List<MenuTree> list = treeMapper.selectManyMenu(sysmenu);
//
//		Map<String, List<MenuTree>> map = list.stream().collect(Collectors.groupingBy(MenuTree::getParent_id));
//		list.stream().forEach(i->{
//			i.setChildren(map.get(i.getId()));
//		});
//
//		return R.SUCCESS(map.get("0"));
//
//	}
//
//	@Operation(summary = "根据菜单id查询", description = "")
//	@PostMapping("/get")
//	public Object get(@Validated @RequestBody Id param){
//		Optional<SysMenu> sysMenu = sysMenuMapper.selectByPrimaryKey(param.getId());
//		return sysMenu.map(R::SUCCESS).orElse(R.FALSE("can no find"));
//	}
//
//
//	@Operation(summary = "新增菜单", description = "")
//	@PostMapping("/save")
//	public Object save(@RequestBody SysMenu param){
//		if (param.getId() == null){
//			String id = ID_HELP.nextId();
//			param.setId(id);
//			sysMenuMapper.insertSelective(param);
//		}else {
//			sysMenuMapper.updateByPrimaryKeySelective(param);
//		}
//		return R.SUCCESS(param);
//	}
//
//	@Operation(summary = "按ID删除", description = "批量删除")
//	@PostMapping("/delete")
//	public Object delete(@Validated @RequestBody Ids param) {
//		List<String> ids = param.getIds();
//		sysMenuMapper.delete(c -> c.where(SysMenuDynamicSqlSupport.id, isIn(ids)));
//		return R.SUCCESS("delete: " + ids);
//	}
//}
