package com.hotent.uc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotent.base.controller.BaseController;
import com.hotent.base.controller.BaseController2;
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.uc.manager.UcGroupUserManager;
import com.hotent.uc.manager.UserManager;
import com.hotent.uc.model.UcGroupUser;
import com.hotent.uc.model.User;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 群组和用户关系表 前端控制器
 *
 * @company 广州宏天软件股份有限公司
 * @author 超级管理员
 * @since 2021-01-15
 */
@RestController
@RequestMapping("/uc/ucGroupUser/v2/")
public class UcGroupUserController2 extends BaseController2<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 CommonResult.<String>ok().message(msg);
	}

	@RequestMapping(value="getGroupUsers",method= RequestMethod.POST, produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "获取角色用户（分页）", httpMethod = "POST", notes = "获取角色用户（分页）",hidden=false)
	public CommonResult<PageList<User>> getGroupUsers(@ApiParam(name="filter",value="查询参数", required = true) @RequestBody QueryFilter filter, @ApiParam(name="groupId",value="群组Id", required = true) @RequestParam String groupId) throws Exception{
		filter.addFilter("ur.GROUP_ID_",groupId, QueryOP.EQUAL, FieldRelation.AND, "group_code");
		Page<User> list = (Page<User>)userService.getGroupUserQuery(filter);
		PageList<User> userPageList = new PageList<>(list);
		return CommonResult.<PageList<User>>ok().value(userPageList);
	}

	@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 CommonResult.<String>result(ResponseErrorEnums.FAIL_OPTION).message("删除实体失败");
		}
		return CommonResult.<String>ok();
	}




}
