diff --git a/go.mod b/go.mod index d4b3ec2..147f317 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,13 @@ require ( xorm.io/xorm v1.3.1 ) +require ( + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect + github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect + github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect +) + require ( github.com/fsnotify/fsnotify v1.5.4 // indirect github.com/gin-contrib/sse v0.1.0 // indirect @@ -36,6 +43,7 @@ require ( github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/leodido/go-urn v1.2.1 // indirect + github.com/lib/pq v1.10.7 github.com/magiconair/properties v1.8.6 // indirect github.com/mattn/go-isatty v0.0.16 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect @@ -47,6 +55,8 @@ require ( github.com/pelletier/go-toml/v2 v2.0.2 // indirect github.com/richardlehane/mscfb v1.0.4 // indirect github.com/richardlehane/msoleps v1.0.3 // indirect + github.com/simukti/sqldb-logger v0.0.0-20220521163925-faf2f2be0eb6 + github.com/simukti/sqldb-logger/logadapter/zapadapter v0.0.0-20220521163925-faf2f2be0eb6 github.com/spf13/afero v1.8.2 // indirect github.com/spf13/cast v1.5.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect @@ -54,6 +64,8 @@ require ( github.com/subosito/gotenv v1.3.0 // indirect github.com/syndtr/goleveldb v1.0.0 // indirect github.com/ugorji/go/codec v1.2.7 // indirect + github.com/uptrace/bun v1.1.8 + github.com/uptrace/bun/dialect/pgdialect 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 diff --git a/go.sum b/go.sum index d1038c0..ebb6e79 100644 --- a/go.sum +++ b/go.sum @@ -319,6 +319,8 @@ github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dv github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= 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/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= @@ -358,6 +360,8 @@ github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw= +github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= @@ -500,6 +504,11 @@ github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFR 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/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/simukti/sqldb-logger v0.0.0-20201125162808-c35f87e285f2/go.mod h1:/AAqA51IzZd7M3fbS+z7MCM31xVjx+oxBa7mMd3s7Rc= +github.com/simukti/sqldb-logger v0.0.0-20220521163925-faf2f2be0eb6 h1:WLw6hNExwBYnkakVZuCzWyV23Mv0tKhOLPBSIPkXWdg= +github.com/simukti/sqldb-logger v0.0.0-20220521163925-faf2f2be0eb6/go.mod h1:/AAqA51IzZd7M3fbS+z7MCM31xVjx+oxBa7mMd3s7Rc= +github.com/simukti/sqldb-logger/logadapter/zapadapter v0.0.0-20220521163925-faf2f2be0eb6 h1:M29Umbhae+Oxun/fvGBI1TmKhb6yJ5O7CqyFmosdgjk= +github.com/simukti/sqldb-logger/logadapter/zapadapter v0.0.0-20220521163925-faf2f2be0eb6/go.mod h1:0B7LRWOpOjkxfj9gYAmam0YXHPVkDqb/vwgAAwPv5NA= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= @@ -529,6 +538,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf 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.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 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= @@ -540,13 +550,23 @@ github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFd github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= +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/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY= +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/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= +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/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= 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= @@ -576,9 +596,11 @@ go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/atomic v1.9.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.11-0.20210813005559-691160354723/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= +go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4= @@ -591,6 +613,7 @@ go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM= go.uber.org/zap v1.19.1/go.mod h1:j3DNczoxDZroyBnOT1L/Q79cfUMGZxlv/9dzN7SM1rI= +go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw= go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY= go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= @@ -990,6 +1013,7 @@ gopkg.in/yaml.v2 v2.3.0/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-20200601152816-913338de1bd2/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= diff --git a/migration/20220915101901_initialize.tx.up.sql b/migration/20220915101901_initialize.tx.up.sql new file mode 100644 index 0000000..1474c6e --- /dev/null +++ b/migration/20220915101901_initialize.tx.up.sql @@ -0,0 +1,240 @@ +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 bigint 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) +); \ No newline at end of file diff --git a/migration/20220915101901_uninitialize.tx.down.sql b/migration/20220915101901_uninitialize.tx.down.sql new file mode 100644 index 0000000..f117cdb --- /dev/null +++ b/migration/20220915101901_uninitialize.tx.down.sql @@ -0,0 +1,21 @@ +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; \ No newline at end of file diff --git a/migration/20220915131901_add_constraints.up.sql b/migration/20220915131901_add_constraints.up.sql new file mode 100644 index 0000000..d18d440 --- /dev/null +++ b/migration/20220915131901_add_constraints.up.sql @@ -0,0 +1,31 @@ +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); \ No newline at end of file diff --git a/migration/20220915131901_remove_constraints.down.sql b/migration/20220915131901_remove_constraints.down.sql new file mode 100644 index 0000000..bf653a4 --- /dev/null +++ b/migration/20220915131901_remove_constraints.down.sql @@ -0,0 +1,31 @@ +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; \ No newline at end of file diff --git a/migration/main.go b/migration/main.go new file mode 100644 index 0000000..09979af --- /dev/null +++ b/migration/main.go @@ -0,0 +1,21 @@ +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)) + } +} diff --git a/model/shared.go b/model/shared.go index 6347556..5a8a852 100644 --- a/model/shared.go +++ b/model/shared.go @@ -3,30 +3,30 @@ package model import "time" type Created struct { - CreatedAt time.Time `xorm:"timestampz not null created" json:"createdAt" time_format:"simple_datetime" time_location:"shanghai"` + CreatedAt time.Time `bun:"type:timestamptz,notnull,default:current_timestamp" json:"createdAt" time_format:"simple_datetime" time_location:"shanghai"` } type CreatedWithUser struct { - Created `xorm:"extends"` - CreatedBy *string `xorm:"varchar(100)" json:"createdBy"` + Created `bun:",extend"` + CreatedBy *string `json:"createdBy"` } type Deleted struct { - DeletedAt *time.Time `xorm:"timestampz deleted" json:"deletedAt" time_format:"simple_datetime" time_location:"shanghai"` + DeletedAt *time.Time `bun:"type:timestamptz,soft_delete,nullzero" json:"deletedAt" time_format:"simple_datetime" time_location:"shanghai"` } type DeletedWithUser struct { - Deleted `xorm:"extends"` - DeletedBy *string `xorm:"varchar(120)" json:"deletedBy"` + Deleted `bun:",extend"` + DeletedBy *string `json:"deletedBy"` } type CreatedAndModified struct { - Created `xorm:"extends"` - LastModifiedAt *time.Time `xorm:"timestampz updated" json:"lastModifiedAt" time_format:"simple_datetime" time_location:"shanghai"` + Created `bun:",extend"` + LastModifiedAt *time.Time `bun:"type:timestamptz,nullzero,default:current_timestamp" json:"lastModifiedAt" time_format:"simple_datetime" time_location:"shanghai"` } type CreatedAndModifiedWithUser struct { - CreatedAndModified `xorm:"extends"` - CreatedBy *string `xorm:"varchar(100)" json:"createdBy"` - LastModifiedBy *string `xorm:"varchar(100)" json:"lastModifiedBy"` + CreatedAndModified `bun:",extend"` + CreatedBy *string `json:"createdBy"` + LastModifiedBy *string `json:"lastModifiedBy"` }