package com.zcloud.primeport.command;

import com.alibaba.cola.exception.BizException;
import com.zcloud.gbscommon.utils.DateUtil;
import com.zcloud.primeport.domain.gateway.MkmjGateGateway;
import com.zcloud.primeport.domain.gateway.PersonApplyGateway;
import com.zcloud.primeport.domain.gateway.XgfApplyPersonGateway;
import com.zcloud.primeport.domain.model.PersonApplyE;
import com.zcloud.primeport.domain.model.XgfApplyPersonE;
import com.zcloud.primeport.dto.PersonApplyUpdateCmd;
import com.zcloud.primeport.dto.PersonAuthCmd;
import com.zcloud.primeport.dto.XgfPersonApplyAuditCmd;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.util.Date;


/**
 * web-app
 *
 * @Author makejava
 * @Date 2026-03-05 11:33:23
 */
@Component
@AllArgsConstructor
public class PersonApplyUpdateExe {
    private final PersonApplyGateway personApplyGateway;
    private final XgfApplyPersonGateway xgfApplyPersonGateway;
    private final MkmjGateGateway mkmjGateGateway;

    @Transactional(rollbackFor = Exception.class)
    public void execute(PersonApplyUpdateCmd personApplyUpdateCmd) {
        PersonApplyE personApplyE = new PersonApplyE();
        BeanUtils.copyProperties(personApplyUpdateCmd, personApplyE);
        boolean res = personApplyGateway.update(personApplyE);
        if (!res) {
            throw new BizException("修改失败");
        }
    }
    @Transactional(rollbackFor = Exception.class)

    public void xgfPersonAudit(XgfPersonApplyAuditCmd cmd) {
        XgfApplyPersonE build = XgfApplyPersonE.builder()
                .auditFlag(cmd.getAuditFlag())
                .auditTime(DateUtil.date2Str(new Date()))
                .reasonsRefusal(cmd.getReasonsRefusal()).build();
        build.setId(cmd.getId());
        personApplyGateway.xgfPersonAudit(build);

    }

    @Transactional(rollbackFor = Exception.class)
    public void authorization(Long applyPersonId, Long XgfApplyPersonId, Integer mkmjPermission, String gate) {
        PersonApplyE personApplyE = new PersonApplyE();
        personApplyE.setId(applyPersonId);
        personApplyE.setMkmjPermission(mkmjPermission);
        personApplyGateway.update(personApplyE);
        XgfApplyPersonE xgfApplyPersonE = new XgfApplyPersonE();
        xgfApplyPersonE.setId(XgfApplyPersonId);
        xgfApplyPersonE.setGateLevelAuthArea(gate);
        xgfApplyPersonGateway.update(xgfApplyPersonE);
    }

    @Transactional(rollbackFor = Exception.class)
    public void xgfPersonAuthorization(PersonAuthCmd cmd) {
        PersonApplyE personApplyE = new PersonApplyE();
        personApplyE.setId(cmd.getApplyPersonId());
        personApplyE.setMkmjPermission(cmd.getMkmjPermission());
        personApplyGateway.update(personApplyE);
    }
}

