package com.ecep.contract.service;

import java.time.LocalDate;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ecep.contract.MessageHolder;
import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.vm.InventoryViewModel;
import com.ecep.contract.vo.InventoryVo;

@Service
public class InventoryService extends QueryService<InventoryVo, InventoryViewModel> {

    @Override
    public InventoryVo createNewEntity() {
        InventoryVo inventory = new InventoryVo();
        inventory.setCreateTime(LocalDate.now());
        inventory.setCreatorId(com.ecep.contract.Desktop.instance.getActiveEmployeeId());
        inventory.setActive(false);
        return inventory;
    }

    public void syncInventory(InventoryVo inventory, MessageHolder holder) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'syncInventory'");
    }

    public InventoryVo findByCode(String code) {
        Page<InventoryVo> page = findAll(ParamUtils.builder().equals("code", code).build(), Pageable.ofSize(1));
        if (page.isEmpty()) {
            return null;
        }
        return page.getContent().getFirst();
    }

    public InventoryVo findByName(String text) {
        Page<InventoryVo> page = findAll(ParamUtils.builder().equals("name", text).build(), Pageable.ofSize(1));
        if (page.isEmpty()) {
            return null;
        }
        return page.getContent().getFirst();
    }

}
