package org.jeecg.modules.database.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.database.entity.Equipment;
import org.jeecg.modules.database.entity.ExperimentDeviationEquipment;
import org.jeecg.modules.database.mapper.ExperimentDeviationEquipmentMapper;
import org.jeecg.modules.database.service.IEquipmentService;
import org.jeecg.modules.database.service.IExperimentDeviationEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 试验加偏设备信息
 * @Author: jeecg-boot
 * @Date: 2024-12-05
 * @Version: V1.0
 */
@Service
public class ExperimentDeviationEquipmentServiceImpl extends ServiceImpl<ExperimentDeviationEquipmentMapper, ExperimentDeviationEquipment>
        implements IExperimentDeviationEquipmentService {
    @Autowired
    private IEquipmentService equipmentService;

    @Override
    public List<ExperimentDeviationEquipment> getByExperimentId(String experimentId) {
        List<ExperimentDeviationEquipment> list = list(
                new LambdaQueryWrapper<ExperimentDeviationEquipment>().eq(ExperimentDeviationEquipment::getExperimentId, experimentId));
        list.forEach(this::assembleDetails);
        return list;
    }

    @Override
    public ExperimentDeviationEquipment getById(Serializable id) {
        ExperimentDeviationEquipment byId = super.getById(id);
        this.assembleDetails(byId);
        return byId;
    }

    private void assembleDetails(ExperimentDeviationEquipment experimentDeviationEquipment) {
        if (ObjUtil.isNull(experimentDeviationEquipment)) {
            return;
        }
        Equipment one = equipmentService.getByManagementNo(experimentDeviationEquipment.getEquipmentNo());
        if (Objects.nonNull(one)) {
            experimentDeviationEquipment.setEquipmentName(one.getName());
        }
    }
}
