cpTrade.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. // Package cptrade 产能预售
  2. package cptrade
  3. import (
  4. "fmt"
  5. "mtp2_if/db"
  6. "mtp2_if/global/app"
  7. "mtp2_if/global/e"
  8. "mtp2_if/logger"
  9. "net/http"
  10. "strings"
  11. "time"
  12. "github.com/gin-gonic/gin"
  13. )
  14. // QueryPresaleApplyReq 产能预售申请表请求参数
  15. type QueryPresaleApplyReq struct {
  16. UserID int `form:"userid" binding:"required"`
  17. ApplyID int `form:"applyid"`
  18. AccountID int `form:"accountid"`
  19. }
  20. // Cptradepresaleapply CPTRADE_PRESALEAPPLY 产能预售申请表
  21. type Cptradepresaleapply struct {
  22. Applyid int64 `json:"applyid" xorm:"'APPLYID'" binding:"required"` // 申请ID(181+Unix秒时间戳(10位)+xxxxxx)
  23. Userid int64 `json:"userid" xorm:"'USERID'"` // 申请人ID
  24. Accountid int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 申请人账户ID
  25. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'"` // 商品代码
  26. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'"` // 商品名称
  27. Relatedgoodsid int64 `json:"relatedgoodsid" xorm:"'RELATEDGOODSID'"` // 关联交易合约ID
  28. Presaleqty int64 `json:"presaleqty" xorm:"'PRESALEQTY'"` // 预售数量
  29. Starttime time.Time `json:"starttime" xorm:"'STARTTIME'"` // 预售开始时间
  30. Endtime time.Time `json:"endtime" xorm:"'ENDTIME'"` // 预售结束时间
  31. Attachmenturl string `json:"attachmenturl" xorm:"'ATTACHMENTURL'"` // 附件地址
  32. Applystatus int64 `json:"applystatus" xorm:"'APPLYSTATUS'"` // 申请状态 - 1:已提交 2:初审通过 3:初审拒绝 4:初审失败 5复审通过 6:复审拒绝 7:复审失败 8:已撤销
  33. Handlestatus int64 `json:"handlestatus" xorm:"'HANDLESTATUS'"` // 处理状态
  34. Applytime time.Time `json:"applytime" xorm:"'APPLYTIME'"` // 申请时间
  35. Marketid int64 `json:"marketid" xorm:"'MARKETID'"` // 预售市场ID
  36. Tradedate string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd)
  37. Relatedgoodscode string `json:"relatedgoodscode" xorm:"'RELATEDGOODSCODE'"` // 关联交易合约代码
  38. Relatedgoodsname string `json:"relatedgoodsname" xorm:"'RELATEDGOODSNAME'"` // 关联交易合约名称
  39. Marketname string `json:"marketname" xorm:"'MARKETNAME'"` // 预售市场名称
  40. Trademode int64 `json:"trademode" xorm:"'TRADEMODE'"` // 交易模式 - 16:挂牌点选 21:大宗竞拍
  41. Goodunit string `json:"goodunit" xorm:"'GOODUNIT'"` // 报价单位
  42. }
  43. // TableName is
  44. func (Cptradepresaleapply) TableName() string {
  45. return "CPTRADE_PRESALEAPPLY"
  46. }
  47. // QueryPreasleApply 查询产能预售申请信息
  48. // @Summary 查询产能预售申请信息
  49. // @Produce json
  50. // @Security ApiKeyAuth
  51. // @Param userid query int true "账户ID"
  52. // @Param applyid query int false "申请ID"
  53. // @Param accountid query int false "资金账户ID"
  54. // @Success 200 {object} Cptradepresaleapply
  55. // @Failure 500 {object} app.Response
  56. // @Router /CPTrade/QueryPreasleApply [get]
  57. // @Tags 产能预售
  58. func QueryPreasleApply(c *gin.Context) {
  59. appG := app.Gin{C: c}
  60. // 获取请求参数
  61. var req QueryPresaleApplyReq
  62. if err := appG.C.ShouldBindQuery(&req); err != nil {
  63. logger.GetLogger().Errorf("QueryPreasleApply failed: %s", err.Error())
  64. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  65. return
  66. }
  67. // 查询数据
  68. engine := db.GetEngine()
  69. datas := make([]Cptradepresaleapply, 0)
  70. // s := engine.Where("userid=?", req.UserID)
  71. // if req.AccountID > 0 {
  72. // s = s.And("accountid=?", req.AccountID)
  73. // }
  74. // if req.ApplyID > 0 {
  75. // s = s.And("applyid=?", req.ApplyID)
  76. // }
  77. sql := fmt.Sprintf(`select
  78. t.ApplyID,
  79. t.UserID,
  80. t.AccountID,
  81. t.GoodsCode,
  82. t.GoodsName,
  83. t.RelatedGoodsID,
  84. t.PresaleQty,
  85. t.StartTime,
  86. t.EndTime,
  87. t.AttachmentUrl,
  88. t.ApplyStatus,
  89. t.HandleStatus,
  90. t.ApplyTime,
  91. t.CreatorID,
  92. t.CreateTime,
  93. t.MarketID,
  94. t.TradeDate,
  95. g.goodscode RelatedGoodsCode,
  96. g.goodsname RelatedGoodsName,
  97. m.MarketName,
  98. m.TradeMode,
  99. e.enumdicname GoodUnit
  100. from CPTrade_PresaleApply t
  101. left join goods g on t.RelatedGoodsID = g.goodsid
  102. left join market m on t.MarketID = m.marketid
  103. left join enumdicitem e on g.goodunitid = e.enumitemname and e.enumdiccode = 'goodsunit'
  104. where t.UserID = %d`, req.UserID)
  105. if req.AccountID > 0 {
  106. sql += fmt.Sprintf(` and t.AccountID = %d`, req.AccountID)
  107. }
  108. if req.ApplyID > 0 {
  109. sql += fmt.Sprintf(` and t.ApplyID = %d`, req.ApplyID)
  110. }
  111. if err := engine.SQL(sql).Find(&datas); err != nil {
  112. // 查询失败
  113. logger.GetLogger().Errorf("QueryPreasleApply failed: %s", err.Error())
  114. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  115. return
  116. }
  117. // 查询成功
  118. logger.GetLogger().Infof("QueryPreasleApply successed: %v", datas)
  119. appG.Response(http.StatusOK, e.SUCCESS, datas)
  120. }
  121. // QueryCPTradeUserGoodsDataReq 远期订单查询请求参数
  122. type QueryCPTradeUserGoodsDataReq struct {
  123. AccountID int `form:"accountid" binding:"required"`
  124. }
  125. // Cptradeusergoodsdata 用户合约数据表 - 导历史 (远期订单数据)
  126. type Cptradeusergoodsdata struct {
  127. Accountid int64 `json:"accountid" xorm:"'ACCOUNTID'" binding:"required"` // 账户ID
  128. Goodsid int64 `json:"goodsid" xorm:"'GOODSID'" binding:"required"` // 商品ID
  129. Wrstandardid int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 仓单标准ID
  130. Userid int64 `json:"userid" xorm:"'USERID'"` // 用户ID
  131. Inqty int64 `json:"inqty" xorm:"'INQTY'"` // 转入量(总数量)
  132. Cancelqty int64 `json:"cancelqty" xorm:"'CANCELQTY'"` // 注销量
  133. Deliveryqty int64 `json:"deliveryqty" xorm:"'DELIVERYQTY'"` // 交割量
  134. Curpresaleqty int64 `json:"curpresaleqty" xorm:"'CURPRESALEQTY'"` // 当前预售量
  135. Presaledqty int64 `json:"presaledqty" xorm:"'PRESALEDQTY'"` // 已预售量
  136. Presaledamount int64 `json:"presaledamount" xorm:"'PRESALEDAMOUNT'"` // 已预售总金额
  137. Marketid int64 `json:"marketid" xorm:"'MARKETID'"` // 市场ID
  138. Freezeamount float64 `json:"freezeamount" xorm:"'FREEZEAMOUNT'"` // 冻结金额
  139. Hasspotfreeze int64 `json:"hasspotfreeze" xorm:"'HASSPOTFREEZE'"` // 是否有现货冻结 - 0:否 1:有
  140. Goodscode string `json:"GoodsCode" xorm:"'GOODSCODE'"` // 订单商品代码
  141. Goodsname string `json:"GoodsName" xorm:"'GOODSNAME'"` // 订单商品名称
  142. Wrstandardcode string `json:"WRStandardCode" xorm:"'WRSTANDARDCODE'"` // 仓单标准代码
  143. Wrstandardname string `json:"WRStandardName" xorm:"'WRSTANDARDNAME'"` // 仓单标准名称
  144. Enabledqty int64 `json:"EnabledQty" xorm:"'ENABLEDQTY'"` // 可用量
  145. Goodunit string `json:"goodunit" xorm:"'GOODUNIT'"` // 报价单位
  146. }
  147. // TableName is CPTRADE_USERGOODSDATA
  148. func (Cptradeusergoodsdata) TableName() string {
  149. return "CPTRADE_USERGOODSDATA"
  150. }
  151. // QueryUserGoodsData 查询远期订单信息
  152. // @Summary 查询远期订单信息
  153. // @Produce json
  154. // @Security ApiKeyAuth
  155. // @Param accountid query int true "资金账户ID"
  156. // @Success 200 {object} Cptradeusergoodsdata
  157. // @Failure 500 {object} app.Response
  158. // @Router /CPTrade/QueryUserGoodsData [get]
  159. // @Tags 产能预售
  160. func QueryUserGoodsData(c *gin.Context) {
  161. appG := app.Gin{C: c}
  162. // 获取请求参数
  163. var req QueryCPTradeUserGoodsDataReq
  164. if err := appG.C.ShouldBindQuery(&req); err != nil {
  165. logger.GetLogger().Errorf("QueryUserGoodsData failed: %s", err.Error())
  166. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  167. return
  168. }
  169. // 查询数据
  170. engine := db.GetEngine()
  171. datas := make([]Cptradeusergoodsdata, 0)
  172. if err := engine.SQL(`select
  173. t.AccountID,
  174. t.GoodsID,
  175. t.WRStandardID,
  176. t.UserID,
  177. t.InQty,
  178. t.CancelQty,
  179. t.DeliveryQty,
  180. t.CurPresaleQty,
  181. t.PresaledQty,
  182. t.PresaledAmount,
  183. t.FreezeAmount,
  184. t.MarketID,
  185. t.HasSpotFreeze,
  186. g.GoodsCode,
  187. g.GoodsName,
  188. ws.WRStandardCode,
  189. ws.WRStandardName,
  190. (t.InQty - t.CancelQty - t.DeliveryQty - t.CurPresaleQty) EnabledQty,
  191. e.enumdicname GoodUnit
  192. from CPTrade_UserGoodsData t
  193. left join goods g on t.goodsid = g.goodsid
  194. left join WRStandard ws on t.wrstandardid = ws.wrstandardid
  195. left join enumdicitem e on g.goodunitid = e.enumitemname and e.enumdiccode = 'goodsunit'
  196. where t.accountid = ?`, req.AccountID).Find(&datas); err != nil {
  197. // 查询失败
  198. logger.GetLogger().Errorf("QueryUserGoodsData failed: %s", err.Error())
  199. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  200. return
  201. }
  202. // 查询成功
  203. logger.GetLogger().Infof("QueryUserGoodsData successed: %v", datas)
  204. appG.Response(http.StatusOK, e.SUCCESS, datas)
  205. }
  206. // QueryPositionCancelReq 查询远期订单注销申请请求参数
  207. type QueryPositionCancelReq struct {
  208. UserID int `form:"userid" binding:"required"`
  209. AccountID int `form:"accountid"`
  210. CancelID int `form:"cancelid"`
  211. }
  212. // Cptradepositioncancel 远期订单注销申请表
  213. type Cptradepositioncancel struct {
  214. Cancelid int64 `json:"cancelid" xorm:"'CANCELID'" binding:"required"` // 注销ID(SEQ_CPTRADE_POSITIONCANCEL)
  215. Userid int64 `json:"userid" xorm:"'USERID'"` // 申请人ID
  216. Accountid int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 申请人账户ID
  217. Goodsid int64 `json:"goodsid" xorm:"'GOODSID'"` // 商品ID
  218. Cancelqty int64 `json:"cancelqty" xorm:"'CANCELQTY'"` // 注销数量
  219. Applystatus int64 `json:"applystatus" xorm:"'APPLYSTATUS'"` // 申请状态 - 1:已提交 2:初审通过 3:初审拒绝 4:初审失败 5复审通过 6:复审拒绝 7:复审失败 8:已撤销
  220. Handlestatus int64 `json:"handlestatus" xorm:"'HANDLESTATUS'"` // 处理状态
  221. Applytime time.Time `json:"applytime" xorm:"'APPLYTIME'"` // 申请时间
  222. Creatorid int64 `json:"creatorid" xorm:"'CREATORID'"` // 创建人ID
  223. Createtime time.Time `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  224. Tradedate string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd)
  225. Marketid int64 `json:"marketid" xorm:"'MARKETID'"` // 市场ID
  226. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'"` // 订单商品代码
  227. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'"` // 订单商品名称
  228. Marketname string `json:"marketname" xorm:"'GOODSNAME'"` // 市场名称
  229. Goodunit string `json:"goodunit" xorm:"'GOODUNIT'"` // 报价单位
  230. Creatorname string `json:"creatorname" xorm:"CREATORNAME"` // 创建人
  231. }
  232. // TableName is CPTRADE_POSITIONCANCEL
  233. func (Cptradepositioncancel) TableName() string {
  234. return "CPTRADE_POSITIONCANCEL"
  235. }
  236. // QueryPositionCancel 查询远期订单注销申请信息
  237. // @Summary 查询远期订单注销申请信息
  238. // @Produce json
  239. // @Security ApiKeyAuth
  240. // @Param userid query int true "账户ID"
  241. // @Param cancelid query int false "注销ID"
  242. // @Param accountid query int false "资金账户ID"
  243. // @Success 200 {object} Cptradepositioncancel
  244. // @Failure 500 {object} app.Response
  245. // @Router /CPTrade/QueryPositionCancel [get]
  246. // @Tags 产能预售
  247. func QueryPositionCancel(c *gin.Context) {
  248. appG := app.Gin{C: c}
  249. // 获取请求参数
  250. var req QueryPositionCancelReq
  251. if err := appG.C.ShouldBindQuery(&req); err != nil {
  252. logger.GetLogger().Errorf("QueryPositionCancel failed: %s", err.Error())
  253. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  254. return
  255. }
  256. // 查询数据
  257. sql := fmt.Sprintf(`select
  258. t.CancelID,
  259. t.UserID,
  260. t.AccountID,
  261. t.GoodsID,
  262. t.CancelQty,
  263. t.ApplyStatus,
  264. t.HandleStatus,
  265. t.ApplyTime,
  266. t.CreatorID,
  267. t.CreateTime,
  268. t.TradeDate,
  269. t.MarketID,
  270. g.GoodsCode,
  271. g.GoodsName,
  272. m.MarketName,
  273. e.enumdicname GoodUnit,
  274. u.username CreatorName
  275. from CPTrade_PositionCancel t
  276. left join goods g on t.goodsid = g.goodsid
  277. left join market m on t.marketid = m.marketid
  278. left join enumdicitem e on g.goodunitid = e.enumitemname and e.enumdiccode = 'goodsunit'
  279. left join systemmanager u on t.creatorid = u.autoid
  280. where t.userid = %d`, req.UserID)
  281. if req.AccountID > 0 {
  282. sql += fmt.Sprintf(` and t.AccountID= %d`, req.AccountID)
  283. }
  284. if req.CancelID > 0 {
  285. sql += fmt.Sprintf(` and t.CancelID= %d`, req.CancelID)
  286. }
  287. engine := db.GetEngine()
  288. datas := make([]Cptradepositioncancel, 0)
  289. if err := engine.SQL(sql).Find(&datas); err != nil {
  290. // 查询失败
  291. logger.GetLogger().Errorf("QueryPositionCancel failed: %s", err.Error())
  292. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  293. return
  294. }
  295. // 查询成功
  296. logger.GetLogger().Infof("QueryPositionCancel successed: %v", datas)
  297. appG.Response(http.StatusOK, e.SUCCESS, datas)
  298. }
  299. // QueryPresaleGoodsExReq 查询产能预售商品扩展请求参数
  300. type QueryPresaleGoodsExReq struct {
  301. GoodsID int `form:"goodsid"`
  302. MarketID int `form:"marketid"`
  303. PresaleMode int `form:"presalemode"`
  304. GoodsIDs string `form:"goodsids"`
  305. }
  306. // Cptradepresalegoodsex 产能预售商品扩展表
  307. type Cptradepresalegoodsex struct {
  308. Goodsid int64 `json:"goodsid" xorm:"'GOODSID'" binding:"required"` // 商品ID(预售)
  309. Relatedgoodsid int64 `json:"relatedgoodsid" xorm:"'RELATEDGOODSID'"` // 关联交易合约ID
  310. Presaleqty int64 `json:"presaleqty" xorm:"'PRESALEQTY'"` // 预售数量
  311. Starttime time.Time `json:"starttime" xorm:"'STARTTIME'"` // 预售开始时间
  312. Endtime time.Time `json:"endtime" xorm:"'ENDTIME'"` // 预售结束时间
  313. Attachmenturl string `json:"attachmenturl" xorm:"'ATTACHMENTURL'"` // 附件地址
  314. Presalemode int64 `json:"presalemode" xorm:"'PRESALEMODE'"` // 预售模式 - 1:一口价 2:大宗式竞拍
  315. Marketid int64 `json:"marketid" xorm:"'MARKETID'"` // 预售市场ID - 根据预售模式选择市场
  316. Refprice float64 `json:"refprice" xorm:"'REFPRICE'"` // 参考价格[一口价]
  317. Startprice float64 `json:"startprice" xorm:"'STARTPRICE'"` // 起拍价[大宗式竞拍]
  318. Floorprice float64 `json:"floorprice" xorm:"'FLOORPRICE'"` // 底价[大宗式竞拍]
  319. Createtime time.Time `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  320. Tradedate string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd)
  321. Relatedmarketid int64 `json:"relatedmarketid" xorm:"'RELATEDMARKETID'"` // 关联交易合约市场ID
  322. Presaledqty int64 `json:"presaledqty" xorm:"'PRESALEDQTY'"` // 已预售量(预售结束时更新)
  323. Sellstatus int64 `json:"sellstatus" xorm:"'SELLSTATUS'"` // 卖方处理状态 - 1:卖方头寸未处理 2:卖方头寸已处理
  324. Presaledamount float64 `json:"presaledamount" xorm:"'PRESALEDAMOUNT'"` // 已预售总金额(预售结束时更新)
  325. Goodsdetail string `json:"goodsdetail" xorm:"'GOODSDETAIL'"` // 详情[大宗]
  326. Tradeprice float64 `json:"tradeprice" xorm:"'TRADEPRICE'"` // 成交价[大宗]
  327. }
  328. // TableName is CPTRADE_PRESALEGOODSEX
  329. func (Cptradepresalegoodsex) TableName() string {
  330. return "CPTRADE_PRESALEGOODSEX"
  331. }
  332. // QueryPresaleGoodsEx 查询产能预售商品扩展信息
  333. // @Summary 查询产能预售商品扩展信息
  334. // @Produce json
  335. // @Security ApiKeyAuth
  336. // @Param goodsid query int false "预售商品ID"
  337. // @Param marketid query int false "预售市场ID"
  338. // @Param presalemode query int false "预售模式 - 1:一口价 2:大宗式竞拍"
  339. // @Param goodsids query string false "预售商品ID列表 - 格式:1,2,3"
  340. // @Success 200 {object} Cptradepresalegoodsex
  341. // @Failure 500 {object} app.Response
  342. // @Router /CPTrade/QueryPresaleGoodsEx [get]
  343. // @Tags 产能预售
  344. func QueryPresaleGoodsEx(c *gin.Context) {
  345. appG := app.Gin{C: c}
  346. // FIXME: 由于数据中包括[]int类型,会造成校验报错,故暂时不使用
  347. // 获取请求参数
  348. var req QueryPresaleGoodsExReq
  349. if err := appG.C.ShouldBindQuery(&req); err != nil {
  350. logger.GetLogger().Errorf("QueryPresaleGoodsEx failed: %s", err.Error())
  351. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  352. return
  353. }
  354. // 查询数据
  355. engine := db.GetEngine()
  356. datas := make([]Cptradepresalegoodsex, 0)
  357. s := engine.Where("1=1")
  358. if req.GoodsID > 0 {
  359. s = s.And("GoodsID=?", req.GoodsID)
  360. }
  361. if req.MarketID > 0 {
  362. s = s.And("MarketID=?", req.MarketID)
  363. }
  364. if req.PresaleMode > 0 {
  365. s = s.And("PresaleMode=?", req.PresaleMode)
  366. }
  367. if len(req.GoodsIDs) > 0 {
  368. // s = s.And("GoodsID in (?)", req.GoodsIDs)
  369. s = s.In("GOODSID", strings.Split(req.GoodsIDs, ","))
  370. }
  371. if err := s.Find(&datas); err != nil {
  372. // 查询失败
  373. logger.GetLogger().Errorf("QueryPresaleGoodsEx failed: %s", err.Error())
  374. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  375. return
  376. }
  377. // 查询成功
  378. logger.GetLogger().Infof("QueryPresaleGoodsEx successed: %v", datas)
  379. appG.Response(http.StatusOK, e.SUCCESS, datas)
  380. }