package com.jianshui.invoice.service.impl;

import java.util.List;
import com.jianshui.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.jianshui.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.jianshui.invoice.domain.InvoiceDetail;
import com.jianshui.invoice.mapper.InvoiceMapper;
import com.jianshui.invoice.domain.Invoice;
import com.jianshui.invoice.service.IInvoiceListService;

/**
 * 已开发票列表Service业务层处理
 * 
 * @author Gong Quanlin
 * @date 2022-03-08
 */
@Service
public class InvoiceListServiceImpl implements IInvoiceListService
{
    @Autowired
    private InvoiceMapper invoiceMapper;

    /**
     * 查询已开发票列表
     * 
     * @param id 已开发票列表主键
     * @return 已开发票列表
     */
    @Override
    public Invoice selectInvoiceById(Long id)
    {
        return invoiceMapper.selectInvoiceById(id);
    }

    /**
     * 查询已开发票列表列表
     * 
     * @param invoice 已开发票列表
     * @return 已开发票列表
     */
    @Override
    public List<Invoice> selectInvoiceList(Invoice invoice)
    {
        return invoiceMapper.selectInvoiceList(invoice);
    }

    /**
     * 新增已开发票列表
     * 
     * @param invoice 已开发票列表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertInvoice(Invoice invoice)
    {
        invoice.setCreateTime(DateUtils.getNowDate());
        int rows = invoiceMapper.insertInvoice(invoice);
        insertInvoiceDetail(invoice);
        return rows;
    }

    /**
     * 修改已开发票列表
     * 
     * @param invoice 已开发票列表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateInvoice(Invoice invoice)
    {
        invoice.setUpdateTime(DateUtils.getNowDate());
        invoiceMapper.deleteInvoiceDetailByInvoiceId(invoice.getId());
        insertInvoiceDetail(invoice);
        return invoiceMapper.updateInvoice(invoice);
    }

    /**
     * 批量删除已开发票列表
     * 
     * @param ids 需要删除的已开发票列表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInvoiceByIds(Long[] ids)
    {
        invoiceMapper.deleteInvoiceDetailByInvoiceIds(ids);
        return invoiceMapper.deleteInvoiceByIds(ids);
    }

    /**
     * 删除已开发票列表信息
     * 
     * @param id 已开发票列表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteInvoiceById(Long id)
    {
        invoiceMapper.deleteInvoiceDetailByInvoiceId(id);
        return invoiceMapper.deleteInvoiceById(id);
    }

    /**
     * 新增已开发票明细信息
     * 
     * @param invoice 已开发票列表对象
     */
    public void insertInvoiceDetail(Invoice invoice)
    {
        List<InvoiceDetail> invoiceDetailList = invoice.getInvoiceDetailList();
        Long id = invoice.getId();
        if (StringUtils.isNotNull(invoiceDetailList))
        {
            List<InvoiceDetail> list = new ArrayList<InvoiceDetail>();
            for (InvoiceDetail invoiceDetail : invoiceDetailList)
            {
                invoiceDetail.setInvoiceId(id);
                list.add(invoiceDetail);
            }
            if (list.size() > 0)
            {
                invoiceMapper.batchInvoiceDetail(list);
            }
        }
    }
}
