package com.zcloud.primeport.command;

import com.alibaba.cola.dto.SingleResponse;
import com.jjb.saas.framework.auth.model.SSOUser;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.gbscommon.todolistmq.TodoListEventPusherUtil;
import com.zcloud.gbscommon.todolistmq.event.TodoListAddEvent;
import com.zcloud.gbscommon.zclouduser.facade.ZcloudUserFacade;
import com.zcloud.gbscommon.zclouduser.response.ZcloudUserCo;
import com.zcloud.primeport.domain.config.MessageTemplateConfig;
import com.zcloud.primeport.domain.gateway.ClosedAreaPersonApplyGateway;
import com.zcloud.primeport.domain.model.ClosedAreaPersonApplyE;
import com.zcloud.primeport.dto.ClosedAreaPersonApplyAddCmd;
import com.alibaba.cola.exception.BizException;
import jodd.util.Base64;
import lombok.AllArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;


/**
 * web-app
 *
 * @Author dearLin
 * @Date 2026-03-19 13:59:15
 */
@Component
@AllArgsConstructor
public class ClosedAreaPersonApplyAddExe {
    private final ClosedAreaPersonApplyGateway closedAreaPersonApplyGateway;
    private final MessageNoticeExe messageNoticeExe;
    private final MessageTemplateConfig messageTemplateConfig;

    @DubboReference
    private ZcloudUserFacade zcloudUserFacade;

    @Transactional(rollbackFor = Exception.class)
    public Long execute(ClosedAreaPersonApplyAddCmd cmd) {
        ClosedAreaPersonApplyE closedAreaPersonApplyE = new ClosedAreaPersonApplyE();
        BeanUtils.copyProperties(cmd, closedAreaPersonApplyE);
        closedAreaPersonApplyE.setAuditFlag(1);
        if (!cmd.getPersonBelongType().equals(4)) {
            SingleResponse<ZcloudUserCo> infoByUserId = zcloudUserFacade.getInfoByUserId(AuthContext.getUserId());
            ZcloudUserCo userData = infoByUserId.getData();
            SSOUser currentUser = AuthContext.getCurrentUser();
            closedAreaPersonApplyE.setApplyPersonCorpId(currentUser.getTenantId());
            closedAreaPersonApplyE.setApplyPersonCorpName(currentUser.getTenantName());
            closedAreaPersonApplyE.setApplyPersonDepartmentId(currentUser.getOrgId());
            closedAreaPersonApplyE.setApplyPersonDepartmentName(currentUser.getOrgName());
            closedAreaPersonApplyE.setApplyPersonUserId(currentUser.getUserId());
            closedAreaPersonApplyE.setApplyPersonUserName(userData.getName());
            closedAreaPersonApplyE.setUserPhone(userData.getPhone());
            closedAreaPersonApplyE.setUserCard(userData.getUserIdCard());
            closedAreaPersonApplyE.setUserFaceUrl(userData.getUserAvatarUrl());
        }else {
//            closedAreaPersonApplyE.setUserCard(Base64.encodeToString(cmd.getUserCard()));
        }
        boolean res = closedAreaPersonApplyGateway.add(closedAreaPersonApplyE);
        if (!res) {
            throw new BizException("保存失败");
        }

        // 发送 消息通知 和 待办
        // 发送消息通知 和 待办
        if (cmd.getAuditPersonUserId() != null) {
            // 发送消息提醒
            Map <String, Object> sendParams = new HashMap <>();
            messageNoticeExe.sendMessageNotice(cmd.getAuditPersonUserId(), messageTemplateConfig.getClosedAreaApplyReceiveAudit(), sendParams);
            // 发送待办
            messageNoticeExe.sendMessageTodoEvent(cmd.getAuditPersonUserId(), "您有一条封闭区域审核，请及时进行审核。", "待审核", closedAreaPersonApplyE.getId(), closedAreaPersonApplyE.getId(), 1, 1);
        }

        return closedAreaPersonApplyE.getId();
    }

    public static void main(String[] args) {
        System.out.println(Base64.encodeToString("130323199504189999"));
        System.out.println(Base64.decodeToString("MTMwMzIzMTk5NTA0MTg5OTk5"));
        System.out.println(Base64.decodeToString("MTMwMzU0MTk5NzA0MDQyMDEw"));
    }
}

