<?php


namespace App\Modules\Subscribe\Models;
use Illuminate\Database\Eloquent\Model;
use DB;


class Order extends Model
{
    protected $table = 'orders';

    protected $fillable = [
        'uid', 'product_id', 'price', 'status', 'pay_type', 'trade_no', 'send_order_id', 'send_order_name',
        'transaction_id', 'distribution_channel_id', 'pay_end_at', 'create_ip', 'u', 'pay_merchant_source', 'pay_merchant_id', 'from_bid', 'order_type',
        'activity_id','from_type','inner_send_order_id'
    ];

	static function getByTradeNo($trade_no)
	{
		return self::where('trade_no',$trade_no)->first();
	}

	public static  function  getOrderList($uid,$page_size){
	    return self::select('id','price','created_at','status','trade_no')->where('uid',$uid)->orderBy('id','desc')->paginate($page_size);
    }

    /**
     * 订单生成
     */
    static function generate($data)
    {
        //订单号生成
        $data['trade_no'] = $data['product_id'].date("YmdHis").hexdec(uniqid());
        return self::create($data);
    }


    static function save_order($data){

        return self::firstOrCreate($data);
    }

    /**
     * 渠道公众号重充值统计
     */
    static function OfficialAccountOrderStat($distribution_channel_id,$start_time,$end_time,$page_size = 15){
        return self::where('orders.distribution_channel_id',$distribution_channel_id)
            ->where('orders.status','=','PAID')
            ->where('orders.created_at','>=',$start_time)
            ->where('orders.created_at','<',$end_time)
            ->join('force_subscribe_users','force_subscribe_users.uid','=','orders.uid')
            ->groupBy(DB::raw('date(orders.created_at)'))
            ->groupBy('force_subscribe_users.appid')
            ->select(DB::raw('sum(price) as amount'),DB::raw('date(orders.created_at) as `date`'),'force_subscribe_users.appid')
            ->orderBy(DB::raw('date(orders.created_at)'),'desc')
            ->paginate($page_size);
    }


    /**
     * 渠道公众号重充值统计
     */
    static function OfficialAccountOrderSum($distribution_channel_id,$start_time,$end_time,$appid){
$data = DB::select("select sum(price) price from orders where `orders`.`created_at` >= '{$start_time}' and `orders`.`created_at` < '{$end_time}' and `orders`.`status` = 'PAID' and uid in (select uid from force_subscribe_users where `appid` = '{$appid}') and `orders`.`distribution_channel_id` = {$distribution_channel_id}");
return $data ? $data[0]->price : 0;/*
        return self::where('orders.distribution_channel_id',$distribution_channel_id)
            ->join('force_subscribe_users','force_subscribe_users.uid','=','orders.uid')
            ->where('orders.created_at','>=',$start_time)
            ->where('orders.created_at','<',$end_time)
            ->where('orders.status','=','PAID')
            ->where('force_subscribe_users.appid',$appid)
            ->sum('orders.price');*/
    }

    /**
     * 个人充值统计
     */
    static function getRechargeAmountByUidAndTime($uid,$start_time,$end_time){
        return self::where('uid',$uid)
            ->where('orders.status','=','PAID')
            ->where('orders.created_at','>=',$start_time)
            ->where('orders.created_at','<',$end_time)
            ->sum('price');
    }

}
