DeviceController.php 9.78 KB
<?php

namespace app\ht\modules\datas\controllers;

use domain\device\DeviceAuthFailRepository;
use Yii;
use yii\data\Pagination;
use app\ht\controllers\BaseController;

use domain\device\CreateBatchRepository;
use domain\device\DeviceRepository;
use domain\device\DeviceStatus;
use function strtotime;

/**
 * 机器状态码管理
 * Class DeviceController
 * @package app\ht\modules\upgrade\controllers
 */
class DeviceController extends BaseController
{
    /**
     * 版本日志管理
     */
    public function actionIndex()
    {
        $params = $this->dataList(1);
        /**
         * 渲染模板
         */
        return $this->render('index', $params);
    }

    /**
     * 查询数据列表
     */
    protected function dataList($type = '')
    {
        $request     = Yii::$app->request;
        $manufactureName   = $request->get('manufacture_name');
        $projectName     = $request->get('project_name');
        $modelName = $request->get('model_name');
        $productionName  = $request->get('production_name');
        $batchName = $request->get('batch_no');

        $manufactureName = $this->filterVar($manufactureName);
        $projectName = $this->filterVar($projectName);
        $modelName = $this->filterVar($modelName);
        $productionName = $this->filterVar($productionName);
        $batchName = $this->filterVar($batchName);

        $gets = [
            'manufacture_name'          => $manufactureName,
            'project_name'              => $projectName,
            'model_name'                => $modelName,
            'production_name'           => $productionName,
            'batch_no'                  => $batchName
        ];

        $where = ['and'];

        if ($manufactureName) {
            $where[] = ['like', 'm.name', $manufactureName];
        }
        if ($projectName) {
            $where[] = ['like', 'pro.name', $projectName];
        }
        if ($modelName) {
            $where[] = ['like', 'mo.name', $modelName];
        }
        if ($productionName) {
            $where[] = ['like', 'prod.name', $productionName];
        }
        if ($type == 0) {
            $pageList = CreateBatchRepository::getPageList($where, 0 , 0);
            $pages = null;
        } else {
            $pageSize = 20;
            $pages = new Pagination(['totalCount' => CreateBatchRepository::getPageCount($where), 'pageSize' => $pageSize]);
            $pageList = CreateBatchRepository::getPageList($where, $pages->offset, $pages->limit);
        }

        /**
         * 数据整理
         */
        return [
            'listdata'   => $pageList,
            'pages'      => $pages,
            'gets'       => $gets
        ];
    }

    /**
     * 导出设备状态数据
     * @return string
     */
    public function actionExport()
    {
        $params = $this->dataList(0);
        return $this->renderPartial('export', $params);
    }

    /**
     * @return string
     */
    public function actionDeviceList()
    {
        $params = $this->batchDataList(1);
        return $this->render('device-list', $params);
    }

    /**
     * @return string
     */
    public function actionDeleteDeviceList()
    {
        $params = $this->batchDataList(1,1);
        return $this->render('delete-device-list', $params);
    }

    /**
     * @return string
     */
    public function actionBatchDeleteExport()
    {
        $params = $this->batchDataList(0,1);
        return $this->renderPartial('batch-delete-export', $params);
    }

    /**
     * @param $type
     * @return mixed
     */
    private function batchDataList($type, $isDelete = 0)
    {
        $request = Yii::$app->request;
        $serialNo = $request->get('serial_no');
        $mac = $request->get('mac');
        $batchId = $request->get('batch_id');
        $deviceId = $request->get('device_id');
        $status = $request->get('status');
        $startApplyAt = $request->get('start_apply_at');
        $endApplyAt = $request->get('end_apply_at');
        $startAuthAt = $request->get('start_auth_at');
        $endAuthAt = $request->get('end_auth_at');

        $page = $request->get('page');
        $where = [
            'and',
            ['=','a.is_delete', $isDelete],
            ['=','a.batch_id', $batchId]
        ];
        if (!empty($serialNo)) {
            $where[] = ['like', 'a.serial_no', $serialNo];
        }

        if (!empty($mac)) {
            $where[] = ['like', 'a.mac', $mac];
        }

        if (!empty($deviceId)) {
            $where[] = ['like', 'a.device_id', $deviceId];
        }

        if (!empty($startApplyAt)) {
            $where[] = ['>=', 'a.apply_at', strtotime($startApplyAt)];
        }
        if (!empty($endApplyAt)) {
            $where[] = ['<=', 'a.apply_at', strtotime($endApplyAt) + 3600 * 24];
        }

        if (!empty($startAuthAt)) {
            $where[] = ['>=', 'a.auth_at', strtotime($startAuthAt)];
        }
        if (!empty($endAuthAt)) {
            $where[] = ['<=', 'a.auth_at', strtotime($endAuthAt) + 3600 * 24];
        }
        if (isset($_GET['status']) && -1 != $status) {
            $where[] = ['=', 'a.status', $status];
        } else {
            $status = -1;
        }
        if (0 >= $page) {
            $page = 1;
        }
        $statusList = DeviceStatus::statusLabels(); //
        if (1 == $type) {
            $pageSize = 20;
            $page = ($page -1) * $pageSize;
            $deviceData = DeviceRepository::getList($where, $pageSize, $page);
            $pages = new Pagination(['totalCount' => DeviceRepository::getListCount($where), 'pageSize' => $pageSize]);

            if (empty($deviceData)) {
                $where = ['a.id' => $batchId];
                $batchInfo = CreateBatchRepository::getBatchInfo($where);
            } else {
                $batchInfo = $deviceData[0];
            }
            $project = $batchInfo['project'];
            $production =  $batchInfo['production'];
            $model = $batchInfo['model'];
            $manufacture = $batchInfo['manufacture'];
            $batchNo = $batchInfo['batch_no'];

        } else {
            $deviceData = DeviceRepository::getList($where, 0, 0);
            $pages = null;
            $project = $production = $model = $manufacture = $batchNo = null;
        }


        $params['statusList'] = $statusList;
        $params['deviceList'] = $deviceData;
        $params['pages']      = $pages;
        $params["gets"] = [
            'serial_no' => $serialNo,
            'mac' => $mac,
            'project'  => $project,
            'model'  => $model,
            'device_id' => $deviceId,
            'batch_id'  => $batchId,
            'production'  => $production,
            'manufacture' => $manufacture,
            'start_apply_at' => $startApplyAt,
            'end_apply_at' => $endApplyAt,
            'start_auth_at' => $startAuthAt,
            'end_auth_at' => $endAuthAt,
            'batch_no'    => $batchNo,
            'status'      => $status
        ];

        return $params;
    }


    /**
     * 导出某个批次的数据
     * @return string
     */
    public function actionBatchExport()
    {
        $params = $this->batchDataList(0);
        return $this->renderPartial('batch-export', $params);
    }

    /**
     * @param $type
     * @return mixed
     */
    private function batchFailDataList($type)
    {
        $request = Yii::$app->request;
        $batchNo = $request->get('batch_no');
        $deviceId = $request->get('device_id');
        $startApplyAt = $request->get('start_apply_at');
        $endApplyAt = $request->get('end_apply_at');

        $page = $request->get('page');
        $where = [
            'and',
            ['=','a.is_delete', 0],
            ['=','concat(a.manufacture_no,a.project_no,a.model_no,a.production_no)', $batchNo]
        ];


        if (!empty($deviceId)) {
            $where[] = ['like', 'a.device_id', $deviceId];
        }

        if (!empty($startApplyAt)) {
            $where[] = ['>=', 'a.apply_at', strtotime($startApplyAt)];
        }
        if (!empty($endApplyAt)) {
            $where[] = ['<=', 'a.apply_at', strtotime($endApplyAt) + 3600 * 24];
        }


        if (0 >= $page) {
            $page = 1;
        }

        if (1 == $type) {
            $pageSize = 20;
            $page = ($page -1) * $pageSize;
            $deviceData = DeviceAuthFailRepository::getList($where, $pageSize, $page);
            $pages = new Pagination(['totalCount' => DeviceAuthFailRepository::getListCount($where), 'pageSize' => $pageSize]);

            $batchInfo = CreateBatchRepository::getBatchInfo(['a.batch_no' => $batchNo]);
            $project = $batchInfo['project'];
            $production =  $batchInfo['production'];
            $model = $batchInfo['model'];
            $manufacture = $batchInfo['manufacture'];
            $batchNo = $batchInfo['batch_no'];

        } else {
            $deviceData = DeviceAuthFailRepository::getList($where, 0, 0);
            $pages = null;
            $project = $production = $model = $manufacture = $batchNo = null;
        }

        $params['deviceList'] = $deviceData;
        $params['pages']      = $pages;
        $params["gets"] = [

            'project'  => $project,
            'model'  => $model,
            'device_id' => $deviceId,
            'batch_no'  => $batchNo,
            'production'  => $production,
            'manufacture' => $manufacture,
            'start_apply_at' => $startApplyAt,
            'end_apply_at' => $endApplyAt,

        ];

        return $params;
    }

    /**
     * @return string
     */
    public function actionFailList()
    {
        $params = $this->batchFailDataList(1);
        return $this->render('fail-list', $params);
    }

    /**
     * @return string
     */
    public function actionFailListExport()
    {
        $params = $this->batchFailDataList(0);
        return $this->renderPartial('fail-list-export', $params);
    }
}