package com.zcloud.personnel.positioning.command;

import com.alibaba.cola.exception.BizException;
import com.zcloud.personnel.positioning.command.query.TerminalCardQueryExe;
import com.zcloud.personnel.positioning.domain.model.TerminalE;
import com.zcloud.personnel.positioning.dto.TerminalCardSaveCmd;
import com.zcloud.personnel.positioning.dto.clientobject.TerminalCardCO;
import com.zcloud.personnel.positioning.persistence.dataobject.CorpInfoSnapshotDO;
import com.zcloud.personnel.positioning.persistence.dataobject.TerminalPersonSnapshotDO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

@Component
@AllArgsConstructor
public class TerminalCardAddExe {
    private static final String DEVICE_TYPE_CARD = "CARD";

    private final TerminalBindingService terminalBindingService;
    private final FindsPendingSyncService findsPendingSyncService;
    private final TerminalCardQueryExe terminalCardQueryExe;

    @Transactional(rollbackFor = Exception.class)
    public TerminalCardCO execute(TerminalCardSaveCmd cmd) {
        validate(cmd);
        if (terminalBindingService.findTerminalByNo(cmd.getTerminalNo()) != null) {
            throw new BizException("Terminal card no already exists");
        }
        CorpInfoSnapshotDO corpInfo = terminalBindingService.resolveCorpInfo(cmd.getCorpinfoId(),
                cmd.getCorpinfoName(), cmd.getOrgCode(), cmd.getOrgName());
        TerminalE terminal = new TerminalE();
        terminal.setTerminalNo(cmd.getTerminalNo().trim());
        terminal.setTerminalName(firstText(cmd.getTerminalName(), cmd.getTerminalNo()));
        terminal.setTerminalModel(cmd.getTerminalModel());
        terminal.setDeviceType(DEVICE_TYPE_CARD);
        terminal.setSimCardNo(cmd.getSimCardNo());
        terminal.setCheckCode(cmd.getCheckCode());
        terminal.setCorpinfoId(corpInfo == null ? cmd.getCorpinfoId() : corpInfo.getCorpinfoId());
        terminal.setCorpinfoName(firstText(cmd.getCorpinfoName(),
                corpInfo == null ? null : corpInfo.getCorpName(), cmd.getOrgName()));
        terminal.setOrgCode(firstText(cmd.getOrgCode(), corpInfo == null ? null : corpInfo.getCreditCode()));
        terminal.setOrgName(firstText(cmd.getOrgName(), terminal.getCorpinfoName()));
        terminal.setEnable(cmd.getEnable() == null ? 1 : cmd.getEnable());
        terminal.setSyncStatus(FindsPendingSyncService.STATUS_WAIT);
        terminal.setSyncToken(FindsSyncToken.next());
        terminal.setRemarks(cmd.getRemarks());
        terminalBindingService.addTerminal(terminal);

        TerminalPersonSnapshotDO person = terminalBindingService.resolvePerson(cmd.getBindIdCardNo(),
                cmd.getStaffNo(), cmd.getStaffName());
        if (person != null) {
            terminalBindingService.upsertActiveBinding(terminal, person, true, LocalDateTime.now());
        }
        findsPendingSyncService.scheduleTerminal(terminal.getId());
        return terminalCardQueryExe.queryCardById(terminal.getId());
    }

    private void validate(TerminalCardSaveCmd cmd) {
        if (cmd == null || !StringUtils.hasText(cmd.getTerminalNo())) {
            throw new BizException("Terminal card no is required");
        }
    }

    private String firstText(String... values) {
        for (String value : values) {
            if (StringUtils.hasText(value)) {
                return value;
            }
        }
        return null;
    }
}
