package com.czg.service.market.mapper;

import com.czg.account.entity.HandoverRecord;
import com.czg.account.vo.HandoverCategoryListVo;
import com.czg.account.vo.HandoverProductListVo;
import com.czg.order.entity.OrderInfo;
import com.mybatisflex.core.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

/**
 * 订单表 映射层。
 *
 * @author ww
 * @since 2025-02-13
 */
public interface OrderInfoMapper extends BaseMapper<OrderInfo> {

    /**
     * 交班售出商品明细
     *
     * @param shopId       店铺id
     * @param loginTime    上岗时间
     * @param handoverTime 交班时间
     * @return 交班售出商品明细
     */
    List<HandoverProductListVo> getHandoverDetailList(Long shopId, String loginTime, String handoverTime);

    /**
     * 交班售出商品分类统计
     *
     * @param shopId       店铺id
     * @param loginTime    上岗时间
     * @param handoverTime 交班时间
     * @return 售出商品分类统计
     */
    List<HandoverCategoryListVo> getHandoverCategoryList(Long shopId, String loginTime, String handoverTime);

    int decrMoney(@Param("id") Long id, @Param("amount") BigDecimal amount);

    int updatePayOrderId(@Param("orderId") Long orderId, @Param("paymentId") Long paymentId, @Param("payType") String payType, @Param("remark") String remark);


    /**
     * 订单支付方式统计 当日实时数据
     */
    @Select("SELECT" +
            " SUM(tb_order_info.pay_amount) AS orderTurnover," +
            " SUM(CASE WHEN pay_type = 'main_scan' THEN pay_amount ELSE 0 END) AS selfScan," +
            " SUM(CASE WHEN pay_type = 'back_scan' THEN pay_amount ELSE 0 END) AS barScan," +
            " SUM(CASE WHEN pay_type = 'wechat_mini' THEN pay_amount ELSE 0 END) AS wechat," +
            " SUM(CASE WHEN pay_type = 'alipay_mini' THEN pay_amount ELSE 0 END) AS alipay," +
            " SUM(CASE WHEN pay_type = 'vip_pay' THEN pay_amount ELSE 0 END) AS balance," +
            " SUM(CASE WHEN pay_type = 'cash_pay' THEN pay_amount ELSE 0 END) AS cash," +
            " SUM(CASE WHEN pay_type = 'credit_pay' THEN pay_amount ELSE 0 END) AS owed," +

            " IFNULL(SUM(refund_amount), 0) as refundAmount," +
            " count(1) as orderCount " +
            " FROM" +
            " tb_order_info " +
            " WHERE" +
            " shop_id = #{shopId} " +
            "and create_time >= #{loginTime} and create_time <= #{handoverTime} " +
            "and paid_time is not null ")
    HandoverRecord getOnlinePayTypeDate(Long shopId, String loginTime, String handoverTime);

    /**
     * 订单退菜数量
     */
    @Select("SELECT" +
            "  SUM(o.return_num) AS returnDishCount " +
            " FROM" +
            "  tb_order_info left join tb_order_detail o on tb_order_info.id = o.order_id" +
            " WHERE" +
            " tb_order_info.shop_id = #{shopId} " +
            "and tb_order_info.create_time >= #{loginTime} and tb_order_info.create_time <= #{handoverTime} " +
            "and tb_order_info.paid_time is not null ")
    BigDecimal countReturnDish(Long shopId, String loginTime, String handoverTime);

    @Select("SELECT " +
            "    SUM(CASE WHEN biz_code IN ('cashIn', 'wechatIn', 'alipayIn', 'adminIn','freeIn') THEN amount ELSE 0 END) AS recharge," +
            "    SUM(CASE WHEN biz_code IN ('rechargeRefund') THEN amount ELSE 0 END) AS refundAmount " +
            "FROM `tb_shop_user_flow` " +
            " WHERE" +
            " shop_id = #{shopId} " +
            "and create_time >= #{loginTime} and create_time <= #{handoverTime} ")
    HandoverRecord countShopUserFlow(Long shopId, String loginTime, String handoverTime);
}
