package com.keao.edu.user.controller;


import com.keao.edu.common.controller.BaseController;
import com.keao.edu.common.entity.HttpResponseResult;
import com.keao.edu.common.page.PageInfo;
import com.keao.edu.common.page.QueryInfo;
import com.keao.edu.common.tenant.TenantContextHolder;
import com.keao.edu.user.entity.TenantInfo;
import com.keao.edu.user.service.TenantInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("tenantInfo")
@Api(tags = "机构服务")
public class TenantInfoController extends BaseController {

	@Autowired
	private TenantInfoService tenantInfoService;
	
	@ApiOperation("机构服务分页查询")
	@GetMapping(value = "/list")
	public HttpResponseResult<PageInfo<TenantInfo>> getList(QueryInfo queryInfo) {
		return succeed(tenantInfoService.queryPage(queryInfo));
	}

	@ApiOperation("查询机构详情")
	@ApiImplicitParam(name = "id", value = "机构ID", required = true, dataType = "Integer", paramType = "path")
	@GetMapping(value = "/query")
	public HttpResponseResult<TenantInfo> query() {
		String tenantId = TenantContextHolder.getTenantId().toString();
		return succeed(tenantInfoService.get(Integer.parseInt(tenantId)));
	}

	@ApiOperation("新增机构")
	@PostMapping(value = "/add")
	public HttpResponseResult add(TenantInfo tenantInfo) {
		tenantInfoService.addTenant(tenantInfo);
		return succeed();
	}

	@ApiOperation("更新机构")
	@PostMapping(value = "/update")
	public HttpResponseResult update(TenantInfo tenantInfo) {
		String tenantId = TenantContextHolder.getTenantId().toString();
		tenantInfo.setUpdateTime(new Date());
		tenantInfo.setId(Integer.parseInt(tenantId));
		tenantInfoService.update(tenantInfo);
		return succeed();
	}

	@ApiOperation("删除")
	@PostMapping(value = "/del")
	public HttpResponseResult add(Integer id) {
		return succeed(tenantInfoService.delete(id));
	}

}
