package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.domain.BMainTwoRandomTask;
import cn.cecep.talroad.domain.BMainTwoRandomTaskStep;
import cn.cecep.talroad.mapper.BMainTwoRandomTaskMapper;
import cn.cecep.talroad.mapper.BMainTwoRandomTaskStepMapper;
import cn.cecep.talroad.service.IBMainTwoRandomTaskService;
import cn.cecep.talroad.vo.BMainTwoRandomTaskVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 移动执法信息-双随机任务Service业务层处理
 *
 * @author szhpt
 * @date 2023-03-29*/
@Service
public class BMainTwoRandomTaskServiceImpl extends ServiceImpl<BMainTwoRandomTaskMapper,BMainTwoRandomTask> implements IBMainTwoRandomTaskService
{
    @Autowired
    private BMainTwoRandomTaskStepMapper twoRandomTaskStepMapper;

    @Override
    public BMainTwoRandomTaskVo getInfo(String id) {
        BMainTwoRandomTask byId = getById(id);
        BMainTwoRandomTaskVo vo = new BMainTwoRandomTaskVo();
        BeanUtils.copyProperties(byId, vo);

        List<BMainTwoRandomTaskStep> steps = twoRandomTaskStepMapper.selectList(new QueryWrapper<BMainTwoRandomTaskStep>()
                .eq("two_random_task_id", id).orderByAsc("operate_time"));
        vo.setSteps(steps);

        // 步骤号从0开始（表示一步都没完成）
        int stepNum = 0;

        for (int i = 0; i < steps.size(); i++) {
            BMainTwoRandomTaskStep step = steps.get(i);

            // 若操作时间不为空，表示该步骤已完成
            if (StringUtils.isNotEmpty(step.getOperateTime())) {
                stepNum = i + 1; // 步骤号=下标加1
            }
        }
        vo.setStepNum(stepNum);
        return vo;
    }
}
