123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?php
- namespace catchAdmin\stations\controller;
- use catcher\base\CatchRequest as Request;
- use catcher\CatchResponse;
- use catcher\base\CatchController;
- use catchAdmin\stations\model\Station as StationModel;
- use catchAdmin\school\model\KqBuilding as BuildingModel;
- use catcher\Utils;
- use catcher\library\excel\Excel;
- use catchAdmin\stations\excel\SkExport;
- class IndoorStation extends CatchController
- {
- protected $stationModel;
- protected $buildingModel;
- public function __construct(StationModel $stationModel, BuildingModel $buildingModel)
- {
- $this->stationModel = $stationModel;
- $this->buildingModel = $buildingModel;
- }
- /**
- * 列表
- * @time 2021年05月21日 15:17
- * @param Request $request
- */
- public function index(Request $request): \think\Response
- {
- $field = $request->get('field')?:'id';
- $order = $request->get('order')?:'desc';
- return CatchResponse::paginate($this->stationModel->getStationList($field,$order));
- }
- /**
- * 检测保存字段
- */
- protected function checkSaveData($post)
- {
- if (!isset($post['station_mac']) || !$post['station_mac']) {
- return ['success' => false, 'message' =>'设备MAC不能为空'];
- }
- if (!$this->stationModel->checkStationMacFormat($post['station_mac'])) {
- return ['success' => false, 'message' =>'设备Mac格式不正确'];
- }
- if ($this->stationModel->isStationMacDuplicate($post['station_mac'])) {
- return ['success' => false, 'message' =>'设备MAC已存在'];
- }
- if (!isset($post['station_name']) || !$post['station_name']) {
- return ['success' => false, 'message' =>'设备名称不能为空'];
- }
- if(!trim($post['coordinate_x'])){
- return ['success' => false, 'message' =>'未获取到横向坐标'];
- }
- if(!trim($post['coordinate_y'])){
- return ['success' => false, 'message' =>'未获取到纵向坐标'];
- }
- if (!is_numeric($post['coordinate_x']) || !is_numeric($post['coordinate_y'])) {
- return CatchResponse::fail('坐标必须是数字');
- }
- // if (!isset($post['room_no']) || !$post['room_no']) {
- // return ['success' => false, 'message' =>'房间号不能为空'];
- // }
- if (!isset($post['school_id']) || !$post['school_id']) {
- return ['success' => false, 'message' =>'未获取到所属学校'];
- }
- if (!isset($post['building_id']) || !$post['building_id']) {
- return ['success' => false, 'message' =>'未获取到所属建筑'];
- }
- if (!isset($post['floor_id']) || !$post['floor_id']) {
- return ['success' => false, 'message' =>'未获取到所属楼层'];
- }
- }
- /**
- * 保存信息
- * @time 2021年05月21日 15:17
- * @param Request $request
- */
- public function save(Request $request): \think\Response
- {
- $post = $request->post();
- // 检测字段
- $res = $this->checkSaveData($post);
- if ($res && !$res['success']) {
- return CatchResponse::fail($res['message']);
- }
- $post['station_mac'] = strtoupper(trim($post['station_mac']));
- $post['station_name'] = trim($post['station_name']);
- $post['station_code'] = substr($post['station_mac'], -6);
- // 是否需要检测房间号重复,一个房间可装多台?
- // 省市区随建筑
- $buildingInfo = $this->buildingModel->where('id', $post['building_id'])->find();
- if (empty($buildingInfo)) {
- return CatchResponse::fail('获取建筑信息失败');
- }
- $post['province_id'] = $buildingInfo['province_id'];
- $post['city_id'] = $buildingInfo['city_id'];
- $post['district_id'] = $buildingInfo['district_id'];
- $post['longitude'] = $buildingInfo['longitude'];
- $post['latitude'] = $buildingInfo['latitude'];
- $post['station_type'] = 3; // 1-考勤,2-定位,3-室内(插座)
- $post['station_model'] = 1; // 1-G31W1,2-G23
- return CatchResponse::success($this->stationModel->storeBy($post), '修改成功');
- }
- /**
- * 读取
- * @time 2021年05月21日 15:17
- * @param $id
- */
- public function read($id): \think\Response
- {
- return CatchResponse::success($this->stationModel->findBy($id));
- }
- /**
- * 更新
- * @time 2021年05月21日 15:17
- * @param Request $request
- * @param $id
- */
- public function update(Request $request, $id): \think\Response
- {
- $post = $request->post();
-
- if (!isset($post['station_mac']) || !$post['station_mac']) {
- return CatchResponse::fail('设备MAC不能为空');
- }
- if (!$this->stationModel->checkStationMacFormat($post['station_mac'])) {
- return CatchResponse::fail('设备Mac格式不正确');
- }
- if ($this->stationModel->isStationMacDuplicate($post['station_mac'], $id)) {
- return CatchResponse::fail('设备MAC已存在');
- }
- if(!trim($post['station_name'])){
- return CatchResponse::fail('设备名称不能为空');
- }
- if(!trim($post['coordinate_x'])){
- return CatchResponse::fail('未获取到横向坐标');
- }
- if(!trim($post['coordinate_y'])){
- return CatchResponse::fail('未获取到纵向坐标');
- }
- if (!is_numeric($post['coordinate_x']) || !is_numeric($post['coordinate_y'])) {
- return CatchResponse::fail('坐标必须是数字');
- }
- //基站Mac校验
- $post['station_mac'] = strtoupper(trim($post['station_mac']));
- $post['station_code'] = substr($post['station_mac'], -6);
- return CatchResponse::success($this->stationModel->updateBy($id, $post), '修改成功');
- }
- /**
- * 删除
- * @time 2021年05月21日 15:17
- * @param $id
- */
- public function delete($id): \think\Response
- {
- return CatchResponse::success($this->stationModel->deleteBy($id,true));
- }
- /**
- * 更新坐标
- */
- public function updateLocation(Request $request, $id): \think\Response
- {
- $coor_x = $request->post('coordinate_x');
- $coor_y = $request->post('coordinate_y');
- if(!trim($coor_x)){
- return CatchResponse::fail('未获取到横向坐标');
- }
- if(!trim($coor_y)){
- return CatchResponse::fail('未获取到纵向坐标');
- }
- $save_data = [
- 'coordinate_x' => $coor_x,
- 'coordinate_y' => $coor_y,
- ];
- return CatchResponse::success($this->stationModel->updateBy($id, $save_data), '修改成功');
- }
-
- /**
- * 导出室内插座基站
- *
- * @time 2020年09月08日
- * @param Excel $excel
- * @param DeviceExport $deviceExport
- * @throws \PhpOffice\PhpSpreadsheet\Exception
- * @return \think\response\Json
- */
- public function export(Excel $excel, SkExport $skExport)
- {
- // var_dump(Utils::publicPath('export/students'));//导出路径
- return CatchResponse::success($excel->save($skExport, Utils::publicPath('export/skstations'), 'local', '室内插座基站'));
- }
- }
|