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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.material.entity.BuMaterialInfo;
import org.jeecg.modules.material.entity.BuMaterialSheet;
import org.jeecg.modules.material.mapper.BuMaterialInfoMapper;
import org.jeecg.modules.material.mapper.BuMaterialSheetMapper;
import org.jeecg.modules.material.service.IBuMaterialInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 物料基础
 * @Author: jeecg-boot
 * @Date:   2025-01-15
 * @Version: V1.0
 */
@Service
public class BuMaterialInfoServiceImpl extends ServiceImpl<BuMaterialInfoMapper, BuMaterialInfo> implements IBuMaterialInfoService {


    @Autowired
    private BuMaterialSheetMapper materialSheetMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public boolean save(BuMaterialInfo buMaterialInfo) {

        if (super.save(buMaterialInfo)) {
            if (materialSheetMapper.selectCount(new LambdaQueryWrapper<BuMaterialSheet>().eq(BuMaterialSheet::getMaterialId, buMaterialInfo.getId())) > 0) {
                return true;
            } else {
                // 如果物料台账没有相应数据则新增一份基础数据
                BuMaterialSheet buMaterialSheet = new BuMaterialSheet();
                buMaterialSheet.setMaterialId(buMaterialInfo.getId());
                buMaterialSheet.setModel(buMaterialInfo.getSpecifaction());
                buMaterialSheet.setQuantity(1.0);
                return materialSheetMapper.insert(buMaterialSheet) > 0;
            }
        } else
            return false;
    }

}
