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.ErpModel;
import org.dromara.erp.domain.query.ErpModelQuery;
import org.dromara.erp.domain.vo.ErpModelVo;
import org.dromara.erp.mapper.ErpModelMapper;
import org.dromara.erp.service.IErpModelService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ERP 型材档案 Service 业务层处理
 *
 * @Author: Antigravity
 */
@RequiredArgsConstructor
@Service
public class ErpModelServiceImpl implements IErpModelService {

    private final ErpModelMapper baseMapper;

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

    @Override
    public List<ErpModelVo> queryList(ErpModelQuery query) {
        LambdaQueryWrapper<ErpModel> lqw = buildQueryWrapper(query);
        if (ObjectUtil.isEmpty(query.getNum()) && ObjectUtil.isEmpty(query.getName())) {
            lqw.last("OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY");
        }
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpModel> buildQueryWrapper(ErpModelQuery query) {
        LambdaQueryWrapper<ErpModel> lqw = Wrappers.lambdaQuery();
        lqw.like(ObjectUtil.isNotEmpty(query.getNum()), ErpModel::getNum, query.getNum());
        lqw.like(ObjectUtil.isNotEmpty(query.getName()), ErpModel::getName, query.getName());
        lqw.eq(ErpModel::getStopUse, 0);
        lqw.orderByAsc(ErpModel::getNum);
        return lqw;
    }

}
