package com.hotent.uc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.controller.BaseController;
import com.hotent.base.controller.BaseController2;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.BeanUtils;
import com.hotent.uc.manager.UserUniteManager;
import com.hotent.uc.model.UserUnite;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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

/**
 * 用户与外部通讯录关系表 前端控制器
 *
 * @company 广州宏天软件股份有限公司
 * @author pangquan
 * @since 2020-10-30
 */
@RestController
@RequestMapping("/uc/userUnite/v2/")
@Api(tags="用户与外部通讯录关系2")
@ApiGroup(group= {ApiGroupConsts.GROUP_UC})
public class UserUniteController2 extends BaseController2<UserUniteManager, UserUnite> {
	
	@PostMapping("updateUserUnite")
	@ApiOperation(value = "更新", httpMethod = "POST", notes = "更新")
	public CommonResult<String> updateUserUnite(@RequestBody UserUnite userUnite){
		this.baseService.updateUserUnite(userUnite);
		return CommonResult.<String>ok().message("更新成功！");
	}

	@PostMapping("save")
	@ApiOperation(value = "保存", httpMethod = "POST", notes = "保存")
	public CommonResult<String> save(@RequestBody UserUnite userUnite){
		if(BeanUtils.isNotEmpty(userUnite.getId())){
			this.baseService.update(userUnite);
		}else{
			this.baseService.create(userUnite);
			return CommonResult.<String>ok().message("新增成功！");
		}
		return CommonResult.<String>ok().message("更新成功！");
	}

	/**
	 * 根据企业微信通讯录userid获取数据
	 * @param wxWorkId
	 * @return
	 */
	@GetMapping("getUserUniteByWxWorkId")
	@ApiOperation(value = "根据企业微信通讯录userid获取数据", httpMethod = "POST", notes = "根据企业微信通讯录userid获取数据")
	public CommonResult<UserUnite> getUserUniteByWxWorkId(@RequestParam(value="wxWorkId",required=true) String wxWorkId){
		QueryWrapper<UserUnite> queryWrapper = new QueryWrapper<UserUnite>().eq("WX_WORK_ID_", wxWorkId);
		UserUnite exist = this.baseService.getOne(queryWrapper);
		return CommonResult.<UserUnite>ok().value(exist);
	}

	/**
	 * 根据用户id获取数据
	 * @param userId
	 * @return
	 */
	@GetMapping("/getByUserId/{userId}")
	@ApiOperation(value = "根据用户id获取数据", httpMethod = "GET", notes = "根据用户id获取数据")
	public CommonResult<UserUnite> getByUserId(@PathVariable String userId){
		QueryWrapper<UserUnite> queryWrapper = new QueryWrapper<UserUnite>().eq("USER_ID_", userId);
		UserUnite exist = this.baseService.getOne(queryWrapper);
		return CommonResult.<UserUnite>ok().value(exist);
	}

	/**
	 * 根据userIds获取钉钉ids
	 * @param userId
	 * @return
	 */
	@GetMapping("/getDingtalkIdsByUserIds")
	@ApiOperation(value = "根据userIds获取钉钉ids", httpMethod = "GET", notes = "根据userIds获取钉钉ids")
	public CommonResult<String> getDingtalkIdsByUserIds(@RequestParam(value="userIds",required=true) String userIds){
		if(BeanUtils.isNotEmpty(userIds)){
			Object[] userIdList = userIds.split(",");
			QueryWrapper<UserUnite> queryWrapper = new QueryWrapper<UserUnite>();
			queryWrapper.in("USER_ID_", userIdList);
			List<UserUnite> list = this.baseService.list(queryWrapper);
			if(BeanUtils.isNotEmpty(list)){
				List<String> dingtalkIds = list.stream().filter(e->BeanUtils.isNotEmpty(e.getDingtalkId())).
														map(UserUnite::getDingtalkId).collect(Collectors.toList());
				if(BeanUtils.isNotEmpty(dingtalkIds)){
					String dingDingIds = String.join(",", dingtalkIds);
					return CommonResult.<String>ok().value(dingDingIds);
				}
			}
		}
		return CommonResult.<String>ok().value("").message("暂无数据！");
	}

	/**
	 * 根据userIds获取企业微信ids
	 * @param userId
	 * @return
	 */
	@GetMapping("/getWxWorkIdsByUserIds")
	@ApiOperation(value = "根据userIds获取企业微信ids", httpMethod = "GET", notes = "根据userIds获取企业微信ids")
	public CommonResult<String> getWxWorkIdsByUserIds(@RequestParam(value="userIds",required=true) String userIds){
		if(BeanUtils.isNotEmpty(userIds)){
			Object[] userIdList = userIds.split(",");
			QueryWrapper<UserUnite> queryWrapper = new QueryWrapper<UserUnite>();
			queryWrapper.in("USER_ID_", userIdList);
			List<UserUnite> list = this.baseService.list(queryWrapper);
			if(BeanUtils.isNotEmpty(list)){
				List<String> wxWorkIds = list.stream().filter(e->BeanUtils.isNotEmpty(e.getWxWorkId())).
						map(UserUnite::getWxWorkId).collect(Collectors.toList());
				if(BeanUtils.isNotEmpty(wxWorkIds)){
					String wxIds = String.join(",", wxWorkIds);
					return CommonResult.<String>ok().value(wxIds);

				}
			}
		}
		return CommonResult.<String>ok().value("").message("暂无数据！");

	}
	
	/**
	 * 根据userIds获取公众号openids
	 * @param userId
	 * @return
	 */
	@GetMapping("/getOpenIdsByUserIds")
	@ApiOperation(value = "根据userIds获取公众号openids", httpMethod = "GET", notes = "根据userIds获取公众号openids")
	public CommonResult<String> getOpenIdsByUserIds(@RequestParam(value="userIds",required=true) String userIds){
		if(BeanUtils.isNotEmpty(userIds)){
			Object[] userIdList = userIds.split(",");
			QueryWrapper<UserUnite> queryWrapper = new QueryWrapper<UserUnite>();
			queryWrapper.in("USER_ID_", userIdList);
			List<UserUnite> list = this.baseService.list(queryWrapper);
			if(BeanUtils.isNotEmpty(list)){
				List<String> getOpenId = list.stream().filter(e->BeanUtils.isNotEmpty(e.getOpenId())).
						map(UserUnite::getOpenId).collect(Collectors.toList());
				if(BeanUtils.isNotEmpty(getOpenId)){
					String commonIds = String.join(",", getOpenId);
					return CommonResult.<String>ok().value(commonIds);
				}
			}
		}
		return CommonResult.<String>ok().value("").message("暂无数据！");

	}
}
