package com.hai.dao;


import com.hai.model.OutOrderModel;
import com.hai.model.OutRechargeOrderModel;
import com.hai.model.OutUserOrderModel;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.type.JdbcType;


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

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

    @Select(value = {
            "<script>",
            "SELECT DATE_FORMAT(create_timed, '%Y-%m-%d') as day,",
            "count(1) as count,",
            "sum(pay_price) as pay_price,",
            "sum(order_price) as order_price,",
            "`status`",
            "from out_recharge_order",
            "where ",
            " create_timed  between #{finishTimeS} and #{finishTimeE}  " ,
            "<if test='status != null'> and status = #{status} </if>",
            "GROUP BY day",
            "</script>"
    })
    @Results({
            @Result(column="count", property="count", jdbcType=JdbcType.BIGINT),
            @Result(column="day", property="day", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="pay_price", property="payPrice", jdbcType=JdbcType.DECIMAL),
            @Result(column="order_price", property="orderPrice", jdbcType=JdbcType.DECIMAL),
            @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
    })
    List<OutOrderModel> getListOrderCount(String finishTimeS , String finishTimeE , Integer status);

    @Select(value = {
            "<script>",
            "select agent_name, count(1) as count, sum(pay_price) as pay_price",
            "from high_agent ha",
            "right join out_recharge_order oro on ha.agent_key = oro.agent_key",
            "where ha.type = 2 and oro.status in (3)",
            " and finish_time  between #{finishTimeS} and #{finishTimeE}  " ,
            "GROUP BY agent_name",
            "</script>"
    })
    @Results({
            @Result(column="count", property="count", jdbcType=JdbcType.BIGINT),
            @Result(column="agent_name", property="agentName", jdbcType=JdbcType.VARCHAR),
            @Result(column="pay_price", property="payPrice", jdbcType=JdbcType.DECIMAL),
    })
    List<OutUserOrderModel> getListUserCount(String finishTimeS , String finishTimeE);
    
    @Select({"<script>" +
                   "SELECT" +
                   " a.order_no orderNo," +
                   " a.user_name userName," +
                   " a.user_phone userPhone," +
                   " case a.recharge_model when 1 then '电信话费' when 2 then '移动话费' when 3 then '联通话费' end rechargeModelName," +
                   " a.recharge_content rechargeContent," +
                   " a.recharge_name rechargeName," +
                   " a.id_card rechargeIdCard," +
                   " a.remarks remarks," +
                   " case a.pay_type when 1 then '微信' when 2 then '工会卡' when 3 then '积分' when 4 then '银联' end payTypeName," +
                   " a.order_price orderPrice," +
                   " a.pay_price payPrice," +
                   " a.pay_real_price payRealPrice," +
                   " case a.status when 1 then '待支付' when 2 then '已支付' when 3 then '已完成' when 4 then '已取消' when 5 then '已退款' end statusName," +
                   " a.create_timed createTime," +
                   " a.pay_time payTime," +
                   " a.cancel_time cancelTime," +
                   " a.finish_time finishTime," +
                   " a.refund_time refundTime," +
                   " a.refund_fee refundFee," +
                   " a.out_refund_no outRefundNo," +
                   " a.refund_id refundId," +
                   " b.card_no cardNo" +
                   " FROM" +
                   " out_recharge_order a join high_user_card b on a.user_id = b.user_id where 1 = 1 " +
                   "<if test='map.status != null'> and a.`status` = #{map.status} </if>",
                   "<if test='map.payType != null'> and a.`pay_type` = #{map.payType} </if>",
                   "<if test='map.code != null'> and a.`Identification_code` = #{map.code} </if>",
                   "<if test='map.orderNo != null'> and a.`order_no` = #{map.orderNo} </if>",
                   "<if test='map.rechargeModel != null'> and a.`recharge_model` = #{map.rechargeModel} </if>",
                   "<if test='map.rechargeContent != null'> and a.`recharge_content` LIKE '%${map.rechargeContent}%'  </if>",
                   "<if test='map.userPhone != null'> and a.`user_phone` = #{map.userPhone} </if>",
                   "<if test='map.createTimeS != null'> <![CDATA[ and a.create_timed >= #{map.createTimeS} ]]> </if>",
                   "<if test='map.createTimeE != null'> <![CDATA[ and a.create_timed <= #{map.createTimeE} ]]> </if>",
                   "<if test='map.payTimeS != null'> <![CDATA[ and a.pay_time >= #{map.payTimeS} ]]> </if>",
                   "<if test='map.payTimeE != null'> <![CDATA[ and a.pay_time <= #{map.payTimeE} ]]> </if>",
                   " ORDER BY" +
                   " a.create_timed DESC" +
                "</script>"
    })
    List<OutRechargeOrderModel> selectOrderCount(@Param("map") Map<String,Object> map);
}
