package com.yc.ship.module.trade.service.invoiceinventory.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.trade.controller.admin.invoice.inventory.InvoiceInventoryPageReqVO;
import com.yc.ship.module.trade.controller.admin.invoice.inventory.InvoiceInventorySaveReqVO;
import com.yc.ship.module.trade.dal.dataobject.invoiceinventory.InvoiceInventoryDO;
import com.yc.ship.module.trade.dal.mysql.invoiceinventory.InvoiceInventoryMapper;
import com.yc.ship.module.trade.service.invoiceinventory.InvoiceInventoryService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception0;


/**
 * 发票清单 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class InvoiceInventoryServiceImpl implements InvoiceInventoryService {

    @Resource
    private InvoiceInventoryMapper invoiceInventoryMapper;

    @Override
    public InvoiceInventoryDO createInvoiceInventory(InvoiceInventorySaveReqVO createReqVO) {
        // 插入
        InvoiceInventoryDO invoiceInventory = BeanUtils.toBean(createReqVO, InvoiceInventoryDO.class);
        invoiceInventory.setId(IdWorker.getId(invoiceInventory));
        invoiceInventory.setDeleted(false);
        invoiceInventoryMapper.insert(invoiceInventory);
        // 返回
        return invoiceInventory;
    }

    @Override
    public void updateInvoiceInventory(InvoiceInventorySaveReqVO updateReqVO) {
        // 校验存在
        validateInvoiceInventoryExists(updateReqVO.getId());
        // 更新
        InvoiceInventoryDO updateObj = BeanUtils.toBean(updateReqVO, InvoiceInventoryDO.class);
        invoiceInventoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteInvoiceInventory(Long id) {
        // 校验存在
        validateInvoiceInventoryExists(id);
        // 删除
        invoiceInventoryMapper.deleteById(id);
    }

    private void validateInvoiceInventoryExists(Long id) {
        if (invoiceInventoryMapper.selectById(id) == null) {
            throw exception0(30_030_06, "发票清单不存在");
        }
    }

    @Override
    public InvoiceInventoryDO getInvoiceInventory(Long id) {
        return invoiceInventoryMapper.selectById(id);
    }

    @Override
    public List<InvoiceInventoryDO> getInvoiceInventoryListByInvoiceId(Long invoiceId) {
        return invoiceInventoryMapper.selectListByInvoiceId(invoiceId);
    }

    @Override
    public PageResult<InvoiceInventoryDO> getInvoiceInventoryPage(InvoiceInventoryPageReqVO pageReqVO) {
        return invoiceInventoryMapper.selectPage(pageReqVO);
    }

}
