package com.yn.bftl.thirdparty.common.repository;

import com.yn.bftl.common.modules.order.entity.Order;
import com.yn.bftl.common.modules.order.enums.OrderStatus;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 订单Repository
 *
 * @author chendehuihuo
 * @date 2021/11/19 14:38
 **/
@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {

    Order findFirstBySn(String sn);

    /**
     * 订单-查询
     *
     * @param orderStatus:订单状态
     * @param localDateTime:售后结束时间
     * @param sellerCompanyId:销售企业ID
     * @return 符合分账条件的订单
     * @description 订单状态：已完成、售后时间：已过、是否分账：false
     * @author huabiao
     * @create 2022/9/3  15:10
     */
    List<Order> findAllByOrderStatusAndCompletedDateBeforeAndIsSeparateAccountsFalseAndSellerCompanyId(OrderStatus orderStatus, LocalDateTime localDateTime, Long sellerCompanyId);


    /**
     * 订单-修改分账状态
     *
     * @param id:订单ID
     * @description
     * @author huabiao
     * @create 2022/9/3  15:33
     */
    @Transactional
    @Modifying
    @Query(value = "update Order orders set orders.isSeparateAccounts = 1 where orders.id = ?1")
    void updateIsSeparateAccountsById(Long id);

    /**
     * 经销商对账单统计明细
     *
     * @param startTime
     * @param endTime
     * @param companyId
     * @param dealersId
     * @return
     */
    @Deprecated
    @Query(value = "SELECT " +
            " DATE_FORMAT(rr.pay_time,'%Y年%m月%d日') as billDateTime, " +
            " sum( IFNULL( rr.amount, 0 )) AS payAmount , " +
            " sum( IFNULL( rr.amount, 0 ) - IFNULL( oo.refund_amount, 0 )) AS amount, " +
            " sum( IFNULL( oo.refund_amount, 0 )) AS refundAmount, " +
            " sum( IF( rr.payment_method = 'WECHAT', IFNULL( rr.amount, 0 ), 0 )) AS wechatAmount, " +
            " sum( IF( rr.payment_method = 'ALIPAY', IFNULL( rr.amount, 0 ), 0 )) AS alipayAmount, " +
            " sum( IF( rr.payment_method = 'DEPOSIT', IFNULL( rr.amount, 0 ), 0 )) AS depositAmount, " +
            " sum( IF( rr.payment_method = 'CREDIT_PAY', IFNULL( rr.amount, 0 ), 0 )) AS creditPayAmount, " +
            " sum( IF( rr.payment_method = 'POS', IFNULL( rr.amount, 0 ), 0 )) AS posAmount, " +
            " sum( IF( rr.payment_method = 'CHINA_UNIONPAY_QUICK_PASS', IFNULL( rr.amount, 0 ), 0 )) AS quickPassAmount, " +
            " sum( IFNULL( oo.discount_amount, 0 )) AS discountAmount, " +
            " sum( IFNULL( oo.deduction_integral, 0 )) AS deductionIntegral " +
            "FROM " +
            " order_order oo " +
            " LEFT JOIN order_receipt rr ON rr.orders = oo.id  " +
            "WHERE " +
            " rr.pay_time BETWEEN ?1 AND ?2 " +
            " AND ( oo.seller_company = ?3 OR oo.consignment_company = ?3 )  " +
            " AND oo.buyer_company = ?4 " +
            " GROUP BY DATE_FORMAT(rr.pay_time,'%Y-%m-%d')", nativeQuery = true)
    List<Map<String, Object>> dealerStatementStatisticsItem(LocalDateTime startTime, LocalDateTime endTime, Long companyId, Long dealersId);
}
