package com.jsh.erp.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.IService;
import com.jsh.erp.datasource.entities.*;
import com.jsh.erp.datasource.pda.vo.PDADepotItemVO;
import com.jsh.erp.datasource.pda.vo.PDAPrintVo;
import com.jsh.erp.datasource.vo.DepotHeadXsddRequestVO;
import com.jsh.erp.datasource.vo.DepotItemStockWarningCount;
import com.jsh.erp.datasource.vo.DepotItemVoBatchNumberList;
import com.jsh.erp.datasource.vo.DepotItemXsddRequestVO;
import com.jsh.erp.datasource.vo.InOutPriceVo;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public interface DepotItemService extends IService<DepotItem> {

    /**
     * pda根据订单信息查询商品列表
     * @return
     */
    List<PDADepotItemVO> pdaList(Long id) throws Exception;

    /**
     * pda根据商品订单ID查询商品详情
     * @param id 商品订单ID
     * @return
     */
    PDADepotItemVO pdaDetail(Long id) throws Exception;

    /**
     * 商品库存详情列表
     * @param materialId 商品ID
     * @return
     */
    List<PDADepotItemVO> materialDepotDetail(String type, Long materialId);

    /**
     * pda 获取商品打印条码信息
     * @param depotItemId 单据子表id
     * @return
     */
    PDAPrintVo pdaPrintMaterial(Long depotItemId);

    DepotItem getDepotItem(long id)throws Exception;

    List<DepotItem> getDepotItem()throws Exception;

    List<DepotItem> select(String name, Integer type, String remark, int offset, int rows)throws Exception;

    Long countDepotItem(String name, Integer type, String remark) throws Exception;

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    int insertDepotItem(JSONObject obj, HttpServletRequest request)throws Exception;

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    int updateDepotItem(JSONObject obj, HttpServletRequest request)throws Exception;

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    int deleteDepotItem(Long id, HttpServletRequest request)throws Exception;

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    int batchDeleteDepotItem(String ids, HttpServletRequest request)throws Exception;

    int checkIsNameExist(Long id, String name)throws Exception;

    List<DepotItemVo4DetailByTypeAndMId> findDetailByDepotIdsAndMaterialIdList(String depotIds, Boolean forceFlag, Boolean inOutManageFlag, String sku, String batchNumber,
                                                                               String number, String beginTime, String endTime, Long mId, Integer offset, Integer rows)throws Exception;

    Long findDetailByDepotIdsAndMaterialIdCount(String depotIds, Boolean forceFlag, Boolean inOutManageFlag, String sku, String batchNumber,
                                                String number, String beginTime, String endTime, Long mId)throws Exception;

    /**
     * 插入单据子表
     * @param depotItem 单据子表
     */
    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    int insertDepotItemWithObj(DepotItem depotItem)throws Exception;

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    int updateDepotItemWithObj(DepotItem depotItem)throws Exception;

    List<DepotItem> getListByHeaderId(Long headerId)throws Exception;

    DepotItem getItemByHeaderIdAndMaterial(Long headerId, Long meId)throws Exception;

    DepotItem getPreItemByHeaderIdAndMaterial(String linkStr, Long meId, Long linkId)throws Exception;

    List<DepotItemVo4WithInfoEx> getDetailList(Long headerId)throws Exception;

    List<DepotItemVo4WithInfoEx> getInOutStock(String materialParam, List<Long> categoryIdList, String endTime, Integer offset, Integer rows)throws Exception;

    int getInOutStockCount(String materialParam, List<Long> categoryIdList, String endTime)throws Exception;

    List<DepotItemVo4WithInfoEx> getListWithBuyOrSale(String materialParam, String billType,
                                                      String beginTime, String endTime, String[] creatorArray, Long organId, String[] organArray, List<Long> categoryList, List<Long> depotList, Boolean forceFlag, Integer offset, Integer rows)throws Exception;

    int getListWithBuyOrSaleCount(String materialParam, String billType,
                                  String beginTime, String endTime, String[] creatorArray, Long organId, String[] organArray, List<Long> categoryList, List<Long> depotList, Boolean forceFlag)throws Exception;

    BigDecimal buyOrSale(String type, String subType, Long MId, String beginTime, String endTime,
                         String[] creatorArray, Long organId, String[] organArray, List<Long> depotList, Boolean forceFlag, String sumType) throws Exception;

    BigDecimal buyOrSalePriceTotal(String type, String subType, String materialParam, String beginTime, String endTime,
                                   String[] creatorArray, Long organId, String[] organArray, List<Long> categoryList, List<Long> depotList, Boolean forceFlag) throws Exception;

    List<InOutPriceVo> inOrOutPriceList(String beginTime, String endTime) throws Exception;

    /**
     * 保存单据子表信息
     * @param rows 子表信息
     * @param headerId  主表id
     * @param actionType    操作类型
     */
    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    void saveDetials(String rows, Long headerId, String actionType, HttpServletRequest request) throws Exception;

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    void saveOrderItem(DepotHeadXsddRequestVO order, List<DepotItemXsddRequestVO> itemList) throws Exception;

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    String getBillStatusByParam(DepotHead depotHead, String linkStr, String linkType);

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    void changeBillStatus(String linkStr, String billStatus);

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    void changeBillPurchaseStatus(String linkStr, String billStatus);

    DepotItem getDepotItemByBatchNumber(Long materialExtendId, String batchNumber);

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    void deleteDepotItemHeadId(Long headerId)throws Exception;

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    void deleteOrCancelSerialNumber(String actionType, DepotHead depotHead, Long headerId) throws Exception;

    void checkAssembleWithMaterialType(JSONArray rowArr, String subType);

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    void updateMaterialExtendPrice(Long meId, String subType, String billType, JSONObject rowObj) throws Exception;

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    List<DepotItemStockWarningCount> findStockWarningCount(Integer offset, Integer rows, String materialParam, List<Long> depotList, List<Long> categoryList);

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    int findStockWarningCountTotal(String materialParam, List<Long> depotList, List<Long> categoryList);

    BigDecimal getSkuStockByParam(Long depotId, Long meId, String beginTime, String endTime) throws Exception;

    BigDecimal getStockByParam(Long depotId, Long mId, String beginTime, String endTime) throws Exception;

    BigDecimal getStockByParamWithDepotList(List<Long> depotList, Long mId, String beginTime, String endTime) throws Exception;

    Map<String, BigDecimal> getIntervalMapByParamWithDepotList(List<Long> depotList, Long mId, String beginTime, String endTime) throws Exception;

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    void updateCurrentStock(DepotItem depotItem) throws Exception;

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    void updateCurrentUnitPrice(DepotItem depotItem) throws Exception;

    /**
     * 根据商品id、仓库id更新当前库存
     * @param mId 商品id
     * @param dId 仓库id
     */
    void updateCurrentStockFun(Long mId, Long dId) throws Exception;

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    BigDecimal getFinishNumber(Long meId, Long id, Long headerId, Unit unitInfo, String materialUnit, String linkType);

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    BigDecimal getRealFinishNumber(String currentSubType, Long meId, Long linkId, Long preHeaderId, Long currentHeaderId, Unit unitInfo, String materialUnit);

    List<DepotItemVoBatchNumberList> getBatchNumberList(String number, String name, Long depotId, String barCode,
                                                        String batchNumber, Boolean forceFlag, Boolean inOutManageFlag) throws Exception;

    BigDecimal getOneBatchNumberStock(Long depotId, String barCode, String batchNumber) throws Exception;

    Long getCountByMaterialAndDepot(Long mId, Long depotId);

    /**
     * 解析excel表格数据
     * @param barCodes 批次号
     * @param detailList    数据明细
     * @param prefixNo  单据类型
     */
    JSONObject parseMapByExcelData(String barCodes, List<Map<String, String>> detailList, String prefixNo) throws Exception;

    BigDecimal getLastUnitPriceByParam(Long organId, Long meId, String prefixNo);

    BigDecimal getCurrentStockByParam(Long depotId, Long mId);

    String getOtherInfo(String[] mpArr, DepotItemVo4WithInfoEx diEx)throws Exception;
}
