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

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.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.SchoolUserCountParam;
import com.zhongshu.card.client.model.school.ImportResultModel;
import com.zhongshu.card.client.model.school.RegisterBindSchoolParam;
import com.zhongshu.card.client.model.school.RegisterUserParam;
import com.zhongshu.card.client.model.school.SchoolUserModel;
import com.zhongshu.card.client.service.org.UserAccountService;
import com.zhongshu.card.client.service.school.SchoolUserService;
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.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @author TRX
 * @date 2024/6/12
 */
@RestController
@RequestMapping("/school/schoolUser")
@Tag(name = "学校-用户管理")
public class SchoolUserController {

    @Autowired
    SchoolUserService schoolUserService;

    @Autowired
    UserAccountService userAccountService;

    @Autowired
    OrganizationUserServiceImpl organizationUserService;

//    @ResourceAuth(value = "schoolUserManager", type = AuthType.Platform, remark = "人员管理_人员列表_1_Menu")
//    @ResourceAuth(value = "schoolUserManager", type = AuthType.Project, remark = "人员管理_人员列表_1_Menu")
//    @ResourceAuth(value = "schoolUserManager", type = AuthType.School, remark = "人员管理_人员列表_1_Menu")
    @ResourceAuth(value = AuthConstant.SuperAdmin, type = AuthType.Platform, remark = "")
    @ResourceAuth(value = AuthConstant.Admin, type = AuthType.Project, remark = "")
    @ResourceAuth(value = AuthConstant.Admin, type = AuthType.School, remark = "")
    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "人员列表-分页查询", description = "人员列表-分页查询")
    @RequestMapping(value = {"pageOrgUserManager"}, method = {RequestMethod.POST})
    public ResultContent<Page<OrganizationUserModel>> pageOrgUserManager(
            @Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable,
            @Parameter(required = false) OrganizationUserSearch param) {
        return userAccountService.pageOrgUser(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "人员列表-分页查询", description = "人员列表-分页查询")
    @RequestMapping(value = {"pageOrgUser"}, method = {RequestMethod.POST})
    public ResultContent<Page<OrganizationUserModel>> page(
            @Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable,
            @Parameter(required = false) OrganizationUserSearch param) {
        return userAccountService.pageOrgUser(param, pageable);
    }

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

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

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "添加-编辑校园端用户（包含卡片信息）", description = "添加-编辑校园端用户")
    @RequestMapping(value = {"addOrUpdateSchoolUser"}, method = {RequestMethod.POST})
    public ResultContent addOrUpdateSchoolUser(@RequestBody SchoolUserCountParam param) {
        return schoolUserService.addOrUpdateSchoolUser(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "删除校园端用户", description = "删除校园端用户")
    @RequestMapping(value = {"deleteSchoolUser"}, method = {RequestMethod.POST})
    public ResultContent<SchoolUserModel> deleteUser(@RequestBody IDParam param) {
        return schoolUserService.deleteUser(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到校园用户详情（包含卡片信息）", description = "得到用户详情")
    @RequestMapping(value = {"getSchoolUserDetail"}, method = {RequestMethod.POST})
    public ResultContent<SchoolUserModel> getSchoolUserDetail(@RequestBody OrgUserDetailParam param) {
        return schoolUserService.getSchoolUserDetail(param);
    }

    //-----------------------小程序c端用户管理 start--------------------

    @Operation(summary = "注册用户c端用户 第一步", description = "注册用户c端用户")
    @RequestMapping(value = {"registerUser"}, method = {RequestMethod.POST})
    public ResultContent<SchoolUserModel> registerUser(@RequestBody @Valid RegisterUserParam param) {
        return schoolUserService.register(param);
    }

    @Operation(summary = "注册用户c端用户 第二步 用户绑定学校", description = "注册用户c端用户 第二步")
    @RequestMapping(value = {"bindUser2School"}, method = {RequestMethod.POST})
    public ResultContent<SchoolUserModel> bindUser2School(@RequestBody @Valid RegisterBindSchoolParam param) {
        return organizationUserService.bindUser2School(param);
    }

}
