CreateBatchRepository.php 7.18 KB
<?php

namespace domain\device;

use domain\device\models\CreateBatch;
use yii\db\Expression;
use yii\db\Query;
use domain\device\models\CreateBatch as CreateBatchModel;
use domain\device\models\Device as DeviceModel;
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;
use domain\device\models\DeviceAuthFail as DeviceAuthFailModel;


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

    /**
     * @param $type
     * @param $where
     * @return array
     */
    static function getSerialNoComponent($type, $where)
    {
        if ('manufacture' == $type) {
            $q = new Query();
            $q->select(['concat(id, "_", manufacture_no) as uid', 'id','manufacture_no', 'name']);
            $q->from('manufacture');
            $q->where($where);

            $list = $q->all();

            return $list;
        } elseif('project' == $type) {
            $q = new Query();
            $q->select(['concat(id,"_", project_no) as uid', 'id','project_no', 'name']);
            $q->from('project');
            $q->where('id >0 and is_delete = 0');

            $list = $q->all();

            return $list;
        } elseif ('model' == $type) {
            $q = new Query();
            $q->select(['concat(id,"_", model_no) as uid','id','model_no', 'name']);
            $q->from('model');
            $q->where('id >0 and is_delete = 0');
            $list = $q->all();

            return $list;
        } elseif ('production' == $type) {
            $q = new Query();
            $q->select(['concat(id,"_", production_no) as uid','id','production_no', 'name']);
            $q->from('production');
            $q->where('id >0 and is_delete = 0');
            $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)
    {
        $totalNumExpress = new Expression("(select count(*) from ".DeviceModel::tableName(). "as dd where dd.batch_id = a.id and dd.is_delete = 0) as total_num");
        $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 no_auth_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");
        $outOfLimitExpress = new Expression("(select count(*) from ".DeviceAuthFailModel::tableName(). "as dd where concat(dd.manufacture_no, dd.project_no, dd.model_no, dd.production_no) = a.batch_no and dd.is_delete = 0) as out_of_num");
        $sysFailNumExpress = new Expression("(select count(*) from ".DeviceModel::tableName(). "as dd where dd.status =".DeviceStatus::FAIL_AUTH." and dd.is_delete = 0 and dd.batch_id = a.id) as auth_fail_num");
        $handleAuthFailNumExpress = new Expression("(select count(*) from ".DeviceModel::tableName(). "as dd where dd.batch_id = a.id and dd.has_re_auth = 1 and dd.is_delete = 0) as handle_auth_fail_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',
            $totalNumExpress, $hasAuthNumExpress, $noAuthNumExpress, $delNumExpress, $outOfLimitExpress, $sysFailNumExpress, $handleAuthFailNumExpress
        ]);
        $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($where)
    {
        $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($where);
        $batchModelFind->asArray();
        $info = $batchModelFind->one();

        return $info;
    }

    /**
     * @param $batchId
     * @return array|null|\yii\db\ActiveRecord
     */
    static function getBatchSelectList($where)
    {
        $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($where);
        $batchModelFind->asArray();
        $info = $batchModelFind->all();

        return $info;
    }
}