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

import com.alibaba.fastjson.JSONObject;
import com.neusoft.smart.pos.dto.inventory.request.PostInventoryStoreBatchRequest;
import com.neusoft.smart.pos.dto.inventory.request.SellAndWarehouseOutRequest;
import com.neusoft.smart.pos.framework.bean.PaginationExtendBean;
import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.inventory.constants.GoodsConstant;
import com.neusoft.smart.pos.inventory.constants.GoodsErrorCode;
import com.neusoft.smart.pos.dto.inventory.bean.LockStorageBean;
import com.neusoft.smart.pos.dto.inventory.request.InventoryGoodsQueryRequest;
import com.neusoft.smart.pos.dto.inventory.request.PostInventoryGoodsInfoRequest;
import com.neusoft.smart.pos.dto.inventory.request.PutInventoryGoodsInfoRequest;
import com.neusoft.smart.pos.dto.inventory.response.InventoryGoodsResponse;
import com.neusoft.smart.pos.inventory.util.EmojiUtil;

import java.util.List;

public interface GoodsBaseServiceInterface {
    
    /**
     * 用于第三方商品库存请求的签名或者其他验签的逻辑
     *
     * @param authData
     * @return
     */
    Object sign(JSONObject authData);
    
    /**
     * 用于获取第三方系统认证数据
     *
     * @return
     */
    JSONObject getAuthData();
    
    
    /**
     * 分页查询商品
     *
     * @param inventoryGoodsQueryRequest
     * @return
     */
    PaginationExtendBean<InventoryGoodsResponse> getPage(InventoryGoodsQueryRequest inventoryGoodsQueryRequest);
    
    /**
     * 创建商品
     *
     * @param postInventoryGoodsInfoRequest
     * @return
     */
    Boolean createGoods(PostInventoryGoodsInfoRequest postInventoryGoodsInfoRequest);
    
    /**
     * 批量创建商品
     *
     * @param postInventoryGoodsInfoRequestList
     * @return
     */
    Boolean batchCreateGoods(List<PostInventoryGoodsInfoRequest> postInventoryGoodsInfoRequestList);
    
    /**
     * 修改商品
     *
     * @param putInventoryGoodsInfoRequest
     * @return
     */
    Boolean updateGoods(PutInventoryGoodsInfoRequest putInventoryGoodsInfoRequest);
    
    /**
     * 删除商品
     *
     * @param goodsId
     * @param deleteBatch 是否级联删除批次
     * @return
     */
    Boolean deleteGoods(Integer goodsId, Boolean deleteBatch);
    
    /**
     * 通过id获取商品
     *
     * @param goodsId
     * @return
     */
    InventoryGoodsResponse getGoods(Integer goodsId);
    
    /**
     * 批量删除商品
     *
     * @param goodsIds
     * @return
     */
    boolean batchDeleteGoods(List<Integer> goodsIds);
    
    /**
     * 商品入库
     *
     * @param postInventoryStoreBatchRequest
     * @return
     */
    boolean warehouseIn(PostInventoryStoreBatchRequest postInventoryStoreBatchRequest);
    
    /**
     * 商品出库(多个商品,可以是订单中的商品列表) 暂时只是作为售卖所用， 暂不考虑商品后台系统出库
     *
     * @param sellAndWarehouseOutRequest
     * @return
     */
    boolean warehouseOut(SellAndWarehouseOutRequest sellAndWarehouseOutRequest);
    
    /**
     * 锁库存
     *
     * @param lockStorageBean
     * @return
     */
    boolean lockStorage(LockStorageBean lockStorageBean);
    
    /**
     * 释放库存锁
     *
     * @param orderId
     * @param orgId
     * @param shopId
     * @param status
     * @return
     */
    Boolean releaseLockStorage(String orderId, Integer orgId, Integer shopId, Integer status);
    
    
    default String validateBarCode(String barCode) {
        if (barCode.length() > GoodsConstant.BAR_CODE_SIZE) {
            throw new BusinessCommonException(
                    GoodsErrorCode.INVALID_BAR_CODE.getCode(), GoodsErrorCode.INVALID_BAR_CODE.getMessage() + ": " + barCode);
        }
        return barCode;
    }
    
    default String validateName(String name) {
        if (name.length() > GoodsConstant.MAX_GOODS_NAME_LENGTH || EmojiUtil.containsEmoji(name)) {
            throw new BusinessCommonException(
                    GoodsErrorCode.INVALID_GOODS_NAME.getCode(), GoodsErrorCode.INVALID_GOODS_NAME.getMessage() + ": " + name);
        }
        return name;
    }
    
    default String validateUnit(String unit) {
        if (unit.length() > GoodsConstant.MAX_GOODS_UNIT_LENGTH) {
            throw new BusinessCommonException(
                    GoodsErrorCode.INVALID_UNIT_NAME.getCode(), GoodsErrorCode.INVALID_GOODS_NAME.getMessage() + ": " + unit);
        }
        return unit;
    }
    
    default Double validatePrice(String priceStr) {
        Double price;
        try {
            price = Double.parseDouble(priceStr);
        } catch (Exception e) {
            GoodsErrorCode errorCode = GoodsErrorCode.INVALID_PRICE;
            throw new BusinessCommonException(errorCode.getCode(), errorCode.getMessage() + ": " + priceStr);
        }
        
        return validatePrice(price);
    }
    
    default Double validatePrice(Double price) {
        if (price <= 0) {
            throw new BusinessCommonException(
                    GoodsErrorCode.INVALID_PRICE.getCode(), GoodsErrorCode.INVALID_PRICE.getMessage() + ": " + price);
        }
        return price;
    }
    
}
