
package tech.easyflow.ai.service.impl;
import com.alibaba.fastjson2.JSON;
import tech.easyflow.ai.entity.Workflow;
import tech.easyflow.ai.enums.PublishStatus;
import tech.easyflow.ai.mapper.WorkflowMapper;
import tech.easyflow.ai.service.WorkflowService;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import tech.easyflow.ai.utils.RegexUtils;
import com.mybatisflex.core.query.QueryWrapper;
import tech.easyflow.common.web.exceptions.BusinessException;
import tech.easyflow.ai.utils.CustomBeanUtils;

import java.math.BigInteger;
import java.util.Map;

/**
 * 服务层实现。
 *
 * @author michael
 * @since 2024-08-23
 */
@Service
public class WorkflowServiceImpl extends ServiceImpl<WorkflowMapper, Workflow> implements WorkflowService {

    /**
     * 根据别名或 id 查询详情
     */
    @Override
    public Workflow getDetail(String idOrAlias) {

        Workflow workflow = null;

        if (idOrAlias.matches(RegexUtils.ALL_NUMBER)) {
            workflow = getById(idOrAlias);
            if (workflow == null) {
                workflow = getByAlias(idOrAlias);
            }
        }

        if (workflow == null) {
            workflow = getByAlias(idOrAlias);
        }

        return workflow;
    }

    @Override
    public Workflow getByAlias(String idOrAlias) {

        QueryWrapper queryWrapper = QueryWrapper.create();
        queryWrapper.eq("alias",idOrAlias);

        return getOne(queryWrapper);

    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Workflow getPublishedDetail(String idOrAlias) {
        return toPublishedView(getDetail(idOrAlias));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Workflow getPublishedById(BigInteger id) {
        return toPublishedView(getById(id));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Workflow toPublishedView(Workflow workflow) {
        if (workflow == null) {
            return null;
        }
        Map<String, Object> snapshot = workflow.getPublishedSnapshotJson();
        if (snapshot == null || snapshot.isEmpty()) {
            return workflow;
        }
        Workflow published = JSON.parseObject(JSON.toJSONString(snapshot), Workflow.class);
        if (published == null) {
            return workflow;
        }
        published.setId(workflow.getId());
        published.setTenantId(workflow.getTenantId());
        published.setCategoryId(workflow.getCategoryId());
        published.setDeptId(workflow.getDeptId());
        published.setCreated(workflow.getCreated());
        published.setCreatedBy(workflow.getCreatedBy());
        published.setModified(workflow.getModified());
        published.setModifiedBy(workflow.getModifiedBy());
        published.setPublishStatus(workflow.getPublishStatus());
        published.setCurrentApprovalInstanceId(workflow.getCurrentApprovalInstanceId());
        published.setPublishedSnapshotJson(workflow.getPublishedSnapshotJson());
        published.setPublishedAt(workflow.getPublishedAt());
        published.setPublishedBy(workflow.getPublishedBy());
        if (published.getPublishStatus() == null) {
            published.setPublishStatus(PublishStatus.DRAFT.getCode());
        }
        return published;
    }


    @Override
    public boolean updateById(Workflow entity, boolean ignoreNulls) {
        Workflow workFlow = getById(entity.getId());
        if (workFlow == null) {
            throw new BusinessException("工作流不存在");
        }

        CustomBeanUtils.copyPropertiesIgnoreNull(entity,workFlow);

        if ("".equals(workFlow.getAlias())){
            workFlow.setAlias(null);
        }


        return super.updateById(workFlow,false);
    }


}
