package com.neusoft.smart.pos.inventory.repository;

import com.neusoft.smart.pos.inventory.constants.InventoryConstant;
import com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreBatch;
import com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreBatchExample;
import com.neusoft.smart.pos.inventory.persistent.mapper.InventoryStoreBatchMapper;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author tjm
 * @date 2018-06-20 13：57
 * @description 库存Repository
 */
@Component
public class InventoryStoreBatchRepository {
    
    @Autowired
    public InventoryStoreBatchMapper inventoryStoreBatchMapper;
    
    public int insertSelective(InventoryStoreBatch inventoryStoreBatch) {
        return inventoryStoreBatchMapper.insertSelective(inventoryStoreBatch);
    }
    
    public int updateSelective(InventoryStoreBatch inventoryStoreBatch) {
        return inventoryStoreBatchMapper.updateByPrimaryKeySelective(inventoryStoreBatch);
    }
    
    public List<InventoryStoreBatch> selectAllBatchByGoodsId(InventoryStoreBatch inventoryStoreBatch) {
        val example = new InventoryStoreBatchExample();
        example.setOrderByClause("id");
        example.createCriteria()
                .andGoodsIdEqualTo(inventoryStoreBatch.getGoodsId())
                .andStatusEqualTo(inventoryStoreBatch.getStatus());
        val InventoryBatchs = inventoryStoreBatchMapper.selectByExample(example);
        return InventoryBatchs;
    }
    
    public BigDecimal sumStockNumByGoodsId(InventoryStoreBatch inventoryStoreBatch) {
        val example = new InventoryStoreBatchExample();
        example.createCriteria()
                .andGoodsIdEqualTo(inventoryStoreBatch.getGoodsId())
                .andStatusEqualTo(inventoryStoreBatch.getStatus());
        return inventoryStoreBatchMapper.sumByExample(example);
    }
    
    public List<InventoryStoreBatch> selectMinusStockByGoodsId(InventoryStoreBatch inventoryStoreBatch) {
        val example = new InventoryStoreBatchExample();
        example.createCriteria()
                .andGoodsIdEqualTo(inventoryStoreBatch.getGoodsId())
                .andStatusEqualTo(inventoryStoreBatch.getStatus())
                .andStorageNumLessThan(0);
        return inventoryStoreBatchMapper.selectByExample(example);
    }
    
    public List<InventoryStoreBatch> selectMaxIdZeroBatchByGoodsId(InventoryStoreBatch inventoryStoreBatch) {
        val example = new InventoryStoreBatchExample();
        example.createCriteria()
                .andGoodsIdEqualTo(inventoryStoreBatch.getGoodsId())
                .andStatusEqualTo(InventoryConstant.ZERO_BATCH);
        return inventoryStoreBatchMapper.selectMaxIdBatchByGoodsId(example);
    }
    
    public List<InventoryStoreBatch> selectMaxIdBatchByGoodsId(InventoryStoreBatch inventoryStoreBatch) {
        val example = new InventoryStoreBatchExample();
        example.createCriteria()
                .andGoodsIdEqualTo(inventoryStoreBatch.getGoodsId());
        return inventoryStoreBatchMapper.selectMaxIdBatchByGoodsId(example);
    }
    
    public BigDecimal getAvgPrice(InventoryStoreBatch inventoryStoreBatch) {
        val example = new InventoryStoreBatchExample();
        example.createCriteria()
                .andGoodsIdEqualTo(inventoryStoreBatch.getGoodsId());
        return inventoryStoreBatchMapper.getAvgPrice(example);
    }

//	public List<InventoryStoreBatch> selectMinIdBatchByGoodsId(InventoryStoreBatch inventoryStoreBatch){
//		val example = new InventoryStoreBatchExample();
//		example.createCriteria()
//				.andGoodsIdEqualTo(inventoryStoreBatch.getGoodsId())
//				.andStatusEqualTo(InventoryConstant.USABLE_BATCH);
//		return inventoryStoreBatchMapper.selectMinIdBatchByGoodsId(example);
//	}

}
