Model.php 2.35 KB
<?php

namespace common\models;

use yii\db\ActiveRecord;
use common\helpers\SqlHelper;
use common\helpers\Utils;
use domain\device\models\Device;

/**
 * 型号
 * This is the model class for table "{{%model}}".
 */
class Model extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%model}}';
    }

    /* @inheritdoc */
    public function init()
    {
        parent::init();
        $this->attachEvents();
    }

    /* @inheritdoc */
    public function attachEvents()
    {
        $this->on(static::EVENT_BEFORE_INSERT, [$this, 'onBeforeInsert']);
    }

    /**
     * 记录插入前的回调处理
     */
    public function onBeforeInsert()
    {
        $this->uuid = Utils::genUUID();
    }

    public function getModelList($offset,$limit,$map)
    {
        $sql = "select m.id,m.model,d.device_name,b.chinese_name,t.name as cat_name from model as m left join device as d on m.device_cat_id=d.device_cat_id and m.id=d.model_id left join brand as b on b.id=m.brand_id
left join device_cat as t on t.id=m.device_cat_id ";
        if(sizeof($map) > 0){
            $sql = $sql." where ".SqlHelper::parseSql($map);
        }
        $sql = $sql." order by m.id desc limit {$offset},$limit ";
        return self::findBySql($sql)->asArray()->all();
    }

    /**
     * 获取总数
     * @return mixed
     */
    public function getModelListCount($map)
    {
        $sql = "select count(m.id) as cnt from model as m left join device as d on m.device_cat_id=d.device_cat_id and m.id=d.model_id left join brand as b on b.id=m.brand_id
left join device_cat as t on t.id=m.device_cat_id ";
        if(sizeof($map) > 0){
            $sql = $sql." where ".SqlHelper::parseSql($map);
        }
        $model =  self::findBySql($sql);
        return  $model->asArray()->one()['cnt'];
    }

    static public function checkLimitModel($modelName)
    {
        if(empty($modelName)){
            return '';
        }
        $modelNameArr = explode('-',$modelName);
        $speedArr = array(Device::HIGH_FLAG,Device::MEDIUM_FLAG,Device::LOW_FLAG,Device::NONE_FLAG);
        if($modelNameArr[0] == 'JIWORK' &&
            (isset($modelNameArr[1]) && in_array($modelNameArr[1],$speedArr))  &&
             isset($modelNameArr[2])
            ){

            return true;
        }else{
            return false;
        }
    }
}