提交 fd4382bb 作者: 史连宁

init

上级
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="smopest@218.94.154.74" uuid="0d474d38-6c5f-4cb8-b874-3073746729ec">
<driver-ref>sqlserver.ms</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>com.microsoft.sqlserver.jdbc.SQLServerDriver</jdbc-driver>
<jdbc-url>jdbc:sqlserver://218.94.154.74:39782;database=smopest</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/pestiot.iml" filepath="$PROJECT_DIR$/.idea/pestiot.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?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>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/solutions/countEquipmentAvailability.go" dialect="GenericSQL" />
<file url="PROJECT" dialect="SQL92" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
# 一、打包
## 1.linux包
> set GOARCH=amd64
> go env -w GOARCH=amd64
> set GOOS=linux
> go env -w GOOS=linux
> go build main.go
## 2.windows包
> set GOARCH=amd64
> go env -w GOARCH=amd64
> set GOOS=windows
> go env -w GOOS=windows
> go build main.go
package eneity
type Column struct {
Name string
Prop string
}
package eneity
import (
"database/sql"
"errors"
"fmt"
)
type DbInfo struct {
Server string
User string
Password string
Port int32
Database string
Dbtype string
}
func (db *DbInfo) GetConnect() (*sql.DB, error) {
if db.Dbtype == "mssql" {
connString := fmt.Sprintf("server=%s;port=%d;user id=%s;password=%s;database=%s", db.Server, db.Port, db.User, db.Password, db.Database)
return sql.Open("mssql", connString)
}
return nil, errors.New("this dbtype is not supported")
}
module pestiot
go 1.19
require (
github.com/deckarep/golang-set v1.8.0
github.com/denisenkom/go-mssqldb v0.12.3
github.com/levigross/grequests v0.0.0-20221222020224-9eee758d18d5
github.com/xuri/excelize/v2 v2.8.0
)
require (
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/richardlehane/mscfb v1.0.4 // indirect
github.com/richardlehane/msoleps v1.0.3 // indirect
github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca // indirect
github.com/xuri/nfp v0.0.0-20230819163627-dc951e3ffe1a // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/text v0.12.0 // indirect
)
github.com/Azure/azure-sdk-for-go/sdk/azcore v0.19.0/go.mod h1:h6H6c8enJmmocHUbLiiGY6sx7f9i+X3m1CHdd5c6Rdw=
github.com/Azure/azure-sdk-for-go/sdk/azidentity v0.11.0/go.mod h1:HcM1YX14R7CJcghJGOYCgdezslRSVzqwLf/q+4Y2r/0=
github.com/Azure/azure-sdk-for-go/sdk/internal v0.7.0/go.mod h1:yqy467j36fJxcRV2TzfVZ1pCb5vxm4BtZPUdYWe/Xo8=
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 v1.8.0 h1:sk9/l/KqpunDwP7pSjUg0keiOOLEnOBHzykLrsPppp4=
github.com/deckarep/golang-set v1.8.0/go.mod h1:5nI87KwE7wgsBU1F4GKAw2Qod7p5kyS383rP6+o6qqo=
github.com/denisenkom/go-mssqldb v0.12.3 h1:pBSGx9Tq67pBOTLmxNuirNTeB8Vjmf886Kx+8Y+8shw=
github.com/denisenkom/go-mssqldb v0.12.3/go.mod h1:k0mtMFOnU+AihqFxPMiF05rtiDrorD1Vrm1KEz5hxDo=
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/levigross/grequests v0.0.0-20221222020224-9eee758d18d5 h1:AsF9Q1mQoyLv0HzvHFW7O+19dHilOcKU74k7E5ufI1A=
github.com/levigross/grequests v0.0.0-20221222020224-9eee758d18d5/go.mod h1:XfzeIE2WC7CGDhlZJY/rUdqUPy0IPcyI6hoIjhAMNbQ=
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
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/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/richardlehane/mscfb v1.0.4 h1:WULscsljNPConisD5hR0+OyZjwK46Pfyr6mPu5ZawpM=
github.com/richardlehane/mscfb v1.0.4/go.mod h1:YzVpcZg9czvAuhk9T+a3avCpcFPMUWm7gK3DypaEsUk=
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/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/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.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca h1:uvPMDVyP7PXMMioYdyPH+0O+Ta/UO1WFfNYMO3Wz0eg=
github.com/xuri/efp v0.0.0-20230802181842-ad255f2331ca/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
github.com/xuri/excelize/v2 v2.8.0 h1:Vd4Qy809fupgp1v7X+nCS/MioeQmYVVzi495UCTqB7U=
github.com/xuri/excelize/v2 v2.8.0/go.mod h1:6iA2edBTKxKbZAa7X5bDhcCg51xdOn1Ar5sfoXRGrQg=
github.com/xuri/nfp v0.0.0-20230819163627-dc951e3ffe1a h1:Mw2VNrNNNjDtw68VsEj2+st+oCSn4Uz7vZw6TbhcV1o=
github.com/xuri/nfp v0.0.0-20230819163627-dc951e3ffe1a/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/image v0.11.0 h1:ds2RoQvBvYTiJkwpSFDwCcDFNX7DqjL2WsUgTNk0Ooo=
golang.org/x/image v0.11.0/go.mod h1:bglhjqbqVuEb9e9+eNR45Jfu7D+T4Qan+NhQk8Ck2P8=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210610132358-84b48f89b13b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/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/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/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-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.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.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.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
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.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
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=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
package main
import (
"flag"
"fmt"
mapset "github.com/deckarep/golang-set"
"pestiot/solutions"
"pestiot/utils"
"strings"
)
// 命令行接收参数
var (
provIds string
beginDate string
endDate string
devIds string
solution string
mentioneds string
)
// 获取命令行参数
func getCmdParams() bool {
flag.StringVar(&provIds, "provIds", "", "省份编号(必填)(e.g., 650000,340000)")
flag.StringVar(&beginDate, "beginDate", "", "开始时间(必填)(e.g., 2022-07-01)")
flag.StringVar(&endDate, "endDate", "", "结束时间(必填)(e.g., 2022-09-30)")
flag.StringVar(&devIds, "devIds", "", "设备编号(可选)(e.g., 129301230102,12039912093,21399422244)")
flag.StringVar(&solution, "solution", "", "解决方案(必填)(e.g., 01)\n01 计算设备可用率\n02 灯诱及性诱日统计")
flag.StringVar(&mentioneds, "mentioneds", "", "消息提醒人员(可选)(e.g., gpguo@jinhetech.com,eszhong@jinhetech.com)")
flag.Parse()
if provIds == "" || beginDate == "" || endDate == "" || solution == "" {
fmt.Println("必要参数缺失,请使用 -help 命令查看入参!")
return false
} else {
return true
}
}
func main() {
var versionCode int32 = 0
utils.CheckUpdate(versionCode)
if !getCmdParams() {
return
}
devSet := mapset.NewSet()
if devIds != "" {
for _, v := range strings.Split(devIds, ",") {
devSet.Add(v)
}
}
var mentionedList []string
if mentioneds != "" {
for _, v := range strings.Split(mentioneds, ",") {
mentionedList = append(mentionedList, v)
}
}
for _, provId := range strings.Split(provIds, ",") {
fmt.Println(provId)
var filepath, msg string
if solution == "01" { // 计算设备可用率
filepath, msg = solutions.CountEquipmentAvailability(provId, beginDate, endDate, devSet)
} else if solution == "02" {
filepath, msg = solutions.LightAndSexLureStatistics(provId, beginDate, endDate)
}
if filepath != "" {
utils.WechatFileUpload(filepath, msg, &mentionedList, nil)
}
}
}
package solutions
import (
"database/sql"
"fmt"
mapset "github.com/deckarep/golang-set"
_ "github.com/denisenkom/go-mssqldb"
"github.com/xuri/excelize/v2"
"pestiot/eneity"
"pestiot/static"
"pestiot/utils"
"time"
)
// 表头
var cols = []eneity.Column{
{"城市名", "CityName"},
{"县名", "AreaName"},
{"账号名", "DeptShort"},
{"监测点名称", "SiteName"},
{"厂商名称", "FactName"},
{"设备类型", "DevTypeName"},
{"设备名称", "DevName"},
{"设备编号", "DevID"},
{"设备状态", "DevStatusName"},
{"厂商编号", "FactDev"},
{"厂商编号", "DevCode"},
}
func CountEquipmentAvailability(provId, beginDate, endDate string, devSet mapset.Set) (string, string) {
dateList := utils.GetBetweenDates(beginDate, endDate)
columnList := cols
for _, v := range dateList {
columnList = append(columnList, eneity.Column{Name: v, Prop: v})
}
dbInfo := &eneity.DbInfo{Server: static.Server, User: static.User, Password: static.Password, Port: int32(static.Port), Database: static.Database, Dbtype: static.Dbtype}
db, err := dbInfo.GetConnect()
if err != nil {
fmt.Println("sql连接异常:", err)
return "", ""
}
sqllan := fmt.Sprintf(
"select t1.DevID, t1.DevName, t2.DeptShort, t2.CityName, t2.AreaName, t3.SiteName, t4.DevTypeName, t5.FactName, t6.FactDev, t6.DevCode, t7.DevStatusName from T_Bas_Dev t1 left join T_Con_Dev t6 on t1.DevID = t6.DevID, T_Bas_Dept t2, T_Bas_Site t3, T_Pmt_Dev_Type t4, T_Bas_Factory t5, T_Pmt_Dev_status t7 where t1.DeptID = t2.DeptID and t1.MonSite = t3.MonSite and t1.DevType = t4.DevType and t1.FactID = t5.FactID and t1.ProvID = '%s' and t1.DevStatus = t7.DevStatus order by DevID",
provId)
fmt.Println(sqllan)
rows, err := db.Query(sqllan)
if err != nil {
fmt.Println("获取设备数据异常:", err)
return "", ""
}
var devList []string
dataMap := make(map[string]map[string]string)
dataCountMap := make(map[string]int32)
days := len(dateList)
for rows.Next() {
var (
CityName sql.NullString
AreaName sql.NullString
DeptShort sql.NullString
SiteName sql.NullString
FactName sql.NullString
DevTypeName sql.NullString
DevName sql.NullString
DevID sql.NullString
DevStatusName sql.NullString
FactDev sql.NullString
DevCode sql.NullString
)
err := rows.Scan(&DevID, &DevName, &DeptShort, &CityName, &AreaName, &SiteName, &DevTypeName, &FactName, &FactDev, &DevCode, &DevStatusName)
if err != nil {
fmt.Println("读取设备数据异常异常:", err)
return "", ""
}
if DevID.String == "" {
continue
}
if devSet.Cardinality() > 0 && !devSet.Contains(DevID.String) {
continue
}
devList = append(devList, DevID.String)
devInfo := make(map[string]string)
devInfo["DevID"] = DevID.String
devInfo["DevName"] = DevName.String
devInfo["DeptShort"] = DeptShort.String
devInfo["CityName"] = CityName.String
devInfo["AreaName"] = AreaName.String
devInfo["SiteName"] = SiteName.String
devInfo["DevTypeName"] = DevTypeName.String
devInfo["FactName"] = FactName.String
devInfo["FactDev"] = FactDev.String
devInfo["DevCode"] = DevCode.String
devInfo["DevStatusName"] = DevStatusName.String
dataMap[DevID.String] = devInfo
}
sqllan = fmt.Sprintf("select DevID, DataYMD, sum(num) Num from (select DevID, DataYMD, count(*) num from T_Dat_Detail_%s WITH(NOLOCK) where DataYMD >= '%s' and DataYMD <= '%s' group by DevID, DataYMD union all select DevID, DataYMD, count(*) num from T_Dat_Image_%s WITH(NOLOCK) where DataYMD >= '%s' and DataYMD <= '%s' group by DevID, DataYMD)a group by DevID, DataYMD", provId, dateList[0], dateList[days-1], provId, dateList[0], dateList[days-1])
fmt.Println(sqllan)
rows, err = db.Query(sqllan)
if err != nil {
fmt.Println("获取设备统计数据异常:", err)
return "", ""
} else {
fmt.Println("获取到数据")
}
for rows.Next() {
var (
DevID sql.NullString
DataYMD sql.NullString
Num sql.NullInt32
)
err := rows.Scan(&DevID, &DataYMD, &Num)
if err != nil {
fmt.Println("读取设备统计数据异常:", err)
return "", ""
}
devId := DevID.String
data, ok := dataMap[devId]
if !ok {
continue
}
data[DataYMD.String] = fmt.Sprintf("%d", Num.Int32)
if dataCount, ok := dataCountMap[devId]; ok {
dataCountMap[devId] = dataCount + 1
} else {
dataCountMap[devId] = 1
}
}
xlsx := excelize.NewFile()
var header []interface{}
for _, col := range columnList {
header = append(header, col.Name)
}
header = append(header, "设备数据联通率(%)")
err = xlsx.SetSheetRow("Sheet1", "A1", &header)
if err != nil {
fmt.Println("插入表头异常:", err)
return "", ""
}
for idx, devId := range devList {
var info []interface{}
data := dataMap[devId]
for _, col := range columnList {
if v, ok := data[col.Prop]; ok {
info = append(info, v)
} else {
info = append(info, "0")
}
}
if data["DevTypeName"] == "视频监控" {
info = append(info, "/")
} else {
if num, ok := dataCountMap[devId]; ok {
info = append(info, fmt.Sprintf("%.2f", 100*float64(num)/float64(days)))
} else {
info = append(info, "0.00")
}
}
row := fmt.Sprintf("A%d", idx+2)
err := xlsx.SetSheetRow("Sheet1", row, &info)
if err != nil {
fmt.Println(fmt.Sprintf("插入行%s异常:", row), err)
}
}
filepath := fmt.Sprintf("./file/[result]%s设备(%s~%s)可用率统计结果_%d.xlsx", static.Citys[provId], beginDate, endDate, time.Now().Unix())
err = xlsx.SaveAs(filepath)
if err != nil {
fmt.Println("保存excel异常:", err)
return "", ""
}
err = db.Close()
if err != nil {
fmt.Println("数据库连接关闭异常:", err)
return "", ""
}
msg := fmt.Sprintf("%s设备(%s~%s)可用率统计结果可见附件,请尽快完成设备排查并完成数据补录。", static.Citys[provId], beginDate, endDate)
return filepath, msg
}
package solutions
import (
"database/sql"
"fmt"
mapset "github.com/deckarep/golang-set"
"github.com/xuri/excelize/v2"
"pestiot/eneity"
"pestiot/static"
"pestiot/utils"
"time"
)
func LightAndSexLureStatistics(provId, beginDate, endDate string) (string, string) {
dateList := utils.GetBetweenDates(beginDate, endDate)
days := len(dateList)
dbInfo := &eneity.DbInfo{Server: static.Server, User: static.User, Password: static.Password, Port: int32(static.Port), Database: static.Database, Dbtype: static.Dbtype}
db, err := dbInfo.GetConnect()
if err != nil {
fmt.Println("sql连接异常:", err)
return "", ""
}
// 获取设备信息
sqllan := fmt.Sprintf(
"select t1.DevID, t1.DevName, t1.SourceID, t2.CityName, t2.AreaName, t3.SiteName, t4.DevTypeName from T_Bas_Dev t1, T_Bas_Dept t2, T_Bas_Site t3, T_Pmt_Dev_Type t4 where t1.DeptID = t2.DeptID and t1.MonSite = t3.MonSite and t1.DevType = t4.DevType and t1.ProvID = '%s' order by DevID",
provId)
rows, err := db.Query(sqllan)
if err != nil {
fmt.Println("获取设备数据异常:", err)
return "", ""
}
var lightDevList []map[string]string
var sexDevList []map[string]string
for rows.Next() {
var (
CityName sql.NullString
AreaName sql.NullString
SiteName sql.NullString
DevTypeName sql.NullString
DevName sql.NullString
DevID sql.NullString
SourceID sql.NullString
)
err = rows.Scan(&DevID, &DevName, &SourceID, &CityName, &AreaName, &SiteName, &DevTypeName)
if err != nil {
fmt.Println("读取设备数据异常异常:", err)
return "", ""
}
devInfo := make(map[string]string)
devInfo["DevID"] = DevID.String
devInfo["DevName"] = DevName.String
devInfo["CityName"] = CityName.String
devInfo["AreaName"] = AreaName.String
devInfo["SiteName"] = SiteName.String
devInfo["DevTypeName"] = DevTypeName.String
devInfo["SourceID"] = SourceID.String
if DevTypeName.String == "虫情测报灯" {
lightDevList = append(lightDevList, devInfo)
} else if DevTypeName.String == "性诱监测仪" {
sexDevList = append(sexDevList, devInfo)
}
}
// 获取灯诱数据
sqllan = fmt.Sprintf(
"select a.DevID, a.DataYMD, b.MonItemName, a.DevCount from (select a.DevID, a.DataYMD, b.MonItem, sum(b.DevCount) DevCount from T_Dat_Image_%s a, T_Dat_Pest_Count b, T_Bas_Dev c where a.DevID = c.DevID and c.DevType = '07' and a.DataYMD >= '%s' and a.DataYMD <= '%s' and a.ImageCode = b.ImageUrl group by a.DevID, a.DataYMD, b.MonItem)a, T_Std_Item b where a.MonItem = b.MonItem",
provId, dateList[0], dateList[days-1])
rows, err = db.Query(sqllan)
if err != nil {
fmt.Println("获取灯诱数据异常:", err)
return "", ""
}
lightDevDateDataMap := make(map[string]map[string]map[string]float64)
lightDateDataMap := make(map[string]map[string]float64)
lightMonitemSet := mapset.NewSet()
for rows.Next() {
var (
DevID sql.NullString
DataYMD sql.NullString
MonItemName sql.NullString
DevCount sql.NullFloat64
)
err = rows.Scan(&DevID, &DataYMD, &MonItemName, &DevCount)
if err != nil {
fmt.Println("读取灯诱数据异常异常:", err)
return "", ""
}
if _, ok := lightDevDateDataMap[DevID.String]; !ok {
lightDevDateDataMap[DevID.String] = make(map[string]map[string]float64)
}
if _, ok := lightDevDateDataMap[DevID.String][DataYMD.String]; !ok {
lightDevDateDataMap[DevID.String][DataYMD.String] = make(map[string]float64)
lightDevDateDataMap[DevID.String][DataYMD.String]["sum"] = 0
}
if _, ok := lightDevDateDataMap[DevID.String][DataYMD.String][MonItemName.String]; !ok {
lightDevDateDataMap[DevID.String][DataYMD.String][MonItemName.String] = 0
}
lightDevDateDataMap[DevID.String][DataYMD.String][MonItemName.String] += DevCount.Float64
lightDevDateDataMap[DevID.String][DataYMD.String]["sum"] += DevCount.Float64
if _, ok := lightDateDataMap[DataYMD.String]; !ok {
lightDateDataMap[DataYMD.String] = make(map[string]float64)
}
if _, ok := lightDateDataMap[DataYMD.String][MonItemName.String]; !ok {
lightDateDataMap[DataYMD.String][MonItemName.String] = 0
lightDateDataMap[DataYMD.String]["sum"] = 0
}
lightDateDataMap[DataYMD.String][MonItemName.String] += DevCount.Float64
lightDateDataMap[DataYMD.String]["sum"] += DevCount.Float64
lightMonitemSet.Add(MonItemName.String)
}
// 获取性诱数据
sqllan = fmt.Sprintf(
"select a.DevID, a.DataYMD, c.MonItemName, sum(a.DataValue) DevCount from T_Dat_Detail_%s a, T_Bas_Dev b, T_Std_Item c where a.DevID = b.DevID and b.DevType = '09' and a.MonItem = c.MonItem and c.MonType = '03' and a.DataYMD >= '%s' and a.DataYMD <= '%s' group by a.DevID, a.DataYMD, c.MonItemName",
provId, dateList[0], dateList[days-1])
rows, err = db.Query(sqllan)
if err != nil {
fmt.Println("获取灯诱数据异常:", err)
return "", ""
}
sexDevDateDataMap := make(map[string]map[string]map[string]float64)
sexDateDataMap := make(map[string]map[string]float64)
sexMonitemSet := mapset.NewSet()
for rows.Next() {
var (
DevID sql.NullString
DataYMD sql.NullString
MonItemName sql.NullString
DevCount sql.NullFloat64
)
err = rows.Scan(&DevID, &DataYMD, &MonItemName, &DevCount)
if err != nil {
fmt.Println("读取灯诱数据异常异常:", err)
return "", ""
}
if _, ok := sexDevDateDataMap[DevID.String]; !ok {
sexDevDateDataMap[DevID.String] = make(map[string]map[string]float64)
}
if _, ok := sexDevDateDataMap[DevID.String][DataYMD.String]; !ok {
sexDevDateDataMap[DevID.String][DataYMD.String] = make(map[string]float64)
sexDevDateDataMap[DevID.String][DataYMD.String]["sum"] = 0
}
if _, ok := sexDevDateDataMap[DevID.String][DataYMD.String][MonItemName.String]; !ok {
sexDevDateDataMap[DevID.String][DataYMD.String][MonItemName.String] = 0
}
sexDevDateDataMap[DevID.String][DataYMD.String][MonItemName.String] += DevCount.Float64
sexDevDateDataMap[DevID.String][DataYMD.String]["sum"] += DevCount.Float64
if _, ok := sexDateDataMap[DataYMD.String]; !ok {
sexDateDataMap[DataYMD.String] = make(map[string]float64)
}
if _, ok := sexDateDataMap[DataYMD.String][MonItemName.String]; !ok {
sexDateDataMap[DataYMD.String][MonItemName.String] = 0
sexDateDataMap[DataYMD.String]["sum"] = 0
}
sexDateDataMap[DataYMD.String][MonItemName.String] += DevCount.Float64
sexDateDataMap[DataYMD.String]["sum"] += DevCount.Float64
sexMonitemSet.Add(MonItemName.String)
}
xlsx := excelize.NewFile()
curSheetName := "灯诱逐设备统计"
err = xlsx.SetSheetName("Sheet1", curSheetName)
if err != nil {
fmt.Println("修改Sheet1名称错误")
}
// 生成灯诱统计
lightDevColumnList := []eneity.Column{
{"市", "CityName"},
{"县", "AreaName"},
{"监测点", "SiteName"},
{"设备名称", "DevName"},
{"设备类型", "DevTypeName"},
{"设备编号", "DevID"},
{"设备源编号", "SourceID"},
}
lightMonitemColumnList := []eneity.Column{
{"该设备当日累计虫量", "sum"},
}
header := []interface{}{"市", "县", "监测点", "设备名称", "设备类型", "设备编号", "设备源编号", "日期", "该设备当日累计虫量"}
for _, monitem := range lightMonitemSet.ToSlice() {
lightMonitemColumnList = append(lightMonitemColumnList, eneity.Column{
Name: monitem.(string), Prop: monitem.(string),
})
header = append(header, monitem.(string))
}
err = xlsx.SetSheetRow(curSheetName, "A1", &header)
if err != nil {
fmt.Println("生成灯诱单灯表头错误!")
return "", ""
}
row := 2
for _, dev := range lightDevList {
for _, date := range dateList {
var data []interface{}
for _, col := range lightDevColumnList {
data = append(data, dev[col.Prop])
}
data = append(data, date)
for _, col := range lightMonitemColumnList {
monitem := col.Prop
if v, ok := lightDevDateDataMap[dev["DevID"]][date][monitem]; ok {
data = append(data, v)
} else {
data = append(data, "/")
}
}
err = xlsx.SetSheetRow(curSheetName, fmt.Sprintf("A%d", row), &data)
if err != nil {
fmt.Println("插入灯诱单灯错误!", err)
return "", ""
}
row += 1
}
}
// 生成性诱统计
curSheetName = "性诱逐设备统计"
_, err = xlsx.NewSheet(curSheetName)
if err != nil {
fmt.Println("新建性诱单灯Sheet错误!", err)
return "", ""
}
sexDevColumnList := []eneity.Column{
{"市", "CityName"},
{"县", "AreaName"},
{"监测点", "SiteName"},
{"设备名称", "DevName"},
{"设备类型", "DevTypeName"},
{"设备编号", "DevID"},
{"设备源编号", "SourceID"},
}
sexMonitemColumnList := []eneity.Column{
{"该设备当日累计虫量", "sum"},
}
header = []interface{}{"市", "县", "监测点", "设备名称", "设备类型", "设备编号", "设备源编号", "日期", "该设备当日累计虫量"}
for _, monitem := range sexMonitemSet.ToSlice() {
sexMonitemColumnList = append(sexMonitemColumnList, eneity.Column{
Name: monitem.(string), Prop: monitem.(string),
})
header = append(header, monitem.(string))
}
err = xlsx.SetSheetRow(curSheetName, "A1", &header)
if err != nil {
fmt.Println("生成性诱单灯表头错误!", err)
return "", ""
}
row = 2
for _, dev := range sexDevList {
for _, date := range dateList {
var data []interface{}
for _, col := range sexDevColumnList {
data = append(data, dev[col.Prop])
}
data = append(data, date)
for _, col := range sexMonitemColumnList {
monitem := col.Prop
if v, ok := sexDevDateDataMap[dev["DevID"]][date][monitem]; ok {
data = append(data, v)
} else {
data = append(data, "/")
}
}
err = xlsx.SetSheetRow(curSheetName, fmt.Sprintf("A%d", row), &data)
if err != nil {
fmt.Println("插入性诱单灯错误!", err)
return "", ""
}
row += 1
}
}
// 逐日合计
curSheetName = "全自治区逐日灯诱和性诱数据统计"
_, err = xlsx.NewSheet(curSheetName)
if err != nil {
fmt.Println("新建逐日合计Sheet错误!", err)
return "", ""
}
allMonitemSet := lightMonitemSet.Union(sexMonitemSet)
allColumnList := []eneity.Column{
{"当日总计", "sum"},
}
header = []interface{}{"日期", "当日总计", "性诱当日总计", "灯诱当日总计"}
for _, monitem := range allMonitemSet.ToSlice() {
allColumnList = append(allColumnList, eneity.Column{Name: monitem.(string), Prop: monitem.(string)})
header = append(header, fmt.Sprintf("%s-性诱", monitem))
header = append(header, fmt.Sprintf("%s-灯诱", monitem))
}
err = xlsx.SetSheetRow(curSheetName, "A1", &header)
if err != nil {
fmt.Println("生成逐日合计表头错误!", err)
return "", ""
}
row = 2
for _, date := range dateList {
data := []interface{}{date}
var sum float64
if v, ok := sexDateDataMap[date]["sum"]; ok {
sum += v
}
if v, ok := lightDateDataMap[date]["sum"]; ok {
sum += v
}
data = append(data, sum)
for _, col := range allColumnList {
monitem := col.Prop
if v, ok := sexDateDataMap[date][monitem]; ok {
data = append(data, v)
} else {
data = append(data, "/")
}
if v, ok := lightDateDataMap[date][monitem]; ok {
data = append(data, v)
} else {
data = append(data, "/")
}
}
err = xlsx.SetSheetRow(curSheetName, fmt.Sprintf("A%d", row), &data)
if err != nil {
fmt.Println("插入逐日合计错误!", err)
return "", ""
}
row += 1
}
filepath := fmt.Sprintf("./file/%s(%s~%s)灯诱及性诱日统计_%d.xlsx", static.Citys[provId], beginDate, endDate, time.Now().Unix())
err = xlsx.SaveAs(filepath)
if err != nil {
fmt.Println("保存excel异常:", err)
return "", ""
}
err = db.Close()
if err != nil {
fmt.Println("数据库连接关闭异常:", err)
return "", ""
}
msg := fmt.Sprintf("%s设备(%s~%s)灯诱性诱日统计结果可见附件,请尽快完成设备排查并完成数据补录。", static.Citys[provId], beginDate, endDate)
return filepath, msg
}
package static
var Citys = map[string]string{
"110000": "北京",
"120000": "天津",
"130000": "河北省",
"140000": "山西省",
"150000": "内蒙古自治区",
"210000": "辽宁省",
"220000": "吉林省",
"230000": "黑龙江省",
"310000": "上海",
"320000": "江苏省",
"330000": "浙江省",
"340000": "安徽省",
"350000": "福建省",
"360000": "江西省",
"370000": "山东省",
"410000": "河南省",
"420000": "湖北省",
"430000": "湖南省",
"440000": "广东省",
"450000": "广西壮族自治区",
"460000": "海南省",
"500000": "重庆",
"510000": "四川省",
"520000": "贵州省",
"530000": "云南省",
"540000": "西藏自治区",
"610000": "陕西省",
"620000": "甘肃省",
"630000": "青海省",
"640000": "宁夏回族自治区",
"650000": "新疆维吾尔自治区",
"710000": "台湾省",
"810000": "香港特别行政区",
"820000": "澳门特别行政区",
"660000": "新疆生产建设兵团",
}
package static
// 数据库连接参数
var (
Server = "218.94.154.74"
User = "xcanary"
Password = "$L7x5hkeN=YC"
Port = 39782
Database = "smopest"
Dbtype = "mssql"
)
package static
var (
WechatWebHook = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=a95a3a1b-4f45-4ffa-a9d7-2db73d5cbea8"
WechatFileUploadUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=a95a3a1b-4f45-4ffa-a9d7-2db73d5cbea8&type=file"
)
package utils
import "time"
// GetBetweenDates 获取两天之间所有日期
func GetBetweenDates(sdate, edate string) []string {
var d []string
timeFormatTpl := "2006-01-02 15:04:05"
if len(timeFormatTpl) != len(sdate) {
timeFormatTpl = timeFormatTpl[0:len(sdate)]
}
date, err := time.Parse(timeFormatTpl, sdate)
if err != nil {
// 时间解析,异常
return d
}
date2, err := time.Parse(timeFormatTpl, edate)
if err != nil {
// 时间解析,异常
return d
}
if date2.Before(date) {
// 如果结束时间小于开始时间,异常
return d
}
// 输出日期格式固定
timeFormatTpl = "20060102"
date2Str := date2.Format(timeFormatTpl)
d = append(d, date.Format(timeFormatTpl))
for {
date = date.AddDate(0, 0, 1)
dateStr := date.Format(timeFormatTpl)
d = append(d, dateStr)
if dateStr == date2Str {
break
}
}
return d
}
package utils
func CheckUpdate(curVersionCode int32) {
}
package utils
import (
"encoding/json"
"fmt"
"github.com/levigross/grequests"
"pestiot/static"
"strings"
)
func WechatSendMessage(message string, mentionedList *[]string, mentionedMobileList *[]string) {
data := map[string]interface{}{
"msgtype": "text",
"text": map[string]interface{}{
"content": message,
"mentioned_list": &mentionedList,
"mentioned_mobile_list": &mentionedMobileList,
},
}
response, err := grequests.Post(static.WechatWebHook, &grequests.RequestOptions{
JSON: data,
})
if err != nil {
fmt.Println("推送消息失败:", err)
return
}
fmt.Println("推送消息成功:", response.String())
}
func WechatFileUpload(filePath string, message string, mentionedList *[]string, mentionedMobileList *[]string) {
fd, err := grequests.FileUploadFromDisk(filePath)
if err != nil {
fmt.Println(fmt.Sprintf("读取文件(%s)异常:", filePath), err)
return
}
nameStrs := strings.Split(fd[0].FileName, "/")
fd[0].FileName = nameStrs[len(nameStrs)-1]
response, err := grequests.Post(static.WechatFileUploadUrl, &grequests.RequestOptions{
Files: fd,
})
if err != nil {
fmt.Println(fmt.Sprintf("上传文件(%s)异常:", filePath), err)
return
}
var retJson map[string]interface{}
err = json.Unmarshal(response.Bytes(), &retJson)
if err != nil {
fmt.Println(fmt.Sprintf("解析上传文件(%s)返回值异常:", filePath), err)
return
}
mediaId := retJson["media_id"]
data := map[string]interface{}{
"msgtype": "file",
"file": map[string]interface{}{
"media_id": mediaId,
"mentioned_list": mentionedList,
"mentioned_mobile_list": mentionedMobileList,
},
}
response, err = grequests.Post(static.WechatWebHook, &grequests.RequestOptions{
JSON: data,
})
if err != nil {
fmt.Println(fmt.Sprintf("推送文件(%s)异常:", filePath), err)
return
}
fmt.Println("推送文件成功:", response.String())
WechatSendMessage(message, mentionedList, nil)
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论