package cn.axzo.workflow.core.service.converter;

import cn.axzo.workflow.common.model.response.bpmn.process.HistoricProcessInstanceVO;
import org.flowable.engine.history.HistoricProcessInstance;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.mapstruct.NullValueCheckStrategy.ALWAYS;

/**
 *
 *
 * @author wangli
 * @since 2023/9/21 16:02
 */
@Mapper(
        componentModel = "spring",
        nullValueCheckStrategy = ALWAYS,
        imports = Arrays.class
)
public interface BpmnHistoricProcessInstanceConverter extends EntityConverter<HistoricProcessInstanceVO,
        HistoricProcessInstance> {

    @Mapping(target = "processInstanceId", source = "id")
    @Mapping(target = "name", source = "name")
    @Mapping(target = "businessStatus", source = "businessStatus")
    @Mapping(target = "startTime", source = "startTime")
    @Mapping(target = "endTime", source = "endTime")
    @Mapping(target = "businessKey", source = "businessKey")
    @Mapping(target = "startUserId", source = "startUserId")
    @Override
    HistoricProcessInstanceVO toVo(HistoricProcessInstance entity);

    default HistoricProcessInstanceVO toVo(HistoricProcessInstance processInstance, String category) {
        HistoricProcessInstanceVO vo = toVo(processInstance);
        vo.setCategory(category);
        return vo;
    }

    default List<HistoricProcessInstanceVO> toVos(List<HistoricProcessInstance> list, String category) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<HistoricProcessInstanceVO> vos = new ArrayList<>();
        list.forEach(i -> vos.add(toVo(i, category)));
        return vos;
    }
}
