CreateBatchRepository.php 5.43 KB
<?php

namespace domain\device;

use yii\db\Expression;
use yii\db\Query;
use domain\device\models\CreateBatch as CreateBatchModel;
use domain\device\models\Device as DeviceModel;
use domain\device\models\DeviceStats;
use domain\manufacturer\models\Manufacturer as ManufacturerModel;
use domain\model\models\Model as ModelModel;
use domain\production\models\Production as ProductionModel;
use domain\project\models\Project as ProjectModel;


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

    /**
     * @param $type
     * @param $keyword
     * @return array
     */
    static function getSerialNoComponent($type, $keyword)
    {
        if ('manufacture' == $type) {
            $q = new Query();
            $q->select('id,manufacture_no, name');
            $q->from('manufacture');
            $q->where('name like "%'.$keyword.'%" or manufacture_no like "%'.$keyword.'%"');
            $q->limit(20);
            $list = $q->all();

            return $list;
        } elseif('project' == $type) {
            $q = new Query();
            $q->select('id,project_no, name');
            $q->from('project');
            $q->where('name like "%'.$keyword.'%" or project_no like "%'.$keyword.'%"');
            $q->limit(20);
            $list = $q->all();

            return $list;
        } elseif ('model' == $type) {
            $q = new Query();
            $q->select('id,model_no, name');
            $q->from('model');
            $q->where('name like "%'.$keyword.'%" or model_no like "%'.$keyword.'%"');
            $q->limit(20);
            $list = $q->all();

            return $list;
        } elseif ('production' == $type) {
            $q = new Query();
            $q->select('id,production_no, name');
            $q->from('production');
            $q->where('name like "%'.$keyword.'%" or production_no like "%'.$keyword.'%"');
            $q->limit(20);
            $list = $q->all();

            return $list;
        } else {
            return [];
        }
    }

    /**
     * @param $where
     * @param $offset
     * @param $limit
     * @return array|\yii\db\ActiveRecord[]
     */
    static function getPageList($where, $offset = 0, $limit = 0)
    {
        $hasAuthNumExpress = new Expression("(select count(*) from ".DeviceModel::tableName(). "as dd where dd.status =".DeviceStatus::HAS_AUTH.' and dd.batch_id = a.id and dd.is_delete = 0) as has_auth_num');
        $noAuthNumExpress = new Expression("(select count(*) from ".DeviceModel::tableName(). "as dd where dd.status =".DeviceStatus::NO_AUTH.' and dd.batch_id = a.id and dd.is_delete = 0) as del_num');

        $delNumExpress = new Expression("(select count(*) from ".DeviceModel::tableName(). "as dd where dd.is_delete = 1 and dd.batch_id = a.id) as del_num");

        $batchModelFind = CreateBatchModel::find();
        $batchModelFind->alias('a');
        $batchModelFind->select(['a.*','m.name as manufacture_name','pro.name as project_name','mo.name as model_name','prod.name as production_name', $hasAuthNumExpress, $noAuthNumExpress, $delNumExpress]);
        $batchModelFind->leftJoin(ManufacturerModel::tableName(). ' m', 'm.id = a.manufacture_id');
        $batchModelFind->leftJoin(ProjectModel::tableName(). ' pro', 'pro.id = a.project_id');
        $batchModelFind->leftJoin(ModelModel::tableName(). ' mo', 'mo.id = a.model_id');
        $batchModelFind->leftJoin(ProductionModel::tableName(). ' prod', 'prod.id = a.production_id');
        $batchModelFind->where($where);
        $batchModelFind->asArray();
        if ($offset) {
            $batchModelFind->offset($offset);
        }
        if ($limit) {
            $batchModelFind->limit($limit);
        }
        $batchModelFind->orderBy('created_at desc');
        $all = $batchModelFind->all();

        return $all;
    }

    /**
     * @param $where
     * @return int|string
     */
    static function getPageCount($where)
    {
        $batchModelFind = CreateBatchModel::find();
        $batchModelFind->alias('a');
        $batchModelFind->leftJoin(ManufacturerModel::tableName(). ' m', 'm.id = a.manufacture_id');
        $batchModelFind->leftJoin(ProjectModel::tableName(). ' pro', 'pro.id = a.project_id');
        $batchModelFind->leftJoin(ModelModel::tableName(). ' mo', 'mo.id = a.model_id');
        $batchModelFind->leftJoin(ProductionModel::tableName(). ' prod', 'prod.id = a.production_id');
        $batchModelFind->where($where);
        $count= $batchModelFind->count();

        return $count;
    }

    /**
     * @param $batchId
     * @return array|null|\yii\db\ActiveRecord
     */
    static function getBatchInfo($batchId)
    {
        $batchModelFind = CreateBatchModel::find();
        $batchModelFind->alias('a');
        $batchModelFind->select(['a.*','m.name as manufacture','pro.name as project','mo.name as model','prod.name as production']);
        $batchModelFind->leftJoin(ManufacturerModel::tableName(). ' m', 'm.id = a.manufacture_id');
        $batchModelFind->leftJoin(ProjectModel::tableName(). ' pro', 'pro.id = a.project_id');
        $batchModelFind->leftJoin(ModelModel::tableName(). ' mo', 'mo.id = a.model_id');
        $batchModelFind->leftJoin(ProductionModel::tableName(). ' prod', 'prod.id = a.production_id');
        $batchModelFind->where(['a.id' => $batchId]);
        $batchModelFind->asArray();
        $info = $batchModelFind->one();

        return $info;
    }
}