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
<?php
namespace app\em\model;
class EmList extends Base
{
protected $table = 'em';
//获取电表列表与基站相关信息
public function getEmList($map,$Nowpage,$limits){
$data = $this
->field('em.id,em.em_numb,em.em_type_id,em.create_time,s.station_name,s.original_sp_id,
s.station_sp_code,s.proj_number,s.area_id,r.region_name,o.operator_name')
->leftJoin(['station_info'=>'s'],'em.station_id = s.station_id')
->leftJoin(['region'=>'r'],'s.area_id = r.id')
->leftJoin(['operator'=>'o'],'o.operator_id = s.original_sp_id')
// ->leftJoin(['em_rule'=>'er'],'er.em_id=em.id')
// ->leftJoin(['dc'=>'d'],'em.dc_id=d.id')
->with('account,rule')
->where($map)
->page($Nowpage, $limits)
->order('id desc')
->select()
->toarray();
return $data;
}
public function getEmListCount($map){
$data = $this
->field('em.id,em.em_numb,em.em_type_id,em.create_time,s.station_name,s.original_sp_id,
s.station_sp_code,s.proj_number,s.area_id,r.region_name,o.operator_name')
->leftJoin(['station_info'=>'s'],'em.station_id = s.station_id')
->leftJoin(['region'=>'r'],'s.area_id = r.id')
->leftJoin(['operator'=>'o'],'o.operator_id = s.original_sp_id')
// ->leftJoin(['em_rule'=>'er'],'er.em_id=em.id')
// ->leftJoin(['dc'=>'d'],'em.dc_id=d.id')
->with('account,rule')
->where($map)
->count();
return $data;
}
public function account(){
return $this->hasOne('em_account','em_id');
}
//通过id获取单个电表信息
public function getEmInfoById($id,$mode=false){
if($mode === false)
$em = self::with('rule,dc,photo')->find($id);
else
$em = self::with($mode)->find($id);
return $em;
}
/**
* @param $id
* @param bool $curr 获取本次抄表记录还是上次抄表记录
* @return array|mixed|\PDOStatement|string|\think\Model|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getEmUseageInfoById($id,$curr=false){
$em = $this->getEmInfoById($id,'type,rule');
//最后一次抄表记录
$ue = Useage::with('photo')->order('create_time desc,id desc')->get(['em_id'=>$id]);
//如果没有超过表 不论是峰谷表还是普通表 都是空的
if(!$ue) {
//如果不是详情页
$em->last_date = $em->init_date;
$em->last_numb = $em->init_numb;
//如果是峰谷表 初始化
if($em->rule->pricing_type == 2) {
$ulist[] = ['typeName' => '尖峰段', 'last_numb' => 0, 'current_numb' => ''];
$ulist[] = ['typeName' => '峰段', 'last_numb' => 0, 'current_numb' => ''];
$ulist[] = ['typeName' => '谷段', 'last_numb' => 0, 'current_numb' => ''];
$ulist[] = ['typeName' => '平段', 'last_numb' => 0, 'current_numb' => ''];
} else {
$ulist[] = ['typeName' => '普通', 'last_numb' => 0, 'current_numb' => ''];
}
$em->last_sum_numb = 0;
$em->useage = $ulist;
//历史抄表
if($em->rule->settle_type==1){
$em->is_history = 1;
}
} else {
$em->last_date = $ue->current_date;
$em->last_sum_numb = $ue->current_sum_numb;
$fields = 'last_numb,current_numb,price';
if($em->rule->pricing_type == 2){
$ulist = UseageDetail
::field('case type
when 1 then "尖峰段"
when 2 then "峰段"
when 3 then "谷段"
when 4 then "平段"
else "普通" end as typeName
')
->field($fields)
->where(['useage_id'=>$ue->useage_id])->select();
foreach ($ulist as &$u){
$u->last_numb = $u->current_numb;
$u->current_numb = '';
$u->price = '';
}
$em->useage = $ulist;
}
$em->photo = $ue->photo;
}
return $em;
}
//获取电表信息与station信息
public function getBaseInfo($em_id){
$data = self::field('em.id,em.em_numb,em.em_type_id')
->field('s.station_sp_code,s.station_name')
->field('et.name as typeName')
->leftJoin(['station_info'=>'s'],'s.station_id=em.station_id')
->leftJoin(['em_type'=>'et'],'et.id=em.em_type_id')
->where('em.id','=',$em_id)
->find();
return $data;
}
//关联模型
public function rule(){
return $this->hasOne('EmRule','em_id','id');
}
public function dc(){
return $this->belongsTo('Dc','dc_id');
}
public function photo(){
return $this->belongsTo('Receipt','photo_id','id');
}
public function type(){
return $this->belongsTo('EmType','em_type_id','id');
}
}