package com.fjhx.service.supplier.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.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;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

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

    @Override
    public List<Map<String, Object>> getList(Map<String, String> condition) {
        QueryWrapper<?> wrapper = getWrapper(condition);
        return baseMapper.getList(wrapper);
    }

    @Override
    public Page<Map<String, Object>> getPage(Map<String, String> condition) {
        QueryWrapper<?> wrapper = getWrapper(condition);
        return baseMapper.getPage(createPage(condition), wrapper);
    }

    @Override
    public void add(SupplierPriceVo supplierPriceVo) {
        Long count = count(
                Wrappers.<SupplierPrice>lambdaQuery()
                .eq(SupplierPrice::getSupplierId, supplierPriceVo.getSupplierId())
                .eq(SupplierPrice::getMaterialId, supplierPriceVo.getMaterialId()));

        Assert.eqZero(count, "物料价格维护已存在");

        save(supplierPriceVo);
    }

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

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

    private QueryWrapper<?> getWrapper(Map<String, String> condition) {
        return WrapperUtil.init(condition)
                .keyword("m.name", "m.code", "s.name")
                .eq("s.id", "supplierId")
                .createTimeDesc()
                .getWrapper();
    }

}
