import com.neusoft.smart.pos.dto.inventory.request.PostInventoryStoreBatchRequest;
import com.neusoft.smart.pos.dto.inventory.request.SellAndWarehouseOutRequest;
import com.neusoft.smart.pos.inventory.ServiceInventoryApplication;
import com.neusoft.smart.pos.inventory.constants.InventoryConstant;
import com.neusoft.smart.pos.dto.inventory.request.PostInventoryGoodsInfoRequest;
import com.neusoft.smart.pos.inventory.persistent.domain.InventoryGoodsLockInfo;
import com.neusoft.smart.pos.inventory.persistent.mapper.InventoryGoodsLockInfoMapper;
import com.neusoft.smart.pos.inventory.repository.InventoryGoodsLockInfoRepository;
import com.neusoft.smart.pos.inventory.service.GoodsDefaultService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

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

/**
 * Created by 姜涛 on 2017/3/22.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {ServiceInventoryApplication.class})
public class VersionTest {
    
    @Autowired
    private InventoryGoodsLockInfoMapper inventoryGoodsLockInfoMapper;
    
    @Autowired
    private InventoryGoodsLockInfoRepository InventoryGoodsLockInfoRepository;
    
    
    @Autowired
    GoodsDefaultService goodsDefaultService;
    
    
    @Test
    @Rollback(false)
    public void versionTest() {
        InventoryGoodsLockInfo record = new InventoryGoodsLockInfo();
//        record.setBeforeLockCount(1);
//        record.setGoodsBatchId(1);
//        record.setExpireTime(new Date());
//        record.setLockCount(1);
//        record.setLockTime(new Date());
//        record.setGoodsId(1);
//        record.setOrderId("11");
//        inventoryGoodsLockInfoMapper.insertSelective(record);
        record.setId(1);
        record.setOrderId("222");
        record.setVersion(0);
        InventoryGoodsLockInfoRepository.updateSelectiveWithVersion(record);
    }
    
    
    @Test
    @Rollback(false)
    public void insertGoods() {
        PostInventoryGoodsInfoRequest postInventoryGoodsInfoRequest = new PostInventoryGoodsInfoRequest();
        postInventoryGoodsInfoRequest.setStore(0);
        postInventoryGoodsInfoRequest.setOrg(0);
        postInventoryGoodsInfoRequest.setBarCode("1234567890");
        postInventoryGoodsInfoRequest.setCategoryId(1);
        postInventoryGoodsInfoRequest.setIsStock(1);
        postInventoryGoodsInfoRequest.setName("测试商品");
        postInventoryGoodsInfoRequest.setPrice(new BigDecimal(20.5));
        postInventoryGoodsInfoRequest.setUnit("份");
        postInventoryGoodsInfoRequest.setScale(2);
        goodsDefaultService.createGoods(postInventoryGoodsInfoRequest);
    }
    
    @Test
    @Rollback(false)
    public void createBatch() {
        PostInventoryStoreBatchRequest postInventoryStoreBatchRequest = new PostInventoryStoreBatchRequest();
        postInventoryStoreBatchRequest.setGoodsId(2);
        postInventoryStoreBatchRequest.setOperator("1");
        postInventoryStoreBatchRequest.setShopId(0);
        postInventoryStoreBatchRequest.setStorageNum(new BigDecimal(2000));
        postInventoryStoreBatchRequest.setOrgId(0);
        postInventoryStoreBatchRequest.setGoodsName("测试商品");
        postInventoryStoreBatchRequest.setInPrice(new BigDecimal(20.5));
        goodsDefaultService.warehouseIn(postInventoryStoreBatchRequest);
    }
    
    @Test
    @Rollback(false)
    public void sell() {
        SellAndWarehouseOutRequest sellAndWarehouseOutRequest = new SellAndWarehouseOutRequest();
        sellAndWarehouseOutRequest.setOrgId(171);
        sellAndWarehouseOutRequest.setShopId(322);
//        sellAndWarehouseOutRequest.setOrderExpireTime(DateUtils.addDay(new Date(), 1));
        sellAndWarehouseOutRequest.setOrderNum(new Date().getTime()+"");
        List<SellAndWarehouseOutRequest.OrderItem> orderItems = new ArrayList<>();
        SellAndWarehouseOutRequest.OrderItem orderItem = new SellAndWarehouseOutRequest.OrderItem();
        orderItem.setGoodsId(11);
        orderItem.setOrgId(171);
        orderItem.setShopId(322);
        orderItem.setScale(2);
        orderItem.setStorageNum(new BigDecimal(10));
        orderItems.add(orderItem);
        sellAndWarehouseOutRequest.setOrderItems(orderItems);
        goodsDefaultService.warehouseOut(sellAndWarehouseOutRequest);
    }
    
    @Test
    @Rollback(false)
    public void releaseLock() {
        goodsDefaultService.releaseLockStorage("238", 0,0 , InventoryConstant.LOCKER_DEDUCTED);
    }
}
