<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2017/11/21
 * Time: 10:42
 */

namespace App\Modules\User\Models;

use Illuminate\Database\Eloquent\Model;
use DB;

class User extends Model
{
    protected $table = 'users';

    protected $fillable =
        ['id', 'openid', 'unionid', 'distribution_channel_id', 'head_img',
            'register_ip', 'send_order_id', 'balance', 'sex', 'country', 'city',
            'province', 'nickname', 'charge_balance', 'reward_balance', 'is_new', 'nickname'
        ];

    /**
     * 获取用户信息列表
     * @param $pageSize 每页显示的条数
     */
    static function getUserList($limit = 10000, $offset = 0)
    {
        return self::orderBy('id', 'asc')->limit($limit)->offset($offset)->get();
    }

    /**
     * 获取用户信息列表
     * @param $pageSize 每页显示的条数
     */
    static function getPaginationList($pageSize = 20, $careStatus = 0)
    {
        if (0 == $careStatus) {
            return self::orderBy('id', 'desc')->paginate($pageSize);
        } elseif (1 == $careStatus) {
            return self::where('head_img', '!=', '')->orderBy('id', 'desc')->paginate($pageSize);
        } elseif (2 == $careStatus) {
            return self::where('head_img', '=', '')->orderBy('id', 'desc')->paginate($pageSize);
        }
    }


    /**
     * 根据id获取用户信息
     */
    static function getById($id)
    {
        return self::find($id);
    }

    /**
     * 根据id获取用户信息
     */
    static function getUserByNickAndChannelId($distribution_channel_id, $nickname)
    {
        return self::where(['distribution_channel_id' => $distribution_channel_id, 'nickname' => $nickname])->first();
    }


    /**
     * 根据id获取用户信息
     */
    static function getUserDataById($id)
    {
        return self::where('id', $id)->get();
    }

    /**
     * 创建用户
     */
    static function addUser($params)
    {
        $openid = isset($params['openid']) ? $params['openid'] : '';
        $unionid = isset($params['unionid']) ? $params['unionid'] : '';
        $distribution_channel_id = isset($params['distribution_channel_id']) ? $params['distribution_channel_id'] : '';
        if ($openid && $unionid && $distribution_channel_id) {
            $province = isset($params['province']) ? $params['province'] : '';
            $city = isset($params['city']) ? $params['city'] : '';
            $country = isset($params['country']) ? $params['country'] : '';
            $head_img = isset($params['headimgurl']) ? $params['headimgurl'] : '';
            $send_order_id = isset($params['send_order_id']) ? $params['send_order_id'] : 0;
            $sex = isset($params['sex']) ? $params['sex'] : 0;
            $register_ip = isset($params['register_ip']) ? $params['register_ip'] : '';
            $is_new = isset($params['is_new']) ? $params['is_new'] : 0;
            $nickname = isset($params['nickname']) ? $params['nickname'] : '';

            $base_data = compact('unionid', 'distribution_channel_id');
            $extra_data = compact('openid', 'province', 'city', 'country', 'head_img', 'send_order_id', 'sex', 'register_ip', 'is_new', 'nickname');
            return self::firstOrCreate($base_data, $extra_data);
        }

    }

    /**
     * 更新用户
     */
    static function updateInfo($id, $params = [])
    {
        return self::where('id', $id)->update($params);
    }

    static function getUser($uid)
    {
        return self::where('id', $uid)->first();
    }

    /**
     * 用户余额添加
     * @param $uid
     * @param $fee
     * @return mixed
     */
    static function addBalance($uid, $fee, $charge, $given)
    {
        $res1 = User::where('id', $uid)->increment('balance', $fee);
        $res2 = User::where('id', $uid)->increment('charge_balance', $charge);
        if ($given) User::where('id', $uid)->increment('reward_balance', $given);
        return $res1 && $res2;
    }

    //获取推广用户总数
    static function getPromotionTotal($send_order_id)
    {
        return self::where('send_order_id', $send_order_id)->count();
    }

    /**
     * 查询渠道某天注册用户总数
     * @param $channel_id
     * @param $date
     * @return mixed
     */
    static function getChannelDayTotal($channel_id, $date)
    {
        $end_date = date('Y-m-d', strtotime($date) + 86400);
        return self::where('distribution_channel_id', $channel_id)->where('created_at', '>=', $date)->where('created_at', '<', $end_date)->count();
    }

    /**
     * 查询渠道注册用户总数
     * @param $channel_id
     * @return mixed
     */
    static function getChannelTotal($channel_id)
    {
        return self::where('distribution_channel_id', $channel_id)->count();
    }

    /**
     * 查询渠道某段时间注册用户总数
     * @param $channel_id
     * @param $startDate
     * @param $endDate
     * @return mixed
     */
    static function getChannelDayToDayTotal($channel_id, $startDate = '', $endDate = '')
    {
        $searchObj = self::whereIn('distribution_channel_id', $channel_id);
        if ($startDate) {
            $searchObj->where('created_at', '>=', $startDate);
        }
        if ($endDate) {
            $searchObj->where('created_at', '<', $endDate);
        }

        return $searchObj->count();
    }

    /**
     * 查询渠道某段时间注册用户总数
     * @param array $channelIds
     * @param $startDate
     * @param $endDate
     * @return mixed
     */
    static function getChannelsDayToDayTotal($channelIds, $startDate = '', $endDate = '')
    {
        $searchObj = DB::table(DB::raw('users use index(distribution_channel_created)'))->whereIn('distribution_channel_id', $channelIds);
        //$searchObj = self::whereIn('distribution_channel_id', $channelIds);
        if ($startDate) {
            $searchObj->where('created_at', '>=', $startDate);
        }
        if ($endDate) {
            $searchObj->where('created_at', '<', $endDate);
        }
        //\Log::info($searchObj->toSql());
        return $searchObj->count();
    }

    /**
     * 查询注册用户总数
     * @param $params
     */
    static function getTotalCount($params)
    {
        $searchObj = self::orderBy('id', 'desc');
        if (isset($params['begin_time'])) $searchObj->where('created_at', '>=', $params['begin_time']);
        if (isset($params['end_time'])) $searchObj->where('created_at', '<=', $params['end_time']);

        return $searchObj->count();
    }

    static function search($params)
    {
        $searchObj = self::orderBy('id', 'desc');
        if (isset($params['begin_time'])) $searchObj->where('created_at', '>=', $params['begin_time']);
        if (isset($params['end_time'])) $searchObj->where('created_at', '<=', $params['end_time']);
        return $searchObj;
    }

    static function getIdIndex($params)
    {
        $searchObj = self::orderBy('id', 'desc');
        if (isset($params['begin_time'])) $searchObj->where('created_at', '>=', $params['begin_time']);
        if (isset($params['end_time'])) $searchObj->where('created_at', '<=', $params['end_time']);

        $searchObj->select(
            DB::raw('max(id) max_id'),
            DB::raw('min(id) min_id')
        );
        return $searchObj->first();
    }

    static function getUserDataDetailById($id)
    {
        return self::where('users.id', $id)
            ->select(DB::raw("users.*,year_orders.begin_time,year_orders.end_time"))
            ->leftjoin('year_orders', 'year_orders.uid', '=', 'users.id')
            ->get();
    }
}