report.go 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. package models
  2. import (
  3. "mtp2_if/db"
  4. "mtp2_if/utils"
  5. "strconv"
  6. "time"
  7. )
  8. // Reckondaytaaccount 资金账户日照表
  9. type ReportReckondaytaaccount struct {
  10. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID" form:"accountid" binding:"required"` // 资金账户ID
  11. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE" form:"reckondate" binding:"required"` // 日照日期(yyyyMMdd)
  12. USERID int64 `json:"userid" xorm:"USERID"` // 用户ID
  13. CURRENCYID int64 `json:"currencyid" xorm:"CURRENCYID"` // 货币ID
  14. CHANGEFLAG int32 `json:"changeflag" xorm:"CHANGEFLAG"` // 变动标志(当前账户资金有任何变动更新为1系统结算时更新0;供清算时使用) 0:无变动 1:有变动
  15. PASSWORD string `json:"password" xorm:"PASSWORD"` // 资金密码
  16. TRADESTATUS int32 `json:"tradestatus" xorm:"TRADESTATUS"` // 交易状态 - 1:正常 2:受限 3:冻结 4:禁止建仓(人工受限) 5:禁止交易(人工冻结)
  17. SIGNSTATUS int32 `json:"signstatus" xorm:"SIGNSTATUS"` // 签约状态 - 1:未签约 2:已签约 3:已解约
  18. ISMAIN int32 `json:"ismain" xorm:"ISMAIN"` // 是否账号 0:不是母账户 1:是母账户
  19. PARENTACCOUNTID int64 `json:"parentaccountid" xorm:"PARENTACCOUNTID"` // 所属根账户
  20. RELATEUSERID int64 `json:"relateuserid" xorm:"RELATEUSERID"` // 关联用户
  21. RELATEDACCOUNTSTATUS int32 `json:"relatedaccountstatus" xorm:"RELATEDACCOUNTSTATUS"` // 关联用户交易状态 - 1:正常(可交易) 2:受限(可平仓,不可建仓) 3:冻结(不可交易)
  22. TAACCOUNTTYPE int32 `json:"taaccounttype" xorm:"TAACCOUNTTYPE"` // 外部/内部账号 - 1:外部账号 2:内部账号
  23. ISRECKONACCOUNT int32 `json:"isreckonaccount" xorm:"ISRECKONACCOUNT"` // 是否机构分润账号 0:不是 1:是
  24. ISMARKETACCOUNT int32 `json:"ismarketaccount" xorm:"ISMARKETACCOUNT"` // 是否机构接单账号 0:不是 1:是
  25. BALANCE float64 `json:"balance" xorm:"BALANCE"` // 期初余额
  26. ORIFREEZEMARGIN float64 `json:"orifreezemargin" xorm:"ORIFREEZEMARGIN"` // 期初冻结保证金
  27. ORIUSEDMARGIN float64 `json:"oriusedmargin" xorm:"ORIUSEDMARGIN"` // 期初占用保证金
  28. ORIOTHERFREEZEMARGIN float64 `json:"oriotherfreezemargin" xorm:"ORIOTHERFREEZEMARGIN"` // 期初其他冻结保证金(出金冻结资金 交割买方冻结 申购冻结 全款买入 商城买入)
  29. ORIFREEZECHARGE float64 `json:"orifreezecharge" xorm:"ORIFREEZECHARGE"` // 期初手续费冻结
  30. ORIMORTGAGECREDIT float64 `json:"orimortgagecredit" xorm:"ORIMORTGAGECREDIT"` // 期初授信金额
  31. ORIOTHERCREDIT float64 `json:"oriothercredit" xorm:"ORIOTHERCREDIT"` // 期初其它授信金额
  32. ORIOUTAMOUNTFREEZE float64 `json:"orioutamountfreeze" xorm:"ORIOUTAMOUNTFREEZE"` // 期初出金冻结
  33. CURRENTBALANCE float64 `json:"currentbalance" xorm:"CURRENTBALANCE"` // 期末余额
  34. FREEZEMARGIN float64 `json:"freezemargin" xorm:"FREEZEMARGIN"` // 冻结保证金
  35. USEDMARGIN float64 `json:"usedmargin" xorm:"USEDMARGIN"` // 占用保证金
  36. OTHERFREEZEMARGIN float64 `json:"otherfreezemargin" xorm:"OTHERFREEZEMARGIN"` // 其他冻结保证金(出金冻结资金 交割买方冻结 申购冻结 全款买入 商城买入)
  37. FREEZECHARGE float64 `json:"freezecharge" xorm:"FREEZECHARGE"` // 手续费冻结
  38. MORTGAGECREDIT float64 `json:"mortgagecredit" xorm:"MORTGAGECREDIT"` // 授信金额
  39. OTHERCREDIT float64 `json:"othercredit" xorm:"OTHERCREDIT"` // 其它授信金额
  40. OUTAMOUNTFREEZE float64 `json:"outamountfreeze" xorm:"OUTAMOUNTFREEZE"` // 出金冻结
  41. INAMOUNT float64 `json:"inamount" xorm:"INAMOUNT"` // 今日入金金额
  42. OUTAMOUNT float64 `json:"outamount" xorm:"OUTAMOUNT"` // 今日出金金额
  43. PAYCHARGE float64 `json:"paycharge" xorm:"PAYCHARGE"` // 今日手续费支出
  44. CLOSEPL float64 `json:"closepl" xorm:"CLOSEPL"` // 今日平仓盈亏
  45. RECKONPL float64 `json:"reckonpl" xorm:"RECKONPL"` // 今日结算盈亏
  46. CREDITINCREASE float64 `json:"creditincrease" xorm:"CREDITINCREASE"` // 今日授信增加
  47. CREDITDECREASE float64 `json:"creditdecrease" xorm:"CREDITDECREASE"` // 今日授信减少
  48. OTHERCREDITINCREASE float64 `json:"othercreditincrease" xorm:"OTHERCREDITINCREASE"` // 今日其它授信增加
  49. OTHERCREDITDECREASE float64 `json:"othercreditdecrease" xorm:"OTHERCREDITDECREASE"` // 今日其它授信减少
  50. OTHERPAY float64 `json:"otherpay" xorm:"OTHERPAY"` // 其他支出(交割付款 申购付款 全款买入 商城买入 卖家退货)
  51. OTHERINCOME float64 `json:"otherincome" xorm:"OTHERINCOME"` // 其他收入(交割收款 申购收款 全款卖出 商城卖出 买家退货 会员手续费收入)
  52. TRANSFERAMOUNT float64 `json:"transferamount" xorm:"TRANSFERAMOUNT"` // 今日划转金额(母子账号资金划转,从账号划入为正,从账号划出为负)
  53. OUTTHRESHOLD float64 `json:"outthreshold" xorm:"OUTTHRESHOLD"` // 出金阈值
  54. CLEARBALANCE float64 `json:"clearbalance" xorm:"CLEARBALANCE"` // 清算余额 = 期末余额 - 授信金额 - 其它授信金额
  55. DIVIDEDCHARGE float64 `json:"dividedcharge" xorm:"DIVIDEDCHARGE"` // 今日已收手续费分成
  56. PAYEXCHCHARGE float64 `json:"payexchcharge" xorm:"PAYEXCHCHARGE"` // 今日应付交易所手续费
  57. CLEARPL float64 `json:"clearpl" xorm:"CLEARPL"` // 真实资金变动值(除出入金、应付交易所手续费)清算盈亏 = 期末余额 - 期初余额 - (今日入金 + 今日出金) - 今日应付交易所手续费 - 【(今日授信增加 + 今日授信减少) - (今日其它授信增加 + 今日其它授信减少) ----母账户】 = 今日手续费支出 + 今日平仓盈亏 + 今日结算盈亏 + 其他支出 + 其他收入 + 今日划转金额 + 今日已收手续费分成 - 今日应付交易所手续费
  58. THIRDINAMOUNT float64 `json:"thirdinamount" xorm:"THIRDINAMOUNT"` // 今日三方入金
  59. TRADECHARGE float64 `json:"tradecharge" xorm:"TRADECHARGE"` // 今日交易手续费支出 [101,102]
  60. INTERESTCHARGE float64 `json:"interestcharge" xorm:"INTERESTCHARGE"` // 今日递延费支出[103, 104]
  61. DELIVERYCHARGE float64 `json:"deliverycharge" xorm:"DELIVERYCHARGE"` // 今日交收手续费支出 [105,106]
  62. HOLDCHARGE float64 `json:"holdcharge" xorm:"HOLDCHARGE"` // 今日持仓过夜费支出[108,109,110]
  63. THIRDOUTAMOUNT float64 `json:"thirdoutamount" xorm:"THIRDOUTAMOUNT"` // 今日三方出金
  64. TRADEQTY int64 `json:"tradeqty" xorm:"TRADEQTY"` // 今日成交量
  65. TRADEAMOUNT float64 `json:"tradeamount" xorm:"TRADEAMOUNT"` // 今日成交金额
  66. DELIVERYAMOUNTOUT float64 `json:"deliveryamountout" xorm:"DELIVERYAMOUNTOUT"` // 今日交收货款支出
  67. DELIVERYAMOUNTIN float64 `json:"deliveryamountin" xorm:"DELIVERYAMOUNTIN"` // 今日交收货款收入
  68. DELIVERYCOMPENSATIONFEEOUT float64 `json:"deliverycompensationfeeout" xorm:"DELIVERYCOMPENSATIONFEEOUT"` // 今日交收补偿费支出
  69. DELIVERYCOMPENSATIONFEEIN float64 `json:"deliverycompensationfeein" xorm:"DELIVERYCOMPENSATIONFEEIN"` // 今日交收补偿费收入
  70. CAPITALBALANCE float64 `json:"capitalbalance" xorm:"CAPITALBALANCE"` // 本金余额[外部子账户实际出入金余额]
  71. CLOSEPL2 float64 `json:"closepl2" xorm:"CLOSEPL2"` // 平仓盈亏(逐笔)
  72. RECKONPL2 float64 `json:"reckonpl2" xorm:"RECKONPL2"` // 结算盈亏(逐笔) - 汇总取期末
  73. NETVALUE float64 `json:"netvalue" xorm:"NETVALUE"` // 净值 = 期末余额 + 所有权市值 + 仓单市值仓单市值由统计服务更新
  74. PAYCHARGE2 float64 `json:"paycharge2" xorm:"PAYCHARGE2"` // 今日手续费支出(外部配置)
  75. DEFERCHARGE float64 `json:"defercharge" xorm:"DEFERCHARGE"` // 今日递延费2支出[111, 112]
  76. FROMACCOUNTID int64 `json:"fromaccountid" xorm:"FROMACCOUNTID"` // 所属上级账户
  77. SUBLEVELPATH string `json:"sublevelpath" xorm:"SUBLEVELPATH"` // 账号层级路径(逗号分隔,首尾加逗号)
  78. OWNERSHIPVALUE float64 `json:"ownershipvalue" xorm:"OWNERSHIPVALUE"` // 所有权市值
  79. SPOTVALUE float64 `json:"spotvalue" xorm:"SPOTVALUE"` // 现货市值-统计服务更新
  80. ACCOUNTPL float64 `json:"accountpl" xorm:"ACCOUNTPL"` // 今日账户盈亏 = 今日净值-昨日净值+今日出金-今日入金-今日划转+今日授信减少-今日授信增加+今日其它授信减少-今日其它授信增加+今日三方出金-今日三方入金;由统计服务更新
  81. ACCOUNTNAME string `json:"accountname" xorm:"ACCOUNTNAME"` // 账户名称
  82. ORIDEBT float64 `json:"oridebt" xorm:"ORIDEBT"` // 期初欠款
  83. CURDEBT float64 `json:"curdebt" xorm:"CURDEBT"` // 期末欠款
  84. AVAIABLEMONEY float64 `json:"avaiablemoney" xorm:"AVAIABLEMONEY"` // 可用资金
  85. AVAIABLEOUTMONEY float64 `json:"avaiableoutmoney" xorm:"AVAIABLEOUTMONEY"` // 可出资金
  86. FLOATPL float64 `json:"floatpl" xorm:"FLOATPL"` // 浮动盈亏 - 每日有负债类商品
  87. FLOATNETVALUE float64 `json:"floatnetvalue" xorm:"FLOATNETVALUE"` // 浮动净值 = 净值 + 浮动盈亏
  88. RISKRATE float64 `json:"riskrate" xorm:"RISKRATE"` // 风险率 = 占用/ 浮动净值 或 浮动净值/占用
  89. }
  90. func (r *ReportReckondaytaaccount) calc() {
  91. }
  92. func (r *ReportReckondaytaaccount) buildSql() string {
  93. var sqlId utils.SQLVal = `
  94. select
  95. t.accountid,
  96. t.reckondate,
  97. t.userid,
  98. t.currencyid,
  99. t.changeflag,
  100. t.password,
  101. t.tradestatus,
  102. t.signstatus,
  103. t.ismain,
  104. t.parentaccountid,
  105. t.relateuserid,
  106. t.relatedaccountstatus,
  107. t.taaccounttype,
  108. t.isreckonaccount,
  109. t.ismarketaccount,
  110. t.balance,
  111. t.orifreezemargin,
  112. t.oriusedmargin,
  113. t.oriotherfreezemargin,
  114. t.orifreezecharge,
  115. t.orimortgagecredit,
  116. t.oriothercredit,
  117. t.orioutamountfreeze,
  118. t.currentbalance,
  119. t.freezemargin,
  120. t.usedmargin,
  121. t.otherfreezemargin,
  122. t.freezecharge,
  123. t.mortgagecredit,
  124. t.othercredit,
  125. t.outamountfreeze,
  126. t.inamount,
  127. t.outamount,
  128. t.paycharge,
  129. t.closepl,
  130. t.reckonpl,
  131. t.creditincrease,
  132. t.creditdecrease,
  133. t.othercreditincrease,
  134. t.othercreditdecrease,
  135. t.otherpay,
  136. t.otherincome,
  137. t.transferamount,
  138. t.outthreshold,
  139. t.clearbalance,
  140. t.dividedcharge,
  141. t.payexchcharge,
  142. t.clearpl,
  143. t.thirdinamount,
  144. t.tradecharge,
  145. t.interestcharge,
  146. t.deliverycharge,
  147. t.holdcharge,
  148. t.thirdoutamount,
  149. t.tradeqty,
  150. t.tradeamount,
  151. t.deliveryamountout,
  152. t.deliveryamountin,
  153. t.deliverycompensationfeeout,
  154. t.deliverycompensationfeein,
  155. t.capitalbalance,
  156. t.closepl2,
  157. t.reckonpl2,
  158. t.netvalue,
  159. t.paycharge2,
  160. t.defercharge,
  161. t.fromaccountid,
  162. t.sublevelpath,
  163. t.ownershipvalue,
  164. t.spotvalue,
  165. t.accountpl,
  166. t.accountname,
  167. t.oridebt,
  168. t.curdebt,
  169. t.avaiablemoney,
  170. t.avaiableoutmoney,
  171. t.floatpl,
  172. t.floatnetvalue,
  173. t.riskrate
  174. from reckon_daytaaccount t
  175. where t.reckondate = %v and t.accountid = %v
  176. `
  177. sqlId.FormatParam(r.RECKONDATE, r.ACCOUNTID)
  178. return sqlId.String()
  179. }
  180. // GetDataEx 从数据库中查询数据
  181. func (r *ReportReckondaytaaccount) GetDataEx() (interface{}, error) {
  182. e := db.GetEngine()
  183. s := e.SQL(r.buildSql())
  184. sData := make([]ReportReckondaytaaccount, 0)
  185. if err := s.Find(&sData); err != nil {
  186. return nil, err
  187. }
  188. for i := range sData {
  189. sData[i].calc()
  190. }
  191. return sData, nil
  192. }
  193. type ReportBankAccountOutInLog struct {
  194. UPDATETIME string `json:"updatetime" xorm:"UPDATETIME"` // 更新时间
  195. Executetypedisplay string `json:"executetypedisplay" xorm:"EXECUTETYPEDISPLAY"` // 申请类型
  196. AMOUNT float64 `json:"amount" xorm:"AMOUNT"` // 金额
  197. Applystatusdisplay string `json:"applystatusdisplay" xorm:"APPLYSTATUSDISPLAY"` // 申请状态
  198. APPLYSTATUS int32 `json:"applystatus" xorm:"APPLYSTATUS"` // 申请状态 - 1:待审核 2:待复审 3:初审拒绝 4:交易冻结中 5:交易解冻中 6:交易解冻扣款中 7:交易入金中 8:交易冻结/解冻/扣款中(银行发起出金时用) 9:银行出金中 10:银行入金中 11:成功 12:失败 13:银行审核中 14:账户服务入金失败; 15:账户服务解冻失败; 16:账户服务解冻扣款失败; 17:账户服务出金失败 18:复审通过 19:复审拒绝 20:提交审核,账户冻结中 21:审核拒绝,账户解冻中;22: 待审核,账户服务解冻回滚中; 23:待复审,账户服务解冻回滚中; 24: 审核通过,账户冻结金额检查中;25: 复审通过,账户冻结金额检查中;
  199. EXECUTETYPE int32 `json:"executetype" xorm:"EXECUTETYPE"` // 申请类型 - 1:出金 2:入金 3: 单边账调整:入金; 4:单边账调整:出金 5:外部母账户调整:入金 6:外部母账户调整:出金 7:外部子账户:入金 8:外部子账户:出金
  200. AccountId int64 `form:"accountid" binding:"required"` // 资金账户
  201. Tradedate string `form:"tradedate" binding:"required"` // 查询日期 日报表-yyyyMMdd 月报表-yyyyMM
  202. ReportType int `form:"reporttype" binding:"required"` // 报表类型 日报表-1 月报表-2
  203. PageEx `xorm:"extends"` // 页码信息
  204. }
  205. func (r *ReportBankAccountOutInLog) calc() {
  206. }
  207. func (r *ReportBankAccountOutInLog) buildSql() string {
  208. var sqlId utils.SQLVal = `
  209. select
  210. to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME,
  211. en1.enumdicname EXECUTETYPEDISPLAY,
  212. t.AMOUNT,
  213. en2.enumdicname APPLYSTATUSDISPLAY,
  214. t.executetype,
  215. t.applystatus
  216. from Bank_AccountOutInApply t
  217. left join enumdicitem en1
  218. on en1.enumdiccode = 'executetype'
  219. and en1.enumitemname = t.executetype
  220. left join enumdicitem en2
  221. on en2.enumdiccode = 'applystatus'
  222. and en2.enumitemname = t.applystatus
  223. where 1=1
  224. `
  225. sqlId.And("t.accountcode", strconv.Itoa(int(r.AccountId)))
  226. if r.ReportType == 1 {
  227. // 日报表
  228. sqlId.And("t.tradedate", r.Tradedate)
  229. } else {
  230. // 月报表
  231. sqlId.And("substr(t.tradedate, 0,6)", r.Tradedate)
  232. }
  233. sqlId.OrderByDesc("t.updatetime")
  234. sqlId.Page(r.Page, r.PageSize)
  235. return sqlId.String()
  236. }
  237. func (r *ReportBankAccountOutInLog) GetDataByPage() (interface{}, error, int, int, int) {
  238. sData := make([]ReportBankAccountOutInLog, 0)
  239. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  240. total := 0
  241. for i := range sData {
  242. sData[i].calc()
  243. total = sData[i].Total
  244. }
  245. return sData, err, r.Page, r.PageSize, total
  246. }
  247. type ReportReckonDayPosition struct {
  248. CURPOSITIONQTY int64 `json:"curpositionqty" xorm:"CURPOSITIONQTY"` // 当前持仓总数量
  249. FROZENQTY int64 `json:"frozenqty" xorm:"FROZENQTY"` // 持仓冻结
  250. CURHOLDERAMOUNT float64 `json:"curholderamount" xorm:"CURHOLDERAMOUNT"` // 当前持仓总金额
  251. GOODSID int32 `json:"goodsid" xorm:"GOODSID"` // 商品Id
  252. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID" form:"accountid" binding:"required"` // 账号Id
  253. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE" form:"reckondate" binding:"required"` // 日照日期(yyyyMMdd)
  254. GOODSDISPLAY string `json:"goodsdisplay" xorm:"GOODSDISPLAY"` // 商品名称
  255. DECIMALPLACE int32 `json:"decimalplace" xorm:"DECIMALPLACE"` // 报价小数位
  256. BUYORSELLDISPLAY string `json:"buyorselldisplay" xorm:"BUYORSELLDISPLAY"` // 方向
  257. AVAGEPRICEDISPLAY string `json:"avagepricedisplay" xorm:"AVAGEPRICEDISPLAY"` // 均价
  258. BUYORSELL int32 `json:"buyorsell" xorm:"'BUYORSELL'" form:"buyorsell"` // 买卖方向 0-买 1-卖
  259. ReportType int `form:"reporttype" binding:"required"` // 报表类型 日报表-1 月报表-2
  260. PageEx `xorm:"extends"` // 页码信息
  261. }
  262. func (r *ReportReckonDayPosition) calc() {
  263. }
  264. func (r *ReportReckonDayPosition) buildSql() string {
  265. var sqlId utils.SQLVal = `
  266. select p.*,
  267. en1.enumdicname buyorselldisplay,
  268. round(p.curholderamount / p.curpositionqty, g.decimalplace) avagepricedisplay
  269. from (select 0 buyorsell,
  270. t.buycurpositionqty curpositionqty,
  271. t.buyfrozenqty frozenqty,
  272. t.buycurholderamount curholderamount,
  273. t.goodsid,
  274. t.accountid,
  275. t.reckondate,
  276. g.goodscode goodsdisplay,
  277. g.decimalplace
  278. from reckon_dayposition t
  279. left join goods g
  280. on g.goodsid = t.goodsid
  281. left join market m
  282. on m.marketid = g.marketid
  283. where t.buycurpositionqty > 0
  284. and m.trademode not in (40, 41, 48, 51)
  285. and t.accountid = %v
  286. and t.reckondate = '%v'
  287. union
  288. select 1 buyorsell,
  289. t.sellcurpositionqty curpositionqty,
  290. t.sellfrozenqty frozenqty,
  291. t.sellcurholderamount sellholderamount,
  292. t.goodsid,
  293. t.accountid,
  294. t.reckondate,
  295. g.goodscode goodsdisplay,
  296. g.decimalplace
  297. from reckon_dayposition t
  298. left join goods g
  299. on g.goodsid = t.goodsid
  300. left join market m
  301. on m.marketid = g.marketid
  302. where t.sellcurpositionqty > 0
  303. and m.trademode not in (40, 41, 48, 51)
  304. and t.accountid = %v
  305. and t.reckondate = '%v' ) p
  306. left join goods g
  307. on g.goodsid = p.goodsid
  308. left join market m
  309. on m.marketid = g.marketid
  310. left join enumdicitem en1
  311. on en1.enumdiccode = 'buyOrSell'
  312. and en1.enumitemname = p.buyorsell
  313. order by g.goodscode, p.buyorsell
  314. `
  315. sqlId.FormatParam(r.ACCOUNTID, r.RECKONDATE, r.ACCOUNTID, r.RECKONDATE)
  316. sqlId.Page(r.Page, r.PageSize)
  317. return sqlId.String()
  318. }
  319. func (r *ReportReckonDayPosition) buildSql_m() string {
  320. var sqlId utils.SQLVal = `
  321. select p.*,
  322. en1.enumdicname buyorselldisplay,
  323. round(p.curholderamount / p.curpositionqty, g.decimalplace) avagepricedisplay
  324. from (select 0 buyorsell,
  325. t.buycurpositionqty curpositionqty,
  326. t.buyfrozenqty frozenqty,
  327. t.buycurholderamount curholderamount,
  328. t.goodsid,
  329. t.accountid,
  330. t.cycletype,
  331. t.cycletime,
  332. g.goodscode || '/' || g.goodsname goodsdisplay,
  333. g.decimalplace
  334. from report_tradeposition t
  335. left join goods g
  336. on g.goodsid = t.goodsid
  337. left join market m
  338. on m.marketid = g.marketid
  339. where t.buycurpositionqty > 0
  340. and m.trademode not in (40, 41, 48, 51)
  341. and t.cycletype = 1
  342. and t.cycletime = '%v'
  343. and t.accountid = %v
  344. union
  345. select 1 buyorsell,
  346. t.sellcurpositionqty curpositionqty,
  347. t.sellfrozenqty frozenqty,
  348. t.sellcurholderamount sellholderamount,
  349. t.goodsid,
  350. t.accountid,
  351. t.cycletype,
  352. t.cycletime,
  353. g.goodscode || '/' || g.goodsname goodsdisplay,
  354. g.decimalplace
  355. from report_tradeposition t
  356. left join goods g
  357. on g.goodsid = t.goodsid
  358. left join market m
  359. on m.marketid = g.marketid
  360. where t.sellcurpositionqty > 0
  361. and m.trademode not in (40, 41, 48, 51)
  362. and t.cycletype = 1
  363. and t.cycletime = '%v'
  364. and t.accountid = %v) p
  365. left join goods g
  366. on g.goodsid = p.goodsid
  367. left join market m
  368. on m.marketid = g.marketid
  369. left join enumdicitem en1
  370. on en1.enumdiccode = 'buyOrSell'
  371. and en1.enumitemname = p.buyorsell
  372. order by g.goodscode, p.buyorsell
  373. `
  374. sqlId.FormatParam(r.RECKONDATE, r.ACCOUNTID, r.RECKONDATE, r.ACCOUNTID)
  375. sqlId.Page(r.Page, r.PageSize)
  376. return sqlId.String()
  377. }
  378. func (r *ReportReckonDayPosition) GetDataByPage() (interface{}, error, int, int, int) {
  379. sData := make([]ReportReckonDayPosition, 0)
  380. sql := r.buildSql()
  381. if r.ReportType == 2 {
  382. sql = r.buildSql_m()
  383. }
  384. err := db.GetEngine().SQL(sql).Find(&sData)
  385. total := 0
  386. for i := range sData {
  387. sData[i].calc()
  388. total = sData[i].Total
  389. }
  390. return sData, err, r.Page, r.PageSize, total
  391. }
  392. type ReportTradeDetail struct {
  393. TRADETIME string `json:"tradetime" xorm:"TRADETIME"` // 时间
  394. GOODSDISPLAY string `json:"goodsdisplay" xorm:"GOODSDISPLAY"` // 商品
  395. BUYORSELLDISPLAY string `json:"buyorselldisplay" xorm:"BUYORSELLDISPLAY"` // 类型/方向
  396. TRADEQTY int64 `json:"tradeqty" xorm:"TRADEQTY"` // 数量
  397. TRADEPRICE float64 `json:"tradeprice" xorm:"TRADEPRICE"` // 价格
  398. TRADEAMOUNT float64 `json:"tradeamount" xorm:"TRADEAMOUNT"` // 成交金额
  399. CHARGE float64 `json:"charge" xorm:"CHARGE"` // 服务费
  400. TRADEID string `json:"tradeid" xorm:"TRADEID"` // 成交单号
  401. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID" form:"accountid" binding:"required"` // 资金账户
  402. GOODSID int32 `json:"goodsid" xorm:"GOODSID"` // 期货合约ID(自增ID SEQ_GOODS)
  403. DECIMALPLACE int32 `json:"decimalplace" xorm:"DECIMALPLACE"` // 报价小数位
  404. HISTRADEDATE string `json:"histradedate" xorm:"HISTRADEDATE" form:"histradedate" binding:"required"` // 查询日期 日报表-yyyyMMdd 月报表-yyyyMM
  405. BUYORSELL int32 `json:"buyorsell" xorm:"BUYORSELL"` // 买卖方向 - 0:买 1:卖
  406. BUILDTYPE int32 `json:"buildtype" xorm:"BUILDTYPE"` // 委托单据类型 - 1:建仓 2:平仓 3:先平后建
  407. ReportType int `form:"reporttype" binding:"required"` // 报表类型 日报表-1 月报表-2
  408. PageEx `xorm:"extends"` // 页码信息
  409. }
  410. func (r *ReportTradeDetail) calc() {
  411. }
  412. func (r *ReportTradeDetail) buildSql() string {
  413. var sqlId utils.SQLVal = `
  414. select
  415. to_char(t.tradetime, 'yyyy-mm-dd hh24:mi:ss') TRADETIME,
  416. g.goodscode goodsdisplay,
  417. en2.enumdicname || '/' || en1.enumdicname buyorselldisplay,
  418. t.tradeqty,
  419. t.tradeprice,
  420. t.tradeamount,
  421. t.opencharge + t.closecharge charge,
  422. t.tradeid,
  423. g.decimalplace,
  424. g.goodsid,
  425. t.accountid,
  426. t.buyorsell,
  427. t.buildtype
  428. from his_trade_tradedetail t
  429. left join goods g
  430. on g.goodsid = t.goodsid
  431. left join enumdicitem en1
  432. on en1.enumdiccode = 'buyOrSell'
  433. and en1.enumitemname = t.buyorsell
  434. left join enumdicitem en2
  435. on en2.enumdiccode = 'buildtype'
  436. and en2.enumitemname = t.buildtype
  437. where t.isvaliddata = 1
  438. `
  439. sqlId.And("t.accountid", r.ACCOUNTID)
  440. if r.ReportType == 1 {
  441. // 日报表
  442. sqlId.And("t.histradedate", r.HISTRADEDATE)
  443. } else {
  444. // 月报表
  445. sqlId.And("substr(t.histradedate, 0,6)", r.HISTRADEDATE)
  446. }
  447. sqlId.OrderByDesc("t.tradetime")
  448. sqlId.Page(r.Page, r.PageSize)
  449. return sqlId.String()
  450. }
  451. func (r *ReportTradeDetail) GetDataByPage() (interface{}, error, int, int, int) {
  452. sData := make([]ReportTradeDetail, 0)
  453. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  454. total := 0
  455. for i := range sData {
  456. sData[i].calc()
  457. total = sData[i].Total
  458. }
  459. return sData, err, r.Page, r.PageSize, total
  460. }
  461. // Reporttaaccount 资金账户报表表
  462. type Reporttaaccount struct {
  463. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID" form:"accountid" binding:"required"` // 资金账户ID
  464. CYCLETYPE int32 `json:"cycletype" xorm:"CYCLETYPE"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表
  465. CYCLETIME string `json:"cycletime" xorm:"CYCLETIME" form:"cycletime" binding:"required"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)
  466. UPDATETIME time.Time `json:"updatetime" xorm:"UPDATETIME"` // 更新时间
  467. USERID int64 `json:"userid" xorm:"USERID"` // 用户ID
  468. ISMAIN int32 `json:"ismain" xorm:"ISMAIN"` // 是否母账号 0:不是母账户 1:是母账户
  469. PARENTACCOUNTID int64 `json:"parentaccountid" xorm:"PARENTACCOUNTID"` // 所属根账户
  470. TAACCOUNTTYPE int32 `json:"taaccounttype" xorm:"TAACCOUNTTYPE"` // 外部/内部账号 - 1:外部账号 2:内部账号
  471. CURRENCYID int64 `json:"currencyid" xorm:"CURRENCYID"` // 货币ID
  472. BALANCE float64 `json:"balance" xorm:"BALANCE"` // 期初余额
  473. ORIFREEZEMARGIN float64 `json:"orifreezemargin" xorm:"ORIFREEZEMARGIN"` // 期初冻结保证金
  474. ORIUSEDMARGIN float64 `json:"oriusedmargin" xorm:"ORIUSEDMARGIN"` // 期初占用保证金
  475. ORIOTHERFREEZEMARGIN float64 `json:"oriotherfreezemargin" xorm:"ORIOTHERFREEZEMARGIN"` // 期初其他冻结保证金(出金冻结资金 交割买方冻结 申购冻结 全款买入 商城买入)
  476. ORIFREEZECHARGE float64 `json:"orifreezecharge" xorm:"ORIFREEZECHARGE"` // 期初手续费冻结
  477. ORIMORTGAGECREDIT float64 `json:"orimortgagecredit" xorm:"ORIMORTGAGECREDIT"` // 期初授信金额
  478. ORIOTHERCREDIT float64 `json:"oriothercredit" xorm:"ORIOTHERCREDIT"` // 期初其它授信金额
  479. ORIOUTAMOUNTFREEZE float64 `json:"orioutamountfreeze" xorm:"ORIOUTAMOUNTFREEZE"` // 期初出金冻结
  480. CURRENTBALANCE float64 `json:"currentbalance" xorm:"CURRENTBALANCE"` // 期末余额
  481. FREEZEMARGIN float64 `json:"freezemargin" xorm:"FREEZEMARGIN"` // 冻结保证金
  482. USEDMARGIN float64 `json:"usedmargin" xorm:"USEDMARGIN"` // 占用保证金
  483. OTHERFREEZEMARGIN float64 `json:"otherfreezemargin" xorm:"OTHERFREEZEMARGIN"` // 其他冻结保证金(出金冻结资金 交割买方冻结 申购冻结 全款买入 商城买入)
  484. FREEZECHARGE float64 `json:"freezecharge" xorm:"FREEZECHARGE"` // 手续费冻结
  485. MORTGAGECREDIT float64 `json:"mortgagecredit" xorm:"MORTGAGECREDIT"` // 授信金额
  486. OTHERCREDIT float64 `json:"othercredit" xorm:"OTHERCREDIT"` // 其它授信金额
  487. OUTAMOUNTFREEZE float64 `json:"outamountfreeze" xorm:"OUTAMOUNTFREEZE"` // 出金冻结
  488. INAMOUNT float64 `json:"inamount" xorm:"INAMOUNT"` // 本周期入金金额
  489. OUTAMOUNT float64 `json:"outamount" xorm:"OUTAMOUNT"` // 本周期出金金额
  490. PAYCHARGE float64 `json:"paycharge" xorm:"PAYCHARGE"` // 本周期手续费支出
  491. CLOSEPL float64 `json:"closepl" xorm:"CLOSEPL"` // 本周期平仓盈亏
  492. RECKONPL float64 `json:"reckonpl" xorm:"RECKONPL"` // 本周期结算盈亏
  493. CREDITINCREASE float64 `json:"creditincrease" xorm:"CREDITINCREASE"` // 本周期授信增加
  494. CREDITDECREASE float64 `json:"creditdecrease" xorm:"CREDITDECREASE"` // 本周期授信减少
  495. OTHERCREDITINCREASE float64 `json:"othercreditincrease" xorm:"OTHERCREDITINCREASE"` // 本周期其它授信增加
  496. OTHERCREDITDECREASE float64 `json:"othercreditdecrease" xorm:"OTHERCREDITDECREASE"` // 本周期其它授信减少
  497. OTHERPAY float64 `json:"otherpay" xorm:"OTHERPAY"` // 其他支出(交割付款 申购付款 全款买入 商城买入 卖家退货)
  498. OTHERINCOME float64 `json:"otherincome" xorm:"OTHERINCOME"` // 其他收入(交割收款 申购收款 全款卖出 商城卖出 买家退货 会员手续费收入)
  499. TRANSFERAMOUNT float64 `json:"transferamount" xorm:"TRANSFERAMOUNT"` // 本周期划转金额(母子账号资金划转,从账号划入为正,从账号划出为负)
  500. OUTTHRESHOLD float64 `json:"outthreshold" xorm:"OUTTHRESHOLD"` // 出金阈值
  501. CLEARBALANCE float64 `json:"clearbalance" xorm:"CLEARBALANCE"` // 清算余额 = 期末余额 - 授信金额 - 其它授信金额
  502. DIVIDEDCHARGE float64 `json:"dividedcharge" xorm:"DIVIDEDCHARGE"` // 本周期已收手续费分成
  503. PAYEXCHCHARGE float64 `json:"payexchcharge" xorm:"PAYEXCHCHARGE"` // 本周期应付交易所手续费
  504. CLEARPL float64 `json:"clearpl" xorm:"CLEARPL"` // 清算盈亏
  505. THIRDINAMOUNT float64 `json:"thirdinamount" xorm:"THIRDINAMOUNT"` // 本周期三方入金
  506. TRADECHARGE float64 `json:"tradecharge" xorm:"TRADECHARGE"` // 本周期交易手续费支出 [101,102]
  507. INTERESTCHARGE float64 `json:"interestcharge" xorm:"INTERESTCHARGE"` // 本周期递延费支出[103, 104]
  508. DELIVERYCHARGE float64 `json:"deliverycharge" xorm:"DELIVERYCHARGE"` // 本周期交收手续费支出 [105,106]
  509. HOLDCHARGE float64 `json:"holdcharge" xorm:"HOLDCHARGE"` // 本周期持仓过夜费支出[108,109, 110]
  510. THIRDOUTAMOUNT float64 `json:"thirdoutamount" xorm:"THIRDOUTAMOUNT"` // 本周期三方出金
  511. TRADEQTY int64 `json:"tradeqty" xorm:"TRADEQTY"` // 本周期成交量
  512. TRADEAMOUNT float64 `json:"tradeamount" xorm:"TRADEAMOUNT"` // 本周期成交金额
  513. DELIVERYAMOUNTOUT float64 `json:"deliveryamountout" xorm:"DELIVERYAMOUNTOUT"` // 本周期交收货款支出
  514. DELIVERYAMOUNTIN float64 `json:"deliveryamountin" xorm:"DELIVERYAMOUNTIN"` // 本周期交收货款收入
  515. DELIVERYCOMPENSATIONFEEOUT float64 `json:"deliverycompensationfeeout" xorm:"DELIVERYCOMPENSATIONFEEOUT"` // 本周期交收补偿费支出
  516. DELIVERYCOMPENSATIONFEEIN float64 `json:"deliverycompensationfeein" xorm:"DELIVERYCOMPENSATIONFEEIN"` // 本周期交收补偿费收入
  517. CAPITALBALANCE float64 `json:"capitalbalance" xorm:"CAPITALBALANCE"` // 本周期本金余额[外部子账户实际出入金余额]
  518. CLOSEPL2 float64 `json:"closepl2" xorm:"CLOSEPL2"` // 平仓盈亏(逐笔)
  519. RECKONPL2 float64 `json:"reckonpl2" xorm:"RECKONPL2"` // 结算盈亏(逐笔) - 汇总取期末
  520. NETVALUE float64 `json:"netvalue" xorm:"NETVALUE"` // 净值 = 期末余额 + 所有权市值
  521. PAYCHARGE2 float64 `json:"paycharge2" xorm:"PAYCHARGE2"` // 今日手续费支出(外部配置)
  522. DEFERCHARGE float64 `json:"defercharge" xorm:"DEFERCHARGE"` // 本周期递延费2支出[111, 112]
  523. FROMACCOUNTID int64 `json:"fromaccountid" xorm:"FROMACCOUNTID"` // 所属上级账户
  524. SUBLEVELPATH string `json:"sublevelpath" xorm:"SUBLEVELPATH"` // 账号层级路径(逗号分隔,首尾加逗号)
  525. RELATEUSERID int64 `json:"relateuserid" xorm:"RELATEUSERID"` // 关联用户
  526. OWNERSHIPVALUE float64 `json:"ownershipvalue" xorm:"OWNERSHIPVALUE"` // 所有权市值
  527. SPOTVALUE float64 `json:"spotvalue" xorm:"SPOTVALUE"` // 现货市值-统计服务更新
  528. ACCOUNTPL float64 `json:"accountpl" xorm:"ACCOUNTPL"` // 今日账户盈亏 = 今日净值-昨日净值+今日出金-今日入金-今日划转+今日授信减少-今日授信增加+今日其它授信减少-今日其它授信增加+今日三方出金-今日三方入金;由统计服务更新
  529. ACCOUNTNAME string `json:"accountname" xorm:"ACCOUNTNAME"` // 账户名称
  530. ORIDEBT float64 `json:"oridebt" xorm:"ORIDEBT"` // 期初欠款【期初】
  531. CURDEBT float64 `json:"curdebt" xorm:"CURDEBT"` // 期末欠款【期末】
  532. AVAIABLEMONEY float64 `json:"avaiablemoney" xorm:"AVAIABLEMONEY"` // 可用资金【期末】
  533. AVAIABLEOUTMONEY float64 `json:"avaiableoutmoney" xorm:"AVAIABLEOUTMONEY"` // 可出资金【期末】
  534. FLOATPL float64 `json:"floatpl" xorm:"FLOATPL"` // 浮动盈亏【期末】
  535. FLOATNETVALUE float64 `json:"floatnetvalue" xorm:"FLOATNETVALUE"` // 浮动净值【期末】
  536. RISKRATE float64 `json:"riskrate" xorm:"RISKRATE"` // 风险率【期末】
  537. }
  538. func (r *Reporttaaccount) calc() {
  539. }
  540. func (r *Reporttaaccount) buildSql() string {
  541. var sqlId utils.SQLVal = `
  542. select
  543. t.accountid,
  544. t.cycletype,
  545. t.cycletime,
  546. t.updatetime,
  547. t.userid,
  548. t.ismain,
  549. t.parentaccountid,
  550. t.taaccounttype,
  551. t.currencyid,
  552. t.balance,
  553. t.orifreezemargin,
  554. t.oriusedmargin,
  555. t.oriotherfreezemargin,
  556. t.orifreezecharge,
  557. t.orimortgagecredit,
  558. t.oriothercredit,
  559. t.orioutamountfreeze,
  560. t.currentbalance,
  561. t.freezemargin,
  562. t.usedmargin,
  563. t.otherfreezemargin,
  564. t.freezecharge,
  565. t.mortgagecredit,
  566. t.othercredit,
  567. t.outamountfreeze,
  568. t.inamount,
  569. t.outamount,
  570. t.paycharge,
  571. t.closepl,
  572. t.reckonpl,
  573. t.creditincrease,
  574. t.creditdecrease,
  575. t.othercreditincrease,
  576. t.othercreditdecrease,
  577. t.otherpay,
  578. t.otherincome,
  579. t.transferamount,
  580. t.outthreshold,
  581. t.clearbalance,
  582. t.dividedcharge,
  583. t.payexchcharge,
  584. t.clearpl,
  585. t.thirdinamount,
  586. t.tradecharge,
  587. t.interestcharge,
  588. t.deliverycharge,
  589. t.holdcharge,
  590. t.thirdoutamount,
  591. t.tradeqty,
  592. t.tradeamount,
  593. t.deliveryamountout,
  594. t.deliveryamountin,
  595. t.deliverycompensationfeeout,
  596. t.deliverycompensationfeein,
  597. t.capitalbalance,
  598. t.closepl2,
  599. t.reckonpl2,
  600. t.netvalue,
  601. t.paycharge2,
  602. t.defercharge,
  603. t.fromaccountid,
  604. t.sublevelpath,
  605. t.relateuserid,
  606. t.ownershipvalue,
  607. t.spotvalue,
  608. t.accountpl,
  609. t.accountname,
  610. t.oridebt,
  611. t.curdebt,
  612. t.avaiablemoney,
  613. t.avaiableoutmoney,
  614. t.floatpl,
  615. t.floatnetvalue,
  616. t.riskrate
  617. from report_taaccount t
  618. where t.cycletype = 1 and t.cycletime = '%v' and t.accountid = %v
  619. `
  620. sqlId.FormatParam(r.CYCLETIME, r.ACCOUNTID)
  621. return sqlId.String()
  622. }
  623. // GetDataEx 从数据库中查询数据
  624. func (r *Reporttaaccount) GetDataEx() (interface{}, error) {
  625. e := db.GetEngine()
  626. s := e.SQL(r.buildSql())
  627. sData := make([]Reporttaaccount, 0)
  628. if err := s.Find(&sData); err != nil {
  629. return nil, err
  630. }
  631. for i := range sData {
  632. sData[i].calc()
  633. }
  634. return sData, nil
  635. }