package cn.com.ctop.crawler.modules.appium.service.impl;

import cn.com.ctop.crawler.modules.appium.entity.AppiumTaskItem;
import cn.com.ctop.crawler.modules.appium.mapper.AppiumTaskItemMapper;
import cn.com.ctop.crawler.modules.appium.service.IAppiumTaskItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 任务内容表
 * @author jeecg-boot
 * @date 2019-10-31
 * @version V1.0
 */
@Service
public class AppiumTaskItemServiceImpl extends ServiceImpl<AppiumTaskItemMapper, AppiumTaskItem> implements IAppiumTaskItemService {
    @Override
    public List<AppiumTaskItem> initTaskItemList(Long taskId, Integer parentId) {
        QueryWrapper<AppiumTaskItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("task_id", taskId);
        if (parentId == null) {
            queryWrapper.isNull("parent_id");
        } else {
            queryWrapper.eq("parent_id", parentId);
        }

        queryWrapper.orderByAsc("seq");
        List<AppiumTaskItem> list = this.list(queryWrapper);
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                AppiumTaskItem taskItem = list.get(i);
                taskItem.setSubList(initTaskItemList(taskItem.getTaskId(), taskItem.getId()));
                list.set(i, taskItem);
            }
        }
        return list;
    }
}
