package com.hotent.portal.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hotent.base.feign.UCFeignService;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.*;
import com.hotent.base.util.JsonUtil;
import com.hotent.base.util.StringUtil;
import com.hotent.portal.model.PortalTemplateUser;
import com.hotent.portal.service.PortalTemplateUserManager;
import com.hotent.uc.api.impl.model.Org;
import com.hotent.uc.api.impl.model.UserFacade;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.hotent.base.controller.BaseController;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 门户模板用户设置 前端控制器
 * @author: 梁宏锦
 * @date: 2024/05/17
 **/
@RestController
@RequestMapping("/portaltemplate/user/v1/")
public class PortalTemplateUserController extends BaseController<PortalTemplateUserManager, PortalTemplateUser> {
	private final Logger logger= LoggerFactory.getLogger(this.getClass());
	@Resource
	UCFeignService ucFeignService;
	@Autowired
	PortalTemplateUserManager portalTemplateUserManager;

	/**
	 * 获取 门户模板用户设置
	 */
	@GetMapping(value="/getDetail")
	@ApiOperation(value="获取 门户模板用户设置",httpMethod = "GET",notes = "获取 门户模板用户设置")
	public CommonResult<PortalTemplateUser> getDetail(@ApiParam(name="userId",value="用户id", required = true)@RequestParam(required=true) String userId,
													  @ApiParam(name="orgId",value="组织id", required = true)@RequestParam(required=true) String orgId) throws Exception{
		LambdaQueryWrapper<PortalTemplateUser> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(PortalTemplateUser::getUserId, userId);
		queryWrapper.eq(PortalTemplateUser::getOrgId, orgId);
		List<PortalTemplateUser> list = baseService.getBaseMapper().selectList(queryWrapper);
		if(list==null || list.size()<=0){
			return new CommonResult<PortalTemplateUser>(true, "查询成功", null);
		} else {
			PortalTemplateUser templateUser = list.get(0);
			return new CommonResult<PortalTemplateUser>(true, "查询成功", templateUser);
		}
	}
    /**
	 * 保存 门户模板用户设置
	 */
	@PostMapping(value="/save")
	@ApiOperation(value = "保存 门户模板用户设置", httpMethod = "POST", notes = "保存 门户模板用户设置")
	public CommonResult<String> save(@ApiParam(name="userId",value="用户id", required = true)@RequestParam(required=true) String userId,
									 @ApiParam(name="orgId",value="组织id", required = true)@RequestParam(required=true) String orgId,
									 @ApiParam(name="templateName",value="模板名称", required = true)@RequestParam(required=true) String templateName) throws Exception{
		LambdaQueryWrapper<PortalTemplateUser> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(PortalTemplateUser::getUserId, userId);
		queryWrapper.eq(PortalTemplateUser::getOrgId, orgId);
		List<PortalTemplateUser> list = baseService.getBaseMapper().selectList(queryWrapper);
		if(list!=null && list.size()>0){
			portalTemplateUserManager.removeByUserIdAndOrgId(userId, orgId);
		}

		CommonResult<JsonNode> res = ucFeignService.getUserById(userId);
        if(res==null || !res.getState()) {
            return new CommonResult<>(false, "未匹配到该用户信息");
        }
        UserFacade user = JsonUtil.toBean(res.getValue().toString(), UserFacade.class);

		ObjectNode orgNode = ucFeignService.getOrgById(orgId);
		if(orgNode==null) {
			return new CommonResult<>(false, "未匹配到该组织信息");
		}
		Org org = JsonUtil.toBean(orgNode.toString(), Org.class);

		PortalTemplateUser templateUser = new PortalTemplateUser();
		templateUser.setUserId(userId);
		templateUser.setUserName(user.getUsername());
		templateUser.setOrgId(orgId);
		templateUser.setOrgName(org.getName());
		templateUser.setTemplateName(templateName);
		templateUser.setCreateTime(LocalDateTime.now());
		baseService.save(templateUser);
		return CommonResult.<String>ok().message("保存成功");
	}

	/**
	 * 删除 门户模板用户设置
	 */
	@GetMapping(value="/delTemplateUser")
	@ApiOperation(value = "删除 门户模板用户设置", httpMethod = "GET", notes = "删除 门户模板用户设置")
	public CommonResult<String> delTemplateUser(@ApiParam(name="userId",value="用户id", required = true)@RequestParam(required=true) String userId,
									 @ApiParam(name="orgId",value="组织id", required = true)@RequestParam(required=true) String orgId) throws Exception{
		portalTemplateUserManager.removeByUserIdAndOrgId(userId, orgId);
		return CommonResult.<String>ok().message("删除成功");
	}

}
