package com.zcloud.primeport.service;

import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.SingleResponse;
import com.zcloud.gbscommon.utils.DateUtil;
import com.zcloud.primeport.api.PersonApplyServiceI;
import com.zcloud.primeport.command.PersonApplyAddExe;
import com.zcloud.primeport.command.PersonApplyRemoveExe;
import com.zcloud.primeport.command.PersonApplyUpdateExe;
import com.zcloud.primeport.command.query.PersonApplyQueryExe;
import com.zcloud.primeport.domain.model.PersonApplyE;
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.plan.mjDevice.OnePersonUtil;
import jodd.util.Base64;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * web-app
 *
 * @Author makejava
 * @Date 2026-03-05 11:33:22
 */
@Service
@AllArgsConstructor
public class PersonApplyServiceImpl implements PersonApplyServiceI {
    private final PersonApplyAddExe personApplyAddExe;
    private final PersonApplyUpdateExe personApplyUpdateExe;
    private final PersonApplyRemoveExe personApplyRemoveExe;
    private final PersonApplyQueryExe personApplyQueryExe;
    private final OnePersonUtil onePersonUtil;

    @Override
    public PageResponse<XgfPersonApplyCO> listPage(PersonApplyPageQry qry) {

        return personApplyQueryExe.execute(qry);
    }

    @Override
    public SingleResponse add(PersonApplyAddCmd cmd) {

        personApplyAddExe.execute(cmd);
        return SingleResponse.buildSuccess();
    }

    @Override
    public void edit(PersonApplyUpdateCmd personApplyUpdateCmd) {
        personApplyUpdateExe.execute(personApplyUpdateCmd);
    }

    @Override
    public void remove(Long id) {
        personApplyRemoveExe.execute(id);
    }

    @Override
    public void removeBatch(Long[] ids) {
        personApplyRemoveExe.execute(ids);
    }

    @Override
    public Long xgfPersonSave(XgfPersonApplyAddCmd cmd) {
        return personApplyAddExe.xgfPersonSave(cmd);
    }

    @Override
    public PageResponse<XgfPersonApplyCO> xgfPersonAuditList(XgfApplyPersonPageQry qry) {
        return personApplyQueryExe.xgfPersonAuditList(qry);
    }

    @Override
    public PageResponse<XgfPersonApplyCO> xgfPersonApplyList(XgfApplyPersonPageQry qry) {
        return personApplyQueryExe.xgfPersonApplyList(qry);
    }

    @Override
    public PageResponse<PersonApplyCO> tmpPersonApplyH5List(XgfApplyPersonPageQry qry) {
        return personApplyQueryExe.tmpPersonApplyH5List(qry);
    }

    @Override
    public void xgfPersonAudit(XgfPersonApplyAuditCmd cmd) throws Exception {
        personApplyUpdateExe.xgfPersonAudit(cmd);
    }

    @Override
    public PageResponse<PersonCountCO> personnelVehicleManagementList(PersonCountPageQry qry) {
        return personApplyQueryExe.personnelVehicleManagementList(qry);
    }

    @Override
    public void authorization(PersonAuthCmd cmd) throws Exception {
        // 查一下这个人 有没有 授权，
        PersonApplyCO co = personApplyQueryExe.getInfoByUserId(cmd.getUserId());
        if (cmd.getMkmjPermission().equals(1)) {
            // 开启门禁
            if (co != null) {
                personApplyUpdateExe.authorization(co.getId(), co.getXgfApplyPersonId(), cmd.getMkmjPermission(), cmd.getGateLevelAuthArea());
                onePersonUtil.expirationDateRefreshedUserCard(Base64.decodeToString(co.getUserCard()), DateUtil.date2Str(new Date()), "2030-12-31 23:59:59");
            } else {
                personApplyAddExe.authorization(cmd);
                onePersonUtil.dockLongTermUserByUserCardAndFaceImg(Base64.decodeToString(co.getUserCard()), "hbqa"+Base64.decodeToString(co.getUserCard()), co.getEmployeePersonUserName(), co.getUserFaceUrl(), DateUtil.date2Str(new Date()), "2030-12-31 23:59:59");
            }
        } else {
            // 关闭门禁，这个人都没有授权过的话，什么都不管。
            if (co != null) {
                personApplyUpdateExe.authorization(co.getId(), co.getXgfApplyPersonId(), cmd.getMkmjPermission(), cmd.getGateLevelAuthArea());
                onePersonUtil.freezeByUserCard(Base64.decodeToString(co.getUserCard()), cmd.getMkmjPermission());
            }
        }
    }

    @Override
    public XgfPersonApplyCO xgfPersonApplyInfoById(Long id) {
        return personApplyAddExe.xgfPersonApplyInfoById(id);
    }

    @Override
    public PageResponse<PersonApplyCO> xgfPersonPermissionsList(XgfApplyPersonPageQry qry) {
        return personApplyQueryExe.xgfPersonPermissionsList(qry);
    }

    @Override
    public PageResponse<XgfPersonCountCO> xgfPersonnelVehicleManagementList(PersonCountPageQry qry) {
        return personApplyQueryExe.xgfPersonnelVehicleManagementList(qry);
    }

    @Override
    public void xgfPersonAuthorization(PersonAuthCmd cmd) {

        personApplyUpdateExe.xgfPersonAuthorization(cmd);
    }

    @Override
    public SingleResponse<PersonApplyCO> xgfPersonPermissionsInfo(Long id) {
        return personApplyQueryExe.xgfPersonPermissionsInfo(id);
    }
}

