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.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.OrgUserDetailParam;
import com.zhongshu.card.client.model.org.OrganizationUserModel;
import com.zhongshu.card.client.model.org.OrganizationUserSearch;
import com.zhongshu.card.client.model.org.UserCountAddParam;
import com.zhongshu.card.client.model.orgModel.OrgBindUserAllParam;
import com.zhongshu.card.client.model.school.ImportResultModel;
import com.zhongshu.card.client.service.org.UserAccountService;
import com.zhongshu.card.server.core.service.org.OrganizationUserServiceImpl;
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 jakarta.servlet.http.HttpServletResponse;
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.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;

@RestController
@RequestMapping("organizationUser")
@Tag(name = "机构管理-用户管理")
public class OrganizationUserController {

    @Autowired
    private OrganizationUserServiceImpl organizationUserService;

    @Autowired
    private UserAccountService userAccountService;

    /**
     * 现在主要是给用项目添加用户信息
     *
     * @param param
     * @return
     */
    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "机构保存用户", description = "机构保存用户")
    @RequestMapping(value = {"saveOrgUser"}, method = {RequestMethod.POST})
    public ResultContent saveOrgUser(@RequestBody UserCountAddParam param) {
        Assert.hasText(param.getCode(), "code不能为空");
        Assert.hasText(param.getProjectOid(), "projectOid不能为空");
        Assert.hasText(param.getOid(), "oid不能为空");
        Assert.hasText(param.getPhone(), "phone不能为空");
        return organizationUserService.saveOrgUser(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform)
    @Operation(summary = "机构绑定用户", description = "机构绑定用户，机构已存在 用户已存在")
    @RequestMapping(value = "orgBindUser", method = {RequestMethod.POST})
    public ResultContent orgBindUser(@RequestBody @Valid OrgBindUserAllParam param) {
        return this.organizationUserService.orgBindUser(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "项目用户列表-分页查询", description = "项目用户列表-分页查询")
    @RequestMapping(value = {"projectUserPage"}, method = {RequestMethod.POST})
    public ResultContent<Page<OrganizationUserModel>> projectUserPage(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable, @Parameter(required = false) OrganizationUserSearch param) {
        Assert.hasText(param.getProjectOid(), "projectOid不能为空");
        // 项目用户 包括项目下所有的机构的用户
        param.setIsSearchProject(Boolean.TRUE);
        param.setIsOrg(Boolean.TRUE);
        return userAccountService.projectUserPage(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "机构用户列表-分页查询", description = "机构用户列表-分页查询")
    @RequestMapping(value = {"orgUserPage"}, method = {RequestMethod.POST})
    public ResultContent<Page<OrganizationUserModel>> orgUserPage(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable, @Parameter(required = false) OrganizationUserSearch param) {
        param.setIsSearchProject(Boolean.FALSE);
        Assert.hasText(param.getOid(), "oid不能为空");
        return userAccountService.pageOrgUser(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "项目机构用户列表-分页查询", description = "项目机构用户列表-分页查询")
    @RequestMapping(value = {"projectOrgUserPage"}, method = {RequestMethod.POST})
    public ResultContent<Page<OrganizationUserModel>> projectOrgUserPage(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable, @Parameter(required = false) OrganizationUserSearch param) {
        param.setIsSearchProject(Boolean.TRUE);
        param.setNotAuthType(AuthType.Project);
        Assert.hasText(param.getProjectOid(), "projectOid不能为空");
        return userAccountService.pageOrgUser(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "机构用户详情", description = "平台用户详情")
    @RequestMapping(value = {"getOrgUserDetail"}, method = {RequestMethod.POST})
    public ResultContent getOrgUserDetail(@RequestBody OrgUserDetailParam param) {
        return userAccountService.getOrgUserDetail(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "删除机构用户", description = "删除机构用户")
    @RequestMapping(value = {"deleteOrgUser"}, method = {RequestMethod.POST})
    public ResultContent deleteOrgUser(@RequestBody IDParam param) {
        return organizationUserService.deleteOrgUser(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @RequestMapping(value = "importProjectUsers", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, method = RequestMethod.POST)
    @Operation(summary = "导入项目用户", description = "导入项目用户")
    public ResultContent<ImportResultModel> importProjectUsers(HttpServletRequest request, HttpServletResponse response, @RequestPart("file") MultipartFile file, ProjectOidParam param) throws IOException {
        if (file == null || file.isEmpty()) {
            return ResultContent.buildFail("文件不能为空");
        }
        return organizationUserService.importSchoolUsers(request, response, file, param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @RequestMapping(value = "exportProjectUser", method = RequestMethod.POST)
    @Operation(summary = "导出项目用户", description = "导出项目用户")
    public void exportProjectUser(HttpServletRequest request, HttpServletResponse response, OrganizationUserSearch param) throws IOException {
        organizationUserService.exportProjectUser(request, response, param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "查询当前用户的在项目的机构列表", description = "查询当前用户的在项目的机构列表")
    @RequestMapping(value = {"getCurrentOrgs"}, method = {RequestMethod.POST})
    public ResultContent getCurrentOrgs(@RequestBody ProjectOidParam param) {
        return organizationUserService.getCurrentOrgs(param.getProjectOid());
    }

}
