185 lines
5.6 KiB
Go
185 lines
5.6 KiB
Go
package models
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/codinl/go-logger"
|
|
"github.com/jinzhu/gorm"
|
|
"github.com/xuri/excelize/v2"
|
|
|
|
//"github.com/andreburgaud/crypt2go/ecb"
|
|
//"github.com/andreburgaud/crypt2go/padding"
|
|
_ "github.com/jinzhu/gorm/dialects/mysql"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
var DBDev *gorm.DB
|
|
|
|
// 测试服务器
|
|
func InitTestDB() {
|
|
var err error
|
|
|
|
dialect := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
|
"mh_dev", "m5h4parZsXjbeiFh", "39.108.188.218", "3306", "mh_dev")
|
|
|
|
db, err := gorm.Open("mysql", dialect)
|
|
if err != nil {
|
|
logger.Infof("Open DB error [%s] with dialect[%s]", err.Error(), dialect)
|
|
panic(err)
|
|
}
|
|
|
|
if db.DB().Ping() != nil {
|
|
fmt.Printf("Ping DB fail: %#v", db.Error)
|
|
panic(err)
|
|
}
|
|
db.SingularTable(true)
|
|
|
|
db.AutoMigrate(
|
|
&ErpSupplier{},
|
|
&ErpCommodity{},
|
|
&ErpCashier{},
|
|
|
|
&ErpOrder{},
|
|
&ErpOrderCommodity{},
|
|
&Logging{},
|
|
|
|
&ErpStock{},
|
|
&ErpStockCommodity{},
|
|
&ErpInventoryStock{},
|
|
&ErpInventoryStockCommodity{},
|
|
)
|
|
|
|
fmt.Println("DB init success")
|
|
DBDev = db
|
|
logger.Info("ok")
|
|
}
|
|
|
|
func TestInitTestDBModel(t *testing.T) {
|
|
InitTestDB()
|
|
}
|
|
|
|
var DBProd *gorm.DB
|
|
|
|
func InitDBProd() {
|
|
var err error
|
|
dialect := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
|
"mh_pro", "fLeytnBFCRB553ab", "39.108.188.218", "3306",
|
|
"mh_pro")
|
|
|
|
DBProd, err = gorm.Open("mysql", dialect)
|
|
if err != nil {
|
|
fmt.Println("Connect Database Error, error = ", err)
|
|
panic(err)
|
|
}
|
|
DBProd.DB().SetMaxOpenConns(1000)
|
|
DBProd.DB().SetMaxIdleConns(500)
|
|
DBProd.DB().SetConnMaxLifetime(time.Second * 10)
|
|
//DBProd.LogMode(true)
|
|
DBProd.LogMode(false)
|
|
DBProd.SingularTable(true)
|
|
DBProd.AutoMigrate()
|
|
|
|
if err := DBProd.DB().Ping(); err != nil {
|
|
fmt.Println("Ping Database Fail, error = ", err)
|
|
panic(err)
|
|
}
|
|
fmt.Println("DB init success")
|
|
}
|
|
|
|
func TestInitProdDBModel(t *testing.T) {
|
|
InitDBProd()
|
|
}
|
|
|
|
type ErpStockFile struct {
|
|
Model
|
|
|
|
StoreId uint32 `json:"store_id" gorm:"index"`
|
|
StoreName string `json:"store_name"`
|
|
ErpCommodityId uint32 `json:"erp_commodity_id" gorm:"index"`
|
|
ErpCommodityName string `json:"erp_commodity_name"`
|
|
ErpCategoryId uint32 `json:"erp_category_id" gorm:"index"`
|
|
ErpCategoryName string `json:"erp_category_name"`
|
|
SerialNumber string `json:"serial_number" gorm:"index"`
|
|
IMEIType uint32 `json:"imei_type"` // 1-无串码 2-串码
|
|
RetailPrice uint32 `json:"retail_price"`
|
|
MinRetailPrice uint32 `json:"min_retail_price"`
|
|
Count uint32 `json:"count"`
|
|
DispatchCount uint32 `json:"dispatch_count"`
|
|
ErpSupplierId uint32 `json:"erp_supplier_id" gorm:"index"`
|
|
ErpSupplierName string `json:"erp_supplier_name"`
|
|
IMEI string `json:"imei"`
|
|
StockTime time.Time `json:"stock_time"`
|
|
StaffCostPrice uint32 `json:"staff_cost_price"`
|
|
WholesalePrice uint32 `json:"wholesale_price"`
|
|
OriginalSn string `json:"original_sn" gorm:"index"`
|
|
}
|
|
|
|
func TestCreateStock(t *testing.T) {
|
|
file := excelize.NewFile()
|
|
streamWriter, err := file.NewStreamWriter("Sheet1")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
//url := "http://39.108.188.218:8000/img/export/"
|
|
//f, err := os.OpenFile(fmt.Sprintf("/www/server/images/export/")+fileName, os.O_CREATE|os.O_TRUNC|os.O_APPEND|os.O_RDWR, 0644)
|
|
imint := uint64(10014193353)
|
|
records := make([]*ErpStockFile, 0, 10000)
|
|
for i := 0; i < 1000; i++ {
|
|
stock := &ErpStockFile{
|
|
StoreId: 0,
|
|
StoreName: "深圳万象天地任天堂授权体验店",
|
|
ErpCommodityId: 0,
|
|
//ErpCommodityName: fmt.Sprintf("国行Switch OLED红蓝主机%5d", i+1),
|
|
ErpCommodityName: "国行Switch OLED 白色主机",
|
|
ErpCategoryId: 0,
|
|
ErpCategoryName: "001-003OLED游戏机",
|
|
SerialNumber: "",
|
|
IMEIType: 2,
|
|
IMEI: fmt.Sprintf("XKC%d", imint),
|
|
ErpSupplierId: 0,
|
|
ErpSupplierName: "西盟电玩",
|
|
StockTime: time.Now(),
|
|
RetailPrice: 2599,
|
|
MinRetailPrice: 2499,
|
|
StaffCostPrice: 2249,
|
|
WholesalePrice: 2199,
|
|
Count: 1,
|
|
}
|
|
records = append(records, stock)
|
|
imint++
|
|
}
|
|
|
|
fileName := "库存批量入库" + ".xlsx"
|
|
|
|
//title := []interface{}{"时间", "新增会员数", "会员过期数", "净增会员数", "累计会员数"}
|
|
//title := []interface{}{"用户ID", "注册时间", "开通类型", "开通时间", "续费类型", "续费时间", "当前类型", "会员到期时间"}
|
|
title := []interface{}{"门店", "商品名称", "商品分类", "商品编号", "是否串码", "商品串码", "供应商", "入库时间", "零售价", "最低零售价", "员工成本价", "采购价", "数量"}
|
|
cell, _ := excelize.CoordinatesToCellName(1, 1)
|
|
if err = streamWriter.SetRow(cell, title); err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
var row []interface{}
|
|
for rowId := 0; rowId < len(records); rowId++ {
|
|
row = []interface{}{records[rowId].StoreName, records[rowId].ErpCommodityName, records[rowId].ErpCategoryName, records[rowId].SerialNumber, "是",
|
|
records[rowId].IMEI, records[rowId].ErpSupplierName,
|
|
records[rowId].StockTime.Format("2006/01/02"), records[rowId].RetailPrice, records[rowId].MinRetailPrice,
|
|
records[rowId].StaffCostPrice, records[rowId].WholesalePrice, records[rowId].Count}
|
|
cell, _ := excelize.CoordinatesToCellName(1, rowId+2)
|
|
if err := streamWriter.SetRow(cell, row); err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}
|
|
if err := streamWriter.Flush(); err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
//if err := file.SaveAs("/www/server/images/export/Book1.xlsx"); err != nil {
|
|
//if err := file.SaveAs("/www/server/images/export/" + fileName); err != nil {
|
|
if err := file.SaveAs("./" + fileName); err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
//return url + fileName, nil
|
|
|
|
}
|