package com.dayou.aspect;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dayou.common.BaseEntity;
import com.dayou.common.RestResponse;
import com.dayou.entity.WorkFlowNodeInstance;
import com.dayou.entity.WorkTaskRecord;
import com.dayou.enums.workflow.NodeStateEnum;
import com.dayou.exception.ErrorCode;
import com.dayou.service.workflow.IWorkFlowNodeInstanceService;
import com.dayou.service.workflow.IWorkTaskRecordService;
import com.dayou.utils.LoginContext;
import com.dayou.workflow.annotation.FinishTask;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.time.LocalDateTime;

/**
 * 类说明： 节点任务完成切面类
 *
 * @author: wucl
 * @since: 2023/11/1
 * created with IntelliJ IDEA.
 */
@Slf4j
@Component
@Aspect
public class FinishTaskAOP {

    @Autowired
    private IWorkTaskRecordService workTaskRecordService;

    @Autowired
    private IWorkFlowNodeInstanceService workFlowNodeInstanceService;


    @Around("@annotation(finishTask)")
    public Object finishTaskAround(ProceedingJoinPoint joinPoint, FinishTask finishTask) throws Throwable {
            Object arg = joinPoint.getArgs()[0];
            Field field = arg.getClass().getDeclaredField("recordId");
            field.setAccessible(true);
            Long recordId = (Long) field.get(arg);
            WorkTaskRecord record = workTaskRecordService.getById(recordId);
            //校验当前节点状态是否已完成 若已完成则不进行任务
            String nodeState = workFlowNodeInstanceService.getOne(new LambdaQueryWrapper<WorkFlowNodeInstance>().select(WorkFlowNodeInstance::getState)
                .eq(BaseEntity::getId, record.getInstanceId()).eq(BaseEntity::getDeleted, Boolean.FALSE)).getState();
            if (NodeStateEnum.PENDING.name().equals(nodeState)){
                //校验了当前处理人是否是任务设置的处理人
                if (record!=null && record.getHandlerId()!=null && record.getHandlerId().equals(LoginContext.getCurrentUserId())){
                    RestResponse result = (RestResponse) joinPoint.proceed(joinPoint.getArgs());
                    if ((Boolean) result.getData()){
                        workTaskRecordService.update(new LambdaUpdateWrapper<WorkTaskRecord>().set(WorkTaskRecord::getFinished,Boolean.TRUE)
                                .set(WorkTaskRecord::getFinishTime,LocalDateTime.now()).eq(BaseEntity::getId,recordId));
                    }
                    return result;
                }else{
                    ErrorCode.throwBusinessException(ErrorCode.PERMISSION_ERROR);
                    return null;
                }
            }else {
                ErrorCode.throwBusinessException(ErrorCode.NODE_STATE_EXCEPTION);
                return null;
            }
    }
}
