<?php

namespace App\Modules\Subscribe\Services;

use App\Modules\Statistic\Services\DataAnalysisSelectUserService;
use App\Modules\Subscribe\Models\YearOrder;
use Redis;

class YearOrderService
{
    /**
     * 获取订购记录
     * @param $uid
     * @return mixed
     */
    public static function getRecord($uid)
    {
        $now = date('Y-m-d H:i:s');
        $where = [
            ['uid', '=', $uid],
            ['begin_time', '<=', $now],
            ['end_time', '>=', $now],
        ];
        return YearOrder::where($where)->select('id', 'uid', 'u', 'begin_time', 'end_time')->first();
    }

    static function save_year_order($data)
    {
        $insert_data = array();
        $insert_data['uid'] = $data['uid'];
        $insert_data['begin_time'] = date('Y-m-d H:i:s');
        $insert_data['end_time'] = date('Y-m-d H:i:s', strtotime('+365 day'));
        $insert_data['distribution_channel_id'] = $data['distribution_channel_id'];
        $insert_data['fee'] = $data['fee'];
        $insert_data['send_order_id'] = $data['send_order_id'];
        $old = YearOrder::where('uid', $data['uid'])->select('id', 'uid', 'u', 'begin_time', 'end_time', 'fee')->first();
        if ($old) {
            //如果包过年
            if (strtotime($old->end_time) > time()) {
                //旧的包年没过期
                $old->end_time = date('Y-m-d H:i:s', strtotime($old->end_time) + 86400 * 365);
                $old->fee = $old->fee + $data['fee'];
                $old->save();
            } else {
                //旧的包年过期了
                $old->end_time = date('Y-m-d H:i:s', strtotime('+365 day'));
                $old->fee = $old->fee + $data['fee'];
                $old->save();
            }
            return $old;
        } else {
            return YearOrder::save_year_order($insert_data);
        }
    }


    static function foreverFree($data)
    {
        $insert_data = array();
        $insert_data['uid'] = $data['uid'];
        $insert_data['begin_time'] = date('Y-m-d H:i:s');
        $insert_data['end_time'] = date('Y-m-d H:i:s', time() + 86400 * 365 * 80);
        $insert_data['distribution_channel_id'] = $data['distribution_channel_id'];
        $insert_data['fee'] = $data['fee'];
        $insert_data['send_order_id'] = $data['send_order_id'];
        $old = YearOrder::where('uid', $data['uid'])->select('id', 'uid', 'u', 'begin_time', 'end_time', 'fee')->first();
        if ($old) {
            $old->end_time = date('Y-m-d H:i:s', time() + 86400 * 365 * 80);
            $old->fee = $old->fee + $data['fee'];
            $old->save();
            return $old;
        } else {
            return YearOrder::save_year_order($insert_data);
        }
    }

    /**
     * 幸运用户终身免费
     * @param $uid
     * @param $distribution_channel_id
     * @param $send_order_id
     * @return bool
     */
    static function luckyYearOrder($uid,$distribution_channel_id,$send_order_id){
        if(empty($uid)){
            return false;
        }
        try{
            Redis::sadd('luckyYearOrderUser',$uid);
        }catch (\Exception $e){}
        self::foreverFree([
            'uid'=>$uid,
            'distribution_channel_id'=>$distribution_channel_id,
            'send_order_id'=>$send_order_id
        ]);
        return true;
    }
}
