package com.hai.dao;

import com.hai.entity.HighOilCard;
import com.hai.model.OilCardOrderModel;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

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

/**
 * mapper扩展类
 */
public interface HighOilCardMapperExt {

    @Insert({
            "<script>" +
            "insert into high_oil_card (" +
            "company_id, " +
            "company_name, " +
            "org_id," +
            "org_name, " +
            "card_no, " +
            "amount," +
            "amount_consume," +
            "contact_name, " +
            "contact_phone," +
            "status," +
            "bind_status, " +
            "create_time," +
            "update_time, " +
            "op_user_id, " +
            "op_user_name, " +
            "ext_1, " +
            "ext_2, " +
            "ext_3) " +
            "values " +
            "<foreach collection='list' item='item' index='index' separator=','>",
            "(#{item.companyId,jdbcType=BIGINT}, " +
            " #{item.companyName,jdbcType=VARCHAR}, " +
            " #{item.orgId,jdbcType=BIGINT}, " +
            " #{item.orgName,jdbcType=VARCHAR}," +
            " #{item.cardNo,jdbcType=VARCHAR}," +
            " #{item.amount,jdbcType=DECIMAL}, " +
            " #{item.amountConsume,jdbcType=DECIMAL}, " +
            " #{item.contactName,jdbcType=VARCHAR}," +
            " #{item.contactPhone,jdbcType=VARCHAR}, " +
            " #{item.status,jdbcType=INTEGER}," +
            " #{item.bindStatus,jdbcType=INTEGER}," +
            " #{item.createTime,jdbcType=TIMESTAMP}," +
            " #{item.updateTime,jdbcType=TIMESTAMP}, " +
            " #{item.opUserId,jdbcType=BIGINT}," +
            " #{item.opUserName,jdbcType=VARCHAR}, " +
            " #{item.ext1,jdbcType=VARCHAR}," +
            " #{item.ext2,jdbcType=VARCHAR}, " +
            " #{item.ext3,jdbcType=VARCHAR})",
            "</foreach>",
            "</script>"
    })
    void insertList(@Param(value = "list") List<HighOilCard> oilCardList);

    @Select("<script>" +
            " SELECT" +
            " (select org_name from high_oil_card oc where oc.card_no = b.mem_card_no) orgName," +
            " b.mem_phone merPhone," +
            " b.order_no orderNo," +
            " a.gas_order_no gasOrderNo," +
            " b.pay_type payType," +
            " 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 payTypeName," +
            " b.mem_card_no memCardNo," +
            " b.total_price totalPrice," +
            " b.deduction_price deductionPrice," +
            " b.pay_real_price payRealPrice," +
            " a.goods_name goodsName," +
            " a.gas_oil_no gasOilNo," +
            " a.gas_gun_no gasGunNo," +
            " a.gas_price_gun gasPriceGun," +
            " a.ext_1 platformPriceGun," +
            " b.order_status status," +
            " CASE" +
            " b.order_status " +
            " WHEN 1 THEN '待支付' " +
            " WHEN 2 THEN '已支付'" +
            " WHEN 3 THEN '已完成'" +
            " WHEN 4 THEN '已退款'" +
            " WHEN 5 THEN '已取消'" +
            " WHEN 6 THEN '退款中'" +
            " WHEN 6 THEN '退款失败'" +
            " ELSE '未知' " +
            " END statusName," +
            " b.create_time createTime," +
            " b.pay_time payTime," +
            " b.refund_time refundTime" +
            " FROM" +
            " high_child_order a," +
            " high_order b" +
            " WHERE" +
            " a.order_id = b.id " +
            " AND a.goods_type = 3 " +
            " AND b.order_status in (2,3,4,6,7) " +
            " <if test='param.memPhone != null'> AND b.mem_phone like concat('%',#{param.memPhone},'%') </if>" +
            " <if test='param.cardNo != null'> AND b.mem_card_no like concat('%',#{param.cardNo},'%') </if>" +
            " <if test='param.goodsName != null'> AND b.goods_name like concat('%',#{param.goodsName},'%') </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.status != null'> and b.order_status = #{param.status} </if> " +
            " AND EXISTS (select 1 from high_oil_card c where c.company_id = #{param.companyId} " +
            " <if test='param.orgId != null'> and c.org_id = #{param.orgId} </if> " +
            " and b.mem_card_no = c.card_no)" +
            " ORDER BY" +
            " b.pay_time DESC" +
            "</script>")
    List<OilCardOrderModel> selectOilCardOrderList(@Param(value = "param") Map<String, Object> param);

}
