package com.fjhx.purchase.service.invoice.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.purchase.entity.invoice.po.Invoice;
import com.fjhx.purchase.entity.invoice.po.InvoiceDetails;
import com.fjhx.purchase.entity.purchase.po.Purchase;
import com.fjhx.purchase.mapper.invoice.InvoiceDetailsMapper;
import com.fjhx.purchase.service.invoice.InvoiceDetailsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.supply.entity.supplier.po.SupplierInfo;
import com.fjhx.supply.service.supplier.SupplierInfoService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.wrapper.SqlField;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.purchase.entity.invoice.vo.InvoiceDetailsVo;
import com.fjhx.purchase.entity.invoice.dto.InvoiceDetailsSelectDto;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.fjhx.purchase.entity.invoice.dto.InvoiceDetailsDto;
import cn.hutool.core.bean.BeanUtil;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 发票明细 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-04-12
 */
@DS(SourceConstant.PURCHASE)
@Service
public class InvoiceDetailsServiceImpl extends ServiceImpl<InvoiceDetailsMapper, InvoiceDetails> implements InvoiceDetailsService {

    @Autowired
    private SupplierInfoService supplierInfoService;


    @Override
    public Page<InvoiceDetailsVo> getPage(InvoiceDetailsSelectDto dto) {
        IWrapper<InvoiceDetails> wrapper = getWrapper();
        wrapper.orderByDesc("t1", InvoiceDetails::getCreateTime);
        if(ObjectUtils.isNotEmpty(dto.getInvoiceType())){
            wrapper.eq("t2", Invoice::getType,dto.getInvoiceType());
        }
        if(StringUtils.isNotEmpty(dto.getKeyword())){
            wrapper.keyword(dto.getKeyword(), new SqlField("t3", Purchase::getCode));
        }
        Page<InvoiceDetailsVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<InvoiceDetailsVo> list = page.getRecords();
        if(CollectionUtils.isNotEmpty(list)){
            List<Long> supplyIds = list.stream().map(InvoiceDetailsVo::getSupplyId).collect(Collectors.toList());
            List<SupplierInfo> supplierInfoList = supplierInfoService.list(Wrappers.<SupplierInfo>query().lambda().in(SupplierInfo::getId,supplyIds));
            Map<Long,List<SupplierInfo>> supplyMap = supplierInfoList.stream().distinct().collect(Collectors.groupingBy(SupplierInfo::getId));
            if(MapUtils.isNotEmpty(supplyMap)){
                for(InvoiceDetailsVo p:list){
                    List<SupplierInfo> supplys = supplyMap.getOrDefault(p.getSupplyId(),null);
                    p.setSupplyName(supplys==null?null:supplys.get(0).getName());
                }
            }
        }
        return page;
    }

    @Override
    public InvoiceDetailsVo detail(Long id) {
        InvoiceDetails InvoiceDetails = this.getById(id);
        InvoiceDetailsVo result = BeanUtil.toBean(InvoiceDetails, InvoiceDetailsVo.class);
        return result;
    }

    @Override
    public void add(InvoiceDetailsDto invoiceDetailsDto) {
        this.save(invoiceDetailsDto);
    }

    @Override
    public void edit(InvoiceDetailsDto invoiceDetailsDto) {
        this.updateById(invoiceDetailsDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

    /**
     * 根据采购单ids查询总发票金额
     * @param purchaseIds
     * @return
     */
    @Override
    public List<InvoiceDetailsVo> getSumMoneyByPurchaseIds(List<Long> purchaseIds) {
        return baseMapper.getSumMoneyByPurchaseIds(purchaseIds);
    }

    /**
     * 根据发票ID查询明细
     * @param invoiceId
     * @return
     */
    @Override
    public List<InvoiceDetailsVo> getDetail(Long invoiceId) {
        return baseMapper.getDetail(invoiceId);
    }


}
