package com.foreverwin.mesnac.meapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.foreverwin.mesnac.meapi.mapper.SfcBomMapper;
import com.foreverwin.mesnac.meapi.model.SfcBom;
import com.foreverwin.mesnac.meapi.service.SfcBomService;
import com.foreverwin.mesnac.meapi.util.StringUtils;
import com.foreverwin.modular.core.exception.BaseException;
import com.foreverwin.modular.core.util.FrontPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ervin Chen
 * @since 2020-10-19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SfcBomServiceImpl extends ServiceImpl<SfcBomMapper, SfcBom> implements SfcBomService {


    @Autowired
    private SfcBomMapper sfcBomMapper;

    @Override
    public IPage<SfcBom> selectPage(FrontPage<SfcBom> frontPage, SfcBom sfcBom) {
        QueryWrapper<SfcBom> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(sfcBom);
        return super.page(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<SfcBom> selectList(SfcBom sfcBom) {
        QueryWrapper<SfcBom> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(sfcBom);
        return super.list(queryWrapper);
    }

    @Override
    public String selectBomBO(String sfcBo) {
        LambdaQueryWrapper<SfcBom> lambdaQueryWrapper =new LambdaQueryWrapper<>() ;
        lambdaQueryWrapper.eq(SfcBom::getSfcBo,sfcBo);
        List<SfcBom> list = list(lambdaQueryWrapper);
        if(list.isEmpty()){
            throw new BaseException("未找到sfc"+ StringUtils.trimHandle(sfcBo) +"的物料清单");
        }
        return list.get(0).getBomBo();
    }
}