package org.dromara.erp.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.erp.domain.ErpPack;
import org.dromara.erp.domain.query.ErpPackQuery;
import org.dromara.erp.domain.vo.ErpPackVo;
import org.dromara.erp.mapper.ErpPackMapper;
import org.dromara.erp.service.IErpPackService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ERP 包装档案 Service 业务层处理
 *
 * @Author: Antigravity
 */
@RequiredArgsConstructor
@Service
public class ErpPackServiceImpl implements IErpPackService {

    private final ErpPackMapper baseMapper;

    @Override
    public TableDataInfo<ErpPackVo> queryPageList(ErpPackQuery query, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpPack> lqw = buildQueryWrapper(query);
        IPage<ErpPackVo> page = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    private LambdaQueryWrapper<ErpPack> buildQueryWrapper(ErpPackQuery query) {
        LambdaQueryWrapper<ErpPack> lqw = Wrappers.lambdaQuery();
        lqw.eq(ErpPack::getStopUse, 0);
        if (query != null) {
            lqw.like(StrUtil.isNotBlank(query.getNum()), ErpPack::getNum, query.getNum());
            lqw.like(StrUtil.isNotBlank(query.getName()), ErpPack::getName, query.getName());
        }
        lqw.orderByAsc(ErpPack::getNum);
        return lqw;
    }

    @Override
    public List<ErpPackVo> queryList() {
        return baseMapper.selectVoList(Wrappers.lambdaQuery(ErpPack.class)
            .eq(ErpPack::getStopUse, 0)
            .orderByAsc(ErpPack::getNum));
    }

}
