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

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fjhx.account.entity.account.po.AccountPayment;
import com.fjhx.account.service.account.AccountPaymentService;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.purchase.entity.pay.po.Pay;
import com.fjhx.purchase.entity.purchase.vo.PurchaseVo;
import com.fjhx.purchase.mapper.pay.PayMapper;
import com.fjhx.purchase.service.pay.PayService;
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.pay.vo.PayVo;
import com.fjhx.purchase.entity.pay.dto.PaySelectDto;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.fjhx.purchase.entity.pay.dto.PayDto;
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-13
 */
@Service
public class PayServiceImpl extends ServiceImpl<PayMapper, Pay> implements PayService {

    @Autowired
    private SupplierInfoService supplierInfoService;

    @Autowired
    private AccountPaymentService accountPaymentService;

    /**
     * 分页
     * @param dto
     * @return
     */
    @Override
    public Page<PayVo> getPage(PaySelectDto dto) {
        IWrapper<Pay> wrapper = getWrapper();
        wrapper.orderByDesc("p", Pay::getCreateTime);
        if(StringUtils.isNotEmpty(dto.getStatus())){
            wrapper.eq("p",Pay::getStatus,dto.getStatus());
        }
        if(StringUtils.isNotEmpty(dto.getPayStatus())){
            wrapper.eq("p",Pay::getPayStatus,dto.getPayStatus());
        }
        if(StringUtils.isNotEmpty(dto.getKeyword())){
            wrapper.keyword(dto.getKeyword(),new SqlField(Pay::getUserName),new SqlField(Pay::getRemark));
        }
        Page<PayVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<PayVo> list = page.getRecords();
        if(CollectionUtils.isNotEmpty(list)){
            //查询供应商
            List<Long> supplyIds = list.stream().map(PayVo::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));
            //查询付款状态
            List<Long> ids = list.stream().map(PayVo::getId).collect(Collectors.toList());
            List<AccountPayment> accountPaymentList = accountPaymentService.list(Wrappers.<AccountPayment>query().lambda().in(AccountPayment::getBusinessId,ids));
            Map<Long,List<AccountPayment>> accountPaymentMap = accountPaymentList.stream().distinct().collect(Collectors.groupingBy(AccountPayment::getBusinessId));
            for(PayVo p:list){
                if(MapUtils.isNotEmpty(supplyMap)){
                    List<SupplierInfo> supplys = supplyMap.getOrDefault(p.getSupplyId(),null);
                    p.setSupplyName(supplys==null?null:supplys.get(0).getName());
                }
                if(MapUtils.isNotEmpty(accountPaymentMap)){
                    List<AccountPayment> accountPayments = accountPaymentMap.getOrDefault(p.getId(),null);
                    p.setPaymentStatus(accountPayments==null?null:accountPayments.get(0).getStatus());
                }
            }
        }
        return page;
    }

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

    @Override
    public void add(PayDto payDto) {
        this.save(payDto);
    }

    @Override
    public void edit(PayDto payDto) {
        this.updateById(payDto);
    }

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

}
