package io.hmit.modules.activiti.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.hmit.common.page.PageData;
import io.hmit.common.service.impl.BaseServiceImpl;
import io.hmit.common.utils.ConvertUtils;
import io.hmit.modules.activiti.dao.ProcessActivityDao;
import io.hmit.modules.activiti.dto.HistoryDetailDTO;
import io.hmit.modules.activiti.dto.ProcessActivityDTO;
import io.hmit.modules.activiti.entity.HistoryDetailEntity;
import io.hmit.modules.activiti.entity.ProcessActivityEntity;
import io.hmit.modules.activiti.service.ActivitiService;
import io.hmit.modules.security.user.SecurityUser;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 工作流
 *
 * @author Jone
 */
@Service
public class ActivitiServiceImpl extends BaseServiceImpl<ProcessActivityDao, ProcessActivityEntity> implements ActivitiService {

    @Override
    public PageData<ProcessActivityDTO> getMyProcessInstancePage(Map<String, Object> params) {

        params.put("userId", SecurityUser.getUserId().toString());
        IPage<ProcessActivityEntity> page = getPage(params, null, false);
        List<ProcessActivityEntity> list = baseDao.getMyProcessInstancePage(params);
        return getPageData(list, page.getTotal(), ProcessActivityDTO.class);
    }

    @Override
    public List<HistoryDetailDTO> getTaskHandleDetailInfo(String processInstanceId) {

        List<HistoryDetailEntity> listEntity = baseDao.getTaskHandleDetailInfo(processInstanceId);
        for (HistoryDetailEntity entity : listEntity) {
            if (entity.getEndTime() != null && entity.getStartTime() != null) {
                long diff = entity.getEndTime().getTime() - entity.getStartTime().getTime();
                entity.setDurationInSeconds(diff / 1000);
            }
        }
        return ConvertUtils.sourceToTarget(listEntity, HistoryDetailDTO.class);
    }
}
