Index.php
6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
/**
* 首页接口
*/
class Index extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
/***
* 导入库容数据
* 读取excel数据
*/
public function getkrExceldata()
{
$filePath = 'kr/hgs.xls';
if (!is_file($filePath)) {
$this->error('找不到数据文件,请重新上传');
}
//实例化reader
$ext = pathinfo($filePath, PATHINFO_EXTENSION);
if (!in_array($ext, ['xls', 'xlsx'])) {
$this->error('不支持的数据格式');
}
if ($ext === 'xls') {
$reader = new Xls();
} else {
$reader = new Xlsx();
}
//读取EXCEl的内容
$PHPExcel = $reader->load($filePath);
if (!$PHPExcel) {
$this->error('表格内没有数据内容');
}
$currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
$allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
$allRow = $currentSheet->getHighestRow(); //取得一共有多少行
$maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
$fields = [];
//获得表第1行栏目名称
for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
$val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
$fields[] = $val;
}
}
//获得从第2行起内容列表数组
for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
$values = [];
for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
$val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
$values[] = is_null($val) ? '0' : $val;
}
//导入数据
$data['warterdata'] = $values[0];
$data['CapacityOf'] = $values[1];
$data['reservoir_id'] = $values[2];
//判断是否重复导入
$cks = Db::name('warterdata_capacityof_copy')
->where(['reservoir_id' => $data['reservoir_id'], 'warterdata' => $data['warterdata']])
->find();
if (empty($cks)) {
Db::name('warterdata_capacityof_copy')->insertGetId($data);
}
}
$this->success('导入成功');
}
/**
* 2. 新库容表
*/
public function dealcapacityofnew()
{
$data = Db::name("warterdata_capacityof_copy")
->where("reservoir_id", 1)
->order("warterdata asc")
->select();
$cha_list = [];
// 启动事务
Db::startTrans();
try {
if (!empty($data)) {
$sum = count($data);
foreach ($data as $k => $v) {
$new_insert = [];
$cha = 0;//库容差
$sw_cha = 0;//水位差
if ($k + 1 < $sum) {
$sw_cha = ($data[$k + 1]["warterdata"] - $v['warterdata']) * 100;//精度到0.01
$cha = $data[$k + 1]["CapacityOf"] - $v['CapacityOf'];
$cha_list[] = $cha;
//dump("+++++++");
$meici_add = $cha / $sw_cha;
//dump("=======");
//warterdata每次加0.01;
for ($i = 1; $i < $sw_cha; $i++) {
$warterdata = $v['warterdata'] + $i * 0.01;
$new_add = $meici_add * $i;
$CapacityOf =$v['CapacityOf']+ $new_add;//两个高精度数相乘保留小数点后3位
$new_insert[] = [
"warterdata" => $warterdata,
"CapacityOf" => round($CapacityOf, 4),
"reservoir_id" => $v['reservoir_id']
];
}
Db::name("warterdata_capacityof_copy")->insertAll($new_insert);
} else {
$cha = 0;
$cha_list[] = $cha;
}
//dump($cha);
}
dump($cha_list);
}
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
}
/**
* 首页
*
*/
public function index()
{
$this->success('请求成功');
}
/**
* app版本更新检测
*/
public function getAppVersion()
{
$app_type = $this->request->param("app_type");
if ($app_type) {
$where = [];
$where = [
"app_type" => $app_type,
"status" => "1"
];
$version = Db::name("appversion")->where($where)->order("version desc")->find();
$this->success("最新版本获取成功", $version);
} else {
$this->error("未检测到App版本渠道");
}
}
public function test()
{
$list = $this->reservoir_tree(0, 0, 0, 0);
$this->success('', $list);
}
public function getPptnR()
{
$data = input('post.');
if (!$data['STCD']) {
$this->error('测站编码不能为空');
}
$res = Db::name('st_pptn_r')->insert($data);
if ($res) {
$this->success('添加成功');
} else {
$this->error('添加失败');
}
}
}