package cn.iocoder.yudao.module.system.service.assetreceive;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.assetreceive.vo.AssetReceivePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.assetreceive.vo.AssetReceiveSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.assetreceive.AssetReceiveDO;
import cn.iocoder.yudao.module.system.dal.mysql.assetreceive.AssetReceiveMapper;
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.system.enums.ErrorCodeConstants.ASSET_RECEIVE_NOT_EXISTS;

/**
 * 资产领用 Service 实现类
 *
 * @author 符溶馨
 */
@Service
@Validated
public class AssetReceiveServiceImpl implements AssetReceiveService {

    @Resource
    private AssetReceiveMapper assetReceiveMapper;

    @Override
    public Long createAssetReceive(AssetReceiveSaveReqVO createReqVO) {
        // 插入
        AssetReceiveDO assetReceive = BeanUtils.toBean(createReqVO, AssetReceiveDO.class);
        assetReceiveMapper.insert(assetReceive);
        // 返回
        return assetReceive.getId();
    }

    @Override
    public Boolean createAssetReceives(List<AssetReceiveSaveReqVO> createReqVO) {

        List<AssetReceiveDO> assetReceives = BeanUtils.toBean(createReqVO, AssetReceiveDO.class);
        assetReceiveMapper.insertBatch(assetReceives);

        return true;
    }

    @Override
    public void updateAssetReceive(AssetReceiveSaveReqVO updateReqVO) {
        // 校验存在
        validateAssetReceiveExists(updateReqVO.getId());
        // 更新
        AssetReceiveDO updateObj = BeanUtils.toBean(updateReqVO, AssetReceiveDO.class);
        assetReceiveMapper.updateById(updateObj);
    }

    @Override
    public void deleteAssetReceive(Long id) {
        // 校验存在
        validateAssetReceiveExists(id);
        // 删除
        assetReceiveMapper.deleteById(id);
    }

    private void validateAssetReceiveExists(Long id) {
        if (assetReceiveMapper.selectById(id) == null) {
            throw exception(ASSET_RECEIVE_NOT_EXISTS);
        }
    }

    @Override
    public AssetReceiveDO getAssetReceive(Long id) {
        return assetReceiveMapper.selectById(id);
    }

    @Override
    public PageResult<AssetReceiveDO> getAssetReceivePage(AssetReceivePageReqVO pageReqVO) {
        return assetReceiveMapper.selectPage(pageReqVO);
    }

}