Commit 073f9150 by mushishixian

excel数据的导入

parent f232605a
......@@ -3,22 +3,33 @@ package customer
import (
"fmt"
"github.com/tealeg/xlsx"
"scm_server/internal/logic"
"scm_server/internal/model"
)
//导入委托方的信息(excel导入)
func Import(){
excelFileName := "./cmd/source/data/customer.xlsx";
xlFile, err := xlsx.OpenFile(excelFileName)
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 {
fmt.Printf("Sheet Name: %s\n", sheet.Name)
//for _, row := range sheet.Rows {
// for _, cell := range row.Cells {
// text := cell.String()
// fmt.Printf("%s\n", text)
// }
//}
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)
}
}
}
}
}
\ 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
import (
"scm_server/cmd/source/customer"
"scm_server/cmd/source/supplier"
)
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
go 1.13
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
)
......@@ -4,12 +4,12 @@ import (
"context"
"time"
"scm_server/internal/model"
"github.com/bilibili/kratos/pkg/cache/memcache"
"github.com/bilibili/kratos/pkg/cache/redis"
"github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/bilibili/kratos/pkg/database/sql"
"github.com/bilibili/kratos/pkg/log"
xtime "github.com/bilibili/kratos/pkg/time"
"github.com/go-kratos/kratos/pkg/cache/memcache"
"github.com/go-kratos/kratos/pkg/cache/redis"
"github.com/go-kratos/kratos/pkg/conf/paladin"
"github.com/go-kratos/kratos/pkg/database/sql"
"github.com/go-kratos/kratos/pkg/log"
xtime "github.com/go-kratos/kratos/pkg/time"
)
// Dao dao interface
......
package dao
import (
"scm_server/configs"
"fmt"
"log"
"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) {
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))
if err != nil {
log.Fatalln(err)
}
return db
once.Do(func() {
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))
if err != nil {
log.Fatalln(err)
}
instance = db
})
return instance
}
func GetCmsDb()(*sqlx.DB) {
dbConfig := configs.GetDBCms()
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 {
log.Fatalln(err)
}
return db
onceCms.Do(func() {
dbConfig := configs.GetDBCms()
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 {
log.Fatalln(err)
}
instanceCms = db
})
return instanceCms
}
func GetLiexinDb()(*sqlx.DB) {
dbConfig := configs.GetDBLiexin()
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 {
log.Fatalln(err)
}
return db
}
onceLiexin.Do(func() {
dbConfig := configs.GetDBLiexin()
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 {
log.Fatalln(err)
}
instanceLiexin = db
})
return instanceLiexin
}
\ No newline at end of file
......@@ -4,7 +4,7 @@ import (
"context"
"scm_server/internal/model"
"github.com/bilibili/kratos/pkg/database/sql"
"github.com/go-kratos/kratos/pkg/database/sql"
"github.com/pkg/errors"
)
......
......@@ -6,16 +6,22 @@ import (
"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',
'add_time','is_synced') VALUES (?,?,?,?,?)`
const InsertCustomerSql = "Insert INTO `lie_customer` (`erp_client_code`,`name`,`erp_id`,`add_time`,`sync_status`) VALUES (?,?,?,?,?)"
//检查用户是否存在
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
type Customer struct {
CustomerId int
ErpClientCode string
Name string
ErpId string
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 (
"context"
"scm_server/internal/dao"
"scm_server/internal/model"
"github.com/bilibili/kratos/pkg/conf/paladin"
"github.com/go-kratos/kratos/pkg/conf/paladin"
"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