package com.zcloud.primeport.web.close;


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.alibaba.fastjson.JSON;
import com.zcloud.primeport.api.ClosedAreaCarApplyServiceI;
import com.zcloud.primeport.api.ClosedAreaPersonApplyServiceI;
import com.zcloud.primeport.api.PersonApplyServiceI;
import com.zcloud.primeport.api.VehicleApplyServiceI;
import com.zcloud.primeport.domain.enums.TmpApplyEnum;
import com.zcloud.primeport.domain.model.VehicleApplyE;
import com.zcloud.primeport.dto.*;
import com.zcloud.primeport.dto.clientobject.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * web-adapter
 *
 * @Author dearLin
 * @Date 2026-03-20 10:07:13
 */
@Api(tags = "封闭区域-临时人员-车辆申请")
@RequestMapping("/${application.gateway}/closedTmpApply")
@RestController
@AllArgsConstructor
public class ClosedTmpApplyController {
    private final ClosedAreaCarApplyServiceI closedAreaCarApplyService;
    private final ClosedAreaPersonApplyServiceI closedAreaPersonApplyServiceI;
    private final PersonApplyServiceI personApplyServiceI;
    private final VehicleApplyServiceI vehicleApplyServiceI;

    @ApiOperation("新增")
    @PostMapping("/save")
    public SingleResponse<HashMap<String, Object>> add(@Validated @RequestBody ClosedTmpApplyAddCmd cmd) {
        Long id = 0L;
        if (cmd.getTmpApplyType().equals(TmpApplyEnum.PERSON_APPLY.getCode())) {
            if (cmd.getTmpMkmjType().equals(TmpApplyEnum.TWO_LEVEL_PERSON_APPLY.getCode())) {
                id = closedAreaPersonApplyServiceI.saveTmpPersonApply(cmd);
            }
            if (cmd.getTmpMkmjType().equals(TmpApplyEnum.ONE_LEVEL_PERSON_APPLY.getCode())) {
                XgfPersonApplyAddCmd xgf = new XgfPersonApplyAddCmd();
                BeanUtils.copyProperties(cmd, xgf);
                xgf.setPersonBelongType(4);
                HashMap<String, Object> stringObjectHashMap = new HashMap<>();
                ArrayList<HashMap<String, Object>> hashMaps = new ArrayList<>();
                stringObjectHashMap.put("bianma", cmd.getHgAuthArea());
                stringObjectHashMap.put("value", cmd.getHgAuthAreaName());
                hashMaps.add(stringObjectHashMap);
                HashMap<String, Object> stringObjectHashMap2 = new HashMap<>();
                stringObjectHashMap2.put("area", hashMaps);
                xgf.setGateLevelAuthArea(JSON.toJSONString(stringObjectHashMap2));
                xgf.setAuditCorpId(cmd.getAuditPersonCorpId());
                xgf.setAuditCorpName(cmd.getAuditPersonCorpName());
                xgf.setAuditDeptId(cmd.getAuditPersonDepartmentId());
                xgf.setAuditDeptName(cmd.getAuditPersonDepartmentName());
                xgf.setAuditUserId(cmd.getAuditPersonUserId());
                xgf.setAuditUserName(cmd.getAuditPersonUserName());
                xgf.setReasonVisit(cmd.getApplyReason());
                xgf.setMkmjId(cmd.getLevelOneMkmjId());
                xgf.setMkmjName(cmd.getLevelOneMkmjName());
                XgfPersonApplyAddCmd.PersonApplyAddCmdExt personApplyAddCmdExt = new XgfPersonApplyAddCmd.PersonApplyAddCmdExt();
                personApplyAddCmdExt.setEmployeePersonUserName(cmd.getApplyPersonUserName());
                personApplyAddCmdExt.setUserFaceUrl(cmd.getUserFaceUrl());
                personApplyAddCmdExt.setUserPhone(cmd.getUserPhone());
                personApplyAddCmdExt.setUserCard(cmd.getUserCard());
                ArrayList<XgfPersonApplyAddCmd.PersonApplyAddCmdExt> personApplyAddCmdExts = new ArrayList<>();
                personApplyAddCmdExts.add(personApplyAddCmdExt);
                xgf.setPersonApplyList(personApplyAddCmdExts);
                id = personApplyServiceI.xgfPersonSave(xgf);
            }
        }
        if (cmd.getTmpApplyType().equals(TmpApplyEnum.CAR_APPLY.getCode())) {

            if (cmd.getApplyPersonUserName() == null) {
                cmd.setApplyPersonUserName(cmd.getDrivingUserName());
            }

            if (cmd.getTmpMkmjType().equals(TmpApplyEnum.ONE_LEVEL_CAR_APPLY.getCode())) {
                VehicleApplyAddCmd vehicleApplyAddCmd = new VehicleApplyAddCmd();
                BeanUtils.copyProperties(cmd, vehicleApplyAddCmd);
                vehicleApplyAddCmd.setVehicleBelongType("6");
                HashMap<String, Object> stringObjectHashMap = new HashMap<>();
                ArrayList<HashMap<String, Object>> hashMaps = new ArrayList<>();
                stringObjectHashMap.put("bianma", cmd.getHgAuthArea());
                stringObjectHashMap.put("value", cmd.getHgAuthAreaName());
                hashMaps.add(stringObjectHashMap);
                HashMap<String, Object> stringObjectHashMap2 = new HashMap<>();
                stringObjectHashMap2.put("area", hashMaps);
                vehicleApplyAddCmd.setGateLevelAuthArea(JSON.toJSONString(stringObjectHashMap2));
                vehicleApplyAddCmd.setAuditCorpId(cmd.getAuditPersonCorpId());
                vehicleApplyAddCmd.setAuditCorpName(cmd.getAuditPersonCorpName());
                vehicleApplyAddCmd.setAuditDeptId(cmd.getAuditPersonDepartmentId());
                vehicleApplyAddCmd.setAuditDeptName(cmd.getAuditPersonDepartmentName());
                vehicleApplyAddCmd.setAuditUserId(cmd.getAuditPersonUserId());
                vehicleApplyAddCmd.setAuditUserName(cmd.getAuditPersonUserName());
                vehicleApplyAddCmd.setMkmjId(cmd.getLevelOneMkmjId());
                vehicleApplyAddCmd.setMkmjName(cmd.getLevelOneMkmjName());
                vehicleApplyAddCmd.setLsUserIdcard(cmd.getUserCard());
                vehicleApplyAddCmd.setLsUserPhone(cmd.getUserPhone());
                vehicleApplyAddCmd.setEmployeeVehicleUserName(cmd.getApplyPersonUserName());
                vehicleApplyAddCmd.setDrivingUserName(cmd.getDrivingUserName());
                vehicleApplyAddCmd.setRemarks(cmd.getApplyReason());
                SingleResponse<VehicleApplyE> add = vehicleApplyServiceI.add(vehicleApplyAddCmd);
                id = add.getData().getId();
            }
            if (cmd.getTmpMkmjType().equals(TmpApplyEnum.TWO_LEVEL_CAR_APPLY.getCode())) {
                id = closedAreaCarApplyService.saveTmpCarApply(cmd);
            }
        }
        HashMap<String, Object> stringLongHashMap = new HashMap<>();
        stringLongHashMap.put("id", id);
        stringLongHashMap.put("tmpApplyType", cmd.getTmpApplyType());
        stringLongHashMap.put("tmpMkmjType", cmd.getTmpMkmjType());
        return SingleResponse.of(stringLongHashMap);
    }

    @ApiOperation("详情")
    @GetMapping("/getInfoById")
    public SingleResponse<TmpApplyCO> getInfoById(@RequestParam("id") Long id,
                                                  @RequestParam("tmpApplyType") String tmpApplyType, @RequestParam("tmpMkmjType") String tmpMkmjType) {
        TmpApplyCO tmpApplyCO = new TmpApplyCO();
        if (TmpApplyEnum.PERSON_APPLY.getCode().equals(tmpApplyType)) {
            if (TmpApplyEnum.ONE_LEVEL_PERSON_APPLY.getCode().equals(tmpMkmjType)) {
                XgfPersonApplyCO xgfPersonApplyCO = personApplyServiceI.xgfPersonApplyInfoById(id);
                tmpApplyCO.setApplyReason(xgfPersonApplyCO.getReasonVisit());
                tmpApplyCO.setAuditFlag(xgfPersonApplyCO.getAuditFlag());
                tmpApplyCO.setRefusalReason(xgfPersonApplyCO.getReasonsRefusal());
                tmpApplyCO.setAuditPersonUserId(xgfPersonApplyCO.getAuditUserId());
                tmpApplyCO.setAuditPersonUserName(xgfPersonApplyCO.getAuditUserName());
                tmpApplyCO.setVisitStartTime(xgfPersonApplyCO.getVisitStartTime());
                tmpApplyCO.setVisitEndTime(xgfPersonApplyCO.getVisitEndTime());
                tmpApplyCO.setLevelOneMkmjId(xgfPersonApplyCO.getMkmjId());
                tmpApplyCO.setLevelOneMkmjName(xgfPersonApplyCO.getMkmjName());
                tmpApplyCO.setUserPhone(xgfPersonApplyCO.getPersonApplyList().get(0).getUserPhone());
                tmpApplyCO.setApplyPersonUserName(xgfPersonApplyCO.getPersonApplyList().get(0).getEmployeePersonUserName());
            }
            if (TmpApplyEnum.TWO_LEVEL_PERSON_APPLY.getCode().equals(tmpMkmjType)) {
                ClosedAreaPersonApplyCO closedAreaPersonApplyCO = closedAreaPersonApplyServiceI.queryById(id);
                BeanUtils.copyProperties(closedAreaPersonApplyCO, tmpApplyCO);
            }
        }
        if (TmpApplyEnum.CAR_APPLY.getCode().equals(tmpApplyType)) {
            if (TmpApplyEnum.ONE_LEVEL_CAR_APPLY.getCode().equals(tmpMkmjType)) {
                VehicleApplyCO infoById = vehicleApplyServiceI.getInfoById(id);
                BeanUtils.copyProperties(infoById, tmpApplyCO);
                tmpApplyCO.setLevelOneMkmjName(infoById.getMkmjName());
                tmpApplyCO.setLevelOneMkmjId(infoById.getMkmjId());
                tmpApplyCO.setUserPhone(infoById.getLsUserPhone());
                tmpApplyCO.setApplyPersonUserName(infoById.getEmployeeVehicleUserName());
                tmpApplyCO.setRefusalReason(infoById.getCurrentRemarks());
            }
            if (TmpApplyEnum.TWO_LEVEL_CAR_APPLY.getCode().equals(tmpMkmjType)) {
                ClosedAreaCarApplyCO closedAreaCarApplyCO = closedAreaCarApplyService.queryById(id);
                BeanUtils.copyProperties(closedAreaCarApplyCO, tmpApplyCO);
            }
        }
        tmpApplyCO.setId(id);
        tmpApplyCO.setTmpApplyType(tmpApplyType);
        return SingleResponse.of(tmpApplyCO);
    }

}

