package cn.iocoder.yudao.module.pms.service.iotmaterialrequisitiondetail;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaterialrequisitiondetail.vo.IotMaterialRequisitionDetailPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaterialrequisitiondetail.vo.IotMaterialRequisitionDetailSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmaterialrequisitiondetail.IotMaterialRequisitionDetailDO;
import cn.iocoder.yudao.module.pms.dal.mysql.iotmaterialrequisitiondetail.IotMaterialRequisitionDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstant.IOT_MATERIAL_REQUISITION_DETAIL_NOT_EXISTS;

/**
 * PMS 物料领用明细 Service 实现类
 *
 * @author ruiqi
 */
@Service
@Validated
public class IotMaterialRequisitionDetailServiceImpl implements IotMaterialRequisitionDetailService {

    @Resource
    private IotMaterialRequisitionDetailMapper iotMaterialRequisitionDetailMapper;

    @Override
    public Long createIotMaterialRequisitionDetail(IotMaterialRequisitionDetailSaveReqVO createReqVO) {
        // 插入
        IotMaterialRequisitionDetailDO iotMaterialRequisitionDetail = BeanUtils.toBean(createReqVO, IotMaterialRequisitionDetailDO.class);
        iotMaterialRequisitionDetailMapper.insert(iotMaterialRequisitionDetail);
        // 返回
        return iotMaterialRequisitionDetail.getId();
    }

    @Override
    public void updateIotMaterialRequisitionDetail(IotMaterialRequisitionDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateIotMaterialRequisitionDetailExists(updateReqVO.getId());
        // 更新
        IotMaterialRequisitionDetailDO updateObj = BeanUtils.toBean(updateReqVO, IotMaterialRequisitionDetailDO.class);
        iotMaterialRequisitionDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotMaterialRequisitionDetail(Long id) {
        // 校验存在
        validateIotMaterialRequisitionDetailExists(id);
        // 删除
        iotMaterialRequisitionDetailMapper.deleteById(id);
    }

    private void validateIotMaterialRequisitionDetailExists(Long id) {
        if (iotMaterialRequisitionDetailMapper.selectById(id) == null) {
            throw exception(IOT_MATERIAL_REQUISITION_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public IotMaterialRequisitionDetailDO getIotMaterialRequisitionDetail(Long id) {
        return iotMaterialRequisitionDetailMapper.selectById(id);
    }

    @Override
    public PageResult<IotMaterialRequisitionDetailDO> getIotMaterialRequisitionDetailPage(IotMaterialRequisitionDetailPageReqVO pageReqVO) {
        return iotMaterialRequisitionDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public List<IotMaterialRequisitionDetailDO> getIotMaterialRequisitionDetailList(IotMaterialRequisitionDetailPageReqVO pageReqVO) {
        return iotMaterialRequisitionDetailMapper.selectList(pageReqVO);
    }

}