package com.hotent.uc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotent.base.enums.ResponseErrorEnums;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.FieldRelation;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.query.QueryOP;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.StringUtil;
import com.hotent.uc.manager.UcGroupUserManager;
import com.hotent.uc.manager.UserManager;
import com.hotent.uc.model.UcGroupUser;
import com.hotent.uc.model.User;
import com.hotent.uc.vo.UcGroupUserVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hotent.base.controller.BaseController;

import java.util.*;


/**
 * 群组和用户关系表 前端控制器
 *
 * @company 广州宏天软件股份有限公司
 * @author 超级管理员
 * @since 2021-01-15
 */
@RestController
@RequestMapping("/uc/ucGroupUser/v1/")
public class UcGroupUserController extends BaseController<UcGroupUserManager, UcGroupUser> {

	@Autowired
	UserManager userService;

	@PostMapping("/saveJson")
	@ApiOperation("添加实体的接口")
	public CommonResult<String> saveJson(@ApiParam(name="model", value="实体信息") @RequestBody Map<String,Object> map) {
		baseService.saveJson(map);
		String msg="添加群组成功";
		return new CommonResult<>(msg);
	}

	@RequestMapping(value="getGroupUsers",method= RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取角色用户（分页）", httpMethod = "POST", notes = "获取角色用户（分页）",hidden=false)
	public PageList<User> getGroupUsers(@ApiParam(name="filter",value="查询参数", required = true) @RequestBody QueryFilter filter,
										@ApiParam(name="groupId",value="群组Id", required = true) @RequestParam Optional<String> groupId) throws Exception{
		if (StringUtil.isNotEmpty(groupId.orElse("")) && groupId.isPresent()) {
			filter.addFilter("ur.GROUP_ID_",groupId.get(), QueryOP.EQUAL, FieldRelation.AND, "group_code");
		}else{
			//todo 用当前登录用户做过滤
		}
		Page<User> list = (Page<User>)userService.getGroupUserQuery(filter);
		return new PageList<User>(list);
	}

	@DeleteMapping("/remove")
	@ApiOperation("根据id集合批量删除")
	public CommonResult<String> remove(@ApiParam(name="ids", value="实体集合") @RequestParam String...ids) {
		boolean result = baseService.removeByIds(Arrays.asList(ids));
		if(!result) {
			return new CommonResult<>(ResponseErrorEnums.FAIL_OPTION, "删除实体失败");
		}
		return new CommonResult<>(true, "删除成功");
	}

	@PostMapping(value = "/swap", produces = {"application/json; charset=utf-8"})
	@ApiOperation("切换排序")
	public CommonResult<String> swap(@ApiParam(name = "ids", value = "要交换排序的主键值") @RequestBody List<String> ids) {
		if (BeanUtils.isEmpty(ids) || ids.size() != 2) {
			throw new IllegalArgumentException("请传入两个需要交换的记录");
		}
		baseService.swap(ids.get(0), ids.get(1));
		return new CommonResult<>(true, "交换成功");
	}

	@PostMapping(value = "loadUserByGroupId", produces = {"application/json; charset=utf-8"})
	@ApiOperation(value = "获取用户及其组织岗位信息", httpMethod = "POST", notes = "获取用户及其组织信息")
	public CommonResult<PageList<Map<String, Object>>> loadUserByGroupId(@ApiParam(name = "groupIds", value = "群组ID") @RequestParam("groupIds") Optional<String> groupIds,
																				@ApiParam(name = "demId", value = "维度ID") @RequestParam("demId") Optional<String> demId,
																				@ApiParam(name = "queryFilter", value = "通用查询器") @RequestBody QueryFilter<User> queryFilter)throws Exception {
		return new CommonResult<>(true, "获取成功", baseService.loadUserByGroupId(groupIds.orElse(""), demId.orElse(""), queryFilter));
	}




}
