要用到的包:golang
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"數據庫
func Decode(s []byte) ([]byte, error) {
I := bytes.NewReader(s)
O := transform.NewReader(I, simplifiedchinese.GBK.NewDecoder())
d, e := ioutil.ReadAll(O)
if e != nil {
return nil, e
}
return d, nil
}
func ReadCsv(execlFileName string) {
ProBar.Exist = 0
ProBar.Sucess = 0
ProBar.Fail = 0
ProBar.Count = 0
var (
err error
sucess int64
fail int64
exist int64
errcontent string
OrderTime time.Time
tradedata []TradeData
)
cntb, err := ioutil.ReadFile(execlFileName)
ress, err := Decode(cntb)
if err != nil {
beego.Error("change is err", err.Error())
return
}
buffer := bytes.NewBuffer(ress)
bufferline := bytes.NewBuffer(ress)
var cols int
for {
s, err := bufferline.ReadString(byte('\n'))
if err != nil {
beego.Error("err", err)
break
} else {
linestr := strings.Split(s, ",")
if len(linestr) > 5 && len(linestr[1]) > 5 {
cols++
}
}
}
ProBar.TotalPro = int64((cols - 1) * 3)
var linenum int
for {
s, err := buffer.ReadString(byte('\n'))
if err != nil {
beego.Error("err", err)
break
} else {
if linenum > 0 {
linestr := strings.Split(s, ",")
if len(linestr) > 5 && len(linestr[1]) > 5 {
beego.Debug("linestr lenght:", len(linestr))
var (
trade TradeData
)
for i := 0; i < len(linestr); i++ {
str := linestr[i]
if strings.TrimSpace(str) != "" {
ProBar.CurrentPro++
// for i := 0; i < len(str); i++ {
if strings.TrimSpace(linestr[i]) != "" {
trimsrt := strings.TrimRight(strings.TrimLeft(linestr[i], `="`), `"`)
beego.Debug("str : i", i, trimsrt)
if i == 1 {
customercode, _ := strconv.ParseFloat(trimsrt, 64) //客戶帳號
trade.CustomerCode = int64(customercode) //客戶帳號
}
if i == 2 {
trade.CustomerName = trimsrt //帳戶名稱
}
if i == 3 {
trade.SuccessOrderCode = trimsrt //成交單號
}
if i == 4 {
trade.CommodityCode = trimsrt //成交代碼
}
if i == 5 {
trade.CommodityName = trimsrt //商品名稱
}
if i == 6 {
switch trimsrt {
case "買":
trade.TradeType = 1 //交易類型(0-未知,1-買,2-賣)
break
case "賣":
trade.TradeType = 2 //交易類型(0-未知,1-買,2-賣)
break
default:
trade.TradeType = 0 //交易類型(0-未知,1-買,2-賣)
if trade.TradeType == 0 {
fail++
beego.Debug("Transaction type error : ", linestr[i])
errcontent = errcontent + strconv.Itoa(i) + "行買賣類型錯誤"
break
}
}
}
if i == 7 {
trade.OrderCode = trimsrt //委託單號
}
if i == 8 {
trancount, _ := strconv.ParseFloat(trimsrt, 64) //成交數量
trade.TranCount = int64(trancount)
}
if i == 9 {
trade.TranPrice, _ = strconv.ParseFloat(trimsrt, 64) //成交價格
}
if i == 10 {
trade.TranAmount, _ = strconv.ParseFloat(trimsrt, 64) //成交金額
}
if i == 11 {
tranuser, _ := strconv.ParseFloat(trimsrt, 64) //交易員
trade.TranUser = int64(tranuser) //交易員
}
if i == 12 {
loc, _ := time.LoadLocation("Local") //重要:獲取時區
trade.TradeTime, err = time.ParseInLocation("2006-01-02 15:04:05", trimsrt, loc) //成交時間
if err != nil {
fail++
beego.Debug("TradeTime wrong time : ", trimsrt)
errcontent = errcontent + strconv.Itoa(i) + "行 時間格式錯誤"
break
}
}
if i == 13 {
trade.ProfitLoss, _ = strconv.ParseFloat(trimsrt, 64) //轉讓盈虧
}
if i == 14 {
trade.PoundAge, _ = strconv.ParseFloat(trimsrt, 64) //手續費
}
if i >= 15 && strings.TrimSpace(trimsrt) != "" {
if i == 15 {
trade.OrderPrice, _ = strconv.ParseFloat(trimsrt, 64) //訂立價格
}
if i == 16 {
loc, _ := time.LoadLocation("Local")
OrderTime, err = time.ParseInLocation("2006-01-02 15:04:05", trimsrt, loc) //訂立時間
trade.OrderTime = OrderTime.Unix()
if err != nil {
fail++
beego.Debug("TradeTime wrong time : ", trimsrt)
errcontent = errcontent + strconv.Itoa(i) + "行 時間格式錯誤"
break
}
}
}
}
}
}
beego.Debug("trade", linenum, trade)
checktime := trade.TradeTime.Format("2006-01-02") //登入的時間
IsExist := CheckIsTradeData(checktime)
if !IsExist {
tradedata = append(tradedata, trade)
sucess++
} else {
exist++
beego.Debug("Database duplicate data : ", trade)
errcontent = errcontent + strconv.Itoa(linenum) + "行 數據庫重複"
}
}
}
time.Sleep(time.Millisecond * 0)
}
linenum++
}
if sucess > 0 {
var commitfail int64
sucess, commitfail = AddTradeData(tradedata, sucess)
if commitfail > 0 {
fail = 0
sucess = 0
commitfail = int64(cols - 1)
}
fail += commitfail
os.Remove(execlFileName) //刪除文件
}
ProBar.Sucess = sucess
ProBar.Fail = fail
ProBar.Exist = exist
ProBar.Count = int64(cols - 1)
ProBar.ErrorContent = errcontent
ProBar.Status = false
ProBar.CurrentPro = ProBar.TotalPro
}
func ReadXls(excelFileName string) (res [][]string) {
xlFile, err := xls.Open(excelFileName, "utf-8")
if err == nil {
beego.Debug(excelFileName, " file data read...................")
for i := 0; i < xlFile.NumSheets(); i++ {
sheet := xlFile.GetSheet(i)
temp := make([][]string, sheet.MaxRow+1)
for k, row := range sheet.Rows {
data := make([]string, 0)
for _, col := range row.Cols {
if uint16(len(data)) <= col.LastCol() {
data = append(data, make([]string, col.LastCol()-uint16(len(data))+1)...)
}
str := col.String(xlFile)
for i := uint16(0); i < col.LastCol()-col.FirstCol()+1; i++ {
data[col.FirstCol()+i] = str[i]
}
}
temp[k] = data
}
res = append(res, temp...)
}
} else {
beego.Debug(err)
}
return res
}
func ReadXlsx(excelFileName string) (res [][]string) {
xlFile, err := xlsx.OpenFile(excelFileName)
if err == nil {
beego.Debug(excelFileName, " file data read...................")
for _, sheet := range xlFile.Sheets {
temp := make([][]string, len(sheet.Rows))
for k, row := range sheet.Rows {
var data []string
for _, cell := range row.Cells {
data = append(data, cell.Value)
}
temp[k] = data
}
res = append(res, temp...)
}
} else {
beego.Debug(err)
}
return res
}
app