package com.tuoheng.admin.service.inspection.query.handle;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.entity.Dept;
import com.tuoheng.admin.entity.InspectionFile;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.InspectionFileStatusEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.InspectionFileMapper;
import com.tuoheng.admin.vo.inspection.InspectionVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service("problemCountHandle")
public class ProblemCountHandle implements GenerateInspectionFieldHander {

    private GenerateInspectionFieldHander handler;

    @Autowired
    private InspectionFileMapper inspectionFileMapper;

    @Override
    public void setNextHandler(GenerateInspectionFieldHander handler) {
        this.handler = handler;
    }

    @Override
    public void handler(User user, Dept dept, InspectionVo inspectionVo) {
//        log.info("处理问题数字段");
        List<InspectionFile> inspectionFileList = inspectionFileMapper.selectList(new LambdaQueryWrapper<InspectionFile>()
                .eq(InspectionFile::getInspectionId, inspectionVo.getId())
                .eq(InspectionFile::getMark, MarkEnum.VALID.getCode()));
        Integer confirmedProblemCount = 0;
        Integer discoverProblemCount = 0;
        if (CollectionUtil.isNotEmpty(inspectionFileList)) {
            discoverProblemCount = inspectionFileList.size();
            for (InspectionFile inspectionFile : inspectionFileList) {
                if (InspectionFileStatusEnum.CONFIRMED.getCode() == inspectionFile.getStatus()
                    || InspectionFileStatusEnum.GENERATE_ORDER.getCode() == inspectionFile.getStatus()
                    || InspectionFileStatusEnum.PROCESSED.getCode() == inspectionFile.getStatus()) {
                    confirmedProblemCount++;
                }
            }
        }
        inspectionVo.setConfirmedProblemCount(confirmedProblemCount);
        inspectionVo.setDiscoverProblemCount(discoverProblemCount);

        if (null != handler) {
            handler.handler(user, dept, inspectionVo);
            return;
        }
    }

}
