<?php


namespace App\Web\Models;


use App\Models\BaseModel;


class WebUtmModel extends BaseModel
{
    /**
     * 状态字段
     */
    const DELETED_AT = 'status';
    /**
     * @var string
     */
    protected $table = 'web_utm';

    public function addWebUtm($userId, $params)
    {
        $data = [
            'user_id' => $userId,
            'website_url' => $params['website_url'],
            'short_url' => $params['short_url'] ?? '',
            'campaign_id' => $params['campaign_id'],
            'campaign_name' => $params['campaign_name'],
            'campaign_source' => $params['campaign_source'],
            'campaign_term' => $params['campaign_term'],
            'campaign_medium' => $params['campaign_medium'],
            'campaign_content' => $params['campaign_content'],
            'pv' => 0,
            'uv' => 0,
            'status' => 0,
            'create_time' => nowTime(),
            'update_time' => nowTime(),
        ];
        return $this->insertGetId($data);
    }

    public function updWebUtm($id, $params)
    {
        $update = ['update_time' => nowTime()];
        if (isset($params['campaign_id'])) {
            $update['campaign_id'] = $params['campaign_id'];
        }
        if (isset($params['campaign_name'])) {
            $update['campaign_name'] = $params['campaign_name'];
        }
        if (isset($params['campaign_source'])) {
            $update['campaign_source'] = $params['campaign_source'];
        }
        if (isset($params['campaign_term'])) {
            $update['campaign_term'] = $params['campaign_term'];
        }
        if (isset($params['campaign_medium'])) {
            $update['campaign_medium'] = $params['campaign_medium'];
        }
        if (isset($params['campaign_content'])) {
            $update['campaign_content'] = $params['campaign_content'];
        }
        return $this->where('id', $id)->update($update);
    }

    public function updUtmPvUv($id, $pv, $uv)
    {
        return $this->where('id', $id)->update(['pv' => $pv, 'uv' => $uv]);
    }

    public function delWebUtm($ids)
    {
        if (!is_array($ids)) {
            $ids = [$ids];
        }
        return $this->whereIn('id', $ids)->update(['status' => 2]);
    }

    public function getWebUtm($id)
    {
        $data = $this->where('id', $id)->where('status', 0)->first();
        return $data ? $data->toArray() : [];
    }

    public function getWebUtmByShortUrl($shortUrl)
    {
        $data = $this->where('short_url', $shortUrl)->where('status', 0)->first();
        return $data ? $data->toArray() : [];
    }

    public function getWebUtmList($campaignName = '', $campaignSource = '', $campaignMedium = '', $pageSize = 10, $orderBy = '', $orderDirect = '')
    {
        $where = ['status' => 0];
        $query = $this->where($where);
        if (!empty($campaignName)) {
            $query->where('campaign_name', 'like', "%" . addslashes($campaignName) . "%");
        }
        if (!empty($campaignSource)) {
            $query->where('campaign_source', 'like', "%" . addslashes($campaignSource) . "%");
        }
        if (!empty($campaignMedium)) {
            $query->where('campaign_medium', 'like', "%" . addslashes($campaignMedium) . "%");
        }
        if ($orderBy && $orderDirect) {
            $query = $query->orderBy($orderBy, $orderDirect);
        } else {
            $query = $query->orderBy('id', 'desc');
        }
        return $query->paginate($pageSize)->toArray();
    }

    public function getUtmByParams($params)
    {
        $where = ['status' => 0];
        $query = $this->where($where);
        if (!empty($params['campaign_id'])) {
            $query->where('campaign_id', '=', urldecode($params['campaign_id']));
        }
        if (!empty($params['campaign_name'])) {
            $query->where('campaign_name', '=', urldecode($params['campaign_name']));
        }
        if (!empty($params['campaign_source'])) {
            $query->where('campaign_source', '=', urldecode($params['campaign_source']));
        }
        if (!empty($params['campaign_term'])) {
            $query->where('campaign_term', '=', urldecode($params['campaign_term']));
        }
        if (!empty($params['campaign_medium'])) {
            $query->where('campaign_medium', '=', urldecode($params['campaign_medium']));
        }
        if (!empty($params['campaign_content'])) {
            $query->where('campaign_content', '=', urldecode($params['campaign_content']));
        }
        $data = $query->orderBy('id', 'desc')->first();
        return $data ? $data->toArray() : [];
    }

    public function getWebUtmByPage($page = 1, $pageSize = 10)
    {
        $offset = ($page - 1) * $pageSize;
        $where = ['status' => 0];
        $query = $this->where($where);
        return $query->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageSize)
            ->selectRaw('id')
            ->get()
            ->toArray();
    }

}
