package com.fjhx.service.supplier.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.entity.product.ProductInfo;
import com.fjhx.entity.supplier.GetSupplierPriceByProductIdsDto;
import com.fjhx.entity.supplier.Supplier;
import com.fjhx.entity.supplier.SupplierPrice;
import com.fjhx.mapper.supplier.SupplierPriceMapper;
import com.fjhx.params.supplier.SupplierPriceVo;
import com.fjhx.service.supplier.SupplierPriceService;
import com.fjhx.utils.Assert;
import com.fjhx.utils.wrapperUtil.IWrapper;
import com.fjhx.utils.wrapperUtil.KeywordData;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 供应商价格 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-11-25
 */
@Service
public class SupplierPriceServiceImpl extends ServiceImpl<SupplierPriceMapper, SupplierPrice> implements SupplierPriceService {

    @Override
    public Page<Map<String, Object>> getPage(Map<String, Object> condition) {

        IWrapper<Map<String, Object>> wrapper = IWrapper.getWrapper(condition);
        wrapper.orderByDesc(Supplier::getId)
                .keyword(new KeywordData("s", Supplier::getName),
                        new KeywordData("pi", ProductInfo::getName),
                        new KeywordData("pi", ProductInfo::getCode))
                .eq("s", Supplier::getId, condition.get("supplierId"))
                .like("s", Supplier::getName, condition.get("supplierName"))
                .like("s", Supplier::getType, condition.get("supplierType"))

                .eq("pi", ProductInfo::getId, condition.get("materialId"))
                .eq("pi", ProductInfo::getType, condition.get("materialType"))
                .like("pi", ProductInfo::getName, condition.get("materialName"))
                .like("pi", ProductInfo::getCode, condition.get("materialCode"));

        return baseMapper.getPage(createPage(condition), wrapper);
    }

    @Override
    public void add(SupplierPriceVo supplierPriceVo) {
        Long supplierId = supplierPriceVo.getSupplierId();
        Long materialId = supplierPriceVo.getMaterialId();
        BigDecimal price = supplierPriceVo.getPrice();

        Assert.notEmpty(supplierId, "供应商id不能为空");
        Assert.notEmpty(materialId, "物料id不能为空");
        Assert.notEmpty(price, "物料价格不能为空");

        long count = count(q -> q.eq(SupplierPrice::getSupplierId, supplierId).eq(SupplierPrice::getMaterialId, materialId));

        Assert.eqZero(count, "此供应商物料价格已存在");
        save(supplierPriceVo);
    }

    @Override
    public void edit(SupplierPriceVo supplierPriceVo) {
        updateById(supplierPriceVo);
    }

    @Override
    public void delete(SupplierPriceVo supplierPriceVo) {
        removeById(supplierPriceVo.getId());
    }

    @Override
    public List<SupplierPrice> getList(Map<String, Object> condition) {
        return lambdaQuery().
                eq(SupplierPrice::getSupplierId, condition.get("supplierId"))
                .in(Func.isNotEmpty(condition.get("goodsIds")),
                        SupplierPrice::getMaterialId,
                        JSONObject.parseArray(JSONObject.toJSONString(condition.get("goodsIds")), Long.class))
                .list();
    }

    @Override
    public Map<Long, BigDecimal> getSupplierPriceByProductIds(GetSupplierPriceByProductIdsDto dto) {
        Long supplierId = dto.getSupplierId();
        Assert.notEmpty(supplierId, "供应商id不能为空");

        List<Long> productIdList = dto.getProductIdList();
        Assert.notEmpty(productIdList, "产品id数组不能为空");

        return getKV(SupplierPrice::getMaterialId, SupplierPrice::getPrice,
                q -> q.eq(SupplierPrice::getSupplierId, supplierId).in(SupplierPrice::getMaterialId, productIdList));
    }

}
