DeviceController.php 7.79 KB
<?php

namespace app\ht\modules\device\controllers;


use Yii;
use yii\base\Exception;
use yii\data\Pagination;
use app\ht\controllers\BaseController;
use common\helpers\Utils;
use domain\device\DeviceStatus;
use domain\device\CreateBatchRepository;
use domain\device\Device;
use domain\device\DeviceRepository;
use domain\device\models\Device as DeviceModel;

use domain\device\CreateBatch;

use stdClass;

/**
 * 设备管理
 */
class DeviceController extends BaseController
{
    /**
     * @return string
     */
    public function actionIndex()
    {
        $request = Yii::$app->request;
        $serialNo = $request->get('serial_no');
        $mac = $request->get('mac');
        $project = $request->get('project');
        $model = $request->get('model');
        $production = $request->get('production');
        $manufacture = $request->get('manufacture');
        $deviceId = $request->get('device_id');
        $status = $request->get('status');
        $page = $request->get('page');
        $where = [
            'and',
            ['=','a.is_delete', 0]
        ];
        if (!empty($serialNo)) {
            $where[] = ['like', 'a.serial_no', $serialNo];
        }
        if (!empty($project)) {
            $where[] = ['like', 'p.name', $project];
        }
        if (!empty($model)) {
            $where[] = ['like', 'mo.name', $model];
        }
        if (!empty($production)) {
            $where[] = ['like', 'pd.name', $production];
        }
        if (!empty($mac)) {
            $where[] = ['like', 'a.mac', $mac];
        }
        if (!empty($manufacture)) {
            $where[] = ['like', 'm.name', $manufacture];
        }
        if (!empty($deviceId)) {
            $where[] = ['like', 'a.device_id', $deviceId];
        }
        if (isset($_GET['status']) && -1 != $status) {
            $where[] = ['=', 'a.status', $status];
        }

        if (0 >= $page) {
            $page = 1;
        }
        $pageSize = 20;
        $page = ($page -1) * $pageSize;
        // DeviceRepository::getList($where, $pageSize, $page);
        $deviceData = DeviceRepository::getList($where, $pageSize, $page);
        $pages = new Pagination(['totalCount' => DeviceRepository::getListCount($where), 'pageSize' => $pageSize]);
        $statusList = DeviceStatus::statusLabels(); //

        $params['statusList'] = $statusList;
        $params['deviceList'] = $deviceData;
        $params['pages']      = $pages;
        $params["gets"] = [
            'serial_no' => $serialNo,
            'mac' => $mac,
            'project'  => $project,
            'model'  => $model,
            'device_id' => $deviceId,
            'production'  => $production,
            'manufacture' => $manufacture,
            'status'      => $status
        ];

        return $this->render('index', $params);
    }

    /**
     * @return string
     */
    public function actionCreateDevice()
    {
        return $this->render('createDevice');
    }

    /**
     *
     */
    public function actionDoCreateDevice()
    {
        $req = Yii::$app->request;
        $manufactureId = $req->post('manufactureId');
        $projectId = $req->post('projectId');
        $modelId = $req->post('modelId');
        $productionId = $req->post('productionId');

        $manufactureNo = $req->post('manufactureNo');
        $projectNo = $req->post('projectNo');
        $modelNo = $req->post('modelNo');
        $productionNo = $req->post('productionNo');

        $num = $req->post('num');
        $e = new stdClass();
        $e->success = false;
        $e->message = 'fail';
        if (empty($num)) {
            $e->message = '数量不能为0';
            return $this->renderJson($e);
        }
        if (1 * $num > 30000) {
            $e->message = '数量不能超过3万0';
            return $this->renderJson($e);
        }

        $batchNo = strtoupper(Device::getBatchNo($manufactureNo,$projectNo,$modelNo,$productionNo));
        $batchModel = CreateBatchRepository::findOne(['batch_no' => $batchNo]);
        if ($batchModel) {
            $e->message = '已经创建过这个批次序列号';
            return $this->renderJson($e);
        }

        $item = [
            'batch_no'          => $batchNo,
            'manufacture_id'    => $manufactureId,
            'project_id'        => $projectId,
            'model_id'          => $modelId,
            'num'               => $num,
            'production_id'     => $productionId,
        ];
        $transaction = Yii::$app->db->beginTransaction();
        try {
            $newBatchModel = CreateBatch::create($item);
            $saveData = [];
            $tt = time();
            for ($i = 1 ; $i <= $num; $i++) {
                $saveData[] = [
                    strtoupper($batchNo.sprintf('%04x', $i)),
                    Utils::macGenerate(),
                    0,
                    $newBatchModel->id,
                    0,
                    0,
                    0,
                    0,
                    $tt,
                    $tt
                ];

            }
            $res = Yii::$app->db->createCommand()->batchInsert(DeviceModel::tableName(),
                array('serial_no','mac','status','batch_id','is_delete','has_re_auth','apply_at','auth_at','created_at','updated_at'),
                $saveData)->execute();//执行批量添加
            $transaction->commit();
            $e->success = true;
        } catch (Exception $exception) {
            $transaction->rollBack();
            $e->message = '创建失败';
        }

        return $this->renderJson($e);
    }

    /**
     * @return string
     */
    public function actionSearchItem()
    {
        $req = Yii::$app->request;
        $type = $req->post('type');
        $keyword = $req->post('query');
        $e = new stdClass();
        $list = CreateBatchRepository::getSerialNoComponent($type, $keyword);
        $e->list = $list;

        return $this->renderJson($e);
    }
    /**
     * 导出订单数据
     * @return string
     */
    public function actionExportDa()
    {
        $request = Yii::$app->request;

    }

    /**
     * @return string
     */
    public function actionAuthDevice()
    {
        $req = Yii::$app->request;
        $id = $req->post('id');
        $e = new stdClass();
        $e->success = false;
        $e->message = 'fail';
        $deviceModel = DeviceRepository::findOne(['id' => $id]);
        if (empty($deviceModel)) {
            $e->message = '找不到该设备';
            return $this->renderJson($e);
        }
        if(DeviceStatus::HAS_AUTH == $deviceModel->status) {
            $e->message = '设备已经授权了';
            return $this->renderJson($e);
        }
        if (empty($deviceModel->device_id)) {
            $e->message = '设备ID为空,不能授权';
            return $this->renderJson($e);
        }
        $tt = time();
        $deviceModel->apply_at = $tt;
        $deviceModel->auth_at = $tt;
        $deviceModel->status = DeviceStatus::HAS_AUTH;
        $result = $deviceModel->save();
        if ($result) {
            $e->success = true;
        } else {
            $e->message = '授权失败';
        }

        return $this->renderJson($e);
    }

    /**
     * @return string
     */
    public function actionDelDevice()
    {
        $req = Yii::$app->request;
        $id = $req->post('id');
        $e = new stdClass();
        $e->success = false;
        $e->message = 'fail';
        $deviceModel = DeviceRepository::findOne(['id' => $id]);
        if (empty($deviceModel)) {
            $e->message = '找不到该设备';
            return $this->renderJson($e);
        }

        $tt = time();

        $deviceModel->is_delete = 1;
        $result = $deviceModel->save();
        if ($result) {
            $e->success = true;
        } else {
            $e->message = '删除失败';
        }

        return $this->renderJson($e);
    }

}