ermcpExposure.go 33 KB

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