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

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.zhongshu.card.client.model.base.OidModel;
import com.zhongshu.card.client.model.org.*;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.project.ProjectSaveParam;
import com.zhongshu.card.client.service.org.OrganizationService;
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.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("organization")
@Tag(name = "机构管理")
public class OrganizationController {

    @Autowired
    private OrganizationService organizationService;

    //------------------------------项目管理 start----------------------
    @ResourceAuth(value = "saveProjectInfo", type = AuthType.Platform, remark = "园区项目管理_保存项目_11_Button")
    @ResourceAuth(value = "user", type = AuthType.User)
    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform)
    @Operation(summary = "项目-保存", description = "项目-保存")
    @RequestMapping(value = "saveProjectInfo", method = {RequestMethod.POST})
    public ResultContent saveProjectInfo(@RequestBody @Valid ProjectSaveParam param) {
        param.setAuthType(AuthType.Project);
        return this.organizationService.saveProjectInfo(param);
    }


    @ResourceAuth(value = "user", type = AuthType.User)
    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform)
    @Operation(summary = "项目列表-分页查询", description = "项目列表-分页查询")
    @RequestMapping(value = {"pageProject"}, method = {RequestMethod.POST})
    public ResultContent<Page<ProjectOrgModel>> pageProject(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable, @Parameter(required = false) OrganizationSearchParam param) {
        param.setAuthType(AuthType.Project);
        return organizationService.pageProject(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到项目详情", description = "得到项目详情")
    @RequestMapping(value = "getProjectDetail", method = {RequestMethod.GET})
    public ResultContent<ProjectOrgModel> getProjectDetail(@Parameter(name = "oid", description = "机构oid", example = "") @RequestParam("oid") String oid) {
        Assert.hasText(oid, "oid不能为空");
        return this.organizationService.getProjectDetail(oid);
    }

    @ResourceAuth(value = "deleteProject", type = AuthType.Platform, remark = "园区项目管理_删除项目_12_Button")
    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "删除项目信息", description = "删除项目信息")
    @RequestMapping(value = "deleteProject", method = {RequestMethod.GET})
    public ResultContent<ProjectOrgModel> deleteProject(@Parameter(name = "oid", description = "机构oid", example = "") @RequestParam("oid") String oid) {
        Assert.hasText(oid, "oid不能为空");
        return this.organizationService.deleteProject(oid);
    }


    //-----------------------------old start ---------------------------
    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到平台管理端的机构数据", description = "得到平台管理端的机构数据")
    @RequestMapping(value = "getPlatformInfo", method = {RequestMethod.GET})
    public ResultContent<OrganizationModel> getPlatformInfo() {
        return this.organizationService.getPlatformInfo();
    }


    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到机构详情", description = "得到机构详情--学校、商户")
    @RequestMapping(value = "getOrgDetail", method = {RequestMethod.GET})
    public ResultContent<OrganizationModel> getOrgDetail(@Parameter(name = "oid", description = "机构oid", example = "") @RequestParam("oid") String oid) {
        Assert.hasText(oid, "oid不能为空");
        return this.organizationService.getOrgDetail(oid);
    }

    //------------------------------项目管理 end------------------------

    //------------------------------学校管理 start----------------------

    @ResourceAuth(value = "user", type = AuthType.User)
    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform)
    @Operation(summary = "学校-添加", description = "学校-添加")
    @RequestMapping(value = "addSchool", method = {RequestMethod.POST})
    public ResultContent addSchool(@RequestBody SchoolAddParam param) {
        param.setAuthType(AuthType.School);
        return this.organizationService.addSchool(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "学校-编辑", description = "学校-编辑")
    @RequestMapping(value = "updateSchool", method = {RequestMethod.POST})
    public ResultContent updateSchool(@RequestBody SchoolUpdateParam param) {
        Assert.hasText(param.getId(), "id不能为空");
        return this.organizationService.updateSchool(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "学校列表-分页查询", description = "学校列表-分页查询")
    @RequestMapping(value = {"pageSchool"}, method = {RequestMethod.POST})
    public ResultContent<Page<OrganizationModel>> pageSchool(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable, @Parameter(required = false) OrganizationSearchParam param) {
        param.setAuthType(AuthType.School);
        return organizationService.page(param, pageable);
    }

    @Operation(summary = "得到所有可用的学校数据", description = "得到可用的学校数据")
    @RequestMapping(value = {"getUsableSchool"}, method = {RequestMethod.POST})
    public ResultContent<List<OrganizationSimpleModel>> getUsableSchool(@Parameter(required = false) OrganizationSearchParam param) {
        param.setAuthType(AuthType.School);
        return organizationService.list(param);
    }

    @Operation(summary = "得到可用的商户数据", description = "得到可用的商户数据")
    @RequestMapping(value = {"getUsableShop"}, method = {RequestMethod.POST})
    public ResultContent<List<OrganizationSimpleModel>> getUsableShop(@Parameter(required = false) OrganizationSearchParam param) {
        param.setAuthType(AuthType.BusinessMain);
        return organizationService.list(param);
    }

    //------------------------------学校管理 end------------------------

    //------------------------------商户管理 start----------------------

    @ResourceAuth(value = "user", type = AuthType.User)
    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform)
    @Operation(summary = "商户-添加", description = "商户-添加")
    @RequestMapping(value = "addShop", method = {RequestMethod.POST})
    public ResultContent addShop(@RequestBody OrganizationAddParam param) {
        param.setAuthType(AuthType.BusinessMain);
        return this.organizationService.addOrganization(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "商户-编辑", description = "商户-编辑")
    @RequestMapping(value = "updateShop", method = {RequestMethod.POST})
    public ResultContent updateShop(@RequestBody OrganizationUpdateParam param) {
        Assert.hasText(param.getId(), "id不能为空");
        return this.organizationService.update(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "商户列表-分页查询", description = "商户列表-分页查询")
    @RequestMapping(value = {"pageShop"}, method = {RequestMethod.POST})
    public ResultContent<Page<OrganizationModel>> pageShop(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable, @Parameter(required = false) OrganizationSearchParam param) {
        param.setAuthType(AuthType.BusinessMain);
        return organizationService.page(param, pageable);
    }
    //------------------------------商户管理 end------------------------

    @ResourceAuth(value = "user", type = AuthType.User)
    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform)
    @Operation(summary = "项目-学校-商家-删除", description = "项目-学校-商家-删除")
    @RequestMapping(value = "delete", method = {RequestMethod.POST})
    public ResultContent delete(@RequestBody OidModel param) {
        return this.organizationService.deleteOrganization(param.getOid());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform)
    @Operation(summary = "添加机构", description = "添加机构")
    @RequestMapping(value = "add", method = {RequestMethod.POST})
    public ResultContent add(@RequestBody OrganizationAddParam param) {
        return this.organizationService.addOrganization(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "编辑机构-项目-学校", description = "编辑机构-项目-学校")
    @RequestMapping(value = "update", method = {RequestMethod.POST})
    public ResultContent update(@RequestBody OrganizationUpdateParam param) {
        Assert.hasText(param.getId(), "id不能为空");
        return this.organizationService.update(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "项目编码生成器", description = "项目编码生成器")
    @RequestMapping(value = "buildProjectCode", method = {RequestMethod.GET})
    public ResultContent buildProjectCode() {
        return this.organizationService.buildCode(AuthType.Project);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "查询所有可用的项目列表", description = "查询所有可用的项目列表")
    @RequestMapping(value = "getProjectList", method = {RequestMethod.GET})
    public ResultContent<List<OrganizationSimpleModel>> getProjectList() {
        return this.organizationService.getProjectList();
    }

}
