package net.lab1024.sa.admin.module.business.itemKey.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import net.lab1024.sa.admin.module.business.itemKey.dao.ItemKeyDao;
import net.lab1024.sa.admin.module.business.itemKey.domain.entity.ItemKeyEntity;
import net.lab1024.sa.admin.module.business.itemKey.domain.form.ItemKeyQueryForm;
import net.lab1024.sa.admin.module.business.itemKey.domain.vo.ItemKeyVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemKeyQueryService {

    @Resource
    private ItemKeyDao itemKeyDao;

    /**
     * 分页查询
     */
    public PageResult<ItemKeyVO> queryPage(ItemKeyQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<ItemKeyVO> list = itemKeyDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    public ItemKeyEntity queryItemKey(String orderNumber, Long itemId, String propC1) {
        LambdaQueryWrapper<ItemKeyEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ItemKeyEntity::getOrderNumber, orderNumber);
        queryWrapper.eq(ItemKeyEntity::getItemId, itemId);
        queryWrapper.eq(ItemKeyEntity::getPropC1, propC1);
        List<ItemKeyEntity> itemKeys = itemKeyDao.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(itemKeys)) {
            return null;
        }
        return itemKeys.get(0);
    }
}
