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.IDParam;
import com.zhongshu.card.client.model.base.ProjectOidParam;
import com.zhongshu.card.client.model.org.OrganizationMiniModel;
import com.zhongshu.card.client.model.org.OrganizationRelationSearch;
import com.zhongshu.card.client.model.orgModel.ProjectBindOrgModel;
import com.zhongshu.card.client.model.orgModel.ProjectBindOrgParam;
import com.zhongshu.card.client.model.orgModel.ProjectBindOrgsParam;
import com.zhongshu.card.client.type.OrganizationRelationType;
import com.zhongshu.card.server.core.service.orgManager.ProjectBindOrgServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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.util.Assert;
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;
import java.util.List;

@RestController
@RequestMapping("projectBindOrg")
@Tag(name = "项目绑定机构管理")
public class ProjectBindOrgController {

    @Autowired
    private ProjectBindOrgServiceImpl projectBindOrgService;

    //------------------------------机构管理 start----------------------
    @ResourceAuth(value = "user", type = AuthType.User)
    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform)
    @Operation(summary = "项目绑定机构-保存", description = "项目绑定机构-保存")
    @RequestMapping(value = "projectBindOrg", method = {RequestMethod.POST})
    public ResultContent projectBindOrg(@RequestBody @Valid ProjectBindOrgParam param) {
        return this.projectBindOrgService.projectBindOrg(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform)
    @Operation(summary = "项目绑定机构(多个)-保存", description = "项目绑定机构（多个）-保存")
    @RequestMapping(value = "projectBindOids", method = {RequestMethod.POST})
    public ResultContent projectBindOids(@RequestBody @Valid ProjectBindOrgsParam param) {
        return this.projectBindOrgService.projectBindOids(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform)
    @Operation(summary = "项目机构列表-分页查询", description = "项目机构列表-分页查询")
    @RequestMapping(value = {"projectOrgPage"}, method = {RequestMethod.POST})
    public ResultContent<Page<ProjectBindOrgModel>> projectOrgPage(
            @Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable,
            @Parameter(required = false) OrganizationRelationSearch param) {
        param.setRelationType(OrganizationRelationType.ProjectBindOrg);
        return projectBindOrgService.orgPage(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到项目下所有的机构列表数据", description = "得到项目下所有的机构列表数据")
    @RequestMapping(value = "getProjectOrgList", method = {RequestMethod.POST})
    public ResultContent<List<OrganizationMiniModel>> getProjectOrgList(
            @RequestBody ProjectOidParam param) {
        Assert.hasText(param.getProjectOid(), "projectOid不能为空");
        return this.projectBindOrgService.getProjectOrgList(param.getProjectOid());
    }

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

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到项目机构信息", description = "得到项目机构信息")
    @RequestMapping(value = "getDetailRelation", method = {RequestMethod.POST})
    public ResultContent getDetailRelation(@RequestBody IDParam param) {
        return this.projectBindOrgService.getDetailRelation(param.getId());
    }

}
