package com.zcloud.primeport.web;


import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.jjb.saas.framework.auth.model.SSOUser;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.primeport.api.PersonApplyServiceI;
import com.zcloud.primeport.dto.*;
import com.zcloud.primeport.dto.clientobject.PersonApplyCO;
import com.zcloud.primeport.dto.clientobject.PersonCountCO;
import com.zcloud.primeport.dto.clientobject.XgfPersonApplyCO;
import com.zcloud.primeport.dto.clientobject.XgfPersonCountCO;
import com.zcloud.primeport.persistence.dataobject.XgfApplyPersonDO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * web-adapter
 *
 * @Author makejava
 * @Date 2026-03-05 11:33:21
 */
@Api(tags = "人员申请审批信息")
@RequestMapping("/${application.gateway}/personApply")
@RestController
@AllArgsConstructor
public class PersonApplyController {
    private final PersonApplyServiceI personApplyService;

    @ApiOperation("相关方-人员权限记录-列表")
    @PostMapping("/xgfPersonPermissionsList")
    public PageResponse<PersonApplyCO> xgfPersonPermissionsList(@RequestBody XgfApplyPersonPageQry qry) {
        if (qry.getApplyCorpId() == null) {
            qry.setApplyCorpId(AuthContext.getTenantId());
        }
        return personApplyService.xgfPersonPermissionsList(qry);
    }
    @ApiOperation("相关方-人员权限记录-详情")
    @GetMapping("/xgfPersonPermissionsInfo/{id}")
    public SingleResponse<PersonApplyCO> xgfPersonPermissionsInfo(@PathVariable("id") Long id) {

        return personApplyService.xgfPersonPermissionsInfo(id);
    }
    @ApiOperation("相关方-人员申请-详情")
    @GetMapping("/xgfPersonApplyInfoById/{id}")
    public SingleResponse<XgfPersonApplyCO> xgfPersonApplyInfoById(@PathVariable("id") Long id) {
        return SingleResponse.of(personApplyService.xgfPersonApplyInfoById(id));
    }
    @ApiOperation("待审批的相关方申请人员入场列表-同已审批记录")
    @PostMapping("/xgfPersonAuditList")
    public PageResponse<XgfPersonApplyCO> xgfPersonAuditList(@RequestBody XgfApplyPersonPageQry qry) {
        return personApplyService.xgfPersonAuditList(qry);
    }
    @ApiOperation("相关方人员入场申请列表")
    @PostMapping("/xgfPersonApplyList")
    public PageResponse<XgfPersonApplyCO> xgfPersonApplyList(@RequestBody XgfApplyPersonPageQry qry) {
        return personApplyService.xgfPersonApplyList(qry);
    }
    @ApiOperation("临时访客人员入场申请列表-H5")
    @PostMapping("/tmpPersonApplyH5List")
    public PageResponse<PersonApplyCO> tmpPersonApplyH5List(@RequestBody XgfApplyPersonPageQry qry) {
        return personApplyService.tmpPersonApplyH5List(qry);
    }
    @ApiOperation("相关方、临时-人员申请审批")
    @PostMapping("/xgfPersonAudit")
    public Response xgfPersonAudit(@Validated @RequestBody XgfPersonApplyAuditCmd cmd) throws Exception {
        if (!(cmd.getAuditFlag().equals(2) || cmd.getAuditFlag().equals(3))) {
            throw new BizException("审批状态必须是通过或不通过");
        }
        personApplyService.xgfPersonAudit(cmd);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("相关方-人员申请新增")
    @PostMapping("/xgfPersonSave")
    public Response xgfPersonSave(@Validated @RequestBody XgfPersonApplyAddCmd cmd) {
        personApplyService.xgfPersonSave(cmd);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("人员临时申请-审批待审批分页列表")
    @PostMapping("/pendingApprovalList")
    public PageResponse<XgfPersonApplyCO> page(@RequestBody PersonApplyPageQry qry) {
        return personApplyService.listPage(qry);
    }

    @ApiOperation("详情")
    @GetMapping("/{id}")
    public SingleResponse<PersonApplyCO> getInfoById(@PathVariable("id") Long id) {
        return SingleResponse.of(new PersonApplyCO());
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public Response remove(@PathVariable("id") Long id) {
        personApplyService.remove(id);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("相关方人员及车辆管理-首页分页")
    @GetMapping("/xgfPersonnelVehicleManagementList")
    public PageResponse<XgfPersonCountCO> xgfPersonnelVehicleManagementList(PersonCountPageQry qry) {
        return personApplyService.xgfPersonnelVehicleManagementList(qry);
    }

    @ApiOperation("股份，分公司-人员及车辆管理-分页")
    @GetMapping("/personnelVehicleManagementList")
    public PageResponse<PersonCountCO> personnelVehicleManagementList(PersonCountPageQry qry) {
        if (qry.getCorpId() == null) {
            qry.setCorpId(AuthContext.getTenantId());
        }
        return personApplyService.personnelVehicleManagementList(qry);
    }
    @ApiOperation("相关方-人员授权")
    @PostMapping("/xgfPersonAuthorization")
    public Response xgfPersonAuthorization(@RequestBody PersonAuthCmd cmd) {
        personApplyService.xgfPersonAuthorization(cmd);
        return Response.buildSuccess();
    }
    @ApiOperation("股份，分公司-人员授权")
    @PostMapping("/authorization")
    public Response authorization(@RequestBody PersonAuthCmd cmd) throws Exception {
        personApplyService.authorization(cmd);
        return Response.buildSuccess();
    }
}

