package com.sw.mes.itemclean.clean.service.impl;/*
 * @Auther:赵亚运
 * @Date:2023/1/3
 * @Description:
 * @VERSON:1.0
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sw.mes.itemclean.clean.domain.MaterialReworking;
import com.sw.mes.itemclean.clean.domain.MaterialRewrkingDetail;
import com.sw.mes.itemclean.clean.mapper.MaterialReworkingDetailMapper;
import com.sw.mes.itemclean.clean.mapper.MaterialReworkingMapper;
import com.sw.mes.itemclean.clean.service.MaterialReworkingService;
import com.sw.mes.itemclean.clean.vo.MaterialReworkingVo;

import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;




@Service
public class MaterialReworkingServiceImpl extends ServiceImpl<MaterialReworkingMapper, MaterialReworking> implements MaterialReworkingService {

    @Autowired
    private MaterialReworkingMapper materialReworkingMapper;

    @Autowired
    private MaterialReworkingDetailMapper materialReworkingDetailMapper;

    @Autowired
    ModelMapper modelMapper;

    @Override
    public MaterialReworkingVo selectReworkingStorageSign(String materialBoxNo) {
        LambdaQueryWrapper<MaterialReworking> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MaterialReworking::getMaterialBoxNo, materialBoxNo);
        MaterialReworking materialReworking = materialReworkingMapper.selectOne(queryWrapper);
        if(materialReworking==null) {
            return null;
        }
        MaterialReworkingVo materialReworkingVo = toLazyDto(materialReworking);
        materialReworkingVo.setReworkWeight(materialReworking.getWeight());

        LambdaQueryWrapper<MaterialRewrkingDetail> queryDetailWrapper = new LambdaQueryWrapper<>();
        queryDetailWrapper.eq(MaterialRewrkingDetail::getMaterialBoxNo, materialBoxNo);
        List<MaterialRewrkingDetail> materialRewrkingDetails = materialReworkingDetailMapper.selectList(queryDetailWrapper);
        materialReworkingVo.setMaterialRewrkingDetails(materialRewrkingDetails);
        return materialReworkingVo;
    }


    public MaterialReworkingVo toLazyDto(MaterialReworking entity) {
        return modelMapper.map(entity, MaterialReworkingVo.class);
    }
}
