Запись РМИАС в БД

main
parent 5b5770e2bf
commit 022609f08d

Binary file not shown.

Binary file not shown.

File diff suppressed because it is too large Load Diff

@ -10,12 +10,14 @@ import (
)
type Config struct {
FileName string `yaml:"file"`
FileName string `yaml:"inputFile"`
JsonFile string `yaml:"outputFile"`
SheetName string `yaml:"sheet"`
StartRow int `yaml:"start"`
StartRow int `yaml:"startRow"`
Columns map[string]string `yaml:"columns"`
}
var VERSION = "0.0.1"
// Чтение конфига
func readConfig() Config {
@ -60,13 +62,36 @@ func convert2int (s string) int64 {
return n
}
// выводит результат на экран
func json2screen(dbValues []map[string]string) {
//echo dbValues
//b := json.Marshal(dbValues)!
// для красоты:
pretty := json.MarshalIndent(dbValues, "", " ")!
//echo string(b)
echo string(pretty)
}
func json2file(dbValues []map[string]string, outfile string) {
f := os.OpenFile(outfile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)!
defer f.Close()
// Создаём JSON-эндодер, который пишет напрямую в файл
enc := json.NewEncoder(f)
enc.SetIndent("", " ") // красивый отступ (можно убрать)
// Сериализуем структуру в файл
enc.Encode(dbValues)!
echo `Файл ${outfile} сформирован`
}
// ====================== основная программа ====================
cfg := readConfig()
rows := readExcel(cfg.FileName, cfg.SheetName)
echo "Всего ${len(rows)} строк"
echo "Программа конвертации excel -> json. Версия ${VERSION}"
echo "Файл ${cfg.FileName} содержит записей: ${len(rows)}"
var dbValues = []map[string]string{}
for row <- rows[cfg.StartRow-1:2] {
for row <- rows[cfg.StartRow-1:] {
rowValues := make(map[string]string)
for col, val <- row {
colname := getColumnName(cfg.Columns, col+1)
@ -80,74 +105,13 @@ for row <- rows[cfg.StartRow-1:2] {
if colname != "" {
rowValues[colname] = val
}
echo `${col} [${colname}]: ${val}`
//echo `${col} [${colname}]: ${val}`
}
dbValues = append(dbValues, rowValues)
//printf "[%4d]: %v\n", line, row
}
//echo dbValues
b := json.Marshal(dbValues)!
// для красоты:
//pretty := json.MarshalIndent(dbValues, "", " ")!
echo string(b)
//echo string(pretty)
/*
rowsToRemove := []int{}
bkColor := ""
groupColor := "без цвета"
newGroup := false
newContent := ""
firstCell := "" // первая строка группы -> ячейка для замены
cellName := ""
for line, row <- rows if len(row) > 0 {
cellName = excelize.CoordinatesToCellName(8, line+1)! //H1..H13..
styleID := f.GetCellStyle(sheetName, cellName)!
style := f.GetStyle(styleID)!
fillColor := style.Fill.Color // это слайс строк с цветами заливки
if len(fillColor) > 0 {
bkColor = fillColor[0]
} else {
bkColor = "без цвета"
}
if bkColor != groupColor {
groupColor = bkColor
newGroup = true
// меняем значение для предыдущей группы
f.SetCellValue(sheetName, firstCell, newContent)
firstCell = cellName // запоминаем ячейку, которую потом будем менять
newContent = f.GetCellValue(sheetName, cellName)! // .. и ее содержимое
} else {
newGroup = false
newContent += ", " + f.GetCellValue(sheetName, cellName)!
}
if !newGroup {
// строка не первая в группе - удаляем
rowsToRemove = append(rowsToRemove, line)
}
printf "%v %s, %d [%d] = %#v\n", bkColor, cellName, line, len(row), row
}
// меняем значение для последней группы
f.SetCellValue(sheetName, firstCell, newContent)
newContent = f.GetCellValue(sheetName, cellName)! // .. и ее содержимое
//rowsToRemove = append(rowsToRemove, 3)
echo "удаляемые строки:", rowsToRemove
// Удаляем строки в обратном порядке (снизу вверх)
// первую строку не удаляем!
for i := len(rowsToRemove) - 1; i >= 1; i-- {
row := rowsToRemove[i] + 1
f.RemoveRow(sheetName, row)!
//echo "Удалена строка", row
}
f.SaveAs("result.xlsm")
//json2screen(dbValues)
json2file(dbValues, cfg.JsonFile)
*/

@ -1,7 +1,8 @@
file: tsa.xlsx
inputFile: tsa.xlsx
outputFile: tsa.json
sheet: Лист1
start: 2
startRow: 2
columns:
B: caseId # Ид случая
C: ticketNo # Нонер талона
@ -12,7 +13,7 @@ columns:
H: finVolumeCode # Код объема_фин
I: visitCode # Код посещения
J: amount # Сумма
K: diagnosisCode # Код диагноза
K: diagnosCode # Код диагноза
L: department # Отделение
M: profile # Профиль
N: unit # Подразделение

@ -16,190 +16,242 @@ import (
const _ = true
type Config struct {
FileName string `yaml:"file"`
FileName string `yaml:"inputFile"`
JsonFile string `yaml:"outputFile"`
SheetName string `yaml:"sheet"`
StartRow int `yaml:"start"`
StartRow int `yaml:"startRow"`
Columns map[string]string `yaml:"columns"`
}
//line tsa.xgo:20:1
var VERSION = "0.0.1"
//line tsa.xgo:22:1
// Чтение конфига
func readConfig() Config {
//line tsa.xgo:22:1
//line tsa.xgo:24:1
f := func() (_xgo_ret *os.File) {
//line tsa.xgo:22:1
//line tsa.xgo:24:1
var _xgo_err error
//line tsa.xgo:22:1
//line tsa.xgo:24:1
_xgo_ret, _xgo_err = os.Open("tsa.yaml")
//line tsa.xgo:22:1
//line tsa.xgo:24:1
if _xgo_err != nil {
//line tsa.xgo:22:1
_xgo_err = errors.NewFrame(_xgo_err, "os.Open(\"tsa.yaml\")", "tsa.xgo", 22, "main.readConfig")
//line tsa.xgo:22:1
//line tsa.xgo:24:1
_xgo_err = errors.NewFrame(_xgo_err, "os.Open(\"tsa.yaml\")", "tsa.xgo", 24, "main.readConfig")
//line tsa.xgo:24:1
panic(_xgo_err)
}
//line tsa.xgo:22:1
//line tsa.xgo:24:1
return
}()
//line tsa.xgo:23:1
defer f.Close()
//line tsa.xgo:25:1
defer f.Close()
//line tsa.xgo:27:1
cfg := Config{}
//line tsa.xgo:26:1
//line tsa.xgo:28:1
decoder := yaml.NewDecoder(f)
//line tsa.xgo:27:1
//line tsa.xgo:29:1
func() {
//line tsa.xgo:27:1
//line tsa.xgo:29:1
var _xgo_err error
//line tsa.xgo:27:1
//line tsa.xgo:29:1
_xgo_err = decoder.Decode(&cfg)
//line tsa.xgo:27:1
//line tsa.xgo:29:1
if _xgo_err != nil {
//line tsa.xgo:27:1
_xgo_err = errors.NewFrame(_xgo_err, "decoder.Decode(&cfg)", "tsa.xgo", 27, "main.readConfig")
//line tsa.xgo:27:1
//line tsa.xgo:29:1
_xgo_err = errors.NewFrame(_xgo_err, "decoder.Decode(&cfg)", "tsa.xgo", 29, "main.readConfig")
//line tsa.xgo:29:1
panic(_xgo_err)
}
//line tsa.xgo:27:1
//line tsa.xgo:29:1
return
}()
//line tsa.xgo:28:1
//line tsa.xgo:30:1
return cfg
}
//line tsa.xgo:31:1
//line tsa.xgo:33:1
// Получаем все строки листа
func readExcel(filename string, sheetname string) [][]string {
//line tsa.xgo:33:1
//line tsa.xgo:35:1
f := func() (_xgo_ret *excelize.File) {
//line tsa.xgo:33:1
//line tsa.xgo:35:1
var _xgo_err error
//line tsa.xgo:33:1
//line tsa.xgo:35:1
_xgo_ret, _xgo_err = excelize.OpenFile(filename)
//line tsa.xgo:33:1
//line tsa.xgo:35:1
if _xgo_err != nil {
//line tsa.xgo:33:1
_xgo_err = errors.NewFrame(_xgo_err, "excelize.OpenFile(filename)", "tsa.xgo", 33, "main.readExcel")
//line tsa.xgo:33:1
//line tsa.xgo:35:1
_xgo_err = errors.NewFrame(_xgo_err, "excelize.OpenFile(filename)", "tsa.xgo", 35, "main.readExcel")
//line tsa.xgo:35:1
panic(_xgo_err)
}
//line tsa.xgo:33:1
//line tsa.xgo:35:1
return
}()
//line tsa.xgo:34:1
defer f.Close()
//line tsa.xgo:36:1
defer f.Close()
//line tsa.xgo:38:1
rows := func() (_xgo_ret [][]string) {
//line tsa.xgo:36:1
//line tsa.xgo:38:1
var _xgo_err error
//line tsa.xgo:36:1
//line tsa.xgo:38:1
_xgo_ret, _xgo_err = f.GetRows(sheetname)
//line tsa.xgo:36:1
//line tsa.xgo:38:1
if _xgo_err != nil {
//line tsa.xgo:36:1
_xgo_err = errors.NewFrame(_xgo_err, "f.GetRows(sheetname)", "tsa.xgo", 36, "main.readExcel")
//line tsa.xgo:36:1
//line tsa.xgo:38:1
_xgo_err = errors.NewFrame(_xgo_err, "f.GetRows(sheetname)", "tsa.xgo", 38, "main.readExcel")
//line tsa.xgo:38:1
panic(_xgo_err)
}
//line tsa.xgo:36:1
//line tsa.xgo:38:1
return
}()
//line tsa.xgo:37:1
//line tsa.xgo:39:1
return rows
}
//line tsa.xgo:40:1
//line tsa.xgo:42:1
func getColumnName(cols map[string]string, col int) string {
//line tsa.xgo:41:1
//line tsa.xgo:43:1
if col < 1 || col > 21 {
//line tsa.xgo:42:1
//line tsa.xgo:44:1
fmt.Println(stringutil.Concat(`Столбец: `, strconv.Itoa(col), ` вне диапазона столбцов`))
//line tsa.xgo:43:1
//line tsa.xgo:45:1
os.Exit(0)
}
//line tsa.xgo:46:1
//line tsa.xgo:48:1
type ColsMap struct {
num int
str string
}
//line tsa.xgo:50:1
//line tsa.xgo:52:1
colsmap := []string{" ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U"}
//line tsa.xgo:54:1
//line tsa.xgo:56:1
return cols[colsmap[col]]
}
//line tsa.xgo:57:1
//line tsa.xgo:59:1
func convert2int(s string) int64 {
//line tsa.xgo:58:1
//line tsa.xgo:60:1
f := func() (_xgo_ret float64) {
//line tsa.xgo:58:1
//line tsa.xgo:60:1
var _xgo_err error
//line tsa.xgo:58:1
//line tsa.xgo:60:1
_xgo_ret, _xgo_err = strconv.ParseFloat(s, 64)
//line tsa.xgo:58:1
//line tsa.xgo:60:1
if _xgo_err != nil {
//line tsa.xgo:58:1
_xgo_err = errors.NewFrame(_xgo_err, "strconv.ParseFloat(s, 64)", "tsa.xgo", 58, "main.convert2int")
//line tsa.xgo:58:1
//line tsa.xgo:60:1
_xgo_err = errors.NewFrame(_xgo_err, "strconv.ParseFloat(s, 64)", "tsa.xgo", 60, "main.convert2int")
//line tsa.xgo:60:1
panic(_xgo_err)
}
//line tsa.xgo:58:1
//line tsa.xgo:60:1
return
}()
//line tsa.xgo:59:1
//line tsa.xgo:61:1
n := int64(f)
//line tsa.xgo:60:1
//line tsa.xgo:62:1
return n
}
//line tsa.xgo:63
//line tsa.xgo:65:1
// выводит результат на экран
func json2screen(dbValues []map[string]string) {
//line tsa.xgo:70:1
pretty := func() (_xgo_ret []byte) {
//line tsa.xgo:70:1
var _xgo_err error
//line tsa.xgo:70:1
_xgo_ret, _xgo_err = json.MarshalIndent(dbValues, "", " ")
//line tsa.xgo:70:1
if _xgo_err != nil {
//line tsa.xgo:70:1
_xgo_err = errors.NewFrame(_xgo_err, "json.MarshalIndent(dbValues, \"\", \" \")", "tsa.xgo", 70, "main.json2screen")
//line tsa.xgo:70:1
panic(_xgo_err)
}
//line tsa.xgo:70:1
return
}()
//line tsa.xgo:72:1
fmt.Println(string(pretty))
}
//line tsa.xgo:75:1
func json2file(dbValues []map[string]string, outfile string) {
//line tsa.xgo:76:1
f := func() (_xgo_ret *os.File) {
//line tsa.xgo:76:1
var _xgo_err error
//line tsa.xgo:76:1
_xgo_ret, _xgo_err = os.OpenFile(outfile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)
//line tsa.xgo:76:1
if _xgo_err != nil {
//line tsa.xgo:76:1
_xgo_err = errors.NewFrame(_xgo_err, "os.OpenFile(outfile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644)", "tsa.xgo", 76, "main.json2file")
//line tsa.xgo:76:1
panic(_xgo_err)
}
//line tsa.xgo:76:1
return
}()
//line tsa.xgo:77:1
defer f.Close()
//line tsa.xgo:79:1
enc := json.NewEncoder(f)
//line tsa.xgo:80:1
enc.SetIndent("", " ")
//line tsa.xgo:83:1
func() {
//line tsa.xgo:83:1
var _xgo_err error
//line tsa.xgo:83:1
_xgo_err = enc.Encode(dbValues)
//line tsa.xgo:83:1
if _xgo_err != nil {
//line tsa.xgo:83:1
_xgo_err = errors.NewFrame(_xgo_err, "enc.Encode(dbValues)", "tsa.xgo", 83, "main.json2file")
//line tsa.xgo:83:1
panic(_xgo_err)
}
//line tsa.xgo:83:1
return
}()
//line tsa.xgo:84:1
fmt.Println(stringutil.Concat(`Файл `, outfile, ` сформирован`))
}
//line tsa.xgo:87
// ====================== основная программа ====================
func main() {
//line tsa.xgo:64:1
//line tsa.xgo:88:1
cfg := readConfig()
//line tsa.xgo:65:1
//line tsa.xgo:89:1
rows := readExcel(cfg.FileName, cfg.SheetName)
//line tsa.xgo:66:1
fmt.Println(stringutil.Concat("Всего ", strconv.Itoa(len(rows)), " строк"))
//line tsa.xgo:68:1
//line tsa.xgo:90:1
fmt.Println(stringutil.Concat("Программа конвертации excel -> json. Версия ", VERSION))
//line tsa.xgo:91:1
fmt.Println(stringutil.Concat("Файл ", cfg.FileName, " содержит записей: ", strconv.Itoa(len(rows))))
//line tsa.xgo:93:1
var dbValues = []map[string]string{}
for
//line tsa.xgo:69:1
_, row := range rows[cfg.StartRow-1 : 2] {
//line tsa.xgo:70:1
//line tsa.xgo:94:1
_, row := range rows[cfg.StartRow-1:] {
//line tsa.xgo:95:1
rowValues := make(map[string]string)
for
//line tsa.xgo:71:1
//line tsa.xgo:96:1
col, val := range row {
//line tsa.xgo:72:1
//line tsa.xgo:97:1
colname := getColumnName(cfg.Columns, col+1)
//line tsa.xgo:75:1
//line tsa.xgo:100:1
if colname == "caseId" || colname == "policyNumber" {
//line tsa.xgo:76:1
//line tsa.xgo:101:1
val = fmt.Sprintf("%d", convert2int(val))
}
//line tsa.xgo:80:1
//line tsa.xgo:105:1
if colname != "" {
//line tsa.xgo:81:1
//line tsa.xgo:106:1
rowValues[colname] = val
}
//line tsa.xgo:83:1
fmt.Println(stringutil.Concat(strconv.Itoa(col), ` [`, colname, `]: `, val))
}
//line tsa.xgo:86:1
//line tsa.xgo:111:1
dbValues = append(dbValues, rowValues)
}
//line tsa.xgo:90:1
b := func() (_xgo_ret []byte) {
//line tsa.xgo:90:1
var _xgo_err error
//line tsa.xgo:90:1
_xgo_ret, _xgo_err = json.Marshal(dbValues)
//line tsa.xgo:90:1
if _xgo_err != nil {
//line tsa.xgo:90:1
_xgo_err = errors.NewFrame(_xgo_err, "json.Marshal(dbValues)", "tsa.xgo", 90, "main.main")
//line tsa.xgo:90:1
panic(_xgo_err)
}
//line tsa.xgo:90:1
return
}()
//line tsa.xgo:94:1
fmt.Println(string(b))
//line tsa.xgo:116:1
json2file(dbValues, cfg.JsonFile)
}

@ -0,0 +1 @@
pocketbase serve --dir "D:\projects\tsa\database"
Loading…
Cancel
Save