package com.yunzhupaas.flowable.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.dynamic.datasource.tx.DsPropagation;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunzhupaas.base.service.SuperServiceImpl;
import com.yunzhupaas.flowable.entity.TriggerRecordEntity;
import com.yunzhupaas.flowable.entity.TriggerTaskEntity;
import com.yunzhupaas.flowable.enums.TriggerRecordEnum;
import com.yunzhupaas.flowable.mapper.TriggerRecordMapper;
import com.yunzhupaas.flowable.model.flowable.FlowableTaskModel;
import com.yunzhupaas.flowable.model.templatenode.nodejson.NodeModel;
import com.yunzhupaas.flowable.model.util.FlowNature;
import com.yunzhupaas.flowable.service.TriggerRecordService;
import com.yunzhupaas.util.RandomUtil;
import com.yunzhupaas.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 类的描述
 *
 * @author YUNZHUPAASYUNZHUPAAS开发组
 * @version 5.0.x
 * @since 2024/9/10 17:18
 */
@Service
public class TriggerRecordServiceImpl extends SuperServiceImpl<TriggerRecordMapper, TriggerRecordEntity>
        implements TriggerRecordService {

    @Override
    public List<TriggerRecordEntity> getList(String triggerTaskId) {
        if (StringUtil.isBlank(triggerTaskId)) {
            return new ArrayList<>();
        }
        QueryWrapper<TriggerRecordEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TriggerRecordEntity::getTriggerId, triggerTaskId)
                .orderByAsc(TriggerRecordEntity::getStartTime);
        return this.list(queryWrapper);
    }

    @Override
    public List<TriggerRecordEntity> getListByTaskId(String taskId) {
        if (StringUtil.isBlank(taskId)) {
            return new ArrayList<>();
        }
        QueryWrapper<TriggerRecordEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TriggerRecordEntity::getTaskId, taskId).orderByDesc(TriggerRecordEntity::getStartTime);
        return this.list(queryWrapper);
    }

    @Override
    // @DSTransactional(propagation = DsPropagation.NOT_SUPPORTED)
    public void create(TriggerRecordEntity entity) {
        if (StringUtil.isEmpty(entity.getId())) {
            entity.setId(RandomUtil.uuId());
        }
        entity.setCreatorTime(new Date());
        this.save(entity);
    }

    @Override
    // @DSTransactional(propagation = DsPropagation.NOT_SUPPORTED)
    public void create(TriggerTaskEntity triggerTask, NodeModel nodeModel, FlowableTaskModel taskModel) {
        TriggerRecordEntity entity = new TriggerRecordEntity();
        entity.setId(RandomUtil.uuId());
        entity.setStatus(TriggerRecordEnum.PASSED.getCode());
        Date date = new Date();
        entity.setStartTime(date);
        entity.setEndTime(date);

        entity.setNodeCode(nodeModel.getNodeId());
        entity.setNodeName(nodeModel.getNodeName());
        entity.setNodeId(taskModel.getTaskId());

        entity.setTriggerId(triggerTask.getId());
        if (StringUtil.isNotEmpty(triggerTask.getTaskId())) {
            entity.setTaskId(triggerTask.getTaskId());
        }

        this.save(entity);
    }

    @Override
    // @DSTransactional(propagation = DsPropagation.NOT_SUPPORTED)
    public void createStart(String triggerId) {
        TriggerRecordEntity entity = new TriggerRecordEntity();
        entity.setId(RandomUtil.uuId());
        entity.setStatus(TriggerRecordEnum.PASSED.getCode());
        Date date = new Date();
        entity.setStartTime(date);
        entity.setEndTime(date);
        entity.setTriggerId(triggerId);
        entity.setNodeCode(FlowNature.START_CODE);
        entity.setNodeName(FlowNature.START_NAME);
        this.save(entity);
    }

    @Override
    public void createEnd(String triggerId) {
        TriggerRecordEntity entity = new TriggerRecordEntity();
        entity.setId(RandomUtil.uuId());
        entity.setStatus(TriggerRecordEnum.PASSED.getCode());
        Date date = new Date();
        entity.setStartTime(date);
        entity.setEndTime(date);
        entity.setTriggerId(triggerId);
        entity.setNodeCode(FlowNature.END_CODE);
        entity.setNodeName(FlowNature.END_NAME);
        this.save(entity);
    }

    @Override
    public void delete(List<String> triggerTaskIds) {
        if (CollectionUtil.isNotEmpty(triggerTaskIds)) {
            QueryWrapper<TriggerRecordEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().in(TriggerRecordEntity::getTriggerId, triggerTaskIds);
            this.remove(queryWrapper);
        }
    }
}
