package com.keao.edu.user.controller;


import com.keao.edu.auth.api.client.SysUserFeignService;
import com.keao.edu.auth.api.entity.SysUser;
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.tenant.TenantContextHolder;
import com.keao.edu.user.entity.Organization;
import com.keao.edu.user.page.OrganizationQueryInfo;
import com.keao.edu.user.service.OrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 合作单位服务
 */
@RestController
@Api(tags = "合作单位服务")
@RequestMapping(value = "organization")
public class OrganizationController extends BaseController {

	@Autowired
	private OrganizationService organizationService;
	@Autowired
	private SysUserFeignService sysUserFeignService;

	@ApiOperation(value = "合作单位列表")
	@GetMapping(value = "list")
    @PreAuthorize("@pcs.hasPermissions('organization/list')")
	public HttpResponseResult<PageInfo<Organization>> configList(OrganizationQueryInfo queryInfo) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		queryInfo.setParentId(sysUser.getId());
		if(queryInfo.getOrganId() != null){
			queryInfo.setOrganIds(organizationService.getChildOrganIds(queryInfo.getOrganId(),false));
		}
		return succeed(organizationService.queryPage(queryInfo));
	}

	@ApiOperation(value = "修改合作单位")
	@PostMapping(value = "update")
    @PreAuthorize("@pcs.hasPermissions('organization/update')")
	public HttpResponseResult updateOrganization(@RequestBody Organization organization) {
		organizationService.updateOrganization(organization);
		return succeed();
	}

	@ApiOperation(value = "新增合作单位")
	@PostMapping(value = "add")
    @PreAuthorize("@pcs.hasPermissions('organization/add')")
	public HttpResponseResult addConfig(@RequestBody Organization organization) {
		organization.setTenantId(TenantContextHolder.getTenantId());
		organizationService.add(organization);
		return succeed();
	}

	@ApiOperation(value = "查询合作单位")
	@GetMapping(value = "get")
    @PreAuthorize("@pcs.hasPermissions('organization/get')")
	public HttpResponseResult<Organization> getConfig(Integer id) {
		return succeed(organizationService.getOrganization(id));
	}

	@ApiOperation(value = "删除合作单位")
	@PostMapping(value = "del")
    @PreAuthorize("@pcs.hasPermissions('organization/del')")
	public HttpResponseResult del(Integer id) {
		organizationService.del(id);
		return succeed();
	}

	@ApiOperation(value = "获取当前分部及其所有子合作单位")
	@GetMapping(value = "getChildOrgans")
	@PreAuthorize("@pcs.hasPermissions('organization/getChildOrgans')")
	public HttpResponseResult<List<Organization>> getChildOrgans(Integer includeSelf){
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		Organization organization = organizationService.findByUserId(sysUser.getId());
		return succeed(organizationService.getChildOrgans(organization.getId(), includeSelf==1));
	}
}
