ferroalloy.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. "mtp2_if/db"
  6. "mtp2_if/logger"
  7. "mtp2_if/utils"
  8. "strconv"
  9. "sync"
  10. "time"
  11. )
  12. // 上海铁合金项目
  13. // GErmcpspotgoodsprice 现货市价表
  14. type GErmcpspotgoodsprice struct {
  15. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"DELIVERYGOODSID"` // 现货品种ID
  16. WRSTANDARDID int64 `json:"wrstandardid" xorm:"WRSTANDARDID"` // 现货商品ID(通用则为0)
  17. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"SPOTGOODSBRANDID"` // 现货品牌ID(通用则为0, 不为0则须先有商品ID)
  18. CURRENCYID int64 `json:"currencyid" xorm:"CURRENCYID"` // 报价货币ID
  19. SPOTGOODSPRICE float64 `json:"spotgoodsprice" xorm:"SPOTGOODSPRICE"` // 【最新价】当前价格
  20. TRADEDATE string `json:"tradedate" xorm:"TRADEDATE"` // 【日期】交易日
  21. OPERATESRC int32 `json:"-" xorm:"OPERATESRC"` // 最后操作来源 - 1:管理端 2:终端
  22. OPERATEID int64 `json:"-" xorm:"OPERATEID"` // 最后操作人
  23. OPERATETIME time.Time `json:"-" xorm:"OPERATETIME"` // 最后操作时间
  24. ISVALID int32 `json:"-" xorm:"ISVALID"` // 是否有效 - 0:无效 1:有效
  25. YSTSPOTGOODSPRICE float64 `json:"ystspotgoodsprice" xorm:"YSTSPOTGOODSPRICE"` // 昨价
  26. TODAYSPOTGOODSPRICE float64 `json:"todayspotgoodsprice" xorm:"TODAYSPOTGOODSPRICE"` // 今日指定价
  27. TODAYPRICEDTOTALQTY float64 `json:"todaypricedtotalqty" xorm:"TODAYPRICEDTOTALQTY"` // 今日定价总量
  28. TODAYPRICEDTOTALAMOUNT float64 `json:"todaypricedtotalamount" xorm:"TODAYPRICEDTOTALAMOUNT"` // 今日定价金额
  29. SRCMARKETNAME string `json:"srcmarketname" xorm:"SRCMARKETNAME"` // 来源市场名称
  30. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"WRSTANDARDCODE"` // 现货商品代码
  31. WRSTANDARDNAME string `json:"wrstandardname" xorm:"WRSTANDARDNAME"` // 【名称】现货商品名称
  32. PageEx `xorm:"extends"` // 页码信息
  33. }
  34. func (r *GErmcpspotgoodsprice) calc() {
  35. if len(r.TRADEDATE) == 8 {
  36. r.TRADEDATE = fmt.Sprintf("%v-%v-%v", r.TRADEDATE[0:4], r.TRADEDATE[4:6], r.TRADEDATE[6:8])
  37. }
  38. }
  39. func (r *GErmcpspotgoodsprice) buildSql() string {
  40. var sqlId utils.SQLVal = `
  41. SELECT
  42. t.DELIVERYGOODSID ,
  43. t.WRSTANDARDID ,
  44. t.SPOTGOODSBRANDID ,
  45. t.CURRENCYID ,
  46. t.SPOTGOODSPRICE ,
  47. t.TRADEDATE ,
  48. t.YSTSPOTGOODSPRICE ,
  49. t.TODAYSPOTGOODSPRICE ,
  50. t.TODAYPRICEDTOTALQTY ,
  51. t.TODAYPRICEDTOTALAMOUNT,
  52. t.SRCMARKETNAME,
  53. w.WRSTANDARDCODE,
  54. w.WRSTANDARDNAME
  55. FROM ERMCP_SPOTGOODSPRICE t
  56. LEFT JOIN WRStandard w ON t.WRSTANDARDID = w.WRSTANDARDID
  57. WHERE t.SPOTGOODSBRANDID = 0 AND t.CURRENCYID = 1 AND t.ISVALID = 1
  58. `
  59. sqlId.OrderByDesc("w.WRSTANDARDNAME")
  60. sqlId.Page(r.Page, r.PageSize)
  61. return sqlId.String()
  62. }
  63. func (r *GErmcpspotgoodsprice) GetDataByPage() (interface{}, error, int, int, int) {
  64. sData := make([]GErmcpspotgoodsprice, 0)
  65. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  66. total := 0
  67. for i := range sData {
  68. sData[i].calc()
  69. total = sData[i].Total
  70. }
  71. return sData, err, r.Page, r.PageSize, total
  72. }
  73. type THJSigninReq struct {
  74. USERID int64 `form:"userid" binding:"required"` // 用户ID
  75. }
  76. type THJSigninRsp struct {
  77. SigninStatus int `json:"signinstatus"` // 状态操作标志 1-签到成功 2-当日已签到,不能重复签到
  78. }
  79. // Signin 用户签到
  80. func (r *THJSigninReq) Signin() (rsp *THJSigninRsp, err error) {
  81. // 资源锁
  82. var lock sync.Mutex
  83. lock.Lock()
  84. defer lock.Unlock()
  85. engine := db.GetEngine()
  86. // 获取目标用户信息
  87. if u, err := GetUserInfo(int(r.USERID)); err != nil || u == nil {
  88. logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error())
  89. err = errors.New("错误的用户ID")
  90. return nil, err
  91. }
  92. // 判断目标用户当日是否已经签到
  93. p := new(Thjsignin)
  94. if has, err := engine.Where("userid = ?", r.USERID).And("tradedate = to_char(sysdate, 'yyyymmdd')").Get(p); err != nil {
  95. logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error())
  96. err = errors.New("数据错误")
  97. return nil, err
  98. } else {
  99. if has {
  100. return &THJSigninRsp{SigninStatus: 2}, nil
  101. }
  102. }
  103. // 获取签到积配置
  104. c := Thjscoreconfig{
  105. SCORECONFIGTYPE: 2,
  106. }
  107. if has, err := engine.Get(&c); err != nil || !has {
  108. logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error())
  109. err = errors.New("数据错误")
  110. return nil, err
  111. }
  112. // 事务
  113. session := engine.NewSession()
  114. defer session.Close()
  115. // add Begin() before any action
  116. if err := session.Begin(); err != nil {
  117. logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error())
  118. return nil, errors.New("数据错误")
  119. }
  120. now := time.Now()
  121. // 判断用户积分表是否已经存在此用户
  122. curscore := 0
  123. t := Thjuserscore{USERID: r.USERID}
  124. has, err := session.Exist(&t)
  125. if err != nil {
  126. session.Rollback()
  127. logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error())
  128. return nil, errors.New("数据错误")
  129. }
  130. if has {
  131. // 更新积分
  132. curscore = int(t.CURSCORE)
  133. t.CURSCORE += int64(c.PARMA1)
  134. t.UPDATETIME = now
  135. // _, err = session.Update(&t)
  136. sql := fmt.Sprintf(`UPDATE THJ_USERSCORE
  137. SET CURSCORE = %v, UPDATETIME = to_date('%v', 'yyyy-mm-dd hh24:mi:ss')
  138. WHERE USERID = %v`,
  139. t.CURSCORE, t.UPDATETIME.Format("2006-01-02 15:04:05"), t.USERID)
  140. _, err = session.Exec(sql)
  141. if err != nil {
  142. session.Rollback()
  143. logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error())
  144. return nil, errors.New("数据错误")
  145. }
  146. } else {
  147. // 新增用户积分记录
  148. t.CURSCORE = int64(c.PARMA1)
  149. t.UPDATETIME = now
  150. // _, err = session.Insert(&t)
  151. sql := fmt.Sprintf(`INSERT INTO THJ_USERSCORE VALUES (%v, %v, %v, to_date('%v', 'yyyy-mm-dd hh24:mi:ss'))`,
  152. t.USERID, t.CURSCORE, 0, t.UPDATETIME.Format("2006-01-02 15:04:05"))
  153. _, err = session.Exec(sql)
  154. if err != nil {
  155. session.Rollback()
  156. logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error())
  157. return nil, errors.New("数据错误")
  158. }
  159. }
  160. // 记录积分流水
  161. q := Thjuserscorelog{}
  162. seqMap, err := session.QueryString("SELECT SEQ_THJ_USERSCORELOG.nextval SEQID FROM dual")
  163. if err != nil {
  164. session.Rollback()
  165. logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error())
  166. return nil, errors.New("数据错误")
  167. }
  168. if len(seqMap) <= 0 {
  169. session.Rollback()
  170. return nil, errors.New("获取自增ID错误")
  171. }
  172. seqID, _ := strconv.Atoi(seqMap[0]["SEQID"])
  173. q.AUTOID = int64(seqID)
  174. q.USERID = r.USERID
  175. q.SCORECONFIGTYPE = 2 // 2:签到送积分
  176. q.SCORE = float64(c.PARMA1)
  177. q.ORISCORE = float64(curscore)
  178. q.CURSCORE = float64(t.CURSCORE)
  179. q.CREATETIME = now
  180. q.RELATEDORDERID = r.USERID
  181. // _, err = session.Insert(&q)
  182. sql := fmt.Sprintf(`INSERT INTO THJ_USERSCORELOG VALUES (%v, %v, %v, %v, %v, %v, to_date('%v', 'yyyy-mm-dd hh24:mi:ss'), '%v', NULL)`,
  183. q.AUTOID, q.USERID, q.SCORECONFIGTYPE, q.SCORE, q.ORISCORE, q.CURSCORE, q.CREATETIME.Format("2006-01-02 15:04:05"), "")
  184. _, err = session.Exec(sql)
  185. if err != nil {
  186. session.Rollback()
  187. logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error())
  188. return nil, errors.New("数据错误")
  189. }
  190. // 增加签到记录
  191. sql = fmt.Sprintf(`INSERT INTO THJ_SIGNIN VALUES (%v, %v, to_date('%v', 'yyyy-mm-dd hh24:mi:ss'))`,
  192. r.USERID, now.Format("20060102"), now.Format("2006-01-02 15:04:05"))
  193. _, err = session.Exec(sql)
  194. if err != nil {
  195. session.Rollback()
  196. logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error())
  197. return nil, errors.New("数据错误")
  198. }
  199. // add Commit() after all actions
  200. err = session.Commit()
  201. if err != nil {
  202. session.Rollback()
  203. logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error())
  204. return nil, errors.New("数据错误")
  205. }
  206. return &THJSigninRsp{SigninStatus: 1}, nil
  207. }
  208. // 我的推荐列表
  209. type MyRefer struct {
  210. Accountname string `json:"accountname" xorm:"ACCOUNTNAME"` // 用户名(脱敏)
  211. Score float64 `json:"score" xorm:"SCORE"` // 积分
  212. CREATETIME string `json:"createtime" xorm:"CREATETIME"` // 时间(yyyy-mm-dd)
  213. UserID int `json:"-" form:"userid" binding:"required"` // 用户ID
  214. PageEx `xorm:"extends"` // 页码信息
  215. }
  216. func (r *MyRefer) calc() {
  217. r.Accountname = EncryptByStar(r.Accountname)
  218. }
  219. func (r *MyRefer) buildSql() string {
  220. var sqlId utils.SQLVal = `
  221. select
  222. u.ACCOUNTNAME,
  223. t.SCORE,
  224. to_char(t.CREATETIME, 'yyyy-mm-dd') CREATETIME
  225. from THJ_USERSCORELOG t
  226. inner join USERACCOUNT u on u.USERID = t.RELATEDORDERID
  227. where t.USERID = %v
  228. order by t.CREATETIME desc
  229. `
  230. sqlId.FormatParam(r.UserID)
  231. return sqlId.String()
  232. }
  233. func (r *MyRefer) GetDataByPage() (interface{}, error, int, int, int) {
  234. sData := make([]MyRefer, 0)
  235. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  236. total := 0
  237. for i := range sData {
  238. sData[i].calc()
  239. total = sData[i].Total
  240. }
  241. return sData, err, r.Page, r.PageSize, total
  242. }