package org.dromara.erp.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 lombok.RequiredArgsConstructor;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.erp.domain.GoodsStock;
import org.dromara.erp.domain.query.GoodsStockQuery;
import org.dromara.erp.domain.vo.GoodsStockVo;
import org.dromara.erp.mapper.GoodsStockMapper;
import org.dromara.erp.service.IGoodsStockService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 成品库存 Service 实现
 *
 * @Author: Antigravity
 */
@RequiredArgsConstructor
@Service
public class GoodsStockServiceImpl implements IGoodsStockService {

    private final GoodsStockMapper baseMapper;

    @Override
    public TableDataInfo<GoodsStockVo> queryPageList(GoodsStockQuery query, PageQuery pageQuery) {
        LambdaQueryWrapper<GoodsStock> lqw = buildQueryWrapper(query);
        Page<GoodsStockVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<GoodsStockVo> queryList(GoodsStockQuery query) {
        LambdaQueryWrapper<GoodsStock> lqw = buildQueryWrapper(query);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GoodsStock> buildQueryWrapper(GoodsStockQuery query) {
        LambdaQueryWrapper<GoodsStock> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtil.isNotEmpty(query.getItemNo()), GoodsStock::getItemNo, query.getItemNo());
        lqw.like(ObjectUtil.isNotEmpty(query.getModelNum()), GoodsStock::getModelNum, query.getModelNum());
        lqw.like(ObjectUtil.isNotEmpty(query.getModelName()), GoodsStock::getModelName, query.getModelName());
        lqw.like(ObjectUtil.isNotEmpty(query.getColorNum()), GoodsStock::getColorNum, query.getColorNum());
        lqw.like(ObjectUtil.isNotEmpty(query.getColorName()), GoodsStock::getColorName, query.getColorName());
        lqw.like(ObjectUtil.isNotEmpty(query.getClientNum()), GoodsStock::getClientNum, query.getClientNum());
        lqw.like(ObjectUtil.isNotEmpty(query.getClientName()), GoodsStock::getClientName, query.getClientName());
        lqw.eq(ObjectUtil.isNotEmpty(query.getDocCode()), GoodsStock::getDocCode, query.getDocCode());
        lqw.eq(ObjectUtil.isNotEmpty(query.getOrderState()), GoodsStock::getOrderState, query.getOrderState());
        lqw.eq(query.getIsHalf() != null, GoodsStock::getIsHalf, query.getIsHalf());
        lqw.eq(query.getIsSurplus() != null, GoodsStock::getIsSurplus, query.getIsSurplus());
        return lqw;
    }
}