erms3.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Package models 211.3 风险管理系统2020_现货合同
  2. package models
  3. import (
  4. "mtp2_if/db"
  5. "time"
  6. )
  7. // Erms3Spotcontractdetail 合同标的明细表
  8. type Erms3Spotcontractdetail struct {
  9. Spotdetailid string `json:"spotdetailid" xorm:"SPOTDETAILID"` // 标的明细ID(346+Unix秒时间戳(10位)+xxxxxx)
  10. Spotcontractid string `json:"spotcontractid" xorm:"SPOTCONTRACTID"` // 现货合同ID
  11. Wrstandardid int64 `json:"wrstandardid" xorm:"WRSTANDARDID"` // 仓单标准ID
  12. Deliverygoodsid int64 `json:"deliverygoodsid" xorm:"DELIVERYGOODSID"` // 现货品种ID
  13. Tradedate string `json:"tradedate" xorm:"TRADEDATE"` // 交易日(yyyyMMdd)
  14. Contracttype int32 `json:"contracttype" xorm:"CONTRACTTYPE"` // 现货合同类型 - 1:采购 -1:销售
  15. Accountid int64 `json:"accountid" xorm:"ACCOUNTID"` // 资金账户ID
  16. Customeraccountid int64 `json:"customeraccountid" xorm:"CUSTOMERACCOUNTID"` // 客户资金账户ID
  17. Producttype int32 `json:"producttype" xorm:"PRODUCTTYPE"` // 产品类型 - 1:标准仓单 2:等标 3:非标
  18. Deliverygoodsdesc string `json:"deliverygoodsdesc" xorm:"DELIVERYGOODSDESC"` // 产品规格 - 根据此字段生成仓单标准、仓单要素类型
  19. Warehouseid int64 `json:"warehouseid" xorm:"WAREHOUSEID"` // 仓库ID
  20. Pointdesc string `json:"pointdesc" xorm:"POINTDESC"` // 点价描述
  21. Priceqty float64 `json:"priceqty" xorm:"PRICEQTY"` // 指定价类总量
  22. Pointqty float64 `json:"pointqty" xorm:"POINTQTY"` // 点价类总量
  23. Unpricedqty float64 `json:"unpricedqty" xorm:"UNPRICEDQTY"` // 未确定量
  24. Unpricedrelatedqty float64 `json:"unpricedrelatedqty" xorm:"UNPRICEDRELATEDQTY"` // 未确定量关联量
  25. Unpricedavaliableqty float64 `json:"unpricedavaliableqty" xorm:"UNPRICEDAVALIABLEQTY"` // 未确定量可关联量
  26. Unpricedcancelledqty float64 `json:"unpricedcancelledqty" xorm:"UNPRICEDCANCELLEDQTY"` // 未确定量撤销量(非业务)
  27. Pricedqty float64 `json:"pricedqty" xorm:"PRICEDQTY"` // 确定量
  28. Pricedamount float64 `json:"pricedamount" xorm:"PRICEDAMOUNT"` // 确定金额
  29. Pricedrelatedqty float64 `json:"pricedrelatedqty" xorm:"PRICEDRELATEDQTY"` // 确定量关联量
  30. Pricedavaliableqty float64 `json:"pricedavaliableqty" xorm:"PRICEDAVALIABLEQTY"` // 确定量可关联量
  31. Pricedcancelledqty float64 `json:"pricedcancelledqty" xorm:"PRICEDCANCELLEDQTY"` // 确定量撤销量(非业务)
  32. Pricedcancelledamount float64 `json:"pricedcancelledamount" xorm:"PRICEDCANCELLEDAMOUNT"` // 确定量撤销金额(非业务)
  33. Deliveryqty float64 `json:"deliveryqty" xorm:"DELIVERYQTY"` // 交收数量(非业务)
  34. Deliveryamount float64 `json:"deliveryamount" xorm:"DELIVERYAMOUNT"` // 交收金额(非业务)
  35. Deliveryovershortqty float64 `json:"deliveryovershortqty" xorm:"DELIVERYOVERSHORTQTY"` // 交收溢短数量(非业务)
  36. Deliveryactualamount float64 `json:"deliveryactualamount" xorm:"DELIVERYACTUALAMOUNT"` // 交收实际金额(非业务)
  37. Deliveryotheramount float64 `json:"deliveryotheramount" xorm:"DELIVERYOTHERAMOUNT"` // 交收其它费用(非业务)
  38. Curdeliveryqty float64 `json:"curdeliveryqty" xorm:"CURDELIVERYQTY"` // 剩余交收数量
  39. Curdeliveryamount float64 `json:"curdeliveryamount" xorm:"CURDELIVERYAMOUNT"` // 剩余交收金额
  40. Remark string `json:"remark" xorm:"REMARK"` // 备注
  41. Marketid int64 `json:"marketid" xorm:"MARKETID"` // 市场ID
  42. Handlestatus int32 `json:"handlestatus" xorm:"HANDLESTATUS"` // 处理状态
  43. Updatetime time.Time `json:"updatetime" xorm:"UPDATETIME"` // 更新时间
  44. Reckonedamount float64 `json:"reckonedamount" xorm:"RECKONEDAMOUNT"` // 已结金额
  45. Invoicedamount float64 `json:"invoicedamount" xorm:"INVOICEDAMOUNT"` // 已开票金额
  46. }
  47. // TableName is ERMS3_SPOTCONTRACTDETAIL
  48. func (Erms3Spotcontractdetail) TableName() string {
  49. return "ERMS3_SPOTCONTRACTDETAIL"
  50. }
  51. // Erms3SpotContractInfo 合同明细.
  52. type Erms3SpotContractInfo struct {
  53. Erms3Spotcontractdetail `xorm:"extends"`
  54. Areauserid int64 `json:"areauserid" xorm:"AREAUSERID"` // 所属机构
  55. Accountid int64 `json:"accountid" xorm:"ACCOUNTID"` // 资金账户ID
  56. Customeruserid int64 `json:"customeruserid" xorm:"CUSTOMERUSERID"` // 客户ID
  57. Customeraccountid int64 `json:"customeraccountid" xorm:"CUSTOMERACCOUNTID"` // 客户资金账户ID
  58. Signdate time.Time `json:"signdate" xorm:"SIGNDATE"` // 签订日期
  59. Closestatus int32 `json:"closestatus" xorm:"CLOSESTATUS"` // 完结状态 - 0:未完结 1:已完结
  60. Relatedbizid string `json:"relatedbizid" xorm:"RELATEDBIZID"` // 关联业务ID
  61. Goodunit string `json:"goodunit" xorm:"'GOODUNIT'"` // 报价单位
  62. Wrstandardcode string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 仓单标准代码
  63. Wrstandardname string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 仓单标准名称
  64. Deliverygoodscode string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 交割商品代码
  65. Deliverygoodsname string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 交割商品名称
  66. }
  67. // Erms3SpotContractApply 现货合同申请表
  68. type Erms3SpotContractApply struct {
  69. SpotContractID int64 `json:"spotcontractid" xorm:"'SPOTCONTRACTID'" binging:"required"` // 现货合同ID(345+Unix秒时间戳(10位)+xxxxxx)
  70. TradeDate string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd)
  71. ContractNo string `json:"contractno" xorm:"'CONTRACTNO'"` // 现货合同编号
  72. ContractType int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 现货合同类型 - 1:采购 -1:销售
  73. AreaUserID int32 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  74. AccountID int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 资金账户ID
  75. CustomerUserID int32 `json:"customeruserid" xorm:"'CUSTOMERUSERID'"` // 客户ID
  76. CustomerAccountID int64 `json:"customeraccountid" xorm:"'CUSTOMERACCOUNTID'"` // 客户资金账户ID
  77. SignDate string `json:"signdate" xorm:"'SIGNDATE'"` // 签订日期
  78. LastDate string `json:"lastdate" xorm:"'LASTDATE'"` // 交货时间
  79. ContractAttachment string `json:"contractattachment" xorm:"'CONTRACTATTACHMENT'"` // 合同附件
  80. OriMarginPayer int32 `json:"orimarginpayer" xorm:"'ORIMARGINPAYER'"` // 初始保证金支付方 -1:买方 2:卖方
  81. OriMargin float64 `json:"orimargin" xorm:"'ORIMARGIN'"` // 初始保证金
  82. Remark string `json:"remark" xorm:"'REMARK'"` // 备注
  83. DetailJSON string `json:"detailjson" xorm:"'DETAILJSON'"` // 明细JSON
  84. ApplyStatus int32 `json:"applystatus" xorm:"'APPLYSTATUS'"` // 申请状态 - 0:未审核 1:审核通过 2:审核中 3:审核失败 4已撤销 5:审核拒绝
  85. ApplySrc int32 `json:"applysrc" xorm:"'APPLYSRC'"` // 申请来源 - 1:管理端 2:终端
  86. MarketID int32 `json:"marketid" xorm:"'MARKETID'"` // 市场ID
  87. CreatorID int32 `json:"creatorid" xorm:"'CREATORID'"` // 申请人
  88. CreateTime time.Time `json:"createtime" xorm:"'CREATETIME'"` // 申请时间
  89. AuditID int32 `json:"auditid" xorm:"'AUDITID'"` // 审核人
  90. AuditTime time.Time `json:"audittime" xorm:"'AUDITTIME'"` // 审核时间
  91. AuditRemark string `json:"auditremark" xorm:"'AUDITREMARK'"` // 审核备注
  92. }
  93. // TableName is ERMS3_SPOTCONTRACTAPPLY
  94. func (Erms3SpotContractApply) TableName() string {
  95. return "ERMS3_SPOTCONTRACTAPPLY"
  96. }
  97. // Erms3Biztradedetail 业务关联单据明细表
  98. type Erms3Biztradedetail struct {
  99. Biztradedetailid int64 `json:"biztradedetailid" xorm:"BIZTRADEDETAILID"` // 业务关联单据明细ID(352+Unix秒时间戳(10位)+xxxxxx)
  100. Tradedetailid int64 `json:"tradedetailid" xorm:"TRADEDETAILID"` // 单据明细ID
  101. Relatedbiztype int32 `json:"relatedbiztype" xorm:"RELATEDBIZTYPE"` // 关联业务类型 - 1:期现套利 2:仓单回购 3:现货贸易
  102. Relatedbizid int64 `json:"relatedbizid" xorm:"RELATEDBIZID"` // 关联业务ID
  103. Marketid int64 `json:"marketid" xorm:"MARKETID"` // 业务市场ID
  104. Spotdetailid int64 `json:"spotdetailid" xorm:"SPOTDETAILID"` // 标的明细ID
  105. Spotcontractid int64 `json:"spotcontractid" xorm:"SPOTCONTRACTID"` // 现货合同ID
  106. Contracttype int32 `json:"contracttype" xorm:"CONTRACTTYPE"` // 现货合同类型 - 1:采购 -1:销售
  107. Bizaccountid int64 `json:"bizaccountid" xorm:"BIZACCOUNTID"` // 业务现货资金账户
  108. Contractaccountid int64 `json:"contractaccountid" xorm:"CONTRACTACCOUNTID"` // 现货合同资金账户
  109. Tradedate string `json:"tradedate" xorm:"TRADEDATE"` // 交易日(yyyyMMdd)
  110. Relatedqty float64 `json:"relatedqty" xorm:"RELATEDQTY"` // 关联数量
  111. Relatedamount float64 `json:"relatedamount" xorm:"RELATEDAMOUNT"` // 关联金额
  112. Cancelledqty float64 `json:"cancelledqty" xorm:"CANCELLEDQTY"` // 撤销量
  113. Cancelledamount float64 `json:"cancelledamount" xorm:"CANCELLEDAMOUNT"` // 撤销金额
  114. Deliveryqty float64 `json:"deliveryqty" xorm:"DELIVERYQTY"` // 交收数量(业务)
  115. Deliveryamount float64 `json:"deliveryamount" xorm:"DELIVERYAMOUNT"` // 交收金额(业务) - 按比例计算
  116. Deliveryovershortqty float64 `json:"deliveryovershortqty" xorm:"DELIVERYOVERSHORTQTY"` // 交收溢短数量(业务)
  117. Deliveryactualamount float64 `json:"deliveryactualamount" xorm:"DELIVERYACTUALAMOUNT"` // 交收实际金额(业务)
  118. Deliveryotheramount float64 `json:"deliveryotheramount" xorm:"DELIVERYOTHERAMOUNT"` // 交收其它费用(业务)
  119. Closestatus int32 `json:"closestatus" xorm:"CLOSESTATUS"` // 完结状态 - 0:未完结 1:已完结
  120. Closetradedate string `json:"closetradedate" xorm:"CLOSETRADEDATE"` // 完结交易日(yyyyMMdd)
  121. Updatetime time.Time `json:"updatetime" xorm:"UPDATETIME"` // 更新时间
  122. Handlestatus int32 `json:"handlestatus" xorm:"HANDLESTATUS"` // 处理状态
  123. }
  124. // TableName is ERMS3_BIZTRADEDETAIL
  125. func (m *Erms3Biztradedetail) TableName() string {
  126. return "ERMS3_BIZTRADEDETAIL"
  127. }
  128. // AddSpotContractApply 插入现货合同记录
  129. func AddSpotContractApply(spotContractApply Erms3SpotContractApply) error {
  130. engine := db.GetEngine()
  131. // 组织sql,插入现货合同申请信息
  132. sql := `INSERT INTO ERMS3_SPOTCONTRACTAPPLY(SPOTCONTRACTID,
  133. TRADEDATE,
  134. CONTRACTNO,
  135. CONTRACTTYPE,
  136. AREAUSERID,
  137. ACCOUNTID,
  138. CUSTOMERUSERID,
  139. CUSTOMERACCOUNTID,
  140. SIGNDATE,
  141. CONTRACTATTACHMENT,
  142. ORIMARGIN,
  143. REMARK,
  144. DETAILJSON,
  145. APPLYSTATUS,
  146. APPLYSRC,
  147. CREATORID,
  148. CREATETIME)
  149. VALUES(?,
  150. ?,
  151. ?,
  152. ?,
  153. ?,
  154. ?,
  155. ?,
  156. ?,
  157. to_date(?, 'YYYY-MM-DD HH24:MI:SS'),
  158. ?,
  159. ?,
  160. ?,
  161. ?,
  162. ?,
  163. ?,
  164. ?,
  165. sysdate)`
  166. _, err := engine.Exec(sql,
  167. spotContractApply.SpotContractID,
  168. spotContractApply.TradeDate,
  169. spotContractApply.ContractNo,
  170. spotContractApply.ContractType,
  171. spotContractApply.AreaUserID,
  172. spotContractApply.AccountID,
  173. spotContractApply.CustomerUserID,
  174. spotContractApply.CustomerAccountID,
  175. spotContractApply.SignDate,
  176. spotContractApply.ContractAttachment,
  177. spotContractApply.OriMargin,
  178. spotContractApply.Remark,
  179. spotContractApply.DetailJSON,
  180. spotContractApply.ApplyStatus,
  181. spotContractApply.ApplySrc,
  182. spotContractApply.CreatorID)
  183. return err
  184. }
  185. // QueryErms3SpotContractInfo 查询现货合同信息
  186. func QueryErms3SpotContractInfo(accountids []int64, contracttype, contractmode, status int32) ([]Erms3SpotContractInfo, error) {
  187. datas := make([]Erms3SpotContractInfo, 0)
  188. engine := db.GetEngine()
  189. s := engine.Table("ERMS3_SPOTCONTRACTDETAIL").
  190. Join("LEFT", "ERMS3_SPOTCONTRACT", "ERMS3_SPOTCONTRACTDETAIL.SPOTCONTRACTID = ERMS3_SPOTCONTRACT.SPOTCONTRACTID").
  191. Join("LEFT", "ERMS3_BIZTRADEDETAIL", "ERMS3_BIZTRADEDETAIL.SPOTDETAILID = ERMS3_SPOTCONTRACTDETAIL.SPOTDETAILID").
  192. Join("LEFT", "WRSTANDARD", "WRSTANDARD.WRSTANDARDID = ERMS3_SPOTCONTRACTDETAIL.WRSTANDARDID").
  193. Join("LEFT", "DELIVERYGOODS", "DELIVERYGOODS.DELIVERYGOODSID = WRSTANDARD.DELIVERYGOODSID").
  194. Join("LEFT", "ENUMDICITEM", "WRSTANDARD.UNITID = ENUMDICITEM.ENUMITEMNAME AND ENUMDICITEM.ENUMDICCODE = 'goodsunit'").
  195. Select(`to_char(ERMS3_SPOTCONTRACTDETAIL.SPOTCONTRACTID) SPOTCONTRACTID, ERMS3_SPOTCONTRACT.AREAUSERID, ERMS3_SPOTCONTRACT.ACCOUNTID, ERMS3_SPOTCONTRACT.CUSTOMERUSERID,
  196. ERMS3_SPOTCONTRACT.CUSTOMERACCOUNTID, ERMS3_SPOTCONTRACT.CLOSESTATUS, ERMS3_SPOTCONTRACT.SIGNDATE, ERMS3_SPOTCONTRACTDETAIL.*, WRSTANDARD.WRSTANDARDNAME, WRSTANDARD.WRSTANDARDCODE, DELIVERYGOODS.DELIVERYGOODSNAME, DELIVERYGOODS.DELIVERYGOODSCODE,
  197. to_char(ERMS3_BIZTRADEDETAIL.RELATEDBIZID) RELATEDBIZID, ENUMDICITEM.ENUMDICNAME GOODUNIT`).
  198. Where("ERMS3_SPOTCONTRACT.CONTRACTTYPE = ? AND ERMS3_SPOTCONTRACT.CONTRACTMODE = ? AND ERMS3_SPOTCONTRACT.CLOSESTATUS = ?", contracttype, contractmode, status).
  199. In("ERMS3_SPOTCONTRACT.ACCOUNTID", accountids).
  200. Desc("ERMS3_SPOTCONTRACTDETAIL.SPOTCONTRACTID")
  201. err := s.Find(&datas)
  202. return datas, err
  203. }
  204. // QueryBizTradeDetailByBizID 根据关联业务ID查询.
  205. func QueryBizTradeDetailByBizID(relatedbizid []int64) ([]Erms3Biztradedetail, error) {
  206. data := make([]Erms3Biztradedetail, 0)
  207. engine := db.GetEngine()
  208. err := engine.In("RELATEDBIZID", relatedbizid).Find(&data)
  209. return data, err
  210. }
  211. // Erms3BizTradeInfo 汇总数据.
  212. type Erms3BizTradeInfo struct {
  213. Relatedbizid int64 // 关联业务ID
  214. Buyqty float64 // 采购量.
  215. Buyamount float64 // 采购额.
  216. Sellqty float64 // 销售量.
  217. Sellamount float64 // 销售额.
  218. }
  219. // QueryBizTradeInfo 根据关联业务ID查询汇总信息.
  220. func QueryBizTradeInfo(relatedbizid []int64) (map[int64]Erms3BizTradeInfo, error) {
  221. data, err := QueryBizTradeDetailByBizID(relatedbizid)
  222. if err != nil {
  223. return nil, err
  224. }
  225. infos := make(map[int64]Erms3BizTradeInfo)
  226. for i := range data {
  227. info := infos[data[i].Relatedbizid]
  228. if 1 == data[i].Contracttype {
  229. info.Buyqty += data[i].Relatedqty - data[i].Cancelledqty
  230. info.Buyamount += data[i].Relatedamount - data[i].Cancelledamount
  231. } else if -1 == data[i].Contracttype {
  232. info.Sellqty += data[i].Relatedqty - data[i].Cancelledqty
  233. info.Sellamount += data[i].Relatedamount - data[i].Cancelledamount
  234. }
  235. infos[data[i].Relatedbizid] = info
  236. }
  237. return infos, nil
  238. }