DeviceRepository.php 4.83 KB
<?php

namespace domain\device;

use common\helpers\Log as AppLog;
use domain\device\models\CreateBatch    as CreateBatchModel;
use domain\device\models\Device         as DeviceModel;
use domain\device\models\DeviceStats;

class DeviceRepository
{
    const DEBUG = false;
    private static function myLog($str)
    {
        if (false == self::DEBUG) {
            return '';
        }
        AppLog::DEBUG($str);
    }
    /**
     * @param $where
     * @return array|\yii\db\ActiveRecord[]
     */
    static function getList($where, $limit = 0, $offset = 0)
    {
        $deviceFind = DeviceModel::find();
        $deviceFind->alias('a');
        $deviceFind->select(['a.*','b.batch_no', 'm.name as manufacture','p.name as project','pd.name as production','mo.name as model']);
        $deviceFind->leftJoin(CreateBatchModel::tableName().' b','b.id = a.batch_id');
        $deviceFind->leftJoin('manufacture as m', 'm.id = b.manufacture_id');
        $deviceFind->leftJoin('project as p', 'p.id = b.project_id');
        $deviceFind->leftJoin('model as mo', 'mo.id = b.model_id');
        $deviceFind->leftJoin('production as pd', 'pd.id = b.production_id');

        $deviceFind->where($where);
        $deviceFind->orderBy('created_at desc');
        $deviceFind->asArray();
        if ($offset) {
            $deviceFind->offset($offset);
        }
        if ($limit) {
            $deviceFind->limit($limit);
        }
        $all = $deviceFind->all();

        return $all;
    }

    /**
     * @param $where
     * @return int|string
     */
    static function getListCount($where)
    {
        $deviceFind = DeviceModel::find();
        $deviceFind->alias('a');
        $deviceFind->leftJoin(CreateBatchModel::tableName().' b','b.id = a.batch_id');
        $deviceFind->leftJoin('manufacture as m', 'm.id = b.manufacture_id');
        $deviceFind->leftJoin('project as p', 'p.id = b.project_id');
        $deviceFind->leftJoin('model as mo', 'mo.id = b.model_id');
        $deviceFind->leftJoin('production as pd', 'pd.id = b.production_id');
        $deviceFind->where($where);
        $all = $deviceFind->count();

        return $all;
    }

    /**
     * @param $condition
     * @return null|static
     */
    static function findOne($condition)
    {
        return DeviceModel::findOne($condition);
    }

    /**
     * @param $condition
     * @return array|\yii\db\ActiveRecord[]
     */
    static function findAll($condition)
    {
        $deviceModel = DeviceModel::find();
        $deviceModel->where($condition);
        $list = $deviceModel->all();
        return $list;
    }

    /**
     * @param $condition
     * @return int|string
     */
    static function rowsCount($condition)
    {
        $deviceModel = DeviceModel::find();
        $deviceModel->where($condition);
        $count = $deviceModel->count();
        self::myLog('SQL:'.$deviceModel->createCommand()->rawSql);
        return $count;
    }

    /**
     * 判断是否要登记到授权失败表
     * @param $batchId
     * @param $batchNum
     * @return bool
     */
    static function checkNeedRecordFailRecord($batchId, $batchNum)
    {
        // 第1个情况,全部授权满了
        $authCount = self::rowsCount(['batch_id' => $batchId, 'status' => DeviceStatus::HAS_AUTH, 'is_delete' => 0]);

        if (($authCount *1) >= ($batchNum * 1)) {
            return true;
        }

        // 第2个情况,授权数目 + 删除后恢复数满了
        $where = ['and',
            ['=', 'batch_id', $batchId],
            ['is not', 'device_id', null],
            ['in', 'status', [DeviceStatus::NO_AUTH, DeviceStatus::FAIL_AUTH]],
            ['=', 'is_delete', 0],
        ];
        $restoreCount = self::rowsCount($where);
        if (($restoreCount *1 + $authCount) >= ($batchNum * 1)) {
            return true;
        }

        return false;
    }

    /**
     * 判断是否要自动产生序列号
     * @param $batchId
     * @param $batchNum
     * @return bool
     */
    static function checkNeedAutoGen($batchId, $batchNum)
    {
        $where = [
            'and',
            ['=', 'batch_id' ,$batchId],
            ['=', 'is_delete', 0],
            ['=', 'status', DeviceStatus::NO_AUTH],
            ['is', 'device_id', null]
        ];

        $authCount = self::rowsCount($where);
        self::myLog('checkNeedAutoGen data:'.$authCount.' __'. $batchNum);
        if (empty($authCount)) {
            return true;
        }

        return false;
    }

    /**
     * @param $batchId
     * @return array|null|\yii\db\ActiveRecord
     */
    static function findBatchRandOne($batchId)
    {
        $deviceFind = DeviceModel::find();
        $deviceFind->where(['device_id' => null,'batch_id' => $batchId, 'is_delete' => 0, 'status' => DeviceStatus::NO_AUTH]);
        $deviceFind->orderBy("rand()");
        $nullDevice = $deviceFind->one();

        return $nullDevice;
    }
}