package com.hotent.portal.controller;


import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotent.base.controller.BaseController;
import com.hotent.base.jwt.JwtTokenHandler;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.util.JwtUtil;
import com.hotent.base.util.StringUtil;
import com.hotent.portal.model.PortalBizSystemOrgAuth;
import com.hotent.portal.service.PortalBizSystemOrgAuthManager;
import com.hotent.uc.api.impl.model.Org;
import com.hotent.uc.api.impl.model.UserFacade;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *  外部系统授权组织控制器
 *
 * @company 广州宏天软件股份有限公司
 * @author cola
 * @since 2021-12-03
 */
@RestController
@RequestMapping("/portalBizSystemOrgAuth/v1")
public class PortalBizSystemOrgAuthController extends BaseController<PortalBizSystemOrgAuthManager, PortalBizSystemOrgAuth> {

	@Autowired
	PortalBizSystemOrgAuthManager portalBizSystemOrgAuthManager;
	@Resource
	JwtTokenHandler jwtTokenHandler;
//	@Autowired
//	DemensionManager demensionManager;
	/**
	 * @description  根据系统编码获取数据
	 * @author  yangzibin
	 * @date    2024/7/12 16:09
	 * @param	request
	 * @param	sysCode
	 * @return  com.hotent.base.model.CommonResult<java.util.List<com.hotent.portal.model.PortalBizSystemOrgAuth>>
	 */
	@RequestMapping(value = "getList", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
	@ApiOperation(value="根据系统编码获取数据",httpMethod = "GET",notes = "根据系统编码获取数据")
	public PageList<Org> getDetail(HttpServletRequest request,@ApiParam(name = "sysCode", value = "系统编码", required = true) @RequestParam String sysCode,
								   @ApiParam(name = "type", value = "type", required = true) @RequestParam String type,
								   @ApiParam(name = "queryFilter", value = "通用查询对象", required = true) @RequestBody QueryFilter<Org> queryFilter) throws Exception{
		String token = JwtUtil.getToken(request);
		Claims claims = jwtTokenHandler.parseToken(token);
		String tenantId = (String) claims.get("tenantId"); //获取维度
		String demId="";
//		QueryWrapper<Demension> lambdaQueryWrapper=new QueryWrapper<>();
//		lambdaQueryWrapper.eq("TENANT_ID_",tenantId);
//		lambdaQueryWrapper.eq("IS_DEFAULT_","1");
//		Demension demension=demensionManager.getBaseMapper().selectOne(lambdaQueryWrapper);
//		if(demension!=null){
//			demId=demension.getId();
//		}
		return portalBizSystemOrgAuthManager.queryData(sysCode,type, queryFilter);
	}

	/**
	 * @description  根据系统编码获取数据
	 * @author  yangzibin
	 * @date    2024/7/12 16:09
	 * @param	request
	 * @param	sysCode
	 * @return  com.hotent.base.model.CommonResult<java.util.List<com.hotent.portal.model.PortalBizSystemOrgAuth>>
	 */
	@RequestMapping(value = "getUserList", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
	@ApiOperation(value="根据系统编码获取数据",httpMethod = "POST",notes = "根据系统编码获取数据")
	public PageList<UserFacade> getUserList(HttpServletRequest request,@ApiParam(name = "sysCode", value = "系统编码", required = true) @RequestParam String sysCode,
								   @ApiParam(name = "type", value = "type", required = true) @RequestParam String type,
								   @ApiParam(name = "queryFilter", value = "通用查询对象", required = true) @RequestBody QueryFilter<UserFacade> queryFilter) throws Exception{
		String token = JwtUtil.getToken(request);
		Claims claims = jwtTokenHandler.parseToken(token);
		String tenantId = (String) claims.get("tenantId"); //获取维度
		String demId="";
//		QueryWrapper<Demension> lambdaQueryWrapper=new QueryWrapper<>();
//		lambdaQueryWrapper.eq("TENANT_ID_",tenantId);
//		lambdaQueryWrapper.eq("IS_DEFAULT_","1");
//		Demension demension=demensionManager.getBaseMapper().selectOne(lambdaQueryWrapper);
//		if(demension!=null){
//			demId=demension.getId();
//		}
		return portalBizSystemOrgAuthManager.queryUserData(sysCode,type, queryFilter);
	}
	/**
	 * 新增,更新
	 * @param portalBizSystemAuth
	 * @throws Exception
	 * @return
	 * @exception
	 */
	@RequestMapping(value = "/saveEntity",method = RequestMethod.POST,produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "新增,更新数据", httpMethod = "POST", notes = "新增,更新数据")
	public CommonResult<String> saveEntity(HttpServletRequest request, @RequestBody(required = true)JSONObject jsonObject) throws Exception{
		String token = JwtUtil.getToken(request);
		Claims claims = jwtTokenHandler.parseToken(token);
		String tenantId = (String) claims.get("tenantId"); //获取维度
		String demId="";
//		QueryWrapper<Demension> lambdaQueryWrapper=new QueryWrapper<>();
//		lambdaQueryWrapper.eq("TENANT_ID_",tenantId);
//		lambdaQueryWrapper.eq("IS_DEFAULT_","1");
//		Demension demension=demensionManager.getBaseMapper().selectOne(lambdaQueryWrapper);
//		if(demension!=null){
//			demId=demension.getId();
//		}

		portalBizSystemOrgAuthManager.saveEntity(jsonObject.get("sysCode").toString(),jsonObject.get("orgIdList").toString(),jsonObject.get("type").toString(),tenantId,demId);
		return CommonResult.<String>ok().message("添加成功");
	}


	/**
	 * @description  删除数据
	 * @author  yangzibin
	 * @date    2024/7/12 17:45
	 * @param	request
	 * @param	jsonObject
	 * @return  com.hotent.base.model.CommonResult<java.lang.String>
	 */
	@RequestMapping(value = "/deleteEntity",method = RequestMethod.POST,produces = {
			"application/json; charset=utf-8" })
	@ApiOperation(value = "删除数据", httpMethod = "POST", notes = "删除数据")
	public CommonResult<String> deleteEntity(HttpServletRequest request, @RequestBody(required = true)JSONObject jsonObject) throws Exception{
		portalBizSystemOrgAuthManager.deleteEntity(jsonObject.get("sysCode").toString(),jsonObject.get("orgIdList").toString(),jsonObject.get("type").toString());
		return CommonResult.<String>ok().message("删除成功");
	}

	/**
	 * @description 获取第三方系统授权的用户和组织id集合
	 * @author  yangzibin
	 * @date    2024/7/13 14:54
	 * @param	sysCode
	 * @param	type
	 * @param	tenantId
	 * @param	demId
	 * @return  java.util.List<java.lang.String>
	 */
	@GetMapping(value="/getOrgIdList")
	@ApiOperation(value = "获取第三方系统所授权的组织、部门或者用户id集合", httpMethod = "GET", notes = "获取第三方系统所授权的组织、部门或者用户id集合")
	public List<String> getOrgIdList(@ApiParam(name="sysCode",value="系统编码")@RequestParam(required=true)  String sysCode,
									 @ApiParam(name="type",value="类型（0=组织或者部门1=用户）")@RequestParam(required=true)  String type,
									 @ApiParam(name="tenantId",value="单位id")@RequestParam(required=false)  String tenantId,
									 @ApiParam(name="demId",value="维度id")@RequestParam(required=false)  String demId) throws Exception{
		return portalBizSystemOrgAuthManager.getListByCodeAsId(sysCode,type,tenantId,demId);
	}
}
