package com.hai.dao.order;

import com.hai.order.model.OrderCouponModel;
import com.hai.order.model.OrderIntegralRechargeModel;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @className: OrderExtMapper
 * @author: HuRui
 * @date: 2022/8/26
 **/
public interface OrderMapperExt {

    /**
     * 查询用户购买卡券数据
     * @param userId
     * @param couponId
     * @return
     */
    @Select("SELECT count(1) FROM high_child_order where mem_id = #{userId} and child_order_status in (2,3) and goods_type = 1 and goods_id = #{couponId}")
    int userBuyCouponNum(@Param("userId") Long userId,@Param("couponId") Long couponId);

    @Select({"<script>" +
            " select" +
            " (select `name` from bs_company WHERE id = d.company_id) companyName," +
            " b.order_no orderNo," +
            " b.mem_phone memPhone," +
            " d.coupon_key couponKey," +
            " (select code_name from sec_dictionary WHERE code_type = 'COUPON_SOURCE' and code_value = d.coupon_source) couponSource," +
            " a.goods_name couponName," +
            " a.goods_price couponPrice," +
            " b.total_price totalPrice," +
            " b.total_deduction_price totalDeductionPrice," +
            " b.deduction_coupon_price deductionCouponPrice," +
            " b.deduction_product_price deductionProductPrice," +
            " b.payable_price payablePrice," +
            " b.pay_gold/100 payGold," +
            " b.pay_price payPrice," +
            " CASE b.pay_type " +
            " WHEN 1 THEN '支付宝' " +
            " WHEN 2 THEN '微信'" +
            " WHEN 3 THEN '积分抵扣'" +
            " WHEN 4 THEN '贵州汇联通工会卡'" +
            " WHEN 5 THEN '银联'" +
            " WHEN 6 THEN '银联分期'" +
            " WHEN 7 THEN '嗨森逛油卡'" +
            " ELSE '未知' END payType," +
            " CASE b.order_status " +
            " WHEN 1 THEN '待支付' " +
            " WHEN 2 THEN '已支付'" +
            " WHEN 3 THEN '已完成'" +
            " WHEN 4 THEN '已退款'" +
            " WHEN 5 THEN '已取消'" +
            " WHEN 6 THEN '退款中'" +
            " WHEN 7 THEN '退款拒绝'" +
            " ELSE '未知' END orderStatus," +
            " b.create_time createTime," +
            " b.pay_time payTime," +
            " b.finish_time finishTime," +
            " case b.exception_status when 1 then '异常' when 0 then '正常' else '未知' end exceptionStatus," +
            " c.cou_no couNo" +
            " from high_child_order a" +
            " LEFT JOIN high_order b on b.id = a.order_id" +
            " LEFT JOIN (select child_order_id, cou_no from high_coupon_code_other where child_order_id is not null GROUP BY child_order_id) c on c.child_order_id = a.id" +
            " LEFT JOIN high_coupon d on d.id = a.goods_id" +
            " where a.goods_type = 1" +
            " <if test='param.companyId != null'> and d.company_id = #{param.companyId} </if>" +
            " <if test='param.goodsId != null'> and a.goods_id in (${param.goodsId}) </if>" +
            " <if test='param.orderNo != null'> and b.order_no like concat('%', #{param.orderNo}, '%') </if>" +
            " <if test='param.memPhone != null'> and b.mem_phone like concat('%', #{param.memPhone}, '%') </if>" +
            " <if test='param.couponKey != null'> and d.coupon_key like concat('%', #{param.couponKey}, '%') </if>" +
            " <if test='param.couponSource != null'> and d.coupon_source = #{param.couponSource} </if>" +
            " <if test='param.couponName != null'> and a.goods_name like concat('%', #{param.couponName}, '%') </if>" +
            " <if test='param.payType != null'> and b.pay_type = #{param.payType} </if>" +
            " <if test='param.orderStatus != null'> and b.order_status = #{param.orderStatus} </if>" +
            " <if test='param.createTimeS != null'> <![CDATA[ and b.create_time >= #{param.createTimeS} ]]> </if>" +
            " <if test='param.createTimeE != null'> <![CDATA[ and b.create_time <= #{param.createTimeE} ]]> </if>" +
            " <if test='param.payTimeS != null'> <![CDATA[ and b.pay_time >= #{param.payTimeS} ]]> </if>" +
            " <if test='param.payTimeE != null'>  <![CDATA[ and b.pay_time <= #{param.payTimeE} ]]> </if>" +
            " <if test='param.finishTimeS != null'>  <![CDATA[ and b.finish_time >= #{param.finishTimeS} ]]> </if>" +
            " <if test='param.finishTimeE != null'>  <![CDATA[ and b.finish_time <= #{param.finishTimeE} ]]> </if>" +
            " <if test='param.exceptionStatus != null'> and b.exception_status = #{param.exceptionStatus} </if>" +
            " ORDER BY b.create_time desc" +
            "</script>"})
    List<OrderCouponModel> queryOrderCoupon(@Param("param") Map<String, Object> param);

    @Select({"<script>" +
            " select" +
            " b.order_no orderNo," +
            " b.mem_phone memPhone," +
            " a.goods_price*100 goodsPrice," +
            " b.total_price totalPrice," +
            " b.total_deduction_price totalDeductionPrice," +
            " b.deduction_coupon_price deductionCouponPrice," +
            " b.deduction_product_price deductionProductPrice," +
            " b.payable_price payablePrice," +
            " b.pay_gold payGold," +
            " b.pay_price payPrice," +
            " CASE b.pay_type " +
            " WHEN 1 THEN '支付宝' " +
            " WHEN 2 THEN '微信'" +
            " WHEN 3 THEN '积分抵扣'" +
            " WHEN 4 THEN '贵州汇联通工会卡'" +
            " WHEN 5 THEN '银联'" +
            " WHEN 6 THEN '银联分期'" +
            " WHEN 7 THEN '嗨森逛油卡'" +
            " ELSE '未知' END payType," +
            " CASE b.order_status " +
            " WHEN 1 THEN '待支付' " +
            " WHEN 2 THEN '已支付'" +
            " WHEN 3 THEN '已完成'" +
            " WHEN 4 THEN '已退款'" +
            " WHEN 5 THEN '已取消'" +
            " WHEN 6 THEN '退款中'" +
            " WHEN 7 THEN '退款拒绝'" +
            " ELSE '未知' END orderStatus," +
            " b.create_time createTime," +
            " b.pay_time payTime" +
            " from high_child_order a" +
            " LEFT JOIN high_order b on b.id = a.order_id" +
            " where a.goods_type = 2 " +
            " <if test='param.orderNo != null'> and b.order_no like concat('%', #{param.orderNo}, '%') </if>" +
            " <if test='param.memPhone != null'> and b.mem_phone like concat('%', #{param.memPhone}, '%') </if>" +
            " <if test='param.payType != null'> and b.pay_type = #{param.payType} </if>" +
            " <if test='param.orderStatus != null'> and b.order_status = #{param.orderStatus} </if>" +
            " <if test='param.createTimeS != null'> <![CDATA[ and b.create_time >= #{param.createTimeS} ]]> </if>" +
            " <if test='param.createTimeE != null'> <![CDATA[ and b.create_time <= #{param.createTimeE} ]]> </if>" +
            " <if test='param.payTimeS != null'> <![CDATA[ and b.pay_time >= #{param.payTimeS} ]]> </if>" +
            " <if test='param.payTimeE != null'> <![CDATA[ and b.pay_time <= #{param.payTimeE} ]]> </if>" +
            " ORDER BY b.create_time desc" +
            "</script>"})
    List<OrderIntegralRechargeModel> queryOrderIntegralRecharge(@Param("param") Map<String, Object> param);

}
