Parcourir la source

导入设备为空

李浩杰 il y a 4 ans
Parent
commit
a7b6c93920

+ 15 - 1
app/Http/Controllers/Admin/Base/IndexController.php

xqd xqd
@@ -19,6 +19,7 @@ use App\Services\Base\Tree;
 use App\Services\Base\BaseArea;
 use App\Services\Admin\Menus;
 use App\Services\Admin\Acl;
+use Carbon\Carbon;
 
 class IndexController extends Controller
 {
@@ -58,7 +59,20 @@ class IndexController extends Controller
             $device_stat[$type] = $num;
         }
         $user_auths = UserAuth::where('active', 2)->limit(8)->get();
-        return view('admin.base.index.welcome', compact('total_device', 'total_project', 'total_money', 'users_total', 'users_auth_total', 'users_not_auth_total', 'device_stat', 'user_auths'));
+        $min_order = Order::orderBy('created_at', 'desc')->first();
+        $min_year = $min_order ? $min_order->created_at->year : Carbon::now()->year;
+        $max_year = Carbon::now()->year;
+        $year_options = [];
+        for($i = $min_year; $i <= $max_year; ++$i) {
+            array_push($year_options, ['id' => $i, 'name' => $i . '年']);
+        }
+        $month_options = [['id' => '', 'name' => '所有月份']];
+        for($i = 1; $i <= 12; ++$i) {
+            array_push($month_options, ['id' => $i, 'name' => $i . '月']);
+        }
+        $select_year = $max_year;
+        $select_month = '';
+        return view('admin.base.index.welcome', compact('total_device', 'total_project', 'total_money', 'users_total', 'users_auth_total', 'users_not_auth_total', 'device_stat', 'user_auths', 'year_options', 'month_options', 'select_year', 'select_month'));
     }
     
     function createAreaDate(){

+ 8 - 8
app/Http/Controllers/Admin/InnerDeviceController.php

xqd xqd xqd xqd xqd
@@ -8,6 +8,8 @@ use App\Models\Device;
 use App\Models\DeviceName;
 use App\Models\InnerDevice;
 use App\Models\Option;
+use App\Models\Project;
+use App\Models\WorkPoint;
 use Illuminate\Http\Request;
 use Maatwebsite\Excel\Facades\Excel;
 
@@ -82,7 +84,8 @@ class InnerDeviceController extends BaseController
         list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
         $options = $this->device_name->getNameSpecOptions();
         $status_options = Option::get('inner_devices', 'status');
-        return view($this->view_path . 'create', compact('model', 'model_name','pre_uri', 'options', 'status_options'));
+        $project_work_point_options = (new Project())->getTwoLevelOptions();
+        return view($this->view_path . 'create', compact('model', 'model_name','pre_uri', 'options', 'status_options', 'project_work_point_options'));
     }
 
     public function transObject($items)
@@ -98,9 +101,7 @@ class InnerDeviceController extends BaseController
             return back()->withErrors($validator)->withInput();
         }
         $data = $request->input('data');
-        $data['device_id'] = $request->input('device_id');
-        $data['device_name_id'] = $request->input('device_name_id');
-        $data['spec_id'] = $request->input('spec_id');
+        $data = array_merge($data, $request->only(['device_id', 'device_name_id', 'spec_id', 'project_id', 'work_point_id']));
         $res = $this->model->create($data);
         if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
         return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
@@ -118,8 +119,9 @@ class InnerDeviceController extends BaseController
         if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
         $options = $this->device_name->getNameSpecOptions();
         $status_options = Option::get('inner_devices', 'status');
+        $project_work_point_options = (new Project())->getTwoLevelOptions();
         list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
-        return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item', 'options', 'status_options'));
+        return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item', 'options', 'status_options', 'project_work_point_options'));
     }
 
     public function update(Request $request)
@@ -131,9 +133,7 @@ class InnerDeviceController extends BaseController
             return back()->withErrors($validator)->withInput();
         }
         $data = $request->input('data');
-        $data['device_id'] = $request->input('device_id');
-        $data['device_name_id'] = $request->input('device_name_id');
-        $data['spec_id'] = $request->input('spec_id');
+        $data = array_merge($data, $request->only(['device_id', 'device_name_id', 'spec_id', 'project_id', 'work_point_id']));
         $res = $this->model->where('id', $request->input('id'))->update($data);
         if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
         return back()->with(['sg_success_info' => '编辑成功']);

+ 21 - 3
app/Http/Controllers/Admin/ProjectController.php

xqd xqd xqd
@@ -9,6 +9,7 @@ use App\Models\Order;
 use App\Models\Project;
 use App\Models\Road;
 use App\Models\Role;
+use Carbon\Carbon;
 use Illuminate\Http\Request;
 
 class ProjectController extends BaseController
@@ -143,12 +144,17 @@ class ProjectController extends BaseController
     public function getStat(Request $request)
     {
         $projects = $this->model->get();
-        $names = $projects->pluck('name');
+        $names = $this->limitName($projects->pluck('name'));
         $data = [[], []];
         $max = [0, 0];
+        $dates = $this->getDates($request);
+        $search_items = [
+            ['created_at', '>=', $dates['start_at']],
+            ['created_at', '<', $dates['end_at']]
+        ];
         foreach($projects as $project) {
-            $total = Order::where('project_id', $project->id)->count();
-            $total_money = Order::where('project_id', $project->id)->sum('money') / 100;
+            $total = Order::where('project_id', $project->id)->where($search_items)->count();
+            $total_money = Order::where('project_id', $project->id)->where($search_items)->sum('money') / 100;
             array_push($data[0], $total);
             array_push($data[1], $total_money);
             $max[0] = max($max[0], $total);
@@ -156,4 +162,16 @@ class ProjectController extends BaseController
         }
         return response()->json(['status' => 'success', 'data' => compact('data', 'names', 'max')]);
     }
+
+    public function getDates(Request $request)
+    {
+        if($request->input('month')) {
+            $start_at = Carbon::createFromDate($request->input('year'), $request->input('month'), 1)->toDateTimeString();
+            $end_at = Carbon::createFromDate($request->input('year'), $request->input('month'), 1)->addMonth()->toDateTimeString();
+        } else {
+            $start_at = Carbon::createFromDate($request->input('year'), 1, 1)->toDateTimeString();
+            $end_at = Carbon::createFromDate($request->input('year'), $request->input('month'), 1)->addYear()->toDateTimeString();
+        }
+        return compact('start_at', 'end_at');
+    }
 }

+ 4 - 3
app/Http/Controllers/Admin/TestController.php

xqd
@@ -14,9 +14,10 @@ class TestController extends Controller
 {
     public function index(Request $request)
     {
-        $info = Notification::orderBy('id', 'desc')->first();
-        dd($info->sendOfficialInfo());
-        return false;
+        dd(mb_substr('你好', 0, 1));
+//        $info = Notification::orderBy('id', 'desc')->first();
+//        dd($info->sendOfficialInfo());
+//        return false;
 //        $user = User::where('phone', '13438082119')->first();
 //        User::where('phone', 'like', '%1343808211%')->update([
 //            'open_id' => $user->open_id,

+ 7 - 0
app/Http/Controllers/Controller.php

xqd
@@ -10,4 +10,11 @@ use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
 class Controller extends BaseController
 {
     use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
+
+    public function limitName($items, $len = 8)
+    {
+        return $items->map(function ($item) use($len) {
+            return mb_substr($item, 0, $len);
+        });
+    }
 }

+ 17 - 5
app/Imports/InnerDeviceImport.php

xqd xqd
@@ -57,9 +57,21 @@ class InnerDeviceImport implements ToCollection
                 'name' => $row[2]
             ]);
 
-            $work_point = WorkPoint::firstOrCreate([
-                'name' => $row[8]
-            ])->first();
+            $project = null;
+            $work_point = null;
+            if($row[7]) {
+                $project = Project::firstOrCreate([
+                    'name' => $row[7]
+                ]);
+            }
+
+            if($project && $row[8]) {
+                $work_point = WorkPoint::firstOrCreate([
+                    'project_id' => $project->id,
+                    'name' => $row[8]
+                ]);
+            }
+
             $status = Option::where([
                 ['table', '=', 'inner_devices'],
                 ['column', '=', 'status'],
@@ -75,9 +87,9 @@ class InnerDeviceImport implements ToCollection
                 'buy_origin' => $row[4],
                 'manufacturer' => $row[5],
                 'shape' => $row[6],
+                'project_id' => $project ? $project->id : '',
                 'work_point_id' => $work_point ? $work_point->id : '',
-                'status' => $status ? $status->id : '',
-                'quantity' => (int)$row[7]
+                'status' => $status ? $status->id : ''
             ];
             $this->model->create($data);
 //            $num = (int)$row[7];

+ 33 - 0
app/Models/Project.php

xqd
@@ -3,9 +3,42 @@
 namespace App\Models;
 
 use Illuminate\Support\Facades\Auth;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Validator;
 
 class Project extends BaseModel
 {
+    public function getValidator(Request $request, $type)
+    {
+        $validator = Validator::make($request->input('data'), [
+            'name' => 'required'
+        ], [
+            'name.required' => '名称必填'
+        ]);
+        $validator->after(function ($validator) use($request) {
+            $data = $request->input('data');
+            if (isset($data['name']) && mb_strlen($data['name']) > 16) {
+                $validator->errors()->add('name', '名称长度不能超过16');
+            }
+        });
+        return $validator;
+    }
+
+    public function getTwoLevelOptions()
+    {
+        $projects = Project::select('name as text', 'id as value')->get();
+        $projects = $projects->prepend($this->transObject(['text' => '选择项目', 'value' => '']));
+        foreach ($projects as $project) {
+            $work_points = WorkPoint::where('project_id', $project->value)->select('name as text', 'id as value')->get();
+            $project->work_points = $work_points->prepend($this->transObject(['text' => '选择工点', 'value' => '']));
+        }
+        return $projects;
+    }
+
+    public function transObject($items)
+    {
+        return json_decode(json_encode($items));
+    }
 
     public function getManager()
     {

+ 5 - 0
app/Models/WorkPoint.php

xqd
@@ -16,4 +16,9 @@ class WorkPoint extends BaseModel
     {
         return $this->belongsTo('App\Models\Project', 'project_id');
     }
+
+    public static function getOptions()
+    {
+        return self::where('id', '>', 0)->get()->toArray();
+    }
 }

BIN
public/设备导入模板.xlsx


+ 115 - 68
resources/views/admin/base/index/welcome.blade.php

xqd xqd xqd
@@ -159,6 +159,9 @@
         .sg-font-lg {
             font-size: 2rem;
         }
+        #sg-search-form .layui-form-item .layui-inline {
+            margin-bottom: 0;
+        }
     </style>
 @endsection
 
@@ -203,8 +206,33 @@
         <div class="layui-row layui-col-space15">
             <div class="layui-col-xs12 layui-col-sm12 layui-col-md8">
                 <div class="layui-card">
-                    <div class="layui-card-header">项目数据一览图</div>
+                    <div class="layui-card-header" style="display: flex; align-items: center; justify-content: space-between">
+                        <div>项目数据一览图</div>
+                        <form class="layui-form" id="sg-search-form">
+                            <div class="layui-form-item layui-row" style="margin-bottom: 0">
+                                <div class="layui-inline">
+                                    <div class="layui-input-inline">
+                                        <select name="year">
+                                            @foreach($year_options as $option)
+                                                <option value="{{ $option['id'] }}" {{ $select_year == $option['id'] ? 'selected' : ''  }}>{{ $option['name'] }}</option>
+                                            @endforeach
+                                        </select>
+                                    </div>
+                                </div>
+                                <div class="layui-inline">
+                                    <div class="layui-input-inline">
+                                        <select name="month">
+                                            @foreach($month_options as $option)
+                                                <option value="{{ $option['id'] }}" {{ $select_month == $option['id'] ? 'selected' : ''  }}>{{ $option['name'] }}</option>
+                                            @endforeach
+                                        </select>
+                                    </div>
+                                </div>
+                            </div>
+                        </form>
+                    </div>
                     <div class="layui-card-body">
+
                         <div id="my-chart" style="width: 100%;height:400px;"></div>
                     </div>
                 </div>
@@ -312,82 +340,101 @@
 @section('footer')
     <script>
         $(function() {
-            var myChart = echarts.init(document.getElementById('my-chart'));
+            layui.use(['table', 'layer'], function() {
+                var form = layui.form,
+                    top_window = window;
+                var myChart = echarts.init(document.getElementById('my-chart'));
 
-            $.ajax({
-                method: 'POST',
-                url: '/admin/Project/getStat',
-                headers: {
-                    'X-CSRF-TOKEN': '{{ csrf_token() }}'
-                },
-                data: {},
-                success: function (data) {
-                    if(data.status === 'success') {
-                        // console.log(data.data);
-                        updateChart(data.data);
-                    }
-                },
-                error: function () {
+                form.on('select()', function(){
+                    getChartData();
+                });
+
+                getChartData();
 
+                function transformToJson(formData){
+                    var obj={};
+                    for (var i in formData) {
+                        obj[formData[i].name]=formData[i]['value'];
+                    }
+                    return obj;
                 }
-            });
 
-            function updateChart(data) {
-                // 指定图表的配置项和数据
-                var option = {
-                    color: ['#71D4D4', '#C3B6E6'],
-                    legend: {
-                        data: ['订单数', '订单金额']
-                    },
-                    tooltip: {},
-                    xAxis: [
-                        {
-                            type: 'category',
-                            data: data['names']
-                        }
-                    ],
-                    yAxis: [{
-                        type: 'value',
-                        name: '订单数',
-                        splitLine: {
-                            show: false
-                        }
-                    }, {
-                        type: 'value',
-                        name: '订单金额',
-                        splitLine: {
-                            show: false
+                function getChartData() {
+                    $.ajax({
+                        method: 'POST',
+                        url: '/admin/Project/getStat',
+                        headers: {
+                            'X-CSRF-TOKEN': '{{ csrf_token() }}'
+                        },
+                        data: transformToJson($('#sg-search-form').serializeArray()),
+                        success: function (data) {
+                            if(data.status === 'success') {
+                                // console.log(data.data);
+                                updateChart(data.data);
+                            }
+                        },
+                        error: function () {
+
                         }
-                    }],
-                    // Declare several bar series, each will be mapped
-                    // to a column of dataset.source by default.
-                    series: [
-                        {
-                            name: '订单数',
-                            type: 'bar',
-                            label: {
-                                show: true,
-                                position: 'top'
-                            },
-                            data: data.data[0]
+                    });
+                }
+
+                function updateChart(data) {
+                    // 指定图表的配置项和数据
+                    var option = {
+                        color: ['#71D4D4', '#C3B6E6'],
+                        legend: {
+                            data: ['订单数', '订单金额']
                         },
-                        {
+                        tooltip: {},
+                        xAxis: [
+                            {
+                                type: 'category',
+                                data: data['names']
+                            }
+                        ],
+                        yAxis: [{
+                            type: 'value',
+                            name: '订单数',
+                            splitLine: {
+                                show: false
+                            }
+                        }, {
+                            type: 'value',
                             name: '订单金额',
-                            type: 'bar',
-                            label: {
-                                show: true,
-                                position: 'top'
+                            splitLine: {
+                                show: false
+                            }
+                        }],
+                        // Declare several bar series, each will be mapped
+                        // to a column of dataset.source by default.
+                        series: [
+                            {
+                                name: '订单数',
+                                type: 'bar',
+                                label: {
+                                    show: true,
+                                    position: 'top'
+                                },
+                                data: data.data[0]
                             },
-                            yAxisIndex: 1,
-                            data: data.data[1]
-                        }
-                    ]
-                };
-
-                // 使用刚指定的配置项和数据显示图表。
-                myChart.setOption(option);
-            }
+                            {
+                                name: '订单金额',
+                                type: 'bar',
+                                label: {
+                                    show: true,
+                                    position: 'top'
+                                },
+                                yAxisIndex: 1,
+                                data: data.data[1]
+                            }
+                        ]
+                    };
 
+                    // 使用刚指定的配置项和数据显示图表。
+                    myChart.setOption(option);
+                }
+            });
         })
     </script>
 @endsection

+ 1 - 0
resources/views/admin/inner-devices/create.blade.php

xqd
@@ -22,6 +22,7 @@
                         {{ csrf_field() }}
                         @include('share.name-select-form', ['device_name_id' => '', 'spec_id' => '', 'options' => $options])
                         @include('share.layui-form-item', ['type' => 'radio', 'name' => 'status', 'label' => '状态', 'selected_id' => (old('data') ? old('data')['status'] : ''), 'options' => $status_options])
+                        @include('share.project-work-point-select-form', ['project_id' => '', 'work_point_id' => '', 'options' => $project_work_point_options])
                         @include('share.layui-form-item', ['type' => 'input', 'name' => 'number', 'label' => '固定资产编号', 'required' => true, 'value' => (old('data') ? old('data')['number'] : '')])
                         @include('share.layui-form-item', ['id' => 'sg-produce-date', 'type' => 'date', 'name' => 'produce_date', 'label' => '出厂日期', 'required' => true, 'value' => (old('data') ? old('data')['produce_date'] : '')])
                         @include('share.layui-form-item', ['type' => 'input', 'name' => 'shape', 'label' => '外形尺寸', 'required' => true, 'value' => (old('data') ? old('data')['shape'] : '')])

+ 1 - 0
resources/views/admin/inner-devices/edit.blade.php

xqd
@@ -23,6 +23,7 @@
                         <input type="hidden" name="id" value="{{ $item->id }}">
                         @include('share.name-select-form', ['device_name_id' => $item->device_name_id, 'spec_id' => $item->spec_id, 'options' => $options])
                         @include('share.layui-form-item', ['type' => 'radio', 'name' => 'status', 'label' => '状态', 'selected_id' => $item->status, 'options' => $status_options])
+                        @include('share.project-work-point-select-form', ['project_id' => $item->project_id, 'work_point_id' => $item->work_point_id, 'options' => $project_work_point_options])
                         @include('share.layui-form-item', ['type' => 'input', 'name' => 'number', 'label' => '固定资产编号', 'required' => true, 'value' => $item->number])
                         @include('share.layui-form-item', ['id' => 'sg-produce-date', 'type' => 'date', 'name' => 'produce_date', 'label' => '出厂日期', 'required' => true, 'value' => $item->produce_date])
                         @include('share.layui-form-item', ['type' => 'input', 'name' => 'shape', 'label' => '外形尺寸', 'required' => true, 'value' => $item->shape])

+ 3 - 3
resources/views/admin/inner-devices/index.blade.php

xqd xqd
@@ -129,7 +129,7 @@
 
                 $('#sg-import-all').click(function() {
                     layer.open({
-                        title: '导入项目',
+                        title: '导入设备',
                         type: 1,
                         area: ['90%', '90%'],
                         content: $('#sg-import-box'),
@@ -195,13 +195,13 @@
                     cols: [[
                         { field: 'id', title: 'ID', align: 'center' },
                         { field: 'number', title: '固定资产编号', align: 'center' },
-                        { field: 'device_name_name', title: '名称', align: 'center' },
+                        { field: 'device_name_name', title: '设备名称', align: 'center' },
                         { field: 'spec_name', title: '规格型号', align: 'center' },
                         { field: 'produce_date', title: '出厂日期', align: 'center' },
                         { field: 'buy_origin', title: '采购原值', align: 'center' },
                         { field: 'manufacturer', title: '生产厂家', align: 'center' },
                         { field: 'shape', title: '外形尺寸', align: 'center' },
-                        { field: 'quantity', title: '数量', align: 'center' },
+                        // { field: 'quantity', title: '数量', align: 'center' },
                         { field: 'project_name', title: '在用项目', align: 'center' },
                         { field: 'work_point_name', title: '目前工点', align: 'center' },
                         { field: 'status', title: '状态', align: 'center' },

+ 74 - 0
resources/views/share/project-work-point-select-form.blade.php

xqd
@@ -0,0 +1,74 @@
+<?php
+?>
+<style type="text/css">
+
+</style>
+<div class="layui-form-item {{ $errors->has('project_id') ? 'has-error' : '' }}">
+    <label class="layui-form-label" style="padding: 0px;">在用项目</label>
+    <div class="layui-input-block">
+        <select name="project_id" lay-filter="project_id">
+            @foreach($options as $option)
+                <option value="{{$option->value}}" {{$option->value == $project_id ? 'selected' : ''}}>{{$option->text}}</option>
+            @endforeach
+        </select>
+        @if($errors->has('project_id'))
+            <span class="help-block">{{ $errors->first('project_id') }}</span>
+        @endif
+    </div>
+    @if($errors->has('project_id'))
+        <span class="help-block">{{ $errors->first('project_id') }}</span>
+    @endif
+</div>
+
+@if(!isset($hide_spec))
+<div class="layui-form-item {{ $errors->has('work_point_id') ? 'has-error' : '' }}">
+    <label class="layui-form-label" style="padding: 0px;">目前工点</label>
+    <div class="layui-input-block">
+        <select name="work_point_id" lay-filter="work_point_id">
+        </select>
+        @if($errors->has('work_point_id'))
+            <span class="help-block">{{ $errors->first('work_point_id') }}</span>
+        @endif
+    </div>
+</div>
+@endif
+<script>
+    $(function() {
+        layui.use(['form'], function() {
+            let form = layui.form;
+            let projects = JSON.parse('{!! $options !!}');
+            let work_points = [];
+            let project_id = '{{ $project_id }}';
+            let work_point_id = '{{ $work_point_id }}';
+            let hide_spec = '{{ isset($hide_spec) ? "yes" : "no" }}';
+            // console.log(hide_spec)
+
+            form.on('select(project_id)', function(data) {
+                project_id = data.value;
+                work_point_id = '';
+                updateWorkPoint()
+            });
+
+            updateWorkPoint();
+
+            function updateWorkPoint() {
+                let name_index = getIndex(projects, project_id);
+                work_points = projects[name_index].work_points;
+                let options = '';
+                for(let i = 0; i < work_points.length; ++i) {
+                    options += '<option value=' + work_points[i].value + (parseInt(work_points[i].value) === parseInt(work_point_id) ? ' selected' : '') + '>' + work_points[i].text + '</option>'
+                }
+                $("select[name='work_point_id']").html(options);
+                form.render('select');
+            }
+
+            function getIndex(list, id) {
+                id = parseInt(id);
+                for(var i = 0; i < list.length; ++i) {
+                    if(list[i].value === id) return i;
+                }
+                return 0;
+            }
+        })
+    })
+</script>