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 } type THJSigninRsp struct { SigninStatus int `json:"signinstatus"` // 状态操作标志 1-签到成功 2-当日已签到,不能重复签到 } // Signin 用户签到 func (r *THJSigninReq) Signin() (rsp *THJSigninRsp, err error) { // 资源锁 var lock sync.Mutex lock.Lock() defer lock.Unlock() engine := db.GetEngine() // 获取目标用户信息 if u, err := GetUserInfo(int(r.USERID)); err != nil || u == nil { logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error()) err = errors.New("错误的用户ID") return nil, err } // 判断目标用户当日是否已经签到 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 nil, err } else { if has { return &THJSigninRsp{SigninStatus: 2}, nil } } // 获取签到积配置 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 nil, 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 nil, errors.New("数据错误") } now := time.Now() // 签到积分配置大于0才记积分和流水 if c.PARMA1 > 0 { // 判断用户积分表是否已经存在此用户 curscore := 0 t := Thjuserscore{USERID: r.USERID} has, err := session.Get(&t) if err != nil { session.Rollback() logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error()) return nil, errors.New("数据错误") } if has { // 更新积分 curscore = int(t.CURSCORE) t.CURSCORE += int64(c.PARMA1) t.UPDATETIME = now // _, err = session.Update(&t) sql := fmt.Sprintf(`UPDATE THJ_USERSCORE SET CURSCORE = %v, UPDATETIME = to_date('%v', 'yyyy-mm-dd hh24:mi:ss') WHERE USERID = %v`, t.CURSCORE, t.UPDATETIME.Format("2006-01-02 15:04:05"), t.USERID) _, err = session.Exec(sql) if err != nil { session.Rollback() logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error()) return nil, errors.New("数据错误") } } else { // 新增用户积分记录 t.CURSCORE = int64(c.PARMA1) t.UPDATETIME = now // _, err = session.Insert(&t) sql := fmt.Sprintf(`INSERT INTO THJ_USERSCORE VALUES (%v, %v, %v, to_date('%v', 'yyyy-mm-dd hh24:mi:ss'))`, t.USERID, t.CURSCORE, 0, t.UPDATETIME.Format("2006-01-02 15:04:05")) _, err = session.Exec(sql) if err != nil { session.Rollback() logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error()) return nil, 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 nil, errors.New("数据错误") } if len(seqMap) <= 0 { session.Rollback() return nil, 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 = now q.RELATEDORDERID = r.USERID // _, err = session.Insert(&q) sql := fmt.Sprintf(`INSERT INTO THJ_USERSCORELOG (AUTOID, USERID, SCORECONFIGTYPE, SCORE, ORISCORE, CURSCORE, CREATETIME) VALUES (%v, %v, %v, %v, %v, %v, to_date('%v', 'yyyy-mm-dd hh24:mi:ss'))`, q.AUTOID, q.USERID, q.SCORECONFIGTYPE, q.SCORE, q.ORISCORE, q.CURSCORE, q.CREATETIME.Format("2006-01-02 15:04:05")) _, err = session.Exec(sql) if err != nil { session.Rollback() logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error()) return nil, errors.New("数据错误") } } // 增加签到记录 sql := fmt.Sprintf(`INSERT INTO THJ_SIGNIN VALUES (%v, %v, to_date('%v', 'yyyy-mm-dd hh24:mi:ss'))`, r.USERID, now.Format("20060102"), now.Format("2006-01-02 15:04:05")) _, err = session.Exec(sql) if err != nil { session.Rollback() logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error()) return nil, errors.New("数据错误") } // add Commit() after all actions err = session.Commit() if err != nil { session.Rollback() logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error()) return nil, errors.New("数据错误") } return &THJSigninRsp{SigninStatus: 1}, nil } // 我的推荐列表 type MyRefer struct { Accountname string `json:"accountname" xorm:"ACCOUNTNAME"` // 用户名(脱敏) Score float64 `json:"score" xorm:"SCORE"` // 积分 CREATETIME string `json:"createtime" xorm:"CREATETIME"` // 时间(yyyy-mm-dd) UserID int `json:"-" form:"userid" binding:"required"` // 用户ID PageEx `xorm:"extends"` // 页码信息 } func (r *MyRefer) calc() { r.Accountname = EncryptByStar(r.Accountname) } func (r *MyRefer) buildSql() string { var sqlId utils.SQLVal = ` select u.ACCOUNTNAME, t.SCORE, to_char(t.CREATETIME, 'yyyy-mm-dd') CREATETIME from THJ_USERSCORELOG t inner join USERACCOUNT u on u.USERID = t.RELATEDORDERID where t.USERID = %v order by t.CREATETIME desc ` sqlId.FormatParam(r.UserID) return sqlId.String() } func (r *MyRefer) GetDataByPage() (interface{}, error, int, int, int) { sData := make([]MyRefer, 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 GThjuserscorelog struct { USERID int64 `json:"-" xorm:"USERID" form:"userid" binding:"required"` // 用户ID SCORECONFIGTYPE int32 `json:"scoreconfigtype" xorm:"SCORECONFIGTYPE"` // 配置类型 - 1:注册红包 2:签到积分 3:推广积分 4:下级用户下单积分 5:自己采购下单积分 6:自己供求下单积分 7:抽奖配置 SCORE float64 `json:"score" xorm:"SCORE"` // 变动积分 ORISCORE float64 `json:"-" xorm:"ORISCORE"` // 期初积分(变动前积) CURSCORE float64 `json:"-" xorm:"CURSCORE"` // 期末积分(变动后积) CREATETIME string `json:"createtime" xorm:"CREATETIME"` // 记账时间 REMARK string `json:"-" xorm:"REMARK"` // 备注 RELATEDORDERID int64 `json:"-" xorm:"RELATEDORDERID"` // 关联单号 PageEx `xorm:"extends"` // 页码信息 } func (r *GThjuserscorelog) calc() { } func (r *GThjuserscorelog) buildSql() string { var sqlId utils.SQLVal = ` select t.SCORECONFIGTYPE, t.SCORE, to_char(t.CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME from THJ_USERSCORELOG t where t.USERID = %v order by t.CREATETIME desc ` sqlId.FormatParam(r.USERID) return sqlId.String() } func (r *GThjuserscorelog) GetDataByPage() (interface{}, error, int, int, int) { sData := make([]GThjuserscorelog, 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 }