hsby.go 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. package models
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "math"
  6. "mtp2_if/db"
  7. "mtp2_if/utils"
  8. "strconv"
  9. "time"
  10. )
  11. // Hsbygoodsex 商品扩展表
  12. type Hsbygoodsex struct {
  13. Goodsid int32 `json:"goodsid" xorm:"'GOODSID'" binding:"required"` // 商 品ID
  14. Hotindex int32 `json:"hotindex" xorm:"'HOTINDEX'"` // 景点热度
  15. Descprovinceid int64 `json:"descprovinceid" xorm:"'DESCPROVINCEID'"` // 目的地(省)
  16. Desccityid int64 `json:"desccityid" xorm:"'DESCCITYID'"` // 目的地(市)
  17. Vendorid int32 `json:"vendorid" xorm:"'VENDORID'"` // 供应商ID
  18. Goodsdesc string `json:"goodsdesc" xorm:"'GOODSDESC'"` // 商品详情
  19. Createtime time.Time `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  20. Creatorid int64 `json:"creatorid" xorm:"'CREATORID'"` // 创建人
  21. Modifierid int64 `json:"modifierid" xorm:"'MODIFIERID'"` // 修改人
  22. Modifytime time.Time `json:"modifytime" xorm:"'MODIFYTIME'"` // 修改时间
  23. Memberratio float64 `json:"memberratio" xorm:"'MEMBERRATIO'"` // 会员货款比例 [71]
  24. Videourls string `json:"videourls" xorm:"'VIDEOURLS'"` // 介绍视频[多张用逗号分隔]
  25. Picurls string `json:"picurls" xorm:"'PICURLS'"` // 介绍图片[多张用逗号分隔]
  26. }
  27. // TableName is HSBY_GOODSEX
  28. func (Hsbygoodsex) TableName() string {
  29. return "HSBY_GOODSEX"
  30. }
  31. // Hsbysupplierinfo 供应商表
  32. type Hsbysupplierinfo struct {
  33. Vendorid int32 `json:"vendorid" xorm:"'VENDORID'" binding:"required"` // 供应商ID(SEQ_HSBY_SUPPLIERINFO)
  34. Vendorname string `json:"vendorname" xorm:"'VENDORNAME'"` // 名称描述客服电话名称
  35. Vendordesc string `json:"vendordesc" xorm:"'VENDORDESC'"` // 描述
  36. Vendorphone string `json:"vendorphone" xorm:"'VENDORPHONE'"` // 客服电话
  37. Vendorattr string `json:"vendorattr" xorm:"'VENDORATTR'"` // 附件(多张,逗号分隔)
  38. Createtime time.Time `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  39. Creatorid int64 `json:"creatorid" xorm:"'CREATORID'"` // 创建人
  40. Modifierid int64 `json:"modifierid" xorm:"'MODIFIERID'"` // 修改人
  41. Modifytime time.Time `json:"modifytime" xorm:"'MODIFYTIME'"` // 修改时间
  42. }
  43. // TableName is HSBY_SUPPLIERINFO
  44. func (Hsbysupplierinfo) TableName() string {
  45. return "HSBY_SUPPLIERINFO"
  46. }
  47. // HsbyTopGoods 热卖商品(二级市场挂牌点选)
  48. type HsbyTopGoods struct {
  49. Goodsid int64 `json:"goodsid" xorm:"'GOODSID'" binding:"required"` // 商品ID(自增ID SEQ_GOODS)
  50. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'" binding:"required"` // 商品代码(内部)
  51. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'" binding:"required"` // 商品名称
  52. Decimalplace int64 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 报价小数位
  53. Marketid int64 `json:"marketid" xorm:"'MARKETID'" binding:"required"` // 所属市场ID
  54. Quoteminunit int64 `json:"quoteminunit" xorm:"'QUOTEMINUNIT'"` // 行情最小变动单位 [整数,报价小数位一起使用]
  55. Agreeunit float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约单位
  56. Hotindex int32 `json:"hotindex" xorm:"'HOTINDEX'"` // 景点热度
  57. Videourls string `json:"videourls" xorm:"'VIDEOURLS'"` // 介绍视频[多张用逗号分隔]
  58. Picurls string `json:"picurls" xorm:"'PICURLS'"` // 介绍图片[多张用逗号分隔]
  59. Currency string `json:"currency" xorm:"'CURRENCY'"` // 货币
  60. Currencysign string `json:"currencysign" xorm:"'CURRENCYSIGN'"` // 货币符号
  61. Trademode int32 `json:"trademode" xorm:"'TRADEMODE'" binding:"required"` // 交易模式 - 10:做市 13:竞价 15:通道交易 16:挂牌点选 17:仓单贸易 18:期权 19:竞拍-降价式 20:竞拍-竞价式 21:竞拍-大宗式 22:受托竞价
  62. Last float64 `json:"last" xorm:"-"` // 现价
  63. LotSize float64 `json:"lotsize" xorm:"-"` // 最小变动单位
  64. }
  65. // GetHsbyTopGoodses 获取热门商品列表
  66. // 参数 marketIDs string 所属市场ID列表, 格式 1,2,3
  67. // 参数 descProvinceID int 目标省ID
  68. // 参数 descCityID int 目标城市ID
  69. // 返回 []TopGoods 热门商品列表
  70. // 返回 error error
  71. func GetHsbyTopGoodses(marketIDs string, descProvinceID, descCityID int) ([]HsbyTopGoods, error) {
  72. // 热门商品为二级市场(挂牌点选)下的商品信息
  73. engine := db.GetEngine()
  74. topGoodses := make([]HsbyTopGoods, 0)
  75. // 获取挂牌商品信息,以及扩展表信息
  76. session := engine.Table("GOODS").
  77. Select(`GOODS.*,
  78. HSBY_GOODSEX.HOTINDEX, HSBY_GOODSEX.VIDEOURLS, HSBY_GOODSEX.PICURLS,
  79. ENUMDICITEM.ENUMDICNAME CURRENCY, ENUMDICITEM.PARAM2 CURRENCYSIGN,
  80. MARKET.TRADEMODE`).
  81. Join("LEFT", "HSBY_GOODSEX", "HSBY_GOODSEX.GOODSID = GOODS.GOODSID").
  82. Join("LEFT", "ENUMDICITEM", "ENUMDICITEM.ENUMITEMNAME = GOODS.CURRENCYID and ENUMDICITEM.ENUMDICCODE = 'currency'").
  83. Join("LEFT", "MARKET", "MARKET.MARKETID = GOODS.MARKETID").
  84. Where(fmt.Sprintf("GOODS.GOODSSTATUS = 3 and GOODS.MARKETID in (%s)", marketIDs)) // 二级市场只获取 3:上市 状态的商品
  85. if descProvinceID > 0 {
  86. session = session.And("HSBY_GOODSEX.DESCPROVINCEID = ?", descProvinceID)
  87. }
  88. if descCityID > 0 {
  89. session = session.And("HSBY_GOODSEX.DESCCITYID = ?", descCityID)
  90. }
  91. if err := session.Find(&topGoodses); err != nil {
  92. return nil, err
  93. }
  94. if len(topGoodses) == 0 {
  95. // 无数据
  96. return topGoodses, nil
  97. }
  98. // 获取商品ID列表
  99. goodsCodes := ""
  100. for _, v := range topGoodses {
  101. if len(goodsCodes) == 0 {
  102. goodsCodes = v.Goodscode
  103. } else {
  104. goodsCodes += "," + v.Goodscode
  105. }
  106. }
  107. // 获取商品现价
  108. quoteDays, err := GetQuoteDays(goodsCodes)
  109. if err != nil {
  110. return nil, err
  111. }
  112. for i, g := range topGoodses {
  113. topGoods := &topGoodses[i]
  114. // FIXME: - 这里应该使用 Duck Typing,后期再处理
  115. // 计算最小变动单位
  116. lotSize := float64(topGoods.Quoteminunit) * math.Pow(0.1, float64(topGoods.Decimalplace))
  117. topGoods.LotSize, _ = strconv.ParseFloat(utils.FormatFloat(lotSize, int(topGoods.Decimalplace)), 64)
  118. for _, q := range quoteDays {
  119. if g.Goodscode == q.Goodscode {
  120. if q.Last != 0 {
  121. topGoods.Last = utils.IntToFloat64(int(q.Last), int(g.Decimalplace))
  122. }
  123. // 没有现价则尝试用昨结
  124. if topGoods.Last == 0 && q.Presettle != 0 {
  125. topGoods.Last = utils.IntToFloat64(int(q.Presettle), int(g.Decimalplace))
  126. }
  127. continue
  128. }
  129. }
  130. }
  131. return topGoodses, nil
  132. }
  133. // HsbyListingGoodsDetail 二级市场(挂牌点选)商品信息详情
  134. type HsbyListingGoodsDetail struct {
  135. Goodsid int64 `json:"goodsid" xorm:"'GOODSID'" binding:"required"` // 商品ID(自增ID SEQ_GOODS)
  136. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'" binding:"required"` // 商品代码(内部)
  137. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'" binding:"required"` // 商品名称
  138. Decimalplace int64 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 报价小数位
  139. Marketid int64 `json:"marketid" xorm:"'MARKETID'" binding:"required"` // 所属市场ID
  140. Quoteminunit int64 `json:"quoteminunit" xorm:"'QUOTEMINUNIT'"` // 行情最小变动单位 [整数,报价小数位一起使用]
  141. Agreeunit float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约单位
  142. Hotindex int32 `json:"hotindex" xorm:"'HOTINDEX'"` // 景点热度
  143. Videourls string `json:"videourls" xorm:"'VIDEOURLS'"` // 介绍视频[多张用逗号分隔]
  144. Picurls string `json:"picurls" xorm:"'PICURLS'"` // 介绍图片[多张用逗号分隔]
  145. Descprovinceid int64 `json:"descprovinceid" xorm:"'DESCPROVINCEID'"` // 目的地(省)ID
  146. Desccityid int64 `json:"desccityid" xorm:"'DESCCITYID'"` // 目的地(市)ID
  147. Goodsdesc string `json:"goodsdesc" xorm:"'GOODSDESC'"` // 商品详情
  148. Currency string `json:"currency" xorm:"'CURRENCY'"` // 货币
  149. Currencysign string `json:"currencysign" xorm:"'CURRENCYSIGN'"` // 货币符号
  150. Vendorname string `json:"vendorname" xorm:"'VENDORNAME'"` // 供应商名称
  151. Vendorphone string `json:"vendorphone" xorm:"'VENDORPHONE'"` // 供应商客服电话
  152. Vendorattr string `json:"vendorattr" xorm:"'VENDORATTR'"` // 供应商附件(多张,逗号分隔)
  153. Trademode int32 `json:"trademode" xorm:"'TRADEMODE'" binding:"required"` // 交易模式 - 10:做市 13:竞价 15:通道交易 16:挂牌点选 17:仓单贸易 18:期权 19:竞拍-降价式 20:竞拍-竞价式 21:竞拍-大宗式 22:受托竞价
  154. Last float64 `json:"last" xorm:"-"` // 现价
  155. Limitup float64 `json:"limitup" xorm:"-"` // 涨停价
  156. Limitdown float64 `json:"limitdown" xorm:"-"` // 跌停价
  157. LotSize float64 `json:"lotsize" xorm:"-"` // 最小变动单位
  158. }
  159. // GetHsbyListingGoodsDetail 获取二级市场(挂牌点选)商品信息详情
  160. // 参数 goodsID int 目标商品ID
  161. // 返回 *HsbyListingGoodsDetail 二级市场(挂牌点选)商品信息详情,查询无结果返回nil
  162. // 返回 error error
  163. func GetHsbyListingGoodsDetail(goodsID int) (*HsbyListingGoodsDetail, error) {
  164. engine := db.GetEngine()
  165. details := make([]HsbyListingGoodsDetail, 0)
  166. // 获取挂牌商品信息,以及扩展表信息
  167. // FIXME: - 这里使用Get方法,会造成SQL语句的嵌套出错,后期再研究
  168. session := engine.Table("GOODS").
  169. Select(`GOODS.GOODSID, GOODS.GOODSCODE, GOODS.GOODSNAME, GOODS.DECIMALPLACE, GOODS.MARKETID, GOODS.QUOTEMINUNIT, GOODS.AGREEUNIT,
  170. HSBY_GOODSEX.HOTINDEX, HSBY_GOODSEX.VIDEOURLS, HSBY_GOODSEX.PICURLS, HSBY_GOODSEX.DESCPROVINCEID, HSBY_GOODSEX.Desccityid, HSBY_GOODSEX.Goodsdesc,
  171. ENUMDICITEM.ENUMDICNAME CURRENCY, ENUMDICITEM.PARAM2 CURRENCYSIGN,
  172. HSBY_SUPPLIERINFO.VENDORNAME, HSBY_SUPPLIERINFO.VENDORPHONE, HSBY_SUPPLIERINFO.VENDORATTR,
  173. MARKET.TRADEMODE`).
  174. Join("LEFT", "HSBY_GOODSEX", "HSBY_GOODSEX.GOODSID = GOODS.GOODSID").
  175. Join("LEFT", "ENUMDICITEM", "ENUMDICITEM.ENUMITEMNAME = GOODS.CURRENCYID and ENUMDICITEM.ENUMDICCODE = 'currency'").
  176. Join("LEFT", "HSBY_SUPPLIERINFO", "HSBY_SUPPLIERINFO.VENDORID = HSBY_GOODSEX.VENDORID").
  177. Join("LEFT", "MARKET", "MARKET.MARKETID = GOODS.MARKETID").
  178. Where("GOODS.GOODSID = ?", goodsID)
  179. if err := session.Find(&details); err != nil {
  180. return nil, err
  181. }
  182. // 无目标商品
  183. if len(details) == 0 {
  184. return nil, nil
  185. }
  186. hsbyListingGoodsDetail := details[0]
  187. // 获取商品现价和涨跌停价
  188. quoteDays, err := GetQuoteDays(hsbyListingGoodsDetail.Goodscode)
  189. if err != nil {
  190. return nil, err
  191. }
  192. if len(quoteDays) > 0 {
  193. if quoteDays[0].Last != 0 {
  194. hsbyListingGoodsDetail.Last = utils.IntToFloat64(int(quoteDays[0].Last), int(hsbyListingGoodsDetail.Decimalplace))
  195. }
  196. // 没有现价则尝试用昨结
  197. if hsbyListingGoodsDetail.Last == 0 && quoteDays[0].Presettle != 0 {
  198. hsbyListingGoodsDetail.Last = utils.IntToFloat64(int(quoteDays[0].Presettle), int(hsbyListingGoodsDetail.Decimalplace))
  199. }
  200. if quoteDays[0].Limitup != 0 {
  201. hsbyListingGoodsDetail.Limitup = utils.IntToFloat64(int(quoteDays[0].Limitup), int(hsbyListingGoodsDetail.Decimalplace))
  202. }
  203. if quoteDays[0].Limitdown != 0 {
  204. hsbyListingGoodsDetail.Limitdown = utils.IntToFloat64(int(quoteDays[0].Limitdown), int(hsbyListingGoodsDetail.Decimalplace))
  205. }
  206. }
  207. // FIXME: - 这里应该使用 Duck Typing,后期再处理
  208. // 计算最小变动单位
  209. lotSize := float64(hsbyListingGoodsDetail.Quoteminunit) * math.Pow(0.1, float64(hsbyListingGoodsDetail.Decimalplace))
  210. hsbyListingGoodsDetail.LotSize, _ = strconv.ParseFloat(utils.FormatFloat(lotSize, int(hsbyListingGoodsDetail.Decimalplace)), 64)
  211. return &hsbyListingGoodsDetail, nil
  212. }
  213. // HsbyGoodsOrderDetail 二级市场挂牌商品当前可摘委托单信息
  214. type HsbyGoodsOrderDetail struct {
  215. Orderid string `json:"orderid" xorm:"'ORDERID'" binding:"required"` // 委托单号(100+Unix秒时间戳(10位)+2位(MarketServiceID)+xxxx)
  216. Buyorsell int32 `json:"buyorsell" xorm:"'BUYORSELL'" binding:"required"` // 买卖 - 0:买 1:卖
  217. Ordertime time.Time `json:"ordertime" xorm:"'ORDERTIME'" binding:"required"` // 委托时间
  218. Orderprice float64 `json:"orderprice" xorm:"'ORDERPRICE'"` // 委托价格
  219. Enableqty int64 `json:"enableqty" xorm:"ENABLEQTY"` // 可用数量
  220. Customername string `json:"customername" xorm:"'CUSTOMERNAME'"` // 客户名称(企业名称),已脱敏
  221. Currencysign string `json:"currencysign" xorm:"-"` // 货币符号
  222. Goodunit string `json:"goodunit" xorm:"-"` // 报价单位
  223. }
  224. // GetHsbyGoodsOrderDetails 获取二级市场(挂牌点选)商品对应的挂牌委托单信息
  225. // 输入 goodsID int 商品ID
  226. // 输入 buyOrSell int 挂牌委托单方向(对手单方向),0:买 1:卖
  227. // 输入 price float64 参考价格。买方向委托单则价格小于等于(站在摘牌人的角度);卖方向委托单则价格大于等于
  228. // 输出 []HsbyGoodsOrderDetail 商品对应的挂牌委托单信息
  229. // 输出 error error
  230. func GetHsbyGoodsOrderDetails(goodsID, buyOrSell int, price float64, accountIDs string) ([]HsbyGoodsOrderDetail, error) {
  231. engine := db.GetEngine()
  232. // 获取与目标商品相关的挂牌委托单信息(ListingSelectType = 1 or 3; OrderStatus =3 or 7)
  233. hsbyGoodsOrderDetails := make([]HsbyGoodsOrderDetail, 0)
  234. session := engine.Table("TRADE_ORDERDETAIL").
  235. Select(`to_char(TRADE_ORDERDETAIL.ORDERID) ORDERID, TRADE_ORDERDETAIL.BUYORSELL, TRADE_ORDERDETAIL.ORDERTIME, TRADE_ORDERDETAIL.ORDERPRICE, (TRADE_ORDERDETAIL.ORDERQTY - TRADE_ORDERDETAIL.TRADEQTY - TRADE_ORDERDETAIL.CANCELQTY) ENABLEQTY,
  236. substr(USERINFO.CUSTOMERNAME,0,1)||'****' as CUSTOMERNAME`).
  237. Join("LEFT", "TAACCOUNT", "TAACCOUNT.ACCOUNTID = TRADE_ORDERDETAIL.ACCOUNTID").
  238. Join("LEFT", "USERINFO", "USERINFO.USERID = TAACCOUNT.RELATEDUSERID").
  239. Where("(TRADE_ORDERDETAIL.LISTINGSELECTTYPE = 1 or TRADE_ORDERDETAIL.LISTINGSELECTTYPE = 3) and (TRADE_ORDERDETAIL.ORDERSTATUS = 3 or TRADE_ORDERDETAIL.ORDERSTATUS = 7)").
  240. And("TRADE_ORDERDETAIL.GOODSID = ?", goodsID).
  241. And("TRADE_ORDERDETAIL.BUYORSELL = ?", buyOrSell).
  242. And(fmt.Sprintf("TRADE_ORDERDETAIL.ACCOUNTID not in (%s)", accountIDs))
  243. if price > 0 {
  244. if buyOrSell == 0 {
  245. // 对手单买方向委托单则价格大于等于(站在摘牌人的角度,摘牌方面是卖,我的闲置下单)
  246. session = session.And("TRADE_ORDERDETAIL.ORDERPRICE >= ?", price)
  247. } else {
  248. // 对手单卖方向委托单则价格小于等于(站在摘牌人的角度,摘牌方面是买,热门商品下单)
  249. session = session.And("TRADE_ORDERDETAIL.ORDERPRICE <= ?", price)
  250. }
  251. }
  252. if err := session.Find(&hsbyGoodsOrderDetails); err != nil {
  253. return nil, err
  254. }
  255. // 获取商品货币符号和报价单位
  256. goods, err := GetGoods(goodsID)
  257. if err != nil {
  258. return nil, err
  259. }
  260. currencyEnums, err := GetEnumDicItem("currency", int(goods.Currencyid))
  261. if err != nil {
  262. return nil, err
  263. }
  264. currencysign := ""
  265. if len(currencyEnums) > 0 {
  266. currencysign = currencyEnums[0].Param2
  267. }
  268. goodsUnitEnums, err := GetEnumDicItem("goodsunit", int(goods.Goodunitid))
  269. if err != nil {
  270. return nil, err
  271. }
  272. goodsUnit := ""
  273. if len(goodsUnitEnums) > 0 {
  274. goodsUnit = goodsUnitEnums[0].Enumdicname
  275. }
  276. for i := range hsbyGoodsOrderDetails {
  277. orderDetail := &hsbyGoodsOrderDetails[i]
  278. // 设置货币符号和报价单位
  279. orderDetail.Currencysign = currencysign
  280. orderDetail.Goodunit = goodsUnit
  281. }
  282. return hsbyGoodsOrderDetails, nil
  283. }
  284. // HybsMyBuyOrderDetail 我的订单(一二级市场的买方向委托单)
  285. type HybsMyBuyOrderDetail struct {
  286. Orderid string `json:"orderid" xorm:"'ORDERID'" binding:"required"` // 委托单号(100+Unix秒时间戳(10位)+2位(MarketServiceID)+xxxx)
  287. Marketid int32 `json:"marketid" xorm:"'MARKETID'" binding:"required"` // 市场ID
  288. Goodsid int32 `json:"goodsid" xorm:"'GOODSID'" binding:"required"` // 商品ID
  289. Accountid int64 `json:"accountid" xorm:"'ACCOUNTID'" binding:"required"` // 账户ID[报价币种]
  290. Buyorsell int32 `json:"buyorsell" xorm:"'BUYORSELL'" binding:"required"` // 买卖 - 0:买 1:卖
  291. Orderprice float64 `json:"orderprice" xorm:"'ORDERPRICE'"` // 委托价格
  292. Orderqty int64 `json:"orderqty" xorm:"'ORDERQTY'" binding:"required"` // 委托数量
  293. Tradeqty int64 `json:"tradeqty" xorm:"'TRADEQTY'"` // 成交数量
  294. Cancelqty int64 `json:"cancelqty" xorm:"'CANCELQTY'"` // 撤单数量
  295. Ordertime time.Time `json:"ordertime" xorm:"'ORDERTIME'" binding:"required"` // 委托时间
  296. Listingselecttype int32 `json:"listingselecttype" xorm:"'LISTINGSELECTTYPE'"` // 挂牌点选类型 - 1:挂牌 2:摘牌 3:先摘后挂
  297. Orderstatus int32 `json:"orderstatus" xorm:"'ORDERSTATUS'"` // 委托状态 - 1: 委托请求 2:待冻结 3:委托成功 4: 委托失败 5:配对成功 6: 已撤销 7:部分成交 8:已成交 9:部成部撤 10:成交失败 11:已拒绝 12:经过摘牌(先摘后挂专用-先摘后挂已摘过) 13:冻结成功(通道交易专用) 14:通道已撤 15:通道部成部撤 16:成交失败违约(荷兰式竞拍专用)
  298. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'" binding:"required"` // 商品代码(内部)
  299. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'" binding:"required"` // 商品名称
  300. Decimalplace int64 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 报价小数位
  301. Agreeunit float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约单位
  302. Picurls1 string `json:"picurls1" xorm:"'PICURLS1'"` // 预售商品介绍图片[多张用逗号分隔]
  303. Picurls2 string `json:"picurls2" xorm:"'PICURLS2'"` // 挂牌商品介绍图片[多张用逗号分隔]
  304. Trademode uint32 `json:"trademode" xorm:"'TRADEMODE'" binding:"required"` // 交易模式 - 10:做市 13:竞价 15:通道交易 16:挂牌点选 17:仓单贸易 18:期权 19:竞拍-降价式 20:竞拍-竞价式 21:竞拍-大宗式 22:受托竞价
  305. Currencysign string `json:"currencysign" xorm:"'CURRENCYSIGN'"` // 货币符号
  306. Vendorname1 string `json:"vendorname1" xorm:"'VENDORNAME1'"` // 预售商品供应商名称
  307. Vendorname2 string `json:"vendorname2" xorm:"'VENDORNAME2'"` // 挂牌商品供应商名称
  308. MyBuyStatus int `json:"mybuystatus" xorm:"-"` // "我的订单"显示状态- 1:抢购中 2:求购中 3:已完成 4:已撤消 5:委托失败
  309. Orderamount float64 `json:"orderamount" xorm:"-"` // 委托金额
  310. }
  311. // GetHsbyBuyMyOrderDetails 获取“我的订单”数据(包括一二级市场)
  312. // 输入 accountIDs string 资金账户列表
  313. // 输入 myBuyStatus int "我的订单"状态, 1:抢购中 2:求购中 3:已完成
  314. // 输出 []HybsMyBuyOrderDetail “我的订单”数据
  315. // 输出 error error
  316. func GetHsbyBuyMyOrderDetails(accountIDs string, myBuyStatus int) ([]HybsMyBuyOrderDetail, error) {
  317. // 获取市场信息
  318. markets, err := GetMarkets()
  319. if err != nil {
  320. return nil, err
  321. }
  322. engine := db.GetEngine()
  323. // 委托状态
  324. orderStatus := "0" // 单据状态,为0的时候查询全部
  325. marketIDs := "" // 我的订单包括一二级市场的单据
  326. switch myBuyStatus {
  327. case 1: // 抢购中 (一级市场)
  328. // 获取市场ID
  329. for _, v := range markets {
  330. if v.Trademode == 71 { // 预售挂牌
  331. if len(marketIDs) == 0 {
  332. marketIDs = strconv.Itoa(int(v.Marketid))
  333. } else {
  334. marketIDs += "," + strconv.Itoa(int(v.Marketid))
  335. }
  336. }
  337. }
  338. orderStatus = "3,7"
  339. case 2: // 求购中(二级市场)
  340. // 获取市场ID
  341. for _, v := range markets {
  342. if v.Trademode == 16 { // 挂牌点选
  343. if len(marketIDs) == 0 {
  344. marketIDs = strconv.Itoa(int(v.Marketid))
  345. } else {
  346. marketIDs += "," + strconv.Itoa(int(v.Marketid))
  347. }
  348. }
  349. }
  350. orderStatus = "3,7"
  351. case 3: // 已完成
  352. orderStatus = "8,9"
  353. }
  354. // 默认取 TradeMode = 16 or 71 的市场
  355. if len(marketIDs) == 0 {
  356. for _, v := range markets {
  357. if v.Trademode == 16 || v.Trademode == 71 {
  358. if len(marketIDs) == 0 {
  359. marketIDs = strconv.Itoa(int(v.Marketid))
  360. } else {
  361. marketIDs += "," + strconv.Itoa(int(v.Marketid))
  362. }
  363. }
  364. }
  365. }
  366. hybsMyBuyOrderDetails := make([]HybsMyBuyOrderDetail, 0)
  367. // "我的订单"都是买委托
  368. session := engine.Table("TRADE_ORDERDETAIL").
  369. Select(`to_char(TRADE_ORDERDETAIL.ORDERID) ORDERID, TRADE_ORDERDETAIL.*,
  370. GOODS.GOODSCODE, GOODS.GOODSNAME, GOODS.DECIMALPLACE, GOODS.AGREEUNIT,
  371. HG1.PICURLS PICURLS1,
  372. HG2.PICURLS PICURLS2,
  373. MARKET.TRADEMODE,
  374. ENUMDICITEM.PARAM2 CURRENCYSIGN,
  375. HS1.VENDORNAME VENDORNAME1,
  376. HS2.VENDORNAME VENDORNAME2`).
  377. Join("LEFT", "GOODS", "GOODS.GOODSID = TRADE_ORDERDETAIL.GOODSID").
  378. Join("LEFT", "CPTRADE_PRESALEGOODSEX", "CPTRADE_PRESALEGOODSEX.GOODSID = TRADE_ORDERDETAIL.GOODSID").
  379. Join("LEFT", "HSBY_GOODSEX HG1", "HG1.GOODSID = CPTRADE_PRESALEGOODSEX.RELATEDGOODSID").
  380. Join("LEFT", "HSBY_GOODSEX HG2", "HG2.GOODSID = GOODS.GOODSID").
  381. Join("LEFT", "ENUMDICITEM", "GOODS.CURRENCYID = ENUMDICITEM.ENUMITEMNAME and ENUMDICITEM.ENUMDICCODE = 'currency'").
  382. Join("LEFT", "MARKET", "MARKET.MARKETID = TRADE_ORDERDETAIL.MARKETID").
  383. Join("LEFT", "HSBY_SUPPLIERINFO HS1", "HS1.VENDORID = HG1.VENDORID").
  384. Join("LEFT", "HSBY_SUPPLIERINFO HS2", "HS2.VENDORID = HG2.VENDORID").
  385. Where(fmt.Sprintf("TRADE_ORDERDETAIL.BUYORSELL = 0 and TRADE_ORDERDETAIL.ACCOUNTID in (%s)", accountIDs)).
  386. And(fmt.Sprintf("TRADE_ORDERDETAIL.MARKETID in (%s)", marketIDs))
  387. // 是否过滤状态(抢购中、求购中、已完成)
  388. if orderStatus != "0" {
  389. session = session.And(fmt.Sprintf("TRADE_ORDERDETAIL.ORDERSTATUS in (%s)", orderStatus))
  390. }
  391. if err := session.Find(&hybsMyBuyOrderDetails); err != nil {
  392. return nil, err
  393. }
  394. // 设置“我的订单”显示状态
  395. for i := range hybsMyBuyOrderDetails {
  396. detail := &hybsMyBuyOrderDetails[i]
  397. // 委托金额 = 委托价格 * 委托数量 * 合约单位
  398. detail.Orderamount = detail.Orderprice * float64(detail.Orderqty) * float64(detail.Agreeunit)
  399. if detail.Trademode == 71 && (detail.Orderstatus == 3 || detail.Orderstatus == 7) {
  400. // 抢购中
  401. detail.MyBuyStatus = 1
  402. } else if detail.Trademode == 16 && (detail.Orderstatus == 3 || detail.Orderstatus == 7) {
  403. // 求购中
  404. detail.MyBuyStatus = 2
  405. } else if detail.Orderstatus == 8 || detail.Orderstatus == 9 {
  406. // 已完成
  407. detail.MyBuyStatus = 3
  408. } else if detail.Orderstatus == 6 {
  409. // 已撤销
  410. detail.MyBuyStatus = 4
  411. } else if detail.Orderstatus == 4 {
  412. // 委托失败
  413. detail.MyBuyStatus = 5
  414. }
  415. }
  416. return hybsMyBuyOrderDetails, nil
  417. }
  418. // HsbyMyGoods 我的商品(买方向持仓)
  419. type HsbyMyGoods struct {
  420. Accountid int64 `json:"accountid" xorm:"'ACCOUNTID'" binding:"required"` // 账号Id
  421. Goodsid int32 `json:"goodsid" xorm:"'GOODSID'" binding:"required"` // 商品Id
  422. Buycurpositionqty int64 `json:"buycurpositionqty" xorm:"'BUYCURPOSITIONQTY'"` // 买当前持仓总数量
  423. Buycurholderamount float64 `json:"buycurholderamount" xorm:"'BUYCURHOLDERAMOUNT'"` // 买当前持仓总金额[商品币种]
  424. EnableQty int64 `json:"enableqty" xorm:"'ENABLEQTY'"` // 可用数量
  425. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'" binding:"required"` // 商品代码(内部)
  426. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'" binding:"required"` // 商品名称
  427. Decimalplace int64 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 报价小数位
  428. Agreeunit float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约单位
  429. Picurls string `json:"picurls" xorm:"'PICURLS'"` // 介绍图片[多张用逗号分隔]
  430. Currencysign string `json:"currencysign" xorm:"'CURRENCYSIGN'"` // 货币符号
  431. Buyaverageprice float64 `json:"buyaverageprice" xorm:"-"` // 持仓均价
  432. }
  433. // GetHsbyMyGoods 获取“我的商品”信息
  434. // 输入 accountIDs string 资金账户列表,格式 1,2,3
  435. // 输出 []HsbyMyGoods “我的商品”信息
  436. // 输入 error error
  437. func GetHsbyMyGoods(accountIDs string) ([]HsbyMyGoods, error) {
  438. engine := db.GetEngine()
  439. hsbyMyGoodses := make([]HsbyMyGoods, 0)
  440. // 此定制版本,只查询出当前手数(期末)大于0的数据 (TRADEPOSITION.BUYCURPOSITIONQTY > 0)
  441. if err := engine.Table("TRADEPOSITION").
  442. Select(`TRADEPOSITION.*, (TRADEPOSITION.BUYCURPOSITIONQTY - TRADEPOSITION.BUYFROZENQTY - TRADEPOSITION.BUYOTHERFROZENQTY) ENABLEQTY,
  443. GOODS.GOODSCODE, GOODS.GOODSNAME, GOODS.DECIMALPLACE, GOODS.AGREEUNIT,
  444. HSBY_GOODSEX.PICURLS,
  445. ENUMDICITEM.PARAM2 CURRENCYSIGN`).
  446. Join("LEFT", "GOODS", "GOODS.GOODSID = TRADEPOSITION.GOODSID").
  447. Join("INNER", "HSBY_GOODSEX", "HSBY_GOODSEX.GOODSID = GOODS.GOODSID").
  448. Join("LEFT", "ENUMDICITEM", "GOODS.CURRENCYID = ENUMDICITEM.ENUMITEMNAME and ENUMDICITEM.ENUMDICCODE = 'currency'").
  449. Where(fmt.Sprintf("TRADEPOSITION.BUYCURPOSITIONQTY > 0 and TRADEPOSITION.ACCOUNTID in (%s)", accountIDs)).Find(&hsbyMyGoodses); err != nil {
  450. return nil, err
  451. }
  452. for i := range hsbyMyGoodses {
  453. myGoods := &hsbyMyGoodses[i]
  454. // 持仓均价
  455. averagePrice := myGoods.Buycurholderamount / float64(myGoods.Buycurpositionqty) / myGoods.Agreeunit
  456. myGoods.Buyaverageprice, _ = strconv.ParseFloat(utils.FormatFloat(averagePrice, int(myGoods.Decimalplace)), 64)
  457. }
  458. return hsbyMyGoodses, nil
  459. }
  460. // HsbyPreGoods 新品上市商品(一级市场产能预售)
  461. type HsbyPreGoods struct {
  462. Goodsid int64 `json:"goodsid" xorm:"'GOODSID'" binding:"required"` // 商品ID(自增ID SEQ_GOODS)
  463. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'" binding:"required"` // 商品代码(内部)
  464. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'" binding:"required"` // 商品名称
  465. Decimalplace int64 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 报价小数位
  466. Marketid int64 `json:"marketid" xorm:"'MARKETID'" binding:"required"` // 所属市场ID
  467. Goodsstatus int64 `json:"goodsstatus" xorm:"'GOODSSTATUS'"` // 商品状态- 2:未上市 3:上市
  468. Quoteminunit int64 `json:"quoteminunit" xorm:"'QUOTEMINUNIT'"` // 行情最小变动单位 [整数,报价小数位一起使用]
  469. Agreeunit float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约单位
  470. Relatedgoodsid int64 `json:"relatedgoodsid" xorm:"'RELATEDGOODSID'"` // 关联交易合约ID
  471. Presaleqty int64 `json:"presaleqty" xorm:"'PRESALEQTY'"` // 预售数量
  472. Starttime time.Time `json:"starttime" xorm:"'STARTTIME'"` // 预售开始时间
  473. Endtime time.Time `json:"endtime" xorm:"'ENDTIME'"` // 预售结束时间
  474. Refprice float64 `json:"refprice" xorm:"'REFPRICE'"` // 参考价格[一口价]
  475. Presaledqty int64 `json:"presaledqty" xorm:"'PRESALEDQTY'"` // 已预售量(预售结束时更新)
  476. EnableQty int64 `json:"enableqty" xorm:"ENABLEQTY"` // 剩余数量
  477. Currency string `json:"currency" xorm:"'CURRENCY'"` // 货币
  478. Currencysign string `json:"currencysign" xorm:"'CURRENCYSIGN'"` // 货币符号
  479. Videourls string `json:"videourls" xorm:"'VIDEOURLS'"` // 介绍视频[多张用逗号分隔]
  480. Picurls string `json:"picurls" xorm:"'PICURLS'"` // 介绍图片[多张用逗号分隔]
  481. Trademode int32 `json:"trademode" xorm:"'TRADEMODE'" binding:"required"` // 交易模式 - 10:做市 13:竞价 15:通道交易 16:挂牌点选 17:仓单贸易 18:期权 19:竞拍-降价式 20:竞拍-竞价式 21:竞拍-大宗式 22:受托竞价
  482. LotSize float64 `json:"lotsize" xorm:"-"` // 最小变动单位
  483. }
  484. // GetHsbyPreGoodses 获取“新品上市”商品列表(一级市场产能预售)
  485. // 输入 marketIDs string 市场ID列表,格式 1,2,3
  486. // 输入 descProvinceID int 省ID
  487. // 输入 descCityID int 市ID
  488. // 输出 []HsbyPreGoods “新品上市”商品列表
  489. // 输出 error error
  490. func GetHsbyPreGoodses(marketIDs string, descProvinceID, descCityID int) ([]HsbyPreGoods, error) {
  491. engine := db.GetEngine()
  492. // 注意:一级产能预售市场使用的商品实际上是和二级市场有关系的,所以要使用 CPTRADE_PRESALEGOODSEX.RELATEDGOODSID 进行关联
  493. preGoodses := make([]HsbyPreGoods, 0)
  494. session := engine.Table("GOODS").
  495. Select(`GOODS.*,
  496. CPTRADE_PRESALEGOODSEX.RELATEDGOODSID, CPTRADE_PRESALEGOODSEX.PRESALEQTY,
  497. CPTRADE_PRESALEGOODSEX.STARTTIME, CPTRADE_PRESALEGOODSEX.ENDTIME, CPTRADE_PRESALEGOODSEX.REFPRICE,
  498. CPTRADE_PRESALEGOODSEX.PRESALEDQTY, (CPTRADE_PRESALEGOODSEX.PRESALEQTY - CPTRADE_PRESALEGOODSEX.PRESALEDQTY) ENABLEQTY,
  499. ENUMDICITEM.ENUMDICNAME CURRENCY, ENUMDICITEM.PARAM2 CURRENCYSIGN,
  500. HSBY_GOODSEX.VIDEOURLS, HSBY_GOODSEX.PICURLS,
  501. MARKET.TRADEMODE`).
  502. Join("INNER", "CPTRADE_PRESALEGOODSEX", "CPTRADE_PRESALEGOODSEX.GOODSID = GOODS.GOODSID").
  503. Join("LEFT", "ENUMDICITEM", "ENUMDICITEM.ENUMITEMNAME = GOODS.CURRENCYID and ENUMDICITEM.ENUMDICCODE = 'currency'").
  504. Join("LEFT", "HSBY_GOODSEX", "HSBY_GOODSEX.GOODSID = CPTRADE_PRESALEGOODSEX.RELATEDGOODSID").
  505. Join("LEFT", "MARKET", "MARKET.MARKETID = GOODS.MARKETID").
  506. Where(fmt.Sprintf("GOODS.GOODSSTATUS in (2,3) and GOODS.MARKETID in (%s)", marketIDs)) // 一级市场获取 2:未上市 3:上市 状态的商品
  507. if descProvinceID > 0 {
  508. session = session.And("HSBY_GOODSEX.DESCPROVINCEID = ?", descProvinceID)
  509. }
  510. if descCityID > 0 {
  511. session = session.And("HSBY_GOODSEX.DESCCITYID = ?", descCityID)
  512. }
  513. if err := session.Find(&preGoodses); err != nil {
  514. return nil, err
  515. }
  516. for i := range preGoodses {
  517. preGoods := &preGoodses[i]
  518. // FIXME: - 这里应该使用 Duck Typing,后期再处理
  519. // 计算最小变动单位
  520. lotSize := float64(preGoods.Quoteminunit) * math.Pow(0.1, float64(preGoods.Decimalplace))
  521. preGoods.LotSize, _ = strconv.ParseFloat(utils.FormatFloat(lotSize, int(preGoods.Decimalplace)), 64)
  522. }
  523. return preGoodses, nil
  524. }
  525. // HsbyPreGoodsDetail 一级市场(产能预售)商品信息详情
  526. type HsbyPreGoodsDetail struct {
  527. Goodsid int64 `json:"goodsid" xorm:"'GOODSID'" binding:"required"` // 商品ID(自增ID SEQ_GOODS)
  528. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'" binding:"required"` // 商品代码(内部)
  529. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'" binding:"required"` // 商品名称
  530. Decimalplace int64 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 报价小数位
  531. Marketid int64 `json:"marketid" xorm:"'MARKETID'" binding:"required"` // 所属市场ID
  532. Goodsstatus int64 `json:"goodsstatus" xorm:"'GOODSSTATUS'"` // 商品状态- 2:未上市 3:上市
  533. Quoteminunit int64 `json:"quoteminunit" xorm:"'QUOTEMINUNIT'"` // 行情最小变动单位 [整数,报价小数位一起使用]
  534. Agreeunit float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约单位
  535. Relatedgoodsid int64 `json:"relatedgoodsid" xorm:"'RELATEDGOODSID'"` // 关联交易合约ID
  536. Presaleqty int64 `json:"presaleqty" xorm:"'PRESALEQTY'"` // 预售数量
  537. Starttime time.Time `json:"starttime" xorm:"'STARTTIME'"` // 预售开始时间
  538. Endtime time.Time `json:"endtime" xorm:"'ENDTIME'"` // 预售结束时间
  539. Refprice float64 `json:"refprice" xorm:"'REFPRICE'"` // 参考价格[一口价]
  540. Presaledqty int64 `json:"presaledqty" xorm:"'PRESALEDQTY'"` // 已预售量(预售结束时更新)
  541. EnableQty int64 `json:"enableqty" xorm:"ENABLEQTY"` // 剩余数量
  542. Videourls string `json:"videourls" xorm:"'VIDEOURLS'"` // 介绍视频[多张用逗号分隔]
  543. Picurls string `json:"picurls" xorm:"'PICURLS'"` // 介绍图片[多张用逗号分隔]
  544. Descprovinceid int64 `json:"descprovinceid" xorm:"'DESCPROVINCEID'"` // 目的地(省)ID
  545. Desccityid int64 `json:"desccityid" xorm:"'DESCCITYID'"` // 目的地(市)ID
  546. Goodsdesc string `json:"goodsdesc" xorm:"'GOODSDESC'"` // 商品详情
  547. Currency string `json:"currency" xorm:"'CURRENCY'"` // 货币
  548. Currencysign string `json:"currencysign" xorm:"'CURRENCYSIGN'"` // 货币符号
  549. Goodunit string `json:"goodunit" xorm:"GOODUNIT"` // 报价单位
  550. Vendorname string `json:"vendorname" xorm:"'VENDORNAME'"` // 供应商名称
  551. Vendorphone string `json:"vendorphone" xorm:"'VENDORPHONE'"` // 供应商客服电话
  552. Vendorattr string `json:"vendorattr" xorm:"'VENDORATTR'"` // 供应商附件(多张,逗号分隔)
  553. Trademode int32 `json:"trademode" xorm:"'TRADEMODE'" binding:"required"` // 交易模式 - 10:做市 13:竞价 15:通道交易 16:挂牌点选 17:仓单贸易 18:期权 19:竞拍-降价式 20:竞拍-竞价式 21:竞拍-大宗式 22:受托竞价
  554. Customername string `json:"customername" xorm:"'CUSTOMERNAME'"` // 发行单位
  555. LotSize float64 `json:"lotsize" xorm:"-"` // 最小变动单位
  556. }
  557. // GetHsbyPreGoodsDetail 获取一级市场(产能预售)商品信息详情
  558. // 参数 goodsID int 目标商品ID
  559. // 返回 *HsbyListingGoodsDetail 二级市场(挂牌点选)商品信息详情,查询无结果返回nil
  560. // 返回 error error
  561. func GetHsbyPreGoodsDetail(goodsID int) (*HsbyPreGoodsDetail, error) {
  562. engine := db.GetEngine()
  563. details := make([]HsbyPreGoodsDetail, 0)
  564. // 获取挂牌商品信息,以及扩展表信息
  565. // FIXME: - 这里使用Get方法,会造成SQL语句的嵌套出错,后期再研究
  566. session := engine.Table("GOODS").
  567. Select(`GOODS.*,
  568. CPTRADE_PRESALEGOODSEX.RELATEDGOODSID, CPTRADE_PRESALEGOODSEX.PRESALEQTY,
  569. CPTRADE_PRESALEGOODSEX.STARTTIME, CPTRADE_PRESALEGOODSEX.ENDTIME, CPTRADE_PRESALEGOODSEX.REFPRICE,
  570. CPTRADE_PRESALEGOODSEX.PRESALEDQTY, (CPTRADE_PRESALEGOODSEX.PRESALEQTY - CPTRADE_PRESALEGOODSEX.PRESALEDQTY) ENABLEQTY,
  571. HSBY_GOODSEX.VIDEOURLS, HSBY_GOODSEX.PICURLS, HSBY_GOODSEX.DESCPROVINCEID, HSBY_GOODSEX.Desccityid, HSBY_GOODSEX.Goodsdesc,
  572. ENUMDICITEM.ENUMDICNAME CURRENCY, ENUMDICITEM.PARAM2 CURRENCYSIGN,
  573. E1.ENUMDICNAME GOODUNIT,
  574. HSBY_SUPPLIERINFO.VENDORNAME, HSBY_SUPPLIERINFO.VENDORPHONE, HSBY_SUPPLIERINFO.VENDORATTR,
  575. MARKET.TRADEMODE,
  576. USERINFO.CUSTOMERNAME`).
  577. Join("INNER", "CPTRADE_PRESALEGOODSEX", "CPTRADE_PRESALEGOODSEX.GOODSID = GOODS.GOODSID").
  578. Join("LEFT", "HSBY_GOODSEX", "HSBY_GOODSEX.GOODSID = CPTRADE_PRESALEGOODSEX.RELATEDGOODSID").
  579. Join("LEFT", "ENUMDICITEM", "ENUMDICITEM.ENUMITEMNAME = GOODS.CURRENCYID and ENUMDICITEM.ENUMDICCODE = 'currency'").
  580. Join("LEFT", "ENUMDICITEM E1", "E1.ENUMITEMNAME = GOODS.GOODUNITID and E1.ENUMDICCODE = 'goodsunit'").
  581. Join("LEFT", "HSBY_SUPPLIERINFO", "HSBY_SUPPLIERINFO.VENDORID = HSBY_GOODSEX.VENDORID").
  582. Join("LEFT", "MARKET", "MARKET.MARKETID = GOODS.MARKETID").
  583. Join("LEFT", "USERINFO", "USERINFO.USERID = CPTRADE_PRESALEGOODSEX.USERID").
  584. Where("GOODS.GOODSID = ?", goodsID)
  585. if err := session.Find(&details); err != nil {
  586. return nil, err
  587. }
  588. // 无目标商品
  589. if len(details) == 0 {
  590. return nil, nil
  591. }
  592. preGoodsDetail := details[0]
  593. // FIXME: - 这里应该使用 Duck Typing,后期再处理
  594. // 计算最小变动单位
  595. lotSize := float64(preGoodsDetail.Quoteminunit) * math.Pow(0.1, float64(preGoodsDetail.Decimalplace))
  596. preGoodsDetail.LotSize, _ = strconv.ParseFloat(utils.FormatFloat(lotSize, int(preGoodsDetail.Decimalplace)), 64)
  597. return &preGoodsDetail, nil
  598. }
  599. // HsbySellMyDetail "我的闲置"单据信息(已发布 - 二级市场卖挂,3:委托成功、7:部分成交; 已完成 - 二级市场成交单,包括历史数据)
  600. type HsbySellMyDetail struct {
  601. Orderid string `json:"orderid" xorm:"'ORDERID'" binding:"required"` // 单号(已发布 - 委托单号;已完成 - 成交单号)
  602. Marketid int32 `json:"marketid" xorm:"'MARKETID'" binding:"required"` // 市场ID
  603. Goodsid int32 `json:"goodsid" xorm:"'GOODSID'" binding:"required"` // 商品ID
  604. Accountid int64 `json:"accountid" xorm:"'ACCOUNTID'" binding:"required"` // 账户ID[报价币种]
  605. Buyorsell int32 `json:"buyorsell" xorm:"'BUYORSELL'" binding:"required"` // 买卖 - 0:买 1:卖
  606. Price float64 `json:"price" xorm:"'PRICE'"` // 价格
  607. Qty int64 `json:"qty" xorm:"'QTY'" binding:"required"` // 数量
  608. Time time.Time `json:"time" xorm:"'TIME'" binding:"required"` // 时间
  609. Amount float64 `json:"amount" xorm:"'AMOUNT'"` // 金额 = 价格 * 数量 * 合约单位
  610. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'" binding:"required"` // 商品代码(内部)
  611. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'" binding:"required"` // 商品名称
  612. Decimalplace int64 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 报价小数位
  613. Agreeunit float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约单位
  614. Picurls string `json:"picurls" xorm:"'PICURLS'"` // 介绍图片[多张用逗号分隔]
  615. Trademode uint32 `json:"trademode" xorm:"'TRADEMODE'" binding:"required"` // 交易模式 - 10:做市 13:竞价 15:通道交易 16:挂牌点选 17:仓单贸易 18:期权 19:竞拍-降价式 20:竞拍-竞价式 21:竞拍-大宗式 22:受托竞价
  616. Currencysign string `json:"currencysign" xorm:"'CURRENCYSIGN'"` // 货币符号
  617. Vendorname string `json:"vendorname" xorm:"'VENDORNAME'"` // 供应商名称
  618. OrderType int32 `json:"ordertype" xorm:"'ORDERTYPE'"` // 单据类型:0 - 已发布, 1 - 已完成
  619. }
  620. // GetHsbySellMyOrderDetails 获取"我的闲置 - 已发布"单据信息
  621. // 输入 accountIDs string 资金账户列表
  622. // 输出 []HsbySellMyDetail "我的闲置 - 已发布"单据信息
  623. // 输出 error error
  624. func GetHsbySellMyOrderDetails(accountIDs string) ([]HsbySellMyDetail, error) {
  625. // 获取市场信息
  626. markets, err := GetMarkets()
  627. if err != nil {
  628. return nil, err
  629. }
  630. engine := db.GetEngine()
  631. marketIDs := "" // 我的闲置-已发布: 二级市场卖挂牌
  632. // 默认取 TradeMode = 16
  633. for _, v := range markets {
  634. if v.Trademode == 16 {
  635. if len(marketIDs) == 0 {
  636. marketIDs = strconv.Itoa(int(v.Marketid))
  637. } else {
  638. marketIDs += "," + strconv.Itoa(int(v.Marketid))
  639. }
  640. }
  641. }
  642. orderDetails := make([]HsbySellMyDetail, 0)
  643. // “我的闲置 - 已发布”都是卖挂委托
  644. session := engine.Table("TRADE_ORDERDETAIL").
  645. Select(`to_char(TRADE_ORDERDETAIL.ORDERID) ORDERID,
  646. TRADE_ORDERDETAIL.MARKETID, TRADE_ORDERDETAIL.GOODSID, TRADE_ORDERDETAIL.ACCOUNTID, TRADE_ORDERDETAIL.BUYORSELL,
  647. TRADE_ORDERDETAIL.ORDERPRICE PRICE, TRADE_ORDERDETAIL.ORDERQTY QTY, TRADE_ORDERDETAIL.ORDERTIME TIME,
  648. (TRADE_ORDERDETAIL.ORDERPRICE * TRADE_ORDERDETAIL.ORDERQTY * GOODS.AGREEUNIT) AMOUNT,
  649. GOODS.GOODSCODE, GOODS.GOODSNAME, GOODS.DECIMALPLACE, GOODS.AGREEUNIT,
  650. HSBY_GOODSEX.PICURLS,
  651. MARKET.TRADEMODE,
  652. ENUMDICITEM.PARAM2 CURRENCYSIGN,
  653. HSBY_SUPPLIERINFO.VENDORNAME,
  654. 0 ORDERTYPE`).
  655. Join("LEFT", "GOODS", "GOODS.GOODSID = TRADE_ORDERDETAIL.GOODSID").
  656. Join("LEFT", "HSBY_GOODSEX", "HSBY_GOODSEX.GOODSID = GOODS.GOODSID").
  657. Join("LEFT", "ENUMDICITEM", "GOODS.CURRENCYID = ENUMDICITEM.ENUMITEMNAME and ENUMDICITEM.ENUMDICCODE = 'currency'").
  658. Join("LEFT", "MARKET", "MARKET.MARKETID = TRADE_ORDERDETAIL.MARKETID").
  659. Join("LEFT", "HSBY_SUPPLIERINFO", "HSBY_SUPPLIERINFO.VENDORID = HSBY_GOODSEX.VENDORID").
  660. Where(fmt.Sprintf(`TRADE_ORDERDETAIL.BUYORSELL = 1 and TRADE_ORDERDETAIL.LISTINGSELECTTYPE = 1 and (TRADE_ORDERDETAIL.ORDERSTATUS = 3 or TRADE_ORDERDETAIL.ORDERSTATUS = 7)
  661. and TRADE_ORDERDETAIL.ACCOUNTID in (%s)`, accountIDs)).
  662. And(fmt.Sprintf("TRADE_ORDERDETAIL.MARKETID in (%s)", marketIDs))
  663. if err := session.Find(&orderDetails); err != nil {
  664. return nil, err
  665. }
  666. return orderDetails, nil
  667. }
  668. // GetHsbySellMyTradeDetails 获取"我的闲置 - 已完成"单据信息
  669. // 输入 accountIDs string 资金账户列表
  670. // 输出 []HsbySellMyDetail "我的闲置 - 已完成"单据信息
  671. // 输出 error error
  672. func GetHsbySellMyTradeDetails(accountIDs string) ([]HsbySellMyDetail, error) {
  673. // 获取市场信息
  674. markets, err := GetMarkets()
  675. if err != nil {
  676. return nil, err
  677. }
  678. engine := db.GetEngine()
  679. marketIDs := "" // 我的闲置-已发布: 二级市场卖方向成交单(包括历史成交单)
  680. // 默认取 TradeMode = 16
  681. for _, v := range markets {
  682. if v.Trademode == 16 {
  683. if len(marketIDs) == 0 {
  684. marketIDs = strconv.Itoa(int(v.Marketid))
  685. } else {
  686. marketIDs += "," + strconv.Itoa(int(v.Marketid))
  687. }
  688. }
  689. }
  690. orders := make([]HsbySellMyDetail, 0)
  691. // 当前成交单
  692. curOrders := make([]HsbySellMyDetail, 0)
  693. if err := engine.Table("TRADE_TRADEDETAIL").
  694. Select(`to_char(TRADE_TRADEDETAIL.TRADEID) ORDERID,
  695. TRADE_TRADEDETAIL.MARKETID, TRADE_TRADEDETAIL.GOODSID, TRADE_TRADEDETAIL.ACCOUNTID, TRADE_TRADEDETAIL.BUYORSELL,
  696. TRADE_TRADEDETAIL.TRADEPRICE PRICE, TRADE_TRADEDETAIL.TRADEQTY QTY, TRADE_TRADEDETAIL.TRADETIME TIME, TRADE_TRADEDETAIL.TRADEAMOUNT AMOUNT,
  697. GOODS.GOODSCODE, GOODS.GOODSNAME, GOODS.DECIMALPLACE, GOODS.AGREEUNIT,
  698. HSBY_GOODSEX.PICURLS,
  699. MARKET.TRADEMODE,
  700. ENUMDICITEM.PARAM2 CURRENCYSIGN,
  701. HSBY_SUPPLIERINFO.VENDORNAME,
  702. 1 ORDERTYPE`).
  703. Join("LEFT", "GOODS", "GOODS.GOODSID = TRADE_TRADEDETAIL.GOODSID").
  704. Join("LEFT", "HSBY_GOODSEX", "HSBY_GOODSEX.GOODSID = GOODS.GOODSID").
  705. Join("LEFT", "ENUMDICITEM", "GOODS.CURRENCYID = ENUMDICITEM.ENUMITEMNAME and ENUMDICITEM.ENUMDICCODE = 'currency'").
  706. Join("LEFT", "MARKET", "MARKET.MARKETID = TRADE_TRADEDETAIL.MARKETID").
  707. Join("LEFT", "HSBY_SUPPLIERINFO", "HSBY_SUPPLIERINFO.VENDORID = HSBY_GOODSEX.VENDORID").
  708. Where(fmt.Sprintf(`TRADE_TRADEDETAIL.BUYORSELL = 1
  709. and TRADE_TRADEDETAIL.ACCOUNTID in (%s)`, accountIDs)).
  710. And(fmt.Sprintf("TRADE_TRADEDETAIL.MARKETID in (%s)", marketIDs)).Find(&curOrders); err != nil {
  711. return nil, err
  712. }
  713. if len(curOrders) > 0 {
  714. orders = append(orders, curOrders...)
  715. }
  716. // 历史成交单
  717. hisOrders := make([]HsbySellMyDetail, 0)
  718. if err := engine.Table("HIS_TRADE_TRADEDETAIL").
  719. Select(`to_char(HIS_TRADE_TRADEDETAIL.TRADEID) ORDERID,
  720. HIS_TRADE_TRADEDETAIL.MARKETID, HIS_TRADE_TRADEDETAIL.GOODSID, HIS_TRADE_TRADEDETAIL.ACCOUNTID, HIS_TRADE_TRADEDETAIL.BUYORSELL,
  721. HIS_TRADE_TRADEDETAIL.TRADEPRICE PRICE, HIS_TRADE_TRADEDETAIL.TRADEQTY QTY, HIS_TRADE_TRADEDETAIL.TRADETIME TIME, HIS_TRADE_TRADEDETAIL.TRADEAMOUNT AMOUNT,
  722. GOODS.GOODSCODE, GOODS.GOODSNAME, GOODS.DECIMALPLACE, GOODS.AGREEUNIT,
  723. HSBY_GOODSEX.PICURLS,
  724. MARKET.TRADEMODE,
  725. ENUMDICITEM.PARAM2 CURRENCYSIGN,
  726. HSBY_SUPPLIERINFO.VENDORNAME,
  727. 1 ORDERTYPE`).
  728. Join("LEFT", "GOODS", "GOODS.GOODSID = HIS_TRADE_TRADEDETAIL.GOODSID").
  729. Join("LEFT", "HSBY_GOODSEX", "HSBY_GOODSEX.GOODSID = GOODS.GOODSID").
  730. Join("LEFT", "ENUMDICITEM", "GOODS.CURRENCYID = ENUMDICITEM.ENUMITEMNAME and ENUMDICITEM.ENUMDICCODE = 'currency'").
  731. Join("LEFT", "MARKET", "MARKET.MARKETID = HIS_TRADE_TRADEDETAIL.MARKETID").
  732. Join("LEFT", "HSBY_SUPPLIERINFO", "HSBY_SUPPLIERINFO.VENDORID = HSBY_GOODSEX.VENDORID").
  733. Where(fmt.Sprintf(`HIS_TRADE_TRADEDETAIL.BUYORSELL = 1 and HIS_TRADE_TRADEDETAIL.ISVALIDDATA = 1
  734. and HIS_TRADE_TRADEDETAIL.ACCOUNTID in (%s)`, accountIDs)).
  735. And(fmt.Sprintf("HIS_TRADE_TRADEDETAIL.MARKETID in (%s)", marketIDs)).Find(&hisOrders); err != nil {
  736. return nil, err
  737. }
  738. if len(hisOrders) > 0 {
  739. orders = append(orders, hisOrders...)
  740. }
  741. return orders, nil
  742. }
  743. // HsbyMyPackage 我的包裹信息
  744. type HsbyMyPackage struct {
  745. Takeorderid string `json:"takeorderid" xorm:"'TAKEORDERID'" binding:"required"` // 提货单号(905+Unix秒时间戳(10位)+xxxxxx)
  746. Accountid int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 账户ID
  747. Goodsid int64 `json:"goodsid" xorm:"'GOODSID'"` // 商品ID
  748. Userid int64 `json:"userid" xorm:"'USERID'"` // 用户ID
  749. Qty float64 `json:"qty" xorm:"'QTY'"` // 提货数量
  750. Reqtime time.Time `json:"reqtime" xorm:"'REQTIME'"` // 更新时间
  751. Recivername string `json:"recivername" xorm:"'RECIVERNAME'"` // 提货人姓名
  752. Cardtypeid int32 `json:"cardtypeid" xorm:"'CARDTYPEID'"` // 提货人证件类型
  753. Cardnum string `json:"cardnum" xorm:"'CARDNUM'"` // 提货人证件号码
  754. Phonenum string `json:"phonenum" xorm:"'PHONENUM'"` // 提货人联系方式
  755. Takemode int32 `json:"takemode" xorm:"'TAKEMODE'"` // 提货方式 - 2:自提 3:配送
  756. Address string `json:"address" xorm:"'ADDRESS'"` // 提货人详细地址
  757. Takeremark string `json:"takeremark" xorm:"'TAKEREMARK'"` // 提货备注
  758. Takeorderstatus int32 `json:"takeorderstatus" xorm:"'TAKEORDERSTATUS'"` // 提货状态 - 1:待发货 2:已发货 3:已收货
  759. Auditer int32 `json:"auditer" xorm:"'AUDITER'"` // 审核人
  760. Audittime time.Time `json:"audittime" xorm:"'AUDITTIME'"` // 审核时间
  761. Checkremark string `json:"checkremark" xorm:"'CHECKREMARK'"` // 审核备注
  762. Tradedate string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd)
  763. Marketid int32 `json:"marketid" xorm:"'MARKETID'"` // 市场ID
  764. Handlestatus int32 `json:"handlestatus" xorm:"'HANDLESTATUS'"` // 处理状态
  765. Amount float64 `json:"amount" xorm:"'AMOUNT'"` // 提货金额
  766. Averageprice float64 `json:"averageprice" xorm:"'AVERAGEPRICE'"` // 均价
  767. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'" binding:"required"` // 商品代码(内部)
  768. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'" binding:"required"` // 商品名称
  769. Decimalplace int64 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 报价小数位
  770. Agreeunit float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约单位
  771. Picurls string `json:"picurls" xorm:"'PICURLS'"` // 介绍图片[多张用逗号分隔]
  772. Currencysign string `json:"currencysign" xorm:"'CURRENCYSIGN'"` // 货币符号
  773. Vendorname string `json:"vendorname" xorm:"'VENDORNAME'"` // 供应商名称
  774. }
  775. // GetHsbyMyPackages 获取我的包裹信息
  776. // 输入 accountIDs string 资金账户列表
  777. // 输入 takeOrderStatus int 提货状态 - 1:待发货 2:已发货 3:已收货
  778. // 输出 []HsbyMyPackage 我的包裹信息
  779. // 输出 error error
  780. func GetHsbyMyPackages(accountIDs string, takeOrderStatus int) ([]HsbyMyPackage, error) {
  781. engine := db.GetEngine()
  782. myPackages := make([]HsbyMyPackage, 0)
  783. session := engine.Table("TRADE_GOODSPICKUP").
  784. Select(`to_char(TRADE_GOODSPICKUP.TAKEORDERID) TAKEORDERID, TRADE_GOODSPICKUP.*,
  785. (TRADE_GOODSPICKUP.AMOUNT/TRADE_GOODSPICKUP.QTY/GOODS.AGREEUNIT) AVERAGEPRICE,
  786. GOODS.GOODSCODE, GOODS.GOODSNAME, GOODS.DECIMALPLACE, GOODS.AGREEUNIT,
  787. HSBY_GOODSEX.PICURLS,
  788. ENUMDICITEM.PARAM2 CURRENCYSIGN,
  789. HSBY_SUPPLIERINFO.VENDORNAME`).
  790. Join("LEFT", "GOODS", "GOODS.GOODSID = TRADE_GOODSPICKUP.GOODSID").
  791. Join("LEFT", "HSBY_GOODSEX", "HSBY_GOODSEX.GOODSID = GOODS.GOODSID").
  792. Join("LEFT", "ENUMDICITEM", "GOODS.CURRENCYID = ENUMDICITEM.ENUMITEMNAME and ENUMDICITEM.ENUMDICCODE = 'currency'").
  793. Join("LEFT", "HSBY_SUPPLIERINFO", "HSBY_SUPPLIERINFO.VENDORID = HSBY_GOODSEX.VENDORID").
  794. Where(fmt.Sprintf("TRADE_GOODSPICKUP.ACCOUNTID in (%s)", accountIDs))
  795. if takeOrderStatus > 0 {
  796. session = session.And("TAKEORDERSTATUS = ?", takeOrderStatus)
  797. }
  798. if err := session.Find(&myPackages); err != nil {
  799. return nil, err
  800. }
  801. // FIXME:- 目前暂时在服务端对手机号码进行解密
  802. key, _ := hex.DecodeString(utils.AESSecretKey)
  803. for i := range myPackages {
  804. myPackage := &myPackages[i]
  805. // 解密手机号码
  806. if phonenum, err := hex.DecodeString(myPackage.Phonenum); err == nil { // hex -> []byte
  807. if mobile, err := utils.AESDecrypt(phonenum, key); err == nil {
  808. myPackage.Phonenum = string(mobile)
  809. }
  810. }
  811. }
  812. return myPackages, nil
  813. }
  814. // SetHsbyMyPackagesStatus 设置我的包裹提货状态
  815. func SetHsbyMyPackagesStatus(takeOrderID string, accountID, status int) error {
  816. engine := db.GetEngine()
  817. sql := fmt.Sprintf("update TRADE_GOODSPICKUP set TAKEORDERSTATUS = %d where TAKEORDERID = %s and ACCOUNTID = %d", status, takeOrderID, accountID)
  818. _, err := engine.Exec(sql)
  819. return err
  820. }
  821. // GetHsbyProvincesAndCities 获取省市信息数组
  822. // 参数 provinceID int 省ID,选填
  823. // 返回 []Division 枚举信息数组
  824. // 返回 error error
  825. func GetHsbyProvincesAndCities(provinceID int) ([]Division, error) {
  826. engine := db.GetEngine()
  827. divisions := make([]Division, 0)
  828. session := engine.Where("DIVISIONLEVEL = 'province' or DIVISIONLEVEL = 'city'").
  829. And("AUTOID in ((select DESCPROVINCEID AUTOID from HSBY_GOODSEX) union all (select DESCCITYID AUTOID from HSBY_GOODSEX))")
  830. if provinceID > 0 {
  831. session = session.And("AUTOID = ?", provinceID)
  832. }
  833. if err := session.Find(&divisions); err != nil {
  834. return nil, err
  835. }
  836. return divisions, nil
  837. }
  838. // HsbyBuyMyTradeDetail "我的订单 - 已完成"单据信息(一二级市场成交单,包括历史数据)
  839. type HsbyBuyMyTradeDetail struct {
  840. Orderid string `json:"orderid" xorm:"'ORDERID'" binding:"required"` // 单号(成交单号)
  841. Marketid int32 `json:"marketid" xorm:"'MARKETID'" binding:"required"` // 市场ID
  842. Goodsid int32 `json:"goodsid" xorm:"'GOODSID'" binding:"required"` // 商品ID
  843. Accountid int64 `json:"accountid" xorm:"'ACCOUNTID'" binding:"required"` // 账户ID[报价币种]
  844. Buyorsell int32 `json:"buyorsell" xorm:"'BUYORSELL'" binding:"required"` // 买卖 - 0:买 1:卖
  845. Price float64 `json:"price" xorm:"'PRICE'"` // 价格
  846. Qty int64 `json:"qty" xorm:"'QTY'" binding:"required"` // 数量
  847. Time time.Time `json:"time" xorm:"'TIME'" binding:"required"` // 时间
  848. Amount float64 `json:"amount" xorm:"'AMOUNT'"` // 金额 = 价格 * 数量 * 合约单位
  849. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'" binding:"required"` // 商品代码(内部)
  850. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'" binding:"required"` // 商品名称
  851. Decimalplace int64 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 报价小数位
  852. Agreeunit float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约单位
  853. Picurls string `json:"picurls" xorm:"'PICURLS'"` // 介绍图片[多张用逗号分隔]
  854. Trademode uint32 `json:"trademode" xorm:"'TRADEMODE'" binding:"required"` // 交易模式 - 10:做市 13:竞价 15:通道交易 16:挂牌点选 17:仓单贸易 18:期权 19:竞拍-降价式 20:竞拍-竞价式 21:竞拍-大宗式 22:受托竞价
  855. Currencysign string `json:"currencysign" xorm:"'CURRENCYSIGN'"` // 货币符号
  856. Vendorname string `json:"vendorname" xorm:"'VENDORNAME'"` // 供应商名称
  857. }
  858. // GetHsbyBuyMyTradeDetails 获取"我的订单 - 已完成"单据信息(一二级市场成交单,包括历史数据)
  859. // 输入 accountIDs string 资金账户列表
  860. // 输出 []HsbyBuyMyTradeDetail "我的订单 - 已完成"单据信息
  861. // 输出 error error
  862. func GetHsbyBuyMyTradeDetails(accountIDs string) ([]HsbyBuyMyTradeDetail, error) {
  863. // 获取市场信息
  864. markets, err := GetMarkets()
  865. if err != nil {
  866. return nil, err
  867. }
  868. engine := db.GetEngine()
  869. marketIDs := "" // 我的订单包括一二级市场卖方向成交单(包括历史成交单)
  870. // 默认取 TradeMode = 16
  871. for _, v := range markets {
  872. if v.Trademode == 16 || v.Trademode == 71 {
  873. if len(marketIDs) == 0 {
  874. marketIDs = strconv.Itoa(int(v.Marketid))
  875. } else {
  876. marketIDs += "," + strconv.Itoa(int(v.Marketid))
  877. }
  878. }
  879. }
  880. orders := make([]HsbyBuyMyTradeDetail, 0)
  881. // 当前成交单
  882. curOrders := make([]HsbyBuyMyTradeDetail, 0)
  883. if err := engine.Table("TRADE_TRADEDETAIL").
  884. Select(`to_char(TRADE_TRADEDETAIL.TRADEID) ORDERID,
  885. TRADE_TRADEDETAIL.MARKETID, TRADE_TRADEDETAIL.GOODSID, TRADE_TRADEDETAIL.ACCOUNTID, TRADE_TRADEDETAIL.BUYORSELL,
  886. TRADE_TRADEDETAIL.TRADEPRICE PRICE, TRADE_TRADEDETAIL.TRADEQTY QTY, TRADE_TRADEDETAIL.TRADETIME TIME, TRADE_TRADEDETAIL.TRADEAMOUNT AMOUNT,
  887. GOODS.GOODSCODE, GOODS.GOODSNAME, GOODS.DECIMALPLACE, GOODS.AGREEUNIT,
  888. HSBY_GOODSEX.PICURLS,
  889. MARKET.TRADEMODE,
  890. ENUMDICITEM.PARAM2 CURRENCYSIGN,
  891. HSBY_SUPPLIERINFO.VENDORNAME`).
  892. Join("LEFT", "GOODS", "GOODS.GOODSID = TRADE_TRADEDETAIL.GOODSID").
  893. Join("LEFT", "HSBY_GOODSEX", "HSBY_GOODSEX.GOODSID = GOODS.GOODSID").
  894. Join("LEFT", "ENUMDICITEM", "GOODS.CURRENCYID = ENUMDICITEM.ENUMITEMNAME and ENUMDICITEM.ENUMDICCODE = 'currency'").
  895. Join("LEFT", "MARKET", "MARKET.MARKETID = TRADE_TRADEDETAIL.MARKETID").
  896. Join("LEFT", "HSBY_SUPPLIERINFO", "HSBY_SUPPLIERINFO.VENDORID = HSBY_GOODSEX.VENDORID").
  897. Where(fmt.Sprintf(`TRADE_TRADEDETAIL.BUYORSELL = 0
  898. and TRADE_TRADEDETAIL.ACCOUNTID in (%s)`, accountIDs)).
  899. And(fmt.Sprintf("TRADE_TRADEDETAIL.MARKETID in (%s)", marketIDs)).Find(&curOrders); err != nil {
  900. return nil, err
  901. }
  902. if len(curOrders) > 0 {
  903. orders = append(orders, curOrders...)
  904. }
  905. // 历史成交单
  906. hisOrders := make([]HsbyBuyMyTradeDetail, 0)
  907. if err := engine.Table("HIS_TRADE_TRADEDETAIL").
  908. Select(`to_char(HIS_TRADE_TRADEDETAIL.TRADEID) ORDERID,
  909. HIS_TRADE_TRADEDETAIL.MARKETID, HIS_TRADE_TRADEDETAIL.GOODSID, HIS_TRADE_TRADEDETAIL.ACCOUNTID, HIS_TRADE_TRADEDETAIL.BUYORSELL,
  910. HIS_TRADE_TRADEDETAIL.TRADEPRICE PRICE, HIS_TRADE_TRADEDETAIL.TRADEQTY QTY, HIS_TRADE_TRADEDETAIL.TRADETIME TIME, HIS_TRADE_TRADEDETAIL.TRADEAMOUNT AMOUNT,
  911. GOODS.GOODSCODE, GOODS.GOODSNAME, GOODS.DECIMALPLACE, GOODS.AGREEUNIT,
  912. HSBY_GOODSEX.PICURLS,
  913. MARKET.TRADEMODE,
  914. ENUMDICITEM.PARAM2 CURRENCYSIGN,
  915. HSBY_SUPPLIERINFO.VENDORNAME`).
  916. Join("LEFT", "GOODS", "GOODS.GOODSID = HIS_TRADE_TRADEDETAIL.GOODSID").
  917. Join("LEFT", "HSBY_GOODSEX", "HSBY_GOODSEX.GOODSID = GOODS.GOODSID").
  918. Join("LEFT", "ENUMDICITEM", "GOODS.CURRENCYID = ENUMDICITEM.ENUMITEMNAME and ENUMDICITEM.ENUMDICCODE = 'currency'").
  919. Join("LEFT", "MARKET", "MARKET.MARKETID = HIS_TRADE_TRADEDETAIL.MARKETID").
  920. Join("LEFT", "HSBY_SUPPLIERINFO", "HSBY_SUPPLIERINFO.VENDORID = HSBY_GOODSEX.VENDORID").
  921. Where(fmt.Sprintf(`HIS_TRADE_TRADEDETAIL.BUYORSELL = 0 and HIS_TRADE_TRADEDETAIL.ISVALIDDATA = 1
  922. and HIS_TRADE_TRADEDETAIL.ACCOUNTID in (%s)`, accountIDs)).
  923. And(fmt.Sprintf("HIS_TRADE_TRADEDETAIL.MARKETID in (%s)", marketIDs)).Find(&hisOrders); err != nil {
  924. return nil, err
  925. }
  926. if len(hisOrders) > 0 {
  927. orders = append(orders, hisOrders...)
  928. }
  929. return orders, nil
  930. }