Commit 073f9150 by mushishixian

excel数据的导入

parent f232605a
...@@ -3,22 +3,33 @@ package customer ...@@ -3,22 +3,33 @@ package customer
import ( import (
"fmt" "fmt"
"github.com/tealeg/xlsx" "github.com/tealeg/xlsx"
"scm_server/internal/logic"
"scm_server/internal/model"
) )
//导入委托方的信息(excel导入) //导入委托方的信息(excel导入)
func Import(){ func Import() {
excelFileName := "./cmd/source/data/customer.xlsx"; var (
xlFile, err := xlsx.OpenFile(excelFileName) excelFileName string
xlFile *xlsx.File
err error
customer model.Customer
)
excelFileName = "./cmd/source/data/customer.xls"
xlFile, err = xlsx.OpenFile(excelFileName)
if err != nil { if err != nil {
fmt.Printf("open failed: %s\n", err) fmt.Printf("open failed: %s\n", err)
} }
for _, sheet := range xlFile.Sheets { for _, sheet := range xlFile.Sheets {
fmt.Printf("Sheet Name: %s\n", sheet.Name) for key, row := range sheet.Rows {
//for _, row := range sheet.Rows { if key > 0 {
// for _, cell := range row.Cells { customer.ErpId = row.Cells[0].String()
// text := cell.String() customer.ErpClientCode = row.Cells[1].String()
// fmt.Printf("%s\n", text) customer.Name = row.Cells[2].String()
// } if err = logic.InsertCustomer(customer); err != nil {
//} fmt.Println(err)
}
}
}
} }
} }
\ No newline at end of file
No preview for this file type
No preview for this file type
package customer
import (
"fmt"
"github.com/tealeg/xlsx"
"scm_server/internal/logic"
"scm_server/internal/model"
)
//导入委托方的信息(excel导入)
func Import() {
var (
excelFileName string
xlFile *xlsx.File
err error
customer model.Customer
)
excelFileName = "./cmd/source/data/customer.xls"
xlFile, err = xlsx.OpenFile(excelFileName)
if err != nil {
fmt.Printf("open failed: %s\n", err)
}
for _, sheet := range xlFile.Sheets {
for key, row := range sheet.Rows {
if key > 0 {
customer.ErpId = row.Cells[0].String()
customer.ErpClientCode = row.Cells[1].String()
customer.Name = row.Cells[2].String()
if err = logic.InsertCustomer(customer); err != nil {
fmt.Println(err)
}
}
}
}
}
package main package main
import ( import (
"scm_server/cmd/source/customer" "scm_server/cmd/source/supplier"
) )
func main() { func main() {
customer.Import() supplier.Import()
} }
package supplier
import (
"fmt"
"github.com/tealeg/xlsx"
"scm_server/internal/logic"
"scm_server/internal/model"
)
//导入委托方的信息(excel导入)
func Import() {
var (
excelFileName string
xlFile *xlsx.File
err error
supplier model.Supplier
)
excelFileName = "./cmd/source/data/supplier.xlsx"
xlFile, err = xlsx.OpenFile(excelFileName)
if err != nil {
fmt.Printf("open failed: %s\n", err)
}
for _, sheet := range xlFile.Sheets {
for key, row := range sheet.Rows {
if key > 0 {
supplier.ErpId = row.Cells[0].String()
supplier.ErpSupplierCode = row.Cells[1].String()
supplier.Name = row.Cells[2].String()
//先去查询是否存在,不存在才去插入
if !logic.CheckSupplierExist(supplier.ErpId) {
if err = logic.InsertSupplier(supplier); err != nil {
fmt.Println(err)
}
}
}
}
}
}
...@@ -3,6 +3,8 @@ module scm_server ...@@ -3,6 +3,8 @@ module scm_server
go 1.13 go 1.13
require ( require (
github.com/jmoiron/sqlx v1.2.0 // indirect github.com/go-kratos/kratos v0.4.2
github.com/jmoiron/sqlx v1.2.0
github.com/pkg/errors v0.9.1
github.com/tealeg/xlsx v1.0.5 github.com/tealeg/xlsx v1.0.5
) )
...@@ -4,12 +4,12 @@ import ( ...@@ -4,12 +4,12 @@ import (
"context" "context"
"time" "time"
"scm_server/internal/model" "scm_server/internal/model"
"github.com/bilibili/kratos/pkg/cache/memcache" "github.com/go-kratos/kratos/pkg/cache/memcache"
"github.com/bilibili/kratos/pkg/cache/redis" "github.com/go-kratos/kratos/pkg/cache/redis"
"github.com/bilibili/kratos/pkg/conf/paladin" "github.com/go-kratos/kratos/pkg/conf/paladin"
"github.com/bilibili/kratos/pkg/database/sql" "github.com/go-kratos/kratos/pkg/database/sql"
"github.com/bilibili/kratos/pkg/log" "github.com/go-kratos/kratos/pkg/log"
xtime "github.com/bilibili/kratos/pkg/time" xtime "github.com/go-kratos/kratos/pkg/time"
) )
// Dao dao interface // Dao dao interface
......
package dao package dao
import ( import (
"scm_server/configs"
"fmt" "fmt"
"log"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"log"
"scm_server/configs"
"sync"
) )
var instance * sqlx.DB
var instanceCms * sqlx.DB
var instanceLiexin * sqlx.DB
var once sync.Once
var onceCms sync.Once
var onceLiexin sync.Once
func GetDb()(*sqlx.DB) { func GetDb()(*sqlx.DB) {
dbConfig := configs.GetDBOne()
db, err := sqlx.Open(dbConfig.Engine, fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", dbConfig.User, dbConfig.Pass, dbConfig.Ip, dbConfig.Port, dbConfig.Table)) once.Do(func() {
if err != nil { dbConfig := configs.GetDBOne()
log.Fatalln(err) db, err := sqlx.Open(dbConfig.Engine, fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", dbConfig.User, dbConfig.Pass, dbConfig.Ip, dbConfig.Port, dbConfig.Table))
} if err != nil {
return db log.Fatalln(err)
}
instance = db
})
return instance
} }
func GetCmsDb()(*sqlx.DB) { func GetCmsDb()(*sqlx.DB) {
dbConfig := configs.GetDBCms() onceCms.Do(func() {
db, err := sqlx.Open(dbConfig.Engine, fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", dbConfig.User, dbConfig.Pass, dbConfig.Ip, dbConfig.Port, dbConfig.Table)) dbConfig := configs.GetDBCms()
if err != nil { db, err := sqlx.Open(dbConfig.Engine, fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", dbConfig.User, dbConfig.Pass, dbConfig.Ip, dbConfig.Port, dbConfig.Table))
log.Fatalln(err) if err != nil {
} log.Fatalln(err)
return db }
instanceCms = db
})
return instanceCms
} }
func GetLiexinDb()(*sqlx.DB) { func GetLiexinDb()(*sqlx.DB) {
dbConfig := configs.GetDBLiexin() onceLiexin.Do(func() {
db, err := sqlx.Open(dbConfig.Engine, fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", dbConfig.User, dbConfig.Pass, dbConfig.Ip, dbConfig.Port, dbConfig.Table)) dbConfig := configs.GetDBLiexin()
if err != nil { db, err := sqlx.Open(dbConfig.Engine, fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", dbConfig.User, dbConfig.Pass, dbConfig.Ip, dbConfig.Port, dbConfig.Table))
log.Fatalln(err) if err != nil {
} log.Fatalln(err)
return db }
} instanceLiexin = db
})
return instanceLiexin
}
\ No newline at end of file
...@@ -4,7 +4,7 @@ import ( ...@@ -4,7 +4,7 @@ import (
"context" "context"
"scm_server/internal/model" "scm_server/internal/model"
"github.com/bilibili/kratos/pkg/database/sql" "github.com/go-kratos/kratos/pkg/database/sql"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
......
...@@ -6,16 +6,22 @@ import ( ...@@ -6,16 +6,22 @@ import (
"time" "time"
) )
const CheckCustomerExistSql = `SELECT FROM lie_customer WHERE erp_id = ?` const CheckCustomerExistSql = "SELECT customer_id FROM lie_customer WHERE erp_id = ?"
const InsertCustomerSql = `Insert into lie_customer ('erp_client_code','name','erp_id', const InsertCustomerSql = "Insert INTO `lie_customer` (`erp_client_code`,`name`,`erp_id`,`add_time`,`sync_status`) VALUES (?,?,?,?,?)"
'add_time','is_synced') VALUES (?,?,?,?,?)`
//检查用户是否存在 //检查用户是否存在
func CheckCustomerExist(erpId string) (exist bool) { func CheckCustomerExist(erpId string) (exist bool) {
return true var customerId int
dao.GetDb().QueryRowx(CheckCustomerExistSql, erpId).Scan(&customerId)
return customerId > 0
} }
func InsertCustomer(customer model.Customer) { //插入用户
dao.GetDb().Exec(InsertCustomerSql, customer.ErpClientCode, customer.Name, customer.ErpId, time.Now().Unix(), 0) func InsertCustomer(customer model.Customer) (err error) {
_, err = dao.GetDb().Exec(InsertCustomerSql, customer.ErpClientCode, customer.Name, customer.ErpId, time.Now().Unix(), 0)
if err != nil {
return
}
return nil
} }
package logic
import (
"scm_server/internal/dao"
"scm_server/internal/model"
"time"
)
const CheckSupplierExistSql = "SELECT supplier_id FROM lie_supplier WHERE erp_id = ?"
const InsertSupplierSql = "Insert INTO `lie_supplier` (`erp_supplier_code`,`name`,`erp_id`,`add_time`,`sync_status`) VALUES (?,?,?,?,?)"
//检查供应商是否存在
func CheckSupplierExist(erpId string) (exist bool) {
var supplierId int = 0
dao.GetDb().QueryRowx(CheckSupplierExistSql, erpId).Scan(&supplierId)
return supplierId > 0
}
//插入供应商
func InsertSupplier(supplier model.Supplier) (err error) {
_, err = dao.GetDb().Exec(InsertSupplierSql, supplier.ErpSupplierCode, supplier.Name, supplier.ErpId, time.Now().Unix(), 0)
if err != nil {
return
}
return nil
}
package model package model
type Customer struct { type Customer struct {
CustomerId int
ErpClientCode string ErpClientCode string
Name string Name string
ErpId string ErpId string
AddTime int AddTime int
IsSynced int SyncTime int
SyncStatus int
SyncError string
} }
package model
type Goods struct {
GoodsName string
GoodsCnName string
GoodsSn string
BrandName string
ErpId string
AddTime int
SyncTime int
SyncStatus int
SyncError string
}
package model
type Supplier struct {
ErpSupplierCode string
Name string
ErpId string
AddTime int
SyncTime int
SyncStatus int
SyncError string
}
...@@ -4,7 +4,7 @@ import ( ...@@ -4,7 +4,7 @@ import (
"context" "context"
"scm_server/internal/dao" "scm_server/internal/dao"
"scm_server/internal/model" "scm_server/internal/model"
"github.com/bilibili/kratos/pkg/conf/paladin" "github.com/go-kratos/kratos/pkg/conf/paladin"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment