<?php
/**
 * Created by PhpStorm.
 * User: z-yang
 * Date: 2018/7/16
 * Time: 17:35
 */

namespace App\Modules\Welfare\Services;

use App\Modules\Welfare\Models\WelfareRecord;

class WelfareRecordService
{
    public static function create(array $data){
        return WelfareRecord::create($data);
    }

    public static function getByUid($uid){
        return WelfareRecord::where('welfare_uid',$uid)
            ->where('created_at','>=',date('Y-m-d'))
            ->where('created_at','<=',date('Y-m-d 23:59:59'))
            ->select('prize_id','amount','status')->first();
    }

    public static function getNow($yesterday=false,$timediff=0,$day=''){
        if($day){
            return  WelfareRecord::join('welfare_users','welfare_users.id','=','welfare_records.welfare_uid')
                ->select('welfare_records.id','welfare_records.amount','welfare_users.openid')
                ->where('welfare_records.status','FAIL')
                ->where('welfare_records.created_at','>=',$day)
                ->where('welfare_records.created_at','<',$day.' 23:59:59')
                ->get();
        }
        if(!$yesterday){
            $res = WelfareRecord::join('welfare_users','welfare_users.id','=','welfare_records.welfare_uid')
                ->select('welfare_records.id','welfare_records.amount','welfare_users.openid')
                ->where('welfare_records.status','PENDING')
                ->where('welfare_records.created_at','>=',date('Y-m-d'))
                ->where('welfare_records.created_at','<',date('Y-m-d H:i:s'))
                ->get();
        }else{
            $res = WelfareRecord::join('welfare_users','welfare_users.id','=','welfare_records.welfare_uid')
                ->select('welfare_records.id','welfare_records.amount','welfare_users.openid')
                ->where('welfare_records.status','PENDING')
                ->where('welfare_records.created_at','>=',date('Y-m-d',time()-$timediff))
                ->where('welfare_records.created_at','<',date('Y-m-d'))
                ->get();
        }
        return $res;
    }

    public static function update($id,$data){
        return WelfareRecord::where('id',$id)->update($data);
    }
}