ermcpExposure.go 37 KB

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