<?php
/**
 * Created by PhpStorm.
 * User: Hardy
 * Date: 2020/7/31
 * Time: 9:52
 */


namespace App\Modules\SendOrder\Models;


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

class SendOrderByRegist extends Model
{
    protected $table = 'send_orders_by_regist';
    protected $fillable = [
        'id',
        //send_orders.'send_time',//实际发送时间
        'send_order_id',//渠道ID
        //send_orders.'book_id',//推广书籍ID
        //send_orders.'book_name',//推广图书名称
        //send_orders.'is_enable',
        //send_orders_stats.register_user_num
        //send_orders_stats.uv
        //send_orders_stats.force_user_num
        //send_orders.cost
        'first_pay_count',//首充用户数
        'first_pay_amount',//首充总额
        'success_user_count',//付费人数
        'success_order_count',//付费次数
        'total_amount',//充值总额
        'hour_24',//24小时充值金额
        'hour_24_first_count',//24小时首充用户数
        'hour_24_not_first_count',//24小时非首充用户数
        'day_3',//3天充值金额
        'day_3_first_count',//3天首充用户数
        'day_3_not_first_count',//3天非首充用户数
        //'day_3_rate',//3天复充率 = day_3_first_count/day_3_not_first_count
        'day_7',//7天充值金额
        'day_7_first_count',//7天首充用户数
        'day_7_not_first_count',//7天非首充用户数
        'created_at',
        'updated_at',
    ];

    function search($params, $isAll = false)
    {
        $fields        = ['*'];
        $search_object = self::select($fields);

        if (isset($params['send_order_id']) && $params['send_order_id']) {
            $search_object->where('send_order_id', $params['send_order_id']);
        }

        if (isset($params['start_time']) && $params['start_time']) {
            $search_object->where('send_time', '>=', $params['start_time']);
        }

        if (isset($params['end_time']) && $params['end_time']) {
            $search_object->where('send_time', '<=', $params['end_time']);
        }


        if ($isAll) {
            return $search_object->get();
        } else {
            return $search_object->paginate();
        }
    }

    function getSendOrderIdFromOrigin($start, $end)
    {
        $result = DB::Table('orders')->join('users', 'orders.uid', 'users.id')
            ->where('orders.created_at', '>=', $start)
            ->where('orders.created_at', '<', $end)
            ->where('orders.status', '=', 'paid')
            ->where('users.send_order_id', '>', 0)
            ->select(DB::raw('distinct(users.send_order_id)'))
            ->get();

        return $result ? $result->toArray() : [];
    }

    /**
     * 删除记录
     * @param $ids
     */
    function deleteRecs($ids=[])
    {
        if(count($ids)<1)
            return;
        $idstr  = collect($ids)->implode('send_order_id', ',');

        $result = self::whereIn('send_order_id', $ids)
            ->delete();
    }

    /**
     * @param $params
     * @param bool $isAll
     * @return mixed
     */
    function getSendOrderByRegistList($params, $isAll = false)
    {
        $fields        = ["send_orders.id", "send_orders.distribution_channel_id", "distribution_channels.nickname",
            "send_orders.send_time", "send_orders.book_id", "send_orders.book_name", "send_orders_stats.register_user_num",
            "send_orders_stats.uv", "send_orders_stats.force_user_num", "send_orders.cost",
            "send_orders_by_regist.success_user_count", "send_orders_by_regist.success_order_count",
            "send_orders_by_regist.total_amount", "send_orders_by_regist.first_pay_count", "send_orders_by_regist.first_pay_amount",
            "send_orders_by_regist.hour_24", "send_orders_by_regist.hour_24_first_count", "send_orders_by_regist.hour_24_not_first_count",
            "send_orders_by_regist.day_3", "send_orders_by_regist.day_3_first_count", "send_orders_by_regist.day_3_not_first_count",
            "send_orders_by_regist.day_7", "send_orders_by_regist.day_7_first_count", "send_orders_by_regist.day_7_not_first_count"];
        $search_object = self::join('send_orders', 'send_orders.id', 'send_orders_by_regist.send_order_id')
            ->join('send_orders_stats', 'send_orders.id', 'send_orders_stats.send_order_id')
            ->join('distribution_channels', 'send_orders.distribution_channel_id', 'distribution_channels.id')
            ->where('send_orders_by_regist.first_pay_count', '>', 0)
            ->select($fields);

        if (isset($params['send_order_id']) && $params['send_order_id']) {
            $search_object->where('send_orders.id', $params['send_order_id']);
        }

        if (isset($params['start_time']) && $params['start_time']) {
            $search_object->where('send_orders.send_time', '>=', $params['start_time']);
        }

        if (isset($params['end_time']) && $params['end_time']) {
            $search_object->where('send_orders.send_time', '<=', $params['end_time']);
        }

        if (isset($params['company_name']) && $params['company_name']) {
            $search_object->where('distribution_channels.nickname', '=', $params['company_name']);
        }

        if (isset($params['book_id']) && $params['book_id']) {
            $search_object->where('send_orders.book_id', '=', $params['book_id']);
        }
        if (isset($params['book_name']) && $params['book_name']) {
            $search_object->where('send_orders.book_name', '=', $params['book_name']);
        }

        if (isset($params['uv_min']) && $params['uv_min']) {
            $search_object->where('send_orders.uv', '>=', $params['uv_min']);
        }

        if (isset($params['uv_max']) && $params['uv_max']) {
            $search_object->where('send_orders.uv', '<=', $params['uv_max']);
        }


        if ($isAll) {
            return $search_object->get();
        } else {
            return $search_object->paginate();
        }
    }
}