package com.ruoyi.project.business.service.flow.impl;

import cn.hutool.core.thread.ThreadUtil;
import com.ruoyi.project.business.domain.flow.FlowAuditParam;
import com.ruoyi.project.business.enums.AuditFlowEnum;
import com.ruoyi.project.business.enums.ModuleEnum;
import com.ruoyi.project.business.service.flow.IFlowBusinessService;
import com.ruoyi.project.business.service.flow.IFlowService;
import com.ruoyi.project.business.service.flow.IFlowUserPermService;
import com.ruoyi.project.business.service.quotation.IQuoteApprovalService;
import com.ruoyi.project.common.service.ISseEmitterService;
import com.ruoyi.project.system.service.ISysUserService;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class FlowBusinessServiceImpl implements IFlowBusinessService {

    @Resource
    private IFlowUserPermService flowUserPermService;

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private IFlowService flowService;

    @Resource
    private ISseEmitterService sseEmitterService;

    @Resource
    private IQuoteApprovalService quoteApprovalService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeBusinessStatus(FlowAuditParam auditParam) {
        AuditFlowEnum auditFlow = AuditFlowEnum.from(auditParam.getAuditFlow().intValue());
//        Long businessId = auditParam.getBusinessId();
//        Flow flow = flowService.getFlowMap(Lists.newArrayList(businessId), auditFlow).get(businessId);
//        auditParam.setCurrentStatus(flow.getStatus());

        switch (auditFlow) {
            // 1.
            case QUOTATION:
                quoteApprovalService.changeQuotationStatus(auditParam);
                break;
            case BARGAIN:
                quoteApprovalService.changeBargainStatus(auditParam);
                break;

            default:
                break;
        }

        // 消息提醒逻辑： 1. 每次触发下一节点审批时通知审批人 2. 每次审批结束后（所有流程通过|被驳回）通知提交人
        //        SysUser promoter = Objects.nonNull(flow) ? sysUserService.selectUserById(flow.getPromoter()) : SecurityUtils.getLoginUser().getUser();
        //        String msg = promoter.getUserName() + "提交了一条" + auditFlow.getName() + ", 请及时审批";
        //        List<Long> nextAuditors = auditParam.getNextAuditUsers();
        //        // 1.通知审批人
        //        if (CollectionUtils.isNotEmpty(nextAuditors)) {
        //            sendAuditMessage(auditFlow.getModule(), nextAuditors, msg + detail);
        //            nextAuditors.add(SecurityUtils.getUserId()); // 防止直接提交导致申请人看不到
        //            flowUserPermService.addPerm(nextAuditors, auditFlow, businessId);
        //        }
        //
        //        boolean isPass = AuditStatusEnum.PASS.getValue().equals(auditParam.getStatus());
        //        boolean isReject = AuditStatusEnum.REJECT.getValue().equals(auditParam.getStatus());
        //
        //        // 2. 审批结束（通过或驳回）通知提交人
        //        if (auditParam.isFinish() && (isPass || isReject)) {
        //            String status = isPass ? "已审批通过" : "已被驳回";
        //            sendAuditMessage(auditFlow.getModule(), Lists.newArrayList(auditParam.getPromoter()), "您提交的" + auditFlow.getName() + status + detail);
        //        }
        //
        //        // 3. 清除权限（非通过状态要留下提交人权限）
        //        if (auditParam.isFinish()) {
        //            flowUserPermService.removePerm(auditFlow, businessId);
        //            if (!isPass) {
        //                flowUserPermService.addPerm(Lists.newArrayList(auditParam.getPromoter()), auditFlow, businessId);
        //            }
        //        }
    }

    void sendAuditMessage(ModuleEnum module, List<Long> userIds, String content) {
        switch (module) {
            //todo
            case COMMON:
                break;

        }
        //@date 2024/3/5 11:22 @author jubs 😀 @description: 将发消息另起线程跑
        Runnable sendMessageTask = () -> sseEmitterService.sendMessageToUserIdList(userIds, true);
        ThreadUtil.execAsync(sendMessageTask);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteAttachment(Long businessId, AuditFlowEnum auditFlowEnum) {
        switch (auditFlowEnum) {
            //todo
        }
        flowUserPermService.removePerm(auditFlowEnum, businessId);
        return 1;
    }
}
