ermcpExposure.go 32 KB

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