ferroalloy.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. // 签到积分配置大于0才记积分和流水
  122. if c.PARMA1 > 0 {
  123. // 判断用户积分表是否已经存在此用户
  124. curscore := 0
  125. t := Thjuserscore{USERID: r.USERID}
  126. has, err := session.Get(&t)
  127. if err != nil {
  128. session.Rollback()
  129. logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error())
  130. return nil, errors.New("数据错误")
  131. }
  132. if has {
  133. // 更新积分
  134. curscore = int(t.CURSCORE)
  135. t.CURSCORE += int64(c.PARMA1)
  136. t.UPDATETIME = now
  137. // _, err = session.Update(&t)
  138. sql := fmt.Sprintf(`UPDATE THJ_USERSCORE
  139. SET CURSCORE = %v, UPDATETIME = to_date('%v', 'yyyy-mm-dd hh24:mi:ss')
  140. WHERE USERID = %v`,
  141. t.CURSCORE, t.UPDATETIME.Format("2006-01-02 15:04:05"), t.USERID)
  142. _, err = session.Exec(sql)
  143. if err != nil {
  144. session.Rollback()
  145. logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error())
  146. return nil, errors.New("数据错误")
  147. }
  148. } else {
  149. // 新增用户积分记录
  150. t.CURSCORE = int64(c.PARMA1)
  151. t.UPDATETIME = now
  152. // _, err = session.Insert(&t)
  153. sql := fmt.Sprintf(`INSERT INTO THJ_USERSCORE VALUES (%v, %v, %v, to_date('%v', 'yyyy-mm-dd hh24:mi:ss'))`,
  154. t.USERID, t.CURSCORE, 0, t.UPDATETIME.Format("2006-01-02 15:04:05"))
  155. _, err = session.Exec(sql)
  156. if err != nil {
  157. session.Rollback()
  158. logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error())
  159. return nil, errors.New("数据错误")
  160. }
  161. }
  162. // 记录积分流水
  163. q := Thjuserscorelog{}
  164. seqMap, err := session.QueryString("SELECT SEQ_THJ_USERSCORELOG.nextval SEQID FROM dual")
  165. if err != nil {
  166. session.Rollback()
  167. logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error())
  168. return nil, errors.New("数据错误")
  169. }
  170. if len(seqMap) <= 0 {
  171. session.Rollback()
  172. return nil, errors.New("获取自增ID错误")
  173. }
  174. seqID, _ := strconv.Atoi(seqMap[0]["SEQID"])
  175. q.AUTOID = int64(seqID)
  176. q.USERID = r.USERID
  177. q.SCORECONFIGTYPE = 2 // 2:签到送积分
  178. q.SCORE = float64(c.PARMA1)
  179. q.ORISCORE = float64(curscore)
  180. q.CURSCORE = float64(t.CURSCORE)
  181. q.CREATETIME = now
  182. q.RELATEDORDERID = r.USERID
  183. // _, err = session.Insert(&q)
  184. sql := fmt.Sprintf(`INSERT INTO THJ_USERSCORELOG
  185. (AUTOID, USERID, SCORECONFIGTYPE, SCORE, ORISCORE, CURSCORE, CREATETIME)
  186. VALUES
  187. (%v, %v, %v, %v, %v, %v, to_date('%v', 'yyyy-mm-dd hh24:mi:ss'))`,
  188. q.AUTOID, q.USERID, q.SCORECONFIGTYPE, q.SCORE, q.ORISCORE, q.CURSCORE, q.CREATETIME.Format("2006-01-02 15:04:05"))
  189. _, err = session.Exec(sql)
  190. if err != nil {
  191. session.Rollback()
  192. logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error())
  193. return nil, errors.New("数据错误")
  194. }
  195. }
  196. // 增加签到记录
  197. sql := fmt.Sprintf(`INSERT INTO THJ_SIGNIN VALUES (%v, %v, to_date('%v', 'yyyy-mm-dd hh24:mi:ss'))`,
  198. r.USERID, now.Format("20060102"), now.Format("2006-01-02 15:04:05"))
  199. _, err = session.Exec(sql)
  200. if err != nil {
  201. session.Rollback()
  202. logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error())
  203. return nil, errors.New("数据错误")
  204. }
  205. // add Commit() after all actions
  206. err = session.Commit()
  207. if err != nil {
  208. session.Rollback()
  209. logger.GetLogger().Errorf("Thjsignin failed: %s", err.Error())
  210. return nil, errors.New("数据错误")
  211. }
  212. return &THJSigninRsp{SigninStatus: 1}, nil
  213. }
  214. // 我的推荐列表
  215. type MyRefer struct {
  216. Accountname string `json:"accountname" xorm:"ACCOUNTNAME"` // 用户名(脱敏)
  217. Score float64 `json:"score" xorm:"SCORE"` // 积分
  218. CREATETIME string `json:"createtime" xorm:"CREATETIME"` // 时间(yyyy-mm-dd)
  219. UserID int `json:"-" form:"userid" binding:"required"` // 用户ID
  220. PageEx `xorm:"extends"` // 页码信息
  221. }
  222. func (r *MyRefer) calc() {
  223. r.Accountname = EncryptByStar(r.Accountname)
  224. }
  225. func (r *MyRefer) buildSql() string {
  226. var sqlId utils.SQLVal = `
  227. select
  228. u.ACCOUNTNAME,
  229. t.SCORE,
  230. to_char(t.CREATETIME, 'yyyy-mm-dd') CREATETIME
  231. from THJ_USERSCORELOG t
  232. inner join USERACCOUNT u on u.USERID = t.RELATEDORDERID
  233. where t.USERID = %v
  234. order by t.CREATETIME desc
  235. `
  236. sqlId.FormatParam(r.UserID)
  237. sqlId.Page(r.Page, r.PageSize)
  238. return sqlId.String()
  239. }
  240. func (r *MyRefer) GetDataByPage() (interface{}, error, int, int, int) {
  241. sData := make([]MyRefer, 0)
  242. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  243. total := 0
  244. for i := range sData {
  245. sData[i].calc()
  246. total = sData[i].Total
  247. }
  248. return sData, err, r.Page, r.PageSize, total
  249. }
  250. type GThjuserscorelog struct {
  251. USERID int64 `json:"-" xorm:"USERID" form:"userid" binding:"required"` // 用户ID
  252. SCORECONFIGTYPE int32 `json:"scoreconfigtype" xorm:"SCORECONFIGTYPE"` // 配置类型 - 1:注册红包 2:签到积分 3:推广积分 4:下级用户下单积分 5:自己采购下单积分 6:自己供求下单积分 7:抽奖配置
  253. SCORE float64 `json:"score" xorm:"SCORE"` // 变动积分
  254. ORISCORE float64 `json:"-" xorm:"ORISCORE"` // 期初积分(变动前积)
  255. CURSCORE float64 `json:"-" xorm:"CURSCORE"` // 期末积分(变动后积)
  256. CREATETIME string `json:"createtime" xorm:"CREATETIME"` // 记账时间
  257. REMARK string `json:"-" xorm:"REMARK"` // 备注
  258. RELATEDORDERID int64 `json:"-" xorm:"RELATEDORDERID"` // 关联单号
  259. PageEx `xorm:"extends"` // 页码信息
  260. }
  261. func (r *GThjuserscorelog) calc() {
  262. }
  263. func (r *GThjuserscorelog) buildSql() string {
  264. var sqlId utils.SQLVal = `
  265. select
  266. t.SCORECONFIGTYPE,
  267. t.SCORE,
  268. to_char(t.CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME
  269. from THJ_USERSCORELOG t
  270. where t.USERID = %v
  271. order by t.CREATETIME desc
  272. `
  273. sqlId.FormatParam(r.USERID)
  274. sqlId.Page(r.Page, r.PageSize)
  275. return sqlId.String()
  276. }
  277. func (r *GThjuserscorelog) GetDataByPage() (interface{}, error, int, int, int) {
  278. sData := make([]GThjuserscorelog, 0)
  279. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  280. total := 0
  281. for i := range sData {
  282. sData[i].calc()
  283. total = sData[i].Total
  284. }
  285. return sData, err, r.Page, r.PageSize, total
  286. }
  287. // THJWrstandard 现货商品表
  288. type THJWrstandard struct {
  289. WRSTANDARDID int64 `json:"wrstandardid" xorm:"WRSTANDARDID"` // 现货商品ID(自增 SEQ_GOODS 确保不重复)
  290. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"WRSTANDARDCODE"` // 现货商品代码
  291. WRSTANDARDNAME string `json:"wrstandardname" xorm:"WRSTANDARDNAME" form:"wrstandardname"` // 现货商品名称(模糊查询)
  292. DELIVERYGOODSID int32 `json:"-" xorm:"DELIVERYGOODSID"` // 现货品种ID
  293. UNITID int32 `json:"-" xorm:"UNITID"` // 现货商品单位ID
  294. MINIVALUE int64 `json:"-" xorm:"MINIVALUE"` // 最小变动值
  295. MINIVALUEDP int64 `json:"-" xorm:"MINIVALUEDP"` // 最小变动值小数位
  296. REALMINIVALUE int64 `json:"-" xorm:"REALMINIVALUE"` // 实际最小变动值
  297. REALMINIVALUEDP int64 `json:"-" xorm:"REALMINIVALUEDP"` // 实际最小变动值小数位
  298. WRSSTATUS int32 `json:"-" xorm:"WRSSTATUS"` // 状态 - 作废 - 0:未激活 1:正常
  299. CREATORID int64 `json:"-" xorm:"CREATORID"` // 创建人
  300. CREATETIME time.Time `json:"-" xorm:"CREATETIME"` // 创建时间
  301. UPDATORID int64 `json:"-" xorm:"UPDATORID"` // 更新人
  302. UPDATETIME time.Time `json:"-" xorm:"UPDATETIME"` // 更新时间
  303. FACTORYITEMJSON string `json:"-" xorm:"FACTORYITEMJSON"` // 要素项定义Json[{"DGFactoryItemTypeID": ,"ItemTypeMode": ,"FactoryItemIDs": },{.....},]DGFactoryItemTypeID - 要素项类型ID --DGFactoryItem->DGFactoryItemTypeIDItemTypeMode - 要素项类型模式 --DGFactoryItem->ItemTypeModeFactoryItemIDs - 选择项IDs--DGFactoryItem->DGFactoryItemID, 逗号分隔
  304. ISVALID int32 `json:"-" xorm:"ISVALID"` // 是否有效 - 0:无效 1:有效
  305. AREAUSERID int64 `json:"-" xorm:"AREAUSERID"` // 所属机构
  306. REMARK string `json:"-" xorm:"REMARK"` // 备注
  307. CONVERTFACTOR float64 `json:"-" xorm:"CONVERTFACTOR"` // 标仓系数
  308. VATRATE float64 `json:"-" xorm:"VATRATE"` // 现货增值税率
  309. STORAGEFEE float64 `json:"-" xorm:"STORAGEFEE"` // 仓储费(固定: 111)
  310. THUMURLS string `json:"thumurls" xorm:"THUMURLS"` // 缩略图片(1:1)(逗号分隔)
  311. PICTUREURLS string `json:"-" xorm:"PICTUREURLS"` // 详情图片(逗号分隔)
  312. BANNERPICURL string `json:"-" xorm:"BANNERPICURL"` // Banner图(逗号分隔)
  313. PROVIDERUSERID int64 `json:"-" xorm:"PROVIDERUSERID"` // 供应链提供商
  314. PROVIDERACCOUNTID int64 `json:"-" xorm:"PROVIDERACCOUNTID"` // 供应链提供商资金账户 ID
  315. PageEx `xorm:"extends"` // 页码信息
  316. }
  317. func (r *THJWrstandard) calc() {
  318. }
  319. func (r *THJWrstandard) buildSql() string {
  320. var sqlId utils.SQLVal = `
  321. select
  322. wr.*
  323. from wrstandard wr
  324. where wr.wrstandardid in
  325. (select distinct t.wrstandardid
  326. from WR_PresaleInfo t
  327. where t.presalestatus = 2
  328. and t.marketid = 64201) and %v
  329. order by wr.wrstandardname;
  330. `
  331. param := "1=1"
  332. if r.WRSTANDARDNAME != "" {
  333. param = fmt.Sprintf("wr.wrstandardname like '%%%v%%'", r.WRSTANDARDNAME)
  334. }
  335. sqlId.FormatParam(param)
  336. sqlId.Page(r.Page, r.PageSize)
  337. return sqlId.String()
  338. }
  339. func (r *THJWrstandard) GetDataByPage() (interface{}, error, int, int, int) {
  340. sData := make([]THJWrstandard, 0)
  341. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  342. total := 0
  343. for i := range sData {
  344. sData[i].calc()
  345. total = sData[i].Total
  346. }
  347. return sData, err, r.Page, r.PageSize, total
  348. }
  349. // RegisterMoney 注册红包
  350. type RegisterMoney struct {
  351. Amount float64 `json:"amount" xorm:"AMOUNT"` // 红包
  352. Accountid int64 `json:"-" form:"accountid" binding:"required"` // 资金账户ID
  353. }
  354. func (r *RegisterMoney) calc() {
  355. }
  356. func (r *RegisterMoney) buildSql() string {
  357. var sqlId utils.SQLVal = `
  358. select
  359. t.amount
  360. from taaccountlog t
  361. where t.accountid = %v and t.businesscode = '712'
  362. `
  363. sqlId.FormatParam(r.Accountid)
  364. return sqlId.String()
  365. }
  366. // GetDataEx 从数据库中查询数据
  367. func (r *RegisterMoney) GetDataEx() (interface{}, error) {
  368. e := db.GetEngine()
  369. s := e.SQL(r.buildSql())
  370. sData := make([]RegisterMoney, 0)
  371. if err := s.Find(&sData); err != nil {
  372. return nil, err
  373. }
  374. for i := range sData {
  375. sData[i].calc()
  376. }
  377. return sData, nil
  378. }