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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yc.ship.module.trade.dal.dataobject.invoice.InvoiceDetailDO;
import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO;
import com.yc.ship.module.trade.dal.mysql.invoice.InvoiceDetailMapper;
import com.yc.ship.module.trade.service.invoice.InvoiceDetailService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

/**
 * 发票订单关系 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class InvoiceDetailServiceImpl implements InvoiceDetailService {

    @Resource
    private InvoiceDetailMapper invoiceDetailMapper;

    @Override
    public void createInvoiceDetail(List<TradeOrderDO> orderList, Long invoiceId) {
        orderList.forEach(order ->{
            InvoiceDetailDO detailDO = new InvoiceDetailDO();
            long id = IdWorker.getId(detailDO);
            detailDO.setId(id);
            detailDO.setInvoiceId(invoiceId);
            detailDO.setAmount(order.getPayAmount());
            detailDO.setOrderNo(order.getOrderNo());
            detailDO.setDeleted(false);
            invoiceDetailMapper.insert(detailDO);
        });

    }

    @Override
    public InvoiceDetailDO getInvoiceDetail(Long id) {
        return invoiceDetailMapper.selectById(id);
    }

    @Override
    public List<InvoiceDetailDO> getInvoiceDetails(Long invoiceId) {
        return invoiceDetailMapper.selectList(InvoiceDetailDO::getInvoiceId, invoiceId);
    }

    @Override
    public Boolean validOrderInvoiceDetail(String orderNo) {
        List<InvoiceDetailDO> orderDetailList = invoiceDetailMapper.queryInvoiceOrderDetail(orderNo);
        if (CollUtil.isNotEmpty(orderDetailList)){
            return true;
        }
        return false;
    }
}