package com.jwipc.supplier.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.supplier.entity.Supplier;
import com.jwipc.supplier.entity.SupplierExtension;
import com.jwipc.supplier.mapper.SupplierMapper;
import com.jwipc.supplier.model.param.ChoseSupplierListParam;
import com.jwipc.supplier.model.po.SupplierReportPO;
import com.jwipc.supplier.model.po.SupplierSelectPO;
import com.jwipc.supplier.model.query.SupplierPurchaseCategoryBaseQuery;
import com.jwipc.supplier.model.query.SupplierSelectQuery;
import com.jwipc.supplier.service.ISupplierExtensionService;
import com.jwipc.supplier.service.ISupplierService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 供应商主信息 服务实现类
 * </p>
 *
 * @author zhangjie
 * @since 2024-01-31
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements ISupplierService {
    private final ISupplierExtensionService supplierExtensionService;

    public SupplierServiceImpl(ISupplierExtensionService supplierExtensionService) {
        this.supplierExtensionService = supplierExtensionService;
    }

    @Override
    public List<String> getTableStruct() {
        return this.baseMapper.getTableStruct();
    }

    @Override
    public List<SupplierSelectPO> getListForSelect(Page<SupplierSelectPO> page, SupplierSelectQuery query) {
        return this.baseMapper.getListForSelect(page,query);
    }

    @Override
    public PageView getSupplierList(ChoseSupplierListParam choseSupplierListParam) {
        final Page<Supplier> page = PageUtil.toPage(choseSupplierListParam);
        List<Supplier> contracts = baseMapper.selectList(page,
            new LambdaQueryWrapper<Supplier>()
            .like(Objects.nonNull(choseSupplierListParam.getNameCOde()),Supplier::getSupplierName, choseSupplierListParam.getNameCOde())
            .or()
            .like(Objects.nonNull(choseSupplierListParam.getNameCOde()),Supplier::getSupplierCode, choseSupplierListParam.getNameCOde())
        );
        contracts.forEach(supplier -> {
            SupplierExtension supplierExtension = supplierExtensionService.getOne(Wrappers.lambdaQuery(SupplierExtension.class)
                    .eq(SupplierExtension::getSupplierId,supplier.getId()).eq(SupplierExtension::getExtensionKey,"currencyCode")
                    .last(" limit 1"));
            supplier.setRemark(ObjectUtil.isNotEmpty(supplierExtension)?supplierExtension.getExtensionValue():"");
        });
        return PageUtil.getPV(contracts, page.getTotal());
    }

    @Override
    public List<SupplierReportPO> findForSupplierReportExport(SupplierPurchaseCategoryBaseQuery query) {
        return this.baseMapper.findForSupplierReportExport(query);
    }
}
