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

import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.inventory.constants.GoodsErrorCode;
import com.neusoft.smart.pos.dto.inventory.request.BaseGetGoodsListRequest;
import com.neusoft.smart.pos.redis.bean.DeviceInfo;
import com.neusoft.smart.pos.utils.PosDeviceRedisUtils;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;

import java.util.List;

abstract public class BaseGoodsService {
    
    protected void validateStore(Integer storeId) {
        List<Integer> storeIds = RequestHeaderUtil.getStoreIds();
        if (storeIds == null || !storeIds.contains(storeId)) {
            throw new BusinessCommonException(GoodsErrorCode.INVALID_SHOP);
        }
    }
    
    protected void setRequestBySnCode(String snCode, BaseGetGoodsListRequest request) {
        DeviceInfo deviceInfo = PosDeviceRedisUtils.getDeviceInfoBySerialNo(snCode);
        
        if (null == deviceInfo) {
            throw new BusinessCommonException(GoodsErrorCode.DEVICE_ORG_EMPTY);
        }
        
        Integer orgId = deviceInfo.getOrganizationId();
        Integer storeId = deviceInfo.getStoreId();
        
        request.setOrg(orgId);
        request.setStore(storeId);
        request.setOnSale(1);
    }
    
}
