package models import ( "errors" "fmt" "mtp2_if/db" "mtp2_if/logger" "mtp2_if/utils" "strconv" "sync" "time" ) // 上海铁合金项目 // GErmcpspotgoodsprice 现货市价表 type GErmcpspotgoodsprice struct { DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"DELIVERYGOODSID"` // 现货品种ID WRSTANDARDID int64 `json:"wrstandardid" xorm:"WRSTANDARDID"` // 现货商品ID(通用则为0) SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"SPOTGOODSBRANDID"` // 现货品牌ID(通用则为0, 不为0则须先有商品ID) CURRENCYID int64 `json:"currencyid" xorm:"CURRENCYID"` // 报价货币ID SPOTGOODSPRICE float64 `json:"spotgoodsprice" xorm:"SPOTGOODSPRICE"` // 【最新价】当前价格 TRADEDATE string `json:"tradedate" xorm:"TRADEDATE"` // 【日期】交易日 OPERATESRC int32 `json:"-" xorm:"OPERATESRC"` // 最后操作来源 - 1:管理端 2:终端 OPERATEID int64 `json:"-" xorm:"OPERATEID"` // 最后操作人 OPERATETIME time.Time `json:"-" xorm:"OPERATETIME"` // 最后操作时间 ISVALID int32 `json:"-" xorm:"ISVALID"` // 是否有效 - 0:无效 1:有效 YSTSPOTGOODSPRICE float64 `json:"ystspotgoodsprice" xorm:"YSTSPOTGOODSPRICE"` // 昨价 TODAYSPOTGOODSPRICE float64 `json:"todayspotgoodsprice" xorm:"TODAYSPOTGOODSPRICE"` // 今日指定价 TODAYPRICEDTOTALQTY float64 `json:"todaypricedtotalqty" xorm:"TODAYPRICEDTOTALQTY"` // 今日定价总量 TODAYPRICEDTOTALAMOUNT float64 `json:"todaypricedtotalamount" xorm:"TODAYPRICEDTOTALAMOUNT"` // 今日定价金额 SRCMARKETNAME string `json:"srcmarketname" xorm:"SRCMARKETNAME"` // 来源市场名称 WRSTANDARDCODE string `json:"wrstandardcode" xorm:"WRSTANDARDCODE"` // 现货商品代码 WRSTANDARDNAME string `json:"wrstandardname" xorm:"WRSTANDARDNAME"` // 【名称】现货商品名称 PageEx `xorm:"extends"` // 页码信息 } func (r *GErmcpspotgoodsprice) calc() { if len(r.TRADEDATE) == 8 { r.TRADEDATE = fmt.Sprintf("%v-%v-%v", r.TRADEDATE[0:4], r.TRADEDATE[4:6], r.TRADEDATE[6:8]) } } func (r *GErmcpspotgoodsprice) buildSql() string { var sqlId utils.SQLVal = ` SELECT t.DELIVERYGOODSID , t.WRSTANDARDID , t.SPOTGOODSBRANDID , t.CURRENCYID , t.SPOTGOODSPRICE , t.TRADEDATE , t.YSTSPOTGOODSPRICE , t.TODAYSPOTGOODSPRICE , t.TODAYPRICEDTOTALQTY , t.TODAYPRICEDTOTALAMOUNT, t.SRCMARKETNAME, w.WRSTANDARDCODE, w.WRSTANDARDNAME FROM ERMCP_SPOTGOODSPRICE t LEFT JOIN WRStandard w ON t.WRSTANDARDID = w.WRSTANDARDID WHERE t.SPOTGOODSBRANDID = 0 AND t.CURRENCYID = 1 AND t.ISVALID = 1 ` sqlId.OrderByDesc("w.WRSTANDARDNAME") sqlId.Page(r.Page, r.PageSize) return sqlId.String() } func (r *GErmcpspotgoodsprice) GetDataByPage() (interface{}, error, int, int, int) { sData := make([]GErmcpspotgoodsprice, 0) err := db.GetEngine().SQL(r.buildSql()).Find(&sData) total := 0 for i := range sData { sData[i].calc() total = sData[i].Total } return sData, err, r.Page, r.PageSize, total } type THJSigninReq struct { USERID int64 `form:"userid" binding:"required"` // 用户ID } // Signin 用户签到 func (r *THJSigninReq) Signin() (err error) { // 资源锁 var lock sync.Mutex lock.Lock() defer lock.Unlock() engine := db.GetEngine() // 判断目标用户当日是否已经签到 p := new(Thjsignin) if has, err := engine.Where("userid = ?", r.USERID).And("tradedate = to_char(sysdate, 'yyyymmdd')").Get(&p); err != nil { logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error()) err = errors.New("数据错误") return err } else { if has { err = errors.New("当日已签到") return err } } // 获取签到积配置 c := Thjscoreconfig{ SCORECONFIGTYPE: 2, } if has, err := engine.Get(&c); err != nil || !has { logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error()) err = errors.New("数据错误") return err } // 事务 session := engine.NewSession() defer session.Close() // add Begin() before any action if err := session.Begin(); err != nil { logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error()) return errors.New("数据错误") } // 判断用户积分表是否已经存在此用户 curscore := 0 t := Thjuserscore{USERID: r.USERID} has, err := session.Exist(t) if err != nil { session.Rollback() logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error()) return errors.New("数据错误") } if has { // 更新积分 curscore = int(t.CURSCORE) t.CURSCORE += int64(c.PARMA1) t.UPDATETIME = time.Now() _, err = session.Update(&t) if err != nil { session.Rollback() logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error()) return errors.New("数据错误") } } else { // 新增用户积分记录 t.CURSCORE = int64(c.PARMA1) t.UPDATETIME = time.Now() _, err = session.Insert(&t) if err != nil { session.Rollback() logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error()) return errors.New("数据错误") } } // 记录积分流水 q := Thjuserscorelog{} seqMap, err := session.QueryString("SELECT SEQ_THJ_USERSCORELOG.nextval SEQID FROM dual") if err != nil { session.Rollback() logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error()) return errors.New("数据错误") } if len(seqMap) <= 0 { session.Rollback() return errors.New("获取自增ID错误") } seqID, _ := strconv.Atoi(seqMap[0]["SEQID"]) q.AUTOID = int64(seqID) q.USERID = r.USERID q.SCORECONFIGTYPE = 2 // 2:签到送积分 q.SCORE = float64(c.PARMA1) q.ORISCORE = float64(curscore) q.CURSCORE = float64(t.CURSCORE) q.CREATETIME = time.Now() _, err = session.Insert(&q) if err != nil { session.Rollback() logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error()) return errors.New("数据错误") } // add Commit() after all actions return session.Commit() }