package com.hotent.uc.controller;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.controller.BaseController;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.StringUtil;
import com.hotent.uc.manager.DingApiManager;
import com.hotent.uc.manager.OrgExternalSettingManager;
import com.hotent.uc.model.OrgExternalDept;
import com.hotent.uc.model.OrgExternalSetting;
import com.hotent.uc.params.common.OrgExportObject;
import io.swagger.annotations.Api;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 调用钉钉组织机构接口 接口控制器
 *
 * @company 广州宏天软件股份有限公司
 * @author pangquan
 * @since 2020-10-30
 */
@RestController
@RequestMapping("/uc/orgDing/v1/")
@Api(tags="组织外部系统参数配置")
@ApiGroup(group= {ApiGroupConsts.GROUP_UC})
public class OrgExternalDingController extends BaseController<DingApiManager, OrgExternalDept> {
	@Autowired
	private DingApiManager dingApiManager;

	/**
	 * 根据组织id获取组织
	 *
	 * @param orgId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "dingApi/getAllDept", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "根据组织id获取组织", httpMethod = "GET", notes = "根据组织id获取组织")
	public CommonResult<String> getALlDept(@ApiParam(name = "orgId", value = "组织id", required = true) @RequestParam String orgId,
										   @ApiParam(name = "deptId", value = "父部门id", required = true) @RequestParam String deptId)
			throws Exception {

		return dingApiManager.synDingDept(orgId,deptId);
	}

	/**
	 * 钉钉部门向中台同步
	 *
	 * @param orgId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "dingApi/synDingToSystem", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "钉钉部门向中台同步", httpMethod = "GET", notes = "钉钉部门向中台同步")
	public CommonResult<String> synDingToSystem(@ApiParam(name = "orgId", value = "组织id", required = true) @RequestParam String orgId,
												@ApiParam(name = "externalDeptId", value = "外部系统部门id", required = false) @RequestParam String externalDeptId)
			throws Exception {

		return dingApiManager.releDingToSystemDept(orgId,externalDeptId);
	}


	/**
	 * 中台部门向钉钉同步
	 *
	 * @param orgId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "dingApi/synSystemToDing", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "中台部门向钉钉同步", httpMethod = "GET", notes = "中台部门向钉钉同步")
	public CommonResult<String> synSystemToDing(@ApiParam(name = "orgId", value = "组织id", required = true) @RequestParam String orgId,
												@ApiParam(name = "deptId", value = "外部系统部门id", required = false) @RequestParam String deptId)
			throws Exception {
		return dingApiManager.releSystemToDingdept(orgId,deptId);
	}
	/**
	 * 获取钉钉所有用户，通过遍历部门id获取
	 *
	 * @param orgId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "dingApi/getAllUser", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取钉钉所有用户", httpMethod = "GET", notes = "获取钉钉所有用户")
	public CommonResult<String> getAllUser(@ApiParam(name = "orgId", value = "组织id", required = true) @RequestParam String orgId)
			throws Exception {

		return dingApiManager.getDingALlUser(orgId);
	}

	/**
	 * 获取钉钉所有用户，通过遍历部门id获取
	 *
	 * @param orgId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "dingApi/startDingStream", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取钉钉所有用户", httpMethod = "GET", notes = "获取钉钉所有用户")
	public CommonResult<String> startDingStream(@ApiParam(name = "orgId", value = "组织id", required = true) @RequestParam String orgId,
											   @ApiParam(name = "open", value = "开启或者关闭", required = true ) @RequestParam String open)
			throws Exception {

		return dingApiManager.startDingStreamData(orgId,open);
	}

	/**
	 * 获取当前组织部门，按照parentid查询
	 *
	 * @param orgId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "dingApi/queryExtDeptList", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取当前组织部门，按照parentid查询", httpMethod = "GET", notes = "获取当前组织部门，按照parentid查询")
	public CommonResult<List<OrgExternalDept>> queryExtDeptList(@ApiParam(name = "orgId", value = "组织id", required = true) @RequestParam String orgId,
																@ApiParam(name = "parentId", value = "父id", required = true ) @RequestParam String parentId)
			throws Exception {

		return dingApiManager.queryExtDeptList(orgId,parentId);
	}

	/**
	 * 钉钉推送事件订阅
	 *
	 * @param orgId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "dingApi/getDingPushData", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "钉钉推送事件订阅", httpMethod = "POST", notes = "钉钉推送事件订阅")
	public Map<String, Object> getDingPushData(@RequestParam(value = "msg_signature", required = false) String msg_signature,
											   @RequestParam(value = "timestamp", required = false) String timeStamp,
											   @RequestParam(value = "nonce", required = false) String nonce,
											   @RequestBody(required = false) JSONObject json,
											   @RequestParam(value = "corpId", required = false) String corpId)
			throws Exception {
             System.out.println(corpId);
		return dingApiManager.getDingPushData(msg_signature,timeStamp,nonce,json,corpId);
	}

	/**
	 * 获取钉钉tokenNew
	 * @param orgId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "dingApi/getDingToken", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取钉钉gettoken", httpMethod = "GET", notes = "获取钉钉gettoken")
	public Map<String, Object> getDingTokenByOrgId(@RequestParam(value = "orgId", required = true) String orgId,@RequestParam(value = "tokenType", required = true) String tokenType)
			throws Exception {
		Map<String, Object> map=new HashMap<>();
		if(BeanUtils.isNotEmpty(dingApiManager.getDingTokenNew(orgId,tokenType))){
			map.put("state",true);
			map.put("token",dingApiManager.getDingTokenNew(orgId,tokenType));
			return map;
		}
		map.put("state",false);
		map.put("token","");
		return map;
	}

	/**
	 * @description 获取钉钉用户unionId
	 * @author  yangzibin
	 * @date    2024/3/19 15:04
	 * @param	orgId
	 * @param	dingUserId
	 * @return  java.util.Map<java.lang.String,java.lang.Object>
	 */
	@RequestMapping(value = "dingApi/getDingUserUnionId", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "获取钉钉用户unionId", httpMethod = "GET", notes = "获取钉钉用户unionId")
	public Map<String, Object> getDingUserUnionId(@RequestParam(value = "orgId", required = true) String orgId,@RequestParam(value = "dingUserId", required = true) String dingUserId)
			throws Exception {
		Map<String, Object> map=new HashMap<>();
		if(BeanUtils.isNotEmpty(dingApiManager.getDingUserUnionId(orgId,dingUserId))){
			map.put("state",true);
			map.put("unionId",dingApiManager.getDingUserUnionId(orgId,dingUserId));
			return map;
		}
		map.put("state",false);
		map.put("unionId","");
		return map;
	}


	/**
	 * @description  更新钉钉待办状态
	 * @author  yangzibin
	 * @date    2024/3/20 15:56
	 * @param	taskId
	 * @return  java.util.Map<java.lang.String,java.lang.Object>
	 */
	@RequestMapping(value = "dingApi/updateDingToDoTaskState", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "更新钉钉待办状态", httpMethod = "GET", notes = "更新钉钉待办状态")
	public Map<String, Object> updateDingToDoTaskState(@RequestParam(value = "taskId", required = true) String taskId)
			throws Exception {
		Map<String, Object> map=new HashMap<>();
		boolean flag=dingApiManager.updateDingToTask(taskId);
		if(flag){
			map.put("state",true);
			map.put("message","更新钉钉待办任务成功");
		}
		map.put("state",false);
		map.put("message","更新钉钉待办任务失败");
		return map;
	}

	/**
	 * @description  发送钉钉待办
	 * @author  yangzibin
	 * @date    2024/3/20 15:56
	 * @param	taskId
	 * @return  java.util.Map<java.lang.String,java.lang.Object>
	 */
	@RequestMapping(value = "dingApi/sendDingTodoTask", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "发动钉钉待办", httpMethod = "GET", notes = "发动钉钉待办")
	public JSONObject sendDingTodoTask(@RequestParam(value = "userId", required = true) String userId,
												@RequestParam(value = "todoId", required = true) String todoId,
												@RequestParam(value = "title", required = true) String title,
									   @RequestParam(value = "isPushType", required = true) String isPushType)
			throws Exception {
		return dingApiManager.sendDingTodoTask(userId,todoId,title,isPushType);
	}


	/**
	 * @description  发送钉钉工作通知
	 * @author  yangzibin
	 * @date    2024/3/20 15:56
	 * @param	taskId
	 * @return  java.util.Map<java.lang.String,java.lang.Object>
	 */
	@RequestMapping(value = "dingApi/sendDingMessage", method = RequestMethod.POST, produces = { "application/json; charset=utf-8" })
	@ApiOperation(value = "发送钉钉工作通知", httpMethod = "GET", notes = "发送钉钉工作通知")
	public JSONObject sendDingMessage(@ApiParam(name="exportObject",value="发送钉钉工作通知",required=true) @RequestBody JSONObject exportObject)
			throws Exception {
		String userId= (String) exportObject.get("userId");
		if(StringUtil.isEmpty(userId)){
			cn.hutool.json.JSONObject jsonObject= new cn.hutool.json.JSONObject("{\"errcode\":500,\"errmsg\":\"请传入用户id\"}");
			return jsonObject;
		}
		String todoId= (String) exportObject.get("todoId");
		if(StringUtil.isEmpty(todoId)){
			cn.hutool.json.JSONObject jsonObject= new cn.hutool.json.JSONObject("{\"errcode\":500,\"errmsg\":\"请传入待办id\"}");
			return jsonObject;
		}
		String title= (String) exportObject.get("title");
		String isPushType= (String) exportObject.get("isPushType");
		JSONArray jsonObject= (JSONArray) exportObject.get("formList");
		return dingApiManager.sendDingMessage(userId,todoId,title,jsonObject,isPushType);
	}


}
