package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.EmptyUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.FncInvoice;
import com.ruoyi.system.domain.vo.FncInvoiceItemVo;
import com.ruoyi.system.domain.vo.FncInvoiceVo;
import com.ruoyi.system.mapper.FncInvoiceItemMapper;
import com.ruoyi.system.mapper.FncInvoiceMapper;
import com.ruoyi.system.service.IFncInvoiceItemService;
import com.ruoyi.system.service.IFncInvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;

import com.ruoyi.system.domain.FncInvoiceItem;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 发票Service业务层处理
 *
 * @author jlf
 * @date 2022-11-20
 */
@Service
public class FncInvoiceServiceImpl extends ServiceImpl<FncInvoiceMapper, FncInvoice> implements IFncInvoiceService {

    @Resource
    FncInvoiceItemMapper fncInvoiceItemMapper;

    @Autowired
    IFncInvoiceItemService fncInvoiceItemService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(Collection<? extends Serializable> idList) {

        fncInvoiceItemMapper.delete(new LambdaQueryWrapper<FncInvoiceItem>().in(FncInvoiceItem::getInvoiceId,idList));

        return SqlHelper.retBool(baseMapper.deleteBatchIds(idList));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(FncInvoice entity) {
        baseMapper.insert(entity);
        Long invoiceId = entity.getInvoiceId();

        List<FncInvoiceItem> fncInvoiceItemList = entity.getFncInvoiceItemList();
        fncInvoiceItemList.forEach(item -> {
            item.setInvoiceId(invoiceId);
        });


        return fncInvoiceItemService.saveBatch(fncInvoiceItemList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(FncInvoice entity) {

        Long invoiceId = entity.getInvoiceId();
        fncInvoiceItemService.remove(new LambdaQueryWrapper<FncInvoiceItem>().eq(FncInvoiceItem::getInvoiceId,invoiceId));

        List<FncInvoiceItem> invoiceItemList = entity.getFncInvoiceItemList();
        invoiceItemList.forEach(item -> {
            item.setInvoiceId(invoiceId);
        });
        fncInvoiceItemService.saveBatch(invoiceItemList);


        return SqlHelper.retBool(baseMapper.updateById(entity));
    }

    @Override
    public TableDataInfo<FncInvoiceVo> queryList(FncInvoiceVo entity) {
        Page<FncInvoiceVo> page = this.baseMapper.queryList(PageUtils.buildPage(), entity);
        page.getRecords().forEach(item -> {
            List<FncInvoiceItem> fncInvoiceItemList = fncInvoiceItemMapper.selectList(new LambdaQueryWrapper<FncInvoiceItem>()
                    .eq(FncInvoiceItem::getInvoiceId, item.getInvoiceId()));
            item.setFncInvoiceItemList(fncInvoiceItemList);

        });
        return PageUtils.buildDataInfo(this.baseMapper.queryList(PageUtils.buildPage(), entity));
    }

    @Override
    public List<FncInvoiceVo> queryAll(FncInvoiceVo entity) {
        List<FncInvoiceVo> fncInvoiceVos = this.baseMapper.queryList(entity);
        fncInvoiceVos.forEach(item -> {
            List<FncInvoiceItem> fncInvoiceItemList = fncInvoiceItemMapper.selectList(new LambdaQueryWrapper<FncInvoiceItem>()
                    .eq(FncInvoiceItem::getInvoiceId, item.getInvoiceId()));
            item.setFncInvoiceItemList(fncInvoiceItemList);

        });
        return fncInvoiceVos;
    }

    @Override
    public FncInvoiceVo queryById(Long invoiceId) {
        FncInvoiceVo fncInvoiceVo = this.baseMapper.queryById(invoiceId);
        List<FncInvoiceItem> fncInvoiceItemList = fncInvoiceItemMapper.selectList(new LambdaQueryWrapper<FncInvoiceItem>()
                .eq(FncInvoiceItem::getInvoiceId, fncInvoiceVo.getInvoiceId()));
        fncInvoiceVo.setFncInvoiceItemList(fncInvoiceItemList);
        return fncInvoiceVo;
    }
}
