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

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.devicemoney.vo.DeviceMoneyPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.devicemoney.vo.DeviceMoneySaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.devicemoney.DeviceMoneyDO;
import cn.iocoder.yudao.module.system.dal.mysql.devicemoney.DeviceMoneyMapper;
import cn.iocoder.yudao.module.system.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 门店设备日流水 Service 实现类
 *
 * @author 开发账号
 */
@Service
@Validated
public class DeviceMoneyServiceImpl implements DeviceMoneyService {

    @Resource
    private DeviceMoneyMapper deviceMoneyMapper;

    @Override
    public Long createDeviceMoney(DeviceMoneySaveReqVO createReqVO) {
        // 插入
        DeviceMoneyDO deviceMoney = BeanUtils.toBean(createReqVO, DeviceMoneyDO.class);
        deviceMoneyMapper.insert(deviceMoney);
        // 返回
        return deviceMoney.getId();
    }

    @Override
    public void updateDeviceMoney(DeviceMoneySaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceMoneyExists(updateReqVO.getId());
        // 更新
        DeviceMoneyDO updateObj = BeanUtils.toBean(updateReqVO, DeviceMoneyDO.class);
        deviceMoneyMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceMoney(Long id) {
        // 校验存在
        validateDeviceMoneyExists(id);
        // 删除
        deviceMoneyMapper.deleteById(id);
    }

    private void validateDeviceMoneyExists(Long id) {
        if (deviceMoneyMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.DEVICE_MONEY_NOT_EXISTS);
        }
    }

    @Override
    public DeviceMoneyDO getDeviceMoney(Long id) {
        return deviceMoneyMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceMoneyDO> getDeviceMoneyPage(DeviceMoneyPageReqVO pageReqVO) {
        return deviceMoneyMapper.selectPage(pageReqVO);
    }

}