forked from free-lancers/electricity_bill_calc_service
Compare commits
206 Commits
fiber
...
0f91b8a332
| Author | SHA1 | Date | |
|---|---|---|---|
| 0f91b8a332 | |||
| 59a604bab0 | |||
| eaf45331f1 | |||
| 2844db1a86 | |||
| 7d3fafeb04 | |||
| 032d38181a | |||
| 292a50029f | |||
| de176ce61d | |||
| 8a383515d3 | |||
| 37bd0da1f2 | |||
| bfa7ac1508 | |||
| 3f36153968 | |||
| 845bd75348 | |||
| 559c2d439d | |||
| d6829fce27 | |||
| d1e8c63ec9 | |||
| e229f3976d | |||
| 683907b363 | |||
| c1b84d1fbb | |||
| 2021d67d03 | |||
| b988ffcb75 | |||
| 361b302407 | |||
| d8f470e660 | |||
| c472eb4196 | |||
| 98866a8de5 | |||
| c89a5f20ad | |||
| 01e944cb5a | |||
| c2d43bd98e | |||
| a543b33276 | |||
| 6289257ac1 | |||
| 5d997c14ee | |||
| b50eabbca6 | |||
| a34aa6ad07 | |||
| 3d918eea85 | |||
| 7f46f2f36b | |||
| ed10996a06 | |||
| 2aa3729e03 | |||
| 7d59121c2f | |||
| b2efb972dc | |||
| 5f750dd0e0 | |||
| dd6becc994 | |||
| b59e24333e | |||
| 00cf35ec2a | |||
| ed5a3f6c0a | |||
| a593666542 | |||
| 8a4be5328e | |||
| 0135b81080 | |||
| fb328514d1 | |||
| ad1b816795 | |||
| fe7f8bd692 | |||
| f55cc07b1c | |||
| a3d4dbfca5 | |||
| 2bab62512a | |||
| e3c34c96e2 | |||
| a34fee8339 | |||
| 69357f77dd | |||
| 27a499b8cc | |||
| 568deaf972 | |||
| f995b89ea9 | |||
| cd56f2b444 | |||
| 8163fd99ee | |||
| 0e365cdb6b | |||
| 020e76b901 | |||
| 79d55505a7 | |||
| af359f4429 | |||
| ce4c483bcb | |||
| 94e19f4c5a | |||
| c916301f6b | |||
| 5710a640e8 | |||
| 6b3d3dd93c | |||
| 8fc463bd9d | |||
| f688f50ecb | |||
| f254ec1f3a | |||
| 9b899be33d | |||
| c36bfff05a | |||
| b84c51b18e | |||
| 1dd5f1049d | |||
| 18d48c7fea | |||
| 8ab89bca34 | |||
| b64929c10a | |||
| 1099a7c335 | |||
| 5866882c2d | |||
| 9ad3415cdb | |||
| 39e404451e | |||
| 251c44049a | |||
| b3032638fc | |||
| d8a29e7d17 | |||
| 6fece99e00 | |||
| ab44ff5cc4 | |||
| 61edef5c92 | |||
| 648fc0f370 | |||
|
|
7f2ec68197 | ||
|
|
9dc846c044 | ||
|
|
b7eaaffc3a | ||
|
|
1db60a0e4f | ||
|
|
06f86e3cd4 | ||
|
|
ccc6cac4df | ||
|
|
3ab505e446 | ||
|
|
26a951f970 | ||
|
|
0a9d5cd121 | ||
|
|
4df3efacd8 | ||
|
|
0c389e440a | ||
|
|
a1a72b7204 | ||
|
|
a626869f14 | ||
|
|
b4ce754c0d | ||
|
|
7806f07766 | ||
|
|
877b8304c3 | ||
|
|
b224f51613 | ||
|
|
ac36c158c0 | ||
|
|
037e6258d1 | ||
|
|
aec1655f1c | ||
|
|
0246eaba27 | ||
|
|
e6d9435c14 | ||
|
|
062cc6a9ea | ||
|
|
2792959d1e | ||
|
|
fa03bf5dbd | ||
|
|
fbe4036389 | ||
|
|
b2e4fb809f | ||
|
|
0d73665313 | ||
|
|
316553d81a | ||
|
|
2b5272dad9 | ||
|
|
234e811324 | ||
|
|
541932d62e | ||
|
|
e88477a710 | ||
|
|
11bd661e79 | ||
|
|
986562c2c2 | ||
|
|
e5dadf06be | ||
|
|
db52a140b1 | ||
|
|
ce2bb160e1 | ||
|
|
24f2c26d86 | ||
|
|
fb388c53c7 | ||
|
|
b244fd5823 | ||
|
|
2c303cfba7 | ||
|
|
2d6bff5828 | ||
|
|
577ac9d1dc | ||
|
|
7531a6a5a1 | ||
|
|
be270597f5 | ||
|
|
77587b8157 | ||
|
|
d97db0cf50 | ||
|
|
2558a83024 | ||
|
|
7da5bd1112 | ||
|
|
d4fbf86800 | ||
|
|
c9b7dc3aec | ||
|
|
424ba2e839 | ||
|
|
0cffc1b6d1 | ||
|
|
ef325aede5 | ||
|
|
e2a61d58ac | ||
|
|
542efdac86 | ||
|
|
7c6f211931 | ||
|
|
23e9e2ec4d | ||
|
|
6f0edc54bf | ||
|
|
7bac667c2a | ||
|
|
46ae943653 | ||
|
|
2339e4c725 | ||
|
|
e366888608 | ||
|
|
cadb2db8c7 | ||
|
|
7476278c52 | ||
|
|
bfa9da4a03 | ||
|
|
c2a56c8253 | ||
|
|
5d938da53e | ||
|
|
686890b5d8 | ||
|
|
50418f2e30 | ||
|
|
0020776218 | ||
|
|
48753eb3f0 | ||
|
|
ab2bcb3cf6 | ||
|
|
005f9020a1 | ||
|
|
2d17bd5f0d | ||
|
|
2f17853dc0 | ||
|
|
ea1c1d7829 | ||
|
|
c302b32367 | ||
|
|
7f43965f1c | ||
|
|
0c725e99a4 | ||
|
|
33ccd7e0b6 | ||
|
|
40abbcfb8c | ||
|
|
62560e4eeb | ||
|
|
28b1478e9a | ||
|
|
6bf4009338 | ||
|
|
bfb59a3626 | ||
|
|
8aa3a054b0 | ||
|
|
1fd5e7b9aa | ||
|
|
85f4d04a7f | ||
|
|
c22e7e7dc0 | ||
|
|
98f3bdec0a | ||
|
|
f8ef6aba98 | ||
|
|
097e25f070 | ||
|
|
cd723e98e3 | ||
|
|
919883f521 | ||
|
|
04dc9c51aa | ||
|
|
ee55507e05 | ||
|
|
e5b5322e0d | ||
|
|
bca0fd777d | ||
|
|
6efe16e2fe | ||
|
|
afc0114181 | ||
|
|
9aa32d99b9 | ||
|
|
71f39c8c2f | ||
|
|
73737c3753 | ||
|
|
61fef8d0fa | ||
|
|
523e6215f4 | ||
|
|
47cd27c968 | ||
|
|
0d5457fca3 | ||
|
|
28609df9ec | ||
|
|
31ec847ab2 | ||
|
|
83b0cd89f4 | ||
|
|
1ccd19d78b | ||
|
|
2b30481419 | ||
|
|
ac94c578d6 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -161,6 +161,7 @@ atlassian-ide-plugin.xml
|
||||
|
||||
# Cursive Clojure plugin
|
||||
.idea/replstate.xml
|
||||
.idea/electricity_bill_calc_service.iml
|
||||
|
||||
# SonarLint plugin
|
||||
.idea/sonarlint/
|
||||
|
||||
9
.idea/0.2.iml
generated
Normal file
9
.idea/0.2.iml
generated
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
28
.idea/codeStyles/Project.xml
generated
Normal file
28
.idea/codeStyles/Project.xml
generated
Normal file
@@ -0,0 +1,28 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<code_scheme name="Project" version="173">
|
||||
<DBN-PSQL>
|
||||
<case-options enabled="true">
|
||||
<option name="KEYWORD_CASE" value="lower" />
|
||||
<option name="FUNCTION_CASE" value="lower" />
|
||||
<option name="PARAMETER_CASE" value="lower" />
|
||||
<option name="DATATYPE_CASE" value="lower" />
|
||||
<option name="OBJECT_CASE" value="preserve" />
|
||||
</case-options>
|
||||
<formatting-settings enabled="false" />
|
||||
</DBN-PSQL>
|
||||
<DBN-SQL>
|
||||
<case-options enabled="true">
|
||||
<option name="KEYWORD_CASE" value="lower" />
|
||||
<option name="FUNCTION_CASE" value="lower" />
|
||||
<option name="PARAMETER_CASE" value="lower" />
|
||||
<option name="DATATYPE_CASE" value="lower" />
|
||||
<option name="OBJECT_CASE" value="preserve" />
|
||||
</case-options>
|
||||
<formatting-settings enabled="false">
|
||||
<option name="STATEMENT_SPACING" value="one_line" />
|
||||
<option name="CLAUSE_CHOP_DOWN" value="chop_down_if_statement_long" />
|
||||
<option name="ITERATION_ELEMENTS_WRAPPING" value="chop_down_if_not_single" />
|
||||
</formatting-settings>
|
||||
</DBN-SQL>
|
||||
</code_scheme>
|
||||
</component>
|
||||
11
.idea/dataSources.xml
generated
Normal file
11
.idea/dataSources.xml
generated
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||
<data-source source="LOCAL" name="PostgreSQL - postgres@39.105.39.8" uuid="996b1b9f-5c40-4bd6-8c3c-0af67aaaa15d">
|
||||
<driver-ref>postgresql</driver-ref>
|
||||
<synchronize>true</synchronize>
|
||||
<jdbc-driver>org.postgresql.Driver</jdbc-driver>
|
||||
<jdbc-url>jdbc:postgresql://39.105.39.8:9432/postgres</jdbc-url>
|
||||
</data-source>
|
||||
</component>
|
||||
</project>
|
||||
9
.idea/electricity_bill_calc_service.iml
generated
Normal file
9
.idea/electricity_bill_calc_service.iml
generated
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
4
.idea/electricity_bill_calc_service.iml.bak
generated
Normal file
4
.idea/electricity_bill_calc_service.iml.bak
generated
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
</module>
|
||||
6
.idea/misc.xml
generated
Normal file
6
.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="JavaScriptSettings">
|
||||
<option name="languageLevel" value="ES6" />
|
||||
</component>
|
||||
</project>
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/electricity_bill_calc_service.iml" filepath="$PROJECT_DIR$/.idea/electricity_bill_calc_service.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -1,4 +1,4 @@
|
||||
FROM golang:1.19-alpine AS builder
|
||||
FROM dockerproxy.com/library/golang:1.20-alpine AS builder
|
||||
|
||||
ENV GO111MODULE=on
|
||||
ENV GOPROXY="https://goproxy.io"
|
||||
@@ -9,7 +9,7 @@ RUN go mod download && go mod verify
|
||||
ADD . /app
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -tags=jsoniter -v -o server .
|
||||
|
||||
FROM alpine:latest AS production
|
||||
FROM dockerproxy.com/library/alpine:latest AS production
|
||||
RUN echo "https://mirror.tuna.tsinghua.edu.cn/alpine/v3.4/main/" > /etc/apk/repositories
|
||||
RUN apk add --no-cache tzdata
|
||||
RUN apk update \
|
||||
|
||||
183
_1.gitignore
Normal file
183
_1.gitignore
Normal file
@@ -0,0 +1,183 @@
|
||||
# If you prefer the allow list template instead of the deny list, see community template:
|
||||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
|
||||
#
|
||||
# Binaries for programs and plugins
|
||||
*.exe
|
||||
*.exe~
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
|
||||
# Test binary, built with `go test -c`
|
||||
*.test
|
||||
|
||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
# Go workspace file
|
||||
go.work
|
||||
|
||||
# Windows thumbnail cache files
|
||||
Thumbs.db
|
||||
Thumbs.db:encryptable
|
||||
ehthumbs.db
|
||||
ehthumbs_vista.db
|
||||
|
||||
# Dump file
|
||||
*.stackdump
|
||||
|
||||
# Folder config file
|
||||
[Dd]esktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Windows Installer files
|
||||
*.cab
|
||||
*.msi
|
||||
*.msix
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
# Windows shortcuts
|
||||
*.lnk
|
||||
|
||||
# General
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
|
||||
# Icon must end with two \r
|
||||
Icon
|
||||
|
||||
# Thumbnails
|
||||
._*
|
||||
|
||||
# Files that might appear in the root of a volume
|
||||
.DocumentRevisions-V100
|
||||
.fseventsd
|
||||
.Spotlight-V100
|
||||
.TemporaryItems
|
||||
.Trashes
|
||||
.VolumeIcon.icns
|
||||
.com.apple.timemachine.donotpresent
|
||||
|
||||
# Directories potentially created on remote AFP share
|
||||
.AppleDB
|
||||
.AppleDesktop
|
||||
Network Trash Folder
|
||||
Temporary Items
|
||||
.apdisk
|
||||
|
||||
*~
|
||||
|
||||
# temporary files which can be created if a process still has a handle open of a deleted file
|
||||
.fuse_hidden*
|
||||
|
||||
# KDE directory preferences
|
||||
.directory
|
||||
|
||||
# Linux trash folder which might appear on any partition or disk
|
||||
.Trash-*
|
||||
|
||||
# .nfs files are created when an open file is removed but is still being accessed
|
||||
.nfs*
|
||||
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
!.vscode/*.code-snippets
|
||||
|
||||
# Local History for Visual Studio Code
|
||||
.history/
|
||||
|
||||
# Built Visual Studio Code Extensions
|
||||
*.vsix
|
||||
|
||||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
||||
|
||||
# User-specific stuff
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/**/usage.statistics.xml
|
||||
.idea/**/dictionaries
|
||||
.idea/**/shelf
|
||||
|
||||
# AWS User-specific
|
||||
.idea/**/aws.xml
|
||||
|
||||
# Generated files
|
||||
.idea/**/contentModel.xml
|
||||
|
||||
# Sensitive or high-churn files
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
.idea/**/dbnavigator.xml
|
||||
|
||||
# Gradle
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
# since they will be recreated, and may cause churn. Uncomment if using
|
||||
# auto-import.
|
||||
# .idea/artifacts
|
||||
# .idea/compiler.xml
|
||||
# .idea/jarRepositories.xml
|
||||
# .idea/modules.xml
|
||||
# .idea/*.iml
|
||||
# .idea/modules
|
||||
# *.iml
|
||||
# *.ipr
|
||||
|
||||
# CMake
|
||||
cmake-build-*/
|
||||
|
||||
# Mongo Explorer plugin
|
||||
.idea/**/mongoSettings.xml
|
||||
|
||||
# File-based project format
|
||||
*.iws
|
||||
|
||||
# IntelliJ
|
||||
out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Cursive Clojure plugin
|
||||
.idea/replstate.xml
|
||||
|
||||
# SonarLint plugin
|
||||
.idea/sonarlint/
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
||||
# Editor-based Rest Client
|
||||
.idea/httpRequests
|
||||
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
# Block sensitive configuration files
|
||||
settings.local.yaml
|
||||
log/
|
||||
__debug_bin
|
||||
Binary file not shown.
40
cache/abstract.go
vendored
40
cache/abstract.go
vendored
@@ -40,7 +40,7 @@ func Cache[T interface{}](key string, value *T, expires time.Duration) error {
|
||||
}
|
||||
|
||||
// 从Redis缓存中获取一个数据
|
||||
func Retreive[T interface{}](key string) (*T, error) {
|
||||
func Retrieve[T interface{}](key string) (*T, error) {
|
||||
getCmd := global.Rd.B().Get().Key(key).Build()
|
||||
result := global.Rd.Do(global.Ctx, getCmd)
|
||||
if result.Error() != nil {
|
||||
@@ -81,23 +81,23 @@ func Delete(key string) (bool, error) {
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
func dissembleScan(result rueidis.RedisResult) (int64, []string, error) {
|
||||
func dissembleScan(result rueidis.RedisResult) (uint64, []string, error) {
|
||||
var (
|
||||
err error
|
||||
cursor int64
|
||||
cursor uint64
|
||||
keys = make([]string, 0)
|
||||
)
|
||||
results, err := result.ToArray()
|
||||
if err != nil {
|
||||
return -1, keys, err
|
||||
return 0, keys, err
|
||||
}
|
||||
cursor, err = results[0].AsInt64()
|
||||
cursor, err = results[0].AsUint64()
|
||||
if err != nil {
|
||||
return -1, keys, err
|
||||
return 0, keys, err
|
||||
}
|
||||
keys, err = results[1].AsStrSlice()
|
||||
if err != nil {
|
||||
return -1, keys, err
|
||||
return 0, keys, err
|
||||
}
|
||||
return cursor, keys, err
|
||||
}
|
||||
@@ -106,7 +106,7 @@ func dissembleScan(result rueidis.RedisResult) (int64, []string, error) {
|
||||
func DeleteAll(pattern string) error {
|
||||
var (
|
||||
err error
|
||||
cursor int64
|
||||
cursor uint64
|
||||
keys = make([]string, 0)
|
||||
sKeys []string
|
||||
)
|
||||
@@ -137,3 +137,27 @@ func CacheKey(category string, ids ...string) string {
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
type ToString[T any] interface {
|
||||
ToString() string
|
||||
*T
|
||||
}
|
||||
|
||||
// 用于生成一个内容可以为空的Redis缓存键,这个键的类型必须实现了`ToString`接口。
|
||||
func NullableConditionKey[P any, T ToString[P]](value T, defaultStr ...string) string {
|
||||
defaultStr = append(defaultStr, "UNDEF")
|
||||
if value == nil {
|
||||
return defaultStr[0]
|
||||
} else {
|
||||
return value.ToString()
|
||||
}
|
||||
}
|
||||
|
||||
// 用于生成一个内容可以为空的字符串指针类型的Redis缓存键。
|
||||
func NullableStringKey(value *string, defaultStr ...string) string {
|
||||
defaultStr = append(defaultStr, "UNDEF")
|
||||
if value == nil {
|
||||
return defaultStr[0]
|
||||
}
|
||||
return *value
|
||||
}
|
||||
|
||||
10
cache/count.go
vendored
10
cache/count.go
vendored
@@ -10,7 +10,7 @@ type _CountRecord struct {
|
||||
Count int64
|
||||
}
|
||||
|
||||
func assembleCountKey(entityName string, additional ...string) string {
|
||||
func AssembleCountKey(entityName string, additional ...string) string {
|
||||
var keys = make([]string, 0)
|
||||
keys = append(keys, strings.ToUpper(entityName))
|
||||
keys = append(keys, additional...)
|
||||
@@ -24,7 +24,7 @@ func assembleCountKey(entityName string, additional ...string) string {
|
||||
|
||||
// 向缓存中缓存模型名称明确的包含指定条件的实体记录数量
|
||||
func CacheCount(relationNames []string, entityName string, count int64, conditions ...string) error {
|
||||
countKey := assembleCountKey(entityName, conditions...)
|
||||
countKey := AssembleCountKey(entityName, conditions...)
|
||||
cacheInstance := &_CountRecord{Count: count}
|
||||
err := Cache(countKey, cacheInstance, 5*time.Minute)
|
||||
for _, relationName := range relationNames {
|
||||
@@ -34,8 +34,8 @@ func CacheCount(relationNames []string, entityName string, count int64, conditio
|
||||
}
|
||||
|
||||
// 从缓存中获取模型名称明确的,包含指定条件的实体记录数量
|
||||
func RetreiveCount(entityName string, condtions ...string) (int64, error) {
|
||||
countKey := assembleCountKey(entityName, condtions...)
|
||||
func RetrieveCount(entityName string, condtions ...string) (int64, error) {
|
||||
countKey := AssembleCountKey(entityName, condtions...)
|
||||
exist, err := Exists(countKey)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
@@ -43,7 +43,7 @@ func RetreiveCount(entityName string, condtions ...string) (int64, error) {
|
||||
if !exist {
|
||||
return -1, nil
|
||||
}
|
||||
instance, err := Retreive[_CountRecord](countKey)
|
||||
instance, err := Retrieve[_CountRecord](countKey)
|
||||
if instance != nil && err == nil {
|
||||
return instance.Count, nil
|
||||
} else {
|
||||
|
||||
12
cache/entity.go
vendored
12
cache/entity.go
vendored
@@ -6,7 +6,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func assembleEntityKey(entityName, id string) string {
|
||||
func AssembleEntityKey(entityName, id string) string {
|
||||
var keys = make([]string, 0)
|
||||
keys = append(keys, strings.ToUpper(entityName), id)
|
||||
var b strings.Builder
|
||||
@@ -19,7 +19,7 @@ func assembleEntityKey(entityName, id string) string {
|
||||
|
||||
// 缓存模型名称明确的,使用ID进行检索的实体内容。
|
||||
func CacheEntity[T any](instance T, relationNames []string, entityName, id string) error {
|
||||
entityKey := assembleEntityKey(entityName, id)
|
||||
entityKey := AssembleEntityKey(entityName, id)
|
||||
err := Cache(entityKey, &instance, 5*time.Minute)
|
||||
for _, relationName := range relationNames {
|
||||
CacheRelation(relationName, STORE_TYPE_KEY, entityKey)
|
||||
@@ -28,15 +28,15 @@ func CacheEntity[T any](instance T, relationNames []string, entityName, id strin
|
||||
}
|
||||
|
||||
// 从缓存中取出模型名称明确的,使用ID进行检索的实体内容。
|
||||
func RetreiveEntity[T any](entityName, id string) (*T, error) {
|
||||
entityKey := assembleEntityKey(entityName, id)
|
||||
instance, err := Retreive[T](entityKey)
|
||||
func RetrieveEntity[T any](entityName, id string) (*T, error) {
|
||||
entityKey := AssembleEntityKey(entityName, id)
|
||||
instance, err := Retrieve[T](entityKey)
|
||||
return instance, err
|
||||
}
|
||||
|
||||
// 精确的从缓存中删除指定模型名称、指定ID的实体内容。
|
||||
func AbolishSpecificEntity(entityName, id string) (bool, error) {
|
||||
entityKey := assembleEntityKey(entityName, id)
|
||||
entityKey := AssembleEntityKey(entityName, id)
|
||||
return Delete(entityKey)
|
||||
}
|
||||
|
||||
|
||||
6
cache/exists.go
vendored
6
cache/exists.go
vendored
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func assembleExistsKey(entityName string, additional ...string) string {
|
||||
func AssembleExistsKey(entityName string, additional ...string) string {
|
||||
var keys = make([]string, 0)
|
||||
keys = append(keys, strings.ToUpper(entityName))
|
||||
keys = append(keys, additional...)
|
||||
@@ -22,7 +22,7 @@ func assembleExistsKey(entityName string, additional ...string) string {
|
||||
|
||||
// 缓存模型名称明确的、包含指定ID以及一些附加条件的记录
|
||||
func CacheExists(relationNames []string, entityName string, conditions ...string) error {
|
||||
existskey := assembleExistsKey(entityName, conditions...)
|
||||
existskey := AssembleExistsKey(entityName, conditions...)
|
||||
err := Cache(existskey, lo.ToPtr(true), 5*time.Minute)
|
||||
for _, relationName := range relationNames {
|
||||
CacheRelation(relationName, STORE_TYPE_KEY, existskey)
|
||||
@@ -32,7 +32,7 @@ func CacheExists(relationNames []string, entityName string, conditions ...string
|
||||
|
||||
// 从缓存中获取模型名称明确、包含指定ID以及一些附加条件的实体是否存在的标记,函数在返回false时不保证数据库中相关记录也不存在
|
||||
func CheckExists(entityName string, condtions ...string) (bool, error) {
|
||||
existsKey := assembleExistsKey(entityName, condtions...)
|
||||
existsKey := AssembleExistsKey(entityName, condtions...)
|
||||
return Exists(existsKey)
|
||||
}
|
||||
|
||||
|
||||
12
cache/relation.go
vendored
12
cache/relation.go
vendored
@@ -15,13 +15,13 @@ const (
|
||||
STORE_TYPE_HASH = "HASH"
|
||||
)
|
||||
|
||||
func assembleRelationKey(relationName string) string {
|
||||
func AssembleRelationKey(relationName string) string {
|
||||
var keys = make([]string, 0)
|
||||
keys = append(keys, strings.ToUpper(relationName))
|
||||
return CacheKey(TAG_RELATION, keys...)
|
||||
}
|
||||
|
||||
func assembleRelationIdentity(storeType, key string, field ...string) string {
|
||||
func AssembleRelationIdentity(storeType, key string, field ...string) string {
|
||||
var identity = make([]string, 0)
|
||||
identity = append(identity, storeType, key)
|
||||
identity = append(identity, field...)
|
||||
@@ -30,8 +30,8 @@ func assembleRelationIdentity(storeType, key string, field ...string) string {
|
||||
|
||||
// 向缓存中保存与指定关联名称相关联的键的名称以及键的类型和子字段的组成。
|
||||
func CacheRelation(relationName, storeType, key string, field ...string) error {
|
||||
relationKey := assembleRelationKey(relationName)
|
||||
relationIdentity := assembleRelationIdentity(storeType, key, field...)
|
||||
relationKey := AssembleRelationKey(relationName)
|
||||
relationIdentity := AssembleRelationIdentity(storeType, key, field...)
|
||||
cmd := global.Rd.B().Sadd().Key(relationKey).Member(relationIdentity).Build()
|
||||
result := global.Rd.Do(global.Ctx, cmd)
|
||||
return result.Error()
|
||||
@@ -39,7 +39,7 @@ func CacheRelation(relationName, storeType, key string, field ...string) error {
|
||||
|
||||
// 从缓存中清理指定的关联键
|
||||
func AbolishRelation(relationName string) error {
|
||||
relationKey := assembleRelationKey(relationName)
|
||||
relationKey := AssembleRelationKey(relationName)
|
||||
cmd := global.Rd.B().Smembers().Key(relationKey).Build()
|
||||
relationItems, err := global.Rd.Do(global.Ctx, cmd).AsStrSlice()
|
||||
if err != nil {
|
||||
@@ -74,7 +74,7 @@ func AbolishRelation(relationName string) error {
|
||||
func ClearOrphanRelationItems() error {
|
||||
var (
|
||||
err error
|
||||
cursor int64
|
||||
cursor uint64
|
||||
keys = make([]string, 0)
|
||||
sKeys []string
|
||||
)
|
||||
|
||||
62
cache/search.go
vendored
62
cache/search.go
vendored
@@ -1,12 +1,17 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/logger"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func assembleSearchKey(entityName string, additional ...string) string {
|
||||
var log = logger.Named("Cache")
|
||||
|
||||
func AssembleSearchKey(entityName string, additional ...string) string {
|
||||
var keys = make([]string, 0)
|
||||
keys = append(keys, strings.ToUpper(entityName))
|
||||
keys = append(keys, additional...)
|
||||
@@ -20,7 +25,7 @@ func assembleSearchKey(entityName string, additional ...string) string {
|
||||
|
||||
// 缓存模型名称明确的,使用或者包含非ID检索条件的实体内容。
|
||||
func CacheSearch[T any](instance T, relationNames []string, entityName string, conditions ...string) error {
|
||||
searchKey := assembleSearchKey(entityName, conditions...)
|
||||
searchKey := AssembleSearchKey(entityName, conditions...)
|
||||
err := Cache(searchKey, &instance, 5*time.Minute)
|
||||
for _, relationName := range relationNames {
|
||||
CacheRelation(relationName, STORE_TYPE_KEY, searchKey)
|
||||
@@ -29,9 +34,9 @@ func CacheSearch[T any](instance T, relationNames []string, entityName string, c
|
||||
}
|
||||
|
||||
// 从缓存中取得模型名称明确的,使用或者包含非ID检索条件的实体内容。
|
||||
func RetreiveSearch[T any](entityName string, conditions ...string) (*T, error) {
|
||||
searchKey := assembleSearchKey(entityName, conditions...)
|
||||
instance, err := Retreive[T](searchKey)
|
||||
func RetrieveSearch[T any](entityName string, conditions ...string) (*T, error) {
|
||||
searchKey := AssembleSearchKey(entityName, conditions...)
|
||||
instance, err := Retrieve[T](searchKey)
|
||||
return instance, err
|
||||
}
|
||||
|
||||
@@ -40,3 +45,50 @@ func AbolishSearch(entityName string) error {
|
||||
pattern := fmt.Sprintf("%s:%s:*", TAG_SEARCH, strings.ToUpper(entityName))
|
||||
return DeleteAll(pattern)
|
||||
}
|
||||
|
||||
// 向缓存中保存指定模型名称的分页检索结果,会同时采用`CacheCount`中的方法保存检索结果的总数量。
|
||||
func CachePagedSearch[T any](instance T, total int64, relationNames []string, entityName string, conditions ...string) error {
|
||||
searchKey := AssembleSearchKey(entityName, conditions...)
|
||||
countKey := AssembleCountKey(entityName, conditions...)
|
||||
err := Cache(searchKey, &instance, 5*time.Minute)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cacheInstance := &_CountRecord{Count: total}
|
||||
err = Cache(countKey, cacheInstance, 5*time.Minute)
|
||||
for _, relationName := range relationNames {
|
||||
CacheRelation(relationName, STORE_TYPE_KEY, searchKey)
|
||||
CacheRelation(relationName, STORE_TYPE_KEY, countKey)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// 从缓存中获取指定模型名称的分页检索结果,会同时采用`RetrieveCount`中的方法获取检索结果的总数量。
|
||||
func RetrievePagedSearch[T any](entityName string, conditions ...string) (*T, int64, error) {
|
||||
searchKey := AssembleSearchKey(entityName, conditions...)
|
||||
countKey := AssembleCountKey(entityName, conditions...)
|
||||
instance, err := Retrieve[T](searchKey)
|
||||
if err != nil {
|
||||
return nil, -1, err
|
||||
}
|
||||
count, err := Retrieve[_CountRecord](countKey)
|
||||
if err != nil {
|
||||
return nil, -1, err
|
||||
}
|
||||
if instance == nil || count == nil {
|
||||
log.Warn("检索结果或者检索总数为空。", zap.String("searchKey", searchKey), zap.String("countKey", countKey))
|
||||
return nil, -1, nil
|
||||
}
|
||||
return instance, count.Count, nil
|
||||
}
|
||||
|
||||
// 从缓存中删除指定模型名称的分页检索结果,会同时采用`AbolishCountEntity`中的方法删除检索结果的总数量。
|
||||
func AbolishPagedSearch(entityName string) error {
|
||||
pattern := fmt.Sprintf("%s:%s:*", TAG_SEARCH, strings.ToUpper(entityName))
|
||||
err := DeleteAll(pattern)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pattern = fmt.Sprintf("%s:%s:*", TAG_COUNT, strings.ToUpper(entityName))
|
||||
return DeleteAll(pattern)
|
||||
}
|
||||
|
||||
4
cache/session.go
vendored
4
cache/session.go
vendored
@@ -21,9 +21,9 @@ func CacheSession(session *model.Session) error {
|
||||
return Cache(key, session, config.ServiceSettings.MaxSessionLife)
|
||||
}
|
||||
|
||||
func RetreiveSession(token string) (*model.Session, error) {
|
||||
func RetrieveSession(token string) (*model.Session, error) {
|
||||
key := SessionKey(token)
|
||||
return Retreive[model.Session](key)
|
||||
return Retrieve[model.Session](key)
|
||||
}
|
||||
|
||||
func HasSession(token string) (bool, error) {
|
||||
|
||||
@@ -31,8 +31,14 @@ type RedisSetting struct {
|
||||
|
||||
type ServiceSetting struct {
|
||||
MaxSessionLife time.Duration
|
||||
ItemsPageSize int
|
||||
ItemsPageSize uint
|
||||
CacheLifeTime time.Duration
|
||||
HostSerial int64
|
||||
}
|
||||
|
||||
// 读取基准线损率
|
||||
type BaseLossSetting struct {
|
||||
Base string
|
||||
}
|
||||
|
||||
// 定义全局变量
|
||||
@@ -41,6 +47,7 @@ var (
|
||||
DatabaseSettings *DatabaseSetting
|
||||
RedisSettings *RedisSetting
|
||||
ServiceSettings *ServiceSetting
|
||||
BaseLoss *BaseLossSetting
|
||||
)
|
||||
|
||||
// 读取配置到全局变量
|
||||
@@ -68,5 +75,10 @@ func SetupSetting() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.ReadSection("BaselineLineLossRatio", &BaseLoss)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,8 +3,12 @@ package controller
|
||||
import (
|
||||
"electricity_bill_calc/exceptions"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/repository"
|
||||
"electricity_bill_calc/response"
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func _retreiveSession(c *fiber.Ctx) (*model.Session, error) {
|
||||
@@ -18,3 +22,26 @@ func _retreiveSession(c *fiber.Ctx) (*model.Session, error) {
|
||||
}
|
||||
return userSession, nil
|
||||
}
|
||||
|
||||
// 检查当前用户是否拥有指定园区,在判断完成之后直接产生响应
|
||||
func checkParkBelongs(parkId string, logger *zap.Logger, c *fiber.Ctx, result *response.Result) (bool, error) {
|
||||
session := c.Locals("session")
|
||||
if session == nil {
|
||||
logger.Error("用户会话不存在。")
|
||||
return false, result.Unauthorized("用户会话不存在。")
|
||||
}
|
||||
userSession, ok := session.(*model.Session)
|
||||
if !ok {
|
||||
return false, result.Unauthorized("用户会话格式不正确,需要重新登录")
|
||||
}
|
||||
ok, err := repository.ParkRepository.IsParkBelongs(parkId, userSession.Uid)
|
||||
switch {
|
||||
case err != nil:
|
||||
logger.Error("无法判断园区是否隶属于当前用户。", zap.String("park id", parkId), zap.String("user id", userSession.Uid), zap.Error(err))
|
||||
return false, result.Error(http.StatusInternalServerError, err.Error())
|
||||
case err == nil && !ok:
|
||||
logger.Error("用户试图访问不属于自己的园区。", zap.String("park id", parkId), zap.String("user id", userSession.Uid))
|
||||
return false, result.Forbidden("您无权访问该园区。")
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
@@ -1,93 +1,97 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/repository"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"electricity_bill_calc/service"
|
||||
"electricity_bill_calc/types"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/shopspring/decimal"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func InitializeChargesController(app *fiber.App) {
|
||||
app.Get("/charges", security.OPSAuthorize, listAllCharges)
|
||||
app.Post("/charge", security.OPSAuthorize, recordNewCharge)
|
||||
app.Put("/charge/:uid/:seq", security.OPSAuthorize, modifyChargeState)
|
||||
var chargeLog = logger.Named("Handler", "Charge")
|
||||
|
||||
func InitializeChargeHandlers(router *fiber.App) {
|
||||
router.Get("/charge", searchCharges)
|
||||
router.Post("/charge", createNewUserChargeRecord)
|
||||
router.Put("/charge/:uid/:seq", modifyUserChargeState)
|
||||
}
|
||||
|
||||
func listAllCharges(c *fiber.Ctx) error {
|
||||
// 检索用户的充值记录列表
|
||||
func searchCharges(c *fiber.Ctx) error {
|
||||
chargeLog.Info("检索用户的充值记录列表。")
|
||||
result := response.NewResult(c)
|
||||
requestPage, err := strconv.Atoi(c.Query("page", "1"))
|
||||
keyword := c.Query("keyword", "")
|
||||
page := c.QueryInt("page", 1)
|
||||
beginTime := types.ParseDateWithDefault(c.Query("begin"), types.NewEmptyDate())
|
||||
endTime := types.ParseDateWithDefault(c.Query("end"), types.MaxDate())
|
||||
charges, total, err := repository.ChargeRepository.FindCharges(uint(page), &beginTime, &endTime, &keyword)
|
||||
if err != nil {
|
||||
return result.NotAccept("查询参数[page]格式不正确。")
|
||||
chargeLog.Error("检索用户的充值记录列表失败。", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
requestKeyword := c.Query("keyword", "")
|
||||
requestBeginDate := c.Query("begin", "")
|
||||
requestEndDate := c.Query("end", "")
|
||||
charges, total, err := service.ChargeService.ListPagedChargeRecord(requestKeyword, requestBeginDate, requestEndDate, requestPage)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
}
|
||||
return result.Json(
|
||||
http.StatusOK, "已获取到符合条件的计费记录。",
|
||||
response.NewPagedResponse(requestPage, total).ToMap(),
|
||||
return result.Success(
|
||||
"已经获取到符合条件的计费记录。",
|
||||
response.NewPagedResponse(page, total).ToMap(),
|
||||
fiber.Map{"records": charges},
|
||||
)
|
||||
}
|
||||
|
||||
type _NewChargeFormData struct {
|
||||
UserId string `json:"userId" form:"userId"`
|
||||
Fee decimal.NullDecimal `json:"fee" form:"fee"`
|
||||
Discount decimal.NullDecimal `json:"discount" form:"discount"`
|
||||
Amount decimal.NullDecimal `json:"amount" form:"amount"`
|
||||
ChargeTo model.Date `json:"chargeTo" form:"chargeTo"`
|
||||
}
|
||||
|
||||
func recordNewCharge(c *fiber.Ctx) error {
|
||||
// 创建一条新的用户充值记录
|
||||
func createNewUserChargeRecord(c *fiber.Ctx) error {
|
||||
chargeLog.Info("创建一条新的用户充值记录。")
|
||||
result := response.NewResult(c)
|
||||
formData := new(_NewChargeFormData)
|
||||
if err := c.BodyParser(formData); err != nil {
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
createionForm := new(model.ChargeRecordCreationForm)
|
||||
if err := c.BodyParser(createionForm); err != nil {
|
||||
chargeLog.Error("无法解析创建充值记录的请求数据。", zap.Error(err))
|
||||
return result.Error(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
currentTime := time.Now()
|
||||
newRecord := &model.UserCharge{
|
||||
UserId: formData.UserId,
|
||||
Fee: formData.Fee,
|
||||
Discount: formData.Discount,
|
||||
Amount: formData.Amount,
|
||||
Settled: true,
|
||||
SettledAt: ¤tTime,
|
||||
ChargeTo: formData.ChargeTo,
|
||||
}
|
||||
err := service.ChargeService.CreateChargeRecord(newRecord, true)
|
||||
fee, _ := createionForm.Fee.Decimal.Float64()
|
||||
discount, _ := createionForm.Discount.Decimal.Float64()
|
||||
amount, _ := createionForm.Amount.Decimal.Float64()
|
||||
ok, err := service.ChargeService.RecordUserCharge(
|
||||
createionForm.UserId,
|
||||
&fee,
|
||||
&discount,
|
||||
&amount,
|
||||
createionForm.ChargeTo,
|
||||
true,
|
||||
)
|
||||
if err != nil {
|
||||
chargeLog.Error("创建用户充值记录失败。", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Created("指定用户的服务已延期。")
|
||||
if !ok {
|
||||
chargeLog.Error("创建用户充值记录失败。")
|
||||
return result.NotAccept("创建用户充值记录失败。")
|
||||
} else {
|
||||
return result.Success("创建用户充值记录成功, 指定用户的服务已延期。")
|
||||
}
|
||||
}
|
||||
|
||||
type _StateChangeFormData struct {
|
||||
Cancelled bool `json:"cancelled"`
|
||||
}
|
||||
|
||||
func modifyChargeState(c *fiber.Ctx) error {
|
||||
// 改变用户充值记录的状态
|
||||
func modifyUserChargeState(c *fiber.Ctx) error {
|
||||
chargeLog.Info("改变用户充值记录的状态。")
|
||||
result := response.NewResult(c)
|
||||
formData := new(_StateChangeFormData)
|
||||
if err := c.BodyParser(formData); err != nil {
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
requestUserID := c.Params("uid")
|
||||
requestChargeSeq, err := strconv.Atoi(c.Params("seq", "-1"))
|
||||
if err != nil || requestChargeSeq == -1 {
|
||||
return result.Error(http.StatusNotAcceptable, "参数[记录流水号]解析错误。")
|
||||
}
|
||||
err = service.ChargeService.CancelCharge(int64(requestChargeSeq), requestUserID)
|
||||
uid := c.Params("uid")
|
||||
seq, err := c.ParamsInt("seq")
|
||||
if err != nil {
|
||||
chargeLog.Error("无法解析请求参数。", zap.Error(err))
|
||||
return result.Error(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
ok, err := service.ChargeService.CancelUserCharge(uid, int64(seq))
|
||||
if err != nil {
|
||||
chargeLog.Error("取消用户充值记录失败。", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Updated("指定用户服务延期记录状态已经更新。")
|
||||
if !ok {
|
||||
chargeLog.Error("取消用户充值记录失败。")
|
||||
return result.NotAccept("取消用户充值记录失败。")
|
||||
} else {
|
||||
return result.Success("取消用户充值记录成功。")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,237 +0,0 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"electricity_bill_calc/excel"
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"electricity_bill_calc/service"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/samber/lo"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
func InitializeEndUserController(router *fiber.App) {
|
||||
router.Get("/report/:rid/submeter", security.EnterpriseAuthorize, fetchEndUserInReport)
|
||||
router.Get("/report/:rid/meter/template", downloadEndUserRegisterTemplate)
|
||||
router.Post("/report/:rid/meter/batch", security.EnterpriseAuthorize, uploadEndUserRegisterTemplate)
|
||||
router.Put("/report/:rid/submeter/:pid/:mid", security.EnterpriseAuthorize, modifyEndUserRegisterRecord)
|
||||
router.Get("/end/user/adjusts", security.MustAuthenticated, statEndUserInPeriod)
|
||||
}
|
||||
|
||||
func fetchEndUserInReport(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
if ensure, err := ensureReportBelongs(c, &result, requestReportId); !ensure {
|
||||
return err
|
||||
}
|
||||
keyword := c.Query("keyword")
|
||||
requestPage, err := strconv.Atoi(c.Query("page", "1"))
|
||||
if err != nil {
|
||||
return result.NotAccept("查询参数[page]格式不正确。")
|
||||
}
|
||||
endUsers, totalItem, err := service.EndUserService.SearchEndUserRecord(requestReportId, keyword, requestPage)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
}
|
||||
return result.Json(
|
||||
http.StatusOK,
|
||||
"已获取到符合条件的终端用户集合",
|
||||
response.NewPagedResponse(requestPage, totalItem).ToMap(),
|
||||
fiber.Map{"meters": endUsers},
|
||||
)
|
||||
}
|
||||
|
||||
func downloadEndUserRegisterTemplate(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
users, err := service.EndUserService.AllEndUserRecord(requestReportId)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
}
|
||||
reportIndex, err := service.ReportService.RetreiveReportIndex(requestReportId)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
}
|
||||
park, err := service.ParkService.FetchParkDetail(reportIndex.ParkId)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
}
|
||||
meterType, err := service.ReportService.RetreiveParkEndUserMeterType(requestReportId)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
if meterType == -1 {
|
||||
return result.NotFound("未能确定用户表计类型。")
|
||||
}
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
c.Set("Content-Type", "application/octet-stream")
|
||||
c.Set("Content-Transfer-Encoding", "binary")
|
||||
c.Set("Content-Disposition", fmt.Sprintf("attachment; filename=抄表记录-%s-%s.xlsx", park.Name, reportIndex.Period.Format("2006-01")))
|
||||
|
||||
gen := lo.Ternary[excel.ExcelTemplateGenerator](
|
||||
meterType == 0,
|
||||
excel.NewMeterNonPVExcelTemplateGenerator(),
|
||||
excel.NewMeterPVExcelTemplateGenerator(),
|
||||
)
|
||||
defer gen.Close()
|
||||
gen.WriteMeterData(users)
|
||||
gen.WriteTo(c.Response().BodyWriter())
|
||||
return nil
|
||||
}
|
||||
|
||||
func uploadEndUserRegisterTemplate(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
if ensure, err := ensureReportBelongs(c, &result, requestReportId); !ensure {
|
||||
return err
|
||||
}
|
||||
meterType, err := service.ReportService.RetreiveParkEndUserMeterType(requestReportId)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if meterType == -1 {
|
||||
return result.NotFound("未能确定用户表计类型。")
|
||||
}
|
||||
|
||||
uploadedFile, err := c.FormFile("data")
|
||||
if err != nil {
|
||||
return result.NotAccept("没有接收到上传的档案文件。")
|
||||
}
|
||||
archiveFile, err := uploadedFile.Open()
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
if meterType == 0 {
|
||||
errs := service.EndUserService.BatchImportNonPVRegister(requestReportId, archiveFile)
|
||||
if errs.Len() > 0 {
|
||||
return result.Json(http.StatusInternalServerError, "上传抄表文件存在解析错误", fiber.Map{"errors": errs.Errs})
|
||||
}
|
||||
} else {
|
||||
errs := service.EndUserService.BatchImportPVRegister(requestReportId, archiveFile)
|
||||
if errs.Len() > 0 {
|
||||
return result.Json(http.StatusInternalServerError, "上传抄表文件存在解析错误", fiber.Map{"errors": errs.Errs})
|
||||
}
|
||||
}
|
||||
return result.Json(http.StatusOK, "已经成功完成抄表记录的导入。", fiber.Map{"errors": make([]error, 0)})
|
||||
}
|
||||
|
||||
type ModifyEndUserRegisterFormData struct {
|
||||
CurrentPeriodOverall decimal.NullDecimal `json:"currentPeriodOverall" form:"currentPeriodOverall"`
|
||||
CurrentPeriodCritical decimal.NullDecimal `json:"currentPeriodCritical" form:"currentPeriodCritical"`
|
||||
CurrentPeriodPeak decimal.NullDecimal `json:"currentPeriodPeak" form:"currentPeriodPeak"`
|
||||
CurrentPeriodValley decimal.NullDecimal `json:"currentPeriodValley" form:"currentPeriodValley"`
|
||||
AdjustOverall decimal.NullDecimal `json:"adjustOverall" form:"adjustOverall"`
|
||||
AdjustCritical decimal.NullDecimal `json:"adjustCritical" form:"adjustCritical"`
|
||||
AdjustPeak decimal.NullDecimal `json:"adjustPeak" form:"adjustPeak"`
|
||||
AdjustValley decimal.NullDecimal `json:"adjustValley" form:"adjustValley"`
|
||||
}
|
||||
|
||||
func modifyEndUserRegisterRecord(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
if ensure, err := ensureReportBelongs(c, &result, requestReportId); !ensure {
|
||||
return err
|
||||
}
|
||||
meterType, err := service.ReportService.RetreiveParkEndUserMeterType(requestReportId)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if meterType == -1 {
|
||||
return result.NotFound("未能确定用户表计类型。")
|
||||
}
|
||||
requestParkId := c.Params("pid")
|
||||
requestMeterId := c.Params("mid")
|
||||
formData := new(ModifyEndUserRegisterFormData)
|
||||
if err := c.BodyParser(formData); err != nil {
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
meter, err := service.EndUserService.FetchSpecificEndUserRecord(requestReportId, requestParkId, requestMeterId)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
}
|
||||
if formData.CurrentPeriodOverall.Valid {
|
||||
meter.CurrentPeriodOverall = formData.CurrentPeriodOverall.Decimal
|
||||
}
|
||||
if formData.CurrentPeriodCritical.Valid {
|
||||
meter.CurrentPeriodCritical = formData.CurrentPeriodCritical.Decimal
|
||||
}
|
||||
if formData.CurrentPeriodPeak.Valid {
|
||||
meter.CurrentPeriodPeak = formData.CurrentPeriodPeak.Decimal
|
||||
}
|
||||
if formData.CurrentPeriodValley.Valid {
|
||||
meter.CurrentPeriodValley = formData.CurrentPeriodValley.Decimal
|
||||
}
|
||||
if formData.AdjustOverall.Valid {
|
||||
meter.AdjustOverall = formData.AdjustOverall.Decimal
|
||||
}
|
||||
if formData.AdjustCritical.Valid {
|
||||
meter.AdjustCritical = formData.AdjustCritical.Decimal
|
||||
}
|
||||
if formData.AdjustPeak.Valid {
|
||||
meter.AdjustPeak = formData.AdjustPeak.Decimal
|
||||
}
|
||||
if formData.AdjustValley.Valid {
|
||||
meter.AdjustValley = formData.AdjustValley.Decimal
|
||||
}
|
||||
valid, err := meter.Validate()
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if !valid {
|
||||
return result.NotAccept("抄表数据合法性验证失败。")
|
||||
}
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
tx, err := global.DB.BeginTx(ctx, &sql.TxOptions{})
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
err = service.EndUserService.UpdateEndUserRegisterRecord(&tx, &ctx, *meter)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Success("指定终端用户抄表记录已经更新。")
|
||||
}
|
||||
|
||||
func statEndUserInPeriod(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
requestUser := lo.
|
||||
If(session.Type == model.USER_TYPE_ENT, session.Uid).
|
||||
Else(c.Query("user"))
|
||||
requestPark := c.Query("park")
|
||||
if len(requestPark) > 0 && session.Type == model.USER_TYPE_ENT {
|
||||
if ensure, err := ensureParkBelongs(c, &result, requestPark); !ensure {
|
||||
return err
|
||||
}
|
||||
}
|
||||
startDate := c.Query("start")
|
||||
endDate := c.Query("end")
|
||||
stat, err := service.EndUserService.StatEndUserRecordInPeriod(requestUser, requestPark, startDate, endDate)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Success(
|
||||
"已经完成终端用户的费用统计",
|
||||
fiber.Map{"details": stat},
|
||||
)
|
||||
}
|
||||
24
controller/foundation.go
Normal file
24
controller/foundation.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/config"
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
var foundLog = logger.Named("Handler", "Foundation")
|
||||
|
||||
func InitializeFoundationHandlers(router *fiber.App) {
|
||||
router.Get("/norm/authorized/loss/rate", security.MustAuthenticated, getNormAuthorizedLossRate)
|
||||
}
|
||||
|
||||
func getNormAuthorizedLossRate(c *fiber.Ctx) error {
|
||||
foundLog.Info("获取系统中定义的基准核定线损率")
|
||||
result := response.NewResult(c)
|
||||
BaseLoss := config.BaseLoss
|
||||
return result.Success("已经获取到系统设置的基准核定线损率。",
|
||||
fiber.Map{"normAuthorizedLossRate": BaseLoss.Base},
|
||||
)
|
||||
}
|
||||
@@ -1,176 +1,132 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/exceptions"
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"electricity_bill_calc/service"
|
||||
"net/http"
|
||||
|
||||
"errors"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func InitializeGodModeController(router *fiber.App) {
|
||||
gmR := router.Group("/gm")
|
||||
{
|
||||
gmR.Delete("/report/:rid/summary", security.SingularityAuthorize, gmResetReportSummary)
|
||||
gmR.Delete("/report/:rid/maintenance", security.SingularityAuthorize, gmResetReportMaintenance)
|
||||
gmR.Delete("/report/:rid/meters", security.SingularityAuthorize, gmResetReportEndUserRecord)
|
||||
gmR.Post("/report/:rid/meters", security.SingularityAuthorize, gmResynchronizeReportEndUserRecord)
|
||||
gmR.Delete("/report/:rid", security.SingularityAuthorize, gmResetReport)
|
||||
gmR.Delete("/report/:rid/force", security.SingularityAuthorize, gmDeleteReport)
|
||||
gmR.Delete("/park/:pid/maintenance/:mid", security.SingularityAuthorize, gmDeleteSpecificMaintenance)
|
||||
gmR.Delete("/park/:pid/maintenance", security.SingularityAuthorize, gmDeleteAllMaintenance)
|
||||
gmR.Delete("/park/:pid/meters", security.SingularityAuthorize, gmDeleteAllMeters)
|
||||
gmR.Delete("/park/:pid/force", security.SingularityAuthorize, gmDeletePark)
|
||||
gmR.Delete("/enterprise/:uid/force", security.SingularityAuthorize, gmDeleteUser)
|
||||
}
|
||||
var GmLog = logger.Named("Handler", "GM")
|
||||
|
||||
func InitializeGmController(router *fiber.App) {
|
||||
router.Delete("/gm/tenement", security.SingularityAuthorize, deleteTenement)
|
||||
router.Delete("/gm/park", security.SingularityAuthorize, deletePark)
|
||||
router.Delete("/gm/report", security.SingularityAuthorize, deleteReports)
|
||||
router.Delete("/gm/tenement/meter", security.SingularityAuthorize, deleteTenementMeterRelations)
|
||||
router.Delete("/gm/enterprise", security.SingularityAuthorize, deleteEnterprise)
|
||||
router.Delete("/gm/meter/pooling", security.SingularityAuthorize, deleteMeterPoolingRelations)
|
||||
router.Delete("gm/meter", security.SingularityAuthorize, deleteMeters)
|
||||
}
|
||||
|
||||
func gmResetReportSummary(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
done, err := service.GodModeService.ClearReportSummary(requestReportId)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
//用于将参数转化为切片
|
||||
func getQueryValues(c *fiber.Ctx, paramName string) []string {
|
||||
values := c.Request().URI().QueryArgs().PeekMulti(paramName)
|
||||
result := make([]string, len(values))
|
||||
for i, v := range values {
|
||||
result[i] = string(v)
|
||||
}
|
||||
if !done {
|
||||
return result.Error(http.StatusInternalServerError, "未能成功重置指定报表的园区总览部分。")
|
||||
}
|
||||
return result.Success("指定报表的园区总览已经重置。")
|
||||
return result
|
||||
}
|
||||
|
||||
func gmResetReportMaintenance(c *fiber.Ctx) error {
|
||||
func deleteTenement(c *fiber.Ctx) error {
|
||||
park := c.Query("park", "")
|
||||
tenements := getQueryValues(c, "tenements")
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
done, err := service.GodModeService.ClearReportMaintenances(requestReportId)
|
||||
GmLog.Info("[天神模式]删除指定园区中的商户", zap.String("park", park), zap.Strings("tenements", tenements))
|
||||
|
||||
err := service.GMService.DeleteTenements(park, tenements)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if !done {
|
||||
return result.Error(http.StatusInternalServerError, "未能成功重置指定报表的配电维护费部分。")
|
||||
}
|
||||
return result.Success("指定报表的配电维护费已经重置。")
|
||||
GmLog.Error("[天神模式]删除指定园区中的商户失败", zap.Error(err))
|
||||
return result.Error(500, err.Error())
|
||||
}
|
||||
|
||||
func gmResynchronizeReportEndUserRecord(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
done, err := service.GodModeService.ResynchronizeEndUser(requestReportId)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if !done {
|
||||
return result.Error(http.StatusInternalServerError, "未能成功重置指定报表的抄表记录基本档案。")
|
||||
}
|
||||
return result.Success("指定报表的抄表记录基本档案已经重新同步。")
|
||||
return result.Success("指定商户已经删除。")
|
||||
}
|
||||
|
||||
func gmResetReportEndUserRecord(c *fiber.Ctx) error {
|
||||
func deletePark(c *fiber.Ctx) error {
|
||||
parks := getQueryValues(c, "parks")
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
done, err := service.GodModeService.ResetEndUserRegisterRecords(requestReportId)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if !done {
|
||||
return result.Error(http.StatusInternalServerError, "未能成功重置指定报表的抄表记录部分。")
|
||||
}
|
||||
return result.Success("指定报表的抄表记录已经重置。")
|
||||
GmLog.Info("[天神模式]删除指定园区", zap.Strings("parks", parks))
|
||||
|
||||
if len(parks) < 0 {
|
||||
GmLog.Info("[天神模式]用户未指派园区参数或者未指定需要删除的园区。")
|
||||
return result.Error(http.StatusBadRequest, error.Error(errors.New("必须至少指定一个需要删除的园区!")))
|
||||
}
|
||||
|
||||
func gmResetReport(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
done, err := service.GodModeService.ResetReport(requestReportId)
|
||||
err := service.GMService.DeleteParks(parks)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
GmLog.Error("[天神模式]删除指定园区失败", zap.Error(err))
|
||||
return result.Error(500, err.Error())
|
||||
}
|
||||
if !done {
|
||||
return result.Error(http.StatusInternalServerError, "未能成功重置指定报表。")
|
||||
}
|
||||
return result.Success("指定报表已经重置。")
|
||||
return result.Success("指定园区已经删除。")
|
||||
}
|
||||
|
||||
func gmDeleteReport(c *fiber.Ctx) error {
|
||||
func deleteReports(c *fiber.Ctx) error {
|
||||
pid := c.Query("park")
|
||||
reports := getQueryValues(c, "reports")
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
done, err := service.GodModeService.DeleteReport(requestReportId)
|
||||
GmLog.Info("[天神模式]删除符合条件的报表。", zap.Strings("reports", reports))
|
||||
|
||||
err := service.GMService.DeleteReports(pid, reports)
|
||||
if err != nil {
|
||||
if ipErr, ok := err.(exceptions.ImproperOperateError); ok {
|
||||
return result.NotAccept(ipErr.Message)
|
||||
} else {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
}
|
||||
if !done {
|
||||
return result.Error(http.StatusInternalServerError, "未能成功删除指定报表。")
|
||||
GmLog.Error("[天神模式]删除指定园区中的报表失败。", zap.Error(err))
|
||||
return result.Error(500, err.Error())
|
||||
}
|
||||
return result.Success("指定报表已经删除。")
|
||||
}
|
||||
|
||||
func gmDeleteSpecificMaintenance(c *fiber.Ctx) error {
|
||||
func deleteEnterprise(c *fiber.Ctx) error {
|
||||
uid := c.Query("uid")
|
||||
result := response.NewResult(c)
|
||||
requestParkId := c.Params("pid")
|
||||
requestMaintenanceId := c.Params("mid")
|
||||
done, err := service.GodModeService.RemoveSpecificMaintenance(requestParkId, requestMaintenanceId)
|
||||
GmLog.Info("[天神模式]删除指定企业用户", zap.String("uid", uid))
|
||||
|
||||
err := service.GMService.DeleteEnterprises(uid)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
GmLog.Error("[天神模式]删除指定企业用户失败", zap.Error(err))
|
||||
return result.Error(500, "删除指定企业用户失败。")
|
||||
}
|
||||
if !done {
|
||||
return result.Error(http.StatusInternalServerError, "未能成功删除指定的维护费用记录。")
|
||||
}
|
||||
return result.Success("指定维护费用记录已经删除。")
|
||||
return result.Success("指定企业用户已经删除。")
|
||||
}
|
||||
|
||||
func gmDeleteAllMaintenance(c *fiber.Ctx) error {
|
||||
func deleteTenementMeterRelations(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestParkId := c.Params("pid")
|
||||
done, err := service.GodModeService.RemoveAllMaintenance(requestParkId)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
parkId := c.Query("park")
|
||||
tId := getQueryValues(c, "tenements")
|
||||
metersId := getQueryValues(c, "meters")
|
||||
GmLog.Info("删除指定园区中的商户与表计的关联关系", zap.String("park id", parkId))
|
||||
if err := service.GMService.DeleteTenementMeterRelations(parkId, tId, metersId); err != nil {
|
||||
meterLog.Error("无法删除指定园区中的商户与表计的关联关系", zap.Error(err))
|
||||
return result.NotAccept(err.Error())
|
||||
}
|
||||
if !done {
|
||||
return result.Error(http.StatusInternalServerError, "未能成功删除全部维护费用记录。")
|
||||
}
|
||||
return result.Success("全部维护费用记录已经删除。")
|
||||
return result.Success("删除成功")
|
||||
}
|
||||
|
||||
func gmDeleteAllMeters(c *fiber.Ctx) error {
|
||||
func deleteMeterPoolingRelations(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestParkId := c.Params("pid")
|
||||
done, err := service.GodModeService.RemoveAllMeters(requestParkId)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
parkId := c.Query("park")
|
||||
mId := getQueryValues(c, "meters")
|
||||
GmLog.Info("[天神模式]删除指定园区中的表计公摊关系", zap.String("park id", parkId))
|
||||
if err := service.GMService.DeleteMeterPooling(parkId, mId); err != nil {
|
||||
meterLog.Error("[天神模式]删除指定园区中的表计公摊关系失败", zap.Error(err))
|
||||
return result.Error(500, "删除指定园区中的表计公摊关系失败。")
|
||||
}
|
||||
if !done {
|
||||
return result.Error(http.StatusInternalServerError, "未能成功删除全部终端表计档案记录。")
|
||||
}
|
||||
return result.Success("全部终端表计档案记录已经删除。")
|
||||
return result.Success("指定表计公摊关系已经删除。")
|
||||
|
||||
}
|
||||
|
||||
func gmDeletePark(c *fiber.Ctx) error {
|
||||
func deleteMeters(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestParkId := c.Params("pid")
|
||||
done, err := service.GodModeService.RemovePark(requestParkId)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
parkId := c.Query("park")
|
||||
mId := getQueryValues(c, "meters")
|
||||
GmLog.Info("[天神模式]删除指定园区中的表计", zap.String("park id", parkId))
|
||||
if err := service.GMService.DeleteMeters(parkId, mId); err != nil {
|
||||
meterLog.Error("[天神模式]删除指定园区中的表计失败", zap.Error(err))
|
||||
return result.Error(500, "删除指定园区中的表计失败。")
|
||||
}
|
||||
if !done {
|
||||
return result.Error(http.StatusInternalServerError, "未能成功删除指定的园区。")
|
||||
}
|
||||
return result.Success("指定的园区已经删除。")
|
||||
}
|
||||
|
||||
func gmDeleteUser(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestUserId := c.Params("uid")
|
||||
done, err := service.GodModeService.DeleteUser(requestUserId)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if !done {
|
||||
return result.Error(http.StatusInternalServerError, "未能成功删除指定的用户。")
|
||||
}
|
||||
return result.Success("指定的用户及其关联信息已经删除。")
|
||||
return result.Success("指定表计已经删除。")
|
||||
}
|
||||
|
||||
227
controller/invoice.go
Normal file
227
controller/invoice.go
Normal file
@@ -0,0 +1,227 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/repository"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"electricity_bill_calc/service"
|
||||
"electricity_bill_calc/tools"
|
||||
"electricity_bill_calc/types"
|
||||
"electricity_bill_calc/vo"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/jinzhu/copier"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var invoiceLog = logger.Named("Controller", "Invoice")
|
||||
|
||||
func InitializeInvoiceHandler(router *fiber.App) {
|
||||
router.Get("/invoice", security.MustAuthenticated, listInvoices)
|
||||
router.Post("/invoice", security.EnterpriseAuthorize, createNewInvoiceRecord)
|
||||
router.Post("/invoice/precalculate", security.EnterpriseAuthorize, testCalculateInvoice)
|
||||
router.Get("/invoice/:code", security.EnterpriseAuthorize, getInvoiceDetail)
|
||||
router.Delete("/invoice/:code", security.EnterpriseAuthorize, deleteInvoiceRecord)
|
||||
router.Get("/uninvoiced/tenemennt/:tid/report", security.EnterpriseAuthorize, getUninvoicedTenementReports)
|
||||
}
|
||||
|
||||
// 列出指定园区中的符合条件的发票记录
|
||||
func listInvoices(c *fiber.Ctx) error {
|
||||
invoiceLog.Info("列出指定园区中的符合条件的发票记录")
|
||||
result := response.NewResult(c)
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
invoiceLog.Error("列出指定园区中的符合条件的发票记录失败,不能获取到有效的用户会话。", zap.Error(err))
|
||||
return result.Unauthorized("未能获取到有效的用户会话。")
|
||||
}
|
||||
park := tools.EmptyToNil(c.Query("park"))
|
||||
if session.Type == model.USER_TYPE_ENT && park != nil && len(*park) > 0 {
|
||||
pass, err := checkParkBelongs(*park, invoiceLog, c, &result)
|
||||
if err != nil || !pass {
|
||||
return err
|
||||
}
|
||||
}
|
||||
startDate, err := types.ParseDatep(c.Query("start_date"))
|
||||
if err != nil {
|
||||
invoiceLog.Error("列出指定园区中的符合条件的发票记录失败,开始日期参数解析错误。", zap.Error(err))
|
||||
return result.BadRequest("开始日期参数解析错误。")
|
||||
}
|
||||
endDate, err := types.ParseDatep(c.Query("end_date"))
|
||||
if err != nil {
|
||||
invoiceLog.Error("列出指定园区中的符合条件的发票记录失败,结束日期参数解析错误。", zap.Error(err))
|
||||
return result.BadRequest("结束日期参数解析错误。")
|
||||
}
|
||||
keyword := tools.EmptyToNil(c.Query("keyword"))
|
||||
page := c.QueryInt("page", 1)
|
||||
invoices, total, err := repository.InvoiceRepository.ListInvoice(park, startDate, endDate, keyword, uint(page))
|
||||
if err != nil {
|
||||
invoiceLog.Error("列出指定园区中的符合条件的发票记录失败,检索符合条件的发票记录出现错误。", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "检索符合条件的发票记录出现错误。")
|
||||
}
|
||||
invoiceResponse := make([]*vo.InvoiceResponse, 0)
|
||||
copier.Copy(&invoiceResponse, &invoices)
|
||||
return result.Success(
|
||||
"已经获取到符合条件的发票列表。",
|
||||
response.NewPagedResponse(page, total).ToMap(),
|
||||
fiber.Map{
|
||||
"invoices": invoiceResponse,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// 获取指定发票的详细信息
|
||||
func getInvoiceDetail(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
invoiceNo := tools.EmptyToNil(c.Params("code"))
|
||||
invoiceLog.Info("获取指定发票的详细信息", zap.Stringp("InvoiceNo", invoiceNo))
|
||||
if invoiceNo == nil {
|
||||
invoiceLog.Error("获取指定发票的详细信息失败,未指定发票编号。")
|
||||
return result.BadRequest("未指定发票编号。")
|
||||
}
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
invoiceLog.Error("获取指定发票的详细信息失败,不能获取到有效的用户会话。", zap.Error(err))
|
||||
return result.Unauthorized("未能获取到有效的用户会话。")
|
||||
}
|
||||
pass, err := repository.InvoiceRepository.IsBelongsTo(*invoiceNo, session.Uid)
|
||||
if err != nil {
|
||||
invoiceLog.Error("获取指定发票的详细信息失败,检查发票所属权时出现错误。", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "检查发票所属权时出现错误。")
|
||||
}
|
||||
if !pass {
|
||||
invoiceLog.Error("获取指定发票的详细信息失败,发票不属于当前用户。")
|
||||
return result.Forbidden("不能访问不属于自己的发票。")
|
||||
}
|
||||
invoice, err := repository.InvoiceRepository.GetInvoiceDetail(*invoiceNo)
|
||||
if err != nil {
|
||||
invoiceLog.Error("获取指定发票的详细信息失败,检索发票信息时出现错误。", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "检索发票信息时出现错误。")
|
||||
}
|
||||
if invoice == nil {
|
||||
invoiceLog.Error("获取指定发票的详细信息失败,指定发票不存在。")
|
||||
return result.NotFound("指定发票不存在。")
|
||||
}
|
||||
var invoiceResponse vo.ExtendedInvoiceResponse
|
||||
copier.Copy(&invoiceResponse, &invoice)
|
||||
return result.Success(
|
||||
"已经获取到指定发票的详细信息。",
|
||||
fiber.Map{
|
||||
"invoice": invoiceResponse,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// 获取指定商户下所有尚未开票的核算项目
|
||||
func getUninvoicedTenementReports(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
tenement := tools.EmptyToNil(c.Params("tid"))
|
||||
invoiceLog.Info("获取指定商户下所有尚未开票的核算项目", zap.Stringp("Tenement", tenement))
|
||||
if tenement == nil {
|
||||
invoiceLog.Error("获取指定商户下所有尚未开票的核算项目失败,未指定商户。")
|
||||
return result.BadRequest("未指定商户。")
|
||||
}
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
invoiceLog.Error("获取指定商户下所有尚未开票的核算项目失败,不能获取到有效的用户会话。", zap.Error(err))
|
||||
return result.Unauthorized("未能获取到有效的用户会话。")
|
||||
}
|
||||
pass, err := repository.TenementRepository.IsTenementBelongs(*tenement, session.Uid)
|
||||
if err != nil {
|
||||
invoiceLog.Error("获取指定商户下所有尚未开票的核算项目失败,检查商户所属权时出现错误。", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "检查商户所属权时出现错误。")
|
||||
}
|
||||
if !pass {
|
||||
invoiceLog.Error("获取指定商户下所有尚未开票的核算项目失败,商户不属于当前用户。")
|
||||
return result.Forbidden("不能访问不属于自己的商户。")
|
||||
}
|
||||
reports, err := repository.InvoiceRepository.ListUninvoicedTenementCharges(*tenement)
|
||||
if err != nil {
|
||||
invoiceLog.Error("获取指定商户下所有尚未开票的核算项目失败,检索核算项目时出现错误。", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "检索核算项目时出现错误。")
|
||||
}
|
||||
return result.Success(
|
||||
"已经获取到指定商户下所有尚未开票的核算项目。",
|
||||
fiber.Map{
|
||||
"records": reports,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// 试计算指定发票的票面信息
|
||||
func testCalculateInvoice(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
var form vo.InvoiceCreationForm
|
||||
if err := c.BodyParser(&form); err != nil {
|
||||
invoiceLog.Error("试计算指定发票的票面信息失败,请求表单数据解析错误。", zap.Error(err))
|
||||
return result.BadRequest("请求表单数据解析错误。")
|
||||
}
|
||||
invoiceLog.Info("试计算指定发票的票面信息", zap.String("Park", form.Park), zap.String("Tenement", form.Tenement))
|
||||
if pass, err := checkParkBelongs(form.Park, invoiceLog, c, &result); err != nil || !pass {
|
||||
return err
|
||||
}
|
||||
total, cargos, err := service.InvoiceService.TestCalculateInvoice(form.Park, form.Tenement, form.TaxMethod, form.TaxRate, form.Covers)
|
||||
if err != nil {
|
||||
invoiceLog.Error("试计算指定发票的票面信息失败,试计算发票时出现错误。", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "试计算发票时出现错误。")
|
||||
}
|
||||
return result.Success(
|
||||
"已经计算出指定发票的票面信息。",
|
||||
fiber.Map{
|
||||
"total": total,
|
||||
"cargos": cargos,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// 创建一个新的发票记录
|
||||
func createNewInvoiceRecord(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
var form vo.ExtendedInvoiceCreationForm
|
||||
if err := c.BodyParser(&form); err != nil {
|
||||
invoiceLog.Error("创建一个新的发票记录失败,请求表单数据解析错误。", zap.Error(err))
|
||||
return result.BadRequest("请求表单数据解析错误。")
|
||||
}
|
||||
invoiceLog.Info("创建一个新的发票记录", zap.String("Park", form.Park), zap.String("Tenement", form.Tenement))
|
||||
if pass, err := checkParkBelongs(form.Park, invoiceLog, c, &result); err != nil || !pass {
|
||||
return err
|
||||
}
|
||||
err := service.InvoiceService.SaveInvoice(form.Park, form.Tenement, form.InvoiceNo, form.InvoiceType, form.TaxMethod, form.TaxRate, form.Covers)
|
||||
if err != nil {
|
||||
invoiceLog.Error("创建一个新的发票记录失败,保存发票时出现错误。", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "保存发票时出现错误。")
|
||||
}
|
||||
return result.Created("已经创建了一个新的发票记录。")
|
||||
}
|
||||
|
||||
// 删除指定的发票记录
|
||||
func deleteInvoiceRecord(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
invoiceNo := tools.EmptyToNil(c.Params("code"))
|
||||
invoiceLog.Info("删除指定的发票记录", zap.Stringp("InvoiceNo", invoiceNo))
|
||||
if invoiceNo == nil {
|
||||
invoiceLog.Error("删除指定的发票记录失败,未指定发票编号。")
|
||||
return result.BadRequest("未指定发票编号。")
|
||||
}
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
invoiceLog.Error("删除指定的发票记录失败,不能获取到有效的用户会话。", zap.Error(err))
|
||||
return result.Unauthorized("未能获取到有效的用户会话。")
|
||||
}
|
||||
pass, err := repository.InvoiceRepository.IsBelongsTo(*invoiceNo, session.Uid)
|
||||
if err != nil {
|
||||
invoiceLog.Error("删除指定的发票记录失败,检查发票所属权时出现错误。", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "检查发票所属权时出现错误。")
|
||||
}
|
||||
if !pass {
|
||||
invoiceLog.Error("删除指定的发票记录失败,发票不属于当前用户。")
|
||||
return result.Forbidden("不能删除不属于自己的发票。")
|
||||
}
|
||||
err = service.InvoiceService.DeleteInvoice(*invoiceNo)
|
||||
if err != nil {
|
||||
invoiceLog.Error("删除指定的发票记录失败,删除发票时出现错误。", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "删除发票时出现错误。")
|
||||
}
|
||||
return result.Success("已经删除了指定的发票记录。")
|
||||
}
|
||||
@@ -1,202 +0,0 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"electricity_bill_calc/service"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/samber/lo"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
func InitializeMaintenanceFeeController(router *fiber.App) {
|
||||
router.Get("/maintenance/fee", security.MustAuthenticated, listMaintenanceFees)
|
||||
router.Post("/maintenance/fee", security.EnterpriseAuthorize, createMaintenanceFeeRecord)
|
||||
router.Put("/maintenance/fee/:mid", security.EnterpriseAuthorize, modifyMaintenanceFeeRecord)
|
||||
router.Put("/maintenance/fee/:mid/enabled", security.EnterpriseAuthorize, changeMaintenanceFeeState)
|
||||
router.Delete("/maintenance/fee/:mid", security.EnterpriseAuthorize, deleteMaintenanceFee)
|
||||
router.Get("/additional/charges", security.MustAuthenticated, statAdditionalCharges)
|
||||
}
|
||||
|
||||
func ensureMaintenanceFeeBelongs(c *fiber.Ctx, result *response.Result, requestMaintenanceFeeId string) (bool, error) {
|
||||
userSession, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
return false, result.Unauthorized(err.Error())
|
||||
}
|
||||
sure, err := service.MaintenanceFeeService.EnsureFeeBelongs(userSession.Uid, requestMaintenanceFeeId)
|
||||
if err != nil {
|
||||
return false, result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if !sure {
|
||||
return false, result.Unauthorized("所操作维护费记录不属于当前用户。")
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func listMaintenanceFees(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
userSession, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
requestPark := c.Query("park")
|
||||
requestPeriod := c.Query("period")
|
||||
requestPage, err := strconv.Atoi(c.Query("page", "1"))
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, "不能解析给定的参数[page]。")
|
||||
}
|
||||
if len(requestPark) > 0 {
|
||||
if userSession.Type == model.USER_TYPE_ENT {
|
||||
if ensure, err := ensureParkBelongs(c, &result, requestPark); !ensure {
|
||||
return err
|
||||
}
|
||||
}
|
||||
fees, total, err := service.MaintenanceFeeService.ListMaintenanceFees([]string{requestPark}, requestPeriod, requestPage)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Json(
|
||||
http.StatusOK,
|
||||
"已获取指定园区下的维护费记录",
|
||||
response.NewPagedResponse(requestPage, total).ToMap(),
|
||||
fiber.Map{"fees": fees},
|
||||
)
|
||||
} else {
|
||||
parkIds, err := service.ParkService.AllParkIds(userSession.Uid)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
fees, total, err := service.MaintenanceFeeService.ListMaintenanceFees(parkIds, requestPeriod, requestPage)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Json(
|
||||
http.StatusOK,
|
||||
"已获取指定用户下的所有维护费记录。",
|
||||
response.NewPagedResponse(requestPage, total).ToMap(),
|
||||
fiber.Map{"fees": fees},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
type _FeeCreationFormData struct {
|
||||
ParkId string `json:"parkId" form:"parkId"`
|
||||
Name string `json:"name" form:"name"`
|
||||
Period string `json:"period" form:"period"`
|
||||
Fee decimal.Decimal `json:"fee" form:"fee"`
|
||||
Memo *string `json:"memo" form:"memo"`
|
||||
}
|
||||
|
||||
func createMaintenanceFeeRecord(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
formData := new(_FeeCreationFormData)
|
||||
if err := c.BodyParser(formData); err != nil {
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
if ensure, err := ensureParkBelongs(c, &result, formData.ParkId); !ensure {
|
||||
return err
|
||||
}
|
||||
newMaintenanceFee := &model.MaintenanceFee{}
|
||||
copier.Copy(newMaintenanceFee, formData)
|
||||
err := service.MaintenanceFeeService.CreateMaintenanceFeeRecord(*newMaintenanceFee)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Created("新维护费记录已经创建。")
|
||||
}
|
||||
|
||||
type _FeeModificationFormData struct {
|
||||
Fee decimal.Decimal `json:"fee" form:"fee"`
|
||||
Memo *string `json:"memo" form:"memo"`
|
||||
}
|
||||
|
||||
func modifyMaintenanceFeeRecord(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestFee := c.Params("mid")
|
||||
formData := new(_FeeModificationFormData)
|
||||
if err := c.BodyParser(formData); err != nil {
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
if ensure, err := ensureMaintenanceFeeBelongs(c, &result, requestFee); !ensure {
|
||||
return err
|
||||
}
|
||||
newFeeState := new(model.MaintenanceFee)
|
||||
copier.Copy(newFeeState, formData)
|
||||
newFeeState.Id = requestFee
|
||||
err := service.MaintenanceFeeService.ModifyMaintenanceFee(*newFeeState)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Updated("指定维护费条目已更新。")
|
||||
}
|
||||
|
||||
type _FeeStateFormData struct {
|
||||
Enabled bool `json:"enabled" form:"enabled"`
|
||||
}
|
||||
|
||||
func changeMaintenanceFeeState(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestFee := c.Params("mid")
|
||||
formData := new(_FeeStateFormData)
|
||||
if err := c.BodyParser(formData); err != nil {
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
if ensure, err := ensureMaintenanceFeeBelongs(c, &result, requestFee); !ensure {
|
||||
return err
|
||||
}
|
||||
err := service.MaintenanceFeeService.ChangeMaintenanceFeeState(requestFee, formData.Enabled)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Updated("指定维护费条目状态已更新。")
|
||||
}
|
||||
|
||||
func deleteMaintenanceFee(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestFee := c.Params("mid")
|
||||
if ensure, err := ensureMaintenanceFeeBelongs(c, &result, requestFee); !ensure {
|
||||
return err
|
||||
}
|
||||
err := service.MaintenanceFeeService.DeleteMaintenanceFee(requestFee)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Deleted("指定维护费条目已删除。")
|
||||
}
|
||||
|
||||
func statAdditionalCharges(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
requestUser := lo.
|
||||
If(session.Type == model.USER_TYPE_ENT, session.Uid).
|
||||
Else(c.Query("user"))
|
||||
requestPark := c.Query("park")
|
||||
if len(requestPark) > 0 && session.Type == model.USER_TYPE_ENT {
|
||||
if ensure, err := ensureParkBelongs(c, &result, requestPark); !ensure {
|
||||
return err
|
||||
}
|
||||
}
|
||||
period := c.Query("period", "")
|
||||
keyword := c.Query("keyword", "")
|
||||
requestPage, err := strconv.Atoi(c.Query("page", "1"))
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, "不能解析给定的参数[page]。")
|
||||
}
|
||||
fees, total, err := service.MaintenanceFeeService.QueryAdditionalCharges(requestUser, requestPark, period, keyword, requestPage)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Success(
|
||||
"已经成功获取到物业附加费的统计记录。",
|
||||
response.NewPagedResponse(requestPage, total).ToMap(),
|
||||
fiber.Map{"charges": fees},
|
||||
)
|
||||
}
|
||||
538
controller/meter.go
Normal file
538
controller/meter.go
Normal file
@@ -0,0 +1,538 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/excel"
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/repository"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"electricity_bill_calc/service"
|
||||
"electricity_bill_calc/tools"
|
||||
"electricity_bill_calc/types"
|
||||
"electricity_bill_calc/vo"
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/samber/lo"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var meterLog = logger.Named("Handler", "Meter")
|
||||
|
||||
func InitializeMeterHandlers(router *fiber.App) {
|
||||
router.Get("/meter/choice", security.EnterpriseAuthorize, listUnboundMeters)
|
||||
router.Get("/meter/choice/tenement", security.EnterpriseAuthorize, listUnboundTenementMeters)
|
||||
router.Get("/meter/:pid", security.EnterpriseAuthorize, searchMetersWithinPark)
|
||||
router.Post("/meter/:pid", security.EnterpriseAuthorize, createNewMeterManually)
|
||||
router.Get("/meter/:pid/template", security.EnterpriseAuthorize, downloadMeterArchiveTemplate)
|
||||
router.Post("/meter/:pid/batch", security.EnterpriseAuthorize, uploadMeterArchive)
|
||||
router.Get("/meter/:pid/pooled", security.EnterpriseAuthorize, listPooledMeters)
|
||||
router.Get("/meter/:pid/:code", security.EnterpriseAuthorize, retrieveSpecificMeterDetail)
|
||||
router.Put("/meter/:pid/:code", security.EnterpriseAuthorize, updateMeterManually)
|
||||
router.Patch("/meter/:pid/:code", security.EnterpriseAuthorize, replaceMeter)
|
||||
router.Get("/meter/:pid/:code/binding", security.EnterpriseAuthorize, listAssociatedMeters)
|
||||
router.Post("/meter/:pid/:code/binding", security.EnterpriseAuthorize, bindAssociatedMeters)
|
||||
router.Delete("/meter/:pid/:code/binding/:slave", security.EnterpriseAuthorize, unbindAssociatedMeters)
|
||||
router.Get("/reading/:pid", security.EnterpriseAuthorize, queryMeterReadings)
|
||||
router.Put("/reading/:pid/:code/:reading", security.EnterpriseAuthorize, updateMeterReading)
|
||||
router.Get("/reading/:pid/template", security.EnterpriseAuthorize, downloadMeterReadingsTemplate)
|
||||
router.Post("/reading/:pid/batch", security.EnterpriseAuthorize, uploadMeterReadings)
|
||||
router.Post("/reading/:pid/:code", security.EnterpriseAuthorize, recordMeterReading)
|
||||
router.Get("/reading/:pid/choice", security.EnterpriseAuthorize, listReadableMeters)
|
||||
}
|
||||
|
||||
// 查询指定园区下的表计信息
|
||||
func searchMetersWithinPark(c *fiber.Ctx) error {
|
||||
parkId := c.Params("pid")
|
||||
meterLog.Info("查询指定园区下的表计信息", zap.String("park id", parkId))
|
||||
result := response.NewResult(c)
|
||||
if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
keyword := c.Query("keyword")
|
||||
page := c.QueryInt("page", 1)
|
||||
mtype := c.QueryInt("type", 3)
|
||||
meters, total, err := repository.MeterRepository.MetersIn(parkId, uint(page), &keyword, mtype)
|
||||
if err != nil {
|
||||
meterLog.Error("无法查询指定园区下的表计信息,无法获取表计列表", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Success(
|
||||
"已经取得符合条件的0.4kV表计列表。",
|
||||
response.NewPagedResponse(page, total).ToMap(),
|
||||
fiber.Map{"meters": meters},
|
||||
)
|
||||
}
|
||||
|
||||
// 查询指定园区中指定表计的详细信息
|
||||
func retrieveSpecificMeterDetail(c *fiber.Ctx) error {
|
||||
parkId := c.Params("pid")
|
||||
meterId := c.Params("code")
|
||||
meterLog.Info("查询指定园区中指定表计的详细信息", zap.String("park id", parkId), zap.String("meter id", meterId))
|
||||
result := response.NewResult(c)
|
||||
if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
meter, err := repository.MeterRepository.FetchMeterDetail(parkId, meterId)
|
||||
if err != nil {
|
||||
meterLog.Error("无法查询指定园区中指定表计的详细信息,无法获取表计信息", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if meter == nil {
|
||||
meterLog.Warn("无法查询指定园区中指定表计的详细信息,表计不存在")
|
||||
return result.NotFound("指定的表计不存在。")
|
||||
}
|
||||
return result.Success("指定表计信息已经找到。", fiber.Map{"meter": meter})
|
||||
}
|
||||
|
||||
// 手动添加一条0.4kV表计记录
|
||||
func createNewMeterManually(c *fiber.Ctx) error {
|
||||
parkId := c.Params("pid")
|
||||
meterLog.Info("手动添加一条0.4kV表计记录", zap.String("park id", parkId))
|
||||
result := response.NewResult(c)
|
||||
if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
var creationForm vo.MeterCreationForm
|
||||
if err := c.BodyParser(&creationForm); err != nil {
|
||||
meterLog.Error("无法手动添加一条0.4kV表计记录,无法解析表计创建表单", zap.Error(err))
|
||||
return result.NotAccept(err.Error())
|
||||
}
|
||||
|
||||
if err := service.MeterService.CreateMeterRecord(parkId, &creationForm); err != nil {
|
||||
meterLog.Error("无法手动添加一条0.4kV表计记录,无法创建表计记录", zap.Error(err))
|
||||
return result.NotAccept(err.Error())
|
||||
}
|
||||
return result.Created("新0.4kV表计已经添加完成。")
|
||||
}
|
||||
|
||||
// 手动更新一条新的0.4kV表计记录
|
||||
func updateMeterManually(c *fiber.Ctx) error {
|
||||
parkId := c.Params("pid")
|
||||
meterId := c.Params("code")
|
||||
meterLog.Info("手动更新一条新的0.4kV表计记录", zap.String("park id", parkId), zap.String("meter id", meterId))
|
||||
result := response.NewResult(c)
|
||||
if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
var updateForm vo.MeterModificationForm
|
||||
if err := c.BodyParser(&updateForm); err != nil {
|
||||
meterLog.Error("无法手动更新一条新的0.4kV表计记录,无法解析表计更新表单", zap.Error(err))
|
||||
return result.NotAccept(err.Error())
|
||||
}
|
||||
if err := service.MeterService.UpdateMeterRecord(parkId, meterId, &updateForm); err != nil {
|
||||
meterLog.Error("无法手动更新一条新的0.4kV表计记录,无法更新表计记录", zap.Error(err))
|
||||
return result.NotAccept(err.Error())
|
||||
}
|
||||
return result.Updated("0.4kV表计已经更新完成。")
|
||||
}
|
||||
|
||||
// 下载指定的园区表计登记模板
|
||||
func downloadMeterArchiveTemplate(c *fiber.Ctx) error {
|
||||
parkId := c.Params("pid")
|
||||
meterLog.Info("下载指定的园区表计登记模板", zap.String("park id", parkId))
|
||||
result := response.NewResult(c)
|
||||
if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
parkDetail, err := repository.ParkRepository.RetrieveParkDetail(parkId)
|
||||
if err != nil {
|
||||
meterLog.Error("无法下载指定的园区表计登记模板,无法获取园区信息", zap.Error(err))
|
||||
return result.NotFound(err.Error())
|
||||
}
|
||||
buildings, err := repository.ParkRepository.RetrieveParkBuildings(parkId)
|
||||
if err != nil {
|
||||
meterLog.Error("无法下载指定的园区表计登记模板,无法获取园区建筑列表", zap.Error(err))
|
||||
return result.NotFound(fmt.Sprintf("无法获取园区建筑列表,%s", err.Error()))
|
||||
}
|
||||
if err != nil {
|
||||
meterLog.Error("无法下载指定的园区表计登记模板,无法生成表计登记模板", zap.Error(err))
|
||||
return result.NotFound(fmt.Sprintf("无法生成表计登记模板,%s", err.Error()))
|
||||
}
|
||||
|
||||
templateGenerator := excel.NewMeterArchiveExcelTemplateGenerator()
|
||||
defer templateGenerator.Close()
|
||||
err = templateGenerator.WriteTemplateData(buildings)
|
||||
if err != nil {
|
||||
meterLog.Error("无法下载指定的园区表计登记模板,无法生成表计登记模板", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, fmt.Sprintf("无法生成表计登记模板,%s", err.Error()))
|
||||
}
|
||||
|
||||
c.Status(fiber.StatusOK)
|
||||
c.Set(fiber.HeaderContentType, fiber.MIMEOctetStream)
|
||||
c.Set("Content-Transfer-Encoding", "binary")
|
||||
c.Set(fiber.HeaderContentDisposition, fmt.Sprintf("attachment; filename=%s-表计登记模板.xlsx", parkDetail.Name))
|
||||
templateGenerator.WriteTo(c.Response().BodyWriter())
|
||||
return nil
|
||||
}
|
||||
|
||||
// 从Excel文件中导入表计档案
|
||||
func uploadMeterArchive(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
uploadFile, err := c.FormFile("data")
|
||||
if err != nil {
|
||||
meterLog.Error("无法从Excel文件中导入表计档案,无法获取上传的文件", zap.Error(err))
|
||||
return result.NotAccept(fmt.Sprintf("没有接收到上传的文件,%s", err.Error()))
|
||||
}
|
||||
errs, err := service.MeterService.BatchImportMeters(parkId, uploadFile)
|
||||
if err != nil {
|
||||
meterLog.Error("无法从Excel文件中导入表计档案,无法导入表计档案", zap.Error(err))
|
||||
return result.Json(fiber.StatusNotAcceptable, "上传的表计档案存在错误。", fiber.Map{"errors": errs})
|
||||
}
|
||||
return result.Success("表计档案已经导入完成。", fiber.Map{"errors": errs})
|
||||
}
|
||||
|
||||
// 更换系统中的表计
|
||||
func replaceMeter(c *fiber.Ctx) error {
|
||||
parkId := c.Params("pid")
|
||||
meterId := c.Params("code")
|
||||
meterLog.Info("更换系统中的表计", zap.String("park id", parkId), zap.String("meter id", meterId))
|
||||
result := response.NewResult(c)
|
||||
if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
var replacementForm vo.MeterReplacingForm
|
||||
if err := c.BodyParser(&replacementForm); err != nil {
|
||||
meterLog.Error("无法更换系统中的表计,无法解析表计更换表单", zap.Error(err))
|
||||
return result.NotAccept(err.Error())
|
||||
}
|
||||
err := service.MeterService.ReplaceMeter(parkId, meterId, &replacementForm.OldReading, replacementForm.NewMeter.Code, replacementForm.NewMeter.Ratio, &replacementForm.NewMeter.Reading)
|
||||
if err != nil {
|
||||
meterLog.Error("更换表计出错1111", zap.Error(err))
|
||||
return result.Error(500, err.Error())
|
||||
}
|
||||
return result.Success("更换成功")
|
||||
}
|
||||
|
||||
// 列出指定公摊表计下的所有关联表计
|
||||
func listAssociatedMeters(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
meterId := c.Params("code")
|
||||
meterLog.Info("列出指定公摊表计下的所有关联表计", zap.String("park id", parkId), zap.String("meter id", meterId))
|
||||
meters, err := service.MeterService.ListPooledMeterRelations(parkId, meterId)
|
||||
if err != nil {
|
||||
meterLog.Error("无法列出指定公摊表计下的所有关联表计,无法获取关联表计列表", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Success("已经取得指定公摊表计下的所有关联表计列表。", fiber.Map{"meters": meters})
|
||||
}
|
||||
|
||||
// 向指定表计绑定关联表计
|
||||
func bindAssociatedMeters(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
meterId := c.Params("code")
|
||||
meterLog.Info("向指定表计绑定关联表计", zap.String("park id", parkId), zap.String("meter id", meterId))
|
||||
var meters = make([]string, 0)
|
||||
if err := c.BodyParser(&meters); err != nil {
|
||||
meterLog.Error("无法向指定表计绑定关联表计,无法解析关联表计列表", zap.Error(err))
|
||||
return result.NotAccept(err.Error())
|
||||
}
|
||||
ok, err := service.MeterService.BindMeter(parkId, meterId, meters)
|
||||
if err != nil {
|
||||
meterLog.Error("无法向指定表计绑定关联表计,无法绑定关联表计", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if !ok {
|
||||
meterLog.Warn("无法向指定表计绑定关联表计,表计关联失败。")
|
||||
return result.NotAccept("表计关联失败。")
|
||||
}
|
||||
return result.Created("已经向指定表计绑定关联表计。")
|
||||
}
|
||||
|
||||
// 解除指定园区下两个表计之间的关联关系
|
||||
func unbindAssociatedMeters(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
masterMeter := c.Params("code")
|
||||
slaveMeter := c.Params("slave")
|
||||
if len(masterMeter) == 0 || len(slaveMeter) == 0 {
|
||||
meterLog.Warn("无法解除指定园区下两个表计之间的关联关系,表计编号为空。")
|
||||
return result.NotAccept("存在未给定要操作的表计编号。")
|
||||
}
|
||||
ok, err := service.MeterService.UnbindMeter(parkId, masterMeter, []string{slaveMeter})
|
||||
if err != nil {
|
||||
meterLog.Error("无法解除指定园区下两个表计之间的关联关系,无法解除关联关系", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if !ok {
|
||||
meterLog.Warn("无法解除指定园区下两个表计之间的关联关系,表计关联解除失败。")
|
||||
return result.NotAccept("表计关联解除失败。")
|
||||
}
|
||||
return result.Created("已经解除指定园区下两个表计之间的关联关系。")
|
||||
}
|
||||
|
||||
// 分页列出园区中的公摊表计
|
||||
func listPooledMeters(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
page := c.QueryInt("page", 1)
|
||||
keyword := c.Query("keyword")
|
||||
meters, total, err := service.MeterService.SearchPooledMetersDetail(parkId, uint(page), &keyword)
|
||||
if err != nil {
|
||||
meterLog.Error("无法分页列出园区中的公摊表计,无法获取公摊表计列表", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Success(
|
||||
"已经取得符合条件的公摊表计列表。",
|
||||
response.NewPagedResponse(page, total).ToMap(),
|
||||
fiber.Map{"meters": meters},
|
||||
)
|
||||
}
|
||||
|
||||
// 列出指定园区中尚未绑定公摊表计的表计
|
||||
func listUnboundMeters(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
meterLog.Error("无法列出指定园区中尚未绑定公摊表计的表计,无法获取当前用户会话", zap.Error(err))
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
parkId := tools.EmptyToNil(c.Query("park"))
|
||||
if pass, err := checkParkBelongs(*parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
keyword := c.Query("keyword")
|
||||
limit := uint(c.QueryInt("limit", 6))
|
||||
meters, err := repository.MeterRepository.ListUnboundMeters(session.Uid, parkId, &keyword, &limit)
|
||||
if err != nil {
|
||||
meterLog.Error("无法列出指定园区中尚未绑定公摊表计的表计,无法获取表计列表", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
var simplifiedMeters = make([]*vo.SimplifiedMeterQueryResponse, 0)
|
||||
copier.Copy(&simplifiedMeters, &meters)
|
||||
return result.Success("已经取得符合条件的表计列表。", fiber.Map{"meters": simplifiedMeters})
|
||||
}
|
||||
|
||||
// 列出指定园区中尚未绑定商户的表计
|
||||
func listUnboundTenementMeters(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
meterLog.Error("无法列出指定园区中尚未绑定商户的表计,无法获取当前用户会话", zap.Error(err))
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
parkId := c.Query("park")
|
||||
if len(parkId) == 0 {
|
||||
meterLog.Error("无法列出指定园区中尚未绑定商户的表计,未指定要访问的园区ID")
|
||||
return result.NotAccept("未指定要访问的园区。")
|
||||
}
|
||||
if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
keyword := c.Query("keyword")
|
||||
limit := uint(c.QueryInt("limit", 6))
|
||||
meters, err := repository.MeterRepository.ListUnboundTenementMeters(session.Uid, &parkId, &keyword, &limit)
|
||||
if err != nil {
|
||||
meterLog.Error("无法列出指定园区中尚未绑定商户的表计,无法获取表计列表", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
var simplifiedMeters = make([]*vo.SimplifiedMeterQueryResponse, 0)
|
||||
copier.Copy(&simplifiedMeters, &meters)
|
||||
return result.Success("已经取得符合条件的表计列表。", fiber.Map{"meters": simplifiedMeters})
|
||||
}
|
||||
|
||||
// 查询指定园区中的表计读数
|
||||
func queryMeterReadings(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
mtype := c.QueryInt("type", 3)
|
||||
if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
keyword := tools.EmptyToNil(c.Query("keyword"))
|
||||
page := c.QueryInt("page", 1)
|
||||
building := tools.EmptyToNil(c.Query("building"))
|
||||
start := c.Query("start_date")
|
||||
var startDate *types.Date = nil
|
||||
if len(start) > 0 {
|
||||
if parsedDate, err := types.ParseDate(start); err != nil {
|
||||
meterLog.Error("查询指定园区中的表计读数,无法解析开始日期", zap.Error(err))
|
||||
} else {
|
||||
startDate = &parsedDate
|
||||
}
|
||||
}
|
||||
end := c.Query("end_date")
|
||||
var endDate *types.Date = nil
|
||||
if len(end) > 0 {
|
||||
if parsedDate, err := types.ParseDate(end); err != nil {
|
||||
meterLog.Error("查询指定园区中的表计读数,无法解析结束日期", zap.Error(err))
|
||||
} else {
|
||||
endDate = &parsedDate
|
||||
}
|
||||
}
|
||||
readings, total, err := service.MeterService.SearchMeterReadings(parkId, building, startDate, endDate, uint(page), keyword, uint(mtype))
|
||||
if err != nil {
|
||||
meterLog.Error("查询指定园区中的表计读数,无法获取表计读数列表", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
convertedReadings := lo.Map(readings, func(element *model.DetailedMeterReading, _ int) vo.MeterReadingDetailResponse {
|
||||
return vo.FromDetailedMeterReading(*element)
|
||||
})
|
||||
return result.Success(
|
||||
"指定园区的表计读数已经列出。",
|
||||
response.NewPagedResponse(page, total).ToMap(),
|
||||
fiber.Map{"records": convertedReadings},
|
||||
)
|
||||
}
|
||||
|
||||
// 记录一条新的表计抄表记录
|
||||
func recordMeterReading(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
meterCode := c.Params("code")
|
||||
var readingForm vo.MeterReadingForm
|
||||
if err := c.BodyParser(&readingForm); err != nil {
|
||||
meterLog.Error("记录一条新的表计抄表记录,无法解析表计抄表表单", zap.Error(err))
|
||||
return result.NotAccept(fmt.Sprintf("无法解析表计抄表表单,%s", err.Error()))
|
||||
}
|
||||
if !readingForm.Validate() {
|
||||
meterLog.Warn("记录一条新的表计抄表记录,表计读数不能正常配平,尖、峰、谷电量和超过总电量。")
|
||||
return result.NotAccept("表计读数不能正常配平,尖、峰、谷电量和超过总电量。")
|
||||
}
|
||||
err := service.MeterService.RecordReading(parkId, meterCode, &readingForm)
|
||||
if err != nil {
|
||||
meterLog.Error("记录一条新的表计抄表记录,无法记录表计抄表记录", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Created("表计抄表记录已经记录完成。")
|
||||
}
|
||||
|
||||
// 更新指定园区中指定表计的抄表记录
|
||||
func updateMeterReading(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
meterCode := c.Params("code")
|
||||
readingAtMicro, err := c.ParamsInt("reading")
|
||||
if err != nil {
|
||||
meterLog.Error("更新一条新的表计抄表记录,无法解析抄表时间", zap.Error(err))
|
||||
return result.NotAccept(fmt.Sprintf("无法解析抄表时间,%s", err.Error()))
|
||||
}
|
||||
readingAt := types.FromUnixMicro(int64(readingAtMicro))
|
||||
var readingForm vo.MeterReadingForm
|
||||
if err := c.BodyParser(&readingForm); err != nil {
|
||||
meterLog.Error("更新一条新的表计抄表记录,无法解析表计抄表表单", zap.Error(err))
|
||||
return result.NotAccept(fmt.Sprintf("无法解析表计抄表表单,%s", err.Error()))
|
||||
}
|
||||
ok, err := repository.MeterRepository.UpdateMeterReading(parkId, meterCode, readingAt, &readingForm)
|
||||
if err != nil {
|
||||
meterLog.Error("更新一条新的表计抄表记录,无法更新表计抄表记录", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if !ok {
|
||||
meterLog.Warn("更新一条新的表计抄表记录,表计抄表更新失败。")
|
||||
return result.NotAccept("表计抄表记录未能成功更新,可能指定抄表记录不存在。")
|
||||
}
|
||||
return result.Success("表计抄表记录已经更新完成。")
|
||||
}
|
||||
|
||||
// 下载指定园区的表计抄表模板
|
||||
func downloadMeterReadingsTemplate(c *fiber.Ctx) error {
|
||||
parkId := c.Params("pid")
|
||||
meterLog.Info("下载指定的园区表计抄表模板", zap.String("park id", parkId))
|
||||
result := response.NewResult(c)
|
||||
if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
parkDetail, err := repository.ParkRepository.RetrieveParkDetail(parkId)
|
||||
if err != nil {
|
||||
meterLog.Error("无法下载指定的园区表计登记模板,无法获取园区信息", zap.Error(err))
|
||||
return result.NotFound(err.Error())
|
||||
}
|
||||
meterDocs, err := repository.MeterRepository.ListMeterDocForTemplate(parkId)
|
||||
if err != nil {
|
||||
meterLog.Error("无法下载指定的园区表计抄表模板,无法获取表计档案列表", zap.Error(err))
|
||||
return result.NotFound(fmt.Sprintf("无法获取表计档案列表,%s", err.Error()))
|
||||
}
|
||||
if err != nil {
|
||||
meterLog.Error("无法下载指定的园区表计登记模板,无法生成表计登记模板", zap.Error(err))
|
||||
return result.NotFound(fmt.Sprintf("无法生成表计登记模板,%s", err.Error()))
|
||||
}
|
||||
templateGenerator := excel.NewMeterReadingsExcelTemplateGenerator()
|
||||
defer templateGenerator.Close()
|
||||
err = templateGenerator.WriteTemplateData(meterDocs)
|
||||
if err != nil {
|
||||
meterLog.Error("无法下载指定的园区表计抄表模板,无法生成表计抄表模板", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, fmt.Sprintf("无法生成表计抄表模板,%s", err.Error()))
|
||||
}
|
||||
c.Status(fiber.StatusOK)
|
||||
c.Set(fiber.HeaderContentType, fiber.MIMEOctetStream)
|
||||
c.Set("Content-Transfer-Encoding", "binary")
|
||||
c.Set(fiber.HeaderContentDisposition, fmt.Sprintf("attachment; filename=%s-表计抄表模板.xlsx", parkDetail.Name))
|
||||
templateGenerator.WriteTo(c.Response().BodyWriter())
|
||||
return nil
|
||||
}
|
||||
|
||||
// 处理上传的抄表记录文件
|
||||
func uploadMeterReadings(c *fiber.Ctx) error {
|
||||
parkId := c.Params("pid")
|
||||
meterLog.Info("从Excel文件中导入抄表档案", zap.String("park id", parkId))
|
||||
result := response.NewResult(c)
|
||||
if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
uploadFile, err := c.FormFile("data")
|
||||
if err != nil {
|
||||
meterLog.Error("无法从Excel文件中导入抄表档案,无法获取上传的文件", zap.Error(err))
|
||||
return result.NotAccept(fmt.Sprintf("没有接收到上传的文件,%s", err.Error()))
|
||||
}
|
||||
errs, err := service.MeterService.BatchImportReadings(parkId, uploadFile)
|
||||
if err != nil {
|
||||
meterLog.Error("无法从Excel文件中导入抄表档案,无法导入抄表档案", zap.Error(err))
|
||||
return result.Json(fiber.StatusNotAcceptable, "上传的抄表档案存在错误。", fiber.Map{"errors": errs})
|
||||
}
|
||||
return result.Success("表计档案已经导入完成。", fiber.Map{"errors": errs})
|
||||
}
|
||||
|
||||
// 列出可以作为抄表目标的表计,主要用于创建抄表记录时的下拉选择。
|
||||
func listReadableMeters(c *fiber.Ctx) error {
|
||||
parkId := c.Params("pid")
|
||||
meterLog.Info("列出可以作为抄表目标的表计", zap.String("parkId", parkId))
|
||||
result := response.NewResult(c)
|
||||
if len(parkId) == 0 {
|
||||
meterLog.Error("无法列出指定园区中尚未绑定商户的表计,未指定要访问的园区ID")
|
||||
return result.NotAccept("未指定要访问的园区。")
|
||||
}
|
||||
if pass, err := checkParkBelongs(parkId, meterLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
limit := c.QueryInt("limit", 10)
|
||||
keyword := tools.EmptyToNil(c.Query("keyword"))
|
||||
meters, err := repository.MeterRepository.ListReadableMeters(parkId, keyword, uint(limit))
|
||||
if err != nil {
|
||||
meterLog.Error(
|
||||
"无法列出可以作为抄表目标的表计,无法获取表计列表", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
var simplifiedMeters = make([]*vo.ReadableMeterQueryResponse, 0)
|
||||
copier.Copy(&simplifiedMeters, &meters)
|
||||
return result.Success(
|
||||
"指定园区中的可作为抄表目标的表计已经列出。",
|
||||
fiber.Map{"meters": simplifiedMeters},
|
||||
)
|
||||
}
|
||||
@@ -1,188 +0,0 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/excel"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"electricity_bill_calc/service"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/samber/lo"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
func InitializeMeter04kVController(router *fiber.App) {
|
||||
router.Get("/park/:pid/meter/template", download04kvMeterArchiveTemplate)
|
||||
router.Get("/park/:pid/meters", security.EnterpriseAuthorize, ListPaged04kVMeter)
|
||||
router.Get("/park/:pid/meter/:code", security.EnterpriseAuthorize, fetch04kVMeterDetail)
|
||||
router.Post("/park/:pid/meter", security.EnterpriseAuthorize, createSingle04kVMeter)
|
||||
router.Post("/park/:pid/meter/batch", security.EnterpriseAuthorize, batchImport04kVMeterArchive)
|
||||
router.Put("/park/:pid/meter/:code", security.EnterpriseAuthorize, modifySingle04kVMeter)
|
||||
}
|
||||
|
||||
func download04kvMeterArchiveTemplate(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestParkId := c.Params("pid")
|
||||
parkDetail, err := service.ParkService.FetchParkDetail(requestParkId)
|
||||
if err != nil {
|
||||
return result.NotFound("未找到指定的园区信息。")
|
||||
}
|
||||
return c.Download("./assets/meter_04kv_template.xlsx", fmt.Sprintf("%s-户表档案.xlsx", parkDetail.Name))
|
||||
}
|
||||
|
||||
func ListPaged04kVMeter(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestParkId := c.Params("pid")
|
||||
if ensure, err := ensureParkBelongs(c, &result, requestParkId); !ensure {
|
||||
return err
|
||||
}
|
||||
requestPage, err := strconv.Atoi(c.Query("page", "1"))
|
||||
if err != nil {
|
||||
return result.NotAccept("查询参数[page]格式不正确。")
|
||||
}
|
||||
requestKeyword := c.Query("keyword", "")
|
||||
meters, totalItem, err := service.Meter04kVService.ListMeterDetail(requestParkId, requestKeyword, requestPage)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
}
|
||||
return result.Json(
|
||||
http.StatusOK,
|
||||
"已获取到符合条件的0.4kV表计集合。",
|
||||
response.NewPagedResponse(requestPage, totalItem).ToMap(),
|
||||
fiber.Map{"meters": meters},
|
||||
)
|
||||
}
|
||||
|
||||
func fetch04kVMeterDetail(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestParkId := c.Params("pid")
|
||||
if ensure, err := ensureParkBelongs(c, &result, requestParkId); !ensure {
|
||||
return err
|
||||
}
|
||||
requestMeterCode := c.Params("code")
|
||||
meter, err := service.Meter04kVService.Get04kVMeterDetail(requestParkId, requestMeterCode)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
}
|
||||
if meter == nil {
|
||||
return result.Json(http.StatusNotFound, "指定的表计信息未能找到。", fiber.Map{"meter": nil})
|
||||
}
|
||||
return result.Json(http.StatusOK, "指定的表计信息已找到。", fiber.Map{"meter": meter})
|
||||
}
|
||||
|
||||
type _MeterModificationFormData struct {
|
||||
Address *string `json:"address" form:"address"`
|
||||
CustomerName *string `json:"customerName" form:"customerName"`
|
||||
ContactName *string `json:"contactName" form:"contactName"`
|
||||
ContactPhone *string `json:"contactPhone" form:"contactPhone"`
|
||||
Ratio decimal.Decimal `json:"ratio" form:"ratio"`
|
||||
Seq int `json:"seq" form:"seq"`
|
||||
IsPublicMeter bool `json:"isPublicMeter" form:"isPublicMeter"`
|
||||
Enabled bool `json:"enabled" form:"enabled"`
|
||||
}
|
||||
|
||||
type _MeterCreationFormData struct {
|
||||
Code string `json:"code" form:"code"`
|
||||
Address *string `json:"address" form:"address"`
|
||||
CustomerName *string `json:"customerName" form:"customerName"`
|
||||
ContactName *string `json:"contactName" form:"contactName"`
|
||||
ContactPhone *string `json:"contactPhone" form:"contactPhone"`
|
||||
Ratio decimal.Decimal `json:"ratio" form:"ratio"`
|
||||
Seq int `json:"seq" form:"seq"`
|
||||
IsPublicMeter bool `json:"isPublicMeter" form:"isPublicMeter"`
|
||||
Enabled bool `json:"enabled" form:"enabled"`
|
||||
}
|
||||
|
||||
func createSingle04kVMeter(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestParkId := c.Params("pid")
|
||||
if ensure, err := ensureParkBelongs(c, &result, requestParkId); !ensure {
|
||||
return err
|
||||
}
|
||||
formData := new(_MeterCreationFormData)
|
||||
if err := c.BodyParser(formData); err != nil {
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
newMeter := new(model.Meter04KV)
|
||||
copier.Copy(newMeter, formData)
|
||||
newMeter.ParkId = requestParkId
|
||||
err := service.Meter04kVService.CreateSingleMeter(*newMeter)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Created("新0.4kV表计已经添加完成。")
|
||||
}
|
||||
|
||||
func modifySingle04kVMeter(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestParkId := c.Params("pid")
|
||||
if ensure, err := ensureParkBelongs(c, &result, requestParkId); !ensure {
|
||||
return err
|
||||
}
|
||||
requestMeterCode := c.Params("code")
|
||||
meterDetail, err := service.Meter04kVService.Get04kVMeterDetail(requestParkId, requestMeterCode)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
}
|
||||
if meterDetail == nil {
|
||||
return result.NotFound("指定表计的信息为找到,不能修改。")
|
||||
}
|
||||
formData := new(_MeterModificationFormData)
|
||||
if err := c.BodyParser(formData); err != nil {
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
copier.Copy(meterDetail, formData)
|
||||
err = service.Meter04kVService.UpdateSingleMeter(meterDetail)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Updated("指定0.4kV表计信息已经更新。")
|
||||
}
|
||||
|
||||
func batchImport04kVMeterArchive(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestParkId := c.Params("pid")
|
||||
if ensure, err := ensureParkBelongs(c, &result, requestParkId); !ensure {
|
||||
return err
|
||||
}
|
||||
uploadedFile, err := c.FormFile("data")
|
||||
if err != nil {
|
||||
return result.NotAccept("没有接收到上传的档案文件。")
|
||||
}
|
||||
archiveFile, err := uploadedFile.Open()
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
analyzer, err := excel.NewMeterArchiveExcelAnalyzer(archiveFile)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
records, errs := analyzer.Analysis(*new(model.Meter04KV))
|
||||
if len(errs) > 0 {
|
||||
return result.Json(http.StatusNotAcceptable, "上传的表计档案文件存在错误。", fiber.Map{"errors": errs})
|
||||
}
|
||||
|
||||
mergedMeters := lo.Map(records, func(meter model.Meter04KV, index int) model.Meter04KV {
|
||||
meter.ParkId = requestParkId
|
||||
meter.Enabled = true
|
||||
return meter
|
||||
})
|
||||
errs = service.Meter04kVService.DuplicateMeterCodeValidate(mergedMeters)
|
||||
if len(errs) > 0 {
|
||||
return result.Json(http.StatusNotAcceptable, "上传的表计档案文件存在错误。", fiber.Map{"errors": errs})
|
||||
}
|
||||
err = service.Meter04kVService.BatchCreateMeter(mergedMeters)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Json(
|
||||
http.StatusOK,
|
||||
"上传的表计档案已经全部导入。",
|
||||
fiber.Map{"errors": make([]excel.ExcelAnalysisError, 0)},
|
||||
)
|
||||
}
|
||||
@@ -1,185 +1,331 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/repository"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"electricity_bill_calc/service"
|
||||
"electricity_bill_calc/tools"
|
||||
"electricity_bill_calc/vo"
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/shopspring/decimal"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func InitializeParkController(router *fiber.App) {
|
||||
router.Get("/parks", security.EnterpriseAuthorize, listAllParksUnderSessionUser)
|
||||
router.Get("/parks/:uid", security.MustAuthenticated, listAllParksUnderSpecificUser)
|
||||
router.Post("/park", security.EnterpriseAuthorize, createNewPark)
|
||||
router.Put("/park/:pid", security.EnterpriseAuthorize, modifyPark)
|
||||
var parkLog = logger.Named("Handler", "Park")
|
||||
|
||||
func InitializeParkHandlers(router *fiber.App) {
|
||||
router.Get("/park", security.EnterpriseAuthorize, listParksBelongsToCurrentUser)
|
||||
router.Post("/park", security.EnterpriseAuthorize, createPark)
|
||||
router.Get("/park/belongs/:uid", security.OPSAuthorize, listParksBelongsTo)
|
||||
router.Get("/park/:pid", security.EnterpriseAuthorize, fetchParkDetail)
|
||||
router.Put("/park/:pid/enabled", security.EnterpriseAuthorize, changeParkEnableState)
|
||||
router.Put("/park/:pid", security.EnterpriseAuthorize, modifySpecificPark)
|
||||
router.Delete("/park/:pid", security.EnterpriseAuthorize, deleteSpecificPark)
|
||||
router.Put("/park/:pid/enabled", security.EnterpriseAuthorize, modifyParkEnabling)
|
||||
router.Get("/park/:pid/building", security.EnterpriseAuthorize, listBuildingsBelongsToPark)
|
||||
router.Post("/park/:pid/building", security.EnterpriseAuthorize, createBuildingInPark)
|
||||
router.Put("/park/:pid/building/:bid", security.EnterpriseAuthorize, modifySpecificBuildingInPark)
|
||||
router.Delete("/park/:pid/building/:bid", security.EnterpriseAuthorize, deletedParkBuilding)
|
||||
router.Put("/park/:pid/building/:bid/enabled", security.EnterpriseAuthorize, modifyParkBuildingEnabling)
|
||||
}
|
||||
|
||||
func ensureParkBelongs(c *fiber.Ctx, result *response.Result, requestParkId string) (bool, error) {
|
||||
userSession, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
return false, result.Unauthorized(err.Error())
|
||||
}
|
||||
sure, err := service.ParkService.EnsurePark(userSession.Uid, requestParkId)
|
||||
if err != nil {
|
||||
return false, result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if !sure {
|
||||
return false, result.Unauthorized("不能访问不属于自己的园区。")
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func listAllParksUnderSessionUser(c *fiber.Ctx) error {
|
||||
// 列出隶属于当前用户的全部园区
|
||||
func listParksBelongsToCurrentUser(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
userSession, err := _retreiveSession(c)
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
parkLog.Error("列出当前用的全部园区,无法获取当前用户的会话。")
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
keyword := c.Query("keyword")
|
||||
parks, err := service.ParkService.ListAllParkBelongsTo(userSession.Uid, keyword)
|
||||
parkLog.Info("列出当前用户下的全部园区", zap.String("user id", session.Uid))
|
||||
parks, err := repository.ParkRepository.ListAllParks(session.Uid)
|
||||
if err != nil {
|
||||
parkLog.Error("无法获取园区列表。", zap.String("user id", session.Uid))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Json(http.StatusOK, "已获取到指定用户下的园区。", fiber.Map{"parks": parks})
|
||||
return result.Success("已获取到指定用户的下的园区", fiber.Map{"parks": parks})
|
||||
}
|
||||
|
||||
func listAllParksUnderSpecificUser(c *fiber.Ctx) error {
|
||||
// 列出隶属于指定用户的全部园区
|
||||
func listParksBelongsTo(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestUserId := c.Params("uid")
|
||||
keyword := c.Query("keyword")
|
||||
parks, err := service.ParkService.ListAllParkBelongsTo(requestUserId, keyword)
|
||||
userId := c.Params("uid")
|
||||
parkLog.Info("列出指定用户下的全部园区", zap.String("user id", userId))
|
||||
parks, err := repository.ParkRepository.ListAllParks(userId)
|
||||
if err != nil {
|
||||
parkLog.Error("无法获取园区列表。", zap.String("user id", userId))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Json(http.StatusOK, "已获取到指定用户下的园区。", fiber.Map{"parks": parks})
|
||||
}
|
||||
|
||||
type _ParkInfoFormData struct {
|
||||
Name string `json:"name" form:"name"`
|
||||
Region *string `json:"region" form:"region"`
|
||||
Address *string `json:"address" form:"address"`
|
||||
Contact *string `json:"contact" form:"contact"`
|
||||
Phone *string `json:"phone" from:"phone"`
|
||||
Area decimal.NullDecimal `json:"area" from:"area"`
|
||||
Capacity decimal.NullDecimal `json:"capacity" from:"capacity"`
|
||||
TenementQuantity decimal.NullDecimal `json:"tenement" from:"tenement"`
|
||||
Category int8 `json:"category" form:"category"`
|
||||
SubmeterType int8 `json:"submeter" form:"submeter"`
|
||||
}
|
||||
|
||||
func createNewPark(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
userSession, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
formData := new(_ParkInfoFormData)
|
||||
if err := c.BodyParser(formData); err != nil {
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
newPark := new(model.Park)
|
||||
copier.Copy(newPark, formData)
|
||||
newPark.Id = uuid.New().String()
|
||||
newPark.UserId = userSession.Uid
|
||||
nameAbbr := tools.PinyinAbbr(newPark.Name)
|
||||
newPark.Abbr = &nameAbbr
|
||||
newPark.Enabled = true
|
||||
err = service.ParkService.SaveNewPark(*newPark)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Created("新园区完成创建。")
|
||||
}
|
||||
|
||||
func modifyPark(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
userSession, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
requestParkId := c.Params("pid")
|
||||
formData := new(_ParkInfoFormData)
|
||||
if err := c.BodyParser(formData); err != nil {
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
park, err := service.ParkService.FetchParkDetail(requestParkId)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if userSession.Uid != park.UserId {
|
||||
return result.Unauthorized("不能修改不属于自己的园区。")
|
||||
}
|
||||
copier.Copy(park, formData)
|
||||
nameAbbr := tools.PinyinAbbr(formData.Name)
|
||||
park.Abbr = &nameAbbr
|
||||
err = service.ParkService.UpdateParkInfo(park)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Updated("指定园区资料已更新。")
|
||||
return result.Success("已获取到指定用户的下的园区", fiber.Map{"parks": parks})
|
||||
}
|
||||
|
||||
// 获取指定园区的详细信息
|
||||
func fetchParkDetail(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestParkId := c.Params("pid")
|
||||
if ensure, err := ensureParkBelongs(c, &result, requestParkId); !ensure {
|
||||
return err
|
||||
}
|
||||
park, err := service.ParkService.FetchParkDetail(requestParkId)
|
||||
parkId := c.Params("pid")
|
||||
parkLog.Info("获取指定园区的详细信息", zap.String("park id", parkId))
|
||||
park, err := repository.ParkRepository.RetrieveParkDetail(parkId)
|
||||
if err != nil {
|
||||
parkLog.Error("无法获取园区信息。", zap.String("park id", parkId))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Json(http.StatusOK, "已经获取到指定园区的信息。", fiber.Map{"park": park})
|
||||
return result.Success("已获取到指定园区的详细信息", fiber.Map{"park": park})
|
||||
}
|
||||
|
||||
type _ParkStateFormData struct {
|
||||
Enabled bool `json:"enabled" form:"enabled"`
|
||||
}
|
||||
|
||||
func changeParkEnableState(c *fiber.Ctx) error {
|
||||
// 创建一个新的园区
|
||||
func createPark(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
userSession, err := _retreiveSession(c)
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
parkLog.Error("创建一个新的园区,无法获取当前用户的会话。")
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
requestParkId := c.Params("pid")
|
||||
if ensure, err := ensureParkBelongs(c, &result, requestParkId); !ensure {
|
||||
return err
|
||||
parkLog.Info("创建一个新的园区", zap.String("user id", session.Uid))
|
||||
creationForm := new(vo.ParkInformationForm)
|
||||
if err := c.BodyParser(creationForm); err != nil {
|
||||
parkLog.Error("无法解析园区表单数据。", zap.String("user id", session.Uid), zap.Error(err))
|
||||
return result.NotAccept(err.Error())
|
||||
}
|
||||
formData := new(_ParkStateFormData)
|
||||
if err := c.BodyParser(formData); err != nil {
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
err = service.ParkService.ChangeParkState(userSession.Uid, requestParkId, formData.Enabled)
|
||||
park, err := creationForm.TryIntoPark()
|
||||
if err != nil {
|
||||
parkLog.Error("无法将园区表单数据转换为园区对象。", zap.String("user id", session.Uid), zap.Error(err))
|
||||
return result.NotAccept(err.Error())
|
||||
}
|
||||
ok, err := repository.ParkRepository.CreatePark(session.Uid, park)
|
||||
switch {
|
||||
case err == nil && !ok:
|
||||
parkLog.Error("无法创建新的园区。", zap.String("user id", session.Uid))
|
||||
return result.NotAccept("无法创建新的园区。")
|
||||
case err != nil:
|
||||
parkLog.Error("无法创建新的园区。", zap.String("user id", session.Uid), zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Updated("指定园区的可用性状态已成功更新。")
|
||||
return result.Created("已创建一个新的园区")
|
||||
}
|
||||
|
||||
// 修改指定园区的信息
|
||||
func modifySpecificPark(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
parkLog.Error("修改指定园区的信息,无法获取当前用户的会话。")
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
if pass, err := checkParkBelongs(parkId, parkLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
parkForm := new(vo.ParkInformationForm)
|
||||
if err := c.BodyParser(parkForm); err != nil {
|
||||
parkLog.Error("无法解析园区表单数据。", zap.String("park id", parkId), zap.String("user id", session.Uid), zap.Error(err))
|
||||
return result.NotAccept(err.Error())
|
||||
}
|
||||
park, err := parkForm.TryIntoPark()
|
||||
if err != nil {
|
||||
parkLog.Error("无法将园区表单数据转换为园区对象。", zap.String("park id", parkId), zap.String("user id", session.Uid), zap.Error(err))
|
||||
return result.NotAccept(err.Error())
|
||||
}
|
||||
ok, err := repository.ParkRepository.UpdatePark(parkId, park)
|
||||
switch {
|
||||
case err == nil && !ok:
|
||||
parkLog.Error("无法更新园区信息。", zap.String("park id", parkId), zap.String("user id", session.Uid))
|
||||
return result.NotAccept("无法更新园区信息。")
|
||||
case err != nil:
|
||||
parkLog.Error("无法更新园区信息。", zap.String("park id", parkId), zap.String("user id", session.Uid), zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Updated("已更新指定园区的详细信息")
|
||||
}
|
||||
|
||||
// 修改指定园区的可用性
|
||||
func modifyParkEnabling(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
parkLog.Error("修改指定园区的可用性,无法获取当前用户的会话。")
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
if pass, err := checkParkBelongs(parkId, parkLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
stateForm := new(vo.StateForm)
|
||||
if err := c.BodyParser(stateForm); err != nil {
|
||||
parkLog.Error("无法解析园区表单数据。", zap.String("park id", parkId), zap.String("user id", session.Uid), zap.Error(err))
|
||||
return result.NotAccept(err.Error())
|
||||
}
|
||||
ok, err := repository.ParkRepository.EnablingPark(parkId, stateForm.Enabled)
|
||||
switch {
|
||||
case err == nil && !ok:
|
||||
parkLog.Error("无法更新园区可用性。", zap.String("park id", parkId), zap.String("user id", session.Uid))
|
||||
return result.NotAccept("无法更新园区可用性。")
|
||||
case err != nil:
|
||||
parkLog.Error("无法更新园区可用性。", zap.String("park id", parkId), zap.String("user id", session.Uid), zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Updated("已更新指定园区的可用性。")
|
||||
}
|
||||
|
||||
// 删除指定的园区
|
||||
func deleteSpecificPark(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
userSession, err := _retreiveSession(c)
|
||||
parkId := c.Params("pid")
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
parkLog.Error("删除指定的园区,无法获取当前用户的会话。")
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
requestParkId := c.Params("pid")
|
||||
if ensure, err := ensureParkBelongs(c, &result, requestParkId); !ensure {
|
||||
if pass, err := checkParkBelongs(parkId, parkLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
err = service.ParkService.DeletePark(userSession.Uid, requestParkId)
|
||||
if err != nil {
|
||||
ok, err := repository.ParkRepository.DeletePark(parkId)
|
||||
switch {
|
||||
case err == nil && !ok:
|
||||
parkLog.Error("无法删除园区。", zap.String("park id", parkId), zap.String("user id", session.Uid))
|
||||
return result.NotAccept("无法删除园区。")
|
||||
case err != nil:
|
||||
parkLog.Error("无法删除园区。", zap.String("park id", parkId), zap.String("user id", session.Uid), zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Deleted("指定园区已成功删除。")
|
||||
return result.Success("已删除指定的园区")
|
||||
}
|
||||
|
||||
// 列出指定园区中已经登记的建筑
|
||||
func listBuildingsBelongsToPark(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
parkLog.Error("列出指定园区中已经登记的建筑,无法获取当前用户的会话。")
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
ok, err := repository.ParkRepository.IsParkBelongs(parkId, session.Uid)
|
||||
switch {
|
||||
case err != nil:
|
||||
parkLog.Error("无法判断园区是否隶属于当前用户。", zap.String("park id", parkId), zap.String("user id", session.Uid), zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
case err == nil && !ok:
|
||||
parkLog.Error("用户试图访问不属于自己的园区。", zap.String("park id", parkId), zap.String("user id", session.Uid))
|
||||
return result.Forbidden("您无权访问该园区。")
|
||||
}
|
||||
buildings, err := repository.ParkRepository.RetrieveParkBuildings(parkId)
|
||||
if err != nil {
|
||||
parkLog.Error("无法获取园区中的建筑列表。", zap.String("park id", parkId), zap.String("user id", session.Uid), zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Success("已获取到指定园区中的建筑列表", fiber.Map{"buildings": buildings})
|
||||
}
|
||||
|
||||
// 在指定园区中创建一个新的建筑
|
||||
func createBuildingInPark(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
parkLog.Error("在指定园区中创建一个新的建筑,无法获取当前用户的会话。")
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
if pass, err := checkParkBelongs(parkId, parkLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
buildingForm := new(vo.ParkBuildingInformationForm)
|
||||
if err := c.BodyParser(buildingForm); err != nil {
|
||||
parkLog.Error("无法解析建筑表单数据。", zap.String("park id", parkId), zap.String("user id", session.Uid), zap.Error(err))
|
||||
return result.NotAccept(err.Error())
|
||||
}
|
||||
ok, err := repository.ParkRepository.CreateParkBuilding(parkId, buildingForm.Name, &buildingForm.Floors)
|
||||
switch {
|
||||
case err == nil && !ok:
|
||||
parkLog.Error("无法创建新的建筑。", zap.String("park id", parkId), zap.String("user id", session.Uid))
|
||||
return result.NotAccept("无法创建新的建筑。")
|
||||
case err != nil:
|
||||
parkLog.Error("无法创建新的建筑。", zap.String("park id", parkId), zap.String("user id", session.Uid), zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Created("已创建一个新的建筑")
|
||||
}
|
||||
|
||||
// 修改指定园区中的指定建筑的信息
|
||||
func modifySpecificBuildingInPark(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
buildingId := c.Params("bid")
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
parkLog.Error("修改指定园区中的指定建筑的信息,无法获取当前用户的会话。")
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
if pass, err := checkParkBelongs(parkId, parkLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
buildingForm := new(vo.ParkBuildingInformationForm)
|
||||
if err := c.BodyParser(buildingForm); err != nil {
|
||||
parkLog.Error("无法解析建筑表单数据。", zap.String("park id", parkId), zap.String("user id", session.Uid), zap.Error(err))
|
||||
return result.NotAccept(err.Error())
|
||||
}
|
||||
ok, err := repository.ParkRepository.ModifyParkBuilding(buildingId, parkId, buildingForm.Name, &buildingForm.Floors)
|
||||
switch {
|
||||
case err == nil && !ok:
|
||||
parkLog.Error("无法更新建筑信息。", zap.String("park id", parkId), zap.String("user id", session.Uid))
|
||||
return result.NotAccept("无法更新建筑信息。")
|
||||
case err != nil:
|
||||
parkLog.Error("无法更新建筑信息。", zap.String("park id", parkId), zap.String("user id", session.Uid), zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Updated("已更新指定建筑的信息")
|
||||
}
|
||||
|
||||
// 修改指定园区中指定建筑的可用性
|
||||
func modifyParkBuildingEnabling(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
buildingId := c.Params("bid")
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
parkLog.Error("修改指定园区中指定建筑的可用性,无法获取当前用户的会话。")
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
if pass, err := checkParkBelongs(parkId, parkLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
stateForm := new(vo.StateForm)
|
||||
if err := c.BodyParser(stateForm); err != nil {
|
||||
parkLog.Error("无法解析建筑表单数据。", zap.String("park id", parkId), zap.String("user id", session.Uid), zap.Error(err))
|
||||
return result.NotAccept(err.Error())
|
||||
}
|
||||
ok, err := repository.ParkRepository.EnablingParkBuilding(buildingId, parkId, stateForm.Enabled)
|
||||
switch {
|
||||
case err == nil && !ok:
|
||||
parkLog.Error("无法更新建筑可用性。", zap.String("park id", parkId), zap.String("user id", session.Uid))
|
||||
return result.NotAccept("无法更新建筑可用性。")
|
||||
case err != nil:
|
||||
parkLog.Error("无法更新建筑可用性。", zap.String("park id", parkId), zap.String("user id", session.Uid), zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Updated("已更新指定建筑的可用性")
|
||||
}
|
||||
|
||||
// 删除指定园区中的指定建筑
|
||||
func deletedParkBuilding(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
buildingId := c.Params("bid")
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
parkLog.Error("删除指定园区中的指定建筑,无法获取当前用户的会话。")
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
if pass, err := checkParkBelongs(parkId, parkLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
ok, err := repository.ParkRepository.DeleteParkBuilding(buildingId, parkId)
|
||||
switch {
|
||||
case err == nil && !ok:
|
||||
parkLog.Error("无法删除建筑。", zap.String("park id", parkId), zap.String("user id", session.Uid))
|
||||
return result.NotAccept("无法删除建筑。")
|
||||
case err != nil:
|
||||
parkLog.Error("无法删除建筑。", zap.String("park id", parkId), zap.String("user id", session.Uid), zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Deleted("已删除指定的建筑")
|
||||
}
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/repository"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/service"
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func InitializeRegionController(router *fiber.App) {
|
||||
router.Get("/region/:rid", fetchRegions)
|
||||
router.Get("/regions/:rid", fetchAllLeveledRegions)
|
||||
func InitializeRegionHandlers(router *fiber.App) {
|
||||
router.Get("/region/:rid", getSubRegions)
|
||||
router.Get("/regions/:rid", getParentRegions)
|
||||
}
|
||||
|
||||
func fetchRegions(c *fiber.Ctx) error {
|
||||
func getSubRegions(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestParentId := c.Params("rid")
|
||||
regions, err := service.RegionService.FetchSubRegions(requestParentId)
|
||||
regions, err := repository.RegionRepository.FindSubRegions(requestParentId)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
@@ -26,10 +26,10 @@ func fetchRegions(c *fiber.Ctx) error {
|
||||
return result.Json(http.StatusOK, "已经获取到相关的行政区划。", fiber.Map{"regions": regions})
|
||||
}
|
||||
|
||||
func fetchAllLeveledRegions(c *fiber.Ctx) error {
|
||||
func getParentRegions(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestRegionCode := c.Params("rid")
|
||||
regions, err := service.RegionService.FetchAllParentRegions(requestRegionCode)
|
||||
regions, err := repository.RegionRepository.FindParentRegions(requestRegionCode)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
@@ -1,301 +1,484 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/exceptions"
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/repository"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"electricity_bill_calc/service"
|
||||
"electricity_bill_calc/tools"
|
||||
"net/http"
|
||||
"electricity_bill_calc/types"
|
||||
"electricity_bill_calc/vo"
|
||||
|
||||
"log"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/samber/lo"
|
||||
"github.com/shopspring/decimal"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func InitializeReportController(router *fiber.App) {
|
||||
router.Get("/reports/with/drafts", security.EnterpriseAuthorize, fetchNewestReportOfParkWithDraft)
|
||||
router.Post("/park/:pid/report", security.EnterpriseAuthorize, initializeNewReport)
|
||||
router.Get("/report/:rid/step/state", security.EnterpriseAuthorize, fetchReportStepStates)
|
||||
router.Get("/report/:rid/summary", security.EnterpriseAuthorize, fetchReportParkSummary)
|
||||
router.Put("/report/:rid/summary", security.EnterpriseAuthorize, fillReportSummary)
|
||||
router.Get("/report/:rid/summary/calculate", security.EnterpriseAuthorize, testCalculateReportSummary)
|
||||
router.Post("/report/:rid/summary/calculate", security.EnterpriseAuthorize, progressReportSummary)
|
||||
router.Put("/report/:rid/step/meter/register", security.EnterpriseAuthorize, progressEndUserRegister)
|
||||
var reportLog = logger.Named("Handler", "Report")
|
||||
|
||||
func InitializeReportHandlers(router *fiber.App) {
|
||||
router.Get("/reports", security.MustAuthenticated, reportComprehensiveSearch)
|
||||
router.Post("/report", security.EnterpriseAuthorize, initNewReportCalculateTask)
|
||||
router.Get("/report/draft", security.EnterpriseAuthorize, listDraftReportIndicies)
|
||||
router.Post("/report/calcualte", security.EnterpriseAuthorize, testCalculateReportSummary)
|
||||
//TODO: 2023-07-20将calcualte错误请求改为正确的calculate请求
|
||||
router.Post("/report/calculate", security.EnterpriseAuthorize, testCalculateReportSummary)
|
||||
router.Get("/report/calculate/status", security.EnterpriseAuthorize, listCalculateTaskStatus)
|
||||
router.Get("/report/:rid", security.EnterpriseAuthorize, getReportDetail)
|
||||
router.Put("/report/:rid", security.EnterpriseAuthorize, updateReportCalculateTask)
|
||||
router.Post("/report/:rid/publish", security.EnterpriseAuthorize, publishReport)
|
||||
router.Get("/reports", security.MustAuthenticated, searchReports)
|
||||
router.Get("/report/:rid", security.MustAuthenticated, fetchReportPublicity)
|
||||
router.Post("/report/:rid/calculate", security.EnterpriseAuthorize, calculateReport)
|
||||
router.Put("/report/:rid/calculate", security.EnterpriseAuthorize, initiateCalculateTask)
|
||||
router.Get("/report/:rid/publics", security.MustAuthenticated, listPublicMetersInReport)
|
||||
router.Get("/report/:rid/summary", security.MustAuthenticated, getReportSummary)
|
||||
router.Get("/report/:rid/summary/filled", security.EnterpriseAuthorize, getParkFilledSummary)
|
||||
router.Get("/report/:rid/pooled", security.MustAuthenticated, listPooledMetersInReport)
|
||||
router.Get("/report/:rid/pooled/:code/submeter", security.MustAuthenticated, listSubmetersInPooledMeter)
|
||||
router.Get("/report/:rid/tenement", security.MustAuthenticated, listTenementsInReport)
|
||||
router.Get("/report/:rid/tenement/:tid", security.MustAuthenticated, getTenementDetailInReport)
|
||||
}
|
||||
|
||||
func ensureReportBelongs(c *fiber.Ctx, result *response.Result, requestReportId string) (bool, error) {
|
||||
_, err := _retreiveSession(c)
|
||||
// 检查指定报表是否属于当前用户
|
||||
func checkReportBelongs(reportId string, log *zap.Logger, c *fiber.Ctx, result *response.Result) (bool, error) {
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
return false, result.Unauthorized(err.Error())
|
||||
log.Error("无法获取当前用户的会话信息", zap.Error(err))
|
||||
return false, result.Unauthorized("无法获取当前用户的会话信息。")
|
||||
}
|
||||
requestReport, err := service.ReportService.RetreiveReportIndex(requestReportId)
|
||||
ok, err := repository.ReportRepository.IsBelongsTo(reportId, session.Uid)
|
||||
if err != nil {
|
||||
return false, result.NotFound(err.Error())
|
||||
log.Error("无法检查核算报表的所有权", zap.Error(err))
|
||||
return false, result.Error(fiber.StatusInternalServerError, "无法检查核算报表的所有权。")
|
||||
}
|
||||
if requestReport == nil {
|
||||
return false, result.NotFound("指定报表未能找到。")
|
||||
if !ok {
|
||||
log.Error("核算报表不属于当前用户")
|
||||
return false, result.Forbidden("核算报表不属于当前用户。")
|
||||
}
|
||||
return ensureParkBelongs(c, result, requestReport.ParkId)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func fetchNewestReportOfParkWithDraft(c *fiber.Ctx) error {
|
||||
// 获取当前登录用户下所有园区的尚未发布的核算报表索引
|
||||
func listDraftReportIndicies(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
userSession, err := _retreiveSession(c)
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
return result.Unauthorized(err.Error())
|
||||
reportLog.Error("无法获取当前用户的会话信息", zap.Error(err))
|
||||
return result.Unauthorized("无法获取当前用户的会话信息。")
|
||||
}
|
||||
parks, err := service.ReportService.FetchParksWithNewestReport(userSession.Uid)
|
||||
reportLog.Info("检索指定用户下的未发布核算报表索引", zap.String("User", session.Uid))
|
||||
indicies, err := service.ReportService.ListDraftReportIndicies(session.Uid)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
reportLog.Error("无法获取当前用户的核算报表索引", zap.Error(err))
|
||||
return result.NotFound("当前用户下未找到核算报表索引。")
|
||||
}
|
||||
return result.Json(http.StatusOK, "已获取到指定用户下所有园区的最新报表记录。", fiber.Map{"parks": parks})
|
||||
return result.Success(
|
||||
"已经获取到指定用户的报表索引。",
|
||||
fiber.Map{"reports": indicies},
|
||||
)
|
||||
}
|
||||
|
||||
func initializeNewReport(c *fiber.Ctx) error {
|
||||
// 初始化一个新的核算任务
|
||||
func initNewReportCalculateTask(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestParkId := c.Params("pid")
|
||||
userSession, err := _retreiveSession(c)
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
return result.Unauthorized(err.Error())
|
||||
reportLog.Error("无法获取当前用户的会话信息", zap.Error(err))
|
||||
return result.Unauthorized("无法获取当前用户的会话信息。")
|
||||
}
|
||||
if ensure, err := ensureParkBelongs(c, &result, requestParkId); !ensure {
|
||||
reportLog.Info("初始化指定用户的一个新核算任务", zap.String("User", session.Uid))
|
||||
var form vo.ReportCreationForm
|
||||
if err := c.BodyParser(&form); err != nil {
|
||||
reportLog.Error("无法解析创建核算报表的请求数据。", zap.Error(err))
|
||||
return result.BadRequest("无法解析创建核算报表的请求数据。")
|
||||
}
|
||||
if pass, err := checkParkBelongs(form.Park, reportLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
requestPeriod := c.Query("period")
|
||||
reportPeriod, err := time.Parse("2006-01", requestPeriod)
|
||||
ok, err := service.ReportService.CreateNewReport(&form)
|
||||
if err != nil {
|
||||
return result.NotAccept("提供的初始化期数格式不正确。")
|
||||
reportLog.Error("无法创建核算报表", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "无法创建核算报表。")
|
||||
}
|
||||
valid, err := service.ReportService.IsNewPeriodValid(userSession.Uid, requestParkId, reportPeriod)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
if !ok {
|
||||
reportLog.Error("未能完成核算报表的保存。")
|
||||
return result.NotAccept("未能完成核算报表的保存。")
|
||||
}
|
||||
if !valid {
|
||||
return result.NotAccept("只能初始化已发布报表下一个月份的新报表。")
|
||||
}
|
||||
newId, err := service.ReportService.InitializeNewReport(requestParkId, reportPeriod)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Created("新一期报表初始化成功。", fiber.Map{"reportId": newId})
|
||||
return result.Success("已经成功创建核算报表。")
|
||||
}
|
||||
|
||||
func fetchReportStepStates(c *fiber.Ctx) error {
|
||||
// 更新指定的核算任务
|
||||
func updateReportCalculateTask(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
if ensure, err := ensureReportBelongs(c, &result, requestReportId); !ensure {
|
||||
reportId := c.Params("rid")
|
||||
if pass, err := checkReportBelongs(reportId, reportLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
requestReport, err := service.ReportService.RetreiveReportIndex(requestReportId)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
var form vo.ReportModifyForm
|
||||
if err := c.BodyParser(&form); err != nil {
|
||||
reportLog.Error("无法解析更新核算报表的请求数据。", zap.Error(err))
|
||||
return result.BadRequest("无法解析更新核算报表的请求数据。")
|
||||
}
|
||||
return result.Json(http.StatusOK, "已经获取到指定报表的填写状态。", fiber.Map{"steps": requestReport.StepState})
|
||||
ok, err := service.ReportService.UpdateRepoet(reportId, &form)
|
||||
if err != nil {
|
||||
reportLog.Error("无法更新核算报表", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "无法更新核算报表。")
|
||||
}
|
||||
if !ok {
|
||||
reportLog.Error("未能完成核算报表的更新。")
|
||||
return result.NotAccept("未能完成核算报表的更新。")
|
||||
}
|
||||
return result.Success("已经成功更新核算报表。")
|
||||
}
|
||||
|
||||
func fetchReportParkSummary(c *fiber.Ctx) error {
|
||||
// 启动指定的核算任务
|
||||
func initiateCalculateTask(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
if ensure, err := ensureReportBelongs(c, &result, requestReportId); !ensure {
|
||||
reportId := c.Params("rid")
|
||||
if pass, err := checkReportBelongs(reportId, reportLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
summary, err := service.ReportService.RetreiveReportSummary(requestReportId)
|
||||
err := service.ReportService.DispatchReportCalculate(reportId)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
reportLog.Error("无法启动核算报表计算任务", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "无法启动核算报表计算任务。")
|
||||
}
|
||||
//开启核算任务
|
||||
err = service.ReportService.ReportCalcuateDispatch(reportId)
|
||||
if err != nil {
|
||||
return result.Error(500, "核算任务启动失败"+err.Error())
|
||||
}
|
||||
return result.Success("已经成功启动核算报表计算任务。")
|
||||
}
|
||||
|
||||
// 获取自己园区的已经填写的园区电量信息
|
||||
func getParkFilledSummary(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
reportId := c.Params("rid")
|
||||
if pass, err := checkReportBelongs(reportId, reportLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
reportLog.Info("获取园区电量信息", zap.String("Report", reportId))
|
||||
summary, err := repository.ReportRepository.RetrieveReportSummary(reportId)
|
||||
if err != nil {
|
||||
reportLog.Error("无法获取核算报表的园区电量信息", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "无法获取核算报表的园区电量信息。")
|
||||
}
|
||||
if summary == nil {
|
||||
return result.NotFound("指定报表未能找到。")
|
||||
reportLog.Error("未找到核算报表的园区电量信息")
|
||||
return result.NotFound("未找到核算报表的园区电量信息。")
|
||||
}
|
||||
return result.Json(http.StatusOK, "已经获取到指定报表中的园区概况。", fiber.Map{"summary": summary})
|
||||
}
|
||||
|
||||
type ReportSummaryFormData struct {
|
||||
Overall decimal.Decimal `json:"overall" form:"overall"`
|
||||
OverallFee decimal.Decimal `json:"overallFee" form:"overallFee"`
|
||||
Critical decimal.Decimal `json:"critical" form:"critical"`
|
||||
CriticalFee decimal.Decimal `json:"criticalFee" form:"criticalFee"`
|
||||
Peak decimal.Decimal `json:"peak" form:"peak"`
|
||||
PeakFee decimal.Decimal `json:"peakFee" form:"peakFee"`
|
||||
Valley decimal.Decimal `json:"valley" form:"valley"`
|
||||
ValleyFee decimal.Decimal `json:"valleyFee" form:"valleyFee"`
|
||||
BasicFee decimal.Decimal `json:"basicFee" form:"basicFee"`
|
||||
AdjustFee decimal.Decimal `json:"adjustFee" from:"adjustFee"`
|
||||
}
|
||||
|
||||
func fillReportSummary(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
if ensure, err := ensureReportBelongs(c, &result, requestReportId); !ensure {
|
||||
return err
|
||||
}
|
||||
formData := new(ReportSummaryFormData)
|
||||
if err := c.BodyParser(formData); err != nil {
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
originSummary, err := service.ReportService.RetreiveReportSummary(requestReportId)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
}
|
||||
copier.Copy(originSummary, formData)
|
||||
originSummary.ReportId = requestReportId
|
||||
err = service.ReportService.UpdateReportSummary(originSummary)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Updated("指定电费公示报表中的园区概况基本数据已经完成更新。")
|
||||
var summaryResponse vo.SimplifiedReportSummary
|
||||
copier.Copy(&summaryResponse, summary)
|
||||
return result.Success(
|
||||
"已经获取到核算报表的园区电量信息。",
|
||||
fiber.Map{"summary": summaryResponse},
|
||||
)
|
||||
}
|
||||
|
||||
// 对提供的园区电量信息进行试计算,返回试计算结果
|
||||
func testCalculateReportSummary(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
if ensure, err := ensureReportBelongs(c, &result, requestReportId); !ensure {
|
||||
return err
|
||||
reportLog.Info("试计算园区电量信息")
|
||||
var form vo.TestCalculateForm
|
||||
if err := c.BodyParser(&form); err != nil {
|
||||
reportLog.Error("无法解析试计算核算报表的请求数据。", zap.Error(err))
|
||||
return result.BadRequest("无法解析试计算核算报表的请求数据。")
|
||||
}
|
||||
summary, err := service.ReportService.RetreiveReportSummary(requestReportId)
|
||||
return result.Success(
|
||||
"电量电费试计算已经完成。",
|
||||
fiber.Map{"summary": form.Calculate()},
|
||||
)
|
||||
}
|
||||
|
||||
// 获取指定园区中尚未发布的核算报表计算状态
|
||||
func listCalculateTaskStatus(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
reportLog.Error("无法获取当前用户的会话信息", zap.Error(err))
|
||||
return result.Unauthorized("无法获取当前用户的会话信息。")
|
||||
}
|
||||
summary.CalculatePrices()
|
||||
calcResults := tools.ConvertStructToMap(summary)
|
||||
return result.Json(
|
||||
http.StatusOK,
|
||||
"已完成园区概况的试计算。",
|
||||
status, err := repository.ReportRepository.GetReportTaskStatus(session.Uid)
|
||||
if err != nil {
|
||||
reportLog.Error("无法获取核算报表计算状态", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "无法获取核算报表计算状态。")
|
||||
}
|
||||
statusResponse := make([]*vo.ReportCalculateTaskStatusResponse, 0)
|
||||
copier.Copy(&statusResponse, &status)
|
||||
return result.Success(
|
||||
"已经获取到核算报表计算状态。",
|
||||
fiber.Map{"status": statusResponse},
|
||||
)
|
||||
}
|
||||
|
||||
// 获取指定报表的详细信息
|
||||
func getReportDetail(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
reportId := c.Params("rid")
|
||||
reportLog.Info("获取核算报表的详细信息", zap.String("Report", reportId))
|
||||
user, park, report, err := service.ReportService.RetrieveReportIndexDetail(reportId)
|
||||
if err != nil {
|
||||
reportLog.Error("无法获取核算报表的详细信息", zap.Error(err))
|
||||
return result.NotFound("无法获取核算报表的详细信息。")
|
||||
}
|
||||
return result.Success(
|
||||
"已经获取到核算报表的详细信息。",
|
||||
fiber.Map{
|
||||
"result": lo.PickByKeys(
|
||||
calcResults,
|
||||
[]string{"overallPrice", "criticalPrice", "peakPrice", "flat", "flatFee", "flatPrice", "valleyPrice", "consumptionFee"},
|
||||
),
|
||||
"detail": vo.NewReportDetailQueryResponse(user, park, report),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func progressReportSummary(c *fiber.Ctx) error {
|
||||
// 获取指定核算报表的总览信息
|
||||
func getReportSummary(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
if ensure, err := ensureReportBelongs(c, &result, requestReportId); !ensure {
|
||||
return err
|
||||
}
|
||||
err := service.ReportService.CalculateSummaryAndFinishStep(requestReportId)
|
||||
reportId := c.Params("rid")
|
||||
report, err := repository.ReportRepository.RetrieveReportSummary(reportId)
|
||||
if err != nil {
|
||||
if nfErr, ok := err.(exceptions.NotFoundError); ok {
|
||||
return result.NotFound(nfErr.Error())
|
||||
} else {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
reportLog.Error("无法获取核算报表的总览信息", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "无法获取核算报表的总览信息。")
|
||||
}
|
||||
if report == nil {
|
||||
reportLog.Error("未找到核算报表的总览信息")
|
||||
return result.NotFound("未找到核算报表的总览信息。")
|
||||
}
|
||||
return result.Success("已经完成园区概况的计算,并可以进行到下一步骤。")
|
||||
}
|
||||
|
||||
func progressEndUserRegister(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
if ensure, err := ensureReportBelongs(c, &result, requestReportId); !ensure {
|
||||
return err
|
||||
}
|
||||
report, err := service.ReportService.RetreiveReportIndex(requestReportId)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
}
|
||||
err = service.ReportService.ProgressReportRegisterEndUser(*report)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Success("终端用户抄表编辑步骤已经完成。")
|
||||
}
|
||||
|
||||
func publishReport(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
if ensure, err := ensureReportBelongs(c, &result, requestReportId); !ensure {
|
||||
return err
|
||||
}
|
||||
report, err := service.ReportService.RetreiveReportIndex(requestReportId)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
}
|
||||
err = service.ReportService.PublishReport(*report)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Success("指定的公示报表已经发布。")
|
||||
}
|
||||
|
||||
func searchReports(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
requestUser := lo.
|
||||
If(session.Type == model.USER_TYPE_ENT, session.Uid).
|
||||
Else(c.Query("user"))
|
||||
requestPark := c.Query("park")
|
||||
if len(requestPark) > 0 && session.Type == model.USER_TYPE_ENT {
|
||||
if ensure, err := ensureParkBelongs(c, &result, requestPark); !ensure {
|
||||
return err
|
||||
}
|
||||
}
|
||||
requestPeriodString := c.Query("period")
|
||||
var requestPeriod *time.Time = nil
|
||||
if len(requestPeriodString) > 0 {
|
||||
parsedPeriod, err := time.Parse("2006-01", requestPeriodString)
|
||||
if err != nil {
|
||||
return result.NotAccept("参数[period]的格式不正确。")
|
||||
}
|
||||
requestPeriod = lo.ToPtr(parsedPeriod)
|
||||
}
|
||||
requestKeyword := c.Query("keyword")
|
||||
requestPage, err := strconv.Atoi(c.Query("page", "1"))
|
||||
if err != nil {
|
||||
return result.NotAccept("查询参数[page]格式不正确。")
|
||||
}
|
||||
requestAllReports, err := strconv.ParseBool(c.Query("all", "false"))
|
||||
if err != nil {
|
||||
return result.NotAccept("查询参数[all]格式不正确。")
|
||||
}
|
||||
records, totalItems, err := service.ReportService.SearchReport(requestUser, requestPark, requestKeyword, requestPeriod, requestPage, !requestAllReports)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
summaryResponse := vo.ParkSummaryResponse{
|
||||
ReportId: report.ReportId,
|
||||
OverallDisplay: vo.ConsumptionDisplay{
|
||||
AmountStr: strconv.FormatFloat(report.Overall.Amount.InexactFloat64(), 'f', -1, 64),
|
||||
FeeStr: strconv.FormatFloat(report.Overall.Fee.InexactFloat64(), 'f', -1, 64),
|
||||
PriceStr: strconv.FormatFloat(report.Overall.Price.InexactFloat64(), 'f', -1, 64),
|
||||
ProportionStr: strconv.FormatFloat(report.Overall.Proportion.InexactFloat64(), 'f', -1, 64),
|
||||
},
|
||||
Area: report.OverallArea,
|
||||
BasicFee: report.BasicFee,
|
||||
PooledBasicFeeByAmount: report.BasicPooledPriceConsumption.Decimal,
|
||||
PooledBasicFeeByArea: report.BasicPooledPriceArea.Decimal,
|
||||
AdjustFee: report.AdjustFee,
|
||||
PooledAdjustFeeByAmount: report.AdjustPooledPriceConsumption.Decimal,
|
||||
PooledAdjustFeeByArea: report.AdjustPooledPriceArea.Decimal,
|
||||
Consumption: report.ConsumptionFee.Decimal,
|
||||
//Loss: report.Loss.Decimal,
|
||||
Loss: report.AuthorizeLoss.Amount,
|
||||
//LossRate: report.LossFee.Decimal,
|
||||
LossRate: report.AuthorizeLoss.Proportion,
|
||||
}
|
||||
//copier.Copy(&summaryResponse, report)
|
||||
return result.Success(
|
||||
"已经取得符合条件的公示报表记录。",
|
||||
response.NewPagedResponse(requestPage, totalItems).ToMap(),
|
||||
fiber.Map{"reports": records},
|
||||
"已经获取到核算报表的总览信息。",
|
||||
fiber.Map{"summary": summaryResponse},
|
||||
)
|
||||
}
|
||||
|
||||
func fetchReportPublicity(c *fiber.Ctx) error {
|
||||
// 获取指定报表中分页的公共表计的核算摘要信息
|
||||
func listPublicMetersInReport(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
publicity, err := service.ReportService.AssembleReportPublicity(requestReportId)
|
||||
reportId := c.Params("rid")
|
||||
reportLog.Info("获取核算报表中的公共表计信息", zap.String("Report", reportId))
|
||||
page := c.QueryInt("page", 1)
|
||||
keyword := tools.EmptyToNil(c.Query("keyword"))
|
||||
meters, total, err := repository.ReportRepository.ListPublicMetersInReport(reportId, uint(page), keyword)
|
||||
if err != nil {
|
||||
if nfErr, ok := err.(exceptions.NotFoundError); ok {
|
||||
return result.NotFound(nfErr.Error())
|
||||
} else {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
}
|
||||
return result.Success("已经取得指定公示报表的发布版本。", tools.ConvertStructToMap(publicity))
|
||||
reportLog.Error("无法获取核算报表中的公共表计信息", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "无法获取核算报表中的公共表计信息。")
|
||||
}
|
||||
|
||||
func calculateReport(c *fiber.Ctx) error {
|
||||
var meterResponses []vo.Public
|
||||
for _, meter := range meters {
|
||||
meterResponse := vo.Public{
|
||||
Address: meter.Address,
|
||||
AdjustLoss: model.ConsumptionUnit{
|
||||
Amount: meter.LossAdjust.Amount,
|
||||
Fee: meter.LossAdjust.Fee,
|
||||
Price: meter.LossAdjust.Price,
|
||||
Proportion: meter.LossAdjust.Proportion,
|
||||
},
|
||||
Area: meter.Area.Decimal.String(),
|
||||
AttachedAt: meter.AttachedAt,
|
||||
Building: meter.Building,
|
||||
BuildingName: meter.BuildingName,
|
||||
Code: meter.ParkMeterID,
|
||||
DetachedAt: meter.DetachedAt,
|
||||
DisplayRatio: strconv.FormatFloat(meter.DisplayRatio, 'f', -1, 64),
|
||||
Enabled: meter.Enabled,
|
||||
OnFloor: meter.OnFloor,
|
||||
Overall: model.ConsumptionUnit{
|
||||
Amount: meter.Overall.Amount,
|
||||
Fee: meter.Overall.Fee,
|
||||
Price: meter.Overall.Price,
|
||||
Proportion: meter.Overall.Proportion,
|
||||
},
|
||||
ParkID: meter.ParkID,
|
||||
Ratio: meter.Ratio.String(),
|
||||
Seq: meter.Seq,
|
||||
Type: float64(meter.MeterType),
|
||||
}
|
||||
|
||||
meterResponses = append(meterResponses, meterResponse)
|
||||
}
|
||||
|
||||
return result.Success(
|
||||
"已经获取到指定核算报表中的分页公共表计的核算信息。",
|
||||
response.NewPagedResponse(page, total).ToMap(),
|
||||
fiber.Map{"public": meterResponses},
|
||||
)
|
||||
}
|
||||
|
||||
// 获取指定报表中的分页的公摊表计的核算摘要信息
|
||||
func listPooledMetersInReport(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
if ensure, err := ensureReportBelongs(c, &result, requestReportId); !ensure {
|
||||
reportId := c.Params("rid")
|
||||
reportLog.Info("获取核算报表中的公摊表计信息", zap.String("Report", reportId))
|
||||
page := c.QueryInt("page", 1)
|
||||
keyword := tools.EmptyToNil(c.Query("keyword"))
|
||||
meters, total, err := repository.ReportRepository.ListPooledMetersInReport(reportId, uint(page), keyword)
|
||||
if err != nil {
|
||||
reportLog.Error("无法获取核算报表中的公摊表计信息", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "无法获取核算报表中的公摊表计信息。")
|
||||
}
|
||||
meterResponse := lo.Map(meters, func(meter *model.ReportDetailedPooledConsumption, _ int) *vo.ReportPooledQueryResponse {
|
||||
m := &vo.ReportPooledQueryResponse{}
|
||||
m.FromReportDetailPooledConsumption(meter)
|
||||
return m
|
||||
})
|
||||
return result.Success(
|
||||
"已经获取到指定核算报表中的分页公摊表计的核算信息。",
|
||||
response.NewPagedResponse(page, total).ToMap(),
|
||||
fiber.Map{"pooled": meterResponse},
|
||||
)
|
||||
}
|
||||
|
||||
// 列出指定报表中指定公共表计下各个分摊表计的消耗数据
|
||||
func listSubmetersInPooledMeter(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
reportId := c.Params("rid")
|
||||
meterId := c.Params("code")
|
||||
if len(meterId) == 0 {
|
||||
reportLog.Error("未提供公共表计的编号")
|
||||
return result.BadRequest("未提供公共表计的编号。")
|
||||
}
|
||||
meters, err := repository.ReportRepository.ListPooledMeterDetailInReport(reportId, meterId)
|
||||
if err != nil {
|
||||
reportLog.Error("无法获取核算报表中的公共表计信息", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "无法获取核算报表中的公共表计信息。")
|
||||
}
|
||||
meterResponse := lo.Map(meters, func(meter *model.ReportDetailNestedMeterConsumption, _ int) *vo.ReportPooledQueryResponse {
|
||||
m := &vo.ReportPooledQueryResponse{}
|
||||
m.FromReportDetailNestedMeterConsumption(meter)
|
||||
return m
|
||||
})
|
||||
return result.Success(
|
||||
"已经获取到指定核算报表中的公共表计的核算信息。",
|
||||
fiber.Map{"meters": meterResponse},
|
||||
)
|
||||
}
|
||||
|
||||
// 获取指定报表中分页的商户核算电量电费概要数据
|
||||
func listTenementsInReport(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
reportId := c.Params("rid")
|
||||
page := c.QueryInt("page", 1)
|
||||
keyword := tools.EmptyToNil(c.Query("keyword"))
|
||||
tenements, total, err := repository.ReportRepository.ListTenementInReport(reportId, uint(page), keyword)
|
||||
if err != nil {
|
||||
reportLog.Error("无法获取核算报表中的商户信息", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "无法获取核算报表中的商户信息。")
|
||||
}
|
||||
tenementsResponse := lo.Map(tenements, func(tenement *model.ReportTenement, _ int) *vo.ReportTenementSummaryResponse {
|
||||
t := &vo.ReportTenementSummaryResponse{}
|
||||
t.FromReportTenement(tenement)
|
||||
return t
|
||||
})
|
||||
return result.Success(
|
||||
"已经获取到指定核算报表中的分页商户的核算信息。",
|
||||
response.NewPagedResponse(page, total).ToMap(),
|
||||
fiber.Map{"tenements": tenementsResponse},
|
||||
)
|
||||
}
|
||||
|
||||
// 获取指定报表中指定商户的详细核算信息
|
||||
func getTenementDetailInReport(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
reportId := c.Params("rid")
|
||||
tenementId := c.Params("tid")
|
||||
detail, err := repository.ReportRepository.GetTenementDetailInReport(reportId, tenementId)
|
||||
if err != nil {
|
||||
reportLog.Error("无法获取核算报表中的商户信息", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "无法获取核算报表中的商户信息。")
|
||||
}
|
||||
var detailResponse vo.ReportTenementDetailResponse
|
||||
detailResponse.FromReportTenement(detail)
|
||||
return result.Success(
|
||||
"已经获取到指定核算报表中的商户的详细核算信息。",
|
||||
fiber.Map{"detail": detailResponse},
|
||||
)
|
||||
}
|
||||
|
||||
// 发布指定的核算报表
|
||||
func publishReport(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
reportId := c.Params("rid")
|
||||
if pass, err := checkReportBelongs(reportId, reportLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
err := service.CalculateService.ComprehensivelyCalculateReport(requestReportId)
|
||||
ok, err := repository.ReportRepository.PublishReport(reportId)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
reportLog.Error("无法发布核算报表", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "发布核算报表出错。")
|
||||
}
|
||||
return result.Success("指定公示报表中的数据已经计算完毕。")
|
||||
if !ok {
|
||||
reportLog.Error("未能完成核算报表的发布。")
|
||||
return result.NotAccept("未能完成核算报表的发布。")
|
||||
}
|
||||
return result.Success("已经成功发布核算报表。")
|
||||
}
|
||||
|
||||
// 对核算报表进行综合检索
|
||||
func reportComprehensiveSearch(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
user := tools.EmptyToNil(c.Query("user"))
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
reportLog.Error("无法获取当前用户的会话信息", zap.Error(err))
|
||||
return result.Unauthorized("无法获取当前用户的会话信息。")
|
||||
}
|
||||
park := tools.EmptyToNil(c.Query("park"))
|
||||
if session.Type == model.USER_TYPE_ENT && park != nil && len(*park) > 0 {
|
||||
if pass, err := checkParkBelongs(*park, reportLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
}
|
||||
var requestUser *string
|
||||
if session.Type == model.USER_TYPE_ENT {
|
||||
requestUser = lo.ToPtr(tools.DefaultTo(user, session.Uid))
|
||||
} else {
|
||||
requestUser = user
|
||||
}
|
||||
page := c.QueryInt("page", 1)
|
||||
keyword := tools.EmptyToNil(c.Query("keyword"))
|
||||
startDate, err := types.ParseDatep(c.Query("period_start"))
|
||||
if err != nil {
|
||||
reportLog.Error("无法解析核算报表查询的开始日期", zap.Error(err))
|
||||
return result.BadRequest("无法解析核算报表查询的开始日期。")
|
||||
}
|
||||
endDate, err := types.ParseDatep(c.Query("period_end"))
|
||||
if err != nil {
|
||||
reportLog.Error("无法解析核算报表查询的结束日期", zap.Error(err))
|
||||
return result.BadRequest("无法解析核算报表查询的结束日期。")
|
||||
}
|
||||
reports, total, err := service.ReportService.QueryReports(requestUser, park, uint(page), keyword, startDate, endDate)
|
||||
if err != nil {
|
||||
reportLog.Error("无法查询核算报表", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "无法查询核算报表。")
|
||||
}
|
||||
return result.Success(
|
||||
"已经获取到指定核算报表的分页信息。",
|
||||
response.NewPagedResponse(page, total).ToMap(),
|
||||
fiber.Map{"reports": reports},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,31 +1,36 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"electricity_bill_calc/service"
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var StatisticsWithdrawLog = logger.Named("Handler", "StatisticsWithdraw")
|
||||
|
||||
func InitializeStatisticsController(router *fiber.App) {
|
||||
router.Get("/audits", security.OPSAuthorize, currentAuditAmount)
|
||||
router.Get("/stat/reports", security.MustAuthenticated, statReports)
|
||||
router.Get("/stat/reports", security.OPSAuthorize, statReports)
|
||||
|
||||
}
|
||||
|
||||
//获取当前系统中待审核的内容数量
|
||||
func currentAuditAmount(c *fiber.Ctx) error {
|
||||
StatisticsWithdrawLog.Info("开始获取当前系统中待审核的内容数量")
|
||||
result := response.NewResult(c)
|
||||
amount, err := service.WithdrawService.AuditWaits()
|
||||
if err != nil {
|
||||
StatisticsWithdrawLog.Error("获取当前系统中待审核的内容数量出错", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Json(http.StatusOK, "已经获取到指定的统计信息。", fiber.Map{
|
||||
"amounts": map[string]int64{
|
||||
"withdraw": amount,
|
||||
},
|
||||
})
|
||||
|
||||
return result.Success("已经获取到指定的统计信息",
|
||||
fiber.Map{"withdraw": amount})
|
||||
}
|
||||
|
||||
func statReports(c *fiber.Ctx) error {
|
||||
@@ -42,28 +47,35 @@ func statReports(c *fiber.Ctx) error {
|
||||
if session.Type != 0 {
|
||||
enterprises, err = service.StatisticsService.EnabledEnterprises()
|
||||
if err != nil {
|
||||
StatisticsWithdrawLog.Error(err.Error())
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
parks, err = service.StatisticsService.EnabledParks()
|
||||
if err != nil {
|
||||
StatisticsWithdrawLog.Error(err.Error())
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
reports, err = service.StatisticsService.ParksNewestState()
|
||||
//TODO: 2023.07.26 报表数据库结构改变,此处逻辑复杂放在最后处理
|
||||
reports, err = service.StatisticsService.ParkNewestState()
|
||||
if err != nil {
|
||||
StatisticsWithdrawLog.Error(err.Error())
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
} else {
|
||||
parks, err = service.StatisticsService.EnabledParks(session.Uid)
|
||||
if err != nil {
|
||||
StatisticsWithdrawLog.Error(err.Error())
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
reports, err = service.StatisticsService.ParksNewestState(session.Uid)
|
||||
//TODO: 2023.07.26 报表数据库结构改变,此处逻辑复杂放在最后处理
|
||||
reports, err = service.StatisticsService.ParkNewestState(session.Uid)
|
||||
if err != nil {
|
||||
StatisticsWithdrawLog.Error(err.Error())
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return result.Json(http.StatusOK, "已经完成园区报告的统计。", fiber.Map{
|
||||
return result.Success("已经完成园区报告的统计。", fiber.Map{
|
||||
"statistics": fiber.Map{
|
||||
"enterprises": enterprises,
|
||||
"parks": parks,
|
||||
|
||||
121
controller/sync.go
Normal file
121
controller/sync.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/repository"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"electricity_bill_calc/service"
|
||||
"electricity_bill_calc/tools"
|
||||
"electricity_bill_calc/vo"
|
||||
"fmt"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var synchronizeLog = logger.Named("Handler", "Synchronize")
|
||||
|
||||
func InitializeSynchronizeHandlers(router *fiber.App) {
|
||||
router.Get("/synchronize/task", security.EnterpriseAuthorize, searchSynchronizeSchedules)
|
||||
router.Get("/synchronize/configuration", security.EnterpriseAuthorize, getSynchronizeConfiguration)
|
||||
router.Post("/synchronize/configuration", security.EnterpriseAuthorize, recordsynchronizeConfiguration)
|
||||
}
|
||||
|
||||
// 查询当前平台中符合查询条件的同步任务,企业用户无论传入什么用户ID条件,都仅能看到自己的同步任务
|
||||
func searchSynchronizeSchedules(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
synchronizeLog.Error("查询同步任务失败,未能获取当前用户会话信息", zap.Error(err))
|
||||
return result.Unauthorized("未能获取当前用户会话信息。")
|
||||
}
|
||||
parkId := tools.EmptyToNil(c.Params("park"))
|
||||
if parkId != nil && len(*parkId) > 0 {
|
||||
if pass, err := checkParkBelongs(*parkId, reportLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
}
|
||||
userId := tools.EmptyToNil(c.Params("user"))
|
||||
keyword := tools.EmptyToNil(c.Query("keyword"))
|
||||
page := c.QueryInt("page", 1)
|
||||
synchronizeLog.Info("查询当前平台中符合查询条件的同步任务。", zap.String("Ent", session.Uid), zap.Stringp("Park", parkId))
|
||||
schedules, total, err := repository.SynchronizeRepository.SearchSynchronizeSchedules(userId, parkId, uint(page), keyword)
|
||||
if err != nil {
|
||||
reportLog.Error("无法获取同步任务", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "无法获取同步任务")
|
||||
}
|
||||
return result.Success(
|
||||
" ",
|
||||
response.NewPagedResponse(page, total).ToMap(),
|
||||
fiber.Map{"tasks": schedules},
|
||||
)
|
||||
}
|
||||
|
||||
// 获取指定的同步任务配置
|
||||
func getSynchronizeConfiguration(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Query("park")
|
||||
userId := c.Query("user")
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
reportLog.Error("无法获取当前用户的会话信息", zap.Error(err))
|
||||
return result.Unauthorized("无法获取当前用户的会话信息。")
|
||||
}
|
||||
var user_id string
|
||||
if session.Type == model.USER_TYPE_ENT {
|
||||
user_id = session.Uid
|
||||
} else {
|
||||
if userId != "" {
|
||||
user_id = userId
|
||||
} else {
|
||||
return result.NotAccept(fmt.Sprintf("必须指定要记录同步任务的用户,%s", err.Error()))
|
||||
}
|
||||
}
|
||||
fmt.Println("pppppppppppppppppppppppppppp", parkId, len(parkId))
|
||||
if parkId == "" {
|
||||
return result.NotAccept("必须指定要获取同步任务的园区。")
|
||||
}
|
||||
|
||||
fmt.Printf(user_id)
|
||||
configurations, err := repository.SynchronizeRepository.RetrieveSynchronizeConfiguration(user_id, parkId)
|
||||
if err != nil {
|
||||
reportLog.Error("无法获取同步任务", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "无法获取同步任务")
|
||||
}
|
||||
return result.Success(
|
||||
" 123",
|
||||
fiber.Map{"setup": configurations},
|
||||
)
|
||||
}
|
||||
func recordsynchronizeConfiguration(c *fiber.Ctx) error {
|
||||
userId := c.Query("user")
|
||||
synchronizeLog.Info("记录一个新的同步任务配置", zap.String("user id", userId))
|
||||
session, err := _retreiveSession(c)
|
||||
result := response.NewResult(c)
|
||||
if err != nil {
|
||||
reportLog.Error("无法获取当前用户的会话信息", zap.Error(err))
|
||||
return result.Unauthorized("无法获取当前用户的会话信息。")
|
||||
}
|
||||
var Form vo.SynchronizeConfigurationCreateForm
|
||||
if err := c.BodyParser(&Form); err != nil {
|
||||
meterLog.Error("无法更新同步配置,无法解析表计更新表单", zap.Error(err))
|
||||
return result.NotAccept(err.Error())
|
||||
}
|
||||
var user_id string
|
||||
if session.Type == model.USER_TYPE_ENT {
|
||||
user_id = session.Uid
|
||||
} else {
|
||||
if userId != "" {
|
||||
user_id = userId
|
||||
} else {
|
||||
return result.NotAccept(fmt.Sprintf("必须指定更新同步任务的用户,%s", err.Error()))
|
||||
}
|
||||
}
|
||||
//configurations, err := repository.SynchronizeRepository.CreateSynchronizeConfiguration
|
||||
if err := service.SynchronizeService.CreateSynchronizeConfiguration(user_id, &Form); err != nil {
|
||||
synchronizeLog.Error("无法更新同步配置", zap.Error(err))
|
||||
return result.NotAccept(err.Error())
|
||||
}
|
||||
return result.Success("更新完成。")
|
||||
}
|
||||
288
controller/tenement.go
Normal file
288
controller/tenement.go
Normal file
@@ -0,0 +1,288 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/repository"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"electricity_bill_calc/service"
|
||||
"electricity_bill_calc/tools"
|
||||
"electricity_bill_calc/types"
|
||||
"electricity_bill_calc/vo"
|
||||
"fmt"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/samber/lo"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var tenementLog = logger.Named("Handler", "Tenement")
|
||||
|
||||
func InitializeTenementHandler(router *fiber.App) {
|
||||
router.Get("/tenement/choice", security.EnterpriseAuthorize, listTenementForChoice)
|
||||
router.Get("/tenement/:pid", security.EnterpriseAuthorize, listTenement)
|
||||
router.Put("/tenement/:pid/:tid", security.EnterpriseAuthorize, updateTenement)
|
||||
router.Get("/tenement/:pid/:tid", security.EnterpriseAuthorize, getTenementDetail)
|
||||
router.Get("/tenement/:pid/:tid/meter", security.EnterpriseAuthorize, listMeters)
|
||||
//TODO: 2023-07-19再apiFox上该请求是个PUT请求,后端接收是个POST请求,不知道是否有误或是缺少对应请求(apiFox测试请求返回值为405)[完成]
|
||||
router.Put("/tenement/:pid/:tid/move/out", security.EnterpriseAuthorize, moveOutTenement)
|
||||
router.Post("/tenement/:pid", security.EnterpriseAuthorize, addTenement)
|
||||
router.Post("/tenement/:pid/:tid/binding", security.EnterpriseAuthorize, bindMeterToTenement)
|
||||
//TODO: 2023-07-19再apiFox上该请求是个PUT请求,后端接收是个POST请求,不知道是否有误或是缺少对应请求(apiFox测试请求返回值为405)[完成]
|
||||
router.Put("/tenement/:pid/:tid/binding/:code/unbind", security.EnterpriseAuthorize, unbindMeterFromTenement)
|
||||
}
|
||||
|
||||
// 列出园区中的商户
|
||||
func listTenement(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
tenementLog.Info("列出园区中的商户", zap.String("Park", parkId))
|
||||
if pass, err := checkParkBelongs(parkId, tenementLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
page := c.QueryInt("page", 1)
|
||||
keyword := tools.EmptyToNil(c.Query("keyword"))
|
||||
building := tools.EmptyToNil(c.Query("building"))
|
||||
startDate, err := types.ParseDatep(c.Query("startDate"))
|
||||
if err != nil {
|
||||
tenementLog.Error("列出园区中的商户失败,未能解析查询开始日期", zap.Error(err))
|
||||
return result.BadRequest(err.Error())
|
||||
}
|
||||
endDate, err := types.ParseDatep(c.Query("endDate"))
|
||||
if err != nil {
|
||||
tenementLog.Error("列出园区中的商户失败,未能解析查询结束日期", zap.Error(err))
|
||||
return result.BadRequest(err.Error())
|
||||
}
|
||||
state := c.QueryInt("state", 0)
|
||||
tenements, total, err := repository.TenementRepository.ListTenements(parkId, uint(page), keyword, building, startDate, endDate, state)
|
||||
if err != nil {
|
||||
tenementLog.Error("列出园区中的商户失败,未能获取商户列表", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
tenementsResponse := make([]*vo.TenementQueryResponse, 0)
|
||||
copier.Copy(&tenementsResponse, &tenements)
|
||||
return result.Success(
|
||||
"已经获取到要查询的商户。",
|
||||
response.NewPagedResponse(page, total).ToMap(),
|
||||
fiber.Map{
|
||||
"tenements": tenementsResponse,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// 列出指定商户下所有的表计
|
||||
func listMeters(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
if pass, err := checkParkBelongs(parkId, tenementLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
tenementId := c.Params("tid")
|
||||
tenementLog.Info("列出指定商户下所有的表计", zap.String("Park", parkId), zap.String("Tenement", tenementId))
|
||||
meters, err := service.TenementService.ListMeter(parkId, tenementId)
|
||||
if err != nil {
|
||||
tenementLog.Error("列出指定商户下所有的表计失败,未能获取表计列表", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Success(
|
||||
"已经获取到要查询的表计。",
|
||||
fiber.Map{
|
||||
"meters": meters,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// 增加一个新的商户
|
||||
func addTenement(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
if pass, err := checkParkBelongs(parkId, tenementLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
tenementLog.Info("增加一个新的商户", zap.String("Park", parkId))
|
||||
var form vo.TenementCreationForm
|
||||
if err := c.BodyParser(&form); err != nil {
|
||||
tenementLog.Error("增加一个新的商户失败,未能解析要添加的商户信息", zap.Error(err))
|
||||
return result.BadRequest(fmt.Sprintf("无法解析要添加的商户信息,%s", err.Error()))
|
||||
}
|
||||
err := service.TenementService.CreateTenementRecord(parkId, &form)
|
||||
if err != nil {
|
||||
tenementLog.Error("增加一个新的商户失败,未能添加商户记录", zap.Error(err))
|
||||
return result.NotAccept(fmt.Sprintf("无法添加商户记录,%s", err.Error()))
|
||||
}
|
||||
return result.Success("已经成功添加商户。")
|
||||
}
|
||||
|
||||
// 给指定商户绑定一个新的表计
|
||||
func bindMeterToTenement(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
if pass, err := checkParkBelongs(parkId, tenementLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
tenementId := c.Params("tid")
|
||||
if len(tenementId) == 0 {
|
||||
tenementLog.Error("给指定商户绑定一个新的表计失败,未指定商户。")
|
||||
return result.BadRequest("未指定商户。")
|
||||
}
|
||||
tenementLog.Info("向指定商户绑定一个表计。", zap.String("Park", parkId), zap.String("Tenement", tenementId))
|
||||
var form vo.MeterReadingFormWithCode
|
||||
if err := c.BodyParser(&form); err != nil {
|
||||
tenementLog.Error("给指定商户绑定一个新的表计失败,未能解析要绑定的表计信息", zap.Error(err))
|
||||
return result.BadRequest(fmt.Sprintf("无法解析要绑定的表计信息,%s", err.Error()))
|
||||
}
|
||||
if !form.MeterReadingForm.Validate() {
|
||||
tenementLog.Error("给指定商户绑定一个新的表计失败,表计读数不能正确配平,尖锋电量、峰电量、谷电量之和超过总电量。")
|
||||
return result.NotAccept("表计读数不能正确配平,尖锋电量、峰电量、谷电量之和超过总电量。")
|
||||
}
|
||||
err := service.TenementService.BindMeter(parkId, tenementId, form.Code, &form.MeterReadingForm)
|
||||
if err != nil {
|
||||
tenementLog.Error("给指定商户绑定一个新的表计失败,未能绑定表计", zap.Error(err))
|
||||
return result.NotAccept(fmt.Sprintf("无法绑定表计,%s", err.Error()))
|
||||
}
|
||||
return result.Success("已经成功绑定表计。")
|
||||
}
|
||||
|
||||
// 从指定商户下解除一个表计的绑定
|
||||
func unbindMeterFromTenement(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
if pass, err := checkParkBelongs(parkId, tenementLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
tenementId := c.Params("tid")
|
||||
if len(tenementId) == 0 {
|
||||
tenementLog.Error("从指定商户下解除一个表计的绑定失败,未指定商户。")
|
||||
return result.BadRequest("未指定商户。")
|
||||
}
|
||||
meterCode := c.Params("code")
|
||||
if len(meterCode) == 0 {
|
||||
tenementLog.Error("从指定商户下解除一个表计的绑定失败,未指定表计。")
|
||||
return result.BadRequest("未指定表计。")
|
||||
}
|
||||
tenementLog.Info("从指定商户处解绑一个表计。", zap.String("Park", parkId), zap.String("Tenement", tenementId), zap.String("Meter", meterCode))
|
||||
var form vo.MeterReadingForm
|
||||
if err := c.BodyParser(&form); err != nil {
|
||||
tenementLog.Error("从指定商户下解除一个表计的绑定失败,未能解析要解除绑定的表计抄表数据。", zap.Error(err))
|
||||
return result.BadRequest(fmt.Sprintf("无法解析要解除绑定的表计抄表数据,%s", err.Error()))
|
||||
}
|
||||
err := service.TenementService.UnbindMeter(parkId, tenementId, meterCode, &form)
|
||||
if err != nil {
|
||||
tenementLog.Error("从指定商户下解除一个表计的绑定失败,未能解除绑定表计。", zap.Error(err))
|
||||
return result.NotAccept(fmt.Sprintf("无法解除绑定表计,%s", err.Error()))
|
||||
}
|
||||
return result.Success("已经成功解除表计绑定。")
|
||||
}
|
||||
|
||||
// 修改指定商户的详细信息
|
||||
func updateTenement(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
if pass, err := checkParkBelongs(parkId, tenementLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
tenementId := c.Params("tid")
|
||||
if len(tenementId) == 0 {
|
||||
tenementLog.Error("修改指定商户的详细信息失败,未指定商户。")
|
||||
return result.BadRequest("未指定商户。")
|
||||
}
|
||||
tenementLog.Info("修改指定商户的详细信息。", zap.String("Park", parkId), zap.String("Tenement", tenementId))
|
||||
var form vo.TenementCreationForm
|
||||
if err := c.BodyParser(&form); err != nil {
|
||||
tenementLog.Error("修改指定商户的详细信息失败,未能解析要修改的商户信息", zap.Error(err))
|
||||
return result.BadRequest(fmt.Sprintf("无法解析要修改的商户信息,%s", err.Error()))
|
||||
}
|
||||
err := repository.TenementRepository.UpdateTenement(parkId, tenementId, &form)
|
||||
if err != nil {
|
||||
tenementLog.Error("修改指定商户的详细信息失败,未能修改商户信息", zap.Error(err))
|
||||
return result.NotAccept(fmt.Sprintf("无法修改商户信息,%s", err.Error()))
|
||||
}
|
||||
return result.Success("商户信息修改成功。")
|
||||
}
|
||||
|
||||
// 迁出指定园区中的商户
|
||||
func moveOutTenement(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
if pass, err := checkParkBelongs(parkId, tenementLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
tenementId := c.Params("tid")
|
||||
if len(tenementId) == 0 {
|
||||
tenementLog.Error("迁出指定园区中的商户失败,未指定商户。")
|
||||
return result.BadRequest("未指定商户。")
|
||||
}
|
||||
tenementLog.Info("迁出指定园区中的商户。", zap.String("Park", parkId), zap.String("Tenement", tenementId))
|
||||
var readings []*vo.MeterReadingFormWithCode
|
||||
if err := c.BodyParser(&readings); err != nil {
|
||||
tenementLog.Error("迁出指定园区中的商户失败,未能解析要迁出商户的抄表数据。", zap.Error(err))
|
||||
return result.BadRequest(fmt.Sprintf("无法解析要迁出商户的抄表数据,%s", err.Error()))
|
||||
}
|
||||
err := service.TenementService.MoveOutTenement(parkId, tenementId, readings)
|
||||
if err != nil {
|
||||
tenementLog.Error("迁出指定园区中的商户失败,未能迁出商户。", zap.Error(err))
|
||||
return result.NotAccept(fmt.Sprintf("无法迁出商户,%s", err.Error()))
|
||||
}
|
||||
return result.Success("商户迁出成功。")
|
||||
}
|
||||
|
||||
// 列出园区中的商户列表,主要用于下拉列表
|
||||
func listTenementForChoice(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
tenementLog.Error("列出园区中的商户列表失败,未能获取当前用户会话信息", zap.Error(err))
|
||||
return result.Unauthorized("未能获取当前用户会话信息。")
|
||||
}
|
||||
parkId := tools.EmptyToNil(c.Params("pid"))
|
||||
if parkId != nil && len(*parkId) > 0 {
|
||||
if pass, err := checkParkBelongs(*parkId, tenementLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
}
|
||||
tenementLog.Info("列出园区中的商户列表,主要用于下拉列表。", zap.String("Ent", session.Uid), zap.Stringp("Park", parkId))
|
||||
keyword := tools.EmptyToNil(c.Query("keyword"))
|
||||
limit := c.QueryInt("limit", 6)
|
||||
tenements, err := repository.TenementRepository.ListForSelect(session.Uid, parkId, keyword, lo.ToPtr(uint(limit)))
|
||||
if err != nil {
|
||||
tenementLog.Error("列出园区中的商户列表失败,未能获取商户列表", zap.Error(err))
|
||||
return result.NotFound(fmt.Sprintf("未能获取商户列表,%s", err.Error()))
|
||||
}
|
||||
var tenementsResponse []*vo.SimplifiedTenementResponse
|
||||
copier.Copy(&tenementsResponse, &tenements)
|
||||
return result.Success(
|
||||
"已经获取到要查询的商户。",
|
||||
fiber.Map{
|
||||
"tenements": tenementsResponse,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// 获取指定园区中指定商户的详细信息
|
||||
func getTenementDetail(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
parkId := c.Params("pid")
|
||||
if pass, err := checkParkBelongs(parkId, tenementLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
tenementId := c.Params("tid")
|
||||
if len(tenementId) == 0 {
|
||||
tenementLog.Error("获取指定园区中指定商户的详细信息失败,未指定商户。")
|
||||
return result.BadRequest("未指定商户。")
|
||||
}
|
||||
tenementLog.Info("获取指定园区中指定商户的详细信息。", zap.String("Park", parkId), zap.String("Tenement", tenementId))
|
||||
tenement, err := repository.TenementRepository.RetrieveTenementDetail(parkId, tenementId)
|
||||
if err != nil {
|
||||
tenementLog.Error("获取指定园区中指定商户的详细信息失败,未能获取商户信息", zap.Error(err))
|
||||
return result.NotFound(fmt.Sprintf("未能获取商户信息,%s", err.Error()))
|
||||
}
|
||||
var detail vo.TenementDetailResponse
|
||||
copier.Copy(&detail, &tenement)
|
||||
return result.Success(
|
||||
"已经获取到要查询的商户。",
|
||||
fiber.Map{
|
||||
"tenement": detail,
|
||||
},
|
||||
)
|
||||
}
|
||||
144
controller/top_up.go
Normal file
144
controller/top_up.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/repository"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"electricity_bill_calc/tools"
|
||||
"electricity_bill_calc/types"
|
||||
"electricity_bill_calc/vo"
|
||||
"fmt"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/jinzhu/copier"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var topUpLog = logger.Named("Controller", "TopUp")
|
||||
|
||||
func InitializeTopUpHandlers(router *fiber.App) {
|
||||
router.Get("/topup/:pid", security.EnterpriseAuthorize, listTopUps)
|
||||
router.Post("/topup/:pid", security.EnterpriseAuthorize, createTopUp)
|
||||
router.Get("/topup/:pid/:code", security.EnterpriseAuthorize, getTopUp)
|
||||
router.Delete("/topup/:pid/:code", security.EnterpriseAuthorize, deleteTopUp)
|
||||
}
|
||||
|
||||
// 查询符合条件的商户充值记录
|
||||
func listTopUps(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
park := tools.EmptyToNil(c.Params("pid"))
|
||||
if park == nil {
|
||||
topUpLog.Error("查询符合条件的商户充值记录,未指定要访问的园区")
|
||||
return result.BadRequest("未指定要访问的园区")
|
||||
}
|
||||
if pass, err := checkParkBelongs(*park, topUpLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
keyword := tools.EmptyToNil(c.Query("keyword"))
|
||||
startDate, err := types.ParseDatep(c.Query("start_date"))
|
||||
if err != nil {
|
||||
topUpLog.Error("查询符合条件的商户充值记录,查询起始日期格式错误", zap.Error(err))
|
||||
return result.BadRequest("查询起始日期格式错误")
|
||||
}
|
||||
endDate, err := types.ParseDatep(c.Query("end_date"))
|
||||
if err != nil {
|
||||
topUpLog.Error("查询符合条件的商户充值记录,查询结束日期格式错误", zap.Error(err))
|
||||
return result.BadRequest("查询结束日期格式错误")
|
||||
}
|
||||
page := c.QueryInt("page", 1)
|
||||
topUps, total, err := repository.TopUpRepository.ListTopUps(*park, startDate, endDate, keyword, uint(page))
|
||||
if err != nil {
|
||||
topUpLog.Error("查询符合条件的商户充值记录,查询失败", zap.Error(err))
|
||||
return result.Error(fiber.StatusInternalServerError, "商户充值记录查询不成功")
|
||||
}
|
||||
topUpLog.Debug("检查获取到的数据", zap.Any("topUps", topUps), zap.Int64("total", total))
|
||||
topUpDetails := make([]*vo.TopUpDetailQueryResponse, 0)
|
||||
copier.Copy(&topUpDetails, &topUps)
|
||||
topUpLog.Debug("检查转换后的数据", zap.Any("topUpDetails", topUpDetails))
|
||||
return result.Success(
|
||||
"已经获取到符合条件的商户充值记录",
|
||||
response.NewPagedResponse(page, total).ToMap(),
|
||||
fiber.Map{"topUps": topUpDetails},
|
||||
)
|
||||
}
|
||||
|
||||
// 获取指定充值记录的详细内容
|
||||
func getTopUp(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
park := tools.EmptyToNil(c.Params("pid"))
|
||||
if park == nil {
|
||||
topUpLog.Error("获取指定充值记录的详细内容,未指定要访问的园区")
|
||||
return result.BadRequest("未指定要访问的园区")
|
||||
}
|
||||
if pass, err := checkParkBelongs(*park, topUpLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
topUpCode := tools.EmptyToNil(c.Params("code"))
|
||||
if topUpCode == nil {
|
||||
topUpLog.Error("获取指定充值记录的详细内容,未指定要查询的充值记录")
|
||||
return result.BadRequest("未指定要查询的充值记录")
|
||||
}
|
||||
topUp, err := repository.TopUpRepository.GetTopUp(*park, *topUpCode)
|
||||
if err != nil {
|
||||
topUpLog.Error("获取指定充值记录的详细内容,查询失败", zap.Error(err))
|
||||
return result.NotFound("未找到指定的商户充值记录")
|
||||
}
|
||||
var topUpDetail vo.TopUpDetailQueryResponse
|
||||
copier.Copy(&topUpDetail, &topUp)
|
||||
return result.Success(
|
||||
"已经获取到指定充值记录的详细内容",
|
||||
fiber.Map{"topup": topUpDetail},
|
||||
)
|
||||
}
|
||||
|
||||
// 创建一条新的商户充值记录
|
||||
func createTopUp(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
park := tools.EmptyToNil(c.Params("pid"))
|
||||
if park == nil {
|
||||
topUpLog.Error("创建一条新的商户充值记录,未指定要访问的园区")
|
||||
return result.BadRequest("未指定要访问的园区")
|
||||
}
|
||||
if pass, err := checkParkBelongs(*park, topUpLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
var form vo.TopUpCreationForm
|
||||
if err := c.BodyParser(&form); err != nil {
|
||||
topUpLog.Error("创建一条新的商户充值记录,请求体解析失败", zap.Error(err))
|
||||
return result.BadRequest("请求体解析失败")
|
||||
}
|
||||
if err := repository.TopUpRepository.CreateTopUp(*park, &form); err != nil {
|
||||
topUpLog.Error("创建一条新的商户充值记录,创建失败", zap.Error(err))
|
||||
return result.NotAccept("商户充值记录创建不成功")
|
||||
}
|
||||
return result.Created(
|
||||
"已经创建一条新的商户充值记录",
|
||||
)
|
||||
}
|
||||
|
||||
// 删除一条指定的商户充值记录
|
||||
func deleteTopUp(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
park := tools.EmptyToNil(c.Params("pid"))
|
||||
if park == nil {
|
||||
topUpLog.Error("删除一条指定的商户充值记录,未指定要访问的园区")
|
||||
return result.BadRequest("未指定要访问的园区")
|
||||
}
|
||||
if pass, err := checkParkBelongs(*park, topUpLog, c, &result); !pass {
|
||||
return err
|
||||
}
|
||||
topUpCode := tools.EmptyToNil(c.Params("code"))
|
||||
if topUpCode == nil {
|
||||
topUpLog.Error("删除一条指定的商户充值记录,未指定要删除的充值记录")
|
||||
return result.BadRequest("未指定要删除的充值记录")
|
||||
}
|
||||
if err := repository.TopUpRepository.DeleteTopUp(*park, *topUpCode); err != nil {
|
||||
topUpLog.Error("删除一条指定的商户充值记录,删除失败", zap.Error(err))
|
||||
return result.NotAccept("商户充值记录删除不成功")
|
||||
}
|
||||
fmt.Println("已经删除一条指定的商户充值记录")
|
||||
return result.Success(
|
||||
"已经删除一条指定的商户充值记录",
|
||||
)
|
||||
}
|
||||
@@ -3,51 +3,56 @@ package controller
|
||||
import (
|
||||
"electricity_bill_calc/cache"
|
||||
"electricity_bill_calc/exceptions"
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/repository"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"electricity_bill_calc/service"
|
||||
"electricity_bill_calc/tools"
|
||||
"fmt"
|
||||
"electricity_bill_calc/vo"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/shopspring/decimal"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func InitializeUserController(router *fiber.App) {
|
||||
router.Delete("/password/:uid", security.OPSAuthorize, invalidUserPassword)
|
||||
router.Delete("/login", security.MustAuthenticated, logout)
|
||||
router.Put("/password", resetUserPassword)
|
||||
router.Get("/accounts", security.ManagementAuthorize, listPagedUser)
|
||||
router.Post("/login", login)
|
||||
router.Put("/account/enabled/state", security.OPSAuthorize, switchUserEnabling)
|
||||
router.Post("/account", security.OPSAuthorize, createOPSAndManagementAccount)
|
||||
router.Get("/account/:uid", security.MustAuthenticated, getUserDetail)
|
||||
func InitializeUserHandlers(router *fiber.App) {
|
||||
router.Delete("/login", security.MustAuthenticated, doLogout)
|
||||
router.Post("/login", doLogin)
|
||||
router.Get("/account", security.OPSAuthorize, searchUsers)
|
||||
router.Post("/account", security.OPSAuthorize, createOPSAccount)
|
||||
router.Get("/account/:uid", security.MustAuthenticated, fetchUserInformation)
|
||||
router.Put("/account/:uid", security.OPSAuthorize, modifyUserInformation)
|
||||
router.Put("/account/enabled/state", security.OPSAuthorize, changeUserState)
|
||||
router.Get("/expiration", security.EnterpriseAuthorize, getAccountExpiration)
|
||||
router.Post("/enterprise", security.OPSAuthorize, createEnterpriseAccount)
|
||||
router.Put("/account/:uid", security.OPSAuthorize, modifyAccountDetail)
|
||||
router.Get("/enterprise/quick/search", security.OPSAuthorize, quickSearchEnterprise)
|
||||
router.Get("/expiration", security.EnterpriseAuthorize, fetchExpiration)
|
||||
router.Put("/password", resetUserPassword)
|
||||
router.Delete("/password/:uid", security.OPSAuthorize, invalidUserPassword)
|
||||
}
|
||||
|
||||
type _LoginFormData struct {
|
||||
var userLog = logger.Named("Handler", "User")
|
||||
|
||||
type _LoginForm struct {
|
||||
Username string `json:"uname"`
|
||||
Password string `json:"upass"`
|
||||
Type int8 `json:"type"`
|
||||
Type int16 `json:"type"`
|
||||
}
|
||||
|
||||
func login(c *fiber.Ctx) error {
|
||||
func doLogin(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
loginData := new(_LoginFormData)
|
||||
loginData := new(_LoginForm)
|
||||
if err := c.BodyParser(loginData); err != nil {
|
||||
userLog.Error("表单解析失败!", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, "表单解析失败。")
|
||||
}
|
||||
var (
|
||||
session *model.Session
|
||||
err error
|
||||
)
|
||||
userLog.Info("有用户请求登录。", zap.String("username", loginData.Username), zap.Int16("type", loginData.Type))
|
||||
if loginData.Type == model.USER_TYPE_ENT {
|
||||
session, err = service.UserService.ProcessEnterpriseUserLogin(loginData.Username, loginData.Password)
|
||||
} else {
|
||||
@@ -60,74 +65,27 @@ func login(c *fiber.Ctx) error {
|
||||
}
|
||||
return result.Error(int(authError.Code), authError.Message)
|
||||
} else {
|
||||
userLog.Error("用户登录请求处理失败!", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
}
|
||||
return result.LoginSuccess(session)
|
||||
}
|
||||
|
||||
func logout(c *fiber.Ctx) error {
|
||||
func doLogout(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
session := c.Locals("session")
|
||||
if session == nil {
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
return result.Success("用户会话已结束。")
|
||||
}
|
||||
_, err := cache.ClearSession(session.(*model.Session).Token)
|
||||
_, err = cache.ClearSession(session.Token)
|
||||
if err != nil {
|
||||
userLog.Error("用户登出处理失败!", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Success("用户已成功登出系统。")
|
||||
}
|
||||
|
||||
func invalidUserPassword(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
targetUserId := c.Params("uid")
|
||||
verifyCode, err := service.UserService.InvalidUserPassword(targetUserId)
|
||||
if _, ok := err.(exceptions.NotFoundError); ok {
|
||||
return result.NotFound("未找到指定用户。")
|
||||
}
|
||||
if _, ok := err.(exceptions.UnsuccessfulOperationError); ok {
|
||||
return result.NotAccept("未能成功更新用户的密码。")
|
||||
}
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Json(http.StatusAccepted, "用户密码已经失效", fiber.Map{"verify": verifyCode})
|
||||
}
|
||||
|
||||
type _ResetPasswordFormData struct {
|
||||
VerifyCode string `json:"verifyCode"`
|
||||
Username string `json:"uname"`
|
||||
NewPassword string `json:"newPass"`
|
||||
}
|
||||
|
||||
func resetUserPassword(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
resetForm := new(_ResetPasswordFormData)
|
||||
if err := c.BodyParser(resetForm); err != nil {
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
verified, err := service.UserService.VerifyUserPassword(resetForm.Username, resetForm.VerifyCode)
|
||||
if _, ok := err.(exceptions.NotFoundError); ok {
|
||||
return result.NotFound("指定的用户不存在。")
|
||||
}
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if !verified {
|
||||
return result.Error(http.StatusUnauthorized, "验证码不正确。")
|
||||
}
|
||||
completed, err := service.UserService.ResetUserPassword(resetForm.Username, resetForm.NewPassword)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if completed {
|
||||
return result.Updated("用户凭据已更新。")
|
||||
}
|
||||
return result.NotAccept("用户凭据未能成功更新。")
|
||||
}
|
||||
|
||||
func listPagedUser(c *fiber.Ctx) error {
|
||||
func searchUsers(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestPage, err := strconv.Atoi(c.Query("page", "1"))
|
||||
if err != nil {
|
||||
@@ -145,213 +103,230 @@ func listPagedUser(c *fiber.Ctx) error {
|
||||
} else {
|
||||
requestUserStat = &state
|
||||
}
|
||||
users, total, err := service.UserService.ListUserDetail(requestKeyword, requestUserType, requestUserStat, requestPage)
|
||||
users, total, err := repository.UserRepository.FindUser(
|
||||
&requestKeyword,
|
||||
int16(requestUserType),
|
||||
requestUserStat,
|
||||
uint(requestPage),
|
||||
)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
}
|
||||
return result.Json(
|
||||
http.StatusOK,
|
||||
return result.Success(
|
||||
"已取得符合条件的用户集合。",
|
||||
response.NewPagedResponse(requestPage, total).ToMap(),
|
||||
fiber.Map{"accounts": users},
|
||||
)
|
||||
}
|
||||
|
||||
type _UserStateChangeFormData struct {
|
||||
UserID string `json:"uid" form:"uid"`
|
||||
Enabled bool `json:"enabled" form:"enabled"`
|
||||
}
|
||||
|
||||
func switchUserEnabling(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
switchForm := new(_UserStateChangeFormData)
|
||||
if err := c.BodyParser(switchForm); err != nil {
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
err := service.UserService.SwitchUserState(switchForm.UserID, switchForm.Enabled)
|
||||
if err != nil {
|
||||
if nfErr, ok := err.(*exceptions.NotFoundError); ok {
|
||||
return result.NotFound(nfErr.Message)
|
||||
} else {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
}
|
||||
return result.Updated("用户状态已经更新。")
|
||||
}
|
||||
|
||||
type _OPSAccountCreationFormData struct {
|
||||
Username string `json:"username" form:"username"`
|
||||
Name string `json:"name" form:"name"`
|
||||
Contact *string `json:"contact" form:"contact"`
|
||||
Phone *string `json:"phone" form:"phone"`
|
||||
Type int `json:"type" form:"type"`
|
||||
}
|
||||
|
||||
func createOPSAndManagementAccount(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
creationForm := new(_OPSAccountCreationFormData)
|
||||
if err := c.BodyParser(creationForm); err != nil {
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
exists, err := service.UserService.IsUsernameExists(creationForm.Username)
|
||||
if exists {
|
||||
return result.Conflict("指定的用户名已经被使用了。")
|
||||
}
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
newUser := new(model.User)
|
||||
newUser.Username = creationForm.Username
|
||||
newUser.Type = int8(creationForm.Type)
|
||||
newUser.Enabled = true
|
||||
newUserDetail := new(model.UserDetail)
|
||||
newUserDetail.Name = &creationForm.Name
|
||||
newUserDetail.Contact = creationForm.Contact
|
||||
newUserDetail.Phone = creationForm.Phone
|
||||
newUserDetail.UnitServiceFee = decimal.Zero
|
||||
newUserDetail.ServiceExpiration, _ = model.ParseDate("2099-12-31")
|
||||
verifyCode, err := service.UserService.CreateUser(newUser, newUserDetail)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
cache.AbolishRelation("user")
|
||||
return result.Json(http.StatusCreated, "用户已经成功创建。", fiber.Map{"verify": verifyCode})
|
||||
}
|
||||
|
||||
func getUserDetail(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
targetUserId := c.Params("uid")
|
||||
exists, err := service.UserService.IsUserExists(targetUserId)
|
||||
if !exists {
|
||||
return result.NotFound("指定的用户不存在。")
|
||||
}
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
userDetail, err := service.UserService.FetchUserDetail(targetUserId)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Json(http.StatusOK, "用户详细信息已获取到。", fiber.Map{"user": userDetail})
|
||||
}
|
||||
|
||||
type _EnterpriseCreationFormData struct {
|
||||
Username string `json:"username" form:"username"`
|
||||
Name string `json:"name" form:"name"`
|
||||
Region *string `json:"region" form:"region"`
|
||||
Address *string `json:"address" form:"address"`
|
||||
Contact *string `json:"contact" form:"contact"`
|
||||
Phone *string `json:"phone" form:"phone"`
|
||||
UnitServiceFee *string `json:"unitServiceFee" form:"unitServiceFee"`
|
||||
}
|
||||
|
||||
func createEnterpriseAccount(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
creationForm := new(_EnterpriseCreationFormData)
|
||||
if err := c.BodyParser(creationForm); err != nil {
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
exists, err := service.UserService.IsUsernameExists(creationForm.Username)
|
||||
if exists {
|
||||
return result.Conflict("指定的用户名已经被使用了。")
|
||||
}
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
newUser := new(model.User)
|
||||
newUser.Username = creationForm.Username
|
||||
newUser.Type = model.USER_TYPE_ENT
|
||||
newUser.Enabled = true
|
||||
newUserDetail := new(model.UserDetail)
|
||||
newUserDetail.Name = &creationForm.Name
|
||||
newUserDetail.Contact = creationForm.Contact
|
||||
newUserDetail.Phone = creationForm.Phone
|
||||
newUserDetail.UnitServiceFee, err = decimal.NewFromString(*creationForm.UnitServiceFee)
|
||||
if err != nil {
|
||||
return result.BadRequest("用户月服务费无法解析。")
|
||||
}
|
||||
newUserDetail.ServiceExpiration = model.NewEmptyDate()
|
||||
|
||||
verifyCode, err := service.UserService.CreateUser(newUser, newUserDetail)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
cache.AbolishRelation("user")
|
||||
return result.Json(http.StatusCreated, "用户已经成功创建。", fiber.Map{"verify": verifyCode})
|
||||
}
|
||||
|
||||
type _AccountModificationFormData struct {
|
||||
Name string `json:"name" form:"name"`
|
||||
Region *string `json:"region" form:"region"`
|
||||
Address *string `json:"address" form:"address"`
|
||||
Contact *string `json:"contact" form:"contact"`
|
||||
Phone *string `json:"phone" form:"phone"`
|
||||
UnitServiceFee *string `json:"unitServiceFee" form:"unitServiceFee"`
|
||||
}
|
||||
|
||||
func modifyAccountDetail(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
targetUserId := c.Params("uid")
|
||||
modForm := new(_AccountModificationFormData)
|
||||
if err := c.BodyParser(modForm); err != nil {
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
exists, err := service.UserService.IsUserExists(targetUserId)
|
||||
if !exists {
|
||||
return result.NotFound("指定的用户不存在。")
|
||||
}
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
newUserInfo := new(model.UserDetail)
|
||||
newUserInfo.Id = targetUserId
|
||||
newUserInfo.Name = &modForm.Name
|
||||
if len(modForm.Name) > 0 {
|
||||
abbr := tools.PinyinAbbr(modForm.Name)
|
||||
newUserInfo.Abbr = &abbr
|
||||
}
|
||||
newUserInfo.Region = modForm.Region
|
||||
newUserInfo.Address = modForm.Address
|
||||
newUserInfo.Contact = modForm.Contact
|
||||
newUserInfo.Phone = modForm.Phone
|
||||
newUserInfo.UnitServiceFee, err = decimal.NewFromString(*modForm.UnitServiceFee)
|
||||
if err != nil {
|
||||
return result.BadRequest("用户月服务费无法解析。")
|
||||
}
|
||||
_, err = global.DB.NewUpdate().Model(newUserInfo).
|
||||
WherePK().
|
||||
Column("name", "abbr", "region", "address", "contact", "phone", "unit_service_fee").
|
||||
Exec(c.Context())
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
cache.AbolishRelation(fmt.Sprintf("user:%s", targetUserId))
|
||||
return result.Updated("指定用户的信息已经更新。")
|
||||
}
|
||||
|
||||
func quickSearchEnterprise(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
keyword := c.Query("keyword")
|
||||
searchResult, err := service.UserService.SearchLimitUsers(keyword, 6)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Json(http.StatusOK, "已查询到存在符合条件的企业", fiber.Map{"users": searchResult})
|
||||
}
|
||||
|
||||
func fetchExpiration(c *fiber.Ctx) error {
|
||||
func getAccountExpiration(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
return result.Unauthorized(err.Error())
|
||||
userLog.Error("未找到有效的用户会话。", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
user, err := service.UserService.FetchUserDetail(session.Uid)
|
||||
userDetail, err := repository.UserRepository.FindUserDetailById(session.Uid)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
return result.NotFound("未找到指定的用户档案")
|
||||
}
|
||||
return result.Json(
|
||||
http.StatusOK,
|
||||
return result.Success(
|
||||
"已经取得用户的服务期限信息",
|
||||
fiber.Map{"expiration": user.ServiceExpiration.Format("2006-01-02")},
|
||||
fiber.Map{"expiration": userDetail.ServiceExpiration.Format("2006-01-02")},
|
||||
)
|
||||
}
|
||||
|
||||
func createOPSAccount(c *fiber.Ctx) error {
|
||||
userLog.Info("请求创建运维或监管账户。")
|
||||
result := response.NewResult(c)
|
||||
creationForm := new(vo.MGTAndOPSAccountCreationForm)
|
||||
if err := c.BodyParser(creationForm); err != nil {
|
||||
userLog.Error("表单解析失败!", zap.Error(err))
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
exists, err := repository.UserRepository.IsUsernameExists(creationForm.Username)
|
||||
if err != nil {
|
||||
userLog.Error("检查用户名是否已经被使用时发生错误!", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if exists {
|
||||
return result.Conflict("指定的用户名已经被使用了。")
|
||||
}
|
||||
verifyCode, err := service.UserService.CreateUserAccount(creationForm.IntoUser(), creationForm.IntoUserDetail())
|
||||
if err != nil {
|
||||
userLog.Error("创建用户账户时发生错误!", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Created("用户已经成功创建。", fiber.Map{"verify": verifyCode})
|
||||
}
|
||||
|
||||
func fetchUserInformation(c *fiber.Ctx) error {
|
||||
userLog.Info("请求获取用户详细信息。")
|
||||
result := response.NewResult(c)
|
||||
targetUserId := c.Params("uid")
|
||||
exists, err := repository.UserRepository.IsUserExists(targetUserId)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if !exists {
|
||||
return result.NotFound("指定的用户不存在。")
|
||||
}
|
||||
userDetail, err := repository.UserRepository.FindUserDetailById(targetUserId)
|
||||
if err != nil {
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Success("用户详细信息已获取到。", fiber.Map{"user": userDetail})
|
||||
}
|
||||
|
||||
func modifyUserInformation(c *fiber.Ctx) error {
|
||||
userLog.Info("请求修改用户详细信息。")
|
||||
session, _ := _retreiveSession(c)
|
||||
result := response.NewResult(c)
|
||||
targetUserId := c.Params("uid")
|
||||
exists, err := repository.UserRepository.IsUserExists(targetUserId)
|
||||
if err != nil {
|
||||
userLog.Error("检查用户是否存在时发生错误!", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if !exists {
|
||||
return result.NotFound("指定的用户不存在。")
|
||||
}
|
||||
modificationForm := new(vo.UserDetailModificationForm)
|
||||
if err := c.BodyParser(modificationForm); err != nil {
|
||||
userLog.Error("表单解析失败!", zap.Error(err))
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
userLog.Debug("用户服务费修改表单:", zap.Any("form", modificationForm))
|
||||
detailFormForUpdate, err := modificationForm.IntoModificationModel()
|
||||
if err != nil {
|
||||
userLog.Error("用户服务费解析转换失败!", zap.Error(err))
|
||||
return result.UnableToParse("无法解析提交的数据,服务费格式不正确。")
|
||||
}
|
||||
if ok, err := repository.UserRepository.UpdateDetail(targetUserId, *detailFormForUpdate, &session.Uid); err != nil || !ok {
|
||||
userLog.Error("更新用户详细信息失败!", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Updated("指定用户的信息已经更新。")
|
||||
}
|
||||
|
||||
func changeUserState(c *fiber.Ctx) error {
|
||||
userLog.Info("请求修改用户状态。")
|
||||
result := response.NewResult(c)
|
||||
modificationForm := new(vo.UserStateChangeForm)
|
||||
if err := c.BodyParser(modificationForm); err != nil {
|
||||
userLog.Error("表单解析失败!", zap.Error(err))
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
exists, err := repository.UserRepository.IsUserExists(modificationForm.Uid)
|
||||
if err != nil {
|
||||
userLog.Error("检查用户是否存在时发生错误!", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if !exists {
|
||||
return result.NotFound("指定的用户不存在。")
|
||||
}
|
||||
if ok, err := repository.UserRepository.ChangeState(modificationForm.Uid, modificationForm.Enabled); err != nil || !ok {
|
||||
userLog.Error("更新用户状态失败!", zap.Error(err))
|
||||
return result.NotAccept("无法更新用户状态。")
|
||||
}
|
||||
return result.Updated("用户的状态已经更新。")
|
||||
}
|
||||
|
||||
func createEnterpriseAccount(c *fiber.Ctx) error {
|
||||
userLog.Info("请求创建企业账户。")
|
||||
result := response.NewResult(c)
|
||||
creationForm := new(vo.EnterpriseAccountCreationForm)
|
||||
if err := c.BodyParser(creationForm); err != nil {
|
||||
userLog.Error("表单解析失败!", zap.Error(err))
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
exists, err := repository.UserRepository.IsUsernameExists(creationForm.Username)
|
||||
if err != nil {
|
||||
userLog.Error("检查用户名是否已经被使用时发生错误!", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if exists {
|
||||
return result.Conflict("指定的用户名已经被使用了。")
|
||||
}
|
||||
userDetail, err := creationForm.IntoUserDetail()
|
||||
if err != nil {
|
||||
userLog.Error("转换用户详细档案时发生错误!", zap.Error(err))
|
||||
return result.UnableToParse("无法解析提交的数据,服务费格式不正确。")
|
||||
}
|
||||
verifyCode, err := service.UserService.CreateUserAccount(creationForm.IntoUser(), userDetail)
|
||||
if err != nil {
|
||||
userLog.Error("创建用户账户时发生错误!", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Created("用户已经成功创建。", fiber.Map{"verify": verifyCode})
|
||||
}
|
||||
|
||||
func quickSearchEnterprise(c *fiber.Ctx) error {
|
||||
userLog.Info("请求快速查询企业账户。")
|
||||
result := response.NewResult(c)
|
||||
keyword := c.Query("keyword")
|
||||
limit := c.QueryInt("limit", 6)
|
||||
if limit < 1 {
|
||||
limit = 6
|
||||
}
|
||||
users, err := repository.UserRepository.SearchUsersWithLimit(nil, &keyword, uint(limit))
|
||||
if err != nil {
|
||||
userLog.Error("查询用户时发生错误!", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return result.Success("已查询到存在符合条件的企业", fiber.Map{"users": users})
|
||||
}
|
||||
|
||||
func resetUserPassword(c *fiber.Ctx) error {
|
||||
userLog.Info("请求重置用户密码。")
|
||||
result := response.NewResult(c)
|
||||
repasswordForm := new(vo.RepasswordForm)
|
||||
if err := c.BodyParser(repasswordForm); err != nil {
|
||||
userLog.Error("表单解析失败!", zap.Error(err))
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
user, err := repository.UserRepository.FindUserByUsername(repasswordForm.Username)
|
||||
if err != nil {
|
||||
userLog.Error("检查用户是否存在时发生错误!", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if user == nil {
|
||||
return result.NotFound("指定的用户不存在。")
|
||||
}
|
||||
if !service.UserService.MatchUserPassword(user.Password, repasswordForm.VerifyCode) {
|
||||
return result.Unauthorized("验证码不正确。")
|
||||
}
|
||||
ok, err := repository.UserRepository.UpdatePassword(user.Id, repasswordForm.NewPassword, false)
|
||||
if err != nil {
|
||||
userLog.Error("更新用户凭据时发生错误!", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if !ok {
|
||||
return result.NotAccept("无法更新用户凭据。")
|
||||
}
|
||||
return result.Updated("用户凭据已经更新。")
|
||||
}
|
||||
|
||||
func invalidUserPassword(c *fiber.Ctx) error {
|
||||
userLog.Info("请求使用户凭据失效。")
|
||||
result := response.NewResult(c)
|
||||
uid := c.Params("uid")
|
||||
exists, err := repository.UserRepository.IsUserExists(uid)
|
||||
if err != nil {
|
||||
userLog.Error("检查用户是否存在时发生错误!", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if !exists {
|
||||
return result.NotFound("指定的用户不存在。")
|
||||
}
|
||||
verifyCode := tools.RandStr(10)
|
||||
ok, err := repository.UserRepository.UpdatePassword(uid, verifyCode, true)
|
||||
if err != nil {
|
||||
userLog.Error("更新用户凭据时发生错误!", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if !ok {
|
||||
return result.NotAccept("未能更新用户凭据。")
|
||||
}
|
||||
return result.Updated("用户凭据已经更新。", fiber.Map{"verify": verifyCode})
|
||||
}
|
||||
|
||||
@@ -1,81 +1,135 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/exceptions"
|
||||
"electricity_bill_calc/logger"
|
||||
|
||||
"electricity_bill_calc/repository"
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/security"
|
||||
"electricity_bill_calc/service"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"electricity_bill_calc/vo"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func InitializeWithdrawController(router *fiber.App) {
|
||||
router.Delete("/publicity/:pid", security.EnterpriseAuthorize, applyReportWithdraw)
|
||||
router.Get("/withdraws", security.OPSAuthorize, fetchWithdrawsWaitingAutdit)
|
||||
router.Put("/withdraw/:rid", security.OPSAuthorize, auditWithdraw)
|
||||
var withdrawLog = logger.Named("Handler", "Withdraw")
|
||||
|
||||
func InitializeWithdrawHandlers(router *fiber.App) {
|
||||
router.Get("/withdraw", security.OPSAuthorize, withdraw)
|
||||
router.Put("/withdraw/:rid", security.OPSAuthorize, reviewRequestWithdraw)
|
||||
router.Delete("/withdraw/:rid", security.EnterpriseAuthorize, recallReport)
|
||||
}
|
||||
|
||||
func applyReportWithdraw(c *fiber.Ctx) error {
|
||||
// 用于分页检索用户的核算报表
|
||||
func withdraw(c *fiber.Ctx) error {
|
||||
//记录日志
|
||||
withdrawLog.Info("带分页的待审核的核算撤回申请列表")
|
||||
//获取请求参数
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("pid")
|
||||
if ensure, err := ensureReportBelongs(c, &result, requestReportId); !ensure {
|
||||
return err
|
||||
}
|
||||
deleted, err := service.WithdrawService.ApplyWithdraw(requestReportId)
|
||||
keyword := c.Query("keyword", "")
|
||||
page := c.QueryInt("page", 1)
|
||||
withdrawLog.Info("参数为: ", zap.String("keyword", keyword), zap.Int("page", page))
|
||||
//中间数据库操作暂且省略。。。。
|
||||
//首先进行核算报表的分页查询
|
||||
withdraws, total, err := repository.WithdrawRepository.FindWithdraw(uint(page), &keyword)
|
||||
if err != nil {
|
||||
if nfErr, ok := err.(exceptions.NotFoundError); ok {
|
||||
return result.NotFound(nfErr.Error())
|
||||
} else if ioErr, ok := err.(exceptions.ImproperOperateError); ok {
|
||||
return result.NotAccept(ioErr.Error())
|
||||
} else {
|
||||
withdrawLog.Error("检索用户核算报表失败。", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
}
|
||||
if !deleted {
|
||||
return result.Error(http.StatusInternalServerError, "未能完成公示报表的申请撤回操作。")
|
||||
}
|
||||
return result.Success("指定的公示报表已经申请撤回。")
|
||||
}
|
||||
|
||||
func fetchWithdrawsWaitingAutdit(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
keyword := c.Query("keyword")
|
||||
requestPage, err := strconv.Atoi(c.Query("page", "1"))
|
||||
if err != nil {
|
||||
return result.NotAccept("查询参数[page]格式不正确。")
|
||||
}
|
||||
reports, totalitems, err := service.WithdrawService.FetchPagedWithdrawApplies(requestPage, keyword)
|
||||
if err != nil {
|
||||
return result.NotFound(err.Error())
|
||||
}
|
||||
return result.Json(
|
||||
http.StatusOK,
|
||||
"已经取得符合条件的等待审核的撤回申请。",
|
||||
response.NewPagedResponse(requestPage, totalitems).ToMap(),
|
||||
fiber.Map{"records": reports},
|
||||
//TODO: 2023-07-18 此处返回值是个示例,具体返回值需要查询数据库(完成)
|
||||
return result.Success(
|
||||
"withdraw请求成功",
|
||||
response.NewPagedResponse(page, total).ToMap(),
|
||||
fiber.Map{"records": withdraws},
|
||||
)
|
||||
}
|
||||
|
||||
type WithdrawAuditFormData struct {
|
||||
Audit bool `json:"audit" form:"audit"`
|
||||
// 用于审核撤回报表
|
||||
func reviewRequestWithdraw(c *fiber.Ctx) error {
|
||||
Rid := c.Params("rid", "")
|
||||
Data := new(vo.ReviewWithdraw)
|
||||
result := response.NewResult(c)
|
||||
|
||||
if err := c.BodyParser(&Data); err != nil {
|
||||
withdrawLog.Error("无法解析审核指定报表的请求数据", zap.Error(err))
|
||||
return result.BadRequest("无法解析审核指定报表的请求数据。")
|
||||
}
|
||||
|
||||
func auditWithdraw(c *fiber.Ctx) error {
|
||||
result := response.NewResult(c)
|
||||
requestReportId := c.Params("rid")
|
||||
formData := new(WithdrawAuditFormData)
|
||||
if err := c.BodyParser(formData); err != nil {
|
||||
return result.UnableToParse("无法解析提交的数据。")
|
||||
}
|
||||
err := service.WithdrawService.AuditWithdraw(requestReportId, formData.Audit)
|
||||
if Data.Audit == true { //审核通过
|
||||
ok, err := repository.WithdrawRepository.ReviewTrueReportWithdraw(Rid)
|
||||
if err != nil {
|
||||
if nfErr, ok := err.(exceptions.NotFoundError); ok {
|
||||
return result.NotFound(nfErr.Error())
|
||||
withdrawLog.Error("审核同意撤回报表失败")
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
if !ok {
|
||||
withdrawLog.Error("审核同意撤回报表失败")
|
||||
return result.NotAccept("审核同意撤回报表失败")
|
||||
} else {
|
||||
return result.NotAccept(err.Error())
|
||||
return result.Success("审核同意撤回报表成功!")
|
||||
}
|
||||
} else { //审核不通过
|
||||
ok, err := repository.WithdrawRepository.ReviewFalseReportWithdraw(Rid)
|
||||
if err != nil {
|
||||
withdrawLog.Error("审核拒绝撤回报表失败")
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
if !ok {
|
||||
withdrawLog.Error("审核拒绝撤回报表失败")
|
||||
return result.NotAccept("审核拒绝撤回报表失败")
|
||||
} else {
|
||||
return result.Success("审核拒绝撤回报表成功!")
|
||||
}
|
||||
}
|
||||
return result.Success("指定公示报表的撤回申请已经完成审核")
|
||||
|
||||
}
|
||||
|
||||
// 用于撤回电费核算
|
||||
func recallReport(c *fiber.Ctx) error {
|
||||
// 获取用户会话信息和参数
|
||||
rid := c.Params("rid", "")
|
||||
result := response.NewResult(c)
|
||||
session, err := _retreiveSession(c)
|
||||
if err != nil {
|
||||
withdrawLog.Error("无法获取当前用户的会话。")
|
||||
return result.Unauthorized(err.Error())
|
||||
}
|
||||
// 检查指定报表的所属情况
|
||||
isBelongsTo, err := repository.ReportRepository.IsBelongsTo(rid, session.Uid)
|
||||
if err != nil {
|
||||
withdrawLog.Error("检查报表所属情况出现错误。", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
if err == nil && isBelongsTo == true {
|
||||
// 判断指定报表是否是当前园区的最后一张报表
|
||||
isLastReport, err := repository.ReportRepository.IsLastReport(rid)
|
||||
if err != nil {
|
||||
withdrawLog.Error("判断指定报表是否为当前园区的最后一张报表时出错", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
if err == nil && isLastReport == true {
|
||||
// 申请撤回指定的核算报表
|
||||
//TODO: 2023.07.25 申请撤回指定核算报表,正确状态未处理(完成)
|
||||
ok, err := repository.ReportRepository.ApplyWithdrawReport(rid)
|
||||
if err != nil {
|
||||
withdrawLog.Error("申请撤回指定核算报表出错", zap.Error(err))
|
||||
return result.Error(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
if ok {
|
||||
withdrawLog.Info("申请撤回指定核算报表成功")
|
||||
return result.Success("申请撤回指定核算报表成功")
|
||||
}
|
||||
} else {
|
||||
withdrawLog.Info("当前报表不是当前园区的最后一张报表")
|
||||
return result.Error(http.StatusForbidden, "当前报表不是当前园区的最后一张报表")
|
||||
}
|
||||
} else {
|
||||
withdrawLog.Info("指定的核算报表不属于当前用户。")
|
||||
return result.Error(http.StatusForbidden, "指定的核算报表不属于当前用户")
|
||||
}
|
||||
|
||||
return result.Error(http.StatusInternalServerError, "其他错误")
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package excel
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/tools"
|
||||
"electricity_bill_calc/types"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -19,11 +19,10 @@ import (
|
||||
type ExcelTemplateGenerator interface {
|
||||
Close()
|
||||
WriteTo(w io.Writer) (int64, error)
|
||||
WriteMeterData(meters []model.EndUserDetail) error
|
||||
}
|
||||
|
||||
type ColumnRecognizer struct {
|
||||
Pattern []string
|
||||
Pattern [][]string
|
||||
Tag string
|
||||
MatchIndex int
|
||||
MustFill bool
|
||||
@@ -45,7 +44,7 @@ type ExcelAnalysisError struct {
|
||||
Err AnalysisError `json:"error"`
|
||||
}
|
||||
|
||||
func NewColumnRecognizer(tag string, patterns ...string) ColumnRecognizer {
|
||||
func NewColumnRecognizer(tag string, patterns ...[]string) ColumnRecognizer {
|
||||
return ColumnRecognizer{
|
||||
Pattern: patterns,
|
||||
Tag: tag,
|
||||
@@ -67,9 +66,17 @@ func (e ExcelAnalysisError) Error() string {
|
||||
|
||||
func (r *ColumnRecognizer) Recognize(cellValue string) bool {
|
||||
matches := make([]bool, 0)
|
||||
for _, p := range r.Pattern {
|
||||
matches = append(matches, strings.Contains(cellValue, p))
|
||||
for _, pG := range r.Pattern {
|
||||
groupMatch := make([]bool, 0)
|
||||
for _, p := range pG {
|
||||
groupMatch = append(groupMatch, strings.Contains(cellValue, p))
|
||||
}
|
||||
// 这句表示在每一个匹配组中,只要有一个匹配项,就算匹配成功
|
||||
matches = append(matches, lo.Reduce(groupMatch, func(acc, elem bool, index int) bool {
|
||||
return acc || elem
|
||||
}, false))
|
||||
}
|
||||
// 这句表示在尊有的匹配组中,必须全部的匹配组都完成匹配,才算匹配成功
|
||||
return lo.Reduce(matches, func(acc, elem bool, index int) bool {
|
||||
return acc && elem
|
||||
}, true)
|
||||
@@ -189,6 +196,54 @@ func (a *ExcelAnalyzer[T]) Analysis(bean T) ([]T, []ExcelAnalysisError) {
|
||||
} else {
|
||||
actualField.SetBool(false)
|
||||
}
|
||||
case "types.Date":
|
||||
if len(matchValue) == 0 {
|
||||
actualField.Set(reflect.ValueOf(types.NewEmptyDate()))
|
||||
} else {
|
||||
v, err := types.ParseDate(matchValue)
|
||||
if err != nil {
|
||||
errs = append(errs, ExcelAnalysisError{Row: rowIndex + 1, Col: recognizer.MatchIndex + 1, Err: AnalysisError{Err: fmt.Errorf("单元格内容应为日期格式。%w", err)}})
|
||||
actualField.Set(reflect.ValueOf(types.NewEmptyDate()))
|
||||
} else {
|
||||
actualField.Set(reflect.ValueOf(v))
|
||||
}
|
||||
}
|
||||
case "*types.Date":
|
||||
if len(matchValue) == 0 {
|
||||
actualField.Set(reflect.ValueOf(nil))
|
||||
} else {
|
||||
v, err := types.ParseDate(matchValue)
|
||||
if err != nil {
|
||||
errs = append(errs, ExcelAnalysisError{Row: rowIndex + 1, Col: recognizer.MatchIndex + 1, Err: AnalysisError{Err: fmt.Errorf("单元格内容应为日期格式。%w", err)}})
|
||||
actualField.Set(reflect.ValueOf(nil))
|
||||
} else {
|
||||
actualField.Set(reflect.ValueOf(&v))
|
||||
}
|
||||
}
|
||||
case "types.DateTime":
|
||||
if len(matchValue) == 0 {
|
||||
actualField.Set(reflect.ValueOf(types.NewEmptyDateTime()))
|
||||
} else {
|
||||
v, err := types.ParseDateTime(matchValue)
|
||||
if err != nil {
|
||||
errs = append(errs, ExcelAnalysisError{Row: rowIndex + 1, Col: recognizer.MatchIndex + 1, Err: AnalysisError{Err: fmt.Errorf("单元格内容应为日期时间格式。%w", err)}})
|
||||
actualField.Set(reflect.ValueOf(types.NewEmptyDateTime()))
|
||||
} else {
|
||||
actualField.Set(reflect.ValueOf(v))
|
||||
}
|
||||
}
|
||||
case "*types.DateTime":
|
||||
if len(matchValue) == 0 {
|
||||
actualField.Set(reflect.ValueOf(nil))
|
||||
} else {
|
||||
v, err := types.ParseDateTime(matchValue)
|
||||
if err != nil {
|
||||
errs = append(errs, ExcelAnalysisError{Row: rowIndex + 1, Col: recognizer.MatchIndex + 1, Err: AnalysisError{Err: fmt.Errorf("单元格内容应为日期时间格式。%w", err)}})
|
||||
actualField.Set(reflect.ValueOf(nil))
|
||||
} else {
|
||||
actualField.Set(reflect.ValueOf(&v))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
package excel
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/model"
|
||||
"io"
|
||||
)
|
||||
|
||||
var (
|
||||
endUserNonPVRecognizers = []*ColumnRecognizer{
|
||||
{Pattern: []string{"电表编号"}, Tag: "meterId", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: []string{"上期", "(总)"}, Tag: "lastPeriodOverall", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: []string{"本期", "(总)"}, Tag: "currentPeriodOverall", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: []string{"退补", "(总)"}, Tag: "adjustOverall", MatchIndex: -1, MustFill: true},
|
||||
}
|
||||
endUserPVRecognizers = append(
|
||||
endUserNonPVRecognizers,
|
||||
&ColumnRecognizer{Pattern: []string{"上期", "(尖峰)"}, Tag: "lastPeriodCritical", MatchIndex: -1},
|
||||
&ColumnRecognizer{Pattern: []string{"上期", "(峰)"}, Tag: "lastPeriodPeak", MatchIndex: -1},
|
||||
&ColumnRecognizer{Pattern: []string{"上期", "(谷)"}, Tag: "lastPeriodValley", MatchIndex: -1},
|
||||
&ColumnRecognizer{Pattern: []string{"本期", "(尖峰)"}, Tag: "currentPeriodCritical", MatchIndex: -1},
|
||||
&ColumnRecognizer{Pattern: []string{"本期", "(峰)"}, Tag: "currentPeriodPeak", MatchIndex: -1},
|
||||
&ColumnRecognizer{Pattern: []string{"本期", "(谷)"}, Tag: "currentPeriodValley", MatchIndex: -1},
|
||||
&ColumnRecognizer{Pattern: []string{"退补", "(尖峰)"}, Tag: "adjustCritical", MatchIndex: -1},
|
||||
&ColumnRecognizer{Pattern: []string{"退补", "(峰)"}, Tag: "adjustPeak", MatchIndex: -1},
|
||||
&ColumnRecognizer{Pattern: []string{"退补", "(谷)"}, Tag: "adjustValley", MatchIndex: -1},
|
||||
)
|
||||
)
|
||||
|
||||
func NewEndUserNonPVExcelAnalyzer(file io.Reader) (*ExcelAnalyzer[model.EndUserImport], error) {
|
||||
return NewExcelAnalyzer[model.EndUserImport](file, endUserNonPVRecognizers)
|
||||
}
|
||||
|
||||
func NewEndUserPVExcelAnalyzer(file io.Reader) (*ExcelAnalyzer[model.EndUserImport], error) {
|
||||
return NewExcelAnalyzer[model.EndUserImport](file, endUserPVRecognizers)
|
||||
}
|
||||
@@ -1,21 +1,139 @@
|
||||
package excel
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/model"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/samber/lo"
|
||||
"github.com/xuri/excelize/v2"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var meter04kVExcelRecognizers = []*ColumnRecognizer{
|
||||
{Pattern: []string{"表号"}, Tag: "code", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: []string{"户名"}, Tag: "name", MatchIndex: -1},
|
||||
{Pattern: []string{"户址"}, Tag: "address", MatchIndex: -1},
|
||||
{Pattern: []string{"联系人"}, Tag: "contact", MatchIndex: -1},
|
||||
{Pattern: []string{"电话"}, Tag: "phone", MatchIndex: -1},
|
||||
{Pattern: []string{"倍率"}, Tag: "ratio", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: []string{"序号"}, Tag: "seq", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: []string{"公用设备"}, Tag: "public", MatchIndex: -1, MustFill: true},
|
||||
var meterArchiveRecognizers = []*ColumnRecognizer{
|
||||
{Pattern: [][]string{{"表号"}}, Tag: "code", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: [][]string{{"表址", "地址", "户址"}}, Tag: "address", MatchIndex: -1},
|
||||
{Pattern: [][]string{{"类型"}}, Tag: "meterType", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: [][]string{{"建筑"}}, Tag: "building", MatchIndex: -1},
|
||||
{Pattern: [][]string{{"楼层"}}, Tag: "onFloor", MatchIndex: -1},
|
||||
{Pattern: [][]string{{"面积"}}, Tag: "area", MatchIndex: -1},
|
||||
{Pattern: [][]string{{"倍率"}}, Tag: "ratio", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: [][]string{{"序号"}}, Tag: "seq", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: [][]string{{"抄表"}, {"时间", "日期"}}, Tag: "readAt", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: [][]string{{"有功", "表底", "底数"}, {"总"}}, Tag: "overall", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: [][]string{{"有功", "表底", "底数"}, {"尖"}}, Tag: "critical", MatchIndex: -1},
|
||||
{Pattern: [][]string{{"有功", "表底", "底数"}, {"峰"}}, Tag: "peak", MatchIndex: -1},
|
||||
{Pattern: [][]string{{"有功", "表底", "底数"}, {"平"}}, Tag: "flat", MatchIndex: -1},
|
||||
{Pattern: [][]string{{"有功", "表底", "底数"}, {"谷"}}, Tag: "valley", MatchIndex: -1},
|
||||
}
|
||||
|
||||
func NewMeterArchiveExcelAnalyzer(file io.Reader) (*ExcelAnalyzer[model.Meter04KV], error) {
|
||||
return NewExcelAnalyzer[model.Meter04KV](file, meter04kVExcelRecognizers)
|
||||
func NewMeterArchiveExcelAnalyzer(file io.Reader) (*ExcelAnalyzer[model.MeterImportRow], error) {
|
||||
return NewExcelAnalyzer[model.MeterImportRow](file, meterArchiveRecognizers)
|
||||
}
|
||||
|
||||
type MeterArchiveExcelTemplateGenerator struct {
|
||||
file *excelize.File
|
||||
log *zap.Logger
|
||||
}
|
||||
|
||||
func NewMeterArchiveExcelTemplateGenerator() *MeterArchiveExcelTemplateGenerator {
|
||||
return &MeterArchiveExcelTemplateGenerator{
|
||||
file: excelize.NewFile(),
|
||||
log: logger.Named("Excel", "MeterArchive"),
|
||||
}
|
||||
}
|
||||
|
||||
func (MeterArchiveExcelTemplateGenerator) titles() *[]interface{} {
|
||||
return &[]interface{}{
|
||||
"序号",
|
||||
"表址",
|
||||
"表号",
|
||||
"表计类型",
|
||||
"倍率",
|
||||
"所在建筑",
|
||||
"所在楼层",
|
||||
"辖盖面积",
|
||||
"抄表时间",
|
||||
"有功(总)",
|
||||
"有功(尖)",
|
||||
"有功(峰)",
|
||||
"有功(平)",
|
||||
"有功(谷)",
|
||||
}
|
||||
}
|
||||
|
||||
func (g *MeterArchiveExcelTemplateGenerator) Close() {
|
||||
g.file.Close()
|
||||
}
|
||||
|
||||
func (g MeterArchiveExcelTemplateGenerator) WriteTo(w io.Writer) (int64, error) {
|
||||
return g.file.WriteTo(w)
|
||||
}
|
||||
|
||||
func (g *MeterArchiveExcelTemplateGenerator) WriteTemplateData(buildings []*model.ParkBuilding) error {
|
||||
var err error
|
||||
defaultSheet := g.file.GetSheetName(0)
|
||||
g.log.Debug("选定默认输出表格", zap.String("sheet", defaultSheet))
|
||||
err = g.file.SetColWidth(defaultSheet, "B", "I", 20)
|
||||
if err != nil {
|
||||
g.log.Error("未能设定长型列宽。", zap.Error(err))
|
||||
return fmt.Errorf("未能设定长型列宽,%w", err)
|
||||
}
|
||||
err = g.file.SetColWidth(defaultSheet, "J", "N", 15)
|
||||
if err != nil {
|
||||
g.log.Error("未能设定短型列宽。", zap.Error(err))
|
||||
return fmt.Errorf("未能设定短型列宽,%w", err)
|
||||
}
|
||||
err = g.file.SetSheetRow(defaultSheet, "A1", g.titles())
|
||||
if err != nil {
|
||||
g.log.Error("未能输出模板标题。", zap.Error(err))
|
||||
return fmt.Errorf("未能输出模板标题,%w", err)
|
||||
}
|
||||
err = g.file.SetRowHeight(defaultSheet, 1, 20)
|
||||
if err != nil {
|
||||
g.log.Error("未能设定标题行高度。", zap.Error(err))
|
||||
return fmt.Errorf("未能设定标题行高度,%w", err)
|
||||
}
|
||||
|
||||
dateTimeExp := "yyyy-mm-dd hh:mm"
|
||||
dateTimeColStyle, err := g.file.NewStyle(&excelize.Style{
|
||||
CustomNumFmt: &dateTimeExp,
|
||||
})
|
||||
if err != nil {
|
||||
g.log.Error("未能创建日期时间格式。", zap.Error(err))
|
||||
return fmt.Errorf("未能创建日期时间格式,%w", err)
|
||||
}
|
||||
g.file.SetCellStyle(defaultSheet, "I2", "I1048576", dateTimeColStyle)
|
||||
|
||||
numExp := "0.0000"
|
||||
numColStyle, err := g.file.NewStyle(&excelize.Style{
|
||||
CustomNumFmt: &numExp,
|
||||
})
|
||||
if err != nil {
|
||||
g.log.Error("未能创建抄表数字格式。", zap.Error(err))
|
||||
return fmt.Errorf("未能创建抄表数字格式,%w", err)
|
||||
}
|
||||
g.file.SetCellStyle(defaultSheet, "J2", "N1048576", numColStyle)
|
||||
|
||||
meterInstallationTypeValidation := excelize.NewDataValidation(false)
|
||||
meterInstallationTypeValidation.SetDropList([]string{"商户表", "公共表", "楼道表"})
|
||||
meterInstallationTypeValidation.Sqref = "D2:D1048576"
|
||||
err = g.file.AddDataValidation(defaultSheet, meterInstallationTypeValidation)
|
||||
if err != nil {
|
||||
g.log.Error("未能设定表计类型选择器。", zap.Error(err))
|
||||
return fmt.Errorf("未能设定表计类型选择器,%w", err)
|
||||
}
|
||||
buildingValidation := excelize.NewDataValidation(true)
|
||||
buildingNames := lo.Map(buildings, func(b *model.ParkBuilding, _ int) string {
|
||||
return b.Name
|
||||
})
|
||||
buildingValidation.SetDropList(buildingNames)
|
||||
buildingValidation.Sqref = "F2:F1048576"
|
||||
err = g.file.AddDataValidation(defaultSheet, buildingValidation)
|
||||
if err != nil {
|
||||
g.log.Error("未能设定所在建筑选择器。", zap.Error(err))
|
||||
return fmt.Errorf("未能设定所在建筑选择器,%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
package excel
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/model"
|
||||
"io"
|
||||
|
||||
"github.com/xuri/excelize/v2"
|
||||
)
|
||||
|
||||
type MeterNonPVExcelTemplateGenerator struct {
|
||||
file *excelize.File
|
||||
}
|
||||
|
||||
// 生成峰谷计量抄表Excel模板
|
||||
func NewMeterNonPVExcelTemplateGenerator() *MeterNonPVExcelTemplateGenerator {
|
||||
return &MeterNonPVExcelTemplateGenerator{
|
||||
file: excelize.NewFile(),
|
||||
}
|
||||
}
|
||||
|
||||
func (MeterNonPVExcelTemplateGenerator) titles() []interface{} {
|
||||
return []interface{}{
|
||||
"序号",
|
||||
"用户名称",
|
||||
"户址",
|
||||
"电表编号",
|
||||
"倍率",
|
||||
"上期表底(总)",
|
||||
"本期表底(总)",
|
||||
"退补电量(总)",
|
||||
}
|
||||
}
|
||||
|
||||
func (t *MeterNonPVExcelTemplateGenerator) Close() {
|
||||
t.file.Close()
|
||||
}
|
||||
|
||||
func (t MeterNonPVExcelTemplateGenerator) WriteTo(w io.Writer) (int64, error) {
|
||||
return t.file.WriteTo(w)
|
||||
}
|
||||
|
||||
func (t *MeterNonPVExcelTemplateGenerator) WriteMeterData(meters []model.EndUserDetail) error {
|
||||
defaultSheet := t.file.GetSheetName(0)
|
||||
stream, err := t.file.NewStreamWriter(defaultSheet)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
firstCell, err := excelize.CoordinatesToCellName(1, 1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stream.SetColWidth(2, 4, 20)
|
||||
stream.SetColWidth(6, 8, 15)
|
||||
stream.SetRow(firstCell, t.titles(), excelize.RowOpts{Height: 20})
|
||||
|
||||
for index, meter := range meters {
|
||||
firstCell, err := excelize.CoordinatesToCellName(1, index+2)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
customerName := ""
|
||||
if meter.CustomerName != nil {
|
||||
customerName = *meter.CustomerName
|
||||
}
|
||||
customerAddress := ""
|
||||
if meter.Address != nil {
|
||||
customerAddress = *meter.Address
|
||||
}
|
||||
if err = stream.SetRow(
|
||||
firstCell,
|
||||
[]interface{}{
|
||||
meter.Seq,
|
||||
customerName,
|
||||
customerAddress,
|
||||
meter.MeterId,
|
||||
meter.Ratio,
|
||||
meter.LastPeriodOverall,
|
||||
meter.CurrentPeriodOverall,
|
||||
meter.AdjustOverall,
|
||||
},
|
||||
excelize.RowOpts{Height: 15},
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err = stream.Flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
package excel
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/model"
|
||||
"io"
|
||||
|
||||
"github.com/xuri/excelize/v2"
|
||||
)
|
||||
|
||||
type MeterPVExcelTemplateGenerator struct {
|
||||
file *excelize.File
|
||||
}
|
||||
|
||||
// 生成峰谷计量抄表Excel模板
|
||||
func NewMeterPVExcelTemplateGenerator() *MeterPVExcelTemplateGenerator {
|
||||
return &MeterPVExcelTemplateGenerator{
|
||||
file: excelize.NewFile(),
|
||||
}
|
||||
}
|
||||
|
||||
func (MeterPVExcelTemplateGenerator) titles() []interface{} {
|
||||
return []interface{}{
|
||||
"序号",
|
||||
"用户名称",
|
||||
"户址",
|
||||
"电表编号",
|
||||
"倍率",
|
||||
"上期表底(总)",
|
||||
"本期表底(总)",
|
||||
"上期表底(尖峰)",
|
||||
"本期表底(尖峰)",
|
||||
"上期表底(峰)",
|
||||
"本期表底(峰)",
|
||||
"上期表底(谷)",
|
||||
"本期表底(谷)",
|
||||
"退补电量(总)",
|
||||
"退补电量(尖峰)",
|
||||
"退补电量(峰)",
|
||||
"退补电量(谷)",
|
||||
}
|
||||
}
|
||||
|
||||
func (t *MeterPVExcelTemplateGenerator) Close() {
|
||||
t.file.Close()
|
||||
}
|
||||
|
||||
func (t MeterPVExcelTemplateGenerator) WriteTo(w io.Writer) (int64, error) {
|
||||
return t.file.WriteTo(w)
|
||||
}
|
||||
|
||||
func (t *MeterPVExcelTemplateGenerator) WriteMeterData(meters []model.EndUserDetail) error {
|
||||
defaultSheet := t.file.GetSheetName(0)
|
||||
stream, err := t.file.NewStreamWriter(defaultSheet)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
firstCell, err := excelize.CoordinatesToCellName(1, 1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stream.SetColWidth(2, 4, 20)
|
||||
stream.SetColWidth(6, 17, 15)
|
||||
stream.SetRow(firstCell, t.titles(), excelize.RowOpts{Height: 20})
|
||||
|
||||
for index, meter := range meters {
|
||||
firstCell, err := excelize.CoordinatesToCellName(1, index+2)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
customerName := ""
|
||||
if meter.CustomerName != nil {
|
||||
customerName = *meter.CustomerName
|
||||
}
|
||||
customerAddress := ""
|
||||
if meter.Address != nil {
|
||||
customerAddress = *meter.Address
|
||||
}
|
||||
if err = stream.SetRow(
|
||||
firstCell,
|
||||
[]interface{}{
|
||||
meter.Seq,
|
||||
customerName,
|
||||
customerAddress,
|
||||
meter.MeterId,
|
||||
meter.Ratio,
|
||||
meter.LastPeriodOverall,
|
||||
meter.CurrentPeriodOverall,
|
||||
meter.LastPeriodCritical,
|
||||
meter.CurrentPeriodCritical,
|
||||
meter.LastPeriodPeak,
|
||||
meter.CurrentPeriodPeak,
|
||||
meter.LastPeriodValley,
|
||||
meter.CurrentPeriodValley,
|
||||
meter.AdjustOverall,
|
||||
meter.AdjustCritical,
|
||||
meter.AdjustPeak,
|
||||
meter.AdjustValley,
|
||||
},
|
||||
excelize.RowOpts{Height: 15},
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err = stream.Flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
131
excel/meter_reading.go
Normal file
131
excel/meter_reading.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package excel
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/tools"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/xuri/excelize/v2"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var meterReadingsRecognizers = []*ColumnRecognizer{
|
||||
{Pattern: [][]string{{"表", "表计"}, {"编号"}}, Tag: "code", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: [][]string{{"抄表", "结束"}, {"时间", "日期"}}, Tag: "readAt", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: [][]string{{"用电", "有功", "表底", "底数"}, {"总", "量"}}, Tag: "overall", MatchIndex: -1, MustFill: true},
|
||||
{Pattern: [][]string{{"有功", "表底", "底数"}, {"尖"}}, Tag: "critical", MatchIndex: -1},
|
||||
{Pattern: [][]string{{"有功", "表底", "底数"}, {"峰"}}, Tag: "peak", MatchIndex: -1},
|
||||
{Pattern: [][]string{{"有功", "表底", "底数"}, {"谷"}}, Tag: "valley", MatchIndex: -1},
|
||||
}
|
||||
|
||||
func NewMeterReadingsExcelAnalyzer(file io.Reader) (*ExcelAnalyzer[model.ReadingImportRow], error) {
|
||||
return NewExcelAnalyzer[model.ReadingImportRow](file, meterReadingsRecognizers)
|
||||
}
|
||||
|
||||
type MeterReadingsExcelTemplateGenerator struct {
|
||||
file *excelize.File
|
||||
log *zap.Logger
|
||||
}
|
||||
|
||||
func NewMeterReadingsExcelTemplateGenerator() *MeterReadingsExcelTemplateGenerator {
|
||||
return &MeterReadingsExcelTemplateGenerator{
|
||||
file: excelize.NewFile(),
|
||||
log: logger.Named("Excel", "MeterReadings"),
|
||||
}
|
||||
}
|
||||
|
||||
func (MeterReadingsExcelTemplateGenerator) titles() *[]interface{} {
|
||||
return &[]interface{}{
|
||||
"抄表序号",
|
||||
"抄表时间",
|
||||
"表计编号",
|
||||
"表计名称",
|
||||
"商户名称",
|
||||
"倍率",
|
||||
"有功(总)",
|
||||
"有功(尖)",
|
||||
"有功(峰)",
|
||||
"有功(谷)",
|
||||
}
|
||||
}
|
||||
|
||||
func (g MeterReadingsExcelTemplateGenerator) Close() {
|
||||
g.file.Close()
|
||||
}
|
||||
|
||||
func (g MeterReadingsExcelTemplateGenerator) WriteTo(w io.Writer) (int64, error) {
|
||||
return g.file.WriteTo(w)
|
||||
}
|
||||
|
||||
func (g MeterReadingsExcelTemplateGenerator) WriteTemplateData(meters []*model.SimpleMeterDocument) error {
|
||||
var err error
|
||||
defaultSheet := g.file.GetSheetName(0)
|
||||
g.log.Debug("选定默认输出表格", zap.String("sheet", defaultSheet))
|
||||
err = g.file.SetColWidth(defaultSheet, "A", "E", 30)
|
||||
if err != nil {
|
||||
g.log.Error("未能设定长型单元格的宽度。", zap.Error(err))
|
||||
return fmt.Errorf("未能设定长型单元格的宽度,%w", err)
|
||||
}
|
||||
err = g.file.SetColWidth(defaultSheet, "F", "F", 10)
|
||||
if err != nil {
|
||||
g.log.Error("未能设定倍率单元格的宽度。", zap.Error(err))
|
||||
return fmt.Errorf("未能设定倍率单元格的宽度,%w", err)
|
||||
}
|
||||
err = g.file.SetColWidth(defaultSheet, "G", "J", 20)
|
||||
if err != nil {
|
||||
g.log.Error("未能设定短型单元格的宽度。", zap.Error(err))
|
||||
return fmt.Errorf("未能设定短型单元格的宽度,%w", err)
|
||||
}
|
||||
err = g.file.SetSheetRow(defaultSheet, "A1", g.titles())
|
||||
if err != nil {
|
||||
g.log.Error("未能输出模板标题。", zap.Error(err))
|
||||
return fmt.Errorf("未能输出模板标题,%w", err)
|
||||
}
|
||||
err = g.file.SetRowHeight(defaultSheet, 1, 30)
|
||||
if err != nil {
|
||||
g.log.Error("未能设定标题行的高度。", zap.Error(err))
|
||||
return fmt.Errorf("未能设定标题行的高度,%w", err)
|
||||
}
|
||||
|
||||
dateTimeExp := "yyyy-mm-dd hh:mm"
|
||||
dateTimeColStyle, err := g.file.NewStyle(&excelize.Style{
|
||||
CustomNumFmt: &dateTimeExp,
|
||||
})
|
||||
if err != nil {
|
||||
g.log.Error("未能创建日期时间格式。", zap.Error(err))
|
||||
return fmt.Errorf("未能创建日期时间格式,%w", err)
|
||||
}
|
||||
endCellCoord, _ := excelize.CoordinatesToCellName(2, len(meters)+1)
|
||||
g.file.SetCellStyle(defaultSheet, "B2", endCellCoord, dateTimeColStyle)
|
||||
|
||||
numExp := "0.0000"
|
||||
numColStyle, err := g.file.NewStyle(&excelize.Style{
|
||||
CustomNumFmt: &numExp,
|
||||
})
|
||||
if err != nil {
|
||||
g.log.Error("未能创建抄表数字格式。", zap.Error(err))
|
||||
return fmt.Errorf("未能创建抄表数字格式,%w", err)
|
||||
}
|
||||
endCellCoord, _ = excelize.CoordinatesToCellName(9, len(meters)+1)
|
||||
g.file.SetCellStyle(defaultSheet, "F2", endCellCoord, numColStyle)
|
||||
|
||||
for i, meter := range meters {
|
||||
cellCoord, _ := excelize.CoordinatesToCellName(1, i+2)
|
||||
ratio, _ := meter.Ratio.Float64()
|
||||
if err := g.file.SetSheetRow(defaultSheet, cellCoord, &[]interface{}{
|
||||
meter.Seq,
|
||||
"",
|
||||
meter.Code,
|
||||
tools.DefaultTo(meter.Address, ""),
|
||||
tools.DefaultTo(meter.TenementName, ""),
|
||||
ratio,
|
||||
}); err != nil {
|
||||
g.log.Error("向模板写入数据出现错误。", zap.Error(err))
|
||||
return fmt.Errorf("向模板写入数据出现错误,%w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
22
excel/tenement.go
Normal file
22
excel/tenement.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package excel
|
||||
|
||||
var tenementRecognizers = []*ColumnRecognizer{
|
||||
{Pattern: [][]string{{"商户全称"}}, Tag: "fullName", MatchIndex: -1},
|
||||
{Pattern: [][]string{{"联系地址"}}, Tag: "address", MatchIndex: -1},
|
||||
{Pattern: [][]string{{"入驻时间"}}, Tag: "movedInAt", MatchIndex: -1},
|
||||
{Pattern: [][]string{{"商铺名称"}}, Tag: "shortName", MatchIndex: -1},
|
||||
{Pattern: [][]string{{"联系人"}}, Tag: "contactName", MatchIndex: -1},
|
||||
{Pattern: [][]string{{"电话"}}, Tag: "contactPhone", MatchIndex: -1},
|
||||
{Pattern: [][]string{{"USCI"}}, Tag: "usci", MatchIndex: -1},
|
||||
{Pattern: [][]string{{"开票地址"}}, Tag: "invoiceAddress", MatchIndex: -1},
|
||||
{Pattern: [][]string{{"账号"}}, Tag: "account", MatchIndex: -1},
|
||||
{Pattern: [][]string{{"开户行"}}, Tag: "bank", MatchIndex: -1},
|
||||
}
|
||||
|
||||
//func NewTenementExcelAnalyzer(file io.Reader) (*ExcelAnalyzer[model.TenementImportRow], error) {
|
||||
// return NewExcelAnalyzer[model.TenementImportRow](file, tenementRecognizers)
|
||||
//}
|
||||
|
||||
//func NewMeterArchiveExcelAnalyzer(file io.Reader) (*ExcelAnalyzer[model.MeterImportRow], error) {
|
||||
// return NewExcelAnalyzer[model.MeterImportRow](file, meterArchiveRecognizers)
|
||||
//}
|
||||
@@ -31,5 +31,5 @@ func NewUnauthorizedError(msg string) *UnauthorizedError {
|
||||
}
|
||||
|
||||
func (e UnauthorizedError) Error() string {
|
||||
return fmt.Sprintf("Unauthorized: %s", e.Message)
|
||||
return fmt.Sprintf("用户未获得授权: %s", e.Message)
|
||||
}
|
||||
|
||||
@@ -15,5 +15,5 @@ func NewIllegalArgumentsError(msg string, arguments ...string) IllegalArgumentsE
|
||||
}
|
||||
|
||||
func (e IllegalArgumentsError) Error() string {
|
||||
return fmt.Sprintf("Illegal Arguments, %s", e.Message)
|
||||
return fmt.Sprintf("使用了非法参数, %s", e.Message)
|
||||
}
|
||||
|
||||
@@ -15,5 +15,5 @@ func NewImproperOperateError(msg string, arguments ...string) ImproperOperateErr
|
||||
}
|
||||
|
||||
func (e ImproperOperateError) Error() string {
|
||||
return fmt.Sprintf("Improper Operate, %s", e.Message)
|
||||
return fmt.Sprintf("操作不恰当, %s", e.Message)
|
||||
}
|
||||
|
||||
16
exceptions/insufficient_data.go
Normal file
16
exceptions/insufficient_data.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package exceptions
|
||||
|
||||
import "fmt"
|
||||
|
||||
type InsufficientDataError struct {
|
||||
Field string
|
||||
Message string
|
||||
}
|
||||
|
||||
func NewInsufficientDataError(field, msg string) *InsufficientDataError {
|
||||
return &InsufficientDataError{Field: field, Message: msg}
|
||||
}
|
||||
|
||||
func (e InsufficientDataError) Error() string {
|
||||
return fmt.Sprintf("字段 [%s] 数据不足,%s", e.Field, e.Message)
|
||||
}
|
||||
@@ -11,7 +11,7 @@ func NewNotFoundError(msg string) *NotFoundError {
|
||||
}
|
||||
|
||||
func NewNotFoundErrorFromError(msg string, err error) *NotFoundError {
|
||||
return &NotFoundError{Message: fmt.Sprintf("%s,%v", msg, err)}
|
||||
return &NotFoundError{Message: fmt.Sprintf("所需数据未找到,%s,%v", msg, err)}
|
||||
}
|
||||
|
||||
func (e NotFoundError) Error() string {
|
||||
|
||||
@@ -1,11 +1,130 @@
|
||||
package exceptions
|
||||
|
||||
type UnsuccessfulOperationError struct{}
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func NewUnsuccessfulOperationError() *UnsuccessfulOperationError {
|
||||
return &UnsuccessfulOperationError{}
|
||||
type OperationType int16
|
||||
|
||||
const (
|
||||
OPERATE_CREATE OperationType = iota
|
||||
OPERATE_UPDATE
|
||||
OPERATE_DELETE
|
||||
OEPRATE_QUERY
|
||||
OPERATE_CALCULATE
|
||||
OPERATE_DB
|
||||
OPERATE_DB_TRANSACTION
|
||||
OPERATE_CUSTOM OperationType = 98
|
||||
OPERATE_OTHER OperationType = 99
|
||||
)
|
||||
|
||||
type UnsuccessfulOperationError struct {
|
||||
Operate OperationType
|
||||
Description string
|
||||
Message string
|
||||
}
|
||||
|
||||
func (UnsuccessfulOperationError) Error() string {
|
||||
return "Unsuccessful Operation"
|
||||
func NewUnsuccessfulOperationError(oeprate OperationType, describe, message string) *UnsuccessfulOperationError {
|
||||
return &UnsuccessfulOperationError{
|
||||
Operate: oeprate,
|
||||
Description: describe,
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
func NewUnsuccessCreateError(message string) *UnsuccessfulOperationError {
|
||||
return &UnsuccessfulOperationError{
|
||||
Operate: OPERATE_CREATE,
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
func NewUnsuccessUpdateError(message string) *UnsuccessfulOperationError {
|
||||
return &UnsuccessfulOperationError{
|
||||
Operate: OPERATE_UPDATE,
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
func NewUnsuccessDeleteError(message string) *UnsuccessfulOperationError {
|
||||
return &UnsuccessfulOperationError{
|
||||
Operate: OPERATE_DELETE,
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
func NewUnsuccessQueryError(message string) *UnsuccessfulOperationError {
|
||||
return &UnsuccessfulOperationError{
|
||||
Operate: OEPRATE_QUERY,
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
func NewUnsuccessCalculateError(message string) *UnsuccessfulOperationError {
|
||||
return &UnsuccessfulOperationError{
|
||||
Operate: OPERATE_CALCULATE,
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
func NewUnsuccessDBError(message string) *UnsuccessfulOperationError {
|
||||
return &UnsuccessfulOperationError{
|
||||
Operate: OPERATE_DB,
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
func NewUnsuccessDBTransactionError(message string) *UnsuccessfulOperationError {
|
||||
return &UnsuccessfulOperationError{
|
||||
Operate: OPERATE_DB_TRANSACTION,
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
func NewUnsuccessCustomError(describe, message string) *UnsuccessfulOperationError {
|
||||
return &UnsuccessfulOperationError{
|
||||
Operate: OPERATE_CUSTOM,
|
||||
Description: describe,
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
func NewUnsuccessOtherError(message string) *UnsuccessfulOperationError {
|
||||
return &UnsuccessfulOperationError{
|
||||
Operate: OPERATE_OTHER,
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
func (e UnsuccessfulOperationError) Error() string {
|
||||
var builder strings.Builder
|
||||
switch e.Operate {
|
||||
case OPERATE_CREATE:
|
||||
builder.WriteString("创建")
|
||||
case OPERATE_UPDATE:
|
||||
builder.WriteString("更新")
|
||||
case OPERATE_DELETE:
|
||||
builder.WriteString("删除")
|
||||
case OEPRATE_QUERY:
|
||||
builder.WriteString("查询")
|
||||
case OPERATE_CALCULATE:
|
||||
builder.WriteString("计算")
|
||||
case OPERATE_DB:
|
||||
builder.WriteString("数据库")
|
||||
case OPERATE_DB_TRANSACTION:
|
||||
builder.WriteString("数据库事务")
|
||||
case OPERATE_CUSTOM:
|
||||
builder.WriteString(e.Description)
|
||||
case OPERATE_OTHER:
|
||||
builder.WriteString("其他")
|
||||
default:
|
||||
builder.WriteString("未知")
|
||||
}
|
||||
builder.WriteString("操作不成功,")
|
||||
if len(e.Message) > 0 {
|
||||
builder.WriteString(e.Message)
|
||||
} else {
|
||||
builder.WriteString("未知原因")
|
||||
}
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
113
global/db.go
113
global/db.go
@@ -1,60 +1,89 @@
|
||||
package global
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"electricity_bill_calc/config"
|
||||
"electricity_bill_calc/logger"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/uptrace/bun/dialect/pgdialect"
|
||||
"github.com/uptrace/bun/driver/pgdriver"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/samber/lo"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
DB *bun.DB
|
||||
DB *pgxpool.Pool
|
||||
)
|
||||
|
||||
func SetupDatabaseConnection() error {
|
||||
// connStr := fmt.Sprintf(
|
||||
// "host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai connect_timeout=0 tcp_user_timeout=180000",
|
||||
// config.DatabaseSettings.Host,
|
||||
// config.DatabaseSettings.User,
|
||||
// config.DatabaseSettings.Pass,
|
||||
// config.DatabaseSettings.DB,
|
||||
// config.DatabaseSettings.Port,
|
||||
// )
|
||||
pgconn := pgdriver.NewConnector(
|
||||
pgdriver.WithNetwork("tcp"),
|
||||
pgdriver.WithAddr(fmt.Sprintf("%s:%d", config.DatabaseSettings.Host,
|
||||
config.DatabaseSettings.Port)),
|
||||
pgdriver.WithUser(config.DatabaseSettings.User),
|
||||
pgdriver.WithInsecure(true),
|
||||
pgdriver.WithPassword(config.DatabaseSettings.Pass),
|
||||
pgdriver.WithDatabase(config.DatabaseSettings.DB),
|
||||
pgdriver.WithDialTimeout(30*time.Second),
|
||||
pgdriver.WithReadTimeout(3*time.Minute),
|
||||
pgdriver.WithWriteTimeout(10*time.Minute),
|
||||
connString := fmt.Sprintf(
|
||||
"postgres://%s:%s@%s:%d/%s?sslmode=disable&connect_timeout=%d&application_name=%s&pool_max_conns=%d&pool_min_conns=%d&pool_max_conn_lifetime=%s&pool_max_conn_idle_time=%s&pool_health_check_period=%s",
|
||||
config.DatabaseSettings.User,
|
||||
config.DatabaseSettings.Pass,
|
||||
config.DatabaseSettings.Host,
|
||||
config.DatabaseSettings.Port,
|
||||
config.DatabaseSettings.DB,
|
||||
0,
|
||||
"elec_service_go",
|
||||
config.DatabaseSettings.MaxOpenConns,
|
||||
config.DatabaseSettings.MaxIdleConns,
|
||||
"60m",
|
||||
"10m",
|
||||
"10s",
|
||||
)
|
||||
sqldb := sql.OpenDB(pgconn)
|
||||
DB = bun.NewDB(sqldb, pgdialect.New())
|
||||
DB.AddQueryHook(logger.NewQueryHook(logger.QueryHookOptions{
|
||||
LogSlow: 3 * time.Second,
|
||||
Logger: logger.Named("PG"),
|
||||
QueryLevel: zapcore.DebugLevel,
|
||||
ErrorLevel: zapcore.ErrorLevel,
|
||||
SlowLevel: zapcore.WarnLevel,
|
||||
ErrorTemplate: "{{.Operation}}[{{.Duration}}]: {{.Query}}: {{.Error}}",
|
||||
MessageTemplate: "{{.Operation}}[{{.Duration}}]: {{.Query}}",
|
||||
}))
|
||||
DB.SetMaxIdleConns(config.DatabaseSettings.MaxIdleConns)
|
||||
DB.SetMaxOpenConns(config.DatabaseSettings.MaxOpenConns)
|
||||
DB.SetConnMaxIdleTime(10 * time.Minute)
|
||||
DB.SetConnMaxLifetime(60 * time.Minute)
|
||||
DB.Ping()
|
||||
poolConfig, err := pgxpool.ParseConfig(connString)
|
||||
if err != nil {
|
||||
logger.Named("DB INIT").Error("数据库连接初始化失败。", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
poolConfig.ConnConfig.Tracer = QueryLogger{logger: logger.Named("PG")}
|
||||
DB, _ = pgxpool.NewWithConfig(context.Background(), poolConfig)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type QueryLogger struct {
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func (ql QueryLogger) TraceQueryStart(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryStartData) context.Context {
|
||||
ql.logger.Info(fmt.Sprintf("将要执行查询: %s", data.SQL))
|
||||
ql.logger.Info("查询参数", lo.Map(data.Args, func(elem any, index int) zap.Field {
|
||||
return zap.Any(fmt.Sprintf("[Arg %d]: ", index), elem)
|
||||
})...)
|
||||
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (ql QueryLogger) TraceQueryEnd(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryEndData) {
|
||||
var logFunc func(string, ...zap.Field)
|
||||
var templateString string
|
||||
if data.Err != nil {
|
||||
logFunc = ql.logger.Error
|
||||
templateString = "命令 [%s] 执行失败。"
|
||||
} else {
|
||||
logFunc = ql.logger.Info
|
||||
templateString = "命令 [%s] 执行成功。"
|
||||
}
|
||||
switch {
|
||||
case data.CommandTag.Update():
|
||||
fallthrough
|
||||
case data.CommandTag.Delete():
|
||||
fallthrough
|
||||
case data.CommandTag.Insert():
|
||||
logFunc(
|
||||
fmt.Sprintf(templateString, data.CommandTag.String()),
|
||||
zap.Error(data.Err),
|
||||
zap.Any("affected", data.CommandTag.RowsAffected()))
|
||||
case data.CommandTag.Select():
|
||||
logFunc(
|
||||
fmt.Sprintf(templateString, data.CommandTag.String()),
|
||||
zap.Error(data.Err))
|
||||
default:
|
||||
logFunc(
|
||||
fmt.Sprintf(templateString, data.CommandTag.String()),
|
||||
zap.Error(data.Err))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,10 +15,13 @@ var (
|
||||
|
||||
func SetupRedisConnection() error {
|
||||
var err error
|
||||
a := fmt.Sprintf("%s:%d", config.RedisSettings.Host, config.RedisSettings.Port)
|
||||
fmt.Println(a)
|
||||
Rd, err = rueidis.NewClient(rueidis.ClientOption{
|
||||
InitAddress: []string{fmt.Sprintf("%s:%d", config.RedisSettings.Host, config.RedisSettings.Port)},
|
||||
InitAddress: []string{a},
|
||||
Password: config.RedisSettings.Password,
|
||||
SelectDB: config.RedisSettings.DB,
|
||||
DisableCache: true,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
85
go.mod
85
go.mod
@@ -3,68 +3,71 @@ module electricity_bill_calc
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/deckarep/golang-set/v2 v2.1.0
|
||||
github.com/fufuok/utils v0.7.13
|
||||
github.com/gofiber/fiber/v2 v2.38.1
|
||||
github.com/fufuok/utils v0.10.2
|
||||
github.com/georgysavva/scany/v2 v2.0.0
|
||||
github.com/gofiber/fiber/v2 v2.46.0
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/jackc/pgx/v5 v5.3.1
|
||||
github.com/jinzhu/copier v0.3.5
|
||||
github.com/liamylian/jsontime/v2 v2.0.0
|
||||
github.com/mozillazg/go-pinyin v0.19.0
|
||||
github.com/rueian/rueidis v0.0.73
|
||||
github.com/samber/lo v1.27.0
|
||||
github.com/mozillazg/go-pinyin v0.20.0
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/rueian/rueidis v0.0.100
|
||||
github.com/samber/lo v1.38.1
|
||||
github.com/shopspring/decimal v1.3.1
|
||||
github.com/spf13/viper v1.12.0
|
||||
github.com/valyala/fasthttp v1.40.0
|
||||
github.com/xuri/excelize/v2 v2.6.1
|
||||
go.uber.org/zap v1.23.0
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0
|
||||
github.com/spf13/viper v1.16.0
|
||||
github.com/valyala/fasthttp v1.47.0
|
||||
github.com/xuri/excelize/v2 v2.7.1
|
||||
go.uber.org/zap v1.24.0
|
||||
golang.org/x/sync v0.2.0
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/andybalholm/brotli v1.0.4 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/klauspost/compress v1.15.0 // indirect
|
||||
github.com/rogpeppe/go-internal v1.8.0 // indirect
|
||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
|
||||
github.com/andybalholm/brotli v1.0.5 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.0 // indirect
|
||||
github.com/klauspost/compress v1.16.6 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.14 // indirect
|
||||
github.com/philhofer/fwd v1.1.2 // indirect
|
||||
github.com/rivo/uniseg v0.4.4 // indirect
|
||||
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 // indirect
|
||||
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
|
||||
github.com/tinylib/msgp v1.1.8 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/valyala/tcplisten v1.0.0 // indirect
|
||||
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
||||
mellium.im/sasl v0.3.0 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/fsnotify/fsnotify v1.5.4 // indirect
|
||||
github.com/doug-martin/goqu/v9 v9.18.0
|
||||
github.com/fsnotify/fsnotify v1.6.0 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/magiconair/properties v1.8.6 // indirect
|
||||
github.com/magiconair/properties v1.8.7 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
|
||||
github.com/pelletier/go-toml v1.9.5 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
|
||||
github.com/richardlehane/mscfb v1.0.4 // indirect
|
||||
github.com/richardlehane/msoleps v1.0.3 // indirect
|
||||
github.com/spf13/afero v1.8.2 // indirect
|
||||
github.com/spf13/cast v1.5.0 // indirect
|
||||
github.com/spf13/afero v1.9.5 // indirect
|
||||
github.com/spf13/cast v1.5.1 // indirect
|
||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/subosito/gotenv v1.3.0 // indirect
|
||||
github.com/uptrace/bun v1.1.8
|
||||
github.com/uptrace/bun/dialect/pgdialect v1.1.8
|
||||
github.com/uptrace/bun/driver/pgdriver v1.1.8
|
||||
github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 // indirect
|
||||
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 // indirect
|
||||
go.uber.org/atomic v1.10.0 // indirect
|
||||
go.uber.org/multierr v1.8.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d // indirect
|
||||
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
|
||||
golang.org/x/net v0.0.0-20220812174116-3211cb980234 // indirect
|
||||
golang.org/x/sys v0.0.0-20220907062415-87db552b00fd // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
gopkg.in/ini.v1 v1.66.4 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
github.com/subosito/gotenv v1.4.2 // indirect
|
||||
github.com/xuri/efp v0.0.0-20230422071738-01f4e37c47e9 // indirect
|
||||
github.com/xuri/nfp v0.0.0-20230503010013-3f38cdbb0b83 // indirect
|
||||
go.uber.org/atomic v1.11.0 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
golang.org/x/crypto v0.10.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
|
||||
golang.org/x/net v0.10.0 // indirect
|
||||
golang.org/x/sys v0.9.0 // indirect
|
||||
golang.org/x/text v0.10.0 // indirect
|
||||
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
249
go.sum
249
go.sum
@@ -36,11 +36,12 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
|
||||
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
|
||||
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
|
||||
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
|
||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
|
||||
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
|
||||
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
|
||||
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
||||
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
|
||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
|
||||
@@ -50,27 +51,34 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
|
||||
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
|
||||
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
|
||||
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
|
||||
github.com/cockroachdb/cockroach-go/v2 v2.2.0 h1:/5znzg5n373N/3ESjHF5SMLxiW4RKB05Ql//KWfeTFs=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/deckarep/golang-set/v2 v2.1.0 h1:g47V4Or+DUdzbs8FxCCmgb6VYd+ptPAngjM6dtGktsI=
|
||||
github.com/deckarep/golang-set/v2 v2.1.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
|
||||
github.com/denisenkom/go-mssqldb v0.10.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
|
||||
github.com/doug-martin/goqu/v9 v9.18.0 h1:/6bcuEtAe6nsSMVK/M+fOiXUNfyFF3yYtE07DBPFMYY=
|
||||
github.com/doug-martin/goqu/v9 v9.18.0/go.mod h1:nf0Wc2/hV3gYK9LiyqIrzBEVGlI8qW3GuDCEobC4wBQ=
|
||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
||||
github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po=
|
||||
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
|
||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
|
||||
github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
|
||||
github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
|
||||
github.com/fufuok/utils v0.7.13 h1:FGx8Mnfg0ZB8HdVz1X60JJ2kFu1rtcsFDYUxUTzNKkU=
|
||||
github.com/fufuok/utils v0.7.13/go.mod h1:ztIaorPqZGdbvmW3YlwQp80K8rKJmEy6xa1KwpJSsmk=
|
||||
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
|
||||
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
|
||||
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
|
||||
github.com/fufuok/utils v0.10.2 h1:jXgE7yBSUW9z+sJs/VQq3o4MH+jN30PzIILVXFw73lE=
|
||||
github.com/fufuok/utils v0.10.2/go.mod h1:87MJq0gAZDYBgUOpxSGoLkdv8VCuRNOL9vK02F7JC3s=
|
||||
github.com/georgysavva/scany/v2 v2.0.0 h1:RGXqxDv4row7/FYoK8MRXAZXqoWF/NM+NP0q50k3DKU=
|
||||
github.com/georgysavva/scany/v2 v2.0.0/go.mod h1:sigOdh+0qb/+aOs3TVhehVT10p8qJL7K/Zhyz8vWo38=
|
||||
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
||||
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
|
||||
github.com/gofiber/fiber/v2 v2.38.1 h1:GEQ/Yt3Wsf2a30iTqtLXlBYJZso0JXPovt/tmj5H9jU=
|
||||
github.com/gofiber/fiber/v2 v2.38.1/go.mod h1:t0NlbaXzuGH7I+7M4paE848fNWInZ7mfxI/Er1fTth8=
|
||||
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||
github.com/gofiber/fiber/v2 v2.46.0 h1:wkkWotblsGVlLjXj2dpgKQAYHtXumsK/HyFugQM68Ns=
|
||||
github.com/gofiber/fiber/v2 v2.46.0/go.mod h1:DNl0/c37WLe0g92U6lx1VMQuxGUQY5V7EIaVoEsUffc=
|
||||
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
|
||||
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||
@@ -107,7 +115,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
|
||||
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
|
||||
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
|
||||
@@ -135,29 +143,44 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
|
||||
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
||||
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
|
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.3.1 h1:Fcr8QJ1ZeLi5zsPZqQeUZhNhxfkkKBOgJuYkJHoBOtU=
|
||||
github.com/jackc/pgx/v5 v5.3.1/go.mod h1:t3JDKnCBlYIc0ewLF0Q7B8MXmoIaBOZj/ic7iHozM/8=
|
||||
github.com/jackc/puddle/v2 v2.2.0 h1:RdcDk92EJBuBS55nQMMYFXTxwstHug4jkhT5pq8VxPk=
|
||||
github.com/jackc/puddle/v2 v2.2.0/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/jinzhu/copier v0.3.5 h1:GlvfUwHk62RokgqVNvYsku0TATCF7bAHVwEXoBh3iJg=
|
||||
github.com/jinzhu/copier v0.3.5/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
|
||||
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/klauspost/compress v1.15.0 h1:xqfchp4whNFxn5A4XFyyYtitiWI8Hy5EW59jEwcyL6U=
|
||||
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
|
||||
github.com/klauspost/compress v1.16.6 h1:91SKEy4K37vkp255cJ8QesJhjyRO0hn9i9G0GoUwLsk=
|
||||
github.com/klauspost/compress v1.16.6/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
|
||||
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/liamylian/jsontime/v2 v2.0.0 h1:3if2kDW/boymUdO+4Qj/m4uaXMBSF6np9KEgg90cwH0=
|
||||
github.com/liamylian/jsontime/v2 v2.0.0/go.mod h1:UHp1oAPqCBfspokvGmaGe0IAl2IgOpgOgDaKPcvcGGY=
|
||||
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
|
||||
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
|
||||
github.com/lib/pq v1.10.1 h1:6VXZrLU0jHBYyAqrSPa+MgPfnSvTPuMgK+k0o5kVFWo=
|
||||
github.com/lib/pq v1.10.1/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
||||
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
|
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
|
||||
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
|
||||
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
@@ -169,13 +192,14 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
|
||||
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
|
||||
github.com/mozillazg/go-pinyin v0.19.0 h1:p+J8/kjJ558KPvVGYLvqBhxf8jbZA2exSLCs2uUVN8c=
|
||||
github.com/mozillazg/go-pinyin v0.19.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
||||
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
|
||||
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
|
||||
github.com/pelletier/go-toml/v2 v2.0.2 h1:+jQXlF3scKIcSEKkdHzXhCTDLPFi5r1wnK6yPS+49Gw=
|
||||
github.com/pelletier/go-toml/v2 v2.0.2/go.mod h1:MovirKjgVRESsAvNZlAjtFwV867yGuwRkXbG66OzopI=
|
||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||
github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ=
|
||||
github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
||||
github.com/onsi/gomega v1.27.5 h1:T/X6I0RNFw/kTqgfkZPcQ5KU6vCnWNBGdtrIx2dpGeQ=
|
||||
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
|
||||
github.com/pelletier/go-toml/v2 v2.0.8/go.mod h1:vuYfssBdrU2XDZ9bYydBu6t+6a6PYNcZljzZR9VXg+4=
|
||||
github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
|
||||
github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw=
|
||||
github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
|
||||
@@ -187,91 +211,94 @@ github.com/richardlehane/mscfb v1.0.4/go.mod h1:YzVpcZg9czvAuhk9T+a3avCpcFPMUWm7
|
||||
github.com/richardlehane/msoleps v1.0.1/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
|
||||
github.com/richardlehane/msoleps v1.0.3 h1:aznSZzrwYRl3rLKRT3gUk9am7T/mLNSnJINvN0AQoVM=
|
||||
github.com/richardlehane/msoleps v1.0.3/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
|
||||
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
|
||||
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
|
||||
github.com/rueian/rueidis v0.0.73 h1:+r0Z6C6HMnkquPgY3zaHVpTqmCyJL56Z36GSlyBrufk=
|
||||
github.com/rueian/rueidis v0.0.73/go.mod h1:FwnfDILF2GETrvXcYFlhIiru/7NmSIm1f+7C5kutO0I=
|
||||
github.com/samber/lo v1.27.0 h1:GOyDWxsblvqYobqsmUuMddPa2/mMzkKyojlXol4+LaQ=
|
||||
github.com/samber/lo v1.27.0/go.mod h1:it33p9UtPMS7z72fP4gw/EIfQB2eI8ke7GR2wc6+Rhg=
|
||||
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||
github.com/rueian/rueidis v0.0.100 h1:22yp/+8YHuWc/vcrp8bkjeE7baD3vygoh2gZ2+xu1KQ=
|
||||
github.com/rueian/rueidis v0.0.100/go.mod h1:ivvsRYRtAUcf9OnheuKc5Gpa8IebrkLT1P45Lr2jlXE=
|
||||
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
|
||||
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
|
||||
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 h1:rmMl4fXJhKMNWl+K+r/fq4FbbKI+Ia2m9hYBLm2h4G4=
|
||||
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94/go.mod h1:90zrgN3D/WJsDd1iXHT96alCoN2KJo6/4x1DZC3wZs8=
|
||||
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d/go.mod h1:Gy+0tqhJvgGlqnTF8CVGP0AaGRjwBtXs/a5PA0Y3+A4=
|
||||
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee h1:8Iv5m6xEo1NR1AvpV+7XmhI4r39LGNzwUL4YpMuL5vk=
|
||||
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee/go.mod h1:qwtSXrKuJh/zsFQ12yEE89xfCrGKK63Rr7ctU/uCo4g=
|
||||
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
|
||||
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
|
||||
github.com/spf13/afero v1.8.2 h1:xehSyVa0YnHWsJ49JFljMpg1HX19V6NDZ1fkm1Xznbo=
|
||||
github.com/spf13/afero v1.8.2/go.mod h1:CtAatgMJh6bJEIs48Ay/FOnkljP3WeGUG0MC1RfAqwo=
|
||||
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
|
||||
github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU=
|
||||
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
|
||||
github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
|
||||
github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA=
|
||||
github.com/spf13/cast v1.5.1/go.mod h1:b9PdjNptOpzXr7Rq1q9gJML/2cdGQAo69NKzQ10KN48=
|
||||
github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
|
||||
github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/viper v1.12.0 h1:CZ7eSOd3kZoaYDLbXnmzgQI5RlciuXBMA+18HwHRfZQ=
|
||||
github.com/spf13/viper v1.12.0/go.mod h1:b6COn30jlNxbm/V2IqWiNWkJ+vZNiMNksliPCiuKtSI=
|
||||
github.com/spf13/viper v1.16.0 h1:rGGH0XDZhdUOryiDWjmIvUSWpbNqisK8Wk0Vyefw8hc=
|
||||
github.com/spf13/viper v1.16.0/go.mod h1:yg78JgCJcbrQOvV9YLXgkLaZqUidkY9K+Dd1FofRzQg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
|
||||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
|
||||
github.com/subosito/gotenv v1.3.0 h1:mjC+YW8QpAdXibNi+vNWgzmgBH4+5l5dCXv8cNysBLI=
|
||||
github.com/subosito/gotenv v1.3.0/go.mod h1:YzJjq/33h7nrwdY+iHMhEOEEbW0ovIz0tB6t6PwAXzs=
|
||||
github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M=
|
||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
|
||||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
|
||||
github.com/uptrace/bun v1.1.8 h1:slxuaP4LYWFbPRUmTtQhfJN+6eX/6ar2HDKYTcI50SA=
|
||||
github.com/uptrace/bun v1.1.8/go.mod h1:iT89ESdV3uMupD9ixt6Khidht+BK0STabK/LeZE+B84=
|
||||
github.com/uptrace/bun/dialect/pgdialect v1.1.8 h1:wayJhjYDPGv8tgOBLolbBtSFQ0TihFoo8E1T129UdA8=
|
||||
github.com/uptrace/bun/dialect/pgdialect v1.1.8/go.mod h1:nNbU8PHTjTUM+CRtGmqyBb9zcuRAB8I680/qoFSmBUk=
|
||||
github.com/uptrace/bun/driver/pgdriver v1.1.8 h1:gyL22axRQfjJS2Umq0erzJnp0bLOdUE8/USKZHPQB8o=
|
||||
github.com/uptrace/bun/driver/pgdriver v1.1.8/go.mod h1:4tHK0h7a/UoldBoe9J3GU4tEYjr3mkd62U3Kq3PVk3E=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
|
||||
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
|
||||
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
|
||||
github.com/tinylib/msgp v1.1.6/go.mod h1:75BAfg2hauQhs3qedfdDZmWAPcFMAvJE5b9rGOMufyw=
|
||||
github.com/tinylib/msgp v1.1.8 h1:FCXC1xanKO4I8plpHGH2P7koL/RzZs12l/+r7vakfm0=
|
||||
github.com/tinylib/msgp v1.1.8/go.mod h1:qkpG+2ldGg4xRFmx+jfTvZPxfGFhi64BcnL9vkCm/Tw=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasthttp v1.40.0 h1:CRq/00MfruPGFLTQKY8b+8SfdK60TxNztjRMnH0t1Yc=
|
||||
github.com/valyala/fasthttp v1.40.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
|
||||
github.com/valyala/fasthttp v1.47.0 h1:y7moDoxYzMooFpT5aHgNgVOQDrS3qlkfiP9mDtGGK9c=
|
||||
github.com/valyala/fasthttp v1.47.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA=
|
||||
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
|
||||
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
|
||||
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
|
||||
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
|
||||
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
|
||||
github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 h1:6932x8ltq1w4utjmfMPVj09jdMlkY0aiA6+Skbtl3/c=
|
||||
github.com/xuri/efp v0.0.0-20220603152613-6918739fd470/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
|
||||
github.com/xuri/excelize/v2 v2.6.1 h1:ICBdtw803rmhLN3zfvyEGH3cwSmZv+kde7LhTDT659k=
|
||||
github.com/xuri/excelize/v2 v2.6.1/go.mod h1:tL+0m6DNwSXj/sILHbQTYsLi9IF4TW59H2EF3Yrx1AU=
|
||||
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 h1:OAmKAfT06//esDdpi/DZ8Qsdt4+M5+ltca05dA5bG2M=
|
||||
github.com/xuri/efp v0.0.0-20230422071738-01f4e37c47e9 h1:ge5g8vsTQclA5lXDi+PuiAFw5GMIlMHOB/5e1hsf96E=
|
||||
github.com/xuri/efp v0.0.0-20230422071738-01f4e37c47e9/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
|
||||
github.com/xuri/excelize/v2 v2.7.1 h1:gm8q0UCAyaTt3MEF5wWMjVdmthm2EHAWesGSKS9tdVI=
|
||||
github.com/xuri/excelize/v2 v2.7.1/go.mod h1:qc0+2j4TvAUrBw36ATtcTeC1VCM0fFdAXZOmcF4nTpY=
|
||||
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
|
||||
github.com/xuri/nfp v0.0.0-20230503010013-3f38cdbb0b83 h1:xVwnvkzzi+OiwhIkWOXvh1skFI6bagk8OvGuazM80Rw=
|
||||
github.com/xuri/nfp v0.0.0-20230503010013-3f38cdbb0b83/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
|
||||
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
|
||||
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
|
||||
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
|
||||
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
|
||||
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
|
||||
go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
|
||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
|
||||
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||
go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA=
|
||||
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
|
||||
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
|
||||
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
|
||||
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
|
||||
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
||||
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
|
||||
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
|
||||
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d h1:3qF+Z8Hkrw9sOhrFHti9TlB1Hkac1x+DNRkv0XQiFjo=
|
||||
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
|
||||
golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM=
|
||||
golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
|
||||
@@ -282,12 +309,12 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
|
||||
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
|
||||
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
|
||||
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
|
||||
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
|
||||
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
|
||||
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
|
||||
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
|
||||
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
|
||||
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 h1:LRtI4W37N+KFebI/qV0OFiLUv4GLOWeEW5hn/KEJvxE=
|
||||
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
|
||||
golang.org/x/image v0.5.0 h1:5JMiNunQeQw++mMOz48/ISeNu3Iweh/JaZU8ZLqHRrI=
|
||||
golang.org/x/image v0.5.0/go.mod h1:FVC7BI/5Ym8R25iw5OLsgshdUBbT1h5jZTpA+mvAdZ4=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||
golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
@@ -309,6 +336,9 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
@@ -341,9 +371,12 @@ golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v
|
||||
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
||||
golang.org/x/net v0.0.0-20220812174116-3211cb980234 h1:RDqmgfe7SvlMWoqC3xwQ2blLO3fcWcxMa3eBLRdRW7E=
|
||||
golang.org/x/net v0.0.0-20220812174116-3211cb980234/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
|
||||
golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=
|
||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
@@ -363,6 +396,10 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
|
||||
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI=
|
||||
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
@@ -398,14 +435,21 @@ golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220907062415-87db552b00fd h1:AZeIEzg+8RCELJYq8w+ODLVxFgLMMigSwO/ffKPEd9U=
|
||||
golang.org/x/sys v0.0.0-20220907062415-87db552b00fd/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
|
||||
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
|
||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
@@ -413,8 +457,12 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
|
||||
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
@@ -459,12 +507,16 @@ golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc
|
||||
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
|
||||
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
|
||||
golang.org/x/tools v0.0.0-20200904185747-39188db58858/go.mod h1:Cj7w3i3Rnn0Xh82ur9kSqwfTHTeVxaDqrfMjpcNT6bE=
|
||||
golang.org/x/tools v0.0.0-20201022035929-9cf592e881e9/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/tools v0.0.0-20201110124207-079ba7bd75cd/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/tools v0.0.0-20201201161351-ac6f37ff4c2a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/tools v0.0.0-20201208233053-a543418bbed2/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/tools v0.0.0-20210108195828-e2f9c7f1fc8e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
@@ -560,18 +612,13 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
gopkg.in/ini.v1 v1.66.4 h1:SsAcf+mM7mRZo2nJNGt8mZCjG8ZRaNGMURJw7BsIST4=
|
||||
gopkg.in/ini.v1 v1.66.4/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
|
||||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
@@ -581,8 +628,6 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
|
||||
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
|
||||
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
|
||||
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
|
||||
mellium.im/sasl v0.3.0 h1:0qoaTCTo5Py7u/g0cBIQZcMOgG/5LM71nshbXwznBh8=
|
||||
mellium.im/sasl v0.3.0/go.mod h1:xm59PUYpZHhgQ9ZqoJ5QaCqzWMi8IeS49dhp6plPCzw=
|
||||
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
|
||||
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
|
||||
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
|
||||
|
||||
@@ -1,163 +0,0 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
// QueryHookOptions logging options
|
||||
type QueryHookOptions struct {
|
||||
LogSlow time.Duration
|
||||
Logger *zap.Logger
|
||||
QueryLevel zapcore.Level
|
||||
SlowLevel zapcore.Level
|
||||
ErrorLevel zapcore.Level
|
||||
MessageTemplate string
|
||||
ErrorTemplate string
|
||||
}
|
||||
|
||||
// QueryHook wraps query hook
|
||||
type QueryHook struct {
|
||||
opts QueryHookOptions
|
||||
errorTemplate *template.Template
|
||||
messageTemplate *template.Template
|
||||
}
|
||||
|
||||
// LogEntryVars variables made available t otemplate
|
||||
type LogEntryVars struct {
|
||||
Timestamp time.Time
|
||||
Query string
|
||||
Operation string
|
||||
Duration time.Duration
|
||||
Error error
|
||||
}
|
||||
|
||||
// NewQueryHook returns new instance
|
||||
func NewQueryHook(opts QueryHookOptions) *QueryHook {
|
||||
h := new(QueryHook)
|
||||
|
||||
if opts.ErrorTemplate == "" {
|
||||
opts.ErrorTemplate = "{{.Operation}}[{{.Duration}}]: {{.Query}}: {{.Error}}"
|
||||
}
|
||||
if opts.MessageTemplate == "" {
|
||||
opts.MessageTemplate = "{{.Operation}}[{{.Duration}}]: {{.Query}}"
|
||||
}
|
||||
h.opts = opts
|
||||
errorTemplate, err := template.New("ErrorTemplate").Parse(h.opts.ErrorTemplate)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
messageTemplate, err := template.New("MessageTemplate").Parse(h.opts.MessageTemplate)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
h.errorTemplate = errorTemplate
|
||||
h.messageTemplate = messageTemplate
|
||||
return h
|
||||
}
|
||||
|
||||
// BeforeQuery does nothing tbh
|
||||
func (h *QueryHook) BeforeQuery(ctx context.Context, event *bun.QueryEvent) context.Context {
|
||||
return ctx
|
||||
}
|
||||
|
||||
// AfterQuery convert a bun QueryEvent into a logrus message
|
||||
func (h *QueryHook) AfterQuery(ctx context.Context, event *bun.QueryEvent) {
|
||||
var level zapcore.Level
|
||||
var isError bool
|
||||
var msg bytes.Buffer
|
||||
|
||||
now := time.Now()
|
||||
dur := now.Sub(event.StartTime)
|
||||
|
||||
switch event.Err {
|
||||
case nil, sql.ErrNoRows:
|
||||
isError = false
|
||||
if h.opts.LogSlow > 0 && dur >= h.opts.LogSlow {
|
||||
level = h.opts.SlowLevel
|
||||
} else {
|
||||
level = h.opts.QueryLevel
|
||||
}
|
||||
default:
|
||||
isError = true
|
||||
level = h.opts.ErrorLevel
|
||||
}
|
||||
if level == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
args := &LogEntryVars{
|
||||
Timestamp: now,
|
||||
Query: string(event.Query),
|
||||
Operation: eventOperation(event),
|
||||
Duration: dur,
|
||||
Error: event.Err,
|
||||
}
|
||||
|
||||
if isError {
|
||||
if err := h.errorTemplate.Execute(&msg, args); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
if err := h.messageTemplate.Execute(&msg, args); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
switch level {
|
||||
case zapcore.DebugLevel:
|
||||
h.opts.Logger.Debug(msg.String(), zap.String("query", event.Query), zap.Any("args", event.QueryArgs))
|
||||
case zapcore.InfoLevel:
|
||||
h.opts.Logger.Info(msg.String(), zap.String("query", event.Query), zap.Any("args", event.QueryArgs))
|
||||
case zapcore.WarnLevel:
|
||||
h.opts.Logger.Warn(msg.String(), zap.String("query", event.Query), zap.Any("args", event.QueryArgs))
|
||||
case zapcore.ErrorLevel:
|
||||
h.opts.Logger.Error(msg.String(), zap.String("query", event.Query), zap.Any("args", event.QueryArgs), zap.Error(event.Err))
|
||||
case zapcore.FatalLevel:
|
||||
h.opts.Logger.Fatal(msg.String(), zap.String("query", event.Query), zap.Any("args", event.QueryArgs), zap.Error(event.Err))
|
||||
case zapcore.PanicLevel:
|
||||
h.opts.Logger.Panic(msg.String(), zap.String("query", event.Query), zap.Any("args", event.QueryArgs), zap.Error(event.Err))
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported level: %v", level))
|
||||
}
|
||||
}
|
||||
|
||||
// taken from bun
|
||||
func eventOperation(event *bun.QueryEvent) string {
|
||||
switch event.QueryAppender.(type) {
|
||||
case *bun.SelectQuery:
|
||||
return "SELECT"
|
||||
case *bun.InsertQuery:
|
||||
return "INSERT"
|
||||
case *bun.UpdateQuery:
|
||||
return "UPDATE"
|
||||
case *bun.DeleteQuery:
|
||||
return "DELETE"
|
||||
case *bun.CreateTableQuery:
|
||||
return "CREATE TABLE"
|
||||
case *bun.DropTableQuery:
|
||||
return "DROP TABLE"
|
||||
}
|
||||
return queryOperation(event.Query)
|
||||
}
|
||||
|
||||
// taken from bun
|
||||
func queryOperation(name string) string {
|
||||
if idx := strings.Index(name, " "); idx > 0 {
|
||||
name = name[:idx]
|
||||
}
|
||||
if len(name) > 16 {
|
||||
name = name[:16]
|
||||
}
|
||||
return string(name)
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/types"
|
||||
"os"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
@@ -40,7 +42,7 @@ func init() {
|
||||
|
||||
logger = zap.New(core).Named("App")
|
||||
sugaredLogger = logger.Sugar()
|
||||
logger.Info("Logger initialized.")
|
||||
logger.Info("日志系统初始化完成。")
|
||||
}
|
||||
|
||||
func GetLogger() *zap.Logger {
|
||||
@@ -130,3 +132,53 @@ func With(fields ...zap.Field) *zap.Logger {
|
||||
func WithSugar(fields ...zap.Field) *zap.SugaredLogger {
|
||||
return logger.With(fields...).Sugar()
|
||||
}
|
||||
|
||||
func DecimalField(key string, val decimal.Decimal) zap.Field {
|
||||
return zap.String(key, val.String())
|
||||
}
|
||||
|
||||
func DecimalFieldp(key string, val *decimal.Decimal) zap.Field {
|
||||
if val == nil {
|
||||
return zap.Stringp(key, nil)
|
||||
}
|
||||
return DecimalField(key, *val)
|
||||
}
|
||||
|
||||
func NullDecimalField(key string, val decimal.NullDecimal) zap.Field {
|
||||
if val.Valid {
|
||||
return DecimalField(key, val.Decimal)
|
||||
}
|
||||
return zap.Stringp(key, nil)
|
||||
}
|
||||
|
||||
func NullDecimalFieldp(key string, val *decimal.NullDecimal) zap.Field {
|
||||
if val == nil {
|
||||
return zap.Stringp(key, nil)
|
||||
}
|
||||
if val.Valid {
|
||||
return DecimalField(key, val.Decimal)
|
||||
}
|
||||
return zap.Stringp(key, nil)
|
||||
}
|
||||
|
||||
func DateField(key string, val types.Date) zap.Field {
|
||||
return val.Log(key)
|
||||
}
|
||||
|
||||
func DateFieldp(key string, val *types.Date) zap.Field {
|
||||
if val == nil {
|
||||
return zap.Stringp(key, nil)
|
||||
}
|
||||
return DateField(key, *val)
|
||||
}
|
||||
|
||||
func DateTimeField(key string, val types.DateTime) zap.Field {
|
||||
return val.Log(key)
|
||||
}
|
||||
|
||||
func DateTimeFieldp(key string, val *types.DateTime) zap.Field {
|
||||
if val == nil {
|
||||
return zap.Stringp(key, nil)
|
||||
}
|
||||
return DateTimeField(key, *val)
|
||||
}
|
||||
|
||||
@@ -62,9 +62,13 @@ func NewLogMiddleware(config LogMiddlewareConfig) fiber.Handler {
|
||||
fields := []zap.Field{
|
||||
zap.Namespace("context"),
|
||||
zap.String("pid", strconv.Itoa(os.Getpid())),
|
||||
zap.String("method", c.Method()),
|
||||
zap.String("remote", c.IP()),
|
||||
zap.Strings("forwarded", c.IPs()),
|
||||
zap.String("url", c.OriginalURL()),
|
||||
zap.String("time", stop.Sub(start).String()),
|
||||
zap.Object("response", Resp(c.Response())),
|
||||
zap.Object("request", Req(c)),
|
||||
// zap.Object("response", Resp(c.Response())),
|
||||
// zap.Object("request", Req(c)),
|
||||
}
|
||||
|
||||
if u := c.Locals("userId"); u != nil {
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
@@ -14,10 +17,11 @@ func newRollingWriter() io.Writer {
|
||||
return nil
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
return &lumberjack.Logger{
|
||||
Filename: "log/service.log",
|
||||
MaxBackups: 366 * 10, // files
|
||||
Filename: fmt.Sprintf("log/service_%s.log", now.Format("2006-01-02_15")),
|
||||
MaxBackups: math.MaxInt, // files
|
||||
MaxSize: 200, // megabytes
|
||||
MaxAge: 366 * 10, // days
|
||||
MaxAge: math.MaxInt, // days
|
||||
}
|
||||
}
|
||||
|
||||
152
main.go
152
main.go
@@ -1,25 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"electricity_bill_calc/cache"
|
||||
"electricity_bill_calc/config"
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/migration"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/repository"
|
||||
"electricity_bill_calc/router"
|
||||
"electricity_bill_calc/service"
|
||||
"encoding/csv"
|
||||
"electricity_bill_calc/types"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/samber/lo"
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/uptrace/bun/migrate"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
@@ -27,145 +20,70 @@ func init() {
|
||||
l := logger.Named("Init")
|
||||
err := config.SetupSetting()
|
||||
if err != nil {
|
||||
l.Fatal("Configuration load failed.", zap.Error(err))
|
||||
l.Fatal("服务配置文件加载失败!", zap.Error(err))
|
||||
}
|
||||
l.Info("Configuration loaded!")
|
||||
l.Info("服务配置已经完成加载。")
|
||||
|
||||
err = global.SetupDatabaseConnection()
|
||||
if err != nil {
|
||||
l.Fatal("Main Database connect failed.", zap.Error(err))
|
||||
}
|
||||
l.Info("Main Database connected!")
|
||||
|
||||
migrator := migrate.NewMigrator(global.DB, migration.Migrations)
|
||||
ctx, cancel := global.TimeoutContext(12)
|
||||
defer cancel()
|
||||
err = migrator.Init(ctx)
|
||||
if err != nil {
|
||||
l.Fatal("Database migration unable to initialized.", zap.Error(err))
|
||||
}
|
||||
group, err := migrator.Migrate(ctx)
|
||||
if err != nil {
|
||||
l.Fatal("Database migrate failed.", zap.Error(err))
|
||||
}
|
||||
if group.IsZero() {
|
||||
l.Info("There are no new migrations to run (database is up to date)")
|
||||
l.Fatal("主数据库连接失败!", zap.Error(err))
|
||||
}
|
||||
l.Info("主数据库已经连接。")
|
||||
|
||||
err = global.SetupRedisConnection()
|
||||
if err != nil {
|
||||
l.Fatal("Main Cache Database connect failed.", zap.Error(err))
|
||||
l.Fatal("主缓存数据库连接失败!", zap.Error(err))
|
||||
}
|
||||
l.Info("Main Cache Database connected!")
|
||||
|
||||
err = initializeRegions()
|
||||
if err != nil {
|
||||
l.Fatal("Regions initialize failed.", zap.Error(err))
|
||||
}
|
||||
l.Info("Regions synchronized.")
|
||||
l.Info("主缓存数据库已经连接。")
|
||||
|
||||
err = intializeSingularity()
|
||||
if err != nil {
|
||||
l.Fatal("Singularity account intialize failed.", zap.Error(err))
|
||||
l.Fatal("奇点账号初始化失败。", zap.Error(err))
|
||||
}
|
||||
l.Info("Singularity account intialized.")
|
||||
}
|
||||
|
||||
func initializeRegions() error {
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
logger.Info("Synchronize regions...")
|
||||
regionCsvFile, err := os.Open("regions.csv")
|
||||
if err != nil {
|
||||
return fmt.Errorf("region initialize file is not found: %w", err)
|
||||
}
|
||||
defer regionCsvFile.Close()
|
||||
|
||||
var existRegions = make([]string, 0)
|
||||
err = global.DB.NewSelect().Model((*model.Region)(nil)).
|
||||
Column("code").
|
||||
Scan(ctx, &existRegions)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to retreive regions from database: %w", err)
|
||||
}
|
||||
|
||||
regionCsv := csv.NewReader(regionCsvFile)
|
||||
transaction, err := global.DB.BeginTx(ctx, &sql.TxOptions{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to intiate database transaction: %w", err)
|
||||
}
|
||||
for {
|
||||
record, err := regionCsv.Read()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if lo.Contains(existRegions, record[0]) {
|
||||
continue
|
||||
}
|
||||
level, err := strconv.Atoi(record[2])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if _, err := transaction.NewInsert().Model(&model.Region{
|
||||
Code: record[0],
|
||||
Name: record[1],
|
||||
Level: level,
|
||||
Parent: record[3],
|
||||
}).Exec(ctx); err != nil {
|
||||
return fmt.Errorf("region synchronize in failed: %v, %w", record, err)
|
||||
}
|
||||
}
|
||||
if err = transaction.Commit(); err != nil {
|
||||
return fmt.Errorf("synchronize regions to database failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
l.Info("奇点账号已经完成初始化。")
|
||||
}
|
||||
|
||||
func intializeSingularity() error {
|
||||
singularityExists, err := service.UserService.IsUserExists("000")
|
||||
l := logger.Named("Init", "Singularity")
|
||||
singularityExists, err := repository.UserRepository.IsUserExists("000")
|
||||
if err != nil {
|
||||
return fmt.Errorf("singularity detect failed: %w", err)
|
||||
l.Error("检测奇点账号失败。", zap.Error(err))
|
||||
return fmt.Errorf("检测奇点账号失败: %w", err)
|
||||
}
|
||||
if singularityExists {
|
||||
l.Info("奇点账号已经存在,跳过剩余初始化步骤。")
|
||||
return nil
|
||||
}
|
||||
singularity := &model.User{
|
||||
Id: "000",
|
||||
singularityId := "000"
|
||||
singularityExpires, err := types.ParseDate("2099-12-31")
|
||||
if err != nil {
|
||||
l.Error("奇点用户账号过期时间解析失败。", zap.Error(err))
|
||||
return fmt.Errorf("奇点用户账号过期时间解析失败: %w", err)
|
||||
}
|
||||
singularity := &model.ManagementAccountCreationForm{
|
||||
Id: &singularityId,
|
||||
Username: "singularity",
|
||||
Name: "Singularity",
|
||||
Type: 2,
|
||||
Enabled: true,
|
||||
Expires: singularityExpires,
|
||||
}
|
||||
singularityName := "Singularity"
|
||||
singularityExpires, err := model.ParseDate("2099-12-31")
|
||||
verifyCode, err := service.UserService.CreateUserAccount(
|
||||
singularity.IntoUser(),
|
||||
singularity.IntoUserDetail())
|
||||
if err != nil {
|
||||
return fmt.Errorf("singularity expires time parse failed: %w", err)
|
||||
}
|
||||
singularityDetail := &model.UserDetail{
|
||||
Name: &singularityName,
|
||||
UnitServiceFee: decimal.Zero,
|
||||
ServiceExpiration: singularityExpires,
|
||||
}
|
||||
verifyCode, err := service.UserService.CreateUser(singularity, singularityDetail)
|
||||
if err != nil {
|
||||
return fmt.Errorf("singularity account failed to create: %w", err)
|
||||
l.Error("创建奇点账号失败。", zap.Error(err))
|
||||
return fmt.Errorf("创建奇点账号失败: %w", err)
|
||||
}
|
||||
logger.Info(
|
||||
fmt.Sprintf("Singularity account created, use %s as verify code to reset password.", verifyCode),
|
||||
zap.String("account", "singularity"),
|
||||
zap.String("verifyCode", verifyCode),
|
||||
fmt.Sprintf("奇点账号已经完成创建, 首次登录需要使用验证码 [%s] 重置密码。", *verifyCode),
|
||||
zap.String("账号名称", "singularity"),
|
||||
zap.String("验证码", *verifyCode),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
|
||||
func DBConnectionKeepLive() {
|
||||
for range time.Tick(30 * time.Second) {
|
||||
err := global.DB.Ping()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 清理Redis缓存中的孤儿键。
|
||||
func RedisOrphanCleanup() {
|
||||
cleanLogger := logger.Named("Cache").With(zap.String("function", "Cleanup"))
|
||||
for range time.Tick(2 * time.Minute) {
|
||||
@@ -179,8 +97,6 @@ func RedisOrphanCleanup() {
|
||||
}
|
||||
|
||||
func main() {
|
||||
// 本次停用检测的原因是:使用Ping来保持数据库链接看起来没有什么用处。
|
||||
// go DBConnectionKeepLive()
|
||||
go RedisOrphanCleanup()
|
||||
app := router.App()
|
||||
app.Listen(fmt.Sprintf(":%d", config.ServerSettings.HttpPort))
|
||||
|
||||
@@ -1,240 +0,0 @@
|
||||
create table if not exists region (
|
||||
code varchar(15) not null primary key,
|
||||
name varchar(50) not null,
|
||||
level smallint not null default 0,
|
||||
parent varchar(15) not null default '0'
|
||||
);
|
||||
|
||||
create table if not exists `user` (
|
||||
created_at timestamptz not null,
|
||||
id varchar(120) not null primary key,
|
||||
username varchar(30) not null,
|
||||
password varchar(256) not null,
|
||||
reset_needed boolean not null default false,
|
||||
type smallint not null,
|
||||
enabled boolean not null default true
|
||||
);
|
||||
|
||||
create table if not exists user_detail (
|
||||
created_at timestamptz not null,
|
||||
created_by varchar(100),
|
||||
last_modified_at timestamptz,
|
||||
last_modified_by varchar(100),
|
||||
deleted_at timestamptz,
|
||||
deleted_by varchar(100),
|
||||
id varchar(120) not null primary key,
|
||||
name varchar(100),
|
||||
abbr varchar(50),
|
||||
region varchar(10),
|
||||
address varchar(120),
|
||||
contact varchar(100),
|
||||
phone varchar(50),
|
||||
unit_service_fee numeric(8,2) not null default 0,
|
||||
service_expiration date not null
|
||||
);
|
||||
|
||||
create table if not exists user_charge (
|
||||
created_at timestamptz not null,
|
||||
seq bigserial not null primary key,
|
||||
user_id varchar(120) not null,
|
||||
fee numeric(12,2),
|
||||
discount numeric(5,4),
|
||||
amount numeric(12,2),
|
||||
charge_to date not null,
|
||||
settled boolean not null default false,
|
||||
settled_at timestamptz,
|
||||
cancelled boolean not null default false,
|
||||
cancelled_at timestamptz,
|
||||
refunded boolean not null default false,
|
||||
refunded_at timestamptz
|
||||
);
|
||||
|
||||
create table if not exists park (
|
||||
created_at timestamptz not null,
|
||||
created_by varchar(100),
|
||||
last_modified_at timestamptz,
|
||||
last_modified_by varchar(100),
|
||||
deleted_at timestamptz,
|
||||
deleted_by varchar(100),
|
||||
id varchar(120) not null primary key,
|
||||
user_id varchar(120) not null,
|
||||
name varchar(70) not null,
|
||||
abbr varchar(50),
|
||||
area numeric(14,2),
|
||||
tenement_quantity numeric(8,0),
|
||||
capacity numeric(16,2),
|
||||
category smallint not null default 0,
|
||||
meter_04kv_type smallint not null default 0,
|
||||
region varchar(10),
|
||||
address varchar(120),
|
||||
contact varchar(100),
|
||||
phone varchar(50),
|
||||
enabled boolean not null default true
|
||||
);
|
||||
|
||||
create table if not exists meter_04kv (
|
||||
created_at timestamptz not null,
|
||||
created_by varchar(100),
|
||||
last_modified_at timestamptz,
|
||||
last_modified_by varchar(100),
|
||||
code varchar(120) not null,
|
||||
park_id varchar(120) not null,
|
||||
address varchar(100),
|
||||
customer_name varchar(100),
|
||||
contact_name varchar(70),
|
||||
contact_phone varchar(50),
|
||||
ratio numeric(8,4) not null default 1,
|
||||
seq bigint not null default 0,
|
||||
public_meter boolean not null default false,
|
||||
dilute boolean not null default false,
|
||||
enabled boolean not null default true,
|
||||
primary key (code, park_id)
|
||||
);
|
||||
|
||||
create table if not exists maintenance_fee (
|
||||
created_at timestamptz not null,
|
||||
created_by varchar(100),
|
||||
last_modified_at timestamptz,
|
||||
last_modified_by varchar(100),
|
||||
deleted_at timestamptz,
|
||||
deleted_by varchar(100),
|
||||
id varchar(120) not null primary key,
|
||||
park_id varchar(120) not null,
|
||||
name varchar(50) not null,
|
||||
fee numeric(8,2) not null default 0,
|
||||
memo text,
|
||||
enabled boolean not null default true
|
||||
);
|
||||
|
||||
create table if not exists report (
|
||||
created_at timestamptz not null,
|
||||
created_by varchar(100),
|
||||
last_modified_at timestamptz,
|
||||
last_modified_by varchar(100),
|
||||
id varchar(120) not null primary key,
|
||||
park_id varchar(120) not null,
|
||||
period date not null,
|
||||
category smallint not null default 0,
|
||||
meter_04kv_type smallint not null default 0,
|
||||
step_state jsonb not null,
|
||||
published boolean not null default false,
|
||||
published_at timestamptz,
|
||||
withdraw smallint not null default 0,
|
||||
last_withdraw_applied_at timestamptz,
|
||||
last_withdraw_audit_at timestamptz
|
||||
);
|
||||
|
||||
create table if not exists report_summary (
|
||||
report_id varchar(120) not null primary key,
|
||||
overall numeric(14,2) not null default 0,
|
||||
overall_fee numeric(14,2) not null default 0,
|
||||
consumption_fee numeric(14,2),
|
||||
overall_price numeric(16,8),
|
||||
critical numeric(14,2) not null default 0,
|
||||
critical_fee numeric(14,2) not null default 0,
|
||||
critical_price numeric(16,8),
|
||||
peak numeric(14,2) not null default 0,
|
||||
peak_fee numeric(14,2) not null default 0,
|
||||
peak_price numeric(16,8),
|
||||
flat numeric(14,2) not null default 0,
|
||||
flat_fee numeric(14,2) not null default 0,
|
||||
flat_price numeric(16,8),
|
||||
valley numeric(14,2) not null default 0,
|
||||
valley_fee numeric(14,2) not null default 0,
|
||||
valley_price numeric(16,8),
|
||||
loss numeric(14,2),
|
||||
loss_fee numeric(16,2),
|
||||
loss_proportion numeric(16,15),
|
||||
customer_consumption numeric(16,2),
|
||||
customer_consumption_fee numeric(14,2),
|
||||
customer_consumption_critical numeric(16,2),
|
||||
customer_consumption_critical_fee numeric(14,2),
|
||||
customer_consumption_peak numeric(16,2),
|
||||
customer_consumption_peak_fee numeric(14,2),
|
||||
customer_consumption_flat numeric(16,2),
|
||||
customer_consumption_flat_fee numeric(14,2),
|
||||
customer_consumption_valley numeric(16,2),
|
||||
customer_consumption_valley_fee numeric(14,2),
|
||||
public_consumption numeric(16,2),
|
||||
public_consumption_fee numeric(14,2),
|
||||
public_consumption_proportion numeric(16,15),
|
||||
public_consumption_critical numeric(16,2),
|
||||
public_consumption_critical_fee numeric(14,2),
|
||||
public_consumption_peak numeric(16,2),
|
||||
public_consumption_peak_fee numeric(14,2),
|
||||
public_consumption_flat numeric(16,2),
|
||||
public_consumption_flat_fee numeric(14,2),
|
||||
public_consumption_valley numeric(16,2),
|
||||
public_consumption_valley_fee numeric(14,2),
|
||||
basic_fee numeric(14,2) not null default 0,
|
||||
basic_diluted_price numeric(18,8),
|
||||
adjust_fee numeric(14,2) not null default 0,
|
||||
adjust_diluted_price numeric(18,8),
|
||||
maintenance_diluted_price numeric(16,8),
|
||||
loss_diluted_price numeric(16,8),
|
||||
public_consumption_diluted_price numeric(16,8),
|
||||
maintenance_overall numeric(16,8),
|
||||
final_diluted_overall numeric(14,2)
|
||||
);
|
||||
|
||||
create table if not exists will_diluted_fee (
|
||||
id varchar(120) not null primary key,
|
||||
report_id varchar(120) not null,
|
||||
source_id varchar(120),
|
||||
name varchar(50) not null,
|
||||
fee numeric(8,2) not null default 0,
|
||||
memo text
|
||||
);
|
||||
|
||||
create table if not exists end_user_detail (
|
||||
created_at timestamptz not null,
|
||||
created_by varchar(100),
|
||||
last_modified_at timestamptz,
|
||||
last_modified_by varchar(100),
|
||||
report_id varchar(120) not null,
|
||||
park_id varchar(120) not null,
|
||||
meter_04kv_id varchar(120) not null,
|
||||
seq bigint not null default 0,
|
||||
ratio numeric(8,4) not null default 1,
|
||||
address varchar(100),
|
||||
customer_name varchar(100),
|
||||
contact_name varchar(70),
|
||||
contact_phone varcahar(50),
|
||||
public_meter boolean not null default false,
|
||||
dilute boolean not null default false,
|
||||
last_period_overall numeric(14,2) not null default 0,
|
||||
last_period_critical numeric(14,2) not null default 0,
|
||||
last_period_peak numeric(14,2) not null default 0,
|
||||
last_period_flat numeric(14,2) not null default 0,
|
||||
last_period_valley numeric(14,2) not null default 0,
|
||||
current_period_overall numeric(14,2) not null default 0,
|
||||
current_period_critical numeric(14,2) not null default 0,
|
||||
current_period_peak numeric(14,2) not null default 0,
|
||||
current_period_flat numeric(14,2) not null default 0,
|
||||
current_period_valley numeric(14,2) not null default 0,
|
||||
adjust_overall numeric(14,2) not null default 0,
|
||||
adjust_critical numeric(14,2) not null default 0,
|
||||
adjust_peak numeric(14,2) not null default 0,
|
||||
adjust_flat numeric(14,2) not null default 0,
|
||||
adjust_valley numeric(14,2) not null default 0,
|
||||
overall numeric(14,2),
|
||||
overall_fee numeric(14,2),
|
||||
overall_proportion numeric(16,15) not null default 0,
|
||||
critical numeric(14,2),
|
||||
critical_fee numeric(18,8),
|
||||
peak numeric(14,2),
|
||||
peak_fee numeric(18,8),
|
||||
flat numeric(14,2),
|
||||
flat_fee numeric(18,8),
|
||||
valley numeric(14,2),
|
||||
valley_fee numeric(18,8),
|
||||
basic_fee_diluted numeric(18,8),
|
||||
adjust_fee_diluted numeric(18,8),
|
||||
loss_diluted numeric(18,8),
|
||||
loss_fee_diluted numeric(18,8),
|
||||
maintenance_fee_diluted numeric(18,8),
|
||||
public_consumption_diluted numeric(18,8),
|
||||
final_diluted numeric(14,2),
|
||||
final_charge numeric(14,2),
|
||||
primary key (report_id, park_id, meter_04kv_id)
|
||||
);
|
||||
@@ -1,21 +0,0 @@
|
||||
drop table if exists region;
|
||||
|
||||
drop table if exists user;
|
||||
|
||||
drop table if exists user_detail;
|
||||
|
||||
drop table if exists user_charge;
|
||||
|
||||
drop table if exists park;
|
||||
|
||||
drop table if exists meter_04kv;
|
||||
|
||||
drop table if exists maintenance_fee;
|
||||
|
||||
drop table if exists report;
|
||||
|
||||
drop table if exists report_summary;
|
||||
|
||||
drop table if exists will_diluted_fee;
|
||||
|
||||
drop table if exists end_user_detail;
|
||||
@@ -1,31 +0,0 @@
|
||||
alter table if exists `user` add constraint user_type_check check (type in (0, 1, 2));
|
||||
|
||||
alter table if exists user_detail add constraint positive_service_fee check (unit_service_fee >= 0);
|
||||
|
||||
alter table if exists user_charge add constraint positive_fee check (fee >= 0);
|
||||
|
||||
alter table if exists user_charge add constraint positive_amount check (amount >= 0);
|
||||
|
||||
alter table if exists park add constraint positive_tenement check (tenement_quantity >= 0);
|
||||
|
||||
alter table if exists park add constraint positive_area check (area >= 0);
|
||||
|
||||
alter table if exists park add constraint positive_capacity check (capacity >= 0);
|
||||
|
||||
alter table if exists park add constraint category_check check (category in (0, 1, 2));
|
||||
|
||||
alter table if exists park add constraint meter_check check (meter_04kv_type in (0, 1));
|
||||
|
||||
alter table if exists meter_04kv add constraint positive_ratio check (ratio > 0);
|
||||
|
||||
alter table if exists maintenance_fee add constraint positive_fee check (fee >= 0);
|
||||
|
||||
alter table if exists report add constraint category_check check (category in (0, 1, 2));
|
||||
|
||||
alter table if exists report add constraint meter_check check (meter_04kv_type in (0, 1));
|
||||
|
||||
alter table if exists report add constraint withdraw_action_check check (withdraw in (0, 1, 2, 3));
|
||||
|
||||
alter table if exists will_diluted_fee add constraint positive_fee check (fee >= 0);
|
||||
|
||||
alter table if exists end_user_detail add constraint positive_ratio check (ratio > 0);
|
||||
@@ -1,31 +0,0 @@
|
||||
alter table if exists user drop constraint user_type_check;
|
||||
|
||||
alter table if exists user_detail drop constraint positive_service_fee;
|
||||
|
||||
alter table if exists user_charge drop constraint positive_fee;
|
||||
|
||||
alter table if exists user_charge drop constraint positive_amount;
|
||||
|
||||
alter table if exists park drop constraint positive_tenement;
|
||||
|
||||
alter table if exists park drop constraint positive_area;
|
||||
|
||||
alter table if exists park drop constraint positive_capacity;
|
||||
|
||||
alter table if exists park drop constraint category_check;
|
||||
|
||||
alter table if exists park drop constraint meter_check;
|
||||
|
||||
alter table if exists meter_04kv drop constraint positive_ratio;
|
||||
|
||||
alter table if exists maintenance_fee drop constraint positive_fee;
|
||||
|
||||
alter table if exists report drop constraint category_check;
|
||||
|
||||
alter table if exists report drop constraint meter_check;
|
||||
|
||||
alter table if exists report drop constraint withdraw_action_check;
|
||||
|
||||
alter table if exists will_diluted_fee drop constraint positive_fee;
|
||||
|
||||
alter table if exists end_user_detail drop constraint positive_ratio;
|
||||
@@ -1,23 +0,0 @@
|
||||
update report_summary
|
||||
set
|
||||
customer_consumption = nullif(trim(customers->>'consumption'), '')::numeric(16,2),
|
||||
customer_consumption_fee = nullif(trim(customers->>'consumptionFee'), '')::numeric(14,2),
|
||||
customer_consumption_critical = nullif(trim(customers->>'critical'), '')::numeric(16,2),
|
||||
customer_consumption_critical_fee = nullif(trim(customers->>'criticalFee'), '')::numeric(14,2),
|
||||
customer_consumption_peak = nullif(trim(customers->>'peak'), '')::numeric(16,2),
|
||||
customer_consumption_peak_fee = nullif(trim(customers->>'peakFee'), '')::numeric(14,2),
|
||||
customer_consumption_flat = nullif(trim(customers->>'flat'), '')::numeric(16,2),
|
||||
customer_consumption_flat_fee = nullif(trim(customers->>'flatFee'), '')::numeric(14,2),
|
||||
customer_consumption_valley = nullif(trim(customers->>'valley'), '')::numeric(16,2),
|
||||
customer_consumption_valley_fee = nullif(trim(customers->>'valleyFee'), '')::numeric(14,2),
|
||||
public_consumption = nullif(trim(publics->>'consumption'), '')::numeric(16,2),
|
||||
public_consumption_fee = nullif(trim(publics->>'consumptionFee'), '')::numeric(14,2),
|
||||
public_consumption_critical = nullif(trim(publics->>'critical'), '')::numeric(16,2),
|
||||
public_consumption_critical_fee = nullif(trim(publics->>'criticalFee'), '')::numeric(14,2),
|
||||
public_consumption_peak = nullif(trim(publics->>'peak'), '')::numeric(16,2),
|
||||
public_consumption_peak_fee = nullif(trim(publics->>'peakFee'), '')::numeric(14,2),
|
||||
public_consumption_flat = nullif(trim(publics->>'flat'), '')::numeric(16,2),
|
||||
public_consumption_flat_fee = nullif(trim(publics->>'flatFee'), '')::numeric(14,2),
|
||||
public_consumption_valley = nullif(trim(publics->>'valley'), '')::numeric(16,2),
|
||||
public_consumption_valley_fee = nullif(trim(publics->>'valleyFee'), '')::numeric(14,2),
|
||||
public_consumption_proportion = nullif(trim(publics->>'proportion'), '')::numeric(16,15);
|
||||
@@ -1,46 +0,0 @@
|
||||
alter table report_summary
|
||||
add column if not exists customers jsonb,
|
||||
add column if not exists publics jsonb,
|
||||
add column if not exists diluteds jsonb;
|
||||
|
||||
update report_summary
|
||||
set
|
||||
customers = jsonb_build_object(
|
||||
'consumption', customer_consumption,
|
||||
'fee', customer_consumption_fee,
|
||||
'critical', customer_consumption_critical,
|
||||
'criticalFee', customer_consumption_critical_fee,
|
||||
'peak', customer_consumption_peak,
|
||||
'peakFee', customer_consumption_peak_fee,
|
||||
'flat', customer_consumption_flat,
|
||||
'flatFee', customer_consumption_flat_fee,
|
||||
'valley', customer_consumption_valley,
|
||||
'valleyFee', customer_consumption_valley_fee,
|
||||
'proportion', null
|
||||
),
|
||||
diluteds = jsonb_build_object(
|
||||
'consumption', public_consumption,
|
||||
'fee', public_consumption_fee,
|
||||
'critical', public_consumption_critical,
|
||||
'criticalFee', public_consumption_critical_fee,
|
||||
'peak', public_consumption_peak,
|
||||
'peakFee', public_consumption_peak_fee,
|
||||
'flat', public_consumption_flat,
|
||||
'flatFee', public_consumption_flat_fee,
|
||||
'valley', public_consumption_valley,
|
||||
'valleyFee', public_consumption_valley_fee,
|
||||
'proportion', public_consumption_proportion
|
||||
),
|
||||
publics = jsonb_build_object(
|
||||
'consumption', null,
|
||||
'fee', null,
|
||||
'critical', null,
|
||||
'criticalFee', null,
|
||||
'peak', null,
|
||||
'peakFee', null,
|
||||
'flat', null,
|
||||
'flatFee', null,
|
||||
'valley', null,
|
||||
'valleyFee', null,
|
||||
'proportion', null
|
||||
);
|
||||
@@ -1,15 +0,0 @@
|
||||
alter table meter_04kv
|
||||
add column dilute boolean not null default false;
|
||||
|
||||
alter table end_user_detail
|
||||
add column dilute boolean not null default false,
|
||||
add column maintenance_fee_diluted numeric(18,8),
|
||||
add column public_consumption_diluted numeric(18,8);
|
||||
|
||||
alter table maintenance_fee
|
||||
drop column period;
|
||||
|
||||
alter table report_summary
|
||||
drop column authorize_loss,
|
||||
drop column authorize_loss_fee,
|
||||
drop column authorize_loss_proportion;
|
||||
@@ -1,37 +0,0 @@
|
||||
alter table meter_04kv
|
||||
drop column dilute;
|
||||
|
||||
alter table end_user_detail
|
||||
drop column dilute,
|
||||
drop column maintenance_fee_diluted,
|
||||
drop column public_consumption_diluted;
|
||||
|
||||
alter table maintenance_fee
|
||||
add column period varchar(10);
|
||||
|
||||
alter table report_summary
|
||||
drop column customer_consumption,
|
||||
drop column customer_consumption_fee,
|
||||
drop column customer_consumption_critical,
|
||||
drop column customer_consumption_critical_fee,
|
||||
drop column customer_consumption_peak,
|
||||
drop column customer_consumption_peak_fee,
|
||||
drop column customer_consumption_flat,
|
||||
drop column customer_consumption_flat_fee,
|
||||
drop column customer_consumption_valley,
|
||||
drop column customer_consumption_valley_fee,
|
||||
drop column public_consumption,
|
||||
drop column public_consumption_fee,
|
||||
drop column public_consumption_critical,
|
||||
drop column public_consumption_critical_fee,
|
||||
drop column public_consumption_peak,
|
||||
drop column public_consumption_peak_fee,
|
||||
drop column public_consumption_flat,
|
||||
drop column public_consumption_flat_fee,
|
||||
drop column public_consumption_valley,
|
||||
drop column public_consumption_valley_fee,
|
||||
drop column public_consumption_proportion,
|
||||
drop column diluteds,
|
||||
add column authorize_loss numeric(14,2),
|
||||
add column authorize_loss_fee numeric(16,2),
|
||||
add column authorize_loss_proportion numeric(16,15);
|
||||
@@ -1,21 +0,0 @@
|
||||
package migration
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/logger"
|
||||
"embed"
|
||||
|
||||
"github.com/uptrace/bun/migrate"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
//go:embed *.sql
|
||||
sqlMigrations embed.FS
|
||||
Migrations = migrate.NewMigrations()
|
||||
)
|
||||
|
||||
func init() {
|
||||
if err := Migrations.Discover(sqlMigrations); err != nil {
|
||||
logger.Named("Migrations").Fatal("Unable to load migrations.", zap.Error(err))
|
||||
}
|
||||
}
|
||||
214
model/calculate/calculate.go
Normal file
214
model/calculate/calculate.go
Normal file
@@ -0,0 +1,214 @@
|
||||
package calculate
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/types"
|
||||
"fmt"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type Reading struct {
|
||||
ReadAt types.DateTime
|
||||
Ratio decimal.Decimal
|
||||
Overall decimal.Decimal
|
||||
Critical decimal.Decimal
|
||||
Peak decimal.Decimal
|
||||
Flat decimal.Decimal
|
||||
Valley decimal.Decimal
|
||||
}
|
||||
|
||||
type Pooling struct {
|
||||
Code string
|
||||
Detail model.ConsumptionUnit
|
||||
}
|
||||
|
||||
type Meter struct {
|
||||
Code string `json:"code"`
|
||||
Detail model.MeterDetail `json:"detail"`
|
||||
CoveredArea decimal.Decimal `json:"covered_area"`
|
||||
LastTermReading *Reading `json:"last_term_reading"`
|
||||
CurrentTermReading *Reading `json:"current_term_reading"`
|
||||
Overall model.ConsumptionUnit `json:"overall"`
|
||||
Critical model.ConsumptionUnit `json:"critical"`
|
||||
Peak model.ConsumptionUnit `json:"peak"`
|
||||
Flat model.ConsumptionUnit `json:"flat"`
|
||||
Valley model.ConsumptionUnit `json:"valley"`
|
||||
AdjustLoss model.ConsumptionUnit `json:"adjust_loss"`
|
||||
PooledBasic model.ConsumptionUnit `json:"pooled_basic"`
|
||||
PooledAdjust model.ConsumptionUnit `json:"pooled_adjust"`
|
||||
PooledLoss model.ConsumptionUnit `json:"pooled_loss"`
|
||||
PooledPublic model.ConsumptionUnit `json:"pooled_public"`
|
||||
SharedPoolingProportion decimal.Decimal `json:"shared_pooling_proportion"`
|
||||
Poolings []*Pooling `json:"poolings"`
|
||||
}
|
||||
|
||||
type PrimaryTenementStatistics struct {
|
||||
Tenement model.Tenement
|
||||
Meters []Meter
|
||||
}
|
||||
|
||||
type TenementCharge struct {
|
||||
Tenement string `json:"tenement"`
|
||||
Overall model.ConsumptionUnit `json:"overall"`
|
||||
Critical model.ConsumptionUnit `json:"critical"`
|
||||
Peak model.ConsumptionUnit `json:"peak"`
|
||||
Flat model.ConsumptionUnit `json:"flat"`
|
||||
Valley model.ConsumptionUnit `json:"valley"`
|
||||
BasicFee decimal.Decimal `json:"basic_fee"`
|
||||
AdjustFee decimal.Decimal `json:"adjust_fee"`
|
||||
LossPooled decimal.Decimal `json:"loss_pooled"`
|
||||
PublicPooled decimal.Decimal `json:"public_pooled"`
|
||||
FinalCharges decimal.Decimal `json:"final_charges"`
|
||||
Loss model.ConsumptionUnit `json:"loss"`
|
||||
Submeters []*Meter `json:"submeters"`
|
||||
Poolings []*Meter `json:"poolings"`
|
||||
}
|
||||
|
||||
type Summary struct {
|
||||
ReportId string
|
||||
OverallArea decimal.Decimal
|
||||
Overall model.ConsumptionUnit
|
||||
ConsumptionFee decimal.Decimal
|
||||
Critical model.ConsumptionUnit
|
||||
Peak model.ConsumptionUnit
|
||||
Flat model.ConsumptionUnit
|
||||
Valley model.ConsumptionUnit
|
||||
Loss decimal.Decimal
|
||||
LossFee decimal.Decimal
|
||||
LossProportion decimal.Decimal
|
||||
AuthoizeLoss model.ConsumptionUnit
|
||||
BasicFee decimal.Decimal
|
||||
BasicPooledPriceConsumption decimal.Decimal
|
||||
BasicPooledPriceArea decimal.Decimal
|
||||
AdjustFee decimal.Decimal
|
||||
AdjustPooledPriceConsumption decimal.Decimal
|
||||
AdjustPooledPriceArea decimal.Decimal
|
||||
LossDilutedPrice decimal.Decimal
|
||||
TotalConsumption decimal.Decimal
|
||||
FinalDilutedOverall decimal.Decimal
|
||||
}
|
||||
|
||||
type PoolingSummary struct {
|
||||
Tenement string
|
||||
Meter string
|
||||
TargetMeter string
|
||||
Area decimal.NullDecimal
|
||||
OverallAmount decimal.Decimal
|
||||
PoolingProportion decimal.Decimal
|
||||
}
|
||||
|
||||
func FromReportSummary(summary *model.ReportSummary, pricingMode *model.ReportIndex) Summary {
|
||||
var parkPrice float64
|
||||
switch pricingMode.PricePolicy {
|
||||
case model.PRICING_POLICY_CONSUMPTION:
|
||||
parkPrice = summary.ConsumptionFee.Decimal.InexactFloat64() / summary.Overall.Amount.InexactFloat64()
|
||||
case model.PRICING_POLICY_ALL:
|
||||
parkPrice = summary.Overall.Fee.InexactFloat64() / summary.Overall.Amount.InexactFloat64()
|
||||
default:
|
||||
fmt.Println("无法识别类型")
|
||||
}
|
||||
|
||||
flatAmount := summary.Overall.Amount.InexactFloat64() -
|
||||
summary.Critical.Amount.InexactFloat64() -
|
||||
summary.Peak.Amount.InexactFloat64() -
|
||||
summary.Valley.Amount.InexactFloat64()
|
||||
|
||||
flatFee := summary.Overall.Amount.InexactFloat64() -
|
||||
summary.Critical.Fee.InexactFloat64() -
|
||||
summary.Peak.Fee.InexactFloat64() -
|
||||
summary.Valley.Fee.InexactFloat64()
|
||||
|
||||
var OverallPrice float64
|
||||
if summary.Overall.Amount.GreaterThan(decimal.Zero) {
|
||||
OverallPrice = parkPrice
|
||||
} else {
|
||||
OverallPrice = decimal.Zero.InexactFloat64()
|
||||
}
|
||||
|
||||
var CriticalPrice float64
|
||||
if summary.Critical.Amount.GreaterThan(decimal.Zero) {
|
||||
CriticalPrice = summary.Critical.Fee.InexactFloat64() / summary.Critical.Amount.InexactFloat64()
|
||||
} else {
|
||||
CriticalPrice = decimal.Zero.InexactFloat64()
|
||||
}
|
||||
|
||||
var PeakPrice float64
|
||||
if summary.Peak.Amount.GreaterThan(decimal.Zero) {
|
||||
PeakPrice = summary.Peak.Fee.InexactFloat64() / summary.Peak.Amount.InexactFloat64()
|
||||
} else {
|
||||
PeakPrice = decimal.Zero.InexactFloat64()
|
||||
}
|
||||
|
||||
var FlatPrice float64
|
||||
if decimal.NewFromFloat(flatAmount).GreaterThan(decimal.Zero) {
|
||||
FlatPrice = flatFee / flatAmount
|
||||
} else {
|
||||
FlatPrice = decimal.Zero.InexactFloat64()
|
||||
}
|
||||
|
||||
var ValleyPrice float64
|
||||
if summary.Valley.Amount.GreaterThan(decimal.Zero) {
|
||||
ValleyPrice = summary.Valley.Fee.InexactFloat64() / summary.Valley.Amount.InexactFloat64()
|
||||
} else {
|
||||
ValleyPrice = decimal.Zero.InexactFloat64()
|
||||
}
|
||||
|
||||
var LossDilutedPrice float64
|
||||
if summary.Overall.Amount.GreaterThan(decimal.Zero) {
|
||||
LossDilutedPrice = parkPrice
|
||||
} else {
|
||||
LossDilutedPrice = decimal.Zero.InexactFloat64()
|
||||
}
|
||||
|
||||
_ = parkPrice
|
||||
|
||||
return Summary{
|
||||
ReportId: summary.ReportId,
|
||||
OverallArea: decimal.Zero,
|
||||
Overall: model.ConsumptionUnit{
|
||||
Amount: summary.Overall.Amount,
|
||||
Fee: summary.Overall.Fee,
|
||||
Price: decimal.NewFromFloat(OverallPrice),
|
||||
Proportion: decimal.NewFromFloat(1.0),
|
||||
},
|
||||
ConsumptionFee: summary.ConsumptionFee.Decimal,
|
||||
Critical: model.ConsumptionUnit{
|
||||
Amount: summary.Critical.Amount,
|
||||
Fee: summary.Critical.Fee,
|
||||
Price: decimal.NewFromFloat(CriticalPrice),
|
||||
Proportion: decimal.NewFromFloat(summary.Critical.Amount.InexactFloat64() / summary.Overall.Amount.InexactFloat64()),
|
||||
},
|
||||
Peak: model.ConsumptionUnit{
|
||||
Amount: summary.Peak.Amount,
|
||||
Fee: summary.Peak.Fee,
|
||||
Price: decimal.NewFromFloat(PeakPrice),
|
||||
Proportion: decimal.NewFromFloat(summary.Peak.Amount.InexactFloat64() / summary.Overall.Amount.InexactFloat64()),
|
||||
},
|
||||
Flat: model.ConsumptionUnit{
|
||||
Amount: decimal.NewFromFloat(flatAmount),
|
||||
Fee: decimal.NewFromFloat(flatFee),
|
||||
Price: decimal.NewFromFloat(FlatPrice),
|
||||
Proportion: decimal.NewFromFloat(flatAmount / summary.Overall.Amount.InexactFloat64()),
|
||||
},
|
||||
Valley: model.ConsumptionUnit{
|
||||
Amount: summary.Valley.Amount,
|
||||
Fee: summary.Valley.Fee,
|
||||
Price: decimal.NewFromFloat(ValleyPrice),
|
||||
Proportion: decimal.NewFromFloat(summary.Valley.Amount.InexactFloat64() / summary.Overall.Amount.InexactFloat64()),
|
||||
},
|
||||
Loss: decimal.Zero,
|
||||
LossFee: decimal.Zero,
|
||||
LossProportion: decimal.Zero,
|
||||
AuthoizeLoss: model.ConsumptionUnit{},
|
||||
BasicFee: summary.BasicFee,
|
||||
BasicPooledPriceConsumption: decimal.Zero,
|
||||
BasicPooledPriceArea: decimal.Zero,
|
||||
AdjustFee: summary.AdjustFee,
|
||||
AdjustPooledPriceConsumption: decimal.Zero,
|
||||
AdjustPooledPriceArea: decimal.Zero,
|
||||
LossDilutedPrice: decimal.NewFromFloat(LossDilutedPrice),
|
||||
TotalConsumption: decimal.Zero,
|
||||
FinalDilutedOverall: decimal.Zero,
|
||||
}
|
||||
}
|
||||
32
model/charge.go
Normal file
32
model/charge.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/types"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type UserChargeDetail struct {
|
||||
Seq int64 `json:"seq"`
|
||||
UserId string `json:"userId" db:"user_id"`
|
||||
Name string `json:"name"`
|
||||
Fee *float64 `json:"fee"`
|
||||
Discount *float64 `json:"discount"`
|
||||
Amount *float64 `json:"amount"`
|
||||
ChargeTo types.Date `json:"chargeTo"`
|
||||
Settled bool `json:"settled"`
|
||||
SettledAt *types.DateTime `json:"settledAt"`
|
||||
Cancelled bool `json:"cancelled"`
|
||||
CancelledAt *types.DateTime `json:"cancelledAt"`
|
||||
Refunded bool `json:"refunded"`
|
||||
RefundedAt *types.DateTime `json:"refundedAt"`
|
||||
CreatedAt types.DateTime `json:"createdAt"`
|
||||
}
|
||||
|
||||
type ChargeRecordCreationForm struct {
|
||||
UserId string `json:"userId"`
|
||||
Fee decimal.NullDecimal `json:"fee"`
|
||||
Discount decimal.NullDecimal `json:"discount"`
|
||||
Amount decimal.NullDecimal `json:"amount"`
|
||||
ChargeTo types.Date `json:"chargeTo"`
|
||||
}
|
||||
10
model/cunsumption.go
Normal file
10
model/cunsumption.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package model
|
||||
|
||||
import "github.com/shopspring/decimal"
|
||||
|
||||
type ConsumptionUnit struct {
|
||||
Amount decimal.Decimal `json:"amount"`
|
||||
Fee decimal.Decimal `json:"fee"`
|
||||
Price decimal.Decimal `json:"price"`
|
||||
Proportion decimal.Decimal `json:"proportion"`
|
||||
}
|
||||
@@ -1,133 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type EndUserDetail struct {
|
||||
bun.BaseModel `bun:"table:end_user_detail,alias:eud"`
|
||||
CreatedAndModified `bun:"extend"`
|
||||
ReportId string `bun:",pk,notnull" json:"reportId"`
|
||||
ParkId string `bun:",pk,notnull" json:"parkId"`
|
||||
MeterId string `bun:"meter_04kv_id,pk,notnull" json:"meterId"`
|
||||
Seq int64 `bun:"type:bigint,notnull" json:"seq"`
|
||||
Ratio decimal.Decimal `bun:"type:numeric,notnull" json:"ratio"`
|
||||
Address *string `json:"address"`
|
||||
CustomerName *string `json:"customerName"`
|
||||
ContactName *string `json:"contactName"`
|
||||
ContactPhone *string `json:"contactPhone"`
|
||||
IsPublicMeter bool `bun:"public_meter,notnull" json:"isPublicMeter"`
|
||||
LastPeriodOverall decimal.Decimal `bun:"type:numeric,notnull" json:"lastPeriodOverall"`
|
||||
LastPeriodCritical decimal.Decimal `bun:"type:numeric,notnull" json:"lastPeriodCritical"`
|
||||
LastPeriodPeak decimal.Decimal `bun:"type:numeric,notnull" json:"lastPeriodPeak"`
|
||||
LastPeriodFlat decimal.Decimal `bun:"type:numeric,notnull" json:"lastPeriodFlat"`
|
||||
LastPeriodValley decimal.Decimal `bun:"type:numeric,notnull" json:"lastPeriodValley"`
|
||||
CurrentPeriodOverall decimal.Decimal `bun:"type:numeric,notnull" json:"currentPeriodOverall"`
|
||||
CurrentPeriodCritical decimal.Decimal `bun:"type:numeric,notnull" json:"currentPeriodCritical"`
|
||||
CurrentPeriodPeak decimal.Decimal `bun:"type:numeric,notnull" json:"currentPeriodPeak"`
|
||||
CurrentPeriodFlat decimal.Decimal `bun:"type:numeric,notnull" json:"currentPeriodFlat"`
|
||||
CurrentPeriodValley decimal.Decimal `bun:"type:numeric,notnull" json:"currentPeriodValley"`
|
||||
AdjustOverall decimal.Decimal `bun:"type:numeric,notnull" json:"adjustOverall"`
|
||||
AdjustCritical decimal.Decimal `bun:"type:numeric,notnull" json:"adjustCritical"`
|
||||
AdjustPeak decimal.Decimal `bun:"type:numeric,notnull" json:"adjustPeak"`
|
||||
AdjustFlat decimal.Decimal `bun:"type:numeric,notnull" json:"adjustFlat"`
|
||||
AdjustValley decimal.Decimal `bun:"type:numeric,notnull" json:"adjustValley"`
|
||||
Overall decimal.NullDecimal `bun:"type:numeric" json:"overall"`
|
||||
OverallFee decimal.NullDecimal `bun:"type:numeric" json:"overallFee"`
|
||||
OverallProportion decimal.Decimal `bun:"type:numeric,notnull" json:"-"`
|
||||
Critical decimal.NullDecimal `bun:"type:numeric" json:"critical"`
|
||||
CriticalFee decimal.NullDecimal `bun:"type:numeric" json:"criticalFee"`
|
||||
Peak decimal.NullDecimal `bun:"type:numeric" json:"peak"`
|
||||
PeakFee decimal.NullDecimal `bun:"type:numeric" json:"peakFee"`
|
||||
Flat decimal.NullDecimal `bun:"type:numeric" json:"flat"`
|
||||
FlatFee decimal.NullDecimal `bun:"type:numeric" json:"flatFee"`
|
||||
Valley decimal.NullDecimal `bun:"type:numeric" json:"valley"`
|
||||
ValleyFee decimal.NullDecimal `bun:"type:numeric" json:"valleyFee"`
|
||||
BasicFeeDiluted decimal.NullDecimal `bun:"type:numeric" json:"basicFeeDiluted"`
|
||||
AdjustFeeDiluted decimal.NullDecimal `bun:"type:numeric" json:"adjustFeeDiluted"`
|
||||
LossDiluted decimal.NullDecimal `bun:"type:numeric" json:"lossDiluted"`
|
||||
LossFeeDiluted decimal.NullDecimal `bun:"type:numeric" json:"lossFeeDiluted"`
|
||||
FinalDiluted decimal.NullDecimal `bun:"type:numeric" json:"finalDiluted"`
|
||||
FinalCharge decimal.NullDecimal `bun:"type:numeric" json:"finalCharge"`
|
||||
Initialize bool `bun:"-" json:"-"`
|
||||
Origin *Meter04KV `bun:"rel:belongs-to,join:park_id=park_id,join:meter_04kv_id=code" json:"-"`
|
||||
Report *Report `bun:"rel:belongs-to,join:report_id=id" json:"-"`
|
||||
Park *Park `bun:"rel:belongs-to,join:park_id=id" json:"-"`
|
||||
}
|
||||
|
||||
var _ bun.BeforeAppendModelHook = (*EndUserDetail)(nil)
|
||||
|
||||
func (d *EndUserDetail) BeforeAppendModel(ctx context.Context, query bun.Query) error {
|
||||
oprTime := time.Now()
|
||||
switch query.(type) {
|
||||
case *bun.InsertQuery:
|
||||
d.CreatedAt = oprTime
|
||||
d.LastModifiedAt = &oprTime
|
||||
case *bun.UpdateQuery:
|
||||
d.LastModifiedAt = &oprTime
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d EndUserDetail) Validate() (bool, error) {
|
||||
lastPeriodSum := decimal.Sum(d.LastPeriodCritical, d.LastPeriodPeak, d.LastPeriodValley)
|
||||
if lastPeriodSum.GreaterThan(d.LastPeriodOverall) {
|
||||
return false, errors.New("上期峰谷计量总量大于上期总计电量")
|
||||
}
|
||||
currentPeriodSum := decimal.Sum(d.CurrentPeriodCritical, d.CurrentPeriodPeak, d.CurrentPeriodValley)
|
||||
if currentPeriodSum.GreaterThan(d.CurrentPeriodOverall) {
|
||||
return false, errors.New("本期峰谷计量总量大于本期总计电量")
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (d *EndUserDetail) CalculatePeriod() {
|
||||
d.LastPeriodFlat = d.LastPeriodOverall.Sub(d.LastPeriodCritical).Sub(d.LastPeriodPeak).Sub(d.LastPeriodValley)
|
||||
d.CurrentPeriodFlat = d.CurrentPeriodOverall.Sub(d.CurrentPeriodCritical).Sub(d.CurrentPeriodPeak).Sub(d.CurrentPeriodValley)
|
||||
d.Overall = decimal.NewNullDecimal(d.CurrentPeriodOverall.Sub(d.LastPeriodOverall).Mul(d.Ratio).Add(d.AdjustOverall).RoundBank(2))
|
||||
d.Critical = decimal.NewNullDecimal(d.CurrentPeriodCritical.Sub(d.LastPeriodCritical).Mul(d.Ratio).Add(d.AdjustCritical).RoundBank(2))
|
||||
d.Peak = decimal.NewNullDecimal(d.CurrentPeriodPeak.Sub(d.LastPeriodPeak).Mul(d.Ratio).Add(d.AdjustPeak).RoundBank(2))
|
||||
d.Flat = decimal.NewNullDecimal(d.CurrentPeriodFlat.Sub(d.LastPeriodFlat).Mul(d.Ratio).Add(d.AdjustFlat).RoundBank(2))
|
||||
d.Valley = decimal.NewNullDecimal(d.CurrentPeriodValley.Sub(d.LastPeriodValley).Mul(d.Ratio).Add(d.AdjustValley).RoundBank(2))
|
||||
}
|
||||
|
||||
type EndUserImport struct {
|
||||
MeterId string `excel:"meterId"`
|
||||
LastPeriodOverall decimal.Decimal `excel:"lastPeriodOverall"`
|
||||
CurrentPeriodOverall decimal.Decimal `excel:"currentPeriodOverall"`
|
||||
LastPeriodCritical decimal.NullDecimal `excel:"lastPeriodCritical"`
|
||||
LastPeriodPeak decimal.NullDecimal `excel:"lastPeriodPeak"`
|
||||
LastPeriodValley decimal.NullDecimal `excel:"lastPeriodValley"`
|
||||
CurrentPeriodCritical decimal.NullDecimal `excel:"currentPeriodCritical"`
|
||||
CurrentPeriodPeak decimal.NullDecimal `excel:"currentPeriodPeak"`
|
||||
CurrentPeriodValley decimal.NullDecimal `excel:"currentPeriodValley"`
|
||||
AdjustOverall decimal.Decimal `excel:"adjustOverall"`
|
||||
AdjustCritical decimal.NullDecimal `excel:"adjustCritical"`
|
||||
AdjustPeak decimal.NullDecimal `excel:"adjustPeak"`
|
||||
AdjustFlat decimal.NullDecimal `excel:"adjustFlat"`
|
||||
AdjustValley decimal.NullDecimal `excel:"adjustValley"`
|
||||
}
|
||||
|
||||
type EndUserPeriodStat struct {
|
||||
CustomerName string `json:"customerName"`
|
||||
Address string `json:"address"`
|
||||
ParkId string `json:"parkId"`
|
||||
MeterId string `bun:"meter_04kv_id" json:"meterId"`
|
||||
IsPublicMeter bool `bun:"public_meter" json:"isPublicMeter"`
|
||||
Kind int8 `bun:"-" json:"pvKind"`
|
||||
Overall decimal.NullDecimal `json:"overall"`
|
||||
Critical decimal.NullDecimal `json:"critical"`
|
||||
Peak decimal.NullDecimal `json:"peak"`
|
||||
Valley decimal.NullDecimal `json:"valley"`
|
||||
OverallFee decimal.NullDecimal `json:"overallFee"`
|
||||
CriticalFee decimal.NullDecimal `json:"criticalFee"`
|
||||
PeakFee decimal.NullDecimal `json:"peakFee"`
|
||||
ValleyFee decimal.NullDecimal `json:"valleyFee"`
|
||||
AdjustFee decimal.NullDecimal `bun:"final_diluted" json:"adjustFee"`
|
||||
AdjustProportion decimal.NullDecimal `bun:"-" json:"adjustProportion"`
|
||||
}
|
||||
90
model/enums.go
Normal file
90
model/enums.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
ELECTRICITY_CATE_TWO_PART int16 = iota
|
||||
ELECTRICITY_CATE_UNITARY_PV
|
||||
ELECTRICITY_CATE_FULL_PV
|
||||
)
|
||||
|
||||
const (
|
||||
METER_TYPE_UNITARY int16 = iota
|
||||
METER_TYPE_PV
|
||||
)
|
||||
|
||||
const (
|
||||
METER_INSTALLATION_TENEMENT int16 = iota
|
||||
METER_INSTALLATION_PARK
|
||||
METER_INSTALLATION_POOLING
|
||||
)
|
||||
|
||||
func ParseMeterInstallationType(s string) (int16, error) {
|
||||
switch {
|
||||
case strings.Contains(s, "商户"):
|
||||
return METER_INSTALLATION_TENEMENT, nil
|
||||
case strings.Contains(s, "公共"):
|
||||
return METER_INSTALLATION_PARK, nil
|
||||
case strings.Contains(s, "楼道"):
|
||||
return METER_INSTALLATION_POOLING, nil
|
||||
default:
|
||||
return -1, fmt.Errorf("提供了一个无法识别的表计类型: %s", s)
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
PRICING_POLICY_CONSUMPTION int16 = iota
|
||||
PRICING_POLICY_ALL
|
||||
)
|
||||
|
||||
const (
|
||||
POOLING_MODE_NONE int16 = iota
|
||||
POOLING_MODE_CONSUMPTION
|
||||
POOLING_MODE_AREA
|
||||
)
|
||||
|
||||
const (
|
||||
PAYMENT_CASH int16 = iota
|
||||
PAYMENT_BANK_CARD
|
||||
PAYMENT_ALIPAY
|
||||
PAYMENT_WECHAT
|
||||
PAYMENT_UNION_PAY
|
||||
PAYMENT_OTHER int16 = 99
|
||||
)
|
||||
|
||||
const (
|
||||
METER_TELEMETER_HYBRID int16 = iota
|
||||
METER_TELEMETER_AUTOMATIC
|
||||
METER_TELEMETER_MANUAL
|
||||
)
|
||||
|
||||
const (
|
||||
RETRY_INTERVAL_ALGORITHM_EXPONENTIAL_BACKOFF int16 = iota
|
||||
RETRY_INTERVAL_ALGORITHM_DOUBLE_LINEAR_BACKOFF
|
||||
RETRY_INTERVAL_ALGORITHM_TRIPLE_LINEAR_BACKOFF
|
||||
RETRY_INTERVAL_ALGORITHM_FIXED
|
||||
)
|
||||
|
||||
const (
|
||||
TAX_METHOD_INCLUSIVE int16 = iota
|
||||
TAX_METHOD_EXCLUSIVE
|
||||
)
|
||||
|
||||
const (
|
||||
REPORT_CALCULATE_TASK_STATUS_PENDING int16 = iota
|
||||
REPORT_CALCULATE_TASK_STATUS_SUCCESS
|
||||
REPORT_CALCULATE_TASK_STATUS_INSUFICIENT_DATA
|
||||
REPORT_CALCULATE_TASK_STATUS_SUSPENDED
|
||||
REPORT_CALCULATE_TASK_STATUS_UNKNOWN_ERROR
|
||||
REPORT_CALCULATE_TASK_STATUS_UNEXISTS = 99
|
||||
)
|
||||
|
||||
const (
|
||||
REPORT_WITHDRAW_NON int16 = iota
|
||||
REPORT_WITHDRAW_APPLYING
|
||||
REPORT_WITHDRAW_DENIED
|
||||
REPORT_WITHDRAW_GRANTED
|
||||
)
|
||||
45
model/invoice.go
Normal file
45
model/invoice.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/tools"
|
||||
"electricity_bill_calc/types"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type InvoiceTitle struct {
|
||||
Name string `json:"name"`
|
||||
USCI string `json:"usci"`
|
||||
Address string `json:"address"`
|
||||
Phone string `json:"phone"`
|
||||
Bank string `json:"bank"`
|
||||
Account string `json:"account"`
|
||||
}
|
||||
|
||||
type InvoiceCargo struct {
|
||||
Name string `json:"name"`
|
||||
Price decimal.Decimal `json:"price"`
|
||||
Unit string `json:"unit"`
|
||||
Quantity decimal.Decimal `json:"quantity"`
|
||||
TaxRate decimal.Decimal `json:"taxRate"`
|
||||
Tax decimal.Decimal `json:"tax"`
|
||||
Total decimal.Decimal `json:"total"`
|
||||
}
|
||||
|
||||
type Invoice struct {
|
||||
InvoiceNo string `json:"invoiceNo"`
|
||||
Park string `json:"parkId" db:"park_id"`
|
||||
Tenement string `json:"tenementId" db:"tenement_id"`
|
||||
InvoiceType *string `json:"type" db:"type"`
|
||||
Info InvoiceTitle `json:"invoiceInfo" db:"invoice_info"`
|
||||
Cargos []InvoiceCargo `json:"cargos"`
|
||||
TaxRate decimal.Decimal `json:"taxRate" db:"tax_rate"`
|
||||
TaxMethod int16 `json:"taxMethod" db:"tax_method"`
|
||||
Total decimal.Decimal `json:"total" db:"total"`
|
||||
IssuedAt types.DateTime `json:"issuedAt" db:"issued_at"`
|
||||
Covers []string `json:"covers"`
|
||||
}
|
||||
|
||||
func (i Invoice) Type() string {
|
||||
return tools.DefaultOrEmptyStr(i.InvoiceType, "")
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type MaintenanceFee struct {
|
||||
bun.BaseModel `bun:"table:maintenance_fee,alias:m"`
|
||||
CreatedAndModified `bun:"extend"`
|
||||
Deleted `bun:"extend"`
|
||||
Id string `bun:",pk,notnull" json:"id"`
|
||||
ParkId string `bun:",notnull" json:"parkId"`
|
||||
Name string `bun:",notnull" json:"name"`
|
||||
Period string `bun:",notnull" json:"period"`
|
||||
Fee decimal.Decimal `bun:"type:numeric,notnull" json:"fee"`
|
||||
Memo *string `bun:"type:text" json:"memo"`
|
||||
Enabled bool `bun:",notnull" json:"enabled"`
|
||||
Park Park `bun:"rel:belongs-to,join:park_id=id"`
|
||||
}
|
||||
|
||||
var _ bun.BeforeAppendModelHook = (*MaintenanceFee)(nil)
|
||||
|
||||
func (f *MaintenanceFee) BeforeAppendModel(ctx context.Context, query bun.Query) error {
|
||||
oprTime := time.Now()
|
||||
switch query.(type) {
|
||||
case *bun.InsertQuery:
|
||||
f.CreatedAt = oprTime
|
||||
f.LastModifiedAt = &oprTime
|
||||
case *bun.UpdateQuery:
|
||||
f.LastModifiedAt = &oprTime
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type AdditionalCharge struct {
|
||||
ParkId string `json:"parkId"`
|
||||
Period string `json:"period"`
|
||||
Fee decimal.Decimal `json:"fee"`
|
||||
Price decimal.Decimal `json:"price"`
|
||||
QuarterPrice decimal.Decimal `json:"quarterPrice"`
|
||||
SemiAnnualPrice decimal.Decimal `json:"semiAnnualPrice"`
|
||||
Enterprise UserDetailSimplified `json:"user"`
|
||||
Park Park `json:"park"`
|
||||
}
|
||||
128
model/meter.go
Normal file
128
model/meter.go
Normal file
@@ -0,0 +1,128 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/types"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type MeterDetail struct {
|
||||
Code string `json:"code" db:"code"`
|
||||
Park string `json:"parkId" db:"park_id"`
|
||||
Address *string `json:"address" db:"address"`
|
||||
MeterType int16 `json:"type" db:"meter_type"`
|
||||
Building *string `json:"building" db:"building"`
|
||||
BuildingName *string `json:"buildingName" db:"building_name"`
|
||||
OnFloor *string `json:"onFloor" db:"on_floor" `
|
||||
Area decimal.NullDecimal `json:"area" db:"area"`
|
||||
Ratio decimal.Decimal `json:"ratio" db:"ratio"`
|
||||
Seq int64 `json:"seq" db:"seq"`
|
||||
Enabled bool `json:"enabled" db:"enabled"`
|
||||
AttachedAt *types.DateTime `json:"attachedAt" db:"attached_at"`
|
||||
DetachedAt *types.DateTime `json:"detachedAt" db:"detached_at"`
|
||||
CreatedAt types.DateTime `json:"createdAt" db:"created_at"`
|
||||
LastModifiedAt types.DateTime `json:"lastModifiedAt" db:"last_modified_at"`
|
||||
DisplayRatio float64 `json:"display_ratio" db:"display_ratio"`
|
||||
}
|
||||
|
||||
type MeterRelation struct {
|
||||
Id string `json:"id"`
|
||||
Park string `json:"parkId" db:"park_id"`
|
||||
MasterMeter string `json:"masterMeterId" db:"master_meter_id"`
|
||||
SlaveMeter string `json:"slaveMeterId" db:"slave_meter_id"`
|
||||
EstablishedAt types.DateTime `json:"establishedAt"`
|
||||
SuspendedAt *types.DateTime `json:"suspendedAt"`
|
||||
RevokedAt *types.DateTime `json:"revokedAt"`
|
||||
}
|
||||
|
||||
type MeterSynchronization struct {
|
||||
Park string `json:"parkId" db:"park_id"`
|
||||
Meter string `json:"meterId" db:"meter_id"`
|
||||
ForeignMeter string `json:"foreignMeter"`
|
||||
SystemType string `json:"systemType"`
|
||||
SystemIdentity string `json:"systemIdentity"`
|
||||
Enabled bool `json:"enabled"`
|
||||
LastSynchronizedAt types.DateTime `json:"lastSynchronizedAt" db:"last_synchronized_at"`
|
||||
RevokeAt *types.DateTime `json:"revokeAt" db:"revoke_at"`
|
||||
}
|
||||
|
||||
type SimpleMeterDocument struct {
|
||||
Code string `json:"code"`
|
||||
Seq int64 `json:"seq"`
|
||||
Address *string `json:"address"`
|
||||
Ratio decimal.Decimal `json:"ratio"`
|
||||
TenementName *string `json:"tenementName"`
|
||||
}
|
||||
|
||||
type NestedMeter struct {
|
||||
MeterId string `json:"meterId"`
|
||||
MeterDetail MeterDetail `json:"meterDetail"`
|
||||
LastTermReadings Reading `json:"lastTermReadings"`
|
||||
CurrentTermReadings Reading `json:"currentTermReadings"`
|
||||
Overall ConsumptionUnit `json:"overall"`
|
||||
Critical ConsumptionUnit `json:"critical"`
|
||||
Peak ConsumptionUnit `json:"peak"`
|
||||
Flat ConsumptionUnit `json:"flat"`
|
||||
Valley ConsumptionUnit `json:"valley"`
|
||||
BasicPooled decimal.Decimal `json:"basicPooled"`
|
||||
AdjustPooled decimal.Decimal `json:"adjustPooled"`
|
||||
LossPooled decimal.Decimal `json:"lossPooled"`
|
||||
PublicPooled decimal.Decimal `json:"publicPooled"`
|
||||
FinalTotal decimal.Decimal `json:"finalTotal"`
|
||||
Area decimal.Decimal `json:"area"`
|
||||
Proportion decimal.Decimal `json:"proportion"`
|
||||
}
|
||||
|
||||
type PooledMeterDetailCompound struct {
|
||||
MeterDetail
|
||||
BindMeters []MeterDetail `json:"bindedMeters"`
|
||||
}
|
||||
|
||||
// 以下结构体用于导入表计档案数据
|
||||
type MeterImportRow struct {
|
||||
Code string `json:"code" excel:"code"`
|
||||
Address *string `json:"address" excel:"address"`
|
||||
MeterType *string `json:"meterType" excel:"meterType"`
|
||||
Building *string `json:"building" excel:"building"`
|
||||
OnFloor *string `json:"onFloor" excel:"onFloor"`
|
||||
Area decimal.NullDecimal `json:"area" excel:"area"`
|
||||
Ratio decimal.Decimal `json:"ratio" excel:"ratio"`
|
||||
Seq int64 `json:"seq" excel:"seq"`
|
||||
ReadAt types.DateTime `json:"readAt" excel:"readAt"`
|
||||
Overall decimal.Decimal `json:"overall" excel:"overall"`
|
||||
Critical decimal.NullDecimal `json:"critical" excel:"critical"`
|
||||
Peak decimal.NullDecimal `json:"peak" excel:"peak"`
|
||||
Flat decimal.NullDecimal `json:"flat" excel:"flat"`
|
||||
Valley decimal.NullDecimal `json:"valley" excel:"valley"`
|
||||
}
|
||||
|
||||
// 以下结构体用于导入表计抄表数据
|
||||
type ReadingImportRow struct {
|
||||
Code string `json:"code" excel:"code"`
|
||||
ReadAt types.DateTime `json:"readAt" excel:"readAt"`
|
||||
Overall decimal.Decimal `json:"overall" excel:"overall"`
|
||||
Critical decimal.NullDecimal `json:"critical" excel:"critical"`
|
||||
Peak decimal.NullDecimal `json:"peak" excel:"peak"`
|
||||
Valley decimal.NullDecimal `json:"valley" excel:"valley"`
|
||||
}
|
||||
|
||||
type ReadAbleMeter struct {
|
||||
CreatedAt types.DateTime `json:"createdAt" db:"created_at"`
|
||||
LastModifiedAt types.DateTime `json:"lastModifiedAt" db:"last_modified_at"`
|
||||
Code string `json:"code" db:"code"`
|
||||
|
||||
Park string `json:"parkId" db:"park_id"`
|
||||
Address *string `json:"address" db:"address"`
|
||||
Ratio decimal.Decimal `json:"ratio" db:"ratio"`
|
||||
Seq int64 `json:"seq" db:"seq"`
|
||||
Enabled bool `json:"enabled" db:"enabled"`
|
||||
MeterType int16 `json:"type" db:"meter_type"`
|
||||
Building *string `json:"building" db:"building"`
|
||||
|
||||
OnFloor *string `json:"onFloor" db:"on_floor" `
|
||||
Area decimal.NullDecimal `json:"area" db:"area"`
|
||||
AttachedAt *types.DateTime `json:"attachedAt" db:"attached_at"`
|
||||
DetachedAt *types.DateTime `json:"detachedAt" db:"detached_at"`
|
||||
DisplayRatio decimal.Decimal `db:"display_ratio"`
|
||||
BuildingName *string `db:"building_name"`
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type Meter04KV struct {
|
||||
bun.BaseModel `bun:"table:meter_04kv,alias:mt"`
|
||||
CreatedAndModified `bun:"extend"`
|
||||
Code string `bun:",pk,notnull" json:"code" excel:"code"`
|
||||
ParkId string `bun:",pk,notnull" json:"parkId"`
|
||||
Address *string `json:"address" excel:"address"`
|
||||
CustomerName *string `json:"customerName" excel:"name"`
|
||||
ContactName *string `json:"contactName" excel:"contact"`
|
||||
ContactPhone *string `json:"contactPhone" excel:"phone"`
|
||||
Ratio decimal.Decimal `bun:"type:numeric,notnull" json:"ratio" excel:"ratio"`
|
||||
Seq int64 `bun:"type:bigint,notnull" json:"seq" excel:"seq"`
|
||||
IsPublicMeter bool `bun:"public_meter,notnull" json:"isPublicMeter" excel:"public"`
|
||||
Enabled bool `bun:",notnull" json:"enabled"`
|
||||
ParkDetail *Park `bun:"rel:belongs-to,join:park_id=id" json:"-"`
|
||||
}
|
||||
|
||||
var _ bun.BeforeAppendModelHook = (*Meter04KV)(nil)
|
||||
|
||||
func (m *Meter04KV) BeforeAppendModel(ctx context.Context, query bun.Query) error {
|
||||
oprTime := time.Now()
|
||||
switch query.(type) {
|
||||
case *bun.InsertQuery:
|
||||
m.CreatedAt = oprTime
|
||||
m.LastModifiedAt = &oprTime
|
||||
case *bun.UpdateQuery:
|
||||
m.LastModifiedAt = &oprTime
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,89 +1,42 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"electricity_bill_calc/types"
|
||||
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
const (
|
||||
CATEGORY_TWO_PART int8 = iota
|
||||
CATEGORY_SINGLE_PV
|
||||
CATEGORY_SINGLE_NON_PV
|
||||
)
|
||||
|
||||
const (
|
||||
CUSTOMER_METER_NON_PV int8 = iota
|
||||
CUSTOMER_METER_PV
|
||||
)
|
||||
|
||||
type Park struct {
|
||||
bun.BaseModel `bun:"table:park,alias:p"`
|
||||
CreatedAndModified `bun:"extend"`
|
||||
Deleted `bun:"extend"`
|
||||
Id string `bun:",pk,notnull" json:"id"`
|
||||
UserId string `bun:",notnull" json:"userId"`
|
||||
Name string `bun:",notnull" json:"name"`
|
||||
Abbr *string `json:"abbr"`
|
||||
Area decimal.NullDecimal `bun:"type:numeric" json:"area"`
|
||||
TenementQuantity decimal.NullDecimal `bun:"type:numeric" json:"tenement"`
|
||||
Capacity decimal.NullDecimal `bun:"type:numeric" json:"capacity"`
|
||||
Category int8 `bun:"type:smallint,notnull" json:"category"`
|
||||
SubmeterType int8 `bun:"meter_04kv_type,type:smallint,notnull" json:"meter04kvType"`
|
||||
Region *string `json:"region"`
|
||||
Address *string `json:"address"`
|
||||
Contact *string `json:"contact"`
|
||||
Phone *string `json:"phone"`
|
||||
Enabled bool `bun:",notnull" json:"enabled"`
|
||||
EnterpriseIndex *User `bun:"rel:belongs-to,join:user_id=id" json:"-"`
|
||||
Enterprise *UserDetail `bun:"rel:belongs-to,join:user_id=id" json:"-"`
|
||||
MaintenanceFees []*MaintenanceFee `bun:"rel:has-many,join:id=park_id" json:"-"`
|
||||
Meters []*Meter04KV `bun:"rel:has-many,join:id=park_id" json:"-"`
|
||||
Reports []*Report `bun:"rel:has-many,join:id=park_id" json:"-"`
|
||||
}
|
||||
|
||||
type ParkSimplified struct {
|
||||
bun.BaseModel `bun:"table:park,alias:p"`
|
||||
Id string `bun:",pk,notnull" json:"id"`
|
||||
UserId string `bun:",notnull" json:"userId"`
|
||||
Name string `bun:",notnull" json:"name"`
|
||||
Abbr *string `json:"abbr"`
|
||||
Id string `json:"id"`
|
||||
UserId string `json:"userId"`
|
||||
Name string `json:"name"`
|
||||
Abbr string `json:"-"`
|
||||
Area decimal.NullDecimal `json:"area"`
|
||||
TenementQuantity decimal.NullDecimal `json:"tenement"`
|
||||
Capacity decimal.NullDecimal `json:"capacity"`
|
||||
Category int8 `bun:"type:smallint,notnull" json:"category"`
|
||||
SubmeterType int8 `bun:"meter_04kv_type,type:smallint,notnull" json:"meter04kvType"`
|
||||
Category int16 `json:"category"`
|
||||
MeterType int16 `json:"meter04kvType" db:"meter_04kv_type"`
|
||||
PricePolicy int16 `json:"pricePolicy"`
|
||||
BasicPooled int16 `json:"basicDiluted"`
|
||||
AdjustPooled int16 `json:"adjustDiluted"`
|
||||
LossPooled int16 `json:"lossDiluted"`
|
||||
PublicPooled int16 `json:"publicDiluted"`
|
||||
TaxRate decimal.NullDecimal `json:"taxRate"`
|
||||
Region *string `json:"region"`
|
||||
Address *string `json:"address"`
|
||||
Contact *string `json:"contact"`
|
||||
Phone *string `json:"phone"`
|
||||
Enabled bool `json:"enabled"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
LastModifiedAt time.Time `json:"lastModifiedAt"`
|
||||
DeletedAt *time.Time `json:"deletedAt"`
|
||||
NormAuthorizedLossRate float64 `json:"normAuthorizedLoss"db:"norm_authorized_loss_rate"`
|
||||
}
|
||||
|
||||
type ParkPeriodStatistics struct {
|
||||
Id string `bun:"park__id,notnull" json:"id"`
|
||||
Name string `bun:"park__name,notnull" json:"name"`
|
||||
Period *Date `bun:"type:date" json:"period"`
|
||||
}
|
||||
|
||||
func FromPark(park Park) ParkSimplified {
|
||||
dest := ParkSimplified{}
|
||||
copier.Copy(&dest, park)
|
||||
return dest
|
||||
}
|
||||
|
||||
var _ bun.BeforeAppendModelHook = (*Park)(nil)
|
||||
|
||||
func (p *Park) BeforeAppendModel(ctx context.Context, query bun.Query) error {
|
||||
oprTime := time.Now()
|
||||
switch query.(type) {
|
||||
case *bun.InsertQuery:
|
||||
p.CreatedAt = oprTime
|
||||
p.LastModifiedAt = &oprTime
|
||||
case *bun.UpdateQuery:
|
||||
p.LastModifiedAt = &oprTime
|
||||
}
|
||||
return nil
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Period *types.DateRange
|
||||
}
|
||||
|
||||
14
model/park_building.go
Normal file
14
model/park_building.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
type ParkBuilding struct {
|
||||
Id string `json:"id"`
|
||||
Park string `json:"parkId" db:"park_id"`
|
||||
Name string `json:"name"`
|
||||
Floors *string `json:"floors"`
|
||||
Enabled bool `json:"enabled"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
LastModifiedAt time.Time `json:"lastModifiedAt"`
|
||||
DeletedAt *time.Time `json:"deletedAt"`
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
package model
|
||||
|
||||
import "github.com/shopspring/decimal"
|
||||
|
||||
type PaidPart struct {
|
||||
Overall decimal.Decimal `json:"overall"`
|
||||
OverallPrice decimal.Decimal `json:"overallPrice"`
|
||||
ConsumptionFee decimal.Decimal `json:"consumptionFee"`
|
||||
OverallFee decimal.Decimal `json:"overallFee"`
|
||||
Critical decimal.NullDecimal `json:"critical"`
|
||||
CriticalPrice decimal.NullDecimal `json:"criticalPrice"`
|
||||
CriticalFee decimal.NullDecimal `json:"criticalFee"`
|
||||
Peak decimal.NullDecimal `json:"peak"`
|
||||
PeakPrice decimal.NullDecimal `json:"peakPrice"`
|
||||
PeakFee decimal.NullDecimal `json:"peakFee"`
|
||||
Flat decimal.NullDecimal `json:"flat"`
|
||||
FlatPrice decimal.NullDecimal `json:"flatPrice"`
|
||||
FlatFee decimal.NullDecimal `json:"flatFee"`
|
||||
Valley decimal.NullDecimal `json:"valley"`
|
||||
ValleyPrice decimal.NullDecimal `json:"valleyPrice"`
|
||||
ValleyFee decimal.NullDecimal `json:"valleyFee"`
|
||||
BasicFee decimal.Decimal `json:"basicFee"`
|
||||
AdjustFee decimal.Decimal `json:"adjustFee"`
|
||||
}
|
||||
|
||||
type EndUserOverallPart struct {
|
||||
Overall decimal.Decimal `json:"overall"`
|
||||
OverallPrice decimal.Decimal `json:"overallPrice"`
|
||||
OverallFee decimal.Decimal `json:"consumptionFee"`
|
||||
Critical decimal.NullDecimal `json:"critical"`
|
||||
CriticalPrice decimal.NullDecimal `json:"criticalPrice"`
|
||||
CriticalFee decimal.NullDecimal `json:"criticalFee"`
|
||||
Peak decimal.NullDecimal `json:"peak"`
|
||||
PeakPrice decimal.NullDecimal `json:"peakPrice"`
|
||||
PeakFee decimal.NullDecimal `json:"peakFee"`
|
||||
Flat decimal.NullDecimal `json:"flat"`
|
||||
FlatPrice decimal.NullDecimal `json:"flatPrice"`
|
||||
FlatFee decimal.NullDecimal `json:"flatFee"`
|
||||
Valley decimal.NullDecimal `json:"valley"`
|
||||
ValleyPrice decimal.NullDecimal `json:"valleyPrice"`
|
||||
ValleyFee decimal.NullDecimal `json:"valleyFee"`
|
||||
}
|
||||
|
||||
type ConsumptionOverallPart struct {
|
||||
Overall decimal.Decimal `json:"overall"`
|
||||
OverallPrice decimal.Decimal `json:"overallPrice"`
|
||||
ConsumptionFee decimal.Decimal `json:"consumptionFee"`
|
||||
OverallFee decimal.Decimal `json:"overallFee"`
|
||||
Critical decimal.NullDecimal `json:"critical"`
|
||||
CriticalPrice decimal.NullDecimal `json:"criticalPrice"`
|
||||
CriticalFee decimal.NullDecimal `json:"criticalFee"`
|
||||
Peak decimal.NullDecimal `json:"peak"`
|
||||
PeakPrice decimal.NullDecimal `json:"peakPrice"`
|
||||
PeakFee decimal.NullDecimal `json:"peakFee"`
|
||||
Flat decimal.NullDecimal `json:"flat"`
|
||||
FlatPrice decimal.NullDecimal `json:"flatPrice"`
|
||||
FlatFee decimal.NullDecimal `json:"flatFee"`
|
||||
Valley decimal.NullDecimal `json:"valley"`
|
||||
ValleyPrice decimal.NullDecimal `json:"valleyPrice"`
|
||||
ValleyFee decimal.NullDecimal `json:"valleyFee"`
|
||||
Proportion decimal.Decimal `json:"proportion"`
|
||||
}
|
||||
|
||||
type LossPart struct {
|
||||
Quantity decimal.Decimal `json:"quantity"`
|
||||
Price decimal.Decimal `json:"price"`
|
||||
ConsumptionFee decimal.Decimal `json:"consumptionFee"`
|
||||
Proportion decimal.Decimal `json:"proportion"`
|
||||
AuthorizeQuantity decimal.Decimal `json:"authorizeQuantity"`
|
||||
AuthorizeConsumptionFee decimal.Decimal `json:"authorizeConsumptionFee"`
|
||||
}
|
||||
|
||||
type OtherShouldCollectionPart struct {
|
||||
LossFee decimal.NullDecimal `json:"lossFee"`
|
||||
BasicFees decimal.Decimal `json:"basicFees"`
|
||||
}
|
||||
|
||||
type MaintenancePart struct {
|
||||
BasicFees decimal.Decimal `json:"basicFees"`
|
||||
LossFee decimal.Decimal `json:"lossFee"`
|
||||
AdjustFee decimal.Decimal `json:"adjustFee"`
|
||||
LossProportion decimal.Decimal `json:"lossProportion"`
|
||||
AdjustProportion decimal.Decimal `json:"adjustProportion"`
|
||||
AdjustPrice decimal.Decimal `json:"adjustPrice"`
|
||||
}
|
||||
|
||||
type EndUserSummary struct {
|
||||
CustomerName *string `json:"customerName"`
|
||||
Address *string `json:"address"`
|
||||
MeterId string `json:"meterId"`
|
||||
IsPublicMeter bool `json:"isPublicMeter"`
|
||||
Overall decimal.Decimal `json:"overall"`
|
||||
OverallPrice decimal.Decimal `json:"overallPrice"`
|
||||
OverallFee decimal.Decimal `json:"overallFee"`
|
||||
Critical decimal.NullDecimal `json:"critical"`
|
||||
CriticalFee decimal.NullDecimal `json:"criticalFee"`
|
||||
Peak decimal.NullDecimal `json:"peak"`
|
||||
PeakFee decimal.NullDecimal `json:"peakFee"`
|
||||
Valley decimal.NullDecimal `json:"valley"`
|
||||
ValleyFee decimal.NullDecimal `json:"valleyFee"`
|
||||
Loss decimal.Decimal `json:"loss"`
|
||||
LossFee decimal.Decimal `json:"lossFee"`
|
||||
}
|
||||
|
||||
type Publicity struct {
|
||||
Report Report `json:"index"`
|
||||
User UserDetail `json:"enterprise"`
|
||||
Park Park `json:"park"`
|
||||
Paid PaidPart `json:"paid"`
|
||||
EndUser ConsumptionOverallPart `json:"endUserSum"`
|
||||
Loss LossPart `json:"loss"`
|
||||
PublicConsumptionOverall ConsumptionOverallPart `json:"public"`
|
||||
OtherCollections OtherShouldCollectionPart `json:"others"`
|
||||
Maintenance MaintenancePart `json:"maintenance"`
|
||||
EndUserDetails []EndUserSummary `json:"endUser"`
|
||||
}
|
||||
56
model/reading.go
Normal file
56
model/reading.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/types"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type Reading struct {
|
||||
Ratio decimal.Decimal `json:"ratio"`
|
||||
Overall decimal.Decimal `json:"overall"`
|
||||
Critical decimal.Decimal `json:"critical"`
|
||||
Peak decimal.Decimal `json:"peak"`
|
||||
Flat decimal.Decimal `json:"flat"`
|
||||
Valley decimal.Decimal `json:"valley"`
|
||||
}
|
||||
|
||||
func NewPVReading(ratio, overall, critical, peak, flat, valley decimal.Decimal) *Reading {
|
||||
return &Reading{
|
||||
Ratio: ratio,
|
||||
Overall: overall,
|
||||
Critical: critical,
|
||||
Peak: peak,
|
||||
Flat: flat,
|
||||
Valley: valley,
|
||||
}
|
||||
}
|
||||
|
||||
func NewUnitaryReading(ratio, overall decimal.Decimal) *Reading {
|
||||
return &Reading{
|
||||
Ratio: ratio,
|
||||
Overall: overall,
|
||||
Critical: decimal.Zero,
|
||||
Peak: decimal.Zero,
|
||||
Flat: overall,
|
||||
Valley: decimal.Zero,
|
||||
}
|
||||
}
|
||||
|
||||
type MeterReading struct {
|
||||
ReadAt types.DateTime `json:"readAt"`
|
||||
Park string `json:"parkId" db:"park_id"`
|
||||
Meter string `json:"meterId" db:"meter_id"`
|
||||
MeterType int16 `json:"meterType"`
|
||||
Ratio decimal.Decimal `json:"ratio"`
|
||||
Overall decimal.Decimal `json:"overall"`
|
||||
Critical decimal.Decimal `json:"critical"`
|
||||
Peak decimal.Decimal `json:"peak"`
|
||||
Flat decimal.Decimal `json:"flat"`
|
||||
Valley decimal.Decimal `json:"valley"`
|
||||
}
|
||||
|
||||
type DetailedMeterReading struct {
|
||||
Detail MeterDetail `json:"detail"`
|
||||
Reading MeterReading `json:"reading"`
|
||||
}
|
||||
@@ -1,11 +1,8 @@
|
||||
package model
|
||||
|
||||
import "github.com/uptrace/bun"
|
||||
|
||||
type Region struct {
|
||||
bun.BaseModel `bun:"table:region,alias:r"`
|
||||
Code string `bun:",pk,notnull" json:"code"`
|
||||
Name string `bun:",notnull" json:"name"`
|
||||
Level int `bun:",notnull" json:"level"`
|
||||
Parent string `bun:",notnull" json:"parent"`
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
Level int32 `json:"level"`
|
||||
Parent string `json:"parent"`
|
||||
}
|
||||
|
||||
191
model/report.go
191
model/report.go
@@ -1,99 +1,140 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
"electricity_bill_calc/types"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
const (
|
||||
REPORT_NOT_WITHDRAW int8 = iota
|
||||
REPORT_WITHDRAW_APPLIED
|
||||
REPORT_WITHDRAW_DENIED
|
||||
REPORT_WITHDRAW_GRANTED
|
||||
)
|
||||
|
||||
type Report struct {
|
||||
bun.BaseModel `bun:"table:report,alias:r"`
|
||||
CreatedAndModified `bun:"extend"`
|
||||
Id string `bun:",pk,notnull" json:"id"`
|
||||
ParkId string `bun:",notnull" json:"parkId"`
|
||||
Period time.Time `bun:"type:date,notnull" json:"period" time_format:"simple_date" time_location:"shanghai"`
|
||||
Category int8 `bun:"type:smallint,notnull" json:"category"`
|
||||
SubmeterType int8 `bun:"meter_04kv_type,type:smallint,notnull" json:"meter04kvType"`
|
||||
StepState Steps `bun:"type:jsonb,notnull" json:"stepState"`
|
||||
Published bool `bun:",notnull" json:"published"`
|
||||
PublishedAt *time.Time `bun:"type:timestamptz,nullzero" json:"publishedAt" time_format:"simple_datetime" time_location:"shanghai"`
|
||||
Withdraw int8 `bun:"type:smallint,notnull" json:"withdraw"`
|
||||
LastWithdrawAppliedAt *time.Time `bun:"type:timestamptz,nullzero" json:"lastWithdrawAppliedAt" time_format:"simple_datetime" time_location:"shanghai"`
|
||||
LastWithdrawAuditAt *time.Time `bun:"type:timestamptz,nullzero" json:"lastWithdrawAuditAt" time_format:"simple_datetime" time_location:"shanghai"`
|
||||
Park *Park `bun:"rel:belongs-to,join:park_id=id" json:"-"`
|
||||
Summary *ReportSummary `bun:"rel:has-one,join:id=report_id" json:"-"`
|
||||
WillDilutedFees []*WillDilutedFee `bun:"rel:has-many,join:id=report_id" json:"-"`
|
||||
EndUsers []*EndUserDetail `bun:"rel:has-many,join:id=report_id,join:park_id=park_id" json:"-"`
|
||||
type ReportIndex struct {
|
||||
Id string `json:"id"`
|
||||
Park string `json:"parkId" db:"park_id"`
|
||||
Period types.DateRange `json:"period"`
|
||||
Category int16 `json:"category"`
|
||||
MeterType int16 `json:"meter04kvType" db:"meter_04kv_type"`
|
||||
PricePolicy int16 `json:"pricePolicy"`
|
||||
BasisPooled int16 `json:"basisPooled"`
|
||||
AdjustPooled int16 `json:"adjustPooled"`
|
||||
LossPooled int16 `json:"lossPooled"`
|
||||
PublicPooled int16 `json:"publicPooled"`
|
||||
Published bool `json:"published"`
|
||||
PublishedAt *types.DateTime `json:"publishedAt" db:"published_at"`
|
||||
Withdraw int16 `json:"withdraw"`
|
||||
LastWithdrawAppliedAt *types.DateTime `json:"lastWithdrawAppliedAt" db:"last_withdraw_applied_at"`
|
||||
LastWithdrawAuditAt *types.DateTime `json:"lastWithdrawAuditAt" db:"last_withdraw_audit_at"`
|
||||
Status *int16 `json:"status"`
|
||||
Message *string `json:"message"`
|
||||
CreatedAt types.DateTime `json:"createdAt" db:"created_at"`
|
||||
LastModifiedAt types.DateTime `json:"lastModifiedAt" db:"last_modified_at"`
|
||||
AuthorizedLossRate float64 `json:"authorized_loss_rate" db:"authorized_loss_rate"`
|
||||
AuthorizedLossRateIncrement float64 `json:"authorized_loss_rate_increment" db:"authorized_loss_rate_increment"`
|
||||
}
|
||||
|
||||
type Steps struct {
|
||||
Summary bool `json:"summary"`
|
||||
WillDiluted bool `json:"willDiluted"`
|
||||
Submeter bool `json:"submeter"`
|
||||
Calculate bool `json:"calculate"`
|
||||
Preview bool `json:"preview"`
|
||||
Publish bool `json:"publish"`
|
||||
type ReportSummary struct {
|
||||
ReportId string `json:"reportId" db:"report_id"`
|
||||
OverallArea decimal.Decimal `json:"overallArea" db:"overall_area"`
|
||||
Overall ConsumptionUnit `json:"overall"`
|
||||
ConsumptionFee decimal.NullDecimal `json:"consumptionFee" db:"consumption_fee"`
|
||||
Critical ConsumptionUnit `json:"critical"`
|
||||
Peak ConsumptionUnit `json:"peak"`
|
||||
Flat ConsumptionUnit `json:"flat"`
|
||||
Valley ConsumptionUnit `json:"valley"`
|
||||
Loss decimal.NullDecimal `json:"loss"`
|
||||
LossFee decimal.NullDecimal `json:"lossFee" db:"loss_fee"`
|
||||
LossProportion decimal.NullDecimal `json:"lossProportion" db:"loss_proportion"`
|
||||
AuthorizeLoss *ConsumptionUnit `json:"authorizeLoss" db:"authorize_loss"`
|
||||
BasicFee decimal.Decimal `json:"basicFee" db:"basic_fee"`
|
||||
BasicPooledPriceConsumption decimal.NullDecimal `json:"basicPooledPriceConsumption" db:"basic_pooled_price_consumption"`
|
||||
BasicPooledPriceArea decimal.NullDecimal `json:"basicPooledPriceArea" db:"basic_pooled_price_area"`
|
||||
AdjustFee decimal.Decimal `json:"adjustFee" db:"adjust_fee"`
|
||||
AdjustPooledPriceConsumption decimal.NullDecimal `json:"adjustPooledPriceConsumption" db:"adjust_pooled_price_consumption"`
|
||||
AdjustPooledPriceArea decimal.NullDecimal `json:"adjustPooledPriceArea" db:"adjust_pooled_price_area"`
|
||||
LossDilutedPrice decimal.NullDecimal `json:"lossDilutedPrice" db:"loss_diluted_price"`
|
||||
TotalConsumption decimal.Decimal `json:"totalConsumption" db:"total_consumption"`
|
||||
FinalDilutedOverall decimal.NullDecimal `json:"finalDilutedOverall" db:"final_diluted_overall"`
|
||||
}
|
||||
|
||||
func NewSteps() Steps {
|
||||
return Steps{
|
||||
Summary: false,
|
||||
WillDiluted: false,
|
||||
Submeter: false,
|
||||
Calculate: false,
|
||||
Preview: false,
|
||||
Publish: false,
|
||||
func (rs ReportSummary) GetConsumptionFee() decimal.Decimal {
|
||||
if !rs.ConsumptionFee.Valid {
|
||||
return rs.Overall.Fee.Sub(rs.BasicFee).Sub(rs.AdjustFee)
|
||||
}
|
||||
return rs.ConsumptionFee.Decimal
|
||||
}
|
||||
|
||||
type ParkNewestReport struct {
|
||||
Park Park `bun:"extends" json:"park"`
|
||||
Report *Report `bun:"extends" json:"report"`
|
||||
type ReportPublicConsumption struct {
|
||||
ReportId string `json:"reportId" db:"report_id"`
|
||||
MeterId string `json:"parkMeterId" db:"park_meter_id"`
|
||||
Overall ConsumptionUnit `json:"overall"`
|
||||
Critical ConsumptionUnit `json:"critical"`
|
||||
Peak ConsumptionUnit `json:"peak"`
|
||||
Flat ConsumptionUnit `json:"flat"`
|
||||
Valley ConsumptionUnit `json:"valley"`
|
||||
LossAdjust ConsumptionUnit `json:"lossAdjust"`
|
||||
ConsumptionTotal decimal.Decimal `json:"consumptionTotal" db:"consumption_total"`
|
||||
LossAdjustTotal decimal.Decimal `json:"lossAdjustTotal" db:"loss_adjust_total"`
|
||||
FinalTotal decimal.Decimal `json:"finalTotal" db:"final_total"`
|
||||
PublicPooled int16 `json:"publicPooled" db:"public_pooled"`
|
||||
}
|
||||
|
||||
func (p *ParkNewestReport) AfterLoad() {
|
||||
if p.Report != nil && len(p.Report.Id) == 0 {
|
||||
p.Report = nil
|
||||
}
|
||||
type ReportDetailedPublicConsumption struct {
|
||||
MeterDetail
|
||||
ReportPublicConsumption
|
||||
}
|
||||
|
||||
type ReportIndexSimplified struct {
|
||||
bun.BaseModel `bun:"table:report,alias:r"`
|
||||
Id string `bun:",pk,notnull" json:"id"`
|
||||
ParkId string `bun:",notnull" json:"parkId"`
|
||||
Period Date `bun:"type:date,notnull" json:"period"`
|
||||
StepState Steps `bun:"type:jsonb,notnull" json:"stepState"`
|
||||
Published bool `bun:",notnull" json:"published"`
|
||||
PublishedAt *time.Time `bun:"type:timestampz" json:"publishedAt" time_format:"simple_datetime" time_location:"shanghai"`
|
||||
Withdraw int8 `bun:"type:smallint,notnull" json:"withdraw"`
|
||||
LastWithdrawAppliedAt *time.Time `bun:"type:timestamptz" json:"lastWithdrawAppliedAt" time_format:"simple_datetime" time_location:"shanghai"`
|
||||
LastWithdrawAuditAt *time.Time `bun:"type:timestamptz" json:"lastWithdrawAuditAt" time_format:"simple_datetime" time_location:"shanghai"`
|
||||
type ReportPooledConsumption struct {
|
||||
ReportId string `json:"reportId" db:"report_id"`
|
||||
MeterId string `json:"pooledMeterId" db:"pooled_meter_id"`
|
||||
Overall ConsumptionUnit `json:"overall"`
|
||||
Critical ConsumptionUnit `json:"critical"`
|
||||
Peak ConsumptionUnit `json:"peak"`
|
||||
Flat ConsumptionUnit `json:"flat"`
|
||||
Valley ConsumptionUnit `json:"valley"`
|
||||
PooledArea decimal.Decimal `json:"pooledArea" db:"pooled_area"`
|
||||
Diluted []NestedMeter `json:"diluted"`
|
||||
}
|
||||
|
||||
type JoinedReportForWithdraw struct {
|
||||
Report Report `bun:"extends" json:"report"`
|
||||
Park ParkSimplified `bun:"extends" json:"park"`
|
||||
User UserDetailSimplified `bun:"extends" json:"user"`
|
||||
type ReportDetailedPooledConsumption struct {
|
||||
MeterDetail
|
||||
ReportPooledConsumption
|
||||
PublicPooled int16 `json:"publicPooled"`
|
||||
}
|
||||
|
||||
var _ bun.BeforeAppendModelHook = (*Report)(nil)
|
||||
type ReportDetailNestedMeterConsumption struct {
|
||||
Meter MeterDetail `json:"meter"`
|
||||
Consumption NestedMeter `json:"consumption"`
|
||||
}
|
||||
|
||||
func (p *Report) BeforeAppendModel(ctx context.Context, query bun.Query) error {
|
||||
oprTime := time.Now()
|
||||
switch query.(type) {
|
||||
case *bun.InsertQuery:
|
||||
p.CreatedAt = oprTime
|
||||
p.LastModifiedAt = &oprTime
|
||||
case *bun.UpdateQuery:
|
||||
p.LastModifiedAt = &oprTime
|
||||
type ReportTenement struct {
|
||||
ReportId string `json:"reportId" db:"report_id"`
|
||||
Tenement string `json:"tenementId" db:"tenement_id"`
|
||||
Detail Tenement `json:"tenementDetail" db:"tenement_detail"`
|
||||
Period types.DateRange `json:"calcPeriod" db:"calc_period"`
|
||||
Overall ConsumptionUnit `json:"overall"`
|
||||
Critical ConsumptionUnit `json:"critical"`
|
||||
Peak ConsumptionUnit `json:"peak"`
|
||||
Flat ConsumptionUnit `json:"flat"`
|
||||
Valley ConsumptionUnit `json:"valley"`
|
||||
BasicFeePooled decimal.Decimal `json:"basicFeePooled" db:"basic_fee_pooled"`
|
||||
AdjustFeePooled decimal.Decimal `json:"adjustFeePooled" db:"adjust_fee_pooled"`
|
||||
LossFeePooled decimal.Decimal `json:"lossFeePooled" db:"loss_fee_pooled"`
|
||||
FinalPooled decimal.Decimal `json:"finalPooled" db:"final_pooled"`
|
||||
FinalCharge decimal.Decimal `json:"finalCharge" db:"final_charge"`
|
||||
Invoice []string `json:"invoice" db:"invoice"`
|
||||
Meters []NestedMeter `json:"meters" db:"meters"`
|
||||
Pooled []NestedMeter `json:"pooled" db:"pooled"`
|
||||
Loss ConsumptionUnit `json:"loss"` //TODO: 2023.08.11 测试时发现少一个字段(已补全)
|
||||
}
|
||||
return nil
|
||||
|
||||
type ReportTask struct {
|
||||
Id string `json:"id"`
|
||||
LastModifiedAt types.DateTime `json:"lastModifiedAt" db:"last_modified_at"`
|
||||
Status int16 `json:"status"`
|
||||
Message *string `json:"message"`
|
||||
}
|
||||
|
||||
type SimplifiedTenementCharge struct {
|
||||
ReportId string `json:"reportId" db:"report_id"`
|
||||
Period types.DateRange `json:"period"`
|
||||
TotalConsumption decimal.Decimal `json:"totalConsumption" db:"total_consumption"`
|
||||
FinalCharge decimal.Decimal `json:"finalCharge" db:"final_charge"`
|
||||
}
|
||||
|
||||
@@ -1,119 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type ReportSummary struct {
|
||||
bun.BaseModel `bun:"table:report_summary,alias:rs"`
|
||||
ReportId string `bun:",pk,notnull" json:"-"`
|
||||
Overall decimal.Decimal `bun:"type:numeric,notnull" json:"overall"`
|
||||
OverallFee decimal.Decimal `bun:"type:numeric,notnull" json:"overallFee"`
|
||||
ConsumptionFee decimal.NullDecimal `bun:"type:numeric" json:"consumptionFee"`
|
||||
OverallPrice decimal.NullDecimal `bun:"type:numeric" json:"overallPrice"`
|
||||
Critical decimal.Decimal `bun:"type:numeric,notnull" json:"critical"`
|
||||
CriticalFee decimal.Decimal `bun:"type:numeric,notnull" json:"criticalFee"`
|
||||
CriticalPrice decimal.NullDecimal `bun:"type:numeric" json:"criticalPrice"`
|
||||
Peak decimal.Decimal `bun:"type:numeric,notnull" json:"peak"`
|
||||
PeakFee decimal.Decimal `bun:"type:numeric,notnull" json:"peakFee"`
|
||||
PeakPrice decimal.NullDecimal `bun:"type:numeric" json:"peakPrice"`
|
||||
Flat decimal.Decimal `bun:"type:numeric,notnull" json:"flat"`
|
||||
FlatFee decimal.Decimal `bun:"type:numeric,notnull" json:"flatFee"`
|
||||
FlatPrice decimal.NullDecimal `bun:"type:numeric" json:"flatPrice"`
|
||||
Valley decimal.Decimal `bun:"type:numeric,notnull" json:"valley"`
|
||||
ValleyFee decimal.Decimal `bun:"type:numeric,notnull" json:"valleyFee"`
|
||||
ValleyPrice decimal.NullDecimal `bun:"type:numeric" json:"valleyPrice"`
|
||||
Loss decimal.NullDecimal `bun:"type:numeric" json:"loss"`
|
||||
LossFee decimal.NullDecimal `bun:"type:numeric" json:"lossFee"`
|
||||
LossProportion decimal.NullDecimal `bun:"type:numeric" json:"lossProportion"`
|
||||
AuthorizeLoss decimal.NullDecimal `bun:"type:numeric" json:"authorizeLoss"`
|
||||
AuthorizeLossFee decimal.NullDecimal `bun:"type:numeric" json:"authorizeLossFee"`
|
||||
AuthorizeLossProportion decimal.NullDecimal `bun:"type:numeric" json:"authorizeLossProportion"`
|
||||
BasicFee decimal.Decimal `bun:"type:numeric,notnull" json:"basicFee"`
|
||||
BasicDilutedPrice decimal.NullDecimal `bun:"type:numeric" json:"basicDilutedPrice"`
|
||||
AdjustFee decimal.Decimal `bun:"type:numeric,notnull" json:"adjustFee"`
|
||||
AdjustDilutedPrice decimal.NullDecimal `bun:"type:numeric" json:"adjustDilutedPrice"`
|
||||
MaintenanceDilutedPrice decimal.NullDecimal `bun:"type:numeric" json:"maintencanceDilutedPrice"`
|
||||
LossDilutedPrice decimal.NullDecimal `bun:"type:numeric" json:"lossDilutedPrice"`
|
||||
PublicConsumptionDilutedPrice decimal.NullDecimal `bun:"type:numeric" json:"publicConsumptionDilutedPrice"`
|
||||
MaintenanceOverall decimal.NullDecimal `bun:"type:numeric" json:"maintenanceOverall"`
|
||||
FinalDilutedOverall decimal.NullDecimal `bun:"type:numeric" json:"finalDilutedOverall"`
|
||||
Customers Consumptions `bun:"type:jsonb" json:"customers"`
|
||||
Publics Consumptions `bun:"type:jsonb" json:"publics"`
|
||||
}
|
||||
|
||||
type Consumptions struct {
|
||||
Consumption decimal.NullDecimal `json:"consumption"`
|
||||
ConsumptionFee decimal.NullDecimal `json:"fee"`
|
||||
Critical decimal.NullDecimal `json:"critical"`
|
||||
CriticalFee decimal.NullDecimal `json:"criticalFee"`
|
||||
Peak decimal.NullDecimal `json:"peak"`
|
||||
PeakFee decimal.NullDecimal `json:"peakFee"`
|
||||
Flat decimal.NullDecimal `json:"flat"`
|
||||
FlatFee decimal.NullDecimal `json:"flatFee"`
|
||||
Valley decimal.NullDecimal `json:"valley"`
|
||||
ValleyFee decimal.NullDecimal `json:"valleyFee"`
|
||||
Proportion decimal.NullDecimal `json:"proportion"`
|
||||
}
|
||||
|
||||
func NewConsumptions() Consumptions {
|
||||
return Consumptions{
|
||||
Consumption: decimal.NewNullDecimal(decimal.Zero),
|
||||
ConsumptionFee: decimal.NewNullDecimal(decimal.Zero),
|
||||
Critical: decimal.NewNullDecimal(decimal.Zero),
|
||||
Peak: decimal.NewNullDecimal(decimal.Zero),
|
||||
PeakFee: decimal.NewNullDecimal(decimal.Zero),
|
||||
Flat: decimal.NewNullDecimal(decimal.Zero),
|
||||
CriticalFee: decimal.NewNullDecimal(decimal.Zero),
|
||||
FlatFee: decimal.NewNullDecimal(decimal.Zero),
|
||||
Valley: decimal.NewNullDecimal(decimal.Zero),
|
||||
ValleyFee: decimal.NewNullDecimal(decimal.Zero),
|
||||
Proportion: decimal.NewNullDecimal(decimal.Zero),
|
||||
}
|
||||
}
|
||||
|
||||
func (s ReportSummary) Validate() (bool, error) {
|
||||
amountSum := decimal.Sum(s.Critical, s.Peak, s.Valley)
|
||||
if amountSum.GreaterThan(s.Overall) {
|
||||
return false, errors.New("峰谷计量总量大于总计电量")
|
||||
}
|
||||
feeSum := decimal.Sum(s.CriticalFee, s.PeakFee, s.ValleyFee)
|
||||
if feeSum.GreaterThan(s.OverallFee) {
|
||||
return false, errors.New("峰谷计量费用大于总计费用")
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (s *ReportSummary) CalculatePrices() {
|
||||
s.ConsumptionFee = decimal.NewNullDecimal(s.OverallFee.Sub(s.BasicFee).Sub(s.AdjustFee))
|
||||
if s.Overall.GreaterThan(decimal.Zero) {
|
||||
s.OverallPrice = decimal.NewNullDecimal(s.ConsumptionFee.Decimal.Div(s.Overall).RoundBank(8))
|
||||
} else {
|
||||
s.OverallPrice = decimal.NewNullDecimal(decimal.Zero)
|
||||
}
|
||||
if s.Critical.GreaterThan(decimal.Zero) {
|
||||
s.CriticalPrice = decimal.NewNullDecimal(s.CriticalFee.Div(s.Critical).RoundBank(8))
|
||||
} else {
|
||||
s.CriticalPrice = decimal.NewNullDecimal(decimal.Zero)
|
||||
}
|
||||
if s.Peak.GreaterThan(decimal.Zero) {
|
||||
s.PeakPrice = decimal.NewNullDecimal(s.PeakFee.Div(s.Peak).RoundBank(8))
|
||||
} else {
|
||||
s.PeakPrice = decimal.NewNullDecimal(decimal.Zero)
|
||||
}
|
||||
if s.Valley.GreaterThan(decimal.Zero) {
|
||||
s.ValleyPrice = decimal.NewNullDecimal(s.ValleyFee.Div(s.Valley).RoundBank(8))
|
||||
} else {
|
||||
s.ValleyPrice = decimal.NewNullDecimal(decimal.Zero)
|
||||
}
|
||||
s.Flat = s.Overall.Sub(s.Critical).Sub(s.Peak).Sub(s.Valley)
|
||||
s.FlatFee = s.ConsumptionFee.Decimal.Sub(s.CriticalFee).Sub(s.PeakFee).Sub(s.ValleyFee)
|
||||
if s.Flat.GreaterThan(decimal.Zero) {
|
||||
s.FlatPrice = decimal.NewNullDecimal(s.FlatFee.Div(s.Flat).RoundBank(8))
|
||||
} else {
|
||||
s.FlatPrice = decimal.NewNullDecimal(decimal.Zero)
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import "time"
|
||||
type Session struct {
|
||||
Uid string `json:"uid"`
|
||||
Name string `json:"name"`
|
||||
Type int8 `json:"type"`
|
||||
Type int16 `json:"type"`
|
||||
Token string `json:"token"`
|
||||
ExpiresAt time.Time `json:"expiresAt" time_format:"simple_datetime" time_location:"shanghai"`
|
||||
}
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
type Created struct {
|
||||
CreatedAt time.Time `bun:"type:timestamptz,notnull" json:"createdAt" time_format:"simple_datetime" time_location:"shanghai"`
|
||||
}
|
||||
|
||||
type CreatedWithUser struct {
|
||||
Created `bun:"extend"`
|
||||
CreatedBy *string `json:"createdBy"`
|
||||
}
|
||||
|
||||
type Deleted struct {
|
||||
DeletedAt *time.Time `bun:"type:timestamptz,soft_delete,nullzero" json:"deletedAt" time_format:"simple_datetime" time_location:"shanghai"`
|
||||
}
|
||||
|
||||
type DeletedWithUser struct {
|
||||
Deleted `bun:"extend"`
|
||||
DeletedBy *string `json:"deletedBy"`
|
||||
}
|
||||
|
||||
type CreatedAndModified struct {
|
||||
Created `bun:"extend"`
|
||||
LastModifiedAt *time.Time `bun:"type:timestamptz,nullzero" json:"lastModifiedAt" time_format:"simple_datetime" time_location:"shanghai"`
|
||||
}
|
||||
|
||||
type CreatedAndModifiedWithUser struct {
|
||||
CreatedAndModified `bun:"extend"`
|
||||
CreatedBy *string `json:"createdBy"`
|
||||
LastModifiedBy *string `json:"lastModifiedBy"`
|
||||
}
|
||||
38
model/synchronize.go
Normal file
38
model/synchronize.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/types"
|
||||
_ "github.com/shopspring/decimal"
|
||||
"time"
|
||||
)
|
||||
|
||||
type SynchronizeConfiguration struct {
|
||||
User string `json:"user" db:"user_id"`
|
||||
Park string `json:"park" db:"park_id"`
|
||||
MeterReadingType int16 `json:"meter_reading_type"`
|
||||
ImrsType string `json:"imrs_type"`
|
||||
AuthorizationAccount string `json:"authorization_account" db:"imrs_authorization_account"`
|
||||
AuthorizationSecret string `json:"authorization_secret" db:"imrs_authorization_secret"`
|
||||
AuthorizationKey []byte `json:"authorization_key,omitempty" db:"imrs_authorization_key"`
|
||||
Interval int16 `json:"interval"`
|
||||
CollectAt time.Time `json:"collect_at" db:"-"`
|
||||
MaxRetries int16 `json:"max_retries"`
|
||||
RetryInterval int16 `json:"retry_interval"`
|
||||
RetryIntervalAlgorithm int16 `json:"retry_interval_algorithm"`
|
||||
}
|
||||
|
||||
type SynchronizeSchedule struct {
|
||||
User string `json:"userId" db:"user_id"`
|
||||
UserName string `json:"userName" db:"user_name"`
|
||||
Park string `json:"parkId" db:"park_id"`
|
||||
ParkName string `json:"parkName" db:"park_name"`
|
||||
TaskIdentity string `json:"taskIdentity" db:"task_identity"`
|
||||
TaskName string `json:"taskName" db:"task_name"`
|
||||
TaskDescription string `json:"taskDescription" db:"task_description"`
|
||||
CreatedAt types.DateTime `json:"createdAt" db:"created_at"`
|
||||
LastModifiedAt types.DateTime `json:"lastModifiedAt" db:"last_modified_at"`
|
||||
LastDispatchedAt types.DateTime `json:"lastDispatchedAt" db:"last_dispatched_at"`
|
||||
LastDispatchStatus int16 `json:"lastDispatchStatus" db:"last_dispatch_status"`
|
||||
NextDispatchAt types.DateTime `json:"nextDispatchAt" db:"next_dispatch_at"`
|
||||
CurrentRetries int16 `json:"currentRetries" db:"current_retries"`
|
||||
}
|
||||
33
model/tenement.go
Normal file
33
model/tenement.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package model
|
||||
|
||||
import "electricity_bill_calc/types"
|
||||
|
||||
type Tenement struct {
|
||||
Id string `json:"id"`
|
||||
Park string `json:"parkId" db:"park_id"`
|
||||
FullName string `json:"fullName" db:"full_name"`
|
||||
ShortName *string `json:"shortName" db:"short_name"`
|
||||
Abbr string `json:"abbr"`
|
||||
Address string `json:"address"`
|
||||
ContactName string `json:"contactName" db:"contact_name"`
|
||||
ContactPhone string `json:"contactPhone" db:"contact_phone"`
|
||||
Building *string `json:"building"`
|
||||
BuildingName *string `json:"buildingName" db:"building_name"`
|
||||
OnFloor *string `json:"onFloor" db:"on_floor"`
|
||||
InvoiceInfo *InvoiceTitle `json:"invoiceInfo" db:"invoice_info"`
|
||||
MovedInAt *types.DateTime `json:"movedInAt" db:"moved_in_at"`
|
||||
MovedOutAt *types.DateTime `json:"movedOutAt" db:"moved_out_at"`
|
||||
CreatedAt types.DateTime `json:"createdAt" db:"created_at"`
|
||||
LastModifiedAt types.DateTime `json:"lastModifiedAt" db:"last_modified_at"`
|
||||
DeletedAt *types.DateTime `json:"deletedAt" db:"deleted_at"`
|
||||
}
|
||||
|
||||
type TenementMeter struct {
|
||||
ParkId string `db:"park_id"`
|
||||
TenementId string `db:"tenement_id"`
|
||||
MeterId string `db:"meter_id"`
|
||||
ForeignRelation bool `db:"foreign_relation"`
|
||||
AssociatedAt types.DateTime `db:"associated_at"`
|
||||
DisassociatedAt types.DateTime `db:"disassociated_at"`
|
||||
SynchronizedAt types.DateTime `db:"synchronized_at"`
|
||||
}
|
||||
33
model/top_up.go
Normal file
33
model/top_up.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/types"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type TopUp struct {
|
||||
TopUpCode string `json:"topUpCode" db:"top_up_code"`
|
||||
Park string `json:"parkId" db:"park_id"`
|
||||
Tenement string `json:"tenementId" db:"tenement_id"`
|
||||
TenementName string `json:"tenementName" db:"tenement_name"`
|
||||
Meter string `json:"meterId" db:"meter_id"`
|
||||
MeterAddress *string `json:"meterAddress" db:"meter_address"`
|
||||
ToppedUpAt types.DateTime `json:"toppedUpAt" db:"topped_up_at"`
|
||||
Amount decimal.Decimal `json:"amount" db:"amount"`
|
||||
PaymentType int16 `json:"paymentType" db:"payment_type"`
|
||||
SuccessfulSynchronized bool `json:"successfulSynchronized" db:"successful_synchronized"`
|
||||
SynchronizedAt *types.DateTime `json:"synchronizedAt" db:"synchronized_at"`
|
||||
CancelledAt *types.DateTime `json:"cancelledAt" db:"cancelled_at"`
|
||||
}
|
||||
|
||||
func (t TopUp) SyncStatus() int16 {
|
||||
switch {
|
||||
case t.SuccessfulSynchronized && t.SynchronizedAt != nil:
|
||||
return 1
|
||||
case !t.SuccessfulSynchronized && t.SynchronizedAt != nil:
|
||||
return 2
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
118
model/types.go
118
model/types.go
@@ -1,118 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Date struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
func NewDate(t time.Time) Date {
|
||||
loc, err := time.LoadLocation("Asia/Shanghai")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
t = t.In(loc)
|
||||
return Date{
|
||||
Time: time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc),
|
||||
}
|
||||
}
|
||||
|
||||
func NewEmptyDate() Date {
|
||||
loc, err := time.LoadLocation("Asia/Shanghai")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return Date{
|
||||
Time: time.Time{}.In(loc),
|
||||
}
|
||||
}
|
||||
|
||||
func ParseDate(t string) (Date, error) {
|
||||
loc, err := time.LoadLocation("Asia/Shanghai")
|
||||
if err != nil {
|
||||
return NewEmptyDate(), fmt.Errorf("unable to load time zone, %w", err)
|
||||
}
|
||||
d, err := time.ParseInLocation("2006-01-02", t, loc)
|
||||
if err != nil {
|
||||
return NewEmptyDate(), fmt.Errorf("unable to parse given time, %w", err)
|
||||
}
|
||||
return Date{
|
||||
Time: d,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (d Date) IsEmpty() bool {
|
||||
return d.Time.IsZero()
|
||||
}
|
||||
|
||||
func (d Date) Format(fmt string) string {
|
||||
return d.Time.Format(fmt)
|
||||
}
|
||||
|
||||
func (d Date) ToString() string {
|
||||
return d.Time.Format("2006-01-02")
|
||||
}
|
||||
|
||||
var _ driver.Valuer = (*Date)(nil)
|
||||
|
||||
func (d Date) Value() (driver.Value, error) {
|
||||
loc, err := time.LoadLocation("Asia/Shanghai")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return d.In(loc).Format("2006-01-02"), nil
|
||||
}
|
||||
|
||||
var _ sql.Scanner = (*Date)(nil)
|
||||
|
||||
// Scan scans the time parsing it if necessary using timeFormat.
|
||||
func (d *Date) Scan(src interface{}) (err error) {
|
||||
loc, err := time.LoadLocation("Asia/Shanghai")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
switch src := src.(type) {
|
||||
case time.Time:
|
||||
*d = NewDate(src)
|
||||
return nil
|
||||
case string:
|
||||
d.Time, err = time.ParseInLocation("2006-01-02", src, loc)
|
||||
return err
|
||||
case []byte:
|
||||
d.Time, err = time.ParseInLocation("2006-01-02", string(src), loc)
|
||||
return err
|
||||
case nil:
|
||||
d.Time = time.Time{}
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("unsupported data type: %T", src)
|
||||
}
|
||||
}
|
||||
|
||||
var _ json.Marshaler = (*Date)(nil)
|
||||
|
||||
func (d Date) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(d.Time.Format("2006-01-02"))
|
||||
}
|
||||
|
||||
var _ json.Unmarshaler = (*Date)(nil)
|
||||
|
||||
func (d *Date) UnmarshalJSON(raw []byte) error {
|
||||
loc, err := time.LoadLocation("Asia/Shanghai")
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to load time zone, %w", err)
|
||||
}
|
||||
var s string
|
||||
err = json.Unmarshal(raw, &s)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to unmarshal value, %w", err)
|
||||
}
|
||||
d.Time, err = time.ParseInLocation("2006-01-02", s, loc)
|
||||
return err
|
||||
}
|
||||
126
model/user.go
126
model/user.go
@@ -1,48 +1,114 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"electricity_bill_calc/types"
|
||||
"time"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
const (
|
||||
USER_TYPE_ENT int8 = iota
|
||||
USER_TYPE_ENT int16 = iota
|
||||
USER_TYPE_SUP
|
||||
USER_TYPE_OPS
|
||||
)
|
||||
|
||||
type ManagementAccountCreationForm struct {
|
||||
Id *string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Name string `json:"name"`
|
||||
Contact *string `json:"contact"`
|
||||
Phone *string `json:"phone"`
|
||||
Type int16 `json:"type"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Expires types.Date `json:"expires"`
|
||||
}
|
||||
|
||||
func (m ManagementAccountCreationForm) IntoUser() *User {
|
||||
return &User{
|
||||
Id: *m.Id,
|
||||
Username: m.Username,
|
||||
Password: "",
|
||||
ResetNeeded: false,
|
||||
UserType: m.Type,
|
||||
Enabled: m.Enabled,
|
||||
CreatedAt: nil,
|
||||
}
|
||||
}
|
||||
|
||||
func (m ManagementAccountCreationForm) IntoUserDetail() *UserDetail {
|
||||
return &UserDetail{
|
||||
Id: *m.Id,
|
||||
Name: &m.Name,
|
||||
Abbr: nil,
|
||||
Region: nil,
|
||||
Address: nil,
|
||||
Contact: m.Contact,
|
||||
Phone: m.Phone,
|
||||
UnitServiceFee: decimal.Zero,
|
||||
ServiceExpiration: m.Expires,
|
||||
CreatedAt: types.Now(),
|
||||
CreatedBy: nil,
|
||||
LastModifiedAt: types.Now(),
|
||||
LastModifiedBy: nil,
|
||||
DeletedAt: nil,
|
||||
DeletedBy: nil,
|
||||
}
|
||||
}
|
||||
|
||||
type UserModificationForm struct {
|
||||
Name string `json:"name"`
|
||||
Region *string `json:"region"`
|
||||
Address *string `json:"address"`
|
||||
Contact *string `json:"contact"`
|
||||
Phone *string `json:"phone"`
|
||||
UnitServiceFee *decimal.Decimal `json:"unitServiceFee"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
bun.BaseModel `bun:"table:user,alias:u"`
|
||||
Created `bun:"extend"`
|
||||
Id string `bun:",pk,notnull" json:"id"`
|
||||
Username string `bun:",notnull" json:"username"`
|
||||
Password string `bun:",notnull" json:"-"`
|
||||
ResetNeeded bool `bun:",notnull" json:"resetNeeded"`
|
||||
Type int8 `bun:"type:smallint,notnull" json:"type"`
|
||||
Enabled bool `bun:",notnull" json:"enabled"`
|
||||
Detail *UserDetail `bun:"rel:has-one,join:id=id" json:"-"`
|
||||
Charges []*UserCharge `bun:"rel:has-many,join:id=user_id" json:"-"`
|
||||
Id string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
ResetNeeded bool `json:"resetNeeded"`
|
||||
UserType int16 `db:"type"`
|
||||
Enabled bool `json:"enabled"`
|
||||
CreatedAt *time.Time `json:"createdAt"`
|
||||
}
|
||||
|
||||
type UserWithCredentials struct {
|
||||
bun.BaseModel `bun:"table:user,alias:u"`
|
||||
Created `bun:"extend"`
|
||||
Id string `bun:",pk,notnull" json:"id"`
|
||||
Username string `bun:",notnull" json:"username"`
|
||||
Password string `bun:",notnull" json:"credential"`
|
||||
ResetNeeded bool `bun:",notnull" json:"resetNeeded"`
|
||||
Type int8 `bun:"type:smallint,notnull" json:"type"`
|
||||
Enabled bool `bun:",notnull" json:"enabled"`
|
||||
type UserDetail struct {
|
||||
Id string `json:"id"`
|
||||
Name *string `json:"name"`
|
||||
Abbr *string `json:"abbr"`
|
||||
Region *string `json:"region"`
|
||||
Address *string `json:"address"`
|
||||
Contact *string `json:"contact"`
|
||||
Phone *string `json:"phone"`
|
||||
UnitServiceFee decimal.Decimal `db:"unit_service_fee" json:"unitServiceFee"`
|
||||
ServiceExpiration types.Date `json:"serviceExpiration"`
|
||||
CreatedAt types.DateTime `json:"createdAt"`
|
||||
CreatedBy *string `json:"createdBy"`
|
||||
LastModifiedAt types.DateTime `json:"lastModifiedAt"`
|
||||
LastModifiedBy *string `json:"lastModifiedBy"`
|
||||
DeletedAt *types.DateTime `json:"deletedAt"`
|
||||
DeletedBy *string `json:"deletedBy"`
|
||||
}
|
||||
|
||||
var _ bun.BeforeAppendModelHook = (*User)(nil)
|
||||
|
||||
func (u *User) BeforeAppendModel(ctx context.Context, query bun.Query) error {
|
||||
switch query.(type) {
|
||||
case *bun.InsertQuery:
|
||||
u.CreatedAt = time.Now()
|
||||
}
|
||||
return nil
|
||||
type UserWithDetail struct {
|
||||
Id string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
ResetNeeded bool `json:"resetNeeded"`
|
||||
UserType int16 `db:"type" json:"type"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Name *string `json:"name"`
|
||||
Abbr *string `json:"abbr"`
|
||||
Region *string `json:"region"`
|
||||
Address *string `json:"address"`
|
||||
Contact *string `json:"contact"`
|
||||
Phone *string `json:"phone"`
|
||||
UnitServiceFee decimal.Decimal `db:"unit_service_fee" json:"unitServiceFee"`
|
||||
ServiceExpiration types.Date `json:"serviceExpiration"`
|
||||
CreatedAt types.DateTime `json:"createdAt"`
|
||||
CreatedBy *string `json:"createdBy"`
|
||||
LastModifiedAt types.DateTime `json:"lastModifiedAt"`
|
||||
LastModifiedBy *string `json:"lastModifiedBy"`
|
||||
}
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type UserCharge struct {
|
||||
bun.BaseModel `bun:"table:user_charge,alias:c"`
|
||||
Created `bun:"extend"`
|
||||
Seq int64 `bun:"type:bigint,pk,notnull,autoincrement" json:"seq"`
|
||||
UserId string `bun:",notnull" json:"userId"`
|
||||
Fee decimal.NullDecimal `bun:"type:numeric" json:"fee"`
|
||||
Discount decimal.NullDecimal `bun:"type:numeric" json:"discount"`
|
||||
Amount decimal.NullDecimal `bun:"type:numeric" json:"amount"`
|
||||
ChargeTo Date `bun:"type:date,notnull" json:"chargeTo"`
|
||||
Settled bool `bun:",notnull" json:"settled"`
|
||||
SettledAt *time.Time `bun:"type:timestamptz" json:"settledAt" time_format:"simple_datetime" time_location:"shanghai"`
|
||||
Cancelled bool `bun:",notnull" json:"cancelled"`
|
||||
CancelledAt *time.Time `bun:"type:timestamptz" json:"cancelledAt" time_format:"simple_datetime" time_location:"shanghai"`
|
||||
Refunded bool `bun:",notnull" json:"refunded"`
|
||||
RefundedAt *time.Time `bun:"type:timestamptz" json:"refundedAt" time_format:"simple_datetime" time_location:"shanghai"`
|
||||
Detail *UserDetail `bun:"rel:belongs-to,join:user_id=id" json:"-"`
|
||||
}
|
||||
|
||||
type ChargeWithName struct {
|
||||
UserDetail `bun:"extend"`
|
||||
UserCharge `bun:"extend"`
|
||||
}
|
||||
|
||||
var _ bun.BeforeAppendModelHook = (*UserCharge)(nil)
|
||||
|
||||
func (uc *UserCharge) BeforeAppendModel(ctx context.Context, query bun.Query) error {
|
||||
oprTime := time.Now()
|
||||
switch query.(type) {
|
||||
case *bun.InsertQuery:
|
||||
uc.CreatedAt = oprTime
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type UserDetail struct {
|
||||
bun.BaseModel `bun:"table:user_detail,alias:d"`
|
||||
CreatedAndModifiedWithUser `bun:"extend"`
|
||||
DeletedWithUser `bun:"extend"`
|
||||
Id string `bun:",pk,notnull" json:"-"`
|
||||
Name *string `json:"name"`
|
||||
Abbr *string `json:"abbr"`
|
||||
Region *string `json:"region"`
|
||||
Address *string `json:"address"`
|
||||
Contact *string `json:"contact"`
|
||||
Phone *string `json:"phone"`
|
||||
UnitServiceFee decimal.Decimal `bun:"type:numeric,notnull" json:"unitServiceFee"`
|
||||
ServiceExpiration Date `bun:"type:date,notnull" json:"serviceExpiration"`
|
||||
}
|
||||
|
||||
type JoinedUserDetail struct {
|
||||
UserDetail `bun:"extend"`
|
||||
Id string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Type int8 `json:"type"`
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
|
||||
type FullJoinedUserDetail struct {
|
||||
UserDetail `bun:"extend"`
|
||||
User `bun:"extend"`
|
||||
}
|
||||
|
||||
type UserDetailSimplified struct {
|
||||
bun.BaseModel `bun:"table:user_detail,alias:d"`
|
||||
Id string `bun:",pk,notnull" json:"id"`
|
||||
Name *string `json:"name"`
|
||||
Abbr *string `json:"abbr"`
|
||||
Region *string `json:"region"`
|
||||
Address *string `json:"address"`
|
||||
Contact *string `json:"contact"`
|
||||
Phone *string `json:"phone"`
|
||||
}
|
||||
|
||||
func FromUserDetail(user UserDetail) UserDetailSimplified {
|
||||
dest := UserDetailSimplified{}
|
||||
copier.Copy(&dest, user)
|
||||
return dest
|
||||
}
|
||||
|
||||
var _ bun.BeforeAppendModelHook = (*UserDetail)(nil)
|
||||
|
||||
func (d *UserDetail) BeforeAppendModel(ctx context.Context, query bun.Query) error {
|
||||
oprTime := time.Now()
|
||||
switch query.(type) {
|
||||
case *bun.InsertQuery:
|
||||
d.CreatedAt = oprTime
|
||||
d.LastModifiedAt = &oprTime
|
||||
case *bun.UpdateQuery:
|
||||
d.LastModifiedAt = &oprTime
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
type WillDilutedFee struct {
|
||||
bun.BaseModel `bun:"table:will_diluted_fee,alias:w"`
|
||||
CreatedAndModified `bun:"extend"`
|
||||
Id string `bun:",pk,notnull" json:"diluteId"`
|
||||
ReportId string `bun:",notnull" json:"reportId"`
|
||||
SourceId *string `json:"sourceId"`
|
||||
Name string `bun:",notnull" json:"name"`
|
||||
Fee decimal.Decimal `bun:"type:numeric,notnull" json:"fee"`
|
||||
Memo *string `bun:"type:text" json:"memo"`
|
||||
Origin *MaintenanceFee `bun:"rel:belongs-to,join:source_id=id"`
|
||||
}
|
||||
|
||||
var _ bun.BeforeAppendModelHook = (*WillDilutedFee)(nil)
|
||||
|
||||
func (d *WillDilutedFee) BeforeAppendModel(ctx context.Context, query bun.Query) error {
|
||||
oprTime := time.Now()
|
||||
switch query.(type) {
|
||||
case *bun.InsertQuery:
|
||||
d.CreatedAt = oprTime
|
||||
d.LastModifiedAt = &oprTime
|
||||
case *bun.UpdateQuery:
|
||||
d.LastModifiedAt = &oprTime
|
||||
}
|
||||
return nil
|
||||
}
|
||||
84
model/withdraw.go
Normal file
84
model/withdraw.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/types"
|
||||
"github.com/shopspring/decimal"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Withdraw struct {
|
||||
Park SimplifiedPark `json:"park"`
|
||||
Report SimplifiedReport `json:"report"`
|
||||
User UserInfos `json:"user"` // 简易用户详细信息
|
||||
}
|
||||
|
||||
// 简易园区信息
|
||||
type SimplifiedPark struct {
|
||||
Address *string `json:"address"` // 园区地址
|
||||
Area *string `json:"area"` // 园区面积
|
||||
Capacity *string `json:"capacity"` // 供电容量
|
||||
Category int16 `json:"category"` // 用电分类,0:两部制,1:单一峰谷,2:单一单一
|
||||
Contact *string `json:"contact"` // 园区联系人
|
||||
ID string `json:"id"` // 园区ID
|
||||
Meter04KvType int16 `json:"meter04kvType"` // 户表计量类型,0:非峰谷,1:峰谷
|
||||
Name string `json:"name"` // 园区名称
|
||||
Phone *string `json:"phone"` // 园区联系人电话
|
||||
Region *string `json:"region"` // 园区所在行政区划
|
||||
Tenement *string `json:"tenement"` // 园区住户数量
|
||||
UserID string `json:"userId"` // 园区所属用户ID
|
||||
}
|
||||
|
||||
// 简易核算报表信息
|
||||
type SimplifiedReport struct {
|
||||
ID string `json:"id"` // 报表ID
|
||||
LastWithdrawAppliedAt *string `json:"lastWithdrawAppliedAt"` // 最后一次申请撤回的时间,格式为 yyyy-MM-dd HH:mm:ss
|
||||
LastWithdrawAuditAt *string `json:"lastWithdrawAuditAt"` // 最后一次申请审核的时间,格式为 yyyy-MM-dd HH:mm:ss
|
||||
Message *string `json:"message"` // 当前状态的错误提示
|
||||
ParkID string `json:"parkId"` // 所属园区ID
|
||||
PeriodBegin string `json:"periodBegin"` // 核算起始日期,格式为 yyyy-MM-dd
|
||||
PeriodEnd string `json:"periodEnd"` // 核算结束日期,格式为 yyyy-MM-dd
|
||||
Published bool `json:"published"` // 是否已发布
|
||||
PublishedAt *string `json:"publishedAt"` // 发布时间
|
||||
Status float64 `json:"status,omitempty"` // 当前状态,0:计算任务已队列,1:计算任务已完成,2:计算数据不足
|
||||
Withdraw int16 `json:"withdraw"` // 报表撤回状态,0:未撤回,1:申请撤回中,2:申请拒绝,3:申请批准
|
||||
}
|
||||
|
||||
// 简易用户信息
|
||||
type UserInfos struct {
|
||||
Address *string `json:"address"` // 用户地址
|
||||
Contact *string `json:"contact"` // 用户联系人
|
||||
ID string `json:"id"` // 用户ID
|
||||
Name *string `json:"name"` // 用户名称
|
||||
Phone *string `json:"phone"` // 用户联系人电话
|
||||
Region *string `json:"region"` // 用户所在行政区划
|
||||
}
|
||||
|
||||
//用于映射数据库的报表结构体
|
||||
type ReportRes struct {
|
||||
ReportId string `db:"report_id"`
|
||||
LastWithdrawAppliedAt *time.Time `db:"last_withdraw_applied_at"`
|
||||
LastWithdrawAuditAt *time.Time `db:"last_withdraw_audit_at"`
|
||||
ParkID string `db:"report_park_id"`
|
||||
Period types.DateRange `db:"period"`
|
||||
Published bool `db:"published"`
|
||||
PublishedAt *time.Time `db: "published_at"`
|
||||
Withdraw int16 `db:"withdraw"`
|
||||
ParkAddress *string `db:"park_address"`
|
||||
Area decimal.NullDecimal `db:"area"`
|
||||
Capacity decimal.NullDecimal `db:"capacity"`
|
||||
Category int16
|
||||
ParkContact *string `db:"park_contact"`
|
||||
ParkId string `db:"park_id"`
|
||||
Meter04KVType int16 `db:"meter_04kv_type"`
|
||||
ParkName string `db:"park_name"`
|
||||
ParkPhone *string `db:"park_phone"`
|
||||
ParkRegion string `db:"park_region"`
|
||||
TenementQuantity decimal.NullDecimal `db:"tenement_quantity"`
|
||||
UserID string `db:"user_id"`
|
||||
Address *string
|
||||
Contact string `db:"user_detail_contact"`
|
||||
ID string `db:"ud_id"`
|
||||
Name *string `db:"user_detail_name"`
|
||||
Phone string `db:"user_detail_phone"`
|
||||
Region *string `db:"user_detail_region"`
|
||||
}
|
||||
576
repository/calculate.go
Normal file
576
repository/calculate.go
Normal file
@@ -0,0 +1,576 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/model/calculate"
|
||||
"electricity_bill_calc/types"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/shopspring/decimal"
|
||||
"golang.org/x/sync/errgroup"
|
||||
"time"
|
||||
|
||||
"github.com/doug-martin/goqu/v9"
|
||||
_ "github.com/doug-martin/goqu/v9/dialect/postgres"
|
||||
"github.com/georgysavva/scany/v2/pgxscan"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type _CalculateRepository struct {
|
||||
log *zap.Logger
|
||||
ds goqu.DialectWrapper
|
||||
}
|
||||
|
||||
var CalculateRepository = _CalculateRepository{
|
||||
log: logger.Named("Repository", "Calculate"),
|
||||
ds: goqu.Dialect("postgres"),
|
||||
}
|
||||
|
||||
// 更新当前报表的核算状态
|
||||
func (cr _CalculateRepository) UpdateReportCalculateStatus(rid string, status string,
|
||||
message string) (bool, error) {
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
|
||||
var atio int
|
||||
var err error
|
||||
|
||||
currentTime := time.Now()
|
||||
if status == "success" {
|
||||
atio = 1 //创建报表成功
|
||||
} else {
|
||||
atio = 2 // 数据不足
|
||||
}
|
||||
|
||||
updateResultSql, updateResultArgs, _ := cr.ds.
|
||||
Update(goqu.T("report_task")).
|
||||
Set(goqu.Record{
|
||||
"status": int16(atio),
|
||||
"last_modified_at": currentTime,
|
||||
"message": message,
|
||||
}).Where(goqu.I("id").Eq(rid)).
|
||||
ToSQL()
|
||||
|
||||
res, err := global.DB.Exec(ctx, updateResultSql, updateResultArgs...)
|
||||
if err != nil {
|
||||
cr.log.Error("未能更新当前报表的核算状态", zap.Error(err))
|
||||
return false, err
|
||||
}
|
||||
if res.RowsAffected() == 0 {
|
||||
cr.log.Warn("未能保存当前报表的核算状态", zap.String("Report", rid))
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// 获取当前正在等待计算的核算任务ID列表
|
||||
func (cr _CalculateRepository) ListPendingTasks() ([]string, error) {
|
||||
cr.log.Info("获取当前正在等待计算的核算任务ID列表")
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
|
||||
var ids []string
|
||||
querySql, queryArgs, _ := cr.ds.
|
||||
From("report_task").
|
||||
Select("id").
|
||||
Where(goqu.C("status").Eq(model.REPORT_CALCULATE_TASK_STATUS_PENDING)).
|
||||
Prepared(true).ToSQL()
|
||||
if err := pgxscan.Select(ctx, global.DB, &ids, querySql, queryArgs...); err != nil {
|
||||
cr.log.Error("未能获取到当前正在等待计算的核算任务ID列表", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// 更新指定报表的核算状态
|
||||
func (cr _CalculateRepository) UpdateReportTaskStatus(rid string, status int16, message *string) (bool, error) {
|
||||
cr.log.Info("更新指定报表的核算状态", zap.String("Report", rid), zap.Int16("Status", status))
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
|
||||
currentTime := types.Now()
|
||||
updateSql, updateArgs, _ := cr.ds.
|
||||
Update("report_task").
|
||||
Set(goqu.Record{
|
||||
"status": status,
|
||||
"last_modified_at": currentTime,
|
||||
"message": message,
|
||||
}).
|
||||
Where(goqu.C("id").Eq(rid)).
|
||||
Prepared(true).ToSQL()
|
||||
res, err := global.DB.Exec(ctx, updateSql, updateArgs...)
|
||||
if err != nil {
|
||||
cr.log.Error("未能更新指定报表的核算状态", zap.Error(err))
|
||||
return false, err
|
||||
}
|
||||
if res.RowsAffected() == 0 {
|
||||
cr.log.Warn("未能保存指定报表的核算状态", zap.String("Report", rid))
|
||||
return false, nil
|
||||
}
|
||||
return res.RowsAffected() > 0, nil
|
||||
}
|
||||
|
||||
// 获取当前园区中所有公摊表计与商户表计之间的关联关系,包括已经解除的
|
||||
func (cr _CalculateRepository) GetAllPoolingMeterRelations(pid string, revokedAfter time.Time) ([]model.MeterRelation, error) {
|
||||
cr.log.Info("获取当前园区中所有公摊表计与商户表计之间的关联关系,包括已经解除的", zap.String("pid", pid), zap.Time("revokedAfter", revokedAfter))
|
||||
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
relationsSql, relationsArgs, _ := cr.ds.
|
||||
From(goqu.T("meter_relations")).
|
||||
Where(goqu.I("park_id").Eq(pid)).
|
||||
Where(goqu.Or(
|
||||
goqu.I("revoked_at").IsNull(),
|
||||
goqu.I("revoked_at").Gte(revokedAfter),
|
||||
)).ToSQL()
|
||||
|
||||
var meterRelation []model.MeterRelation
|
||||
|
||||
err := pgxscan.Select(ctx, global.DB, &meterRelation, relationsSql, relationsArgs...)
|
||||
if err != nil {
|
||||
cr.log.Error("获取当前园区中所有公摊表计与商户表计之间的关联关系,包括已经解除的出错", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
return meterRelation, nil
|
||||
}
|
||||
|
||||
// 获取当前园区中所有的商户与表计的关联关系,包括已经解除的
|
||||
func (cr _CalculateRepository) GetAllTenementMeterRelations(pid string, associatedBefore time.Time, disassociatedAfter time.Time) ([]model.TenementMeter, error) {
|
||||
cr.log.Info("获取当前园区中所有的商户与表计的关联关系,包括已经解除的", zap.String("pid", pid), zap.Time("associatedBefore", associatedBefore), zap.Time("disassociatedAfter", disassociatedAfter))
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
|
||||
relationsQuerySql, relationsQueryArgs, _ := cr.ds.
|
||||
From(goqu.T("tenement_meter")).
|
||||
Where(goqu.I("park_id").Eq(pid)).
|
||||
Where(goqu.And(
|
||||
goqu.I("associated_at").IsNotNull(),
|
||||
goqu.I("associated_at").Lte(associatedBefore),
|
||||
)).
|
||||
Where(goqu.And(
|
||||
goqu.Or(
|
||||
goqu.I("disassociated_at").IsNull(),
|
||||
goqu.I("disassociated_at").Gte(disassociatedAfter),
|
||||
),
|
||||
)).ToSQL()
|
||||
|
||||
var tenementMeter []model.TenementMeter
|
||||
|
||||
err := pgxscan.Select(ctx, global.DB, &tenementMeter, relationsQuerySql, relationsQueryArgs...)
|
||||
if err != nil {
|
||||
cr.log.Error("获取当前园区中所有的商户与表计的关联关系,包括已经解除的", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
fmt.Println("==", tenementMeter)
|
||||
return tenementMeter, nil
|
||||
|
||||
}
|
||||
|
||||
// 获取指定报表中所有涉及到的指定类型表计在核算时间段内的所有读数数据
|
||||
func (cr _CalculateRepository) GetMeterReadings(rid string, meterType int16) ([]model.MeterReading, error) {
|
||||
cr.log.Info("获取指定报表中所有涉及到的指定类型表计在核算时间段内的所有读数数据", zap.String("rid", rid), zap.Int16("meterType", meterType))
|
||||
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
|
||||
readingsQuerySql, readingsQueryArgs, _ := cr.ds.
|
||||
From(goqu.T("meter_reading").As(goqu.I("mr"))).
|
||||
Join(
|
||||
goqu.T("report").As("r"),
|
||||
goqu.On(goqu.I("r.park_id").Eq(goqu.I("mr.park_id"))),
|
||||
).
|
||||
Where(
|
||||
goqu.I("r.id").Eq(rid),
|
||||
goqu.I("mr.meter_type").Eq(meterType),
|
||||
// TODO:2023.08.02 此方法出错优先查看是否这里出问题
|
||||
goqu.L("?::date <@ ?", goqu.I("mr.read_at"), goqu.I("r.period")),
|
||||
).
|
||||
Order(goqu.I("mr.read_at").Asc()).Select(goqu.I("mr.*")).ToSQL()
|
||||
|
||||
var readings []model.MeterReading
|
||||
|
||||
err := pgxscan.Select(ctx, global.DB, &readings, readingsQuerySql, readingsQueryArgs...)
|
||||
if err != nil {
|
||||
cr.log.Error("获取指定报表中所有涉及到的指定类型表计在核算时间段内的所有读数数据出错", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
return readings, nil
|
||||
}
|
||||
|
||||
// 获取指定报表中所有涉及到的表计在核算起始日期前的最后一次读数
|
||||
func (cr _CalculateRepository) GetLastPeriodReadings(rid string, meterType int16) ([]model.MeterReading, error) {
|
||||
cr.log.Info("获取指定报表中所有涉及到的表计在核算起始日期前的最后一次读数", zap.String("rid", rid), zap.Int16("meterType", meterType))
|
||||
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
|
||||
readingsSql, readingsArgs, _ := cr.ds.
|
||||
From(goqu.T("meter_reading").As("mr")).
|
||||
Select(
|
||||
goqu.MAX("mr.read_at").As("read_at"),
|
||||
goqu.I("mr.park_id"),
|
||||
goqu.I("mr.meter_id"),
|
||||
goqu.I("mr.meter_type"),
|
||||
goqu.I("mr.ratio"),
|
||||
goqu.I("mr.overall"),
|
||||
goqu.I("mr.critical"),
|
||||
goqu.I("mr.peak"),
|
||||
goqu.I("mr.flat"),
|
||||
goqu.I("mr.valley"),
|
||||
).
|
||||
Join(
|
||||
goqu.T("report").As("r"),
|
||||
goqu.On(goqu.I("r.park_id").Eq(goqu.I("mr.park_id"))),
|
||||
).
|
||||
Where(
|
||||
goqu.I("r.id").Eq(rid),
|
||||
goqu.I("mr.meter_type").Eq(meterType),
|
||||
goqu.L(" read_at <= lower(r.period)"),
|
||||
).
|
||||
GroupBy(
|
||||
goqu.I("mr.park_id"),
|
||||
goqu.I("mr.meter_id"),
|
||||
goqu.I("mr.meter_type"),
|
||||
goqu.I("mr.ratio"),
|
||||
goqu.I("mr.overall"),
|
||||
goqu.I("mr.critical"),
|
||||
goqu.I("mr.peak"),
|
||||
goqu.I("mr.flat"),
|
||||
goqu.I("mr.valley"),
|
||||
goqu.I("r.period"),
|
||||
).ToSQL()
|
||||
|
||||
var readings []model.MeterReading
|
||||
err := pgxscan.Select(ctx, global.DB, &readings, readingsSql, readingsArgs...)
|
||||
if err != nil {
|
||||
cr.log.Error("获取指定报表中所有涉及到的表计在核算起始日期前的最后一次读数出错", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return readings, nil
|
||||
}
|
||||
|
||||
// 取得指定报表所涉及的所有商户信息
|
||||
func (cr _CalculateRepository) GetAllTenements(rid string) ([]model.Tenement, error) {
|
||||
cr.log.Info("取得指定报表所涉及的所有商户信息", zap.String("rid", rid))
|
||||
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
|
||||
tenementQuerySql, tenementQueryArgs, _ := cr.ds.
|
||||
From(goqu.T("tenement").As("t")).
|
||||
LeftJoin(
|
||||
goqu.T("park_building").As("b"),
|
||||
goqu.On(goqu.I("b.id").Eq(goqu.I("t.building"))),
|
||||
).
|
||||
Join(
|
||||
goqu.T("report").As("r"),
|
||||
goqu.On(goqu.I("r.park_id").Eq(goqu.I("t.park_id"))),
|
||||
).
|
||||
Select(
|
||||
goqu.I("t.*"),
|
||||
goqu.I("b.name").As("building_name"),
|
||||
).
|
||||
Where(
|
||||
goqu.I("r.id").Eq(rid),
|
||||
goqu.L("t.moved_in_at <= upper(r.period)"),
|
||||
).ToSQL()
|
||||
fmt.Println(tenementQuerySql)
|
||||
var tenements []model.Tenement
|
||||
err := pgxscan.Select(ctx, global.DB, &tenements, tenementQuerySql, tenementQueryArgs...)
|
||||
if err != nil {
|
||||
cr.log.Error("取得指定报表所涉及的所有商户信息出错", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
return tenements, nil
|
||||
}
|
||||
func (cr _CalculateRepository) ClearReportContent(tx pgx.Tx, rid string) error {
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
querysql, querarg, _ := cr.ds.Delete("report_summary").
|
||||
Where(goqu.C("report_id").Eq(rid)).ToSQL()
|
||||
_, err := tx.Exec(ctx, querysql, querarg...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
querysql, querarg, _ = cr.ds.Delete("report_public_consumption").
|
||||
Where(goqu.C("report_id").Eq(rid)).ToSQL()
|
||||
_, err = tx.Exec(ctx, querysql, querarg...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
querysql, querarg, _ = cr.ds.Delete("report_pooled_consumption").
|
||||
Where(goqu.C("report_id").Eq(rid)).ToSQL()
|
||||
_, err = tx.Exec(ctx, querysql, querarg...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
querysql, querarg, _ = cr.ds.Delete("report_tenement").
|
||||
Where(goqu.C("report_id").Eq(rid)).ToSQL()
|
||||
_, err = tx.Exec(ctx, querysql, querarg...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
func (cr _CalculateRepository) SaveReportPublics(tx pgx.Tx, ctx context.Context, rid string, meters []calculate.Meter) error {
|
||||
if len(meters) == 0 {
|
||||
// 如果没有公共表计则直接返回
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, meter := range meters {
|
||||
// 准备插入表达式
|
||||
insertExpr := cr.ds.Insert("report_public_consumption").
|
||||
Cols(
|
||||
"report_id", "park_meter_id", "overall", "critical", "peak", "flat", "valley",
|
||||
"loss_adjust", "consumption_total", "loss_adjust_total", "final_total",
|
||||
)
|
||||
// 添加值到插入表达式中
|
||||
overall, _ := json.Marshal(meter.Overall)
|
||||
criyical, _ := json.Marshal(meter.Critical)
|
||||
peak, _ := json.Marshal(meter.Peak)
|
||||
flat, _ := json.Marshal(meter.Flat)
|
||||
valley, _ := json.Marshal(meter.Valley)
|
||||
adjustLoss, _ := json.Marshal(meter.AdjustLoss)
|
||||
insertExpr = insertExpr.Vals(goqu.Vals{
|
||||
rid,
|
||||
meter.Code,
|
||||
overall,
|
||||
criyical,
|
||||
peak,
|
||||
flat,
|
||||
valley,
|
||||
adjustLoss,
|
||||
meter.Overall.Fee,
|
||||
meter.AdjustLoss.Fee,
|
||||
meter.Overall.Fee.Add(meter.AdjustLoss.Fee),
|
||||
})
|
||||
// 执行插入语句
|
||||
inserSql, insertArgs, _ := insertExpr.ToSQL()
|
||||
_, err := tx.Exec(ctx, inserSql, insertArgs...)
|
||||
if err != nil {
|
||||
_ = tx.Rollback(ctx)
|
||||
return fmt.Errorf("保存报表核算概要失败: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
func (cr _CalculateRepository) SaveReportSummary(tx pgx.Tx, ctx context.Context, summary calculate.Summary) error {
|
||||
// 构建插入表达式
|
||||
Overall, _ := json.Marshal(summary.Overall)
|
||||
Critical, _ := json.Marshal(summary.Critical)
|
||||
Peak, _ := json.Marshal(summary.Peak)
|
||||
Flat, _ := json.Marshal(summary.Flat)
|
||||
Valley, _ := json.Marshal(summary.Valley)
|
||||
AuthoizeLoss, _ := json.Marshal(summary.AuthoizeLoss)
|
||||
insertsql, insertArgs, err := cr.ds.Insert(goqu.T("report_summary")).
|
||||
Cols(
|
||||
"report_id", "overall", "critical", "peak", "flat", "valley",
|
||||
"loss", "loss_fee", "basic_fee", "basic_pooled_price_consumption", "basic_pooled_price_area",
|
||||
"adjust_fee", "adjust_pooled_price_consumption", "adjust_pooled_price_area",
|
||||
"loss_diluted_price", "loss_proportion", "final_diluted_overall",
|
||||
"consumption_fee", "authorize_loss", "overall_area", "total_consumption",
|
||||
).
|
||||
Vals(goqu.Vals{
|
||||
summary.ReportId, Overall, Critical, Peak, Flat,
|
||||
Valley, summary.Loss, summary.LossFee, summary.BasicFee,
|
||||
summary.BasicPooledPriceConsumption, summary.BasicPooledPriceArea,
|
||||
summary.AdjustFee, summary.AdjustPooledPriceConsumption, summary.AdjustPooledPriceArea,
|
||||
summary.LossDilutedPrice, summary.LossProportion, summary.FinalDilutedOverall,
|
||||
summary.ConsumptionFee, AuthoizeLoss, summary.OverallArea, summary.TotalConsumption,
|
||||
}).ToSQL()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
// 执行插入语句
|
||||
if _, err := tx.Exec(ctx, insertsql, insertArgs...); err != nil {
|
||||
cr.log.Error("保存报表核算概要失败。")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type NestedMeter struct {
|
||||
Overall model.ConsumptionUnit
|
||||
Critical model.ConsumptionUnit
|
||||
Peak model.ConsumptionUnit
|
||||
Flat model.ConsumptionUnit
|
||||
Valley model.ConsumptionUnit
|
||||
CoveredArea decimal.Decimal
|
||||
|
||||
// Add other fields here as needed
|
||||
}
|
||||
|
||||
func (cr _CalculateRepository) SaveReportPoolings(tx pgx.Tx,
|
||||
rid string,
|
||||
meters []calculate.Meter,
|
||||
relations []model.MeterRelation,
|
||||
tenements []calculate.Meter) error {
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
if len(meters) == 0 {
|
||||
return nil
|
||||
}
|
||||
relationsSlaves := make(map[string]bool)
|
||||
for _, r := range relations {
|
||||
relationsSlaves[r.SlaveMeter] = true
|
||||
}
|
||||
|
||||
tenementCodes := make(map[string]bool)
|
||||
for _, t := range tenements {
|
||||
tenementCodes[t.Code] = true
|
||||
}
|
||||
|
||||
for _, r := range relations {
|
||||
if _, ok := tenementCodes[r.SlaveMeter]; !ok {
|
||||
return errors.New("unknown tenement meter in active meter relations")
|
||||
}
|
||||
}
|
||||
|
||||
var insertQueries []goqu.InsertDataset
|
||||
for _, meter := range meters {
|
||||
submeters := make([]NestedMeter, 0)
|
||||
for _, r := range relations {
|
||||
if r.MasterMeter == meter.Code {
|
||||
for _, t := range tenements {
|
||||
if t.Code == r.SlaveMeter {
|
||||
submeters = append(submeters, NestedMeter{
|
||||
Overall: t.Overall,
|
||||
Critical: t.Critical,
|
||||
Peak: t.Peak,
|
||||
Flat: t.Flat,
|
||||
Valley: t.Valley,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
submetersJSON, err := json.Marshal(submeters)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
overall, _ := json.Marshal(meter.Overall)
|
||||
criyical, _ := json.Marshal(meter.Critical)
|
||||
peak, _ := json.Marshal(meter.Peak)
|
||||
flat, _ := json.Marshal(meter.Flat)
|
||||
valley, _ := json.Marshal(meter.Valley)
|
||||
|
||||
insertQuery := goqu.Insert("report_pooled_consumption").
|
||||
Cols("report_id", "pooled_meter_id", "overall", "critical", "peak", "flat", "valley", "pooled_area", "diluted").
|
||||
Vals(goqu.Vals{rid, meter.Code, overall, criyical, peak, flat, valley, meter.CoveredArea, submetersJSON})
|
||||
|
||||
insertQueries = append(insertQueries, *insertQuery)
|
||||
}
|
||||
|
||||
eg, _ := errgroup.WithContext(ctx)
|
||||
for _, insertQuery := range insertQueries {
|
||||
insertQuery := insertQuery // Capture loop variable
|
||||
eg.Go(func() error {
|
||||
sql, args, err := insertQuery.ToSQL()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec(ctx, sql, args...)
|
||||
return err
|
||||
})
|
||||
}
|
||||
return eg.Wait()
|
||||
}
|
||||
func (cr _CalculateRepository) SaveReportTenement(tx pgx.Tx, report model.ReportIndex, tenements []model.Tenement, tenementCharges []calculate.TenementCharge) error {
|
||||
if len(tenements) == 0 {
|
||||
// 如果没有商户则直接返回
|
||||
return nil
|
||||
}
|
||||
cr.log.Info("保存商户报表。")
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
insertQuery := cr.ds.
|
||||
Insert("report_tenement")
|
||||
var rows []goqu.Record
|
||||
for _, tenement := range tenements {
|
||||
tenementCharge := findTenementCharge(tenementCharges, tenement.Id)
|
||||
|
||||
tenementDetail, _ := json.Marshal(tenement)
|
||||
overallJSON, _ := json.Marshal(tenementCharge.Overall)
|
||||
criticalJSON, _ := json.Marshal(tenementCharge.Critical)
|
||||
peakJSON, _ := json.Marshal(tenementCharge.Peak)
|
||||
flatJSON, _ := json.Marshal(tenementCharge.Flat)
|
||||
valleyJSON, _ := json.Marshal(tenementCharge.Valley)
|
||||
lossJSON, _ := json.Marshal(tenementCharge.Loss)
|
||||
submetersJSON, _ := json.Marshal(convertToNestedMeters(tenementCharge.Submeters))
|
||||
poolingsJSON, _ := json.Marshal(convertToNestedMeters(tenementCharge.Poolings))
|
||||
|
||||
row := goqu.Record{
|
||||
"report_id": report.Id,
|
||||
"tenement_id": tenement.Id,
|
||||
"tenement_detail": tenementDetail,
|
||||
"calc_period": report.Period,
|
||||
"overall": overallJSON,
|
||||
"critical": criticalJSON,
|
||||
"peak": peakJSON,
|
||||
"flat": flatJSON,
|
||||
"valley": valleyJSON,
|
||||
"loss": lossJSON,
|
||||
"basic_fee_pooled": tenementCharge.BasicFee,
|
||||
"adjust_fee_pooled": tenementCharge.AdjustFee,
|
||||
"loss_fee_pooled": tenementCharge.LossPooled,
|
||||
"final_pooled": tenementCharge.PublicPooled,
|
||||
"final_charge": tenementCharge.FinalCharges,
|
||||
"meters": submetersJSON,
|
||||
"pooled": poolingsJSON,
|
||||
}
|
||||
|
||||
rows = append(rows, row)
|
||||
}
|
||||
|
||||
sql, params, err := insertQuery.Rows(rows).Prepared(true).ToSQL()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
_, err = tx.Exec(ctx, sql, params...)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// findTenementCharge 在 TenementCharges 切片中查找指定商户的核算内容
|
||||
func findTenementCharge(charges []calculate.TenementCharge, tenementID string) calculate.TenementCharge {
|
||||
for _, charge := range charges {
|
||||
if charge.Tenement == tenementID {
|
||||
return charge
|
||||
}
|
||||
}
|
||||
return calculate.TenementCharge{}
|
||||
}
|
||||
|
||||
// convertToNestedMeters 将 Meter 切片转换为 NestedMeter 切片
|
||||
func convertToNestedMeters(meters []*calculate.Meter) []NestedMeter {
|
||||
nestedMeters := []NestedMeter{}
|
||||
for _, meter := range meters {
|
||||
nestedMeters = append(nestedMeters, NestedMeter{
|
||||
Overall: meter.Overall,
|
||||
Critical: meter.Critical,
|
||||
Peak: meter.Peak,
|
||||
Flat: meter.Flat,
|
||||
Valley: meter.Valley,
|
||||
CoveredArea: meter.CoveredArea,
|
||||
})
|
||||
}
|
||||
return nestedMeters
|
||||
}
|
||||
152
repository/charge.go
Normal file
152
repository/charge.go
Normal file
@@ -0,0 +1,152 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"electricity_bill_calc/config"
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/logger"
|
||||
"electricity_bill_calc/model"
|
||||
"electricity_bill_calc/types"
|
||||
"fmt"
|
||||
|
||||
"github.com/doug-martin/goqu/v9"
|
||||
_ "github.com/doug-martin/goqu/v9/dialect/postgres"
|
||||
"github.com/georgysavva/scany/v2/pgxscan"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/samber/lo"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type _ChargeRepository struct {
|
||||
log *zap.Logger
|
||||
ds goqu.DialectWrapper
|
||||
}
|
||||
|
||||
var ChargeRepository = &_ChargeRepository{
|
||||
log: logger.Named("Repository", "Charge"),
|
||||
ds: goqu.Dialect("postgres"),
|
||||
}
|
||||
|
||||
// 分页查询用户的充值记录
|
||||
func (cr _ChargeRepository) FindCharges(page uint, beginTime, endTime *types.Date, keyword *string) ([]*model.UserChargeDetail, int64, error) {
|
||||
cr.log.Info("查询用户的充值记录。", logger.DateFieldp("beginTime", beginTime), logger.DateFieldp("endTime", endTime), zap.Stringp("keyword", keyword), zap.Uint("page", page))
|
||||
ctx, cancel := global.TimeoutContext()
|
||||
defer cancel()
|
||||
|
||||
chargeQuery := cr.ds.
|
||||
From(goqu.T("user_charge").As("c")).
|
||||
Join(goqu.T("user_detail").As("ud"), goqu.On(goqu.I("c.user_id").Eq(goqu.I("ud.id")))).
|
||||
Join(goqu.T("user").As("u"), goqu.On(goqu.I("ud.id").Eq(goqu.I("u.id")))).
|
||||
Select(
|
||||
"c.seq", "c.user_id", "ud.name", "c.fee", "c.discount", "c.amount", "c.charge_to",
|
||||
"c.settled", "c.settled_at", "c.cancelled", "c.cancelled_at", "c.refunded", "c.refunded_at", "c.created_at",
|
||||
)
|
||||
countQuery := cr.ds.
|
||||
From(goqu.T("user_charge").As("c")).
|
||||
Join(goqu.T("user_detail").As("ud"), goqu.On(goqu.I("c.user_id").Eq(goqu.I("ud.id")))).
|
||||
Join(goqu.T("user").As("u"), goqu.On(goqu.I("ud.id").Eq(goqu.I("u.id")))).
|
||||
Select(goqu.COUNT("*"))
|
||||
|
||||
if keyword != nil && len(*keyword) > 0 {
|
||||
pattern := fmt.Sprintf("%%%s%%", *keyword)
|
||||
chargeQuery = chargeQuery.Where(goqu.Or(
|
||||
goqu.I("ud.name").ILike(pattern),
|
||||
goqu.I("ud.abbr").ILike(pattern),
|
||||
goqu.I("u.username").ILike(pattern),
|
||||
))
|
||||
countQuery = countQuery.Where(goqu.Or(
|
||||
goqu.I("ud.name").ILike(pattern),
|
||||
goqu.I("ud.abbr").ILike(pattern),
|
||||
goqu.I("u.username").ILike(pattern),
|
||||
))
|
||||
}
|
||||
|
||||
if beginTime != nil {
|
||||
chargeQuery = chargeQuery.Where(goqu.I("c.created_at").Gte(beginTime.ToBeginningOfDate()))
|
||||
countQuery = countQuery.Where(goqu.I("c.created_at").Gte(beginTime.ToBeginningOfDate()))
|
||||
}
|
||||
|
||||
if endTime != nil {
|
||||
chargeQuery = chargeQuery.Where(goqu.I("c.created_at").Lte(endTime.ToEndingOfDate()))
|
||||
countQuery = countQuery.Where(goqu.I("c.created_at").Lte(endTime.ToEndingOfDate()))
|
||||
}
|
||||
|
||||
chargeQuery = chargeQuery.Order(goqu.I("c.created_at").Desc())
|
||||
|
||||
currentPostion := (page - 1) * config.ServiceSettings.ItemsPageSize
|
||||
chargeQuery = chargeQuery.Offset(currentPostion).Limit(config.ServiceSettings.ItemsPageSize)
|
||||
|
||||
chargeSql, chargeArgs, _ := chargeQuery.Prepared(true).ToSQL()
|
||||
countSql, countArgs, _ := countQuery.Prepared(true).ToSQL()
|
||||
|
||||
var (
|
||||
charges []*model.UserChargeDetail = make([]*model.UserChargeDetail, 0)
|
||||
total int64
|
||||
)
|
||||
if err := pgxscan.Select(ctx, global.DB, &charges, chargeSql, chargeArgs...); err != nil {
|
||||
cr.log.Error("查询用户的充值记录失败。", zap.Error(err))
|
||||
return make([]*model.UserChargeDetail, 0), 0, err
|
||||
}
|
||||
if err := pgxscan.Get(ctx, global.DB, &total, countSql, countArgs...); err != nil {
|
||||
cr.log.Error("查询用户的充值记录总数失败。", zap.Error(err))
|
||||
return make([]*model.UserChargeDetail, 0), 0, err
|
||||
}
|
||||
return charges, total, nil
|
||||
}
|
||||
|
||||
// 在用户充值记录中创建一条新的记录
|
||||
func (cr _ChargeRepository) CreateChargeRecord(tx pgx.Tx, ctx context.Context, uid string, fee, discount, amount *float64, chargeTo types.Date) (bool, error) {
|
||||
createQuery, createArgs, _ := cr.ds.
|
||||
Insert(goqu.T("user_charge")).
|
||||
Cols("user_id", "fee", "discount", "amount", "charge_to", "created_at").
|
||||
Vals(goqu.Vals{uid, fee, discount, amount, chargeTo, types.Now()}).
|
||||
Prepared(true).ToSQL()
|
||||
|
||||
rs, err := tx.Exec(ctx, createQuery, createArgs...)
|
||||
if err != nil {
|
||||
cr.log.Error("创建用户充值记录失败。", zap.Error(err))
|
||||
return false, err
|
||||
}
|
||||
return rs.RowsAffected() > 0, nil
|
||||
}
|
||||
|
||||
// 撤销用户的充值记录
|
||||
func (cr _ChargeRepository) CancelCharge(tx pgx.Tx, ctx context.Context, uid string, seq int64) (bool, error) {
|
||||
updateQuerySql, updateArgs, _ := cr.ds.
|
||||
Update(goqu.T("user_charge")).
|
||||
Set(goqu.Record{"cancelled": true, "cancelled_at": types.Now()}).
|
||||
Where(goqu.I("user_id").Eq(uid), goqu.I("seq").Eq(seq)).
|
||||
Prepared(true).ToSQL()
|
||||
|
||||
rs, err := tx.Exec(ctx, updateQuerySql, updateArgs...)
|
||||
if err != nil {
|
||||
cr.log.Error("撤销用户的充值记录失败。", zap.Error(err))
|
||||
return false, err
|
||||
}
|
||||
return rs.RowsAffected() > 0, nil
|
||||
}
|
||||
|
||||
// 检索用户最近有效的服务期限
|
||||
func (cr _ChargeRepository) LatestValidChargeTo(tx pgx.Tx, ctx context.Context, uid string) (*types.Date, error) {
|
||||
searchSql, searchArgs, _ := cr.ds.
|
||||
From(goqu.T("user_charge")).
|
||||
Select("charge_to").
|
||||
Where(
|
||||
goqu.I("settled").Eq(true),
|
||||
goqu.I("cancelled").Eq(false),
|
||||
goqu.I("refunded").Eq(false),
|
||||
goqu.I("user_id").Eq(uid),
|
||||
).
|
||||
Prepared(true).ToSQL()
|
||||
|
||||
var chargeTo []*types.Date
|
||||
if err := pgxscan.Select(ctx, tx, &chargeTo, searchSql, searchArgs...); err != nil {
|
||||
cr.log.Error("检索用户有效服务期限列表失败。", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
if len(chargeTo) == 0 {
|
||||
return nil, fmt.Errorf("无法找到用户最近的有效服务期限。")
|
||||
}
|
||||
lastCharge := lo.MaxBy(chargeTo, func(a, b *types.Date) bool { return a.Time.After(b.Time) })
|
||||
return lastCharge, nil
|
||||
}
|
||||
19
repository/god.go
Normal file
19
repository/god.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/logger"
|
||||
"github.com/doug-martin/goqu/v9"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type _GodModRepository struct {
|
||||
log *zap.Logger
|
||||
ds goqu.DialectWrapper
|
||||
}
|
||||
|
||||
var GodModRepository = _GodModRepository{
|
||||
log: logger.Named("Repository", "GodMod"),
|
||||
ds: goqu.Dialect("postgres"),
|
||||
}
|
||||
|
||||
// 删除指定园区中的表计和商户的绑定关系
|
||||
449
repository/god_mode.go
Normal file
449
repository/god_mode.go
Normal file
@@ -0,0 +1,449 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"electricity_bill_calc/global"
|
||||
"electricity_bill_calc/logger"
|
||||
"fmt"
|
||||
"github.com/doug-martin/goqu/v9"
|
||||
"github.com/georgysavva/scany/v2/pgxscan"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type _GMRepository struct {
|
||||
log *zap.Logger
|
||||
ds goqu.DialectWrapper
|
||||
}
|
||||
|
||||
var GMRepository = &_GMRepository{
|
||||
log: logger.Named("Repository", "GM"),
|
||||
ds: goqu.Dialect("postgres"),
|
||||
}
|
||||
|
||||
func (gm _GMRepository) DeleteMeterBinding(ctx context.Context, tx pgx.Tx, pid string, tenements []string, meterCodes ...[]string) error {
|
||||
DeleteQuery := gm.ds.From(goqu.T("tenement_meter")).
|
||||
Where(goqu.I("park_id").Eq(pid)).
|
||||
Delete()
|
||||
|
||||
if len(tenements) > 0 {
|
||||
DeleteQuery = DeleteQuery.
|
||||
Where(goqu.I("tenement_id").In(tenements))
|
||||
}
|
||||
|
||||
if len(meterCodes) > 0 {
|
||||
DeleteQuery = DeleteQuery.
|
||||
Where(goqu.I("meter_id").In(meterCodes))
|
||||
}
|
||||
|
||||
DeleteQuerySql, DeleteQueryArgs, _ := DeleteQuery.ToSQL()
|
||||
|
||||
_, err := tx.Exec(ctx, DeleteQuerySql, DeleteQueryArgs...)
|
||||
if err != nil {
|
||||
gm.log.Error("数据库在删除tenement_meter表数据中出错", zap.Error(err))
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gm _GMRepository) DeleteTenements(ctx context.Context, tx pgx.Tx, pid string, tenements ...[]string) error {
|
||||
DeleteTenements := gm.ds.
|
||||
From("tenement").
|
||||
Where(goqu.I("park_id").Eq(pid)).
|
||||
Delete()
|
||||
|
||||
fmt.Println(len(tenements))
|
||||
if len(tenements) > 0 {
|
||||
DeleteTenements = DeleteTenements.
|
||||
Where(goqu.I("id").In(tenements))
|
||||
}
|
||||
|
||||
DeleteTenementsSql, DeleteTenementsArgs, _ := DeleteTenements.ToSQL()
|
||||
|
||||
_, err := tx.Exec(ctx, DeleteTenementsSql, DeleteTenementsArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
gm.log.Error("删除商户信息出错", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gm _GMRepository) DeleteInvoices(ctx context.Context, tx pgx.Tx, parks string, val ...[]string) error {
|
||||
if len(val) > 0 {
|
||||
updateQuery, updateQueryArgs, _ := gm.ds.
|
||||
Update(goqu.T("report_tenement")).
|
||||
Set(goqu.Record{"invoice": nil}).
|
||||
Where(goqu.I("invoice").In(val)).
|
||||
Where(
|
||||
goqu.I("report_id").
|
||||
Eq(
|
||||
gm.ds.
|
||||
From(goqu.T("report")).
|
||||
Where(goqu.I("park_id").Eq(parks)),
|
||||
),
|
||||
).ToSQL()
|
||||
_, err := tx.Exec(ctx, updateQuery, updateQueryArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
gm.log.Error("更新发票记录出错", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
updateQuery, updateQueryArgs, _ := gm.ds.
|
||||
Update(goqu.T("report_tenement")).
|
||||
Set(goqu.Record{"invoice": nil}).
|
||||
Where(
|
||||
goqu.I("report_id").
|
||||
Eq(gm.ds.
|
||||
From(goqu.T("report")).
|
||||
Where(goqu.I("park_id").Eq(parks)),
|
||||
)).ToSQL()
|
||||
_, err := tx.Exec(ctx, updateQuery, updateQueryArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
gm.log.Error("更新发票记录出错", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
deleteQuery := gm.ds.
|
||||
From(goqu.T("invoices")).
|
||||
Where(goqu.I("park_id").Eq(parks)).
|
||||
Delete()
|
||||
if len(val) > 0 {
|
||||
deleteQuery.Where(goqu.I("invoice_code").In(val))
|
||||
}
|
||||
deleteQuerySql, deleteQueryArgs, _ := deleteQuery.ToSQL()
|
||||
|
||||
_, err := tx.Exec(ctx, deleteQuerySql, deleteQueryArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
gm.log.Error("删除指定园区发票记录出错", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (gm _GMRepository) DeleteMeterPoolings(ctx context.Context, tx pgx.Tx, parks string, val ...[]string) error {
|
||||
deleteQuery := gm.ds.
|
||||
Delete(goqu.T("meter_relations")).
|
||||
Where(goqu.I("park_id").Eq(parks))
|
||||
|
||||
if len(val) > 0 {
|
||||
deleteQuery = deleteQuery.
|
||||
Where(
|
||||
goqu.I("master_meter_id").In(val),
|
||||
goqu.Or(goqu.I("slave_meter_id").In(val)),
|
||||
)
|
||||
}
|
||||
deleteQuerySql, deleteQueryArgs, _ := deleteQuery.ToSQL()
|
||||
_, err := tx.Exec(ctx, deleteQuerySql, deleteQueryArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
gm.log.Error("删除指定园区中的表计分摊关系失败", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gm _GMRepository) DeleteMeters(ctx context.Context, tx pgx.Tx, parks string, val ...[]string) error {
|
||||
deleteQuery := gm.ds.
|
||||
Delete(goqu.T("meter_04kv")).
|
||||
Where(goqu.I("park_id").Eq(parks))
|
||||
|
||||
if len(val) > 0 {
|
||||
deleteQuery = deleteQuery.Where(goqu.I("code").In(val))
|
||||
}
|
||||
|
||||
deleteQuerySql, deleteQueryArgs, _ := deleteQuery.ToSQL()
|
||||
|
||||
_, err := tx.Exec(ctx, deleteQuerySql, deleteQueryArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
gm.log.Error("删除指定园区的符合条件的标记出错", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gm _GMRepository) DeleteReports(ctx context.Context, tx pgx.Tx, parks string, val ...[]string) error {
|
||||
var err error
|
||||
|
||||
if len(val) > 0 {
|
||||
deleteReportTenementQuerySql, deleteReportTenementQueryArgs, _ := gm.ds.
|
||||
Delete(goqu.T("report_tenement")).
|
||||
Where(goqu.I("report_id").In(
|
||||
gm.ds.
|
||||
From(goqu.T("report")).
|
||||
Where(goqu.I("park_id").Eq(parks)).
|
||||
Where(goqu.I("id").In(val)),
|
||||
)).ToSQL()
|
||||
_, err = tx.Exec(ctx, deleteReportTenementQuerySql, deleteReportTenementQueryArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
deleteReportPooledConsumptionQuerySql, deleteReportPooledConsumptionQueryArgs, _ := gm.ds.
|
||||
Delete(goqu.T("report_pooled_consumption")).
|
||||
Where(goqu.I("report_id").In(
|
||||
gm.ds.
|
||||
From(goqu.T("report")).
|
||||
Where(goqu.I("park_id").Eq(parks)).
|
||||
Where(goqu.I("id").In(val)),
|
||||
)).ToSQL()
|
||||
_, err = tx.Exec(ctx, deleteReportPooledConsumptionQuerySql, deleteReportPooledConsumptionQueryArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
deleteReportPublicConsumptionQuerySql, deleteReportPublicConsumptionQueryArgs, _ := gm.ds.
|
||||
Delete(goqu.T("report_public_consumption")).
|
||||
Where(goqu.I("report_id").In(
|
||||
gm.ds.
|
||||
From(goqu.T("report")).
|
||||
Where(goqu.I("park_id").Eq(parks)).
|
||||
Where(goqu.I("id").In(val)),
|
||||
)).ToSQL()
|
||||
_, err = tx.Exec(ctx, deleteReportPublicConsumptionQuerySql, deleteReportPublicConsumptionQueryArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
deleteReportSummaryQuerySql, deleteReportSummaryQueryArgs, _ := gm.ds.
|
||||
Delete(goqu.T("report_summary")).
|
||||
Where(goqu.I("report_id").In(
|
||||
gm.ds.
|
||||
From(goqu.T("report")).
|
||||
Where(goqu.I("park_id").Eq(parks)).
|
||||
Where(goqu.I("id").In(val)),
|
||||
)).ToSQL()
|
||||
_, err = tx.Exec(ctx, deleteReportSummaryQuerySql, deleteReportSummaryQueryArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
deleteReportTaskQuerySql, deleteReportTaskQueryArgs, _ := gm.ds.
|
||||
Delete(goqu.T("report_task")).
|
||||
Where(goqu.I("report_id").In(
|
||||
gm.ds.
|
||||
From(goqu.T("report")).
|
||||
Where(goqu.I("park_id").Eq(parks)).
|
||||
Where(goqu.I("id").In(val)),
|
||||
)).ToSQL()
|
||||
_, err = tx.Exec(ctx, deleteReportTaskQuerySql, deleteReportTaskQueryArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
deleteReportQuerySql, deleteReportQueryArgs, _ := gm.ds.
|
||||
Delete(goqu.T("report")).
|
||||
Where(goqu.I("park_id").Eq(parks)).
|
||||
Where(goqu.I("id").In(val)).ToSQL()
|
||||
_, err = tx.Exec(ctx, deleteReportQuerySql, deleteReportQueryArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
} else {
|
||||
deleteReportTenementQuerySql, deleteReportTenementQueryArgs, _ := gm.ds.
|
||||
Delete(goqu.T("report_tenement")).
|
||||
Where(goqu.I("report_id").In(
|
||||
gm.ds.
|
||||
From(goqu.T("report")).
|
||||
Where(goqu.I("park_id").Eq(parks)),
|
||||
)).ToSQL()
|
||||
_, err = tx.Exec(ctx, deleteReportTenementQuerySql, deleteReportTenementQueryArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
deleteReportPooledConsumptionQuerySql, deleteReportPooledConsumptionQueryArgs, _ := gm.ds.
|
||||
Delete(goqu.T("report_pooled_consumption")).
|
||||
Where(goqu.I("report_id").In(
|
||||
gm.ds.
|
||||
From(goqu.T("report")).
|
||||
Where(goqu.I("park_id").Eq(parks)),
|
||||
)).ToSQL()
|
||||
_, err = tx.Exec(ctx, deleteReportPooledConsumptionQuerySql, deleteReportPooledConsumptionQueryArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
deleteReportPublicConsumptionQuerySql, deleteReportPublicConsumptionQueryArgs, _ := gm.ds.
|
||||
Delete(goqu.T("report_public_consumption")).
|
||||
Where(goqu.I("report_id").In(
|
||||
gm.ds.
|
||||
From(goqu.T("report")).
|
||||
Where(goqu.I("park_id").Eq(parks)),
|
||||
)).ToSQL()
|
||||
_, err = tx.Exec(ctx, deleteReportPublicConsumptionQuerySql, deleteReportPublicConsumptionQueryArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
deleteReportSummaryQuerySql, deleteReportSummaryQueryArgs, _ := gm.ds.
|
||||
Delete(goqu.T("report_summary")).
|
||||
Where(goqu.I("report_id").In(
|
||||
gm.ds.
|
||||
From(goqu.T("report")).
|
||||
Where(goqu.I("park_id").Eq(parks)),
|
||||
)).ToSQL()
|
||||
_, err = tx.Exec(ctx, deleteReportSummaryQuerySql, deleteReportSummaryQueryArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
deleteReportTaskQuerySql, deleteReportTaskQueryArgs, _ := gm.ds.
|
||||
Delete(goqu.T("report_task")).
|
||||
Where(goqu.I("report_id").In(
|
||||
gm.ds.
|
||||
From(goqu.T("report")).
|
||||
Where(goqu.I("park_id").Eq(parks)),
|
||||
)).ToSQL()
|
||||
_, err = tx.Exec(ctx, deleteReportTaskQuerySql, deleteReportTaskQueryArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
deleteReportQuerySql, deleteReportQueryArgs, _ := gm.ds.
|
||||
Delete(goqu.T("report")).
|
||||
Where(goqu.I("park_id").Eq(parks)).ToSQL()
|
||||
_, err = tx.Exec(ctx, deleteReportQuerySql, deleteReportQueryArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gm _GMRepository) DeleteBuildings(ctx context.Context, tx pgx.Tx, parks string, val ...[]string) error {
|
||||
if len(val) > 0 {
|
||||
updateBulidingSql, updateBlidingArgs, _ := gm.ds.
|
||||
Update(goqu.T("tenement")).
|
||||
Set(goqu.Record{"building": nil}).
|
||||
Where(goqu.I("park_id").Eq(parks)).
|
||||
Where(goqu.I("building").In(
|
||||
gm.ds.
|
||||
From(goqu.I("park_building")).
|
||||
Where(goqu.I("park_id").Eq(parks)).
|
||||
Where(goqu.I("id").In(val)).
|
||||
Select(goqu.I("id")),
|
||||
)).ToSQL()
|
||||
_, err := tx.Exec(ctx, updateBulidingSql, updateBlidingArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
updateBulidingSql, updateBlidingArgs, _ := gm.ds.
|
||||
Update(goqu.T("tenement")).
|
||||
Set(goqu.Record{"building": nil}).
|
||||
Where(goqu.I("park_id").Eq(parks)).ToSQL()
|
||||
_, err := tx.Exec(ctx, updateBulidingSql, updateBlidingArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
deleteQuery := gm.ds.
|
||||
Delete(goqu.I("park_building")).
|
||||
Where(goqu.I("park_id").Eq(parks))
|
||||
|
||||
if len(val) > 0 {
|
||||
deleteQuery = deleteQuery.
|
||||
Where(goqu.I("id").In(val))
|
||||
}
|
||||
|
||||
deleteQuerySql, deleteQueryArgs, _ := deleteQuery.ToSQL()
|
||||
_, err := tx.Exec(ctx, deleteQuerySql, deleteQueryArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gm _GMRepository) DeleteParks(ctx context.Context, tx pgx.Tx, park []string) error {
|
||||
deleteParksSql, deleteParksArgs, _ := gm.ds.
|
||||
Delete(goqu.T("park")).
|
||||
Where(goqu.I("id").In(park)).ToSQL()
|
||||
|
||||
_, err := tx.Exec(ctx, deleteParksSql, deleteParksArgs...)
|
||||
if err != nil {
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gm _GMRepository) ListAllParkIdsInUser(ctx context.Context, tx pgx.Tx, uid string) ([]string, error) {
|
||||
SearchParkIdsSql, SearchParkIdsArgs, _ := gm.ds.
|
||||
From(goqu.T("park")).
|
||||
Where(goqu.I("user_id").Eq(uid)).
|
||||
Select(goqu.I("id")).ToSQL()
|
||||
var pids []string
|
||||
err := pgxscan.Select(ctx, global.DB, &pids, SearchParkIdsSql, SearchParkIdsArgs...)
|
||||
if err != nil {
|
||||
gm.log.Error("查询["+uid+"]用户下的所有园区失败", zap.Error(err))
|
||||
tx.Rollback(ctx)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pids, nil
|
||||
}
|
||||
|
||||
func (gm _GMRepository) DeleteUsers(ctx context.Context, tx pgx.Tx, uid string) error {
|
||||
var err error
|
||||
//删除用户关联
|
||||
DeleteUserChargeSql, DeleteUserChargeArgs, _ := gm.ds.
|
||||
Delete(goqu.T("user_charge")).
|
||||
Where(goqu.I("id").Eq(uid)).ToSQL()
|
||||
|
||||
_, err = tx.Exec(ctx,DeleteUserChargeSql,DeleteUserChargeArgs...)
|
||||
if err != nil {
|
||||
gm.log.Error("user_charge表关联出错",zap.Error(err))
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
//删除用户详细信息
|
||||
DeleteUserDetailSql, DeleteUserDetailArgs,_ := gm.ds.
|
||||
Delete(goqu.T("user_detail")).
|
||||
Where(goqu.I("id").Eq(uid)).ToSQL()
|
||||
_, err = tx.Exec(ctx,DeleteUserDetailSql,DeleteUserDetailArgs...)
|
||||
if err != nil {
|
||||
gm.log.Error("user_detail表详细信息出错",zap.Error(err))
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
//删除用户基础信息
|
||||
DeleteUserSql, DeleteUserArgs,_ := gm.ds.
|
||||
Delete(goqu.T("users")).
|
||||
Where(goqu.I("id").Eq(uid)).ToSQL()
|
||||
_, err = tx.Exec(ctx,DeleteUserSql,DeleteUserArgs...)
|
||||
if err != nil {
|
||||
gm.log.Error("user表基础信息出错",zap.Error(err))
|
||||
tx.Rollback(ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user