package com.zhongshu.card.server.core.controller.orgManager;

import com.github.microservice.auth.client.constant.AuthConstant;
import com.github.microservice.auth.security.annotations.ResourceAuth;
import com.github.microservice.auth.security.type.AuthType;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.base.OidModel;
import com.zhongshu.card.client.model.org.OrganizationSearchParam;
import com.zhongshu.card.client.model.org.OrganizationSimpleModel;
import com.zhongshu.card.client.model.orgModel.OrgSaveParam;
import com.zhongshu.card.server.core.service.orgManager.OrganizationManagerServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 超级管理员管理所有的机构
 */
@Slf4j
@RestController
@RequestMapping("organizationManager")
@Tag(name = "机构数据管理")
public class OrganizationManagerController {

    @Autowired
    private OrganizationManagerServiceImpl organizationManagerService;

    //------------------------------机构管理 start----------------------

    @ResourceAuth(value = "user", type = AuthType.User)
    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform)
    @Operation(summary = "机构列表-分页查询", description = "机构列表-分页查询")
    @RequestMapping(value = {"orgPage"}, method = {RequestMethod.POST})
    public ResultContent<Page<OrganizationSimpleModel>> orgPage(
            HttpServletRequest request,
            @Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable,
            @Parameter(required = false) OrganizationSearchParam param) {
        return organizationManagerService.orgPage(param, pageable);
    }


    @ResourceAuth(value = "user", type = AuthType.User)
    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform)
    @Operation(summary = "机构信息-保存", description = "机构信息-保存")
    @RequestMapping(value = "saveOrgInfo", method = {RequestMethod.POST})
    public ResultContent saveOrgInfo(@RequestBody @Valid OrgSaveParam param) {
        log.info("oid: {}  code: {}", param.getOid(), param.getCode());
        return this.organizationManagerService.saveOrgInfo(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到机构详情", description = "得到机构详情")
    @RequestMapping(value = "getOrgDetail", method = {RequestMethod.POST})
    public ResultContent<OrganizationSimpleModel> getOrgDetail(@RequestBody OidModel param) {
        return this.organizationManagerService.getOrgDetail(param.getOid());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "删除机构信息", description = "删除机构信息")
    @RequestMapping(value = "deleteOrg", method = {RequestMethod.POST})
    public ResultContent deleteOrg(@RequestBody OidModel param) {
        return this.organizationManagerService.deleteOrg(param.getOid());
    }

}
