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

import com.github.microservice.auth.security.annotations.ResourceAuth;
import com.github.microservice.auth.security.helper.AuthHelper;
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.visitor.*;
import com.zhongshu.card.server.core.service.visitor.VisitorMainService;
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.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 java.io.IOException;

/**
 * 访客管理
 *
 * @author TRX
 * @date 2024/6/5
 */
@RestController
@RequestMapping("/visitorManager")
@Tag(name = "访客管理-申请")
public class VisitorMainController {

    @Autowired
    private VisitorMainService visitorMainService;

    @Autowired
    private AuthHelper authHelper;

    @Operation(summary = "访客申请", description = "访客申请")
    @RequestMapping(value = "saveInfo", method = {RequestMethod.POST})
    public ResultContent saveInfo(@RequestBody VisitorMainParam param) {
        return this.visitorMainService.saveInfo(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "删除数据", description = "删除数据")
    @RequestMapping(value = "deleteInfo", method = {RequestMethod.POST})
    public ResultContent deleteInfo(@RequestBody IDParam param) {
        return this.visitorMainService.deleteInfo(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "所有数据列表-分页查询", description = "数据列表-分页查询")
    @RequestMapping(value = {"page"}, method = {RequestMethod.POST})
    public ResultContent<Page<VisitorMainModel>> page(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable, @Parameter(required = false) VisitorMainSearch param) {
        Assert.hasText(param.getProjectOid(), "projectOid不能为空");
        return visitorMainService.page(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "我的审核列表-分页查询", description = "数据列表-分页查询")
    @RequestMapping(value = {"myReViewPage"}, method = {RequestMethod.POST})
    public ResultContent<Page<VisitorMainModel>> myReViewPage(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable, @Parameter(required = false) VisitorMainSearch param) {
        Assert.hasText(param.getProjectOid(), "projectOid不能为空");
        // 被访人为当前用户
        String visitorUserId = "";
        if (authHelper.getCurrentUser() != null) {
            visitorUserId = authHelper.getCurrentUser().getUserId();
        }
        param.setVisitorUserId(visitorUserId);
        return visitorMainService.page(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "我的申请列表-分页查询（已登录的）", description = "数据列表-分页查询")
    @RequestMapping(value = {"myApplyPage"}, method = {RequestMethod.POST})
    public ResultContent<Page<VisitorMainModel>> myApplyPage(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable, @Parameter(required = false) VisitorMainSearch param) {
        Assert.hasText(param.getProjectOid(), "projectOid不能为空");
        String applyUserId = "";
        if (authHelper.getCurrentUser() != null) {
            applyUserId = authHelper.getCurrentUser().getUserId();
        }
        param.setApplyUserId(applyUserId);
        return visitorMainService.page(param, pageable);
    }

    @Operation(summary = "我的申请列表-分页查询（未登录的）", description = "数据列表-分页查询")
    @RequestMapping(value = {"myApplyPageUnLogin"}, method = {RequestMethod.POST})
    public ResultContent<Page<VisitorMainModel>> myApplyPageUnLogin(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable, @Parameter(required = false) VisitorMainSearch param) {
        Assert.hasText(param.getProjectOid(), "projectOid不能为空");
        Assert.hasText(param.getOpenId(), "openId不能为空");
        param.setIsDesen(Boolean.TRUE);
        return visitorMainService.page(param, pageable);
    }

    @Operation(summary = "访客申请详情", description = "访客申请详情")
    @RequestMapping(value = "getInfo", method = {RequestMethod.POST})
    public ResultContent getInfo(@RequestBody IDParam param) {
        return this.visitorMainService.getInfo(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "审核--拒绝申请", description = "审核--拒绝申请")
    @RequestMapping(value = "refuseVisitApply", method = {RequestMethod.POST})
    public ResultContent refuseVisitApply(@RequestBody VisitorReViewParam param) {
        return this.visitorMainService.refuseVisitApply(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "审核--通过申请", description = "审核--通过申请")
    @RequestMapping(value = "passVisitApply", method = {RequestMethod.POST})
    public ResultContent passVisitApply(@RequestBody VisitorReViewParam param) {
        return this.visitorMainService.passVisitApply(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "取消申请", description = "取消申请")
    @RequestMapping(value = "cancelVisitApply", method = {RequestMethod.POST})
    public ResultContent cancelVisitApply(@RequestBody IDParam param) {
        return this.visitorMainService.cancelVisitApply(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "当前用户信息统计（被访人员）", description = "当前用户信息统计")
    @RequestMapping(value = "staticCurrentUserTotal", method = {RequestMethod.POST})
    public ResultContent staticCurrentUserTotal(@RequestBody VisitorStaticParam param) {
        return this.visitorMainService.staticCurrentUserTotal(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @RequestMapping(value = "exportData", method = RequestMethod.POST)
    @Operation(summary = "导出访客数据", description = "导出访客数据")
    public void exportData(HttpServletRequest request, HttpServletResponse response, VisitorMainSearch param) throws IOException {
        visitorMainService.exportData(request, response, param);
    }
}
