package com.tuoheng.admin.service.third.dsp;

import cn.hutool.core.collection.CollectionUtil;
import com.tuoheng.admin.entity.Accident;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.entity.InspectionFile;
import com.tuoheng.admin.entity.QuestionType;
import com.tuoheng.admin.service.workorder.generate.GenerateWorkorderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Slf4j
@Service
public class AutoGenerateWorkorderService {

    @Autowired
    private GenerateWorkorderService generateWorkorderService;

    /**
     * 自动生成工单
     *
     * @param inspectionFileList
     * @return
     */
    @Transactional
    public void generate(Inspection inspection, Map<String, QuestionType> questionTypeMap, List<InspectionFile> inspectionFileList) {
        log.info("调用自动生成工单....");
        List<InspectionFile> inspectionFileListTmp = new ArrayList<>();
        for (InspectionFile inspectionFile : inspectionFileList) {
            QuestionType questionType = questionTypeMap.get(inspectionFile.getQuestionCode());
            if (0 == questionType.getIsGenerateWorkorder()) {
                // 该问题不用自动生成工单
                continue;
            }
            inspectionFileListTmp.add(inspectionFile);
        }

        if (CollectionUtil.isEmpty(inspectionFileListTmp)) {
            return;
        }
        generateWorkorderService.generateByAuto(inspection, inspectionFileListTmp);
    }

}
