package com.fs.hisStore.service;

import com.alibaba.fastjson.JSONObject;
import com.fs.api.param.OrderListParam;
import com.fs.api.vo.OrderListVO;
import com.fs.common.core.domain.R;
import com.fs.company.domain.CompanyUser;
import com.fs.course.domain.FsCoursePlaySourceConfig;
import com.fs.course.dto.FsOrderDeliveryNoteDTO;
import com.fs.erp.domain.ErpOrder;
import com.fs.his.dto.FsStoreOrderAmountScrmStatsQueryDto;
import com.fs.his.enums.PaymentMethodEnum;
import com.fs.his.param.FsIntegralOrderDoPayParam;
import com.fs.his.param.FsStoreOrderSalesParam;
import com.fs.his.param.PayOrderParam;
import com.fs.his.vo.FsStoreOrderAmountScrmStatsVo;
import com.fs.his.vo.FsPrescribeVO;
import com.fs.his.vo.FsStoreOrderExcelVO;
import com.fs.hisStore.domain.FsStoreOrderItemScrm;
import com.fs.hisStore.domain.FsStoreOrderLogsScrm;
import com.fs.hisStore.domain.FsStoreOrderScrm;
import com.fs.hisStore.domain.FsStorePaymentScrm;
import com.fs.hisStore.dto.ExpressNotifyDTO;
import com.fs.hisStore.dto.ExpressResultDTO;
import com.fs.hisStore.dto.FsStoreOrderComputeDTO;
import com.fs.hisStore.dto.StoreOrderExpressExportDTO;
import com.fs.hisStore.param.*;
import com.fs.hisStore.vo.*;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * 订单Service接口
 *
 * @author fs
 * @date 2022-03-15
 */
public interface IFsStoreOrderScrmService
{

    /**
     * 同步物流信息到微信
     */
    void syncExpressToWx();
    /**
     * 查询订单
     *
     * @param id 订单ID
     * @return 订单
     */
    public FsStoreOrderScrm selectFsStoreOrderById(Long id);

    /**
     * 查询订单列表
     *
     * @param fsStoreOrder 订单
     * @return 订单集合
     */
    public List<FsStoreOrderScrm> selectFsStoreOrderList(FsStoreOrderScrm fsStoreOrder);

    /**
     * 新增订单
     *
     * @param fsStoreOrder 订单
     * @return 结果
     */
    public int insertFsStoreOrder(FsStoreOrderScrm fsStoreOrder);

    /**
     * 修改订单
     *
     * @param fsStoreOrder 订单
     * @return 结果
     */
    public int updateFsStoreOrder(FsStoreOrderScrm fsStoreOrder);

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单ID
     * @return 结果
     */
    public int deleteFsStoreOrderByIds(Long[] ids);

    /**
     * 删除订单信息
     *
     * @param id 订单ID
     * @return 结果
     */
    public int deleteFsStoreOrderById(Long id);

    List<FsStoreOrderVO> selectFsStoreOrderListVO(FsStoreOrderParam param);

    List<FsStoreOrderVO> selectFsStoreOrderAllListVO(FsStoreOrderParam param);

    List<FsStoreOrderVO> selectPayRemainListVO(FsStoreOrderParam param);


    List<FsPromotionOrderVO> selectFsPromotionOrderListVO(FsStoreOrderParam param);

    R confirmOrder(long uid, FsStoreConfirmOrderParam cartParam);

    FsStoreOrderComputeDTO computedOrder(long parseLong, FsStoreOrderComputedParam param);


    /**
     * 查询用户对某个商品规格的已购买数量
     * @param userId 用户ID
     * @param productAttrValueId 商品规格ID
     * @return 已购买数量
     */
    Integer selectUserPurchasedCount(Long userId, Long productAttrValueId);

    R createOrder(long userId, FsStoreOrderCreateParam param);

    R createOrderByPrescribe(Long prescribeId);

    void cancelOrder(Long orderId);

    void refundPrescribeOrder(Long orderId);

    void refundOrder(Long orderId);

    void deliveryOrder(String orderCode,String deliveryId,String deliverCode,String deliverName);

    void updateDeliveryOrder(Long id,String deliveryId,String deliverCode,String deliverName);

    List<FsMyStoreOrderListQueryVO> selectFsMyStoreOrderListVO(FsMyStoreOrderQueryParam param);

    FsStoreOrderScrm selectFsStoreOrderByOrderCode(String orderCode);

    R getExpress(FsStoreOrderScrm order);

    R createUserOrder(FsStoreOrderCreateUserParam param);

    List<FsMyStoreOrderListQueryVO> selectFsCompanyStoreOrderListVO(FsMyStoreOrderQueryParam param);

    FsStoreOrderScrm selectFsStoreOrderByExtendOrderId(String orderId);

    List<FsStoreOrderTuiVO> selectFsStoreOrderTuiListVO(String userId);

    R confirmPackageOrder(long userId, FsStoreConfirmPackageIdOrderParam param);

    R createPackageOrder(long userId, FsStorePackageOrderCreateParam param);

    R finishOrder(Long orderId);

    String payConfirm(Integer type,Long orderId,String payCode,String tradeNo,String bankTransactionId,String bankSerialNo);

    Integer selectFsStoreOrderCount(long userId, int status);

    /**
     * 根据时间查询订单总数
     * @param param
     * @return
     */
    Integer selectFsStoreOrderCount(FsStoreOrderStatisticsParam param);

    R computedPackageOrder(long userId, FsStoreComputedPackageIdOrderParam param);

    FsStoreOrderScrm selectFsStoreOrderByDeliveryId(String logisticCode);

    String importExpress(List<StoreOrderExpressExportDTO> list, boolean updateSupport) throws InterruptedException;

    R createOmsOrder(Long orderId) throws ParseException;

    ErpOrder getErpOrder(FsStoreOrderScrm order) throws ParseException;

    FsStoreOrderVO selectFsStoreOrderVOByOrderCode(String orderCode);

    Long selectFsStoreOrderTotalCount(int type,Long companyId,Long companyUserId);

    R editTuiMoney(FsStoreOrderEditTuiMoneyParam param);

    List<OrderListVO> selectCompanyStoreOrderListAPI(OrderListParam param);

    List<FsStoreOrderScrm> selectFsStoreOrderByCompanyId(Long companyId);

    List<FsStoreOrderScrm> selectFsStoreOrderListByDeliveryId(String logisticCode);

    R refundOrderMoney(Long orderId);

    R updateExpress(FsStoreOrderExpressEditParam param);

    R syncExpress(FsStoreOrderExpressEditParam param);

    R addTuiMoney(FsStoreOrderAddTuiMoneyParam param);

    R createSalesOrder(CompanyUser companyUser, String cateIds,Integer orderType,Integer orderMedium);

    R getSalesOrder(String createOrderKey);

    R addUserCart(long userId, String createOrderKey);

    R updateSalseOrderMoney(String createOrderKey, BigDecimal money,BigDecimal payAmount,Integer payType, Integer isPackage);

    Integer selectFsStoreOrderCountByType(Long companyId, long userId, int type);

    List<FsCustomerStoreOrderListQueryVO> selectFsCustomerStoreOrderListQuery(FsCustomerStoreOrderListQueryParam param);

    List<JSONObject> selectFsStoreOrderCounts(Map<String, Object> toMap);

    List<FsStoreOrderStatisticsVO> selectFsStoreOrderStatisticsList(FsStoreStatisticsParam param);

    List<FsStoreOrderErpExportVO> selectFsStoreOrderListVOByExport(FsStoreOrderParam param);

    List<FsStoreOrderPromotionExportVO> selectFsPromotionOrderListVOByExport(FsStoreOrderParam param);

    List<FsStorePayRemainOrderExportVO> selectFsStorePayRemainOrderListVOByExport(FsStoreOrderParam param);

    List<FsStoreOrderScrm> selectFsStoreOrderListByFinish7Day();

    List<FsStoreOrderVO> selectFsCustomerStoreOrderListVO(FsStoreOrderParam param);

    String payRemainConfirm(String payCode,String tradeNo,String bankTransactionId,String bankSerialNo);

    R auditPayRemain(Long orderId);

    Boolean isEntityNull(FsStoreOrderParam param);

    int uploadCredentials(FsStoreOrderScrm order);

    String getCredentialsById(Long id);

    List<Long> selectFsStoreOrderToPushByIds(String startTime);

    List<FsStoreOrderScrm> selectFsStoreOrderItemJson();

    int uploadItemJson(FsStoreOrderScrm order);

    ExpressResultDTO updateDeliveryItem(ExpressNotifyDTO notifyDTO);

    FsStoreOrderScrm selectOrderByUserIdLimit1(Long userId);


    List<FsStoreOrderScrm> selectFsStoreOrderStatisticsByUserId(List<Long> fsUserIdList);


    /**
     * 完成订单
     * @param orderId 订单id
     * @return R
     */
    R finishStoreOrder(Long orderId);

    /**
     * 根据时间查询实际支付金额
     *
     * @param param
     * @return
     */
    BigDecimal selectFsStoreOrderByPayPriceCount(FsStoreOrderStatisticsParam param);

    /**
     * 订单维度分页查询接口
     * @param param 查询条件
     * @return List<OrderStatisticsVo>
     * **/
    List<OrderStatisticsVo> selectOrderDimensionStatisticsList(OrderStatisticsParam param);

    List<OrderStatisticsVo> selectZDYOrderSaleStatisticsList(OrderStatisticsParam param);

    List<FsStoreOrderCountsVO> selectFsStoreOrderCountsByDept(Map<String, Object> map, Long deptId);

    R updateSalesOrderMoneyByProduct(FsStoreOrderMoneyByProductParam param);

    List<FsStoreProductCountsVO> selectFsStoreProductCountsByDept(Map<String, Object> map, Long deptId);

    FsStoreOrderScrm selectFsStoreOrderByOrderId(Long storeOrderId);

    List<FsStoreOrderExcelVO> selectFsStoreOrderExcelListSVO(FsStoreOrderParam fsStoreOrder);

    String importSroreOrder(List<FsStoreProductDeliverExcelVO> list, Long storeID,Long companyId);

    int deleteFsStoreOrderByOrderIds(String[] orderIds);

    List<FsStoreOrderItemScrm> selectFsStoreOrderItemList(String orderId);

    List<FsStoreOrderLogsScrm> selectFsStoreOrderLogsList(String orderId);

    FsPrescribeVO selectFsPrescribeByPrescribeIdVO(Long prescribeId);

    List<FsStorePaymentScrm> selectFsStorePaymentByOrderId(Long orderId);

    int sendGoods(FsStoreOrderScrm fsStoreOrder);

    int getGoods(Long id);

    int updateMoney(FsStoreOrderScrm fsStoreOrder);

    int tuiOrder(Long id);

    int afterSales(FsStoreOrderSalesParam fsStoreOrder);

    /**
     * 批量导入更新微信订单发货状态
     * @param voList 订单数据
     * @param appId 小程序ID（自动从订单支付记录中获取）
     * @param shipmentType 发货类型
     * **/
    R importDeliveryNoteExpress(List<FsStoreOrderDeliveryNoteExportVO> voList,String appId,Integer shipmentType);

    /**
     * 查询app商城订单金额统计信息
     * */
    FsStoreOrderAmountScrmStatsVo selectFsStoreOrderAmountScrmStats(FsStoreOrderAmountScrmStatsQueryDto queryDto);
    R queryReceiptType(Long orderId);

    /**
     * 刷新订单结算状态
     * **/
    void refreshOrderSettlementStatus();

    /**
     * 代付金额计算方法
     * @param order 订单信息
     * @param rate 比例
     * **/
    void calculateAgentPayment(FsStoreOrderScrm order,Integer rate);

    /**
     * 获取发货单数据
     * @param param 查询条件
     * @return list
     * **/
    List<FsStoreOrderDeliveryNoteExportVO> getDeliveryNote(FsStoreOrderParam param);

    R pay(FsStoreOrderPayParam param);

    void cancelPay(FsStoreOrderPayParam param);

    R otherPaymentRemain(FsStoreOrderOtherPayParam param);

    R otherPayment(FsStoreOrderOtherPayParam param);

    R payRemain(FsStoreOrderPayParam param);

    R editPayType(FsStoreOrderPayParam param);

    /**
     * 跟进代服账户查询订单
     * @param param
     * @return
     */
    List<FsStoreOrderVO> selectFsStoreOrderListVOByErpAccount(FsStoreOrderParam param);

    int batchUpdateErpByOrderIds(FsStoreOrderScrmSetErpPhoneParam param);

    Map<String, BigDecimal> selectFsStoreOrderStatistics(FsStoreOrderParam param);

    String selectFsStoreOrderProductStatistics(FsStoreOrderParam param);

    /**
     * 当从 fs_course_play_source_config 给支付单补全了 company_id 时，同步回写到订单表 fs_store_order_scrm，保证订单归属公司
     */
    void syncOrderCompanyFromPaymentIfSet(FsStoreOrderScrm order, FsStorePaymentScrm storePayment,
                                          FsCoursePlaySourceConfig fsCoursePlaySourceConfig);

    int create(Long orderId, String type, String msg);

    R dfOrderResult(String body);

    R receiveWaybillPush(String body);


    R importDeliveryNoteExpress(List<FsOrderDeliveryNoteDTO> dtoList, String miniAppId);


    /**
     * 套餐包制单
     * @param companyUser 销售
     * @param packageId 套餐包id
     * @param orderType 订单类型
     * @param orderMedium 媒体类型
     * @return
     */
    R createPackageSalesOrder(CompanyUser companyUser, String packageId, Integer orderType, Integer orderMedium);

    void cancelOrderByCode(String outerPayId);

    void updateFsStoreOrderDb(FsStoreOrderScrm order);

    /**
     * 批量审核订单
     *
     * @param param 批量审核参数
     * @return 更新条数
     */
    int batchAuditOrder(FsStoreOrderBatchAuditParam param);

    /**
     * 订单备注
     */
    R orderRemark(FsStoreOrderScrm orderScrm);

    int updateStoreOrderItemJson(Long orderId,Integer backendEditProductType);

    /**
     * 一键发货 - 将所有待发货订单标记为已发货并同步到微信物流
     * @param shipmentType 发货类型，默认为2（用户自提）
     * @return R
     */
    R batchDeliveryAllPendingOrders(Integer shipmentType);

    R payment(FsIntegralOrderDoPayParam param, PaymentMethodEnum paymentMethodEnum);
}
