ermcpExposure.go 31 KB

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