package com.hotent.uc.controller;


import com.hotent.base.cache.annotation.CachePut;
import com.hotent.base.cache.annotation.FirstCache;
import com.hotent.base.constants.CacheKeyConst;
import com.hotent.base.exception.MessagePassingException;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.query.QueryOP;
import com.hotent.base.util.AppUtil;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.StringUtil;
import com.hotent.uc.manager.UcGroupManager;
import com.hotent.uc.model.UcGroup;
import com.hotent.uc.util.ContextUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import com.hotent.base.controller.BaseController;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;


/**
 * 群组管理 前端控制器
 *
 * @company 广州宏天软件股份有限公司
 * @author 超级管理员
 * @email test
 * @since 2021-01-15
 */
@RestController
@RequestMapping("/uc/ucGroup/v1/")
public class UcGroupController extends BaseController<UcGroupManager, UcGroup> {


	@PostMapping("/save")
	@ApiOperation("添加实体的接口")
	public CommonResult<String> create(@ApiParam(name="model", value="实体信息") @RequestBody UcGroup ucGroup) {
		String msg="添加群组成功";
		if(StringUtil.isNotEmpty(ucGroup.getId())){
			baseService.update(ucGroup);
			msg="更新群组成功";
		}else{
			ucGroup.setCreatorName(ContextUtil.getCurrentUser().getFullname());
			UcGroup existsData = baseService.getByAlias(ucGroup.getCode());
			if (BeanUtils.isNotEmpty(existsData)) {
				throw new MessagePassingException(String.format("别名【%s】已存在", ucGroup.getCode()));
			}
			baseService.save(ucGroup);
		}
		return new CommonResult<>(msg);
	}

	@PostMapping(value="/query", produces={"application/json; charset=utf-8" })
	@ApiOperation("分页查询结果")
	public PageList<UcGroup> query(@ApiParam(name="queryFilter", value="分页查询信息") @RequestBody QueryFilter<UcGroup> queryFilter) {
		queryFilter.addFilter("create_by_",ContextUtil.getCurrentUserId(), QueryOP.EQUAL);
		return baseService.query(queryFilter);
	}

	@DeleteMapping("/remove")
	@ApiOperation("根据id集合批量删除")
	public CommonResult<String> remove(@ApiParam(name="ids", value="实体集合") @RequestParam String...ids) {
		baseService.deleteByIds(Arrays.asList(ids));
		return new CommonResult<>("删除群组成功");
	}

	// TODO 切换岗位调整为调整组织
	@RequestMapping(value = "setCookie",method = RequestMethod.GET)
	@ApiOperation(value = "用户切换岗位后，保存当前用户切换岗位的信息",httpMethod = "GET")
	public CommonResult<String> setCurrentUserGroup(@RequestParam(value = "postId")String postId){
		String userId = ContextUtil.getCurrentUser().getId();
		UcGroupController bean = AppUtil.getBean(UcGroupController.class);
		bean.putCache(userId, postId);
		return CommonResult.ok();
	}

	@CachePut(value = CacheKeyConst.EIP_UC_CURRENT_USER_GROUP_ID,key = "#userId",firstCache = @FirstCache(expireTime = 100,timeUnit = TimeUnit.DAYS))
	public String putCache(String userId,String postId){
		return postId;
	}

}
