//package jj.tech.paolu.biz.webadmin.controller;
//
//import static org.mybatis.dynamic.sql.SqlBuilder.equalTo;
//import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
//import static org.mybatis.dynamic.sql.SqlBuilder.isEqualToWhenPresent;
//import static org.mybatis.dynamic.sql.SqlBuilder.isLikeWhenPresent;
//import static org.mybatis.dynamic.sql.SqlBuilder.isNotEqualTo;
//import static org.mybatis.dynamic.sql.SqlBuilder.select;
//
//import java.util.List;
//import java.util.Optional;
//
//import org.apache.commons.lang3.StringUtils;
//import org.mybatis.dynamic.sql.SqlBuilder;
//import org.mybatis.dynamic.sql.render.RenderingStrategies;
//import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
//import org.mybatis.dynamic.sql.where.WhereApplier;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.transaction.annotation.Transactional;
//import org.springframework.validation.annotation.Validated;
//import org.springframework.web.bind.annotation.PathVariable;
//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.service.OrgService;
//import jj.tech.paolu.biz.webadmin.vo.AddAdminRoleMenuVo;
//import jj.tech.paolu.biz.webadmin.vo.Id;
//import jj.tech.paolu.biz.webadmin.vo.SysAdminRoleVo;
//import jj.tech.paolu.biz.webadmin.vo.SysRoleListVo;
//import jj.tech.paolu.config.security.user.WebAdminUtil;
//import jj.tech.paolu.repository.mybatis.dao.SysAdminMapper;
//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.SysRoleMenuMapper;
//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.SysRoleDynamicSqlSupport;
//import jj.tech.paolu.repository.mybatis.dao.support.SysRoleMenuDynamicSqlSupport;
//import jj.tech.paolu.repository.mybatis.entity.SysAdmin;
//import jj.tech.paolu.repository.mybatis.entity.SysAdminRole;
//import jj.tech.paolu.repository.mybatis.entity.SysMenu;
//import jj.tech.paolu.repository.mybatis.entity.SysOrg;
//import jj.tech.paolu.repository.mybatis.entity.SysRole;
//import jj.tech.paolu.repository.mybatis.entity.SysRoleMenu;
//import jj.tech.paolu.utils.IDHelp;
//import jj.tech.paolu.utils.Page;
//import jj.tech.paolu.utils.R;
//
//@RestController
//@RequestMapping(value = "/op/role")
//public class RoleManagerController {
//
//    @Autowired SysAdminRoleMapper sysAdminRoleMapper;
//    @Autowired SysAdminMapper sysAdminMapper;
//    @Autowired SysRoleMapper sysRoleMapper;
//    @Autowired SysRoleMenuMapper sysRoleMenuMapper;
//    @Autowired SysMenuMapper sysMenuMapper;
//    @Autowired OrgService orgService;
//
//
//    @Operation(summary = "用户所属机构下的角色列表", description = "")
//    @PostMapping("/list")
//    public Object list(@RequestBody SysRoleListVo parame) {
//    	
//        Page p = new Page(parame.pageNum, parame.pageSize);
//
//        if (StringUtils.isNotBlank(parame.rolename)) {
//            parame.rolename = "%" + parame.rolename + "%";
//        }
//        if (StringUtils.isNotBlank(parame.describe)) {
//            parame.describe = "%" + parame.describe + "%";
//        }
//        
//        SysAdmin admin = WebAdminUtil.getUser();
//    	List<SysOrg> orgs = orgService.allFarter(admin.getOrg_id());
//    	SysOrg endOrg = orgs.getLast();
//    	
//    	
//
//        var from = select(SysRoleMapper.selectList)
//                .from(SysRoleDynamicSqlSupport.sysRole);
//
//        var where = SqlBuilder.where(SysRoleDynamicSqlSupport.id, isNotEqualTo("1"));
//        if(!admin.getId().equals("1")) {
//        	where.and(SysRoleDynamicSqlSupport.company_id, isEqualTo(endOrg.getId()));
//        }
//        
//        where.and(SysRoleDynamicSqlSupport.rolename, isLikeWhenPresent(parame.rolename));
//        where.and(SysRoleDynamicSqlSupport.describe, isLikeWhenPresent(parame.describe));
//        where.and(SysRoleDynamicSqlSupport.is_open, isEqualToWhenPresent(parame.is_open));
//
//        WhereApplier applier = where.toWhereApplier();
//        SelectStatementProvider provider = from
//                .applyWhere(applier)
//                .configureStatement(config -> config.setNonRenderingWhereClauseAllowed(true))
//                .limit(p.getPageSize())
//                .offset(p.limitStart())
//                .build()
//                .render(RenderingStrategies.MYBATIS3);
//
//        var list = sysRoleMapper.selectMany(provider);
//        long total = sysRoleMapper.count(c -> c
//                .applyWhere(applier)
//                .configureStatement(config -> config.setNonRenderingWhereClauseAllowed(true))
//        );
//        p.setList(list);
//        p.setTotal(total);
//        return R.SUCCESS(p);
//    }
//
//    @Operation(summary = "获取管理员所在公司对应所有角色", description = "获取管理员所在公司对应所有角色")
//    @PostMapping("/all")
//    public Object all() {
//    	SysAdmin admin = WebAdminUtil.getUser();
//      	List<SysOrg> orgs = orgService.allFarter(admin.getOrg_id());
//      	SysOrg endOrg = orgs.getLast();
//    	
//        var from = select(SysRoleMapper.selectList)
//                .from(SysRoleDynamicSqlSupport.sysRole)
//                ;
//        
//        var where = SqlBuilder.where(SysRoleDynamicSqlSupport.is_open, isEqualTo(1));
//        
//        if(!admin.getId().equals("1")) {
//        	where.and(SysRoleDynamicSqlSupport.company_id, isEqualTo(endOrg.getId()));
//        }
//        
//        WhereApplier applier = where.toWhereApplier();
//        SelectStatementProvider provider = from
//                .applyWhere(applier)
//                .configureStatement(config -> config.setNonRenderingWhereClauseAllowed(true))
//                .build()
//                .render(RenderingStrategies.MYBATIS3);
//
//        var list = sysRoleMapper.selectMany(provider);
//
//        return R.SUCCESS(list);
//    }
//
//
//    @Operation(summary = "保存管理员对应的角色", description = "只需传角色roleId:[{'roleId': 0}] ")
//    @PostMapping("/addAdminRole")
//    @Transactional
//    public Object addAdminRole(@RequestBody SysAdminRoleVo params) {
//    	String adminid = params.getAdminid();
//        if (adminid.equals("1")) {
//            return R.FALSE("超级管理员不能修改角色");
//        }
//
//        Optional<SysAdmin> op = sysAdminMapper.selectByPrimaryKey(adminid);
//        if (!op.isPresent()) {
//            return R.FALSE("admin can not find");
//        }
//
//
//        sysAdminRoleMapper.delete(d -> d.where(SysAdminRoleDynamicSqlSupport.adminid, isEqualTo(adminid)));
//        List<SysAdminRole> roles = params.getSysAdminRoles();
//        roles.forEach(bean -> {
//            bean.setAdminid(adminid);
//            if (!bean.getRoleid().equals("1")) {
//                sysAdminRoleMapper.insertSelective(bean);
//            }
//        });
//
//        return R.SUCCESS(roles.size());
//    }
//
//
////    @Operation(summary = "获取管理员所在公司对应所有角色", description = "")
////    @PostMapping("/getAdminRole")
////    public Object getAdminRole(@Validated @RequestBody Id param) {
////    	String id = param.id;
////        Optional<SysAdmin> op = sysAdminMapper.selectByPrimaryKey(id);
////        if (!op.isPresent()) {
////            return R.FALSE("admin can not find");
////        }
////
////        var roleids = select(SysAdminRoleDynamicSqlSupport.roleid)
////                .from(SysAdminRoleDynamicSqlSupport.sysAdminRole)
////                .where(SysAdminRoleDynamicSqlSupport.adminid, isEqualTo(id));
////
////        var from = select(SysRoleMapper.selectList)
////                .from(SysRoleDynamicSqlSupport.sysRole)
////                .where(SysRoleDynamicSqlSupport.id, SqlBuilder.isIn(roleids))
////                .build()
////                .render(RenderingStrategies.MYBATIS3);
////
////        List<SysRole> roles = sysRoleMapper.selectMany(from);
////
////        HashMap<String, Object> h = new HashMap<String, Object>();
////        SysAdmin bean = op.get();
////        bean.setPassword(null);
////
////        h.put("admin", bean);
////        h.put("roles", roles);
////        return R.SUCCESS(h);
////    }
//
//
////    @Operation(summary = "添加角色对应的菜单", description = "")
////	@PostMapping("/addAdminRoleMenu")
////	public Object addAdminRoleMenu(@Validated @RequestBody Ids param) {
////		Long id = param.id;
////		Optional<SysAdmin> op = sysAdminMapper.selectByPrimaryKey(id);
////		if(!op.isPresent()) {
////			return R.FALSE("admin can not find");
////		}
////
////		var roleids = select(SysAdminRoleDynamicSqlSupport.roleid)
////				.from(SysAdminRoleDynamicSqlSupport.sysAdminRole)
////				.where(SysAdminRoleDynamicSqlSupport.adminid, isEqualTo(id));
////
////		var from  = select(SysRoleMapper.selectList)
////				.from(SysRoleDynamicSqlSupport.sysRole)
////				.where(SysRoleDynamicSqlSupport.id, SqlBuilder.isIn(roleids))
////				.build()
////	            .render(RenderingStrategies.MYBATIS3);
////
////		List<SysRole> roles = sysRoleMapper.selectMany(from);
////
////		HashMap<String,Object> h = new HashMap<String,Object>();
////		SysAdmin bean = op.get();
////		bean.setPassword(null);
////
////		h.put("admin", bean);
////		h.put("roles", roles);
////		return R.SUCCESS(h);
////	}
//
//
//    @Operation(summary = "添加角色对应的菜单", description = "")
//    @PostMapping("/addRoleMenu")
//    @Transactional
//    public Object addAdminRoleMenu(@Validated @RequestBody AddAdminRoleMenuVo param) {
//        //删除旧关系
//        int delete = sysRoleMenuMapper.delete(c -> c.applyWhere(whereFinisher -> whereFinisher
//                .and(SysRoleMenuDynamicSqlSupport.roleid, isEqualTo(param.getRole_id()))
//        ));
//
//        //保存新关系
//        param.getMenu_ids().forEach(id -> {
//            SysRoleMenu srm = new SysRoleMenu();
//            srm.setId(IDHelp.getInstance().nextId());
//            srm.setRoleid(param.getRole_id());
//            srm.setMenuid(id);
//            sysRoleMenuMapper.insert(srm);
//        });
//
//        return R.SUCCESS();
//    }
//
//
//    @PostMapping("delete")
//    public Object delete(@Validated @RequestBody List<String> ids) {
//    	for(Integer i=0; i<ids.size(); i++) {
//    		int fi=i;
//    		List<SysAdminRole> list = 
//    				sysAdminRoleMapper.select(s->s.where(SysAdminRoleDynamicSqlSupport.roleid, isEqualTo(ids.get(fi))));
//	        if(list.size()>0) {
//	        	return R.FALSE(ids.get(fi)+"角色下还有绑定用户,请先解除已绑定该角色用户后再进行删除");
//	        	
//	        }else {
//	        	sysRoleMapper.deleteByPrimaryKey(ids.get(i));
//	        }
//    	}
//    	
//        
//        return R.SUCCESS();
//    }
//
//
//    @PostMapping("save")
//    public Object save(@RequestBody SysRole record) {
//    	SysAdmin admin = WebAdminUtil.getUser();
//    	List<SysOrg> orgs = orgService.allFarter(admin.getOrg_id());
//    	SysOrg endOrg = orgs.getLast();
//    	
//        record.setId(IDHelp.getInstance().nextId());
//        if(endOrg==null) {
//        	record.setCompany_id("0");
//            record.setCompany_name("");
//        }else {
//        	record.setCompany_id(endOrg.getId());
//            record.setCompany_name(endOrg.getName());
//        }
//        return R.SUCCESS(sysRoleMapper.insertSelective(record));
//    }
//
//    @PostMapping("update")
//    public Object update(@RequestBody SysRole record) {
//        return R.SUCCESS(sysRoleMapper.updateByPrimaryKeySelective(record));
//    }
//
//    @PostMapping("findById/{id}")
//    public Object findById(@PathVariable("id") String id) {
//        return R.SUCCESS(sysRoleMapper.selectByPrimaryKey(id).orElse(null));
//    }
//
//    @Operation(summary = "根据角色id查询对应的菜单权限", description = "")
//    @PostMapping("/findMenuById")
//    public Object findMenuById(@Validated @RequestBody Id param) {
//        SelectStatementProvider render = select(SysMenuMapper.selectList)
//                .from(SysMenuDynamicSqlSupport.sysMenu)
//                .leftJoin(SysRoleMenuDynamicSqlSupport.sysRoleMenu)
//                .on(SysMenuDynamicSqlSupport.id, equalTo(SysRoleMenuDynamicSqlSupport.menuid))
//                .leftJoin(SysRoleDynamicSqlSupport.sysRole)
//                .on(SysRoleMenuDynamicSqlSupport.roleid, equalTo(SysRoleDynamicSqlSupport.id))
//                .where(SysRoleDynamicSqlSupport.id, isEqualTo(param.getId()))
//                .build().render(RenderingStrategies.MYBATIS3);
//        List<SysMenu> sysMenus = sysMenuMapper.selectMany(render);
//        return R.SUCCESS(sysMenus);
//
//    }
//
//}
