<?php

namespace App\Modules\OfficialAccount\Models;

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

class CustomSendMsgs extends Model
{
	protected $connection = 'api_mysql';
	
    protected $tables = 'custom_send_msgs';

    protected $fillable = ['task_id', 'user_num', 'appid', 'name', 'send_time', 'content', 'redirect_url', 'status', 'distribution_channel_id', 'subscribe_time', 'sex', 'balance', 'order_type', 'category_id', 'del_flag', 'is_full_send', 'trusteeship', 'batch_no', 'is_show_list', 'description', 'book_name', 'chapter_name', 'is_activity', 'custom_type'];


    /**
     * 判断1小时内是否有相同的客服消息插入过
     */
    static function isSendCustomerAtSameTime($distribution_channel_id, $appid, $send_time)
    {

        return self::where(['distribution_channel_id' => $distribution_channel_id, 'appid' => $appid, 'del_flag' => 0])->where('status', '!=', '4')->where('send_time', '<', date('Y-m-d H:i:s', strtotime($send_time) + 3600))->where('send_time', '>', date('Y-m-d H:i:s', strtotime($send_time) - 3600))->first();

    }

    /**
     * 判断1小时内是否有相同的客服消息插入过
     */
    static function isSendCustomerAtSameTimeAndSex($distribution_channel_id, $appid, $send_time, $sex)
    {

        return self::where(['distribution_channel_id' => $distribution_channel_id, 'appid' => $appid, 'sex' => $sex, 'del_flag' => 0])->where('status', '!=', '4')->where('send_time', '<', date('Y-m-d H:i:s', strtotime($send_time) + 3600))->where('send_time', '>', date('Y-m-d H:i:s', strtotime($send_time) - 3600))->first();

    }


    /**
     * 根据渠道号获取客服消息
     */
    static function customSendMsgsByChannelId($distribution_channel_id)
    {

        return self::where('distribution_channel_id', isset($distribution_channel_id) ? $distribution_channel_id : '')->where('del_flag', 0)->where('is_show_list', 1)->orderBy('id', 'desc')->paginate();

    }

    /**
     * 根据渠道号和自定义标识获取客服消息
     */
    static function customSendMsgsByChannelIdAndTrusteeship($distribution_channel_id, $trusteeship = 1)
    {

        return self::where('distribution_channel_id', $distribution_channel_id)->where('trusteeship', $trusteeship)->where('del_flag', 0)->orderBy('id', 'desc')->paginate();

    }

    /**
     * 根据自定义标识获取所有批次客服消息
     */
    static function customSendMsgsByTrusteeship($trusteeship = 1)
    {

        return self::where('trusteeship', $trusteeship)->where('del_flag', 0)->select('custom_send_msgs.*', 'custom_send_msgs.created_at as create_time')->orderBy('batch_no', 'desc')->orderBy('id', 'desc')->paginate();

    }

    /**
     * 根据批次号获取客服消息
     */
    static function customSendMsgsByTrusteeshipAndBacthNo($trusteeship = 1, $batch_no)
    {

        return self::where('trusteeship', $trusteeship)->where('batch_no', $batch_no)->where('del_flag', 0)->orderBy('id', 'desc')->get();

    }


    /**
     * 根据task_id获取客服消息
     */
    static function customSendMsgsByTaskid($task_id)
    {

        return self::where('task_id', isset($task_id) ? $task_id : '')->first();

    }

    /**
     * 根据id获取客服消息
     */
    static function customSendMsgsById($id)
    {

        return self::where('id', isset($id) ? $id : '')->first();

    }

    /**
     * 根据状态获取客服消息
     */
    static function customSendMsgsByStatusStandBy($status)
    {

        return self::where(['status' => $status, 'del_flag' => 0])->get();

    }

    /**
     * 根据分页获取客服消息
     */
    static function customSendMsgsPaginate($start_time, $end_time, $is_all = false)
    {
        $search_obj = self::select('custom_send_msgs.distribution_channel_id as site_id',
            'custom_send_msgs.name as task_name',
            DB::raw('date(custom_send_msgs.created_at) as created_date'),
            'custom_send_msgs.send_time',
            'custom_send_msgs.user_num as suc_send_num',
            DB::raw('"" as template_name'),
            DB::raw('"" as tag'),
            DB::raw('(select group_concat(nickname) from official_accounts where distribution_channel_id=custom_send_msgs.distribution_channel_id group by distribution_channel_id)  as  official_account_name')//official_account_name
        )
            ->where(['del_flag' => 0, 'status' => 3])
            ->where('custom_send_msgs.created_at', '>=', $start_time)
            ->where('custom_send_msgs.created_at', '<', $end_time)
            ->orderBy('id', 'desc');

        if ($is_all) {
            return $search_obj->get();
        } else {
            return $search_obj->paginate();
        }
    }

    static function searchCustomSendMsgs($param, $is_all = false)
    {

        $res = self::where('distribution_channel_id', $param['distribution_channel_id'])
            ->select('*')
            ->where('del_flag', 0)
            ->where('is_show_list', 1);

        if (isset($param['start_time']) && $param['start_time']) {
            $res->where('custom_send_msgs.created_at', '>=', $param['start_time']);
        }
        if (isset($param['end_time']) && $param['end_time']) {
            $res->where('custom_send_msgs.created_at', '<=', $param['end_time']);
        }
        if (isset($param['book_name']) && $param['book_name']) {
            $res->where('custom_send_msgs.book_name', 'like', '%' . $param['book_name'] . '%');
        }
        if (isset($param['task_name']) && $param['task_name']) {
            $res->where('custom_send_msgs.name', 'like', '%' . $param['task_name'] . '%');
        }

        if ($is_all) {
            $custom_send_msgs = $res->orderBy('id', 'desc')->get();
        } else {
            $custom_send_msgs = $res->orderBy('id', 'desc')->paginate();
        }
        return $custom_send_msgs;
    }


}


