package com.aegis.resource.material.service.impl;

import com.aegis.common.utils.StringUtils;
import com.aegis.resource.material.domain.MaterialUsage;
import com.aegis.resource.material.mapper.ResourceMaterialUsageMapper;
import com.aegis.resource.material.service.IResourceMaterialUsageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 * 物资使用记录Service业务层处理.
 */
@Service
public class ResourceMaterialUsageServiceImpl extends ServiceImpl<ResourceMaterialUsageMapper, MaterialUsage>
    implements IResourceMaterialUsageService {

    @Override
    public List<MaterialUsage> listByCondition(MaterialUsage condition) {
        LambdaQueryWrapper<MaterialUsage> wrapper = buildConditionWrapper(condition);
        wrapper.orderByDesc(MaterialUsage::getCreateTime);
        return list(wrapper);
    }

    @Override
    public List<MaterialUsage> listByMaterialId(Long materialId) {
        LambdaQueryWrapper<MaterialUsage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MaterialUsage::getMaterialId, materialId)
            .orderByDesc(MaterialUsage::getCreateTime);
        return list(wrapper);
    }

    private LambdaQueryWrapper<MaterialUsage> buildConditionWrapper(MaterialUsage condition) {
        LambdaQueryWrapper<MaterialUsage> wrapper = new LambdaQueryWrapper<>();
        if (condition == null) {
            return wrapper;
        }
        wrapper.eq(condition.getMaterialId() != null, MaterialUsage::getMaterialId, condition.getMaterialId());
        wrapper.eq(condition.getWarehouseId() != null, MaterialUsage::getWarehouseId, condition.getWarehouseId());
        wrapper.eq(condition.getEventId() != null, MaterialUsage::getEventId, condition.getEventId());
        wrapper.eq(condition.getTaskId() != null, MaterialUsage::getTaskId, condition.getTaskId());
        wrapper.eq(StringUtils.isNotBlank(condition.getAction()), MaterialUsage::getAction, condition.getAction());
        wrapper.like(StringUtils.isNotBlank(condition.getRemark()), MaterialUsage::getRemark, condition.getRemark());
        return wrapper;
    }
}