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

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.module.system.controller.admin.cashregisterinfo.vo.CashRegisterInfoPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.cashregisterinfo.vo.CashRegisterInfoSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.cashregisterinfo.CashRegisterInfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.deviceInfo.DeviceInfoDO;
import cn.iocoder.yudao.module.system.dal.mysql.cashregisterinfo.CashRegisterInfoMapper;
import cn.iocoder.yudao.module.system.enums.ErrorCodeConstants;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.CASH_REGISTER_INFO_NOT_EXISTS;


/**
 * 收银机信息关联门店 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CashRegisterInfoServiceImpl implements CashRegisterInfoService {

    @Resource
    private CashRegisterInfoMapper cashRegisterInfoMapper;

    @Override
    public Long createCashRegisterInfo(CashRegisterInfoSaveReqVO createReqVO) {
        // 插入
        CashRegisterInfoDO cashRegisterInfo = BeanUtils.toBean(createReqVO, CashRegisterInfoDO.class);
        List<CashRegisterInfoDO> cashRegisterInfoDOS = cashRegisterInfoMapper.selectList(new LambdaQueryWrapper<CashRegisterInfoDO>()
                .eq(CashRegisterInfoDO::getEquipmentCode, createReqVO.getEquipmentCode()));
        if (CollectionUtil.isNotEmpty(cashRegisterInfoDOS)){
            throw exception(ErrorCodeConstants.CASH_REGISTER_INFO_EXISTS);
        }
        cashRegisterInfoMapper.insert(cashRegisterInfo);
        // 返回
        return cashRegisterInfo.getId();
    }

    @Override
    public void updateCashRegisterInfo(CashRegisterInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateCashRegisterInfoExists(updateReqVO.getId());
        List<CashRegisterInfoDO> cashRegisterInfoDOS = cashRegisterInfoMapper.selectList(new LambdaQueryWrapper<CashRegisterInfoDO>()
                .eq(CashRegisterInfoDO::getEquipmentCode, updateReqVO.getEquipmentCode())
                .ne(CashRegisterInfoDO::getId, updateReqVO.getId()));
        if (CollectionUtil.isNotEmpty(cashRegisterInfoDOS)){
            throw exception(ErrorCodeConstants.CASH_REGISTER_INFO_EXISTS);
        }
        // 更新
        CashRegisterInfoDO updateObj = BeanUtils.toBean(updateReqVO, CashRegisterInfoDO.class);
        cashRegisterInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteCashRegisterInfo(Long id) {
        // 校验存在
        validateCashRegisterInfoExists(id);
        // 删除
        cashRegisterInfoMapper.deleteById(id);
    }

    private void validateCashRegisterInfoExists(Long id) {
        if (cashRegisterInfoMapper.selectById(id) == null) {
            throw exception(CASH_REGISTER_INFO_NOT_EXISTS);
        }
    }

    @Override
    public CashRegisterInfoDO getCashRegisterInfo(Long id) {
        return cashRegisterInfoMapper.selectById(id);
    }

    @Override
    public PageResult<CashRegisterInfoDO> getCashRegisterInfoPage(CashRegisterInfoPageReqVO pageReqVO) {
        return cashRegisterInfoMapper.selectPage(pageReqVO);
    }

}