ermcpExposure.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /**
  2. * @Author: zou.yingbin
  3. * @Create : 2021/1/18 9:26
  4. * @Modify : 2021/1/18 9:26
  5. */
  6. package models
  7. import (
  8. "fmt"
  9. "github.com/golang/protobuf/proto"
  10. "mtp2_if/db"
  11. "mtp2_if/mtpcache"
  12. "mtp2_if/pb"
  13. "mtp2_if/rediscli"
  14. )
  15. //实时敞口
  16. type ErmcpRealExposureModel struct {
  17. MiddleGoodsID uint32 // 套保品种
  18. AreaUserID uint32 // 所属机构
  19. OriBuyPlanQty float64 // 期初采购计划数量
  20. OriBuyPricedQty float64 // 期初采购合同已定价数量
  21. OriSellPlanQty float64 // 期初销售计划数量
  22. OriSellPricedQty float64 // 期初销售合同已定价数量
  23. OriBuyFutureQty float64 // 期初买入期货数量
  24. OriSellFutureQty float64 // 期初卖出期货数量
  25. BuyPlanQty float64 // 采购计划数量
  26. BuyPricedQty float64 // 采购合同已定价数量
  27. SellPlanQty float64 // 销售计划数量
  28. SellPricedQty float64 // 销售合同已定价数量
  29. BuyFutureQty float64 // 买入期货数量
  30. SellFutureQty float64 // 卖出期货数量
  31. TotalSpotQty float64 // 现货数量
  32. TotalFutureQty float64 // 期货数量
  33. TotalExposure float64 // 总敞口
  34. TotalHedgeRatio float64 // 敞口比例
  35. TotalNeedHedgeQty float64 // 期货应套保量
  36. NeedHedgeExposoure float64 // 应套保敞口
  37. NeedHedgeRatio float64 // 应套保敞口比例
  38. MiddleGoodsName string // 套保品种名称
  39. MiddleGoodsCode string // 套保品种代码
  40. MiddleGoodsHedgeRatio float64 // 应套保比例
  41. OriTotalSpotQty float64 // 期初现货数量=(期初销售计划数量-期初销售合同已定价数量)-(期初采购计划数量-期初采购合同已定价数量)
  42. OriTotalFutuQty float64 // 期初期货数量=期初买入期货数量-期初卖出期货数量
  43. DiffSpotQty float64 // 今日变动量(现货) = 现货数量 - 期初现货数量
  44. DiffFutuQty float64 // 今日变动量(期货) = (买入 - 买入期初) - (卖出 - 卖出期初)
  45. }
  46. // 计算相关字段
  47. func (r *ErmcpRealExposureModel) calc() {
  48. r.OriTotalSpotQty = (r.OriSellPlanQty - r.OriSellPricedQty) - (r.OriBuyPlanQty - r.OriBuyPricedQty)
  49. r.OriTotalFutuQty = r.OriBuyFutureQty - r.OriSellFutureQty
  50. r.DiffSpotQty = r.TotalSpotQty - r.OriTotalSpotQty
  51. r.DiffFutuQty = (r.BuyFutureQty - r.OriBuyFutureQty) - (r.SellFutureQty - r.OriSellFutureQty)
  52. }
  53. func (r *ErmcpRealExposureModel) ParseFromProto(v *pb.ErmcpAreaExposure) {
  54. r.MiddleGoodsID = *v.MiddleGoodsID
  55. r.AreaUserID = *v.AreaUserID
  56. r.OriBuyPlanQty = *v.OriBuyPlanQty
  57. r.OriBuyPricedQty = *v.OriBuyPricedQty
  58. r.OriSellPlanQty = *v.OriSellPlanQty
  59. r.OriSellPricedQty = *v.OriSellPricedQty
  60. r.OriBuyFutureQty = float64(*v.OriBuyFutureQty)
  61. r.OriSellFutureQty = float64(*v.OriSellFutureQty)
  62. r.BuyPlanQty = *v.BuyPlanQty
  63. r.BuyPricedQty = *v.BuyPricedQty
  64. r.SellPlanQty = *v.SellPlanQty
  65. r.SellPricedQty = *v.SellPricedQty
  66. r.BuyFutureQty = *v.BuyFutureQty
  67. r.SellFutureQty = *v.SellFutureQty
  68. r.TotalSpotQty = *v.TotalSpotQty
  69. r.TotalFutureQty = *v.TotalFutureQty
  70. r.TotalExposure = *v.TotalExposure
  71. r.TotalHedgeRatio = *v.TotalHedgeRatio
  72. r.TotalNeedHedgeQty = *v.TotalNeedHedgeQty
  73. r.NeedHedgeExposoure = *v.NeedHedgeExposoure
  74. r.NeedHedgeRatio = *v.NeedHedgeRatio
  75. // 执行相关计算
  76. r.calc()
  77. }
  78. // 实时敞口数据: Redis数据 + 套保品种信息表
  79. func (r *ErmcpRealExposureModel) GetData() ([]ErmcpRealExposureModel, error) {
  80. // 获取关联的套路商品
  81. if sGoods, err := mtpcache.GetMiddleGoodsByUserID(r.AreaUserID); err == nil {
  82. sData := make([]ErmcpRealExposureModel, 0)
  83. // 从Redis获取数据
  84. for i := range sGoods {
  85. key := fmt.Sprintf("ErmcpAreaExposure:%d_%d", sGoods[i].AREAUSERID, sGoods[i].MIDDLEGOODSID)
  86. if ret, err := rediscli.GetRedisClient().Get(key).Result(); err == nil {
  87. if len(ret) > 0 {
  88. var data pb.ErmcpAreaExposure
  89. if err := proto.Unmarshal([]byte(ret), &data); err == nil {
  90. var m ErmcpRealExposureModel
  91. m.ParseFromProto(&data)
  92. sData = append(sData, m)
  93. }
  94. }
  95. }
  96. }
  97. return sData, nil
  98. }
  99. return nil, nil
  100. }
  101. /*************敞口明细**************/
  102. //敞口明细结构
  103. type ErmcpExposureDetailModel struct {
  104. Createtime string `json:"createtime" xorm:"'createtime'"` // 时间
  105. Areauserid uint32 `json:"areauserid" xorm:"'areauserid'"` // 机构ID
  106. Logtype int32 `json:"logtype" xorm:"'logtype'"` // 类型 - 1:套保计划 2:现货合同
  107. Contracttype int32 `json:"contracttype" xorm:"'contracttype'"` // 现货合同类型 - 1:采购 -1:销售
  108. Wrstandardid int32 `json:"wrstandardid" xorm:"'wrstandardid'"` // 现货商品ID
  109. Qty float64 `json:"qty" xorm:"'qty'"` // 数量
  110. RelateNo string `json:"relateNo" xorm:"'relateNo'"` // 现货合同/套保计划编号
  111. Middlegoodsname string `json:"middlegoodsname" xorm:"'middlegoodsname'"` // 套保商品名称
  112. Middlegoodscode string `json:"middlegoodscode" xorm:"'middlegoodscode'"` // 套保商品代码
  113. MiddlegoodsId int32 `json:"middlegoodsId" xorm:"'middlegoodsId'"` // 套保商品id
  114. Unitid int32 `json:"-" xorm:"'unitid'"` // 现货商品单位ID
  115. Wrstandardname string `json:"wrstandardname" xorm:"'wrstandardname'"` // 现货商品名称
  116. Wrstandardcode string `json:"wrstandardcode" xorm:"'wrstandardcode'"` // 现货商品代码
  117. Enumdicname string `json:"enumdicname" xorm:"'enumdicname'"` // 现货商品单位名称
  118. ChangeQty float64 `json:"changeQty" xorm:"'changeQty'"` // 套保变动量
  119. Convertfactor float64 `json:"convertfactor" xorm:"'convertfactor'"` // 标仓系数
  120. Convertratio float64 `json:"convertratio" xorm:"'convertratio'"` // 套保系数
  121. }
  122. func (r *ErmcpExposureDetailModel) buildSql() string {
  123. str := "with tmp as" +
  124. " (select 2 as LogType," +
  125. " s.spotcontractid as relatedid," +
  126. " s.contractno as relateNo" +
  127. " from ermcp_spotcontract s" +
  128. " union all" +
  129. " select 1, t.hedgeplanid as relateid, t.hedgeplanno as relateNo" +
  130. " from ermcp_hedgeplan t)" +
  131. "select to_char(t.createtime, 'yyyy-mm-dd hh24:mi:ss') createtime," +
  132. " t.middlegoodsid," +
  133. " t.areauserid," +
  134. " t.logtype," +
  135. " t.contracttype," +
  136. " wc.wrstandardid," +
  137. " t.qty," +
  138. " t.convertfactor," +
  139. " t.convertratio," +
  140. " t.qty * t.convertfactor * t.convertratio as changeQty," +
  141. " tmp.relateNo," +
  142. " m.middlegoodsname," +
  143. " m.middlegoodscode," +
  144. " w.unitid," +
  145. " w.wrstandardname," +
  146. " w.wrstandardcode," +
  147. " e.enumdicname" +
  148. " from ermcp_spotexposurelog t" +
  149. " left join erms2_wrsconvertdetail wc" +
  150. " on t.wrstandardid = wc.wrstandardid" +
  151. " and t.middlegoodsid = wc.middlegoodsid" +
  152. " left join erms_middlegoods m" +
  153. " on t.middlegoodsid = m.middlegoodsid" +
  154. " left join wrstandard w" +
  155. " on t.wrstandardid = w.wrstandardid" +
  156. " left join enumdicitem e" +
  157. " on w.unitid = e.enumitemname" +
  158. " and e.enumdiccode = 'goodsunit'" +
  159. " left join tmp" +
  160. " on t.logtype = tmp.LogType" +
  161. " and t.relatedid = tmp.relatedid" +
  162. " where t.middlegoodsid=%v and t.areauserid=%v"
  163. return fmt.Sprintf(str, r.MiddlegoodsId, r.Areauserid)
  164. }
  165. // 查询敞口现货明细
  166. func (r *ErmcpExposureDetailModel) GetData() ([]ErmcpExposureDetailModel, error) {
  167. e := db.GetEngine()
  168. s := e.SQL(r.buildSql())
  169. sData := make([]ErmcpExposureDetailModel, 0)
  170. if err := s.Find(&sData); err != nil {
  171. return nil, err
  172. }
  173. return sData, nil
  174. }
  175. /// 现货头寸
  176. // 现货头寸数据
  177. type AreaSpotModel struct {
  178. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID
  179. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 现货品种
  180. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货品种代码
  181. AREAUSERID int32 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  182. ORIBUYPLANQTY float64 `json:"-" xorm:"'ORIBUYPLANQTY'"` // 期初采购计划数量
  183. ORIBUYPRICEDQTY float64 `json:"-" xorm:"'ORIBUYPRICEDQTY'"` // 期初采购合同已定价数量
  184. ORISELLPLANQTY float64 `json:"-" xorm:"'ORISELLPLANQTY'"` // 期初销售计划数量
  185. ORISELLPRICEDQTY float64 `json:"-" xorm:"'ORISELLPRICEDQTY'"` // 期初销售合同已定价数量
  186. BUYPLANQTY float64 `json:"-" xorm:"'BUYPLANQTY'"` // 采购计划数量
  187. BUYPRICEDQTY float64 `json:"-" xorm:"'BUYPRICEDQTY'"` // 采购合同已定价数量
  188. SELLPLANQTY float64 `json:"-" xorm:"'SELLPLANQTY'"` // 销售计划数量
  189. SELLPRICEDQTY float64 `json:"-" xorm:"'SELLPRICEDQTY'"` // 销售合同已定价数量
  190. TOTALSPOTQTY float64 `json:"totalspotqty" xorm:"'TOTALSPOTQTY'"` // 当前数量(现货头寸总量) = (销售计划数量 - 销售已定价数量) - (采购计划数量 - 采购已定价数量)
  191. OriToalSpotQty float64 `json:"oritoalspotqty" xorm:"'OriToalSpotQty'"` // 昨日数量
  192. IncreaseQty float64 `json:"increaseqty" xorm:"'IncreaseQty'"` // 增加数量=销售计划数量+采购已定价数量
  193. DecreaseQty float64 `json:"decreaseqty" xorm:"'DecreaseQty'"` // 减少数量=-(销售已定价数量+采购计划数量)
  194. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  195. }
  196. // 进行相关字段的值计算
  197. func (r *AreaSpotModel) calc() {
  198. r.IncreaseQty = r.SELLPLANQTY + r.BUYPRICEDQTY
  199. r.DecreaseQty = (r.SELLPRICEDQTY + r.BUYPLANQTY) * -1
  200. r.OriToalSpotQty = (r.ORISELLPLANQTY - r.ORISELLPRICEDQTY) - (r.ORIBUYPLANQTY - r.ORIBUYPRICEDQTY)
  201. }
  202. func (r *AreaSpotModel) buildSql() string {
  203. str := "select t.WRSTANDARDID," +
  204. " w.WRSTANDARDNAME," +
  205. " w.WRSTANDARDCODE," +
  206. " t.AREAUSERID," +
  207. " t.ORIBUYPLANQTY," +
  208. " t.ORIBUYPRICEDQTY," +
  209. " t.ORISELLPLANQTY," +
  210. " t.ORISELLPRICEDQTY," +
  211. " t.BUYPLANQTY," +
  212. " t.BUYPRICEDQTY," +
  213. " t.SELLPLANQTY," +
  214. " t.SELLPRICEDQTY," +
  215. " t.TOTALSPOTQTY," +
  216. " to_char(t.UPDATETIME,'yyyy-mm-dd hh24:mi:ss') UPDATETIME" +
  217. " from ermcp_areaspot t" +
  218. " left join wrstandard w" +
  219. " on t.wrstandardid = w.wrstandardid" +
  220. " where t.areauserid=%v"
  221. return fmt.Sprintf(str, r.AREAUSERID)
  222. }
  223. // 从数据库中查询现货头寸
  224. func (r *AreaSpotModel) GetData() ([]AreaSpotModel, error) {
  225. e := db.GetEngine()
  226. sData := make([]AreaSpotModel, 0)
  227. if err := e.SQL(r.buildSql()).Find(&sData); err != nil {
  228. return nil, err
  229. }
  230. for i := range sData {
  231. sData[i].calc()
  232. }
  233. return sData, nil
  234. }
  235. // 现货头寸-现货明细
  236. type ErmcpAreaSpotDetailModel struct {
  237. Relatedid string `json:"relatedid" xorm:"'relatedid'"` // 套保计划ID/现货合同ID
  238. Relatedno string `json:"relatedno" xorm:"'relatedno'"` // 编号
  239. LogType int32 `json:"logtype" xorm:"'logType'"` // 记录类型 1-套保 2-现货合同
  240. Contracttype int32 `json:"contracttype" xorm:"'contracttype'"` // 合同类型 1-采购 -1-销售
  241. Wrstandardname string `json:"wrstandardname" xorm:"'wrstandardname'"` // 现货商品名称
  242. Wrstandardcode string `json:"wrstandardcode" xorm:"'wrstandardcode'"` // 现货商品代码
  243. Qty float64 `json:"qty" xorm:"'qty'"` // 数量
  244. Strtime string `json:"strtime" xorm:"'strtime'"` // 时间
  245. Enumdicname string `json:"enumdicname"` // 现货商品单位名称
  246. Recordname string `json:"recordname"` // 类型名称
  247. Unitid int32 `json:"-" xorm:"'UNITID'"` // 单位ID
  248. UserId int `json:"-"` // 所属用户ID
  249. WrstandardId int32 `json:"-"` // 现货商品ID
  250. }
  251. // 组建查询SQL
  252. func (r *ErmcpAreaSpotDetailModel) buildSql() string {
  253. str := "select to_char(t.hedgeplanid) relatedid," +
  254. " t.hedgeplanno relatedno," +
  255. " 1 as logType," +
  256. " t.contracttype," +
  257. " w.wrstandardname," +
  258. " w.wrstandardcode," +
  259. " t.planqty qty," +
  260. " w.unitid," +
  261. " to_char(t.createtime, 'yyyy-mm-dd hh:mi:ss') strtime" +
  262. " from ermcp_hedgeplan t" +
  263. " left join wrstandard w" +
  264. " on t.wrstandardid = w.wrstandardid" +
  265. " where t.hedgeplanstatus in (2, 3, 5)" +
  266. " and t.areauserid = %v and t.wrstandardid = %v" +
  267. " union all " +
  268. "select to_char(t.spotcontractid)," +
  269. " t.contractno," +
  270. " 1 as logType," +
  271. " t.contracttype," +
  272. " w.wrstandardname," +
  273. " w.wrstandardcode," +
  274. " t.qty," +
  275. " w.unitid," +
  276. " to_char(t.createtime, 'yyyy-mm-dd hh:mi:ss') strtime" +
  277. " from ermcp_spotcontract t" +
  278. " left join wrstandard w" +
  279. " on t.wrstandardid = w.wrstandardid" +
  280. " where t.contractstatus in (2, 3, 5)" +
  281. " and t.userid = %v and t.wrstandardid = %v"
  282. return fmt.Sprintf(str, r.UserId, r.WrstandardId, r.UserId, r.WrstandardId)
  283. }
  284. // 现货头寸-明细:数据加工处理
  285. func (r *ErmcpAreaSpotDetailModel) Calc() {
  286. var logTypeName, contractTypeName string
  287. if r.LogType == 1 {
  288. logTypeName = "计划"
  289. } else {
  290. logTypeName = "合同"
  291. }
  292. if r.Contracttype == 1 {
  293. contractTypeName = "采购"
  294. } else {
  295. contractTypeName = "销售"
  296. }
  297. r.Recordname = contractTypeName + logTypeName
  298. r.Enumdicname = mtpcache.GetEnumDicitemName(r.Unitid)
  299. }
  300. // 现货头寸-明细:从数据库中查询敞口现货头寸明细数据
  301. func (r *ErmcpAreaSpotDetailModel) GetData() ([]interface{}, error) {
  302. sData := make([]ErmcpAreaSpotDetailModel, 0)
  303. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  304. iDatas := make([]interface{}, 0)
  305. for i := range sData {
  306. // 注意要传指针类型(&sData[i]),因为要接口转换
  307. iDatas = append(iDatas, &sData[i])
  308. }
  309. return iDatas, err
  310. }
  311. ///////////////////////////
  312. /*历史/历史敞口*/
  313. type ErmcpHisExposure struct {
  314. RECKONDATE string `json:"reckondate" xorm:"'RECKONDATE'"` // 日照时期(yyyyMMdd)
  315. MIDDLEGOODSID int32 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'"` // 套保品种ID
  316. AREAUSERID int32 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  317. TOTALSPOTQTY float64 `json:"totalspotqty" xorm:"'TOTALSPOTQTY'"` // 现货头寸总量
  318. TOTALFUTUREQTY float64 `json:"totalfutureqty" xorm:"'TOTALFUTUREQTY'"` // 期货头寸总量
  319. TOTALEXPOSURE float64 `json:"totalexposure" xorm:"'TOTALEXPOSURE'"` // 总敞口
  320. TOTALHEDGERATIO float64 `json:"totalhedgeratio" xorm:"'TOTALHEDGERATIO'"` // 敞口比例
  321. TOTALNEEDHEDGEQTY float64 `json:"totalneedhedgeqty" xorm:"'TOTALNEEDHEDGEQTY'"` // 应套保总量
  322. NEEDHEDGEEXPOSOURE float64 `json:"needhedgeexposourE" xorm:"'NEEDHEDGEEXPOSOURE'"` // 应套保敞口
  323. NEEDHEDGERATIO float64 `json:"needhedgeratio" xorm:"'NEEDHEDGERATIO'"` // 应套保敞口比例
  324. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保品种名称
  325. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保品种代码
  326. LastNum int32 `json:"-"` // 查询条数
  327. }
  328. // 历史分品种嵌套结构
  329. type ErmcpHisExposureS struct {
  330. MIDDLEGOODSID int32 `json:"middlegoodsid"` // 套保品种ID
  331. MIDDLEGOODSNAME string `json:"middlegoodsname"` // 套保品种名称
  332. Data []ErmcpHisExposure `json:"data"` // 历史敞口
  333. }
  334. func (r *ErmcpHisExposure) buildSql() string {
  335. str := "select *" +
  336. " from (select t.RECKONDATE," +
  337. " t.MIDDLEGOODSID," +
  338. " t.AREAUSERID," +
  339. " t.TOTALSPOTQTY," +
  340. " t.TOTALFUTUREQTY," +
  341. " t.TOTALEXPOSURE," +
  342. " t.TOTALHEDGERATIO," +
  343. " t.TOTALNEEDHEDGEQTY," +
  344. " t.NEEDHEDGEEXPOSOURE," +
  345. " t.NEEDHEDGERATIO," +
  346. " g.MIDDLEGOODSNAME," +
  347. " g.MIDDLEGOODSCODE" +
  348. " from Reckon_ERMCP_AreaExposure t" +
  349. " left join erms_middlegoods g" +
  350. " on t.middlegoodsid = g.middlegoodsid" +
  351. " where t.areauserid = %v" +
  352. " order by t.middlegoodsid, t.reckondate desc) a" +
  353. " where rownum <= %v"
  354. if r.LastNum <= 0 {
  355. r.LastNum = 100000 //限制最多查10W条
  356. }
  357. return fmt.Sprintf(str, r.AREAUSERID, r.LastNum)
  358. }
  359. // 获取历史敞口
  360. func (r *ErmcpHisExposure) GetData() ([]ErmcpHisExposure, error) {
  361. e := db.GetEngine()
  362. sData := make([]ErmcpHisExposure, 0)
  363. if err := e.SQL(r.buildSql()).Find(&sData); err != nil {
  364. return nil, err
  365. }
  366. return sData, nil
  367. }
  368. // 套保品种关联交易商品
  369. type ErmcpTradeGoods struct {
  370. MIDDLEGOODSID int32 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'"` // 套保商品id
  371. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保商品名称
  372. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保商品代码
  373. SRCGOODSGROUPID int32 `json:"srcgoodsgroupid" xorm:"'SRCGOODSGROUPID'"` // 源期货品种id
  374. DESTGOODSGROUPID int32 `json:"destgoodsgroupid" xorm:"'DESTGOODSGROUPID'"` // 目标期货品种id
  375. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 折算系数
  376. GOODSID int32 `json:"goodsid" xorm:"pk 'GOODSID'"` // 商品id
  377. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 商品代码
  378. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 商品名称
  379. GOODSGROUPID int32 `json:"goodsgroupid" xorm:"'GOODSGROUPID'"` // 商品组id
  380. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODSUNITID'"` // 套保品种单位id
  381. AREAUSERID int32 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构id
  382. AGREEUNIT float64 `json:"agreeunit" xorm:"'agreeunit'"` // 合约单位
  383. }
  384. func (r *ErmcpTradeGoods) buildSq() string {
  385. sqlId := "SELECT t.MIDDLEGOODSID," +
  386. " t.MIDDLEGOODSNAME," +
  387. " t.MIDDLEGOODSCODE," +
  388. " t.GOODSUNITID," +
  389. " t.AREAUSERID," +
  390. " c.SRCGOODSGROUPID," +
  391. " c.DESTGOODSGROUPID," +
  392. " c.CONVERTRATIO," +
  393. " g.GOODSID," +
  394. " g.GOODSCODE," +
  395. " g.GOODSNAME," +
  396. " g.GOODSGROUPID," +
  397. " g.AGREEUNIT" +
  398. " FROM ERMS_MIDDLEGOODS t" +
  399. " INNER JOIN ERMCP_GGCONVERTCONFIG c" +
  400. " ON t.GOODSGROUPID = c.DESTGOODSGROUPID" +
  401. " INNER JOIN GOODS g" +
  402. " ON c.SRCGOODSGROUPID = g.GOODSGROUPID" +
  403. " WHERE t.AREAUSERID = %v" +
  404. " AND t.MIDDLEGOODSID = %v"
  405. sqlId = fmt.Sprintf(sqlId, r.AREAUSERID, r.MIDDLEGOODSID)
  406. return sqlId
  407. }
  408. // 获取套保商品关联的交易商品
  409. func (r *ErmcpTradeGoods) GetData() (map[int32]*ErmcpTradeGoods, error) {
  410. mData := make(map[int32]*ErmcpTradeGoods, 0)
  411. err := db.GetEngine().SQL(r.buildSq()).Find(&mData)
  412. return mData, err
  413. }
  414. // 获取用户头寸(子账户)
  415. type ErmcpTradePosition struct {
  416. USERID int32 `json:"userid" xorm:"'userid'"` // 用户id
  417. GOODSID int32 `json:"goodsid" xorm:"'goodsid'"` // 商品id
  418. BUYPOSITIONQTY int64 `json:"buypositionqty" xorm:"'Buypositionqty'"` // 买期初持仓
  419. BUYCURPOSITIONQTY int64 `json:"buycurpositionqty" xorm:"'Buycurpositionqty'"` // 买当前持仓
  420. SELLPOSITIONQTY int64 `json:"sellpositionqty" xorm:"'Sellpositionqty'"` // 卖期初持仓
  421. SELLCURPOSITIONQTY int64 `json:"sellcurpositionqty" xorm:"'Sellcurpositionqty'"` // 卖当前持仓
  422. }
  423. func (r *ErmcpTradePosition) buildSql() string {
  424. sqlId := "select a.relateduserid userid," +
  425. " a.goodsid," +
  426. " sum(a.buypositionqty) buypositionqty," +
  427. " sum(a.buycurpositionqty) buycurpositionqty," +
  428. " sum(a.sellpositionqty) sellpositionqty," +
  429. " sum(a.sellcurpositionqty) sellcurpositionqty" +
  430. " from (select ta.userid, t.*" +
  431. " from tradeposition t" +
  432. " inner join taaccount ta" +
  433. " on t.accountid = ta.accountid" +
  434. " where ta.relateduserid = %v and ismain=0" +
  435. " ) a" +
  436. " group by a.userid, a.goodsid"
  437. sqlId = fmt.Sprintf(sqlId, r.USERID)
  438. return sqlId
  439. }
  440. // 获取用户持仓头寸(子账户)
  441. func (r *ErmcpTradePosition) GetData() ([]ErmcpTradePosition, error) {
  442. sData := make([]ErmcpTradePosition, 0)
  443. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  444. return sData, err
  445. }
  446. // 用户头寸(母账户)
  447. type ErmcpHedgePosition struct {
  448. RELATEDUSERID int32 `json:"relateduserid" xorm:"'RELATEDUSERID'"` // 关联用户id
  449. ACCOUNTID int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 资金账号[外部母账户]
  450. HEDGEGOODSID int32 `json:"hedgegoodsid" xorm:"'HEDGEGOODSID'"` // 对冲合约ID
  451. HEDGEACCOUNTCODE string `json:"hedgeaccountcode" xorm:"'HEDGEACCOUNTCODE'"` // 对冲账号
  452. TRADEDATE string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd)
  453. MARKETID int32 `json:"marketid" xorm:"'mARKETID'"` // 市场ID
  454. YDBUYPOSITION int32 `json:"ydbuyposition" xorm:"'YDBUYPOSITION'"` // 期初买头寸
  455. CURBUYPOSITION int32 `json:"curbuyposition" xorm:"'CURBUYPOSITION'"` // 期末买头寸
  456. CURYDBUYPOSITION int32 `json:"curydbuyposition" xorm:"'CURYDBUYPOSITION'"` // 期末上日买头寸
  457. CURTDBUYPOSITION int32 `json:"curtdbuyposition" xorm:"'CURTDBUYPOSITION'"` // 期末今日买头寸
  458. FREYDBUYPOSITION int32 `json:"freydbuyposition" xorm:"'FREYDBUYPOSITION'"` // 冻结上日买头寸
  459. FRETDBUYPOSITION int32 `json:"fretdbuyposition" xorm:"'FRETDBUYPOSITION'"` // 冻结今日买头寸
  460. YDSELLPOSITION int32 `json:"ydsellposition" xorm:"'YDSELLPOSITION'"` // 期初卖头寸
  461. CURSELLPOSITION int32 `json:"cursellposition" xorm:"'CURSELLPOSITION'"` // 期末卖头寸
  462. CURYDSELLPOSITION int32 `json:"curydsellposition" xorm:"'CURYDSELLPOSITION'"` // 期末上日卖头寸
  463. CURTDSELLPOSITION int32 `json:"curtdsellposition" xorm:"'CURTDSELLPOSITION'"` // 期末今日卖头寸
  464. FREYDSELLPOSITION int32 `json:"freydsellposition" xorm:"'FREYDSELLPOSITION'"` // 冻结上日卖头寸
  465. FRETDSELLPOSITION int32 `json:"fretdsellposition" xorm:"'FRETDSELLPOSITION'"` // 冻结今日卖头寸
  466. GOODSID int32 `json:"goodsid" xorm:"'GOODSID'"` // 商品id
  467. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 商品代码
  468. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 商品名称
  469. TotalYdQty int32 `json:"totalydqty"` // 昨日数量(净头寸) = 期初买头寸 - 期初卖头寸
  470. TotalCurQty int32 `json:"totalcurqty"` // 当前数量(净头寸) = 期末买头寸 - 期末卖头寸
  471. IncreaseQty int32 `json:"increaseqty"` // 增加数量 = 期末买头寸 - 期初买头寸
  472. DecreaseQty int32 `json:"decreaseqty"` // 减少数量 = (期末卖头寸 - 期初卖头寸)*-1
  473. }
  474. func (r *ErmcpHedgePosition) Calc() {
  475. r.TotalYdQty = r.YDBUYPOSITION - r.YDSELLPOSITION
  476. r.TotalCurQty = r.CURBUYPOSITION - r.CURSELLPOSITION
  477. r.IncreaseQty = r.CURBUYPOSITION - r.YDBUYPOSITION
  478. r.DecreaseQty = (r.CURSELLPOSITION - r.YDSELLPOSITION) * -1
  479. }
  480. func (r *ErmcpHedgePosition) buildSql() string {
  481. sqlId := "select ta.RELATEDUSERID," +
  482. " t.ACCOUNTID," +
  483. " t.HEDGEGOODSID," +
  484. " t.HEDGEACCOUNTCODE," +
  485. " t.TRADEDATE," +
  486. " t.MARKETID," +
  487. " t.YDBUYPOSITION," +
  488. " t.CURBUYPOSITION," +
  489. " t.CURYDBUYPOSITION," +
  490. " t.CURTDBUYPOSITION," +
  491. " t.FREYDBUYPOSITION," +
  492. " t.FRETDBUYPOSITION," +
  493. " t.YDSELLPOSITION," +
  494. " t.CURSELLPOSITION," +
  495. " t.CURYDSELLPOSITION," +
  496. " t.CURTDSELLPOSITION," +
  497. " t.FREYDSELLPOSITION," +
  498. " t.FRETDSELLPOSITION," +
  499. " g.goodsid," +
  500. " g.goodscode," +
  501. " g.goodsname" +
  502. " from hedge_outtradeposition t" +
  503. " inner join taaccount ta" +
  504. " on t.accountid = ta.accountid" +
  505. " and ta.ismain = 1" +
  506. " and ta.relateduserid = %v" +
  507. " left join goods g" +
  508. " on t.hedgegoodsid = g.goodsid"
  509. sqlId = fmt.Sprintf(sqlId, r.RELATEDUSERID)
  510. return sqlId
  511. }
  512. // 获取对冲头寸(母账号头寸)
  513. func (r *ErmcpHedgePosition) GetData() ([]ErmcpHedgePosition, error) {
  514. sData := make([]ErmcpHedgePosition, 0)
  515. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  516. return sData, err
  517. }
  518. // 实时敞口\期货明细(头寸)
  519. type ErmcpExposurePostion struct {
  520. AREAUSERID int32 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构id
  521. MIDDLEGOODSID int32 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'"` // 套保商品id
  522. GOODSID int32 `json:"goodsid" xorm:"'GOODSID'"` // 商品id
  523. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 商品代码
  524. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 商品名称
  525. YdQty int64 `json:"ydqty" xorm:"'YdQty'"` // 昨日持仓
  526. CurQty int64 `json:"curqty" xorm:"'CurQty'"` // 当前持仓
  527. DiffQty int64 `json:"diffqty" xorm:"'DiffQty'"` // 持仓变动量=当前持仓-昨日持仓
  528. DiffHedgeQty float64 `json:"diffhedgeqty" xorm:"'DiffHedgeQty'"` // 套保品种变动量=持仓变动量*期货合约单位*期货品种系数
  529. }
  530. // 子账户相关计算(不一定用得到,现在说都是查母账号的)
  531. func (r *ErmcpExposurePostion) ParseFromPos(val *ErmcpTradeGoods, data *ErmcpTradePosition) {
  532. r.AREAUSERID = val.AREAUSERID
  533. r.MIDDLEGOODSID = val.MIDDLEGOODSID
  534. r.GOODSID = val.MIDDLEGOODSID
  535. r.GOODSCODE = val.GOODSCODE
  536. r.GOODSNAME = val.GOODSNAME
  537. //相关计算
  538. r.YdQty = data.BUYPOSITIONQTY - data.SELLPOSITIONQTY
  539. r.CurQty = data.BUYCURPOSITIONQTY - data.SELLCURPOSITIONQTY
  540. r.DiffQty = r.CurQty - r.YdQty
  541. r.DiffHedgeQty = float64(r.DiffQty) * float64(val.AGREEUNIT) * val.CONVERTRATIO
  542. }
  543. // 母账户相关计算
  544. func (r *ErmcpExposurePostion) ParseFromHedgePos(val *ErmcpTradeGoods, data *ErmcpHedgePosition) {
  545. r.AREAUSERID = val.AREAUSERID
  546. r.MIDDLEGOODSID = val.MIDDLEGOODSID
  547. r.GOODSID = val.MIDDLEGOODSID
  548. r.GOODSCODE = val.GOODSCODE
  549. r.GOODSNAME = val.GOODSNAME
  550. //相关计算
  551. r.YdQty = int64(data.YDBUYPOSITION - data.YDSELLPOSITION)
  552. r.CurQty = int64(data.CURBUYPOSITION - data.CURSELLPOSITION)
  553. r.DiffQty = r.CurQty - r.YdQty
  554. r.DiffHedgeQty = float64(r.DiffQty) * float64(val.AGREEUNIT) * val.CONVERTRATIO
  555. }
  556. // 获取敞口明细期货头寸
  557. func (r *ErmcpExposurePostion) GetDataEx() (interface{}, error) {
  558. // 查询交易商品
  559. mg := ErmcpTradeGoods{AREAUSERID: r.AREAUSERID, MIDDLEGOODSID: r.MIDDLEGOODSID}
  560. sGoods, err := mg.GetData()
  561. if err != nil || sGoods == nil || len(sGoods) == 0 {
  562. return nil, err
  563. }
  564. // 查询头寸(母账号)
  565. mp := ErmcpHedgePosition{RELATEDUSERID: r.AREAUSERID}
  566. sPostion, err1 := mp.GetData()
  567. if err1 != nil || sPostion == nil || len(sPostion) == 0 {
  568. return nil, err1
  569. }
  570. // 合并处理
  571. sData := make([]ErmcpExposurePostion, 0)
  572. for i := range sPostion {
  573. if val, ok := sGoods[sPostion[i].HEDGEGOODSID]; ok {
  574. d := ErmcpExposurePostion{}
  575. d.ParseFromHedgePos(val, &sPostion[i])
  576. sData = append(sData, d)
  577. }
  578. }
  579. return sData, nil
  580. }
  581. // 敞口/期货头寸/期货明细
  582. type ErmcpHedgePositionDetail struct {
  583. HEDGEGOODSID int32 `json:"hedgegoodsid" xorm:"'HEDGEGOODSID'"` // 商品id
  584. BUYORSELL int32 `json:"buyorsell" xorm:"'BUYORSELL'"` // 买卖方向 0-买 1-卖
  585. TRADEQTY int32 `json:"tradeqty" xorm:"'TRADEQTY'"` // 数量(成交数量)
  586. CHANNELBUILDTYPE int32 `json:"channelbuildtype" xorm:"'CHANNELBUILDTYPE'"` // 开平方向 1-建仓 2-平仓
  587. TRADETIME string `json:"tradetime" xorm:"'TRADETIME'"` // 时间(成交时间)
  588. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 商品代码
  589. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 商品名称
  590. AREAUSERID int32 `json:"-"` // 所属机构id
  591. }
  592. func (r *ErmcpHedgePositionDetail) Calc() {
  593. if r.BUYORSELL == 1 && r.TRADEQTY > 0{
  594. r.TRADEQTY = r.TRADEQTY * -1
  595. }
  596. }
  597. func (r *ErmcpHedgePositionDetail) buildSql() string {
  598. sqlId := "select t.hedgegoodsid," +
  599. " t.buyorsell," +
  600. " tradeqty," +
  601. " t.channelbuildtype," +
  602. " to_char(t.tradetime, 'yyyy-mm-dd hh24:mi:ss') tradetime," +
  603. " g.goodscode," +
  604. " g.goodsname" +
  605. " from hedge_outtradedetail t" +
  606. " inner join taaccount ta" +
  607. " on t.accountid = ta.accountid" +
  608. " and ta.ismain = 1" +
  609. " and ta.relateduserid = %v" +
  610. " left join goods g" +
  611. " on t.hedgegoodsid = g.goodsid" +
  612. " where t.hedgegoodsid = %v" +
  613. " union all " +
  614. "select t.hedgegoodsid," +
  615. " t.buyorsell," +
  616. " tradeqty," +
  617. " t.channelbuildtype," +
  618. " to_char(t.tradetime, 'yyyy-mm-dd hh24:mi:ss') tradetime," +
  619. " g.goodscode," +
  620. " g.goodsname" +
  621. " from his_hedge_outtradedetail t" +
  622. " inner join taaccount ta" +
  623. " on t.accountid = ta.accountid" +
  624. " and ta.ismain = 1" +
  625. " and ta.relateduserid = %v" +
  626. " left join goods g" +
  627. " on t.hedgegoodsid = g.goodsid" +
  628. " where t.isvalid = 1" +
  629. " and t.hedgegoodsid = %v"
  630. sqlId = fmt.Sprintf(sqlId, r.AREAUSERID, r.HEDGEGOODSID, r.AREAUSERID, r.HEDGEGOODSID)
  631. return sqlId
  632. }
  633. // 获取持仓头寸明细(成交记录)
  634. func (r *ErmcpHedgePositionDetail) GetDataEx() (interface{}, error) {
  635. sData := make([]ErmcpHedgePositionDetail, 0)
  636. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  637. for i := range sData {
  638. sData[i].Calc()
  639. }
  640. return sData, err
  641. }