diff --git a/archive/archive.go b/archive/archive.go index b62a779..a2f0a42 100644 --- a/archive/archive.go +++ b/archive/archive.go @@ -18,6 +18,7 @@ type CustOrders struct { Custordernr string //客户订单号 Oemordernr string //Oemordernr订单号 Oemseq string //oem订单号 + Calloffnr string //oem订单号 Finr int //工厂编号 } @@ -63,11 +64,11 @@ func CustProduct(conf *config.EnvConfig) { ) //停机时间 stime := "yyyy-MM-dd 02:00:00" - if !common.ValueIsEmpty(conf.Begtime){ + if !common.ValueIsEmpty(conf.Begtime) { stime = conf.Begtime } etime := "yyyy-MM-dd 03:00:00" - if !common.ValueIsEmpty(conf.Endtime){ + if !common.ValueIsEmpty(conf.Endtime) { etime = conf.Endtime } stime = common.TimeFormat(time.Now(), stime) @@ -148,14 +149,20 @@ func CustProduct(conf *config.EnvConfig) { if vv.Status < common.CO_STATUS_CLOSED { isOk = false + glog.InfoExtln("infoShip", "workOrders:", vv.Workordernr) + glog.InfoExtln("infoShip", "Status:", vv.Status) break } if vv.Shippable != 1 { + glog.InfoExtln("infoShip", "workOrders:", vv.Workordernr) + glog.InfoExtln("infoShip", "Shippable:", vv.Shippable) isOk = false break } if vv.Packstatus != 1 { isOk = false + glog.InfoExtln("infoShip", "workOrders:", vv.Workordernr) + glog.InfoExtln("infoShip", "Packstatus:", vv.Packstatus) break } @@ -163,15 +170,21 @@ func CustProduct(conf *config.EnvConfig) { infoShip := db.Jit_shiporder_datalst{} ok, err := e.Table("jit_shiporder_datalst").Join("INNER", "jit_packorder_itemlst", "jit_packorder_itemlst.finr = jit_shiporder_datalst.finr and jit_packorder_itemlst.packorderid = jit_shiporder_datalst.packorderid").Where("jit_packorder_itemlst.finr =? and jit_packorder_itemlst.workordernr = ?", db.G_FINR, vv.Workordernr).Get(&infoShip) if !ok || err != nil { + glog.InfoExtln("infoShip", "workOrders:", vv.Workordernr) + glog.InfoExtln("infoShip", "err:", err) + glog.InfoExtln("infoShip", "ok:", ok) isOk = false break } if infoShip.Status < common.CO_STATUS_CLOSED { + glog.InfoExtln("infoShip", "workOrders:", vv.Workordernr) + glog.InfoExtln("infoShip", "infoShip.Status:", infoShip.Status) isOk = false break } } if !isOk { + glog.InfoExtln("infoShip", "Custordernr:", v.Custordernr) //验证不通过 continue } @@ -996,14 +1009,18 @@ func Archive_Pln_workorder(custorder CustOrders) { //备份:Jit_shiporder func ArchiveJit_shiporder(conf *config.EnvConfig) { for { - + if !conf.Shiptog { + fmt.Println("stop ArchiveJit_shiporder!") + time.Sleep(1 * time.Hour) + continue + } //停机时间 stime := "yyyy-MM-dd 02:00:00" - if !common.ValueIsEmpty(conf.Begtime){ + if !common.ValueIsEmpty(conf.Begtime) { stime = conf.Begtime } etime := "yyyy-MM-dd 03:00:00" - if !common.ValueIsEmpty(conf.Endtime){ + if !common.ValueIsEmpty(conf.Endtime) { etime = conf.Endtime } stime = common.TimeFormat(time.Now(), stime) @@ -1021,35 +1038,33 @@ func ArchiveJit_shiporder(conf *config.EnvConfig) { var ( err error oneWeek time.Time - archive db.Pln_custorder_archive - shiporderids []string ) e := db.G_DbEngine s := db.G_DbSalve - //查询缓存记录表 - cust, err := archive.SelectAll() - if err != nil { - glog.InfoExtln("调度生成终止", "cust is error!") - return - } - for _, v := range cust { - shiporderids = append(shiporderids, v.Custordernr) - } + data := make([]db.Jit_shiporder, 0) //获取七天前的字符串 if common.ValueIsEmpty(conf.Day) { - oneWeek = time.Now().AddDate(0, 0, -14) + oneWeek = time.Now().AddDate(0, 0, -30) } else { - oneWeek = time.Now().AddDate(0, 0, -conf.Day) + oneWeek = time.Now().AddDate(0, 0, -(conf.Day + 30)) } oneWeekStr := common.TimeFormat(oneWeek, "yyyyMMddHHmmss") //查询出订单状态是80,并且是七天前的客户订单的信息,每次查询10条 - err = e.Table("jit_shiporder").NotIn("shiporderid", shiporderids).Where("status = ? and credatuz <= ?", common.CO_STATUS_CLOSED, oneWeekStr).Desc("credatuz").Limit(10).Find(&data) + err = e.Table("jit_shiporder").Where("status = ? and credatuz <= ?", common.CO_STATUS_CLOSED, oneWeekStr).Desc("credatuz").Limit(10).Find(&data) if err != nil { glog.InfoExtln("调度生成终止", "err : ", err) return } for _, v := range data { + + packlst := make([]db.Jit_packorder, 0) + //判断对应的包装单是否全部导出 + err = e.Table("jit_packorder").Join("INNER","jit_shiporder_datalst","jit_packorder.finr = jit_shiporder_datalst.finr and jit_packorder.packorderid = jit_shiporder_datalst.packorderid").Where("jit_shiporder_datalst.finr = ? and jit_shiporder_datalst.shiporderid = ?", db.G_FINR, v.Shiporderid).Find(&packlst) + if len(packlst) > 0{ + glog.InfoExtln("archive数据jit_packorder", "Jit_packorder:","对应的包装单没有全部导出!",v.Shiporderid) + continue + } itemlst := make([]db.Jit_shiporder_itemlst, 0) datalst := make([]db.Jit_shiporder_datalst, 0) var delitemlst db.Jit_shiporder_itemlst @@ -1330,14 +1345,18 @@ func ArchivePln_pickorder(conf *config.EnvConfig) { //备份:jit_packorder func ArchiveJit_packorder(conf *config.EnvConfig) { for { - + if !conf.Packtog { + fmt.Println("stop ArchiveJit_packorder!") + time.Sleep(1 * time.Hour) + continue + } //停机时间 stime := "yyyy-MM-dd 02:00:00" - if !common.ValueIsEmpty(conf.Begtime){ + if !common.ValueIsEmpty(conf.Begtime) { stime = conf.Begtime } etime := "yyyy-MM-dd 03:00:00" - if !common.ValueIsEmpty(conf.Endtime){ + if !common.ValueIsEmpty(conf.Endtime) { etime = conf.Endtime } stime = common.TimeFormat(time.Now(), stime) @@ -1361,9 +1380,9 @@ func ArchiveJit_packorder(conf *config.EnvConfig) { data := make([]db.Jit_packorder, 0) //获取七天前的字符串 if common.ValueIsEmpty(conf.Day) { - oneWeek = time.Now().AddDate(0, 0, -14) + oneWeek = time.Now().AddDate(0, 0, -30) } else { - oneWeek = time.Now().AddDate(0, 0, -conf.Day) + oneWeek = time.Now().AddDate(0, 0, -(conf.Day + 30)) } oneWeekStr := common.TimeFormat(oneWeek, "yyyyMMddHHmmss") @@ -1378,6 +1397,15 @@ func ArchiveJit_packorder(conf *config.EnvConfig) { continue } for _, v := range data { + + wolst := make([]db.Pln_workorder, 0) + //判断对应的工单是否全部导出 + err = e.Table("pln_workorder").Join("INNER","jit_packorder_itemlst","pln_workorder.finr = jit_packorder_itemlst.finr and pln_workorder.workordernr = jit_packorder_itemlst.workordernr").Where("jit_packorder_itemlst.finr = ? and jit_packorder_itemlst.packorderid = ?", db.G_FINR, v.Packorderid).Find(&wolst) + if len(wolst) > 0{ + glog.InfoExtln("archive数据jit_packorder", "pln_workorder:","对应的工单没有全部导出!",v.Packorderid) + continue + } + itemlst := make([]db.Jit_packorder_itemlst, 0) var delitemlst db.Jit_packorder_itemlst var deldata db.Jit_shiporder @@ -1479,163 +1507,165 @@ func ArchiveJit_packorder(conf *config.EnvConfig) { //删除一个礼拜前的打印消息 func DelPrinterInfo(conf *config.EnvConfig) { - tick := time.Tick(10 * time.Second) for { - select { - case <-tick: - stime := "yyyy-MM-dd 02:00:00" - if !common.ValueIsEmpty(conf.Begtime){ - stime = conf.Begtime - } - etime := "yyyy-MM-dd 03:00:00" - if !common.ValueIsEmpty(conf.Endtime){ - etime = conf.Endtime - } - stime = common.TimeFormat(time.Now(), stime) - etime = common.TimeFormat(time.Now(), etime) - //停机时间 - timeLayout := "2006-01-02 15:04:05" - loc, _ := time.LoadLocation("Local") - timeStart, _ := time.ParseInLocation(timeLayout, stime, loc) - timeEnd, _ := time.ParseInLocation(timeLayout, etime, loc) - now := time.Now() - fmt.Println(timeStart) - fmt.Println(timeEnd) - if now.After(timeEnd) || now.Before(timeStart) { - fmt.Println("不在处理时间内!") - time.Sleep(60 * time.Second) - continue - } - var ( - err error - oneWeek time.Time - ) - e := db.G_DbEngine - s := db.G_DbSalve - data := make([]db.Printheadtab, 0) - //获取七天前的字符串 - if common.ValueIsEmpty(conf.Day) { - oneWeek = time.Now().AddDate(0, 0, -14) - } else { - oneWeek = time.Now().AddDate(0, 0, -conf.Day) - } + stime := "yyyy-MM-dd 02:00:00" + if !common.ValueIsEmpty(conf.Begtime) { + stime = conf.Begtime + } + etime := "yyyy-MM-dd 03:00:00" + if !common.ValueIsEmpty(conf.Endtime) { + etime = conf.Endtime + } + stime = common.TimeFormat(time.Now(), stime) + etime = common.TimeFormat(time.Now(), etime) + //停机时间 + timeLayout := "2006-01-02 15:04:05" + loc, _ := time.LoadLocation("Local") + timeStart, _ := time.ParseInLocation(timeLayout, stime, loc) + timeEnd, _ := time.ParseInLocation(timeLayout, etime, loc) + now := time.Now() + fmt.Println(timeStart) + fmt.Println(timeEnd) + if now.After(timeEnd) || now.Before(timeStart) { + fmt.Println("the time is not start!") + time.Sleep(60 * time.Second) + continue + } + fmt.Println("start capy data!") + var ( + err error + oneWeek time.Time + ) + e := db.G_DbEngine + s := db.G_DbSalve + data := make([]db.Printheadtab, 0) + //获取七天前的字符串 + if common.ValueIsEmpty(conf.Day) { + oneWeek = time.Now().AddDate(0, 0, -14) + } else { + oneWeek = time.Now().AddDate(0, 0, -conf.Day) + } + + oneWeekStr := common.TimeFormat(oneWeek, "yyyyMMddHHmmss") + fmt.Println() + fmt.Printf("备份时间:%v", oneWeekStr) + fmt.Println() + //查询出订单状态是80,并且是七天前的客户订单的信息,每次查询10条 - oneWeekStr := common.TimeFormat(oneWeek, "yyyyMMddHHmmss") - //查询出订单状态是80,并且是七天前的客户订单的信息,每次查询10条 + //查询status = 'C' ,并且是七天前的数据 + err = e.Table("printheadtab").Where("finr = ? and status = ? and credatuz < ?", db.G_FINR, "C", oneWeekStr).Limit(50).Find(&data) + if err != nil { + return + } - //查询status = 'C' ,并且是七天前的数据 - err = e.Table("printheadtab").Where("finr = ? and status = ? and credatuz < ?", db.G_FINR, "C", oneWeekStr).Limit(50).Find(&data) + if len(data) == 0 { + time.Sleep(60 * time.Second) + continue + } + for _, v := range data { + //转义为json + jsondata, err := json.Marshal(&v) if err != nil { + info := new(db.Pln_custorder_archive_error) + info.Tablename = "printheadtab" + info.Errorinfo = err.Error() + info.Add() + glog.InfoExtln("archive数据printheadtab", "err:", err.Error()) return } - - if len(data) == 0 { - time.Sleep(60 * time.Second) - continue - } - for _, v := range data { - //转义为json - jsondata, err := json.Marshal(&v) + //判断是否需要备份 + if strings.TrimSpace(v.Printobjtype) == conf.Printobjtype { + //备份数据 + InfoLog := new(db.TableDataInfoLog) + InfoLog.Servername = "Archive_printheadtab" + InfoLog.Tablename = "printheadtab" + InfoLog.Pkname = v.Printheadid + InfoLog.Message = string(jsondata) + InfoLog.Createtime = common.TimeFormat(time.Now(), "yyyyMMddHHmmss") + InfoLog.InsertRecord() + //查询是否存在 + //导出 + _, err = s.Table("printheadtab").Insert(&v) if err != nil { info := new(db.Pln_custorder_archive_error) info.Tablename = "printheadtab" info.Errorinfo = err.Error() info.Add() glog.InfoExtln("archive数据printheadtab", "err:", err.Error()) - return - } - //判断是否需要备份 - if strings.TrimSpace(v.Printobjtype) == conf.Printobjtype { - //备份数据 - InfoLog := new(db.TableDataInfoLog) - InfoLog.Servername = "Archive_printheadtab" - InfoLog.Tablename = "printheadtab" - InfoLog.Pkname = v.Printheadid - InfoLog.Message = string(jsondata) - InfoLog.Createtime = common.TimeFormat(time.Now(), "yyyyMMddHHmmss") - InfoLog.InsertRecord() - //导出 - _, err = s.Table("printheadtab").Insert(&v) - if err != nil { - info := new(db.Pln_custorder_archive_error) - info.Tablename = "printheadtab" - info.Errorinfo = err.Error() - info.Add() - glog.InfoExtln("archive数据printheadtab", "err:", err.Error()) - continue - } - itemlst := make([]db.Printdetailtab, 0) - //查询(printdetailtab) - err = e.Table("printdetailtab").Where("finr = ? and printheadid = ?", db.G_FINR, v.Printheadid).Find(&itemlst) - if err != nil { - info := new(db.Pln_custorder_archive_error) - info.Tablename = "printdetailtab" - info.Errorinfo = err.Error() - info.Add() - glog.InfoExtln("archive数据printdetailtab", "err:", err.Error()) - continue - } - //转义为json - jsonitemlst, err := json.Marshal(&itemlst) - if err != nil { - info := new(db.Pln_custorder_archive_error) - info.Tablename = "printdetailtab" - info.Errorinfo = err.Error() - info.Add() - glog.InfoExtln("archive数据printdetailtab", "err:", err.Error()) - return - } - //备份数据 - InfoLog1 := new(db.TableDataInfoLog) - InfoLog1.Servername = "Archive_printdetailtab" - InfoLog1.Tablename = "printdetailtab" - InfoLog1.Pkname = v.Printheadid - InfoLog1.Message = string(jsonitemlst) - InfoLog1.Createtime = common.TimeFormat(time.Now(), "yyyyMMddHHmmss") - InfoLog1.InsertRecord() - //导出 - _, err = s.Table("printdetailtab").Insert(&itemlst) - if err != nil { - info := new(db.Pln_custorder_archive_error) - info.Tablename = "printdetailtab" - info.Errorinfo = err.Error() - info.Add() - glog.InfoExtln("archive数据printdetailtab", "err:", err.Error()) - continue - } - + continue } - - //删除数据 - _, err = e.Table("printheadtab").Where("finr = ? and printheadid = ?", db.G_FINR, v.Printheadid).Delete(&db.Printheadtab{}) + itemlst := make([]db.Printdetailtab, 0) + //查询(printdetailtab) + err = e.Table("printdetailtab").Where("finr = ? and printheadid = ?", db.G_FINR, v.Printheadid).Find(&itemlst) if err != nil { info := new(db.Pln_custorder_archive_error) - info.Tablename = "printheadtab" + info.Tablename = "printdetailtab" info.Errorinfo = err.Error() info.Add() - glog.InfoExtln("archive数据printheadtab", "err:", err.Error()) + glog.InfoExtln("archive数据printdetailtab", "err:", err.Error()) continue } - - //删除数据 - _, err = e.Table("printdetailtab").Where("finr = ? and printheadid = ?", db.G_FINR, v.Printheadid).Delete(&db.Printdetailtab{}) + //转义为json + jsonitemlst, err := json.Marshal(&itemlst) if err != nil { info := new(db.Pln_custorder_archive_error) info.Tablename = "printdetailtab" info.Errorinfo = err.Error() info.Add() glog.InfoExtln("archive数据printdetailtab", "err:", err.Error()) - continue + return + } + //备份数据 + InfoLog1 := new(db.TableDataInfoLog) + InfoLog1.Servername = "Archive_printdetailtab" + InfoLog1.Tablename = "printdetailtab" + InfoLog1.Pkname = v.Printheadid + InfoLog1.Message = string(jsonitemlst) + InfoLog1.Createtime = common.TimeFormat(time.Now(), "yyyyMMddHHmmss") + InfoLog1.InsertRecord() + //导出 + for _, vv := range itemlst { + _, err = s.Table("printdetailtab").Insert(&vv) + if err != nil { + info := new(db.Pln_custorder_archive_error) + info.Tablename = "printdetailtab" + info.Errorinfo = err.Error() + info.Add() + glog.InfoExtln("archive数据printdetailtab", "err:", err.Error()) + continue + } } } + //删除数据 + _, err = e.Table("printheadtab").Where("finr = ? and printheadid = ?", db.G_FINR, v.Printheadid).Delete(&db.Printheadtab{}) + if err != nil { + info := new(db.Pln_custorder_archive_error) + info.Tablename = "printheadtab" + info.Errorinfo = err.Error() + info.Add() + glog.InfoExtln("archive数据printheadtab", "err:", err.Error()) + continue + } + + //删除数据 + _, err = e.Table("printdetailtab").Where("finr = ? and printheadid = ?", db.G_FINR, v.Printheadid).Delete(&db.Printdetailtab{}) + if err != nil { + info := new(db.Pln_custorder_archive_error) + info.Tablename = "printdetailtab" + info.Errorinfo = err.Error() + info.Add() + glog.InfoExtln("archive数据printdetailtab", "err:", err.Error()) + continue + } + time.Sleep(1 * time.Second) } + time.Sleep(10 * time.Second) } -} - +} //备份(pln_calloffdata_landing) func Archive_Pln_calloffdata_landing(custorder CustOrders) { @@ -1647,7 +1677,7 @@ func Archive_Pln_calloffdata_landing(custorder CustOrders) { s := db.G_DbSalve data := make([]db.Pln_calloffdata_landing, 0) //查询出客户订单状态是80,并且是七天前的客户订单的信息,每次查询10条 - err = e.Table("pln_calloffdata_landing").Where("finr = ? and oemordernr = ?", db.G_FINR, custorder.Oemordernr).Find(&data) + err = e.Table("pln_calloffdata_landing").Where("finr = ? and calloffnr = ?", db.G_FINR, custorder.Calloffnr).Find(&data) if err != nil { info := new(db.Pln_custorder_archive_error) info.Tablename = "pln_calloffdata_landing" @@ -1673,7 +1703,7 @@ func Archive_Pln_calloffdata_landing(custorder CustOrders) { InfoLog := new(db.TableDataInfoLog) InfoLog.Servername = "Archive_pln_calloffdata_landing" InfoLog.Tablename = "pln_calloffdata_landing" - InfoLog.Pkname = common.ValueToString(v.Calloffnr,"") + InfoLog.Pkname = common.ValueToString(v.Calloffnr, "") InfoLog.Message = string(jsondata) InfoLog.Createtime = common.TimeFormat(time.Now(), "yyyyMMddHHmmss") InfoLog.InsertRecord() @@ -1703,4 +1733,4 @@ func Archive_Pln_calloffdata_landing(custorder CustOrders) { } } -} \ No newline at end of file +} diff --git a/cmd/archive/main.go b/cmd/archive/main.go index dcf5c21..38bc73f 100644 --- a/cmd/archive/main.go +++ b/cmd/archive/main.go @@ -115,8 +115,10 @@ func imain() { go archive.ArchiveData(conf) //go archive.ArchivePln_pickorder(conf) go archive.ArchiveJit_shiporder(conf) + + go archive.ArchiveJit_packorder(conf) //备份打印表 - go archive.DelPrinterInfo(conf) + archive.DelPrinterInfo(conf) + - archive.ArchiveJit_packorder(conf) } diff --git a/config/config.go b/config/config.go index d0302b2..68d8fb6 100644 --- a/config/config.go +++ b/config/config.go @@ -33,6 +33,8 @@ type EnvConfig struct { Printobjtype string `yaml:"printobjtype"` Begtime string `yaml:"begtime"` Endtime string `yaml:"endtime"` + Shiptog bool `yaml:"shiptog"` + Packtog bool `yaml:"packtog"` } //read yaml config diff --git a/config/config.yaml b/config/config.yaml index 26a95e6..5e24e69 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -36,5 +36,9 @@ day: 7 # 备份类型 printobjtype: "BBAASN" # 锁定区间 -begtime: "yyyy-MM-dd 02:00:00" -endtime: "yyyy-MM-dd 03:00:00" \ No newline at end of file +begtime: "yyyy-MM-dd 09:00:00" +endtime: "yyyy-MM-dd 10:00:00" +# shipTog 捡料单开关 +shiptog: false +# packTog 包装单开关 +packtog: false diff --git a/service/Ordmsg.go b/service/Ordmsg.go index d17f883..ffbe5b8 100644 --- a/service/Ordmsg.go +++ b/service/Ordmsg.go @@ -48,7 +48,7 @@ func (bl_ordmsg *BL_Ordmsg) Create(session *xorm.Session) (err error) { } // 获取消息号 - if msgid, err = SN_GetNextSnrBySession(strings.TrimSpace(ordsn), session); err != nil { + if msgid, err = SN_SyncGetNextSnr(strings.TrimSpace(ordsn)); err != nil { return } @@ -71,7 +71,6 @@ func (bl_ordmsg *BL_Ordmsg) Create(session *xorm.Session) (err error) { if err = ordmsgtab.Insert(session); err != nil { return } - return } @@ -100,7 +99,7 @@ func (bl_ordmsg *BL_Ordmsg) Add() (err error) { } // 获取消息号 - if msgid, err = SN_GetNextSnr(strings.TrimSpace(ordsn)); err != nil { + if msgid, err = SN_SyncGetNextSnr(strings.TrimSpace(ordsn)); err != nil { return } @@ -155,7 +154,7 @@ func (bl_ordmsg *BL_Ordmsg) CreateForOemorderToParseCarmodels() (err error) { } // 获取消息号 - if msgid, err = SN_GetNextSnr(strings.TrimSpace(ordsn)); err != nil { + if msgid, err = SN_SyncGetNextSnr(strings.TrimSpace(ordsn)); err != nil { glog.InfoExtln("解析并读取SEQ", "为客户订单创建车型解析消息失败: ", err) return } @@ -211,7 +210,7 @@ func (bl_ordmsg *BL_Ordmsg) CreateForOemorderToParseCarmodel(session *xorm.Sessi } // 获取消息号 - if msgid, err = SN_GetNextSnrBySession(strings.TrimSpace(ordsn), session); err != nil { + if msgid, err = SN_SyncGetNextSnr(strings.TrimSpace(ordsn)); err != nil { glog.InfoExtln("解析并读取SEQ", "为客户订单创建车型解析消息失败: ", err) return } diff --git a/service/Snrtab.go b/service/Snrtab.go index 209263d..abf5547 100644 --- a/service/Snrtab.go +++ b/service/Snrtab.go @@ -1,9 +1,11 @@ package service import ( + "fmt" "github.com/go-xorm/xorm" "leit.com/leit_seat_aps/common" "leit.com/leit_seat_aps/db" + "leit.com/leit_seat_aps/etcd" "strconv" "strings" "time" @@ -221,3 +223,119 @@ func SN_GetNextSnrBySession(snr string, session *xorm.Session) (retsnr string, e return } + + +// 获取指定序列的下一个流水号(异步获取) +func SN_SyncGetNextSnr(snr string) (retsnr string, err error) { + /* 获取锁 */ +LOOP: + err = etcd.G_jobLock.TryLock("msgid") + if err != nil { + fmt.Println("msgid groutine lock fail!") + time.Sleep(5 * time.Millisecond) + goto LOOP + } + defer etcd.G_jobLock.UnLock() + var ( + snrtab db.Snrtab + lastupdtime time.Time + y, l_y, d, l_d, wk, l_wk int + m, l_m time.Month + layout []byte + i, nextsnr int + sql string + ) + + snrtab = db.Snrtab{} + if _, err = db.G_DbEngine.Id(core.PK{db.G_FINR, snr}).Get(&snrtab); err != nil { + return + } + snrtab.Clipped() + + if snrtab.Lastnrcreate == "" { + nextsnr = snrtab.Nextnr + } else { + if lastupdtime, err = common.DateParse(snrtab.Lastnrcreate, "YmdHis"); err != nil { + return + } + // 获取下一序列值 + y = time.Now().Year() + m = time.Now().Month() + d = time.Now().Day() + _, wk = time.Now().ISOWeek() + l_y = lastupdtime.Year() + l_m = lastupdtime.Month() + l_d = lastupdtime.Day() + _, l_wk = lastupdtime.ISOWeek() + switch strings.ToUpper(snrtab.Resetnrrule) { + case "DAILY": + if y == l_y && m == l_m && wk == l_wk && d == l_d { + nextsnr = snrtab.Nextnr + } else { + nextsnr = snrtab.Startnr + } + case "WEEKLY": + if y == l_y && m == l_m && wk == l_wk { + nextsnr = snrtab.Nextnr + } else { + nextsnr = snrtab.Startnr + } + case "MONTHLY": + if y == l_y && m == l_m { + nextsnr = snrtab.Nextnr + } else { + nextsnr = snrtab.Startnr + } + case "YEARLY": + if y == l_y { + nextsnr = snrtab.Nextnr + } else { + nextsnr = snrtab.Startnr + } + case "NEVER": + nextsnr = snrtab.Nextnr + default: + nextsnr = snrtab.Nextnr + } + } + + // 组合返回值 + layout = []byte(snrtab.Identifierlayout) + for i = 0; i < len(layout); i++ { + switch string(layout[i]) { + case "1": //前缀 + retsnr = retsnr + snrtab.Prefix + case "2": //日期变量 + retsnr = retsnr + common.Date(time.Now().Unix(), snrtab.Dateformat) + case "3": //中缀 + retsnr = retsnr + snrtab.Infix + case "4": //流水号 + if snrtab.Identifierformat == "CHAR" { + retsnr = retsnr + common.ConvInt2FormatString(nextsnr, snrtab.Length) + } else { + retsnr = retsnr + strconv.Itoa(nextsnr) + } + case "5": //后缀 + retsnr = retsnr + snrtab.Postfix + } + } + //更新下一序列值 + // 处理数值超限 + snrtab.Nextnr = nextsnr + snrtab.Step + snrtab.Lastnrcreate = common.Date(time.Now().Unix(), "YYYYMMDDHHmmss") + snrtab.Lastmodif = common.Date(time.Now().Unix(), "YYYYMMDDHHmmss") + if snrtab.Nextnr > snrtab.Endnr { + switch strings.ToUpper(snrtab.Overflowhandling) { + case "NO": + case "RESET": + snrtab.Nextnr = snrtab.Startnr + } + } + + sql = "update `snrtab` set nextnr = ?, lastnrcreate = ?, lastmodif = ? where finr = ? AND snrid = ?" + if _, err = db.G_DbEngine.Exec(sql, snrtab.Nextnr, snrtab.Lastnrcreate, snrtab.Lastmodif, db.G_FINR, snrtab.Snrid); err != nil { + return + } + + return +} \ No newline at end of file