package com.hotent.uc.controller;

import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.context.BaseContext;
import com.hotent.base.controller.BaseController;
import com.hotent.base.controller.BaseController2;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import com.hotent.base.util.BeanUtils;
import com.hotent.base.util.CommonResultUtil;
import com.hotent.base.util.StringUtil;
import com.hotent.uc.manager.TenantAuthManager;
import com.hotent.uc.manager.TenantManageManager;
import com.hotent.uc.manager.TenantTypeManager;
import com.hotent.uc.model.TenantAuth;
import com.hotent.uc.model.TenantManage;
import com.hotent.uc.model.TenantType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 
 * <pre> 
 * 描述：租户类型管理 控制器类
 * 构建组：x7
 * 作者:zhangxw
 * 邮箱:zhangxw@jee-soft.cn
 * 日期:2020-04-17 10:52:37
 * 版权：广州宏天软件股份有限公司
 * </pre>
 */
@RestController
@RequestMapping(value="/uc/tenantType/v2")
@Api(tags="租户类型管理2")
@ApiGroup(group= {ApiGroupConsts.GROUP_UC})
public class TenantTypeController2 extends BaseController2<TenantTypeManager, TenantType> {
	@Resource
	TenantTypeManager tenantTypeManager;
	@Resource
	TenantManageManager tenantManageManager;
	@Resource
	TenantAuthManager tenantAuthManager;
	@Resource
	BaseContext baseContext;

	
	/**
	 * 租户类型管理明细页面
	 * @param id
	 * @return
	 * @throws Exception 
	 * ModelAndView
	 */
	@GetMapping(value="/getAll")
	@ApiOperation(value="根据状态获取所有租户类型",httpMethod = "GET",notes = "根据状态获取所有租户类型")
	public CommonResult<List<TenantType>> getAll(@ApiParam(name="status:enable（启用）、disabled（禁用）",value="状态", required = false)@RequestParam Optional<String> status) throws Exception{
		List<String> authIds = null;
//		if(BeanUtils.isNotEmpty(SecurityContextHolder.getContext().getAuthentication())){
//			User user = ContextUtil.getCurrentUser();
//			if(!user.isAdmin()){
//				authIds = getAuthId();
//			}
//		}
		List<TenantType> tenantTypeList = tenantTypeManager.getByStatus(status.orElse(null), authIds);
		return CommonResult.<List<TenantType>>ok().value(tenantTypeList);
	}
	
    /**
	 * 新增租户类型管理
	 * @param tenantType
	 * @throws Exception 
	 * @return
	 * @exception 
	 */
	@PostMapping(value="save")
	@ApiOperation(value = "新增,更新租户类型管理数据", httpMethod = "POST", notes = "新增,更新租户类型管理数据")
	public CommonResult<String> save(@ApiParam(name="tenantType",value="租户类型管理业务对象", required = true)@RequestBody TenantType tenantType) throws Exception{
		CommonResult<String> commonResult = tenantTypeManager.saveTenantType(tenantType);
		return CommonResultUtil.<String>getResult(commonResult);
	}
	
	@PostMapping(value="setDefault")
	@ApiOperation(value = "根据租户类型编码设置默认租户类型", httpMethod = "POST", notes = "根据租户类型编码设置默认租户类型")
	public CommonResult<String> setDefault(@ApiParam(name="code",value="租户类型编码", required = true)@RequestParam String code) throws Exception{
		CommonResult<String> commonResult = tenantTypeManager.setDefault(code);
		return CommonResultUtil.<String>getResult(commonResult);
	}
	
	/**
	 * 批量删除租户类型管理记录
	 * @param ids
	 * @throws Exception 
	 * @return
	 * @exception 
	 */
	@DeleteMapping(value="/remove")
	@ApiOperation(value = "批量删除租户类型管理记录", httpMethod = "DELETE", notes = "批量删除租户类型管理记录")
	public CommonResult<String> removes(@ApiParam(name="ids",value="业务主键数组,多个业务主键之间用逗号分隔", required = true)@RequestParam String...ids) throws Exception{
		CommonResult<String> commonResult = tenantTypeManager.removes(ids);
		return CommonResultUtil.<String>getResult(commonResult);
	}
	
	@RequestMapping(value="checkCode", method=RequestMethod.GET, produces={"application/json; charset=utf-8" })
	@ApiOperation(value = "检查租户类型编码是否存在", httpMethod = "GET", notes = "检查租户类型编码是否存在")
	public CommonResult<Boolean> checkCode(@ApiParam(name="code",value="租户类型编码") @RequestParam String code) throws Exception{
		TenantType type = tenantTypeManager.getByCode(code);
		boolean flag = BeanUtils.isNotEmpty(type);
		return CommonResult.<Boolean>ok().value(flag);
	}
	/**
	 * 获取当前用户的权限id
	 * @return
	 */
	private List<String> getAuthId(){
		String currentUserId = baseContext.getCurrentUserId();
		List<String> ids = new ArrayList<String>();
		List<TenantAuth> auths = tenantAuthManager.getByUserId(null, null, currentUserId);
		if(BeanUtils.isNotEmpty(auths)){
			for (TenantAuth tenantAuth : auths) {
				if(StringUtil.isNotEmpty(tenantAuth.getTenantId())){
					ids.add(tenantAuth.getTypeId());
				}
			}
		}
		if(BeanUtils.isEmpty(ids)){
			ids.add("-1");
		}
		return ids;
	}
}
