<?php

namespace app\models\store;

use crmeb\services\UtilService;
use crmeb\traits\ModelTrait;
use crmeb\basic\BaseModel;

/**
 * TODO 弹屏广告Model
 * Class PopScreen
 * @package app\models\store
 */
class PopScreen extends BaseModel
{
	/**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'pop_screen';

    use ModelTrait;

    public static function merSet($mer_id, $alias = '')
    {
        return $mer_id ? self::where($alias ? $alias . '.mer_id' : 'mer_id', $mer_id) : new self;
    }

    /**
     * 弹屏广告列表
     * @param $where
     * @return array
     */
    public static function systemPage($where)
    {
        $model = self::setWhere($where);
        $count = $model->count();
        $list = $model->page((int)$where['page'], (int)$where['limit'])
            ->select()
            ->each(function ($item) {
                if($item['template_id']==0){
                    $item['bg_img'] = image_to_base64("http://img.boofly.cn/pop_bg1.jpg");
                }
                if($item['template_id']==1){
                    $item['bg_img'] = image_to_base64("http://img.boofly.cn/pop_bg2.jpg");
                }
                if($item['template_id']==2){
                    $item['bg_img'] = image_to_base64("http://img.boofly.cn/pop_bg3.jpg");
                }
                if($item['template_id']==3){
                    $item['bg_img'] = image_to_base64("http://img.boofly.cn/pop_bg4.jpg");
                }
                if($item['template_id']==4){
                    $item['bg_img'] = image_to_base64($item['bg_image']);
                }
            });
        return compact('count', 'list');
    }

    /**
     * 设置弹屏广告 where 条件
     * @param $where
     * @param null $model
     * @return mixed
     */
    public static function setWhere($where, $model = null)
    {
        $model = $model === null ? new self() : $model;
        $model = $model->alias('c');
        $model = $model->field('c.*');
        $model = $model->where('c.mer_id', $where['mer_id']);
        if (isset($where['store_name']) && $where['store_name'] != '') $model = $model->where('c.id|c.title', 'LIKE', "%$where[store_name]%");
        return $model->order('c.id desc')->where('c.is_del', 0);
    }

    /**
     * 详情
     */
    public static function getOne($id)
    {
        $info = self::get($id);
        return $info;
    }
}