szdz.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. package szdz
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "mtp2_if/db"
  6. "mtp2_if/global/app"
  7. "mtp2_if/global/e"
  8. "mtp2_if/logger"
  9. "mtp2_if/models"
  10. "mtp2_if/utils"
  11. "net/http"
  12. "sort"
  13. "strconv"
  14. "github.com/gin-gonic/gin"
  15. )
  16. // QueryRecieptOrderReq 点选挂牌委托单据查询参数
  17. type QueryRecieptOrderReq struct {
  18. app.PageInfo
  19. GoodsID int `form:"goodsID" binding:"required"` // 商品ID,必填
  20. BuyOrSell int `form:"buyorsell"` // 方向
  21. AccountName string `form:"accountName"` // 所属账户名称
  22. MarketID int `form:"marketID"` // 市场ID
  23. }
  24. // QueryRecieptOrderRsp 点选挂牌委托单据查询返回模型
  25. type QueryRecieptOrderRsp struct {
  26. Goodsid int64 `json:"goodsid" xorm:"'GOODSID'"` // 商品ID
  27. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'"` // 商品代码
  28. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'"` // 商品名称
  29. Buyorsell uint32 `json:"buyorsell" xorm:"'BUYORSELL'"` // 方向 - 0:买 1:卖
  30. Orderid string `json:"orderid" xorm:"'ORDERID'"` // 委托单号
  31. Tradedate string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd)
  32. Orderprice float64 `json:"orderprice" xorm:"'ORDERPRICE'"` // 委托价格
  33. EnableQty int64 `json:"enableqty" xorm:"ENABLEQTY"` // 可摘数量
  34. AccountName string `json:"accountName" xorm:"ACCOUNTNAME"` // 所属账号名称(已脱敏)
  35. AccountID int64 `json:"accountid" xorm:"ACCOUNTID"` // 资金账号
  36. }
  37. // QueryRecieptOrder 点选挂牌委托单据查询(摘牌大厅)
  38. // @Summary 点选挂牌委托单据查询(摘牌大厅)
  39. // @Description 说明:pagesize参数赋值不为0时表示需要分页;page参数从0开始计算
  40. // @Produce json
  41. // @Security ApiKeyAuth
  42. // @Param page query int false "页码"
  43. // @Param pagesize query int false "每页条数"
  44. // @Param goodsID query int true "商品ID"
  45. // @Param accountName query string false "所属账户名称"
  46. // @Param marketID query int false "市场ID"
  47. // @Param buyorsell query int true "方向 - 0:买 1:卖"
  48. // @Success 200 {object} QueryRecieptOrderRsp
  49. // @Failure 500 {object} app.Response
  50. // @Router /SZDZ/QueryRecieptOrder [get]
  51. // @Tags 定制【尚志大宗】
  52. // 参考通用查询:Client_SZDZ3_SearchRecieptOrder
  53. func QueryRecieptOrder(c *gin.Context) {
  54. appG := app.Gin{C: c}
  55. // 获取请求参数
  56. var req QueryRecieptOrderReq
  57. if err := appG.C.ShouldBindQuery(&req); err != nil {
  58. logger.GetLogger().Errorf("QueryRecieptOrder failed: %s", err.Error())
  59. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  60. return
  61. }
  62. datas := make([]QueryRecieptOrderRsp, 0)
  63. engine := db.GetEngine()
  64. // 投资者挂牌委托单
  65. s := engine.Table("TRADE_ORDERDETAIL").
  66. Join("INNER", "GOODS", "TRADE_ORDERDETAIL.GOODSID = GOODS.GOODSID").
  67. Join("LEFT", "TAACCOUNT", "TAACCOUNT.ACCOUNTID = TRADE_ORDERDETAIL.ACCOUNTID").
  68. Join("LEFT", "USERACCOUNT", "USERACCOUNT.USERID = TAACCOUNT.RELATEDUSERID").
  69. Select(`GOODS.GOODSID, GOODS.GOODSCODE, GOODS.GOODSNAME,
  70. TRADE_ORDERDETAIL.BUYORSELL, to_char(TRADE_ORDERDETAIL.ORDERID) as ORDERID, TRADE_ORDERDETAIL.TRADEDATE, TRADE_ORDERDETAIL.ORDERPRICE,
  71. TRADE_ORDERDETAIL.ORDERQTY - TRADE_ORDERDETAIL.TRADEQTY - TRADE_ORDERDETAIL.CANCELQTY as ENABLEQTY,
  72. substr(USERACCOUNT.ACCOUNTNAME,0,1)||'****' as ACCOUNTNAME`).
  73. Where("TRADE_ORDERDETAIL.ORDERSTATUS in (3,7,12) and GOODS.GOODSID = ?", req.GoodsID)
  74. if len(req.AccountName) > 0 {
  75. s = s.And("USERACCOUNT.ACCOUNTNAME = ?", req.AccountName)
  76. }
  77. if req.MarketID > 0 {
  78. s = s.And("TRADE_ORDERDETAIL.MARKETID = ?", req.MarketID)
  79. }
  80. if req.BuyOrSell > -1 {
  81. s = s.And("TRADE_ORDERDETAIL.BUYORSELL = ?", req.BuyOrSell)
  82. }
  83. // 判断是否要分页
  84. // 这样分页会报错
  85. // if req.Size > 0 {
  86. // s = s.Limit(req.Size, req.Page*req.Size)
  87. // }
  88. d1 := make([]QueryRecieptOrderRsp, 0)
  89. if err := s.Find(&d1); err != nil {
  90. // 查询失败
  91. logger.GetLogger().Errorf("QueryRecieptOrder failed: %s", err.Error())
  92. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  93. return
  94. }
  95. datas = append(datas, d1...)
  96. // 报价商挂牌委托
  97. s = engine.Table("TRADE_QUOTEDETAILNEW").
  98. Join("INNER", "GOODS", "TRADE_QUOTEDETAILNEW.GOODSID = GOODS.GOODSID").
  99. Join("INNER", "QUOTER", "QUOTER.QUOTERID = TRADE_QUOTEDETAILNEW.QUOTERID").
  100. Join("INNER", "AREAROLE", "AREAROLE.USERID = QUOTER.DEFAULTMAKERID and AREAROLE.ROLETYPE = 8").
  101. Select(`GOODS.GOODSID, GOODS.GOODSCODE, GOODS.GOODSNAME,
  102. TRADE_QUOTEDETAILNEW.BUYORSELL, to_char(TRADE_QUOTEDETAILNEW.ORDERID) as ORDERID, TRADE_QUOTEDETAILNEW.TRADEDATE, TRADE_QUOTEDETAILNEW.PRICE as ORDERPRICE,
  103. TRADE_QUOTEDETAILNEW.CURQTY as ENABLEQTY,
  104. substr(QUOTER.QUOTERNAME,0,1)||'****' as ACCOUNTNAME`).
  105. Where("(TRADE_QUOTEDETAILNEW.CURQTY > 0 or TRADE_QUOTEDETAILNEW.PRICE > 0) and TRADE_QUOTEDETAILNEW.ISVALID != 1 and TRADE_QUOTEDETAILNEW.FREEZESTATUS != 3")
  106. d2 := make([]QueryRecieptOrderRsp, 0)
  107. if err := s.Find(&d2); err != nil {
  108. // 查询失败
  109. logger.GetLogger().Errorf("QueryRecieptOrder failed: %s", err.Error())
  110. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  111. return
  112. }
  113. datas = append(datas, d2...)
  114. // 过滤相关账号下非买一卖一的单据(如买一卖一有多张单则都要显示)
  115. if req.BuyOrSell == 0 {
  116. // 买,先按价格倒序排列
  117. sort.Slice(datas, func(i int, j int) bool {
  118. return datas[i].Orderprice > datas[j].Orderprice
  119. })
  120. } else {
  121. // 卖,先按价格顺序排列
  122. sort.Slice(datas, func(i int, j int) bool {
  123. return datas[i].Orderprice < datas[j].Orderprice
  124. })
  125. }
  126. tmpDatas := make([]QueryRecieptOrderRsp, 0)
  127. for _, v := range datas {
  128. needAdd := true
  129. for _, t := range tmpDatas {
  130. if req.BuyOrSell == 0 {
  131. // 买
  132. if t.AccountID == v.AccountID && t.Buyorsell == v.Buyorsell && t.Orderprice > v.Orderprice {
  133. needAdd = false
  134. }
  135. } else {
  136. // 卖
  137. if t.AccountID == v.AccountID && t.Buyorsell == v.Buyorsell && t.Orderprice < v.Orderprice {
  138. needAdd = false
  139. }
  140. }
  141. }
  142. if needAdd {
  143. tmpDatas = append(tmpDatas, v)
  144. }
  145. }
  146. datas = tmpDatas
  147. total := len(datas)
  148. // FIXME: - 排序 & 分页
  149. // 排序
  150. sort.Slice(datas, func(i int, j int) bool {
  151. return datas[i].Tradedate > datas[j].Tradedate
  152. })
  153. // 分页
  154. if req.PageSize > 0 {
  155. // 开始上标
  156. start := req.Page * req.PageSize
  157. // 结束下标
  158. // a := []int{1,2,3,4,5}
  159. // a[2:4] -> [3 4]
  160. end := start + req.PageSize
  161. if start <= len(datas) {
  162. // 判断结束下标是否越界
  163. if end > len(datas) {
  164. end = len(datas)
  165. }
  166. datas = datas[start:end]
  167. } else {
  168. datas = make([]QueryRecieptOrderRsp, 0)
  169. }
  170. }
  171. // 查询成功返回
  172. logger.GetLogger().Debugln("QueryRecieptOrder successed: %v", datas)
  173. if req.PageSize > 0 {
  174. // 分页
  175. appG.ResponseByPage(http.StatusOK, e.SUCCESS, datas, app.PageInfo{Page: req.Page, PageSize: req.PageSize, Total: total})
  176. } else {
  177. // 不分页
  178. appG.Response(http.StatusOK, e.SUCCESS, datas)
  179. }
  180. }
  181. // QueryGoodsPickupReq 商品提货单查询请求参数
  182. type QueryGoodsPickupReq struct {
  183. AccountID string `form:"accountID" binding:"required"`
  184. TakeOrderStatus int `form:"takeOrderStatus"`
  185. }
  186. // QueryGoodsPickupRsp 商品提货单查询返回模型
  187. type QueryGoodsPickupRsp struct {
  188. models.Szdz3goodspickup `xorm:"extends"`
  189. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'"` // 商品代码
  190. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'"` // 商品名称
  191. }
  192. // QueryGoodsPickup 商品提货单查询
  193. // @Summary 商品提货单查询
  194. // @Produce json
  195. // @Security ApiKeyAuth
  196. // @Param accountID query string true "资金账户 - 格式:1,2,3"
  197. // @Param takeOrderStatus query int false "提货状态 - 1:待发货 2:已发货 3:已收货"
  198. // @Success 200 {object} QueryGoodsPickupRsp
  199. // @Failure 500 {object} app.Response
  200. // @Router /SZDZ/QueryGoodsPickup [get]
  201. // @Tags 定制【尚志大宗】
  202. func QueryGoodsPickup(c *gin.Context) {
  203. appG := app.Gin{C: c}
  204. // 获取请求参数
  205. var req QueryGoodsPickupReq
  206. if err := appG.C.ShouldBindQuery(&req); err != nil {
  207. logger.GetLogger().Errorf("QueryGoodsPickup failed: %s", err.Error())
  208. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  209. return
  210. }
  211. datas := make([]QueryGoodsPickupRsp, 0)
  212. engine := db.GetEngine()
  213. s := engine.Join("INNER", "GOODS", "GOODS.GOODSID = SZDZ3_GOODSPICKUP.GOODSID").
  214. Select("to_char(SZDZ3_GOODSPICKUP.TAKEORDERID) as TAKEORDERID, SZDZ3_GOODSPICKUP.*, GOODS.GOODSCODE, GOODS.GOODSNAME").
  215. Where(fmt.Sprintf(`SZDZ3_GOODSPICKUP.ACCOUNTID in (%s)`, req.AccountID)).
  216. Desc("SZDZ3_GOODSPICKUP.TAKEORDERID")
  217. if req.TakeOrderStatus > 0 {
  218. s = s.And("SZDZ3_GOODSPICKUP.TAKEORDERSTATUS = ?", req.TakeOrderStatus)
  219. }
  220. if err := s.Find(&datas); err != nil {
  221. // 查询失败
  222. logger.GetLogger().Errorf("QueryGoodsPickup failed: %s", err.Error())
  223. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  224. return
  225. }
  226. // 查询成功返回
  227. logger.GetLogger().Debugln("QueryGoodsPickup successed: %v", datas)
  228. appG.Response(http.StatusOK, e.SUCCESS, datas)
  229. }
  230. // QueryConvertLogReq 交易系统转换流水查询请求参数
  231. type QueryConvertLogReq struct {
  232. AccountID string `form:"accountID" binding:"required"`
  233. StartDate string `form:"startDate"`
  234. EndDate string `form:"endDate"`
  235. }
  236. // QueryConvertLogRsp 交易系统转换流水查询返回模型
  237. type QueryConvertLogRsp struct {
  238. models.Szdz3convertlog `xorm:"extends"`
  239. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'"` // 商品代码
  240. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'"` // 商品名称
  241. QTY string `json:"qty"` // 数量
  242. }
  243. // QueryConvertLog 交易系统转换流水查询
  244. // @Summary 交易系统转换流水查询
  245. // @Produce json
  246. // @Security ApiKeyAuth
  247. // @Param accountID query string true "资金账户 - 格式:1,2,3"
  248. // @Param startDate query string false "开始时间 - 闭区间,格式:yyyy-MM-dd HH:mm:ss"
  249. // @Param endDate query string false "结束时间 - 闭区间,格式:yyyy-MM-dd HH:mm:ss"
  250. // @Success 200 {object} QueryConvertLogRsp
  251. // @Failure 500 {object} app.Response
  252. // @Router /SZDZ/QueryConvertLog [get]
  253. // @Tags 定制【尚志大宗】
  254. func QueryConvertLog(c *gin.Context) {
  255. appG := app.Gin{C: c}
  256. // 获取请求参数
  257. var req QueryConvertLogReq
  258. if err := appG.C.ShouldBindQuery(&req); err != nil {
  259. logger.GetLogger().Errorf("QueryConvertLog failed: %s", err.Error())
  260. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  261. return
  262. }
  263. datas := make([]QueryConvertLogRsp, 0)
  264. engine := db.GetEngine()
  265. s := engine.Join("INNER", "GOODS", "GOODS.GOODSID = SZDZ3_CONVERTLOG.INNERGOODSID").
  266. Select("SZDZ3_CONVERTLOG.*, GOODS.GOODSCODE, GOODS.GOODSNAME").
  267. Where(fmt.Sprintf(`SZDZ3_CONVERTLOG.HandleStatus = 1 and SZDZ3_CONVERTLOG.ACCOUNTID in (%s)`, req.AccountID)).
  268. Desc("SZDZ3_CONVERTLOG.LOGID")
  269. if len(req.StartDate) > 0 {
  270. s = s.And(fmt.Sprintf(`SZDZ3_CONVERTLOG.CREATETIME >= to_date('%s','yyyy-MM-dd hh24:mi:ss')`, req.StartDate))
  271. }
  272. if len(req.EndDate) > 0 {
  273. s = s.And(fmt.Sprintf(`SZDZ3_CONVERTLOG.CREATETIME <= to_date('%s','yyyy-MM-dd hh24:mi:ss')`, req.EndDate))
  274. }
  275. if err := s.Find(&datas); err != nil {
  276. // 查询失败
  277. logger.GetLogger().Errorf("QueryConvertLog failed: %s", err.Error())
  278. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  279. return
  280. }
  281. // 计算数量
  282. for i, v := range datas {
  283. queryConvertLog := &datas[i]
  284. // 方向
  285. direction := "-"
  286. // 数量
  287. qty := v.Outvalue
  288. if v.Converttype == 1 || v.Converttype == 2 || v.Converttype == 5 {
  289. direction = "+"
  290. qty = v.Invalue
  291. }
  292. queryConvertLog.QTY = fmt.Sprintf("%s%v", direction, qty)
  293. }
  294. // 查询成功返回
  295. logger.GetLogger().Debugln("QueryConvertLog successed: %v", datas)
  296. appG.Response(http.StatusOK, e.SUCCESS, datas)
  297. }
  298. // SearchWhiteReq 搜索白名单请求参数
  299. type SearchWhiteReq struct {
  300. UserID int `form:"userID" binding:"required"`
  301. }
  302. // SearchWhite 搜索白名单
  303. // @Summary 搜索白名单
  304. // @Produce json
  305. // @Security ApiKeyAuth
  306. // @Param userID query int true "用户ID"
  307. // @Success 200 {object} models.Szdz3searchwhitelist
  308. // @Failure 500 {object} app.Response
  309. // @Router /SZDZ/SearchWhite [get]
  310. // @Tags 定制【尚志大宗】
  311. func SearchWhite(c *gin.Context) {
  312. appG := app.Gin{C: c}
  313. // 获取请求参数
  314. var req SearchWhiteReq
  315. if err := appG.C.ShouldBindQuery(&req); err != nil {
  316. logger.GetLogger().Errorf("SearchWhite failed: %s", err.Error())
  317. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  318. return
  319. }
  320. datas := make([]models.Szdz3searchwhitelist, 0)
  321. engine := db.GetEngine()
  322. if err := engine.Where("USERID = ?", req.UserID).Find(&datas); err != nil {
  323. // 查询失败
  324. logger.GetLogger().Errorf("SearchWhite failed: %s", err.Error())
  325. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  326. return
  327. }
  328. // 查询成功返回
  329. logger.GetLogger().Debugln("SearchWhite successed: %v", datas)
  330. appG.Response(http.StatusOK, e.SUCCESS, datas)
  331. }
  332. // QueryConvertConfigReq 查询交易系统转换设置请求参数
  333. type QueryConvertConfigReq struct {
  334. ConvertType int `form:"convertType"` // 转换类型 - 1:金点赞转交易 2:金点拍转交易 3:交易转金点赞(不设置) 4:交易转金点拍(不设置) 5:花生米转交易 6:交易转花生米(不设置)
  335. OuterGoodsCode string `form:"outerGoodsCode"` // 外部商品代码[JD\PD]
  336. InnerGoodsIDs string `form:"innerGoodsIDs"` // 内部商品ID列表[交易]
  337. }
  338. // QueryConvertConfig 查询交易系统转换设置
  339. // @Summary 查询交易系统转换设置
  340. // @Produce json
  341. // @Security ApiKeyAuth
  342. // @Param convertType query int false "转换类型 - 1:金点赞转交易 2:金点拍转交易 3:交易转金点赞(不设置) 4:交易转金点拍(不设置) 5:花生米转交易 6:交易转花生米(不设置)"
  343. // @Param outerGoodsCode query string false "外部商品代码[JD\PD]"
  344. // @Param innerGoodsIDs query string false "内部商品ID列表[交易],格式:1,2,3"
  345. // @Success 200 {object} models.Szdz3convertconfig
  346. // @Failure 500 {object} app.Response
  347. // @Router /SZDZ/QueryConvertConfig [get]
  348. // @Tags 定制【尚志大宗】
  349. func QueryConvertConfig(c *gin.Context) {
  350. appG := app.Gin{C: c}
  351. // 获取请求参数
  352. var req QueryConvertConfigReq
  353. if err := appG.C.ShouldBindQuery(&req); err != nil {
  354. logger.GetLogger().Errorf("QueryConvertConfigReq failed: %s", err.Error())
  355. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  356. return
  357. }
  358. datas := make([]models.Szdz3convertconfig, 0)
  359. engine := db.GetEngine()
  360. s := engine.Table("SZDZ3_CONVERTCONFIG")
  361. if req.ConvertType > 0 {
  362. s = s.And("CONVERTTYPE = ?", req.ConvertType)
  363. }
  364. if len(req.OuterGoodsCode) > 0 {
  365. s = s.And("OUTERGOODSCODE = ?", req.OuterGoodsCode)
  366. }
  367. if len(req.InnerGoodsIDs) > 0 {
  368. s = s.And(fmt.Sprintf("INNERGOODSID in (%s)", req.InnerGoodsIDs))
  369. }
  370. if err := s.Find(&datas); err != nil {
  371. // 查询失败
  372. logger.GetLogger().Errorf("QueryConvertConfigReq failed: %s", err.Error())
  373. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  374. return
  375. }
  376. // 查询成功返回
  377. logger.GetLogger().Debugln("QueryConvertConfigReq successed: %v", datas)
  378. appG.Response(http.StatusOK, e.SUCCESS, datas)
  379. }
  380. // QuerySZDZTradePositionReq 持仓汇总查询请求参数(尚志大宗)
  381. type QuerySZDZTradePositionReq struct {
  382. AccountID int `form:"accountID" binding:"required"`
  383. }
  384. // QuerySZDZTradePositionRsp 持仓汇总查询返回模型(尚志大宗)
  385. type QuerySZDZTradePositionRsp struct {
  386. Accountid int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 账号Id
  387. Goodsid int64 `json:"goodsid" xorm:"'GOODSID'"` // 商品Id
  388. Positionqty int64 `json:"positionqty" xorm:"'POSITIONQTY'"` // 期初持仓数量
  389. Holderamount float64 `json:"holderamount" xorm:"'HOLDERAMOUNT'"` // 期初持仓总金额
  390. Curpositionqty int64 `json:"curpositionqty" xorm:"'CURPOSITIONQTY'"` // 当前持仓总数量
  391. Curholderamount float64 `json:"curholderamount" xorm:"'CURHOLDERAMOUNT'"` // 当前持仓总金额
  392. Frozenqty int64 `json:"frozenqty" xorm:"'FROZENQTY'"` // 持仓冻结数量
  393. Otherfrozenqty int64 `json:"otherfrozenqty" xorm:"'OTHERFROZENQTY'"` // 持仓其他冻结数量(交割冻结)
  394. Openreqqty int64 `json:"openreqqty" xorm:"'OPENREQQTY'"` // 开仓申请数量
  395. Opentotalqty int64 `json:"opentotalqty" xorm:"'OPENTOTALQTY'"` // 开仓总数量
  396. Closetotalqty int64 `json:"closetotalqty" xorm:"'CLOSETOTALQTY'"` // 平仓总数量
  397. Tnqty int64 `json:"tnqty" xorm:"'TNQTY'"` // T+N冻结总量
  398. Tnusedqty int64 `json:"tnusedqty" xorm:"'TNUSEDQTY'"` // T+N使用量
  399. Usedmargin float64 `json:"usedmargin" xorm:"'USEDMARGIN'"` // 占用保证金
  400. Curtdposition int64 `json:"curtdposition" xorm:"'CURTDPOSITION'"` // 期末今日头寸
  401. Fretdposition int64 `json:"fretdposition" xorm:"'FRETDPOSITION'"` // 冻结今日头寸
  402. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'"` // 商品代码(内部)
  403. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'"` // 商品名称
  404. Currencyid int64 `json:"currencyid" xorm:"'CURRENCYID'"` // 报价货币ID
  405. Goodunitid int64 `json:"goodunitid" xorm:"'GOODUNITID'"` // 报价单位ID
  406. Goodunit string `json:"goodunit" xorm:"'GOODUNIT'"` // 报价单位
  407. Agreeunit float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约单位
  408. Decimalplace int64 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 报价小数位
  409. Marketid int32 `json:"marketid" xorm:"'MARKETID'"` // 市场ID
  410. Trademode int32 `json:"trademode" xorm:"'TRADEMODE'"` // 交易模式
  411. SZDZ3FreezQTY int64 `json:"szdz3freezqty" xorm:"'SZDZ3FREEZQTY'"` // 尚志大宗转换冻结总数量
  412. BuyOrSell int64 `json:"buyorsell" xorm:"'BUYORSELL'" ` // 方向 - 0:买 1:卖
  413. EnableQTY int64 `json:"enableqty" xorm:"'ENABLEQTY'"` // 可用量
  414. AveragePrice float64 `json:"averageprice" xorm:"AVERAGEPRICE"` // 持仓均价
  415. }
  416. // QuerySZDZTradePosition 持仓汇总查询(尚志大宗)
  417. // @Summary 持仓汇总查询(尚志大宗)
  418. // @Produce json
  419. // @Security ApiKeyAuth
  420. // @Param accountID query int true "资金账户"
  421. // @Success 200 {object} QuerySZDZTradePositionRsp
  422. // @Failure 500 {object} app.Response
  423. // @Router /SZDZ/QuerySZDZTradePosition [get]
  424. // @Tags 定制【尚志大宗】
  425. func QuerySZDZTradePosition(c *gin.Context) {
  426. appG := app.Gin{C: c}
  427. // 获取请求参数
  428. var req QuerySZDZTradePositionReq
  429. if err := appG.C.ShouldBindQuery(&req); err != nil {
  430. logger.GetLogger().Errorf("QueryTradePosition failed: %s", err.Error())
  431. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  432. return
  433. }
  434. // 查询数据
  435. datas, err := models.GetSZDZBuyTradePosition(req.AccountID)
  436. if err != nil {
  437. // 查询失败
  438. logger.GetLogger().Errorf("QueryConvertConfigReq failed: %s", err.Error())
  439. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  440. return
  441. }
  442. // 构建返回数据
  443. rst := make([]QuerySZDZTradePositionRsp, 0)
  444. for _, v := range datas {
  445. var tradePosition QuerySZDZTradePositionRsp
  446. // 反射数据
  447. // struct -> json
  448. if jsonBytes, err := json.Marshal(v); err == nil {
  449. // json -> struct
  450. json.Unmarshal(jsonBytes, &tradePosition)
  451. tradePosition.Accountid = v.Accountid
  452. tradePosition.Goodsid = v.Goodsid
  453. tradePosition.Positionqty = v.Positionqty
  454. tradePosition.Holderamount = v.Holderamount
  455. tradePosition.Curholderamount = v.Curholderamount
  456. tradePosition.Otherfrozenqty = v.Otherfrozenqty
  457. tradePosition.Openreqqty = v.Openreqqty
  458. tradePosition.Opentotalqty = v.Opentotalqty
  459. tradePosition.Closetotalqty = v.Closetotalqty
  460. tradePosition.Tnqty = v.Tnqty
  461. tradePosition.Tnusedqty = v.Tnusedqty
  462. tradePosition.Usedmargin = v.Usedmargin
  463. tradePosition.Curtdposition = v.Curtdposition
  464. tradePosition.Fretdposition = v.Fretdposition
  465. tradePosition.Goodscode = v.Goodscode
  466. tradePosition.Goodsname = v.Goodsname
  467. tradePosition.Currencyid = v.Currencyid
  468. tradePosition.Goodunitid = v.Goodunitid
  469. tradePosition.Goodunit = v.Goodunit
  470. tradePosition.Agreeunit = v.Agreeunit
  471. tradePosition.Decimalplace = v.Decimalplace
  472. tradePosition.Marketid = v.Marketid
  473. tradePosition.Trademode = v.Trademode
  474. tradePosition.SZDZ3FreezQTY = v.SZDZ3FreezQTY
  475. // 计算相关
  476. tradePosition.Frozenqty = v.Frozenqty + v.SZDZ3FreezQTY // 需要加上
  477. tradePosition.Curpositionqty = v.Curpositionqty + v.SZDZ3FreezQTY
  478. tradePosition.EnableQTY = v.Curpositionqty - v.Frozenqty - v.Otherfrozenqty
  479. averagePrice := tradePosition.Curholderamount / float64(tradePosition.Curpositionqty) / tradePosition.Agreeunit
  480. tradePosition.AveragePrice, _ = strconv.ParseFloat(utils.FormatFloat(averagePrice, int(v.Decimalplace)), 64)
  481. tradePosition.BuyOrSell = 0
  482. rst = append(rst, tradePosition)
  483. }
  484. }
  485. // 查询成功
  486. logger.GetLogger().Debugln("QueryTradePosition successed: %v", rst)
  487. appG.Response(http.StatusOK, e.SUCCESS, rst)
  488. }