ermcp3Report.go 133 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695
  1. /**
  2. * @Author: zou.yingbin
  3. * @Create : 2021/4/16 18:22
  4. * @Modify : 2021/4/16 18:22
  5. */
  6. package models
  7. import (
  8. "fmt"
  9. "mtp2_if/db"
  10. "mtp2_if/mtpcache"
  11. "mtp2_if/utils"
  12. "strings"
  13. )
  14. // Ermcp3ReportOPLog 报表合同操作记录通用查询
  15. type Ermcp3ReportOPLog struct {
  16. LOGID string `json:"logid" xorm:"'lOGID'"` // 流水ID(604+Unix秒时间戳(10位)+xxxxxx)
  17. BIZTYPE int32 `json:"biztype" xorm:"'BIZTYPE'"` // 业务类型 - 1:套保计划 2:现货合同
  18. OPERATELOGTYPE int32 `json:"operatelogtype" xorm:"'OPERATELOGTYPE'"` // 操作流水类型 -
  19. RELATEDID string `json:"relatedid" xorm:"'RELATEDID'"` // 现货合同ID\套保计划
  20. LOGVALUE string `json:"logvalue" xorm:"'LOGVALUE'"` // 数值
  21. LOGDATETIME string `json:"logdatetime" xorm:"'LOGDATETIME'"` // 流水日期(时间)
  22. TRADEDATE string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd)
  23. APPLYID int64 `json:"applyid" xorm:"'APPLYID'"` // 操作人
  24. CONTRACTTYPE int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 现货合同类型 - 1:采购 -1:销售
  25. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构ID
  26. RELATEDNO string `json:"relatedno" xorm:"'RELATEDNO'"` // 合同编号
  27. UNITID int32 `json:"-" xorm:"'UNITID'"` // 现货商品单位id
  28. ENUMDICNAME string `json:"enumdicName"` // 单位名称
  29. OPTYPENAME string `json:"optypename"` // 流水类型名称
  30. LOGTYPENAME string `json:"logtypename"` // 合同类型(名称)
  31. APPLYNAME string `json:"applyname"` // 操作人名称
  32. BRANDNAME string `json:"brandname" xorm:"'brandname'"` // 品牌名称
  33. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种id
  34. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  35. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  36. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'wrstandardid'"` // 品类id
  37. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'wrstandardcode'"` // 品类代码
  38. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'wrstandardname'"` // 品类名称
  39. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 品牌id
  40. LogTypeFilter string `json:"-"` // 查询日志类型, 逗号隔开(如 1,2,4)
  41. }
  42. // Calc 处理数据
  43. func (r *Ermcp3ReportOPLog) Calc() {
  44. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  45. r.APPLYNAME = mtpcache.GetUserNameByUserId(r.APPLYID)
  46. if r.APPLYNAME == "" {
  47. // APPLYID 可能存的是loginId
  48. r.APPLYNAME = mtpcache.GetLoginCodeByLoginId(r.APPLYID)
  49. }
  50. //1:点价价格 2:点价数量 3:结算量 4:其它费用 5:追加保证金 6溢短金额 7:调整金额 8:付款金额 9:收款金额 10:退款金额
  51. //11:收票金额 12:开票金额 13:提交审核(合同) 14:审核通过(合同) 15:审核拒绝(合同) 16:合同撤回 17:提交审核(计划)
  52. //18:审核通过(计划) 19:审核拒绝(计划) 20:计划撤回 21:正常完结(合同) 22:异常终止(合同) 23:退还保证金
  53. //24:采购入库 25:销售出库 26:生产入库 27:生产出库
  54. // 数据库注释与返回值映身关系: 结算量->确定量, 收款->收款金额, 退款->退款金额, 付款->付款金额, 收票->收票金额, 开票->开票金额
  55. sDes := []string{"点价价格", "点价数量", "确定量", "其它费用", "追加保证金", "溢短金额", "调整金额", "付款", "收款", "退款",
  56. "收票", "开票", "提交审核(合同)", "审核通过(合同)", "审核拒绝(合同)", "合同撤回", "提交审核(计划)",
  57. "审核通过(计划)", "审核拒绝(计划)", "计划撤回", "正常完结(合同)", "异常终止(合同)", "退还保证金",
  58. "采购入库", "销售出库", "生产入库", "生产出库"}
  59. if r.OPERATELOGTYPE >= 1 && r.OPERATELOGTYPE <= 27 {
  60. r.OPTYPENAME = sDes[r.OPERATELOGTYPE-1]
  61. }
  62. // 收款款项:采购合同的退款、销售合同的收款应用正数显示,付款款项:采购合同的付款、销售合同的退款应用负数显示
  63. if r.CONTRACTTYPE == 1 {
  64. r.LOGTYPENAME = "采购"
  65. // 采购合同/退款 正数
  66. if r.OPERATELOGTYPE == 10 && len(r.LOGVALUE) > 0 {
  67. if r.LOGVALUE[0] == '-' {
  68. r.LOGVALUE = r.LOGVALUE[1:]
  69. }
  70. }
  71. // 采购合同/付款 负数
  72. if r.OPERATELOGTYPE == 8 && len(r.LOGVALUE) > 0 {
  73. if r.LOGVALUE[0] != '-' {
  74. r.LOGVALUE = "-" + r.LOGVALUE
  75. }
  76. }
  77. } else if r.CONTRACTTYPE == -1 {
  78. r.LOGTYPENAME = "销售"
  79. // 销售合同/收款 正数
  80. if r.OPERATELOGTYPE == 9 && len(r.LOGVALUE) > 0 {
  81. if r.LOGVALUE[0] == '-' {
  82. r.LOGVALUE = r.LOGVALUE[1:]
  83. }
  84. }
  85. // 销售合同/退款 负数
  86. if r.OPERATELOGTYPE == 10 && len(r.LOGVALUE) > 0 {
  87. if r.LOGVALUE[0] != '-' {
  88. r.LOGVALUE = "-" + r.LOGVALUE
  89. }
  90. }
  91. }
  92. // 去除多余的0,最后4个0
  93. if r.LOGVALUE[len(r.LOGVALUE)-4:] == "0000" {
  94. r.LOGVALUE = r.LOGVALUE[:len(r.LOGVALUE)-4]
  95. }
  96. if r.BIZTYPE == 1 {
  97. r.LOGTYPENAME += "计划"
  98. } else if r.BIZTYPE == 2 {
  99. r.LOGTYPENAME += "合同"
  100. }
  101. }
  102. func (r *Ermcp3ReportOPLog) buildSql() string {
  103. var sqlId utils.SQLVal = "SELECT to_char(t.LOGID) LOGID," +
  104. " t.BIZTYPE," +
  105. " t.OPERATELOGTYPE," +
  106. " to_char(t.RELATEDID) RELATEDID," +
  107. " t.LOGVALUE," +
  108. " to_char(t.LOGDATETIME, 'yyyy-mm-dd hh24:mi:ss') LOGDATETIME," +
  109. " t.TRADEDATE," +
  110. " t.APPLYID," +
  111. " t.CONTRACTTYPE," +
  112. " t.USERID," +
  113. " t.wrstandardid," +
  114. " s.contractno relatedno," +
  115. " s.spotgoodsbrandid," +
  116. " g.deliverygoodsid," +
  117. " g.deliverygoodscode," +
  118. " g.deliverygoodsname," +
  119. " g.goodsunitid unitid," +
  120. " gb.dgfactoryitemvalue brandname," +
  121. " s.accountid," +
  122. " w.wrstandardcode," +
  123. " w.wrstandardname" +
  124. " FROM ERMCP_CONTRACTOPERATELOG t" +
  125. " inner join ermcp_spotcontract s" +
  126. " on t.RELATEDID = s.spotcontractid" +
  127. " left join deliverygoods g" +
  128. " on t.deliverygoodsid = g.deliverygoodsid" +
  129. " left join wrstandard w on t.wrstandardid=w.wrstandardid" +
  130. " left join dgfactoryitem gb" +
  131. " on s.spotgoodsbrandid = gb.dgfactoryitemid" +
  132. " WHERE t.biztype = 2"
  133. // 筛选条件
  134. sqlId.And("t.userid", r.USERID)
  135. sqlId.And("t.TRADEDATE", r.TRADEDATE)
  136. sqlId.AndEx("t.deliverygoodsid", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  137. sqlId.AndEx("t.wrstandardid", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  138. sqlId.AndEx("s.spotgoodsbrandid", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  139. if r.LogTypeFilter != "" {
  140. sqlId.Join(fmt.Sprintf(" and t.OPERATELOGTYPE in(%v)", r.LogTypeFilter))
  141. }
  142. return sqlId.String()
  143. }
  144. // GetDataEx 获取日志记录
  145. func (r *Ermcp3ReportOPLog) GetDataEx() (interface{}, error) {
  146. sData := make([]Ermcp3ReportOPLog, 0)
  147. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  148. for i := range sData {
  149. sData[i].Calc()
  150. }
  151. return sData, err
  152. }
  153. // Ermcp3ReckonAreaSpotSub 现货日报表(作废)
  154. type Ermcp3ReckonAreaSpotSub struct {
  155. RECKONDATE string `json:"reckondate" xorm:"'RECKONDATE'" form:"reckondate" binding:"required"` // 日照时期(yyyyMMdd)
  156. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'" form:"userid" binding:"required"` // 账户所属机构
  157. ACCOUNTID int64 `json:"accountid" xorm:"'ACCOUNTID'" form:"accountid"` // 期货账户ID
  158. WRFACTORTYPEID int64 `json:"wrfactortypeid" xorm:"'WRFACTORTYPEID'"` // 仓单要素类型ID(212+Unix秒时间戳(10位)+xxxxxx)
  159. DELIVERYGOODSID int64 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'" form:"deliverygoodsid"` // 现货品种ID
  160. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'" form:"wrstandardid"` // 现货品类ID
  161. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'" form:"spotgoodsbrandid"` // 现货品牌ID
  162. ORIBUYPRICEDQTY float64 `json:"oribuypricedqty" xorm:"'ORIBUYPRICEDQTY'"` // 期初采购合同已定价数量
  163. ORISELLPRICEDQTY float64 `json:"orisellpricedqty" xorm:"'ORISELLPRICEDQTY'"` // 期初销售合同已定价数量
  164. BUYPRICEDQTY float64 `json:"buypricedqty" xorm:"'BUYPRICEDQTY'"` // 采购合同已定价数量
  165. SELLPRICEDQTY float64 `json:"sellpricedqty" xorm:"'SELLPRICEDQTY'"` // 销售合同已定价数量
  166. TOTALSPOTQTY float64 `json:"totalspotqty" xorm:"'TOTALSPOTQTY'"` // 现货头寸总量 = 采购合同已定价数量 - 销售合同已定价数量
  167. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  168. TODAYBUYRECKONQTY float64 `json:"todaybuyreckonqty" xorm:"'TODAYBUYRECKONQTY'"` // 今日采购合同已确定数量
  169. TODAYSELLRECKONQTY float64 `json:"todaysellreckonqty" xorm:"'TODAYSELLRECKONQTY'"` // 今日销售合同已确定数量
  170. ORIBUYPRICEDAMOUNT float64 `json:"oribuypricedamount" xorm:"'ORIBUYPRICEDAMOUNT'"` // 期初采购合同已定价金额
  171. ORISELLPRICEDAMOUNT float64 `json:"orisellpricedamount" xorm:"'ORISELLPRICEDAMOUNT'"` // 期初销售合同已定价金额
  172. BUYPRICEDAMOUNT float64 `json:"buypricedamount" xorm:"'BUYPRICEDAMOUNT'"` // 采购合同已定价金额
  173. SELLPRICEDAMOUNT float64 `json:"sellpricedamount" xorm:"'SELLPRICEDAMOUNT'"` // 销售合同已定价金额
  174. ORIBUYRECKONQTY float64 `json:"oribuyreckonqty" xorm:"'ORIBUYRECKONQTY'"` // 期初采购合同交收量
  175. ORISELLRECKONQTY float64 `json:"orisellreckonqty" xorm:"'ORISELLRECKONQTY'"` // 期初销售合同交收量
  176. ORIBUYCONTRACTINQTY float64 `json:"oribuycontractinqty" xorm:"'ORIBUYCONTRACTINQTY'"` // 期初采购合同入库量
  177. ORISELLCONTRACTOUTQTY float64 `json:"orisellcontractoutqty" xorm:"'ORISELLCONTRACTOUTQTY'"` // 期初销售合同出库量
  178. ORIPRODUCEINQTY float64 `json:"oriproduceinqty" xorm:"'ORIPRODUCEINQTY'"` // 期初生产入库量
  179. ORIPRODUCEOUTQTY float64 `json:"oriproduceoutqty" xorm:"'ORIPRODUCEOUTQTY'"` // 期初生产出库量
  180. BUYRECKONQTY float64 `json:"buyreckonqty" xorm:"'BUYRECKONQTY'"` // 采购合同交收量
  181. SELLRECKONQTY float64 `json:"sellreckonqty" xorm:"'SELLRECKONQTY'"` // 销售合同交收量
  182. BUYCONTRACTINQTY float64 `json:"buycontractinqty" xorm:"'BUYCONTRACTINQTY'"` // 采购合同入库量
  183. SELLCONTRACTOUTQTY float64 `json:"sellcontractoutqty" xorm:"'SELLCONTRACTOUTQTY'"` // 销售合同出库量
  184. PRODUCEINQTY float64 `json:"produceinqty" xorm:"'PRODUCEINQTY'"` // 生产入库量
  185. PRODUCEOUTQTY float64 `json:"produceoutqty" xorm:"'PRODUCEOUTQTY'"` // 生产出库量
  186. ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 现货商品单位名称
  187. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'wrstandardname'"` // 品类名称
  188. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'wrstandardcode'"` // 品类代码
  189. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'deliverygoodscode'"` // 现货商品代码
  190. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'deliverygoodsname'"` // 现货商品名称
  191. GOODSUNITID int32 `json:"goodsunitid" xorm:"'goodsunitid'"` // 现货商品单位id
  192. BRANDNAME string `json:"brandname" xorm:"'brandname'"` // 品牌名称
  193. ACCOUNTNAME string `json:"accountname" xorm:"'accountname'"` // 所属机构名称
  194. BIZTYPE int32 `json:"biztype" xorm:"'BIZTYPE'"` // 业务类型 1-套保 2-套利
  195. CURRENCYID int32 `json:"currencyid" xorm:"'CURRENCYID'"` // 币种id
  196. CURRENCYNAME string `json:"currencyname"` // 币种名称
  197. TOTALBUYPRICEDQTY float64 `json:"totalbuypricedqty"` // 采购定价量 = 期末 - 期初
  198. TOTALSELLPRICEDQTY float64 `json:"totalsellpricedqty"` // 销售定价量 = 期末 - 期初
  199. BeginDate string `json:"-"` // 开始日期
  200. EndDate string `json:"-"` // 结束日期
  201. }
  202. func (r *Ermcp3ReckonAreaSpotSub) calc() {
  203. r.TOTALBUYPRICEDQTY = r.BUYPRICEDQTY - r.ORIBUYPRICEDQTY
  204. r.TOTALSELLPRICEDQTY = r.SELLPRICEDQTY - r.ORISELLPRICEDQTY
  205. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  206. }
  207. func (r *Ermcp3ReckonAreaSpotSub) buildSql() string {
  208. var sqlId utils.SQLVal = "SELECT t.ORIBUYRECKONQTY," +
  209. " t.ORISELLRECKONQTY," +
  210. " t.ORIBUYCONTRACTINQTY," +
  211. " t.ORISELLCONTRACTOUTQTY," +
  212. " t.ORIPRODUCEINQTY," +
  213. " t.ORIPRODUCEOUTQTY," +
  214. " t.BUYRECKONQTY," +
  215. " t.SELLRECKONQTY," +
  216. " t.BUYCONTRACTINQTY," +
  217. " t.SELLCONTRACTOUTQTY," +
  218. " t.PRODUCEINQTY," +
  219. " t.PRODUCEOUTQTY," +
  220. " t.Reckondate," +
  221. " t.AREAUSERID," +
  222. " t.ACCOUNTID," +
  223. " t.WRFACTORTYPEID," +
  224. " t.DELIVERYGOODSID," +
  225. " t.WRSTANDARDID," +
  226. " t.SPOTGOODSBRANDID," +
  227. " t.ORIBUYPRICEDQTY," +
  228. " t.ORISELLPRICEDQTY," +
  229. " t.BUYPRICEDQTY," +
  230. " t.SELLPRICEDQTY," +
  231. " t.TOTALSPOTQTY," +
  232. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  233. " t.TODAYBUYRECKONQTY," +
  234. " t.TODAYSELLRECKONQTY," +
  235. " t.ORIBUYPRICEDAMOUNT," +
  236. " t.ORISELLPRICEDAMOUNT," +
  237. " t.BUYPRICEDAMOUNT," +
  238. " t.SELLPRICEDAMOUNT," +
  239. " t.currencyid," +
  240. " t.biztype," +
  241. " w.wrstandardname," +
  242. " w.wrstandardcode," +
  243. " g.deliverygoodscode," +
  244. " g.deliverygoodsname," +
  245. " g.goodsunitid," +
  246. " d.dgfactoryitemvalue brandname," +
  247. " u.accountname," +
  248. " e.enumdicname" +
  249. " FROM RECKON_ERMCP_AREASPOTSUB t" +
  250. " LEFT JOIN WRSTANDARD w" +
  251. " on t.wrstandardid = w.wrstandardid" +
  252. " LEFT JOIN DELIVERYGOODS g" +
  253. " on t.deliverygoodsid = g.deliverygoodsid" +
  254. " LEFT JOIN DGFACTORYITEM d" +
  255. " on t.spotgoodsbrandid = d.dgfactoryitemid" +
  256. " LEFT JOIN USERACCOUNT u" +
  257. " on t.areauserid = u.userid" +
  258. " LEFT JOIN ENUMDICITEM e" +
  259. " on g.goodsunitid = e.enumitemname" +
  260. " and e.enumdiccode = 'goodsunit'" +
  261. " and e.enumitemstatus = 1" +
  262. " WHERE 1 = 1"
  263. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  264. sqlId.AndEx("t.ACCOUNTID", r.ACCOUNTID, r.ACCOUNTID > 0)
  265. sqlId.AndEx("t.deliverygoodsid", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  266. sqlId.AndEx("t.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  267. sqlId.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  268. if r.RECKONDATE != "" {
  269. sqlId.And("t.RECKONDATE", r.RECKONDATE)
  270. } else if r.BeginDate != "" && r.BeginDate == r.EndDate {
  271. sqlId.And("t.RECKONDATE", r.BeginDate)
  272. } else {
  273. if r.BeginDate != "" {
  274. sqlId.BiggerOrEq("t.RECKONDATE", r.BeginDate)
  275. }
  276. if r.EndDate != "" {
  277. sqlId.LessOrEq("t.RECKONDATE", r.EndDate)
  278. }
  279. }
  280. sqlId.Join(" order by t.RECKONDATE")
  281. return sqlId.String()
  282. }
  283. // GetDataEx 获取现货日报表
  284. func (r *Ermcp3ReckonAreaSpotSub) GetDataEx() (interface{}, error) {
  285. sData := make([]Ermcp3ReckonAreaSpotSub, 0)
  286. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  287. for i := range sData {
  288. sData[i].calc()
  289. }
  290. return sData, err
  291. }
  292. // Ermcp3ReportAreaSpotSub 现货分类月报表(作废)
  293. type Ermcp3ReportAreaSpotSub struct {
  294. ORIBUYRECKONQTY float64 `json:"oribuyreckonqty" xorm:"'ORIBUYRECKONQTY'"` // 期初采购合同交收量【期初】
  295. ORISELLRECKONQTY float64 `json:"orisellreckonqty" xorm:"'ORISELLRECKONQTY'"` // 期初销售合同交收量【期初】
  296. ORIBUYCONTRACTINQTY float64 `json:"oribuycontractinqty" xorm:"'ORIBUYCONTRACTINQTY'"` // 期初采购合同入库量【期初】
  297. ORISELLCONTRACTOUTQTY float64 `json:"orisellcontractoutqty" xorm:"'ORISELLCONTRACTOUTQTY'"` // 期初销售合同出库量【期初】
  298. ORIPRODUCEINQTY float64 `json:"oriproduceinqty" xorm:"'ORIPRODUCEINQTY'"` // 期初生产入库量【期初】
  299. ORIPRODUCEOUTQTY float64 `json:"oriproduceoutqty" xorm:"'ORIPRODUCEOUTQTY'"` // 期初生产出库量【期初】
  300. BUYRECKONQTY float64 `json:"buyreckonqty" xorm:"'BUYRECKONQTY'"` // 采购合同交收量【期末】
  301. SELLRECKONQTY float64 `json:"sellreckonqty" xorm:"'SELLRECKONQTY'"` // 销售合同交收量【期末】
  302. BUYCONTRACTINQTY float64 `json:"buycontractinqty" xorm:"'BUYCONTRACTINQTY'"` // 采购合同入库量【期末】
  303. SELLCONTRACTOUTQTY float64 `json:"sellcontractoutqty" xorm:"'SELLCONTRACTOUTQTY'"` // 销售合同出库量【期末】
  304. PRODUCEINQTY float64 `json:"produceinqty" xorm:"'PRODUCEINQTY'"` // 生产入库量【期末】
  305. PRODUCEOUTQTY float64 `json:"produceoutqty" xorm:"'PRODUCEOUTQTY'"` // 生产出库量【期末】
  306. CYCLETIME string `json:"cycletime" xorm:"'CYCLETIME'" form:"cycletime"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  307. CYCLETYPE int32 `json:"cycletype" xorm:"'CYCLETYPE'" form:"cycletype"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  308. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'" form:"userid" binding:"required"` // 账户所属机构
  309. ACCOUNTID int64 `json:"accountid" xorm:"'ACCOUNTID'" form:"accountid"` // 期货账户ID
  310. WRFACTORTYPEID int64 `json:"wrfactortypeid" xorm:"'WRFACTORTYPEID'"` // 仓单要素类型ID(212+Unix秒时间戳(10位)+xxxxxx)
  311. DELIVERYGOODSID int64 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'" form:"deliverygoodsid"` // 现货品种ID
  312. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'" form:"wrstandardid"` // 品类ID
  313. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'" form:"spotgoodsbrandid"` // 现货品牌ID
  314. ORIBUYPRICEDQTY float64 `json:"oribuypricedqty" xorm:"'ORIBUYPRICEDQTY'"` // 期初采购合同已定价数量【期初】
  315. ORISELLPRICEDQTY float64 `json:"orisellpricedqty" xorm:"'ORISELLPRICEDQTY'"` // 期初销售合同已定价数量【期初】
  316. BUYPRICEDQTY float64 `json:"buypricedqty" xorm:"'BUYPRICEDQTY'"` // 采购合同已定价数量【期末】
  317. SELLPRICEDQTY float64 `json:"sellpricedqty" xorm:"'SELLPRICEDQTY'"` // 销售合同已定价数量【期末】
  318. TOTALSPOTQTY float64 `json:"totalspotqty" xorm:"'TOTALSPOTQTY'"` // 现货头寸总量 = 采购合同已定价数量 - 销售合同已定价数量【期末】
  319. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  320. TODAYBUYRECKONQTY float64 `json:"todaybuyreckonqty" xorm:"'TODAYBUYRECKONQTY'"` // 今日采购合同已确定数量【汇总】
  321. TODAYSELLRECKONQTY float64 `json:"todaysellreckonqty" xorm:"'TODAYSELLRECKONQTY'"` // 今日销售合同已确定数量【汇总】
  322. ORIBUYPRICEDAMOUNT float64 `json:"oribuypricedamount" xorm:"'ORIBUYPRICEDAMOUNT'"` // 期初采购合同已定价金额【期初】
  323. ORISELLPRICEDAMOUNT float64 `json:"orisellpricedamount" xorm:"'ORISELLPRICEDAMOUNT'"` // 期初销售合同已定价金额【期初】
  324. BUYPRICEDAMOUNT float64 `json:"buypricedamount" xorm:"'BUYPRICEDAMOUNT'"` // 采购合同已定价金额【期末】
  325. SELLPRICEDAMOUNT float64 `json:"sellpricedamount" xorm:"'SELLPRICEDAMOUNT'"` // 销售合同已定价金额【期末】
  326. ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 现货商品单位名称
  327. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'wrstandardname'"` // 品类名称
  328. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'wrstandardcode'"` // 品类代码
  329. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'deliverygoodscode'"` // 现货商品代码
  330. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'deliverygoodsname'"` // 现货商品名称
  331. GOODSUNITID int32 `json:"goodsunitid" xorm:"'goodsunitid'"` // 现货商品单位id
  332. BRANDNAME string `json:"brandname" xorm:"'brandname'"` // 品牌名称
  333. ACCOUNTNAME string `json:"accountname" xorm:"'accountname'"` // 所属机构名称
  334. BIZTYPE int32 `json:"biztype" xorm:"'BIZTYPE'"` // 业务类型 1-套保 2-套利
  335. CURRENCYID int32 `json:"currencyid" xorm:"'CURRENCYID'"` // 币种id
  336. CURRENCYNAME string `json:"currencyname"` // 币种名称
  337. TOTALBUYPRICEDQTY float64 `json:"totalbuypricedqty"` // 采购定价量 = 期末 - 期初
  338. TOTALSELLPRICEDQTY float64 `json:"totalsellpricedqty"` // 销售定价量 = 期末 - 期初
  339. }
  340. func (r *Ermcp3ReportAreaSpotSub) calc() {
  341. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  342. r.TOTALBUYPRICEDQTY = r.BUYPRICEDQTY - r.ORIBUYPRICEDQTY
  343. r.TOTALSELLPRICEDQTY = r.SELLPRICEDQTY - r.ORISELLPRICEDQTY
  344. }
  345. func (r *Ermcp3ReportAreaSpotSub) buildSql() string {
  346. var sqlId utils.SQLVal = "SELECT t.ORIBUYRECKONQTY," +
  347. " t.ORISELLRECKONQTY," +
  348. " t.ORIBUYCONTRACTINQTY," +
  349. " t.ORISELLCONTRACTOUTQTY," +
  350. " t.ORIPRODUCEINQTY," +
  351. " t.ORIPRODUCEOUTQTY," +
  352. " t.BUYRECKONQTY," +
  353. " t.SELLRECKONQTY," +
  354. " t.BUYCONTRACTINQTY," +
  355. " t.SELLCONTRACTOUTQTY," +
  356. " t.PRODUCEINQTY," +
  357. " t.PRODUCEOUTQTY," +
  358. " t.CYCLETIME," +
  359. " t.CYCLETYPE," +
  360. " t.AREAUSERID," +
  361. " t.ACCOUNTID," +
  362. " t.WRFACTORTYPEID," +
  363. " t.DELIVERYGOODSID," +
  364. " t.WRSTANDARDID," +
  365. " t.SPOTGOODSBRANDID," +
  366. " t.ORIBUYPRICEDQTY," +
  367. " t.ORISELLPRICEDQTY," +
  368. " t.BUYPRICEDQTY," +
  369. " t.SELLPRICEDQTY," +
  370. " t.TOTALSPOTQTY," +
  371. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  372. " t.TODAYBUYRECKONQTY," +
  373. " t.TODAYSELLRECKONQTY," +
  374. " t.ORIBUYPRICEDAMOUNT," +
  375. " t.ORISELLPRICEDAMOUNT," +
  376. " t.BUYPRICEDAMOUNT," +
  377. " t.SELLPRICEDAMOUNT," +
  378. " t.currencyid," +
  379. " t.biztype," +
  380. " w.wrstandardname," +
  381. " w.wrstandardcode," +
  382. " g.deliverygoodscode," +
  383. " g.deliverygoodsname," +
  384. " g.goodsunitid," +
  385. " d.dgfactoryitemvalue brandname," +
  386. " u.accountname," +
  387. " e.enumdicname" +
  388. " FROM REPORT_ERMCP_AREASPOTSUB t" +
  389. " LEFT JOIN WRSTANDARD w on t.wrstandardid=w.wrstandardid" +
  390. " LEFT JOIN DELIVERYGOODS g on t.deliverygoodsid=g.deliverygoodsid" +
  391. " LEFT JOIN DGFACTORYITEM d on t.spotgoodsbrandid=d.dgfactoryitemid" +
  392. " LEFT JOIN USERACCOUNT u on t.areauserid=u.userid" +
  393. " LEFT JOIN ENUMDICITEM e on g.goodsunitid=e.enumitemname and e.enumdiccode='goodsunit' and e.enumitemstatus=1" +
  394. " WHERE 1 = 1"
  395. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  396. sqlId.And("t.cycletype", r.CYCLETYPE)
  397. sqlId.And("t.cycletime", r.CYCLETIME)
  398. sqlId.AndEx("t.ACCOUNTID", r.ACCOUNTID, r.ACCOUNTID > 0)
  399. sqlId.AndEx("t.deliverygoodsid", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  400. sqlId.AndEx("t.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  401. sqlId.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  402. return sqlId.String()
  403. }
  404. // GetDataEx 获取现货分类报表
  405. func (r *Ermcp3ReportAreaSpotSub) GetDataEx() (interface{}, error) {
  406. sData := make([]Ermcp3ReportAreaSpotSub, 0)
  407. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  408. for i := range sData {
  409. sData[i].calc()
  410. }
  411. return sData, err
  412. }
  413. // Ermcp3ExpourseReport 敞口报表
  414. type Ermcp3ExpourseReport struct {
  415. RECKONDATE string `json:"reckondate" xorm:"'RECKONDATE'"` // 日照时期(yyyyMMdd)
  416. CYCLETIME string `json:"cycletime" xorm:"'CYCLETIME'"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  417. CYCLETYPE int32 `json:"cycletype" xorm:"'CYCLETYPE'"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  418. ORITOTALSPOTQTY float64 `json:"oritotalspotqty" xorm:"'ORITOTALSPOTQTY'"` // 期初现货头寸总量
  419. ORITOTALFUTUREQTY float64 `json:"oritotalfutureqty" xorm:"'ORITOTALFUTUREQTY'"` // 期初期货头寸总量
  420. ORITOTALEXPOSURE float64 `json:"oritotalexposure" xorm:"'ORITOTALEXPOSURE'"` // 期初实时总敞口
  421. ORINEEDHEDGEEXPOSOURE float64 `json:"orineedhedgeexposoure" xorm:"'ORINEEDHEDGEEXPOSOURE'"` // 期初应套保敞口
  422. ORIHEDGEQTY float64 `json:"orihedgeqty" xorm:"'ORIHEDGEQTY'"` // 期初套保量
  423. ORIARBITRAGEQTY float64 `json:"oriarbitrageqty" xorm:"'ORIARBITRAGEQTY'"` // 期初套利量
  424. ORINEEDHEDGEQTY float64 `json:"orineedhedgeqty" xorm:"'ORINEEDHEDGEQTY'"` // 期初应套保量
  425. ORINEEDARBITRAGEQTY float64 `json:"orineedarbitrageqty" xorm:"'ORINEEDARBITRAGEQTY'"` // 期初应套利量
  426. ORITOTALNEEDHEDGEQTY float64 `json:"oritotalneedhedgeqty" xorm:"'ORITOTALNEEDHEDGEQTY'"` // 期初应套保总量
  427. MIDDLEGOODSID int64 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'"` // 套保品种ID
  428. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构\交易用户ID
  429. ACCOUNTID int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 期货账户ID (作废, 默认为0)
  430. ORIBUYPLANQTY float64 `json:"oribuyplanqty" xorm:"'ORIBUYPLANQTY'"` // 期初采购计划数量
  431. ORIBUYPRICEDQTY float64 `json:"oribuypricedqty" xorm:"'ORIBUYPRICEDQTY'"` // 期初采购合同已定价数量
  432. ORISELLPLANQTY float64 `json:"orisellplanqty" xorm:"'ORISELLPLANQTY'"` // 期初销售计划数量
  433. ORISELLPRICEDQTY float64 `json:"orisellpricedqty" xorm:"'ORISELLPRICEDQTY'"` // 期初销售合同已定价数量
  434. ORIBUYFUTUREQTY float64 `json:"oribuyfutureqty" xorm:"'ORIBUYFUTUREQTY'"` // 期初买入期货数量
  435. ORISELLFUTUREQTY float64 `json:"orisellfutureqty" xorm:"'ORISELLFUTUREQTY'"` // 期初卖出期货数量
  436. BUYPLANQTY float64 `json:"buyplanqty" xorm:"'BUYPLANQTY'"` // 采购计划数量
  437. BUYPRICEDQTY float64 `json:"buypricedqty" xorm:"'BUYPRICEDQTY'"` // 采购合同已定价数量
  438. SELLPLANQTY float64 `json:"sellplanqty" xorm:"'SELLPLANQTY'"` // 销售计划数量
  439. SELLPRICEDQTY float64 `json:"sellpricedqty" xorm:"'SELLPRICEDQTY'"` // 销售合同已定价数量
  440. BUYFUTUREQTY float64 `json:"buyfutureqty" xorm:"'BUYFUTUREQTY'"` // 买入期货数量
  441. SELLFUTUREQTY float64 `json:"sellfutureqty" xorm:"'SELLFUTUREQTY'"` // 卖出期货数量
  442. TOTALSPOTQTY float64 `json:"totalspotqty" xorm:"'TOTALSPOTQTY'"` // 现货头寸总量
  443. TOTALFUTUREQTY float64 `json:"totalfutureqty" xorm:"'TOTALFUTUREQTY'"` // 期货头寸总量(期货总量)
  444. TOTALEXPOSURE float64 `json:"totalexposure" xorm:"'TOTALEXPOSURE'"` // 实时总敞口(总敞口)
  445. TOTALHEDGERATIO float64 `json:"totalhedgeratio" xorm:"'TOTALHEDGERATIO'"` // 敞口比例
  446. TOTALNEEDHEDGEQTY float64 `json:"totalneedhedgeqty" xorm:"'TOTALNEEDHEDGEQTY'"` // 应套保总量(现货应套保总量)
  447. NEEDHEDGEEXPOSOURE float64 `json:"needhedgeexposoure" xorm:"'NEEDHEDGEEXPOSOURE'"` // 应套保敞口(套保敞口)
  448. NEEDHEDGERATIO float64 `json:"needhedgeratio" xorm:"'NEEDHEDGERATIO'"` // 应套保敞口比例
  449. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  450. HEDGEQTY float64 `json:"hedgeqty" xorm:"'HEDGEQTY'"` // 套保量
  451. ARBITRAGEQTY float64 `json:"arbitrageqty" xorm:"'ARBITRAGEQTY'"` // 套利量
  452. NEEDHEDGEQTY float64 `json:"needhedgeqty" xorm:"'NEEDHEDGEQTY'"` // 应套保量
  453. NEEDARBITRAGEQTY float64 `json:"needarbitrageqty" xorm:"'NEEDARBITRAGEQTY'"` // 应套利量
  454. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保商品名称
  455. MIDDLEGOODSCODE string `json:"middlgoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保商品代码
  456. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 单位id
  457. MGNEEDHEDGERATIO float64 `json:"mgneedhedgeratio" xorm:"'MGNEEDHEDGERATIO'"` // 套保比例(套保品的)
  458. NEEDARBITRAGERATIO float64 `json:"needarbitrageratio" xorm:"'NEEDARBITRAGERATIO'"` // 套利比例(套保品的)
  459. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  460. ACCOUNTNAME string `json:"accountname"` // 机构名称
  461. DiffSpotQty float64 `json:"diffspotqty"` // 变动量(现货)
  462. DiffMgQtyA float64 `json:"diffmgqtya"` // 套保变动量
  463. DiffMgQtyB float64 `json:"diffmgqtyb"` // 套利变动量
  464. DiffFutuQty float64 `json:"difffutuqty"` // 变动量(期货)
  465. DiffQty float64 `json:"diffqty"` // 变动量(总敞口)
  466. DiffHedgeQty float64 `json:"diffhedgeqty"` // 变动量(现货应套保总量)
  467. DiffExposure float64 `json:"diffexposure"` // 变动量(套保敞口)
  468. QueryType int32 `json:"-"` // 查询类型 1-报表
  469. BeginDate string `json:"begindate"` // 开始交易日
  470. EndDate string `json:"enddate"` // 结束交易日
  471. }
  472. func (r *Ermcp3ExpourseReport) calc() {
  473. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  474. r.ACCOUNTNAME = mtpcache.GetUserNameByUserId(r.AREAUSERID)
  475. // 变动量(现货) = 现货头寸总量 - 期初现货头寸总量
  476. r.DiffSpotQty = r.TOTALSPOTQTY - r.ORITOTALSPOTQTY
  477. // 套保变动量 = 套保量 - 期初套保量
  478. r.DiffMgQtyA = r.HEDGEQTY - r.ORIHEDGEQTY
  479. // 套利变动量 = 套利量 - 期初套利量
  480. r.DiffMgQtyB = r.ARBITRAGEQTY - r.ORIARBITRAGEQTY
  481. // 变动量(期货) = (买入 - 买入期初) - (卖出 - 卖出期初)
  482. r.DiffFutuQty = (r.BUYFUTUREQTY - r.ORIBUYFUTUREQTY) - (r.SELLFUTUREQTY - r.ORISELLFUTUREQTY)
  483. // 变动量(敞口) = 实时敞口 - 期初实时敞口
  484. r.DiffQty = r.TOTALEXPOSURE - r.ORITOTALEXPOSURE
  485. // 变动量(现货应套保总量)
  486. r.DiffHedgeQty = r.TOTALNEEDHEDGEQTY - r.ORITOTALNEEDHEDGEQTY
  487. // 变动量(套保敞口)
  488. r.DiffExposure = r.NEEDHEDGEEXPOSOURE - r.ORINEEDHEDGEEXPOSOURE
  489. }
  490. func (r *Ermcp3ExpourseReport) buildSql() string {
  491. if r.CYCLETYPE == 0 {
  492. return r.buildSqlDay()
  493. }
  494. return r.buildSqlCycle()
  495. }
  496. func (r *Ermcp3ExpourseReport) buildSqlDay() string {
  497. var sqlId utils.SQLVal = "SELECT t.ORITOTALSPOTQTY," +
  498. " t.ORITOTALFUTUREQTY," +
  499. " t.ORITOTALEXPOSURE," +
  500. " t.ORINEEDHEDGEEXPOSOURE," +
  501. " t.ORIHEDGEQTY," +
  502. " t.ORIARBITRAGEQTY," +
  503. " t.ORINEEDHEDGEQTY," +
  504. " t.ORINEEDARBITRAGEQTY," +
  505. " t.ORITOTALNEEDHEDGEQTY," +
  506. " t.RECKONDATE," +
  507. " t.MIDDLEGOODSID," +
  508. " t.AREAUSERID," +
  509. " t.ACCOUNTID," +
  510. " t.ORIBUYPLANQTY," +
  511. " t.ORIBUYPRICEDQTY," +
  512. " t.ORISELLPLANQTY," +
  513. " t.ORISELLPRICEDQTY," +
  514. " t.ORIBUYFUTUREQTY," +
  515. " t.ORISELLFUTUREQTY," +
  516. " t.BUYPLANQTY," +
  517. " t.BUYPRICEDQTY," +
  518. " t.SELLPLANQTY," +
  519. " t.SELLPRICEDQTY," +
  520. " t.BUYFUTUREQTY," +
  521. " t.SELLFUTUREQTY," +
  522. " t.TOTALSPOTQTY," +
  523. " t.TOTALFUTUREQTY," +
  524. " t.TOTALEXPOSURE," +
  525. " t.TOTALHEDGERATIO," +
  526. " t.TOTALNEEDHEDGEQTY," +
  527. " t.NEEDHEDGEEXPOSOURE," +
  528. " t.NEEDHEDGERATIO," +
  529. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  530. " t.HEDGEQTY," +
  531. " t.ARBITRAGEQTY," +
  532. " t.NEEDHEDGEQTY," +
  533. " t.NEEDARBITRAGEQTY," +
  534. " g.middlegoodsname," +
  535. " g.middlegoodscode," +
  536. " g.goodsunitid unitid," +
  537. " g.needhedgeratio MGNEEDHEDGERATIO," +
  538. " g.needarbitrageratio" +
  539. " FROM RECKON_ERMCP_AREAEXPOSURE t" +
  540. " LEFT JOIN ERMS_MIDDLEGOODS g" +
  541. " on t.middlegoodsid = g.middlegoodsid" +
  542. " WHERE 1 = 1"
  543. sqlId.AndEx("t.AREAUSERID", r.AREAUSERID, r.AREAUSERID > 0)
  544. if r.QueryType == 1 {
  545. sqlId.And("t.RECKONDATE", r.RECKONDATE)
  546. } else if r.QueryType == 2 {
  547. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  548. }
  549. return sqlId.String()
  550. }
  551. func (r *Ermcp3ExpourseReport) buildSqlCycle() string {
  552. var sqlId utils.SQLVal = "SELECT t.cycletime," +
  553. " t.cycletype," +
  554. " t.ORITOTALSPOTQTY," +
  555. " t.ORITOTALFUTUREQTY," +
  556. " t.ORITOTALEXPOSURE," +
  557. " t.ORINEEDHEDGEEXPOSOURE," +
  558. " t.ORIHEDGEQTY," +
  559. " t.ORIARBITRAGEQTY," +
  560. " t.ORINEEDHEDGEQTY," +
  561. " t.ORINEEDARBITRAGEQTY," +
  562. " t.ORITOTALNEEDHEDGEQTY," +
  563. " t.MIDDLEGOODSID," +
  564. " t.AREAUSERID," +
  565. " t.ACCOUNTID," +
  566. " t.ORIBUYPLANQTY," +
  567. " t.ORIBUYPRICEDQTY," +
  568. " t.ORISELLPLANQTY," +
  569. " t.ORISELLPRICEDQTY," +
  570. " t.ORIBUYFUTUREQTY," +
  571. " t.ORISELLFUTUREQTY," +
  572. " t.BUYPLANQTY," +
  573. " t.BUYPRICEDQTY," +
  574. " t.SELLPLANQTY," +
  575. " t.SELLPRICEDQTY," +
  576. " t.BUYFUTUREQTY," +
  577. " t.SELLFUTUREQTY," +
  578. " t.TOTALSPOTQTY," +
  579. " t.TOTALFUTUREQTY," +
  580. " t.TOTALEXPOSURE," +
  581. " t.TOTALHEDGERATIO," +
  582. " t.TOTALNEEDHEDGEQTY," +
  583. " t.NEEDHEDGEEXPOSOURE," +
  584. " t.NEEDHEDGERATIO," +
  585. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  586. " t.HEDGEQTY," +
  587. " t.ARBITRAGEQTY," +
  588. " t.NEEDHEDGEQTY," +
  589. " t.NEEDARBITRAGEQTY," +
  590. " g.middlegoodsname," +
  591. " g.middlegoodscode," +
  592. " g.goodsunitid unitid," +
  593. " g.needhedgeratio MGNEEDHEDGERATIO," +
  594. " g.needarbitrageratio" +
  595. " FROM REPORT_ERMCP_AREAEXPOSURE t" +
  596. " LEFT JOIN ERMS_MIDDLEGOODS g" +
  597. " on t.middlegoodsid = g.middlegoodsid" +
  598. " WHERE 1 = 1"
  599. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  600. sqlId.And("t.CYCLETYPE", r.CYCLETYPE)
  601. sqlId.And("t.CYCLETIME", r.CYCLETIME)
  602. return sqlId.String()
  603. }
  604. // GetDataEx 获取敞报表
  605. func (r *Ermcp3ExpourseReport) GetDataEx() (interface{}, error) {
  606. sData := make([]Ermcp3ExpourseReport, 0)
  607. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  608. for i := range sData {
  609. sData[i].calc()
  610. sData[i].BeginDate = r.BeginDate
  611. sData[i].EndDate = r.EndDate
  612. }
  613. return sData, err
  614. }
  615. // Ermcp3ExposureContractDetail 敞口合同明细
  616. type Ermcp3ExposureContractDetail struct {
  617. MIDDLEGOODSID int32 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'"` // 套保品种id
  618. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货商品id
  619. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 折算系数(套保品种)
  620. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构id
  621. SPOTCONTRACTID string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` // 合同id
  622. CONTRACTNO string `json:"contractno" xorm:"'CONTRACTNO'"` // 合同编号
  623. CONTRACTTYPE int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 现货合同类型 - 1:采购 -1:销售
  624. BIZTYPE int32 `json:"biztype" xorm:"'BIZTYPE'"` // 业务类型 1-套保 2-套利
  625. BUYUSERID int64 `json:"buyuserid" xorm:"'BUYUSERID'"` // 采购方id
  626. SELLUSERID int64 `json:"selluserid" xorm:"'SELLUSERID'"` // 销售方id
  627. QTY float64 `json:"qty" xorm:"'QTY'"` // 合同量
  628. PRICEDQTY float64 `json:"pricedqty" xorm:"'PRICEDQTY'"` // 已定价量
  629. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类id
  630. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  631. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  632. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 品类单位id
  633. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保品种名称
  634. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保品种代码
  635. GOODSUNITID int32 `json:"-" xorm:"'GOODSUNITID'"` // 套保品种单位id
  636. TRADEDATE string `json:"tradedate" xorm:"'tradedate'"` // 交易日
  637. NEEDHEDGERATIO float64 `json:"needhedgeratio" xorm:"'NEEDHEDGERATIO'"` // 应套保比例
  638. NEEDARBITRAGERATIO float64 `json:"needarbitrageratio" xorm:"'NEEDARBITRAGERATIO'"` // 应套利比例
  639. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  640. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货商品代码
  641. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货商品名称
  642. CONVERTFACTOR float64 `json:"convertfactor" xorm:"'CONVERTFACTOR'"` // 标仓系数
  643. TODAYPRICEDQTY float64 `json:"todaypricedqty" xorm:"'TODAYPRICEDQTY'"` // 今定价量
  644. EnumdicName string `json:"enumdicname"` // 单位名称
  645. BUYUSERNAME string `json:"buyusername"` // 采购方名称
  646. SELLUSERNAME string `json:"sellusername"` // 销售方名称
  647. CurQty float64 `json:"curqty"` // 今定价量
  648. DiffQty float64 `json:"diffqty"` // 套保品种今变动量
  649. DiffHedgeQty float64 `json:"diffhedgeqty"` // 套保变动量
  650. DiffNeedHedgeQty float64 `json:"diffneedhedgeqty"` // 应套保变动量
  651. BeginDate string `json:"-"` // 开始交易日
  652. EndDate string `json:"-"` // 结束交易日
  653. }
  654. func (r *Ermcp3ExposureContractDetail) calc() {
  655. r.CurQty = r.QTY
  656. r.DiffQty = r.QTY * r.CONVERTRATIO
  657. r.DiffHedgeQty = r.TODAYPRICEDQTY * r.CONVERTRATIO * r.CONVERTFACTOR
  658. if r.BIZTYPE == 1 {
  659. r.DiffNeedHedgeQty = r.TODAYPRICEDQTY * r.CONVERTRATIO * r.CONVERTFACTOR * r.NEEDHEDGERATIO
  660. } else {
  661. r.DiffNeedHedgeQty = r.TODAYPRICEDQTY * r.CONVERTRATIO * r.CONVERTFACTOR * r.NEEDARBITRAGERATIO
  662. }
  663. r.EnumdicName = mtpcache.GetEnumDicitemName(r.UNITID)
  664. r.BUYUSERNAME = mtpcache.GetUserNameByUserId(r.BUYUSERID)
  665. r.SELLUSERNAME = mtpcache.GetUserNameByUserId(r.SELLUSERID)
  666. }
  667. func (r *Ermcp3ExposureContractDetail) buildSql() string {
  668. var sqlId utils.SQLVal = "select t.middlegoodsid," +
  669. " t.deliverygoodsid," +
  670. " t.convertratio," +
  671. " s.userid," +
  672. " to_char(s.spotcontractid) spotcontractid," +
  673. " s.contractno," +
  674. " s.contracttype," +
  675. " s.biztype," +
  676. " s.buyuserid," +
  677. " s.selluserid," +
  678. " s.qty," +
  679. " s.pricedqty," +
  680. " s.wrstandardid," +
  681. " s.reckondate tradedate," +
  682. " s.todaypricedqty," +
  683. " w.wrstandardname," +
  684. " w.wrstandardcode," +
  685. " w.unitid," +
  686. " w.convertfactor," +
  687. " mg.middlegoodsname," +
  688. " mg.middlegoodscode," +
  689. " mg.goodsunitid," +
  690. " mg.needhedgeratio," +
  691. " mg.needarbitrageratio," +
  692. " dg.dgfactoryitemid brandid," +
  693. " dg.dgfactoryitemvalue brandName," +
  694. " g.deliverygoodscode," +
  695. " g.deliverygoodsname" +
  696. " from erms2_wrsconvertdetail t" +
  697. " inner join reckon_ermcp_spotcontract s" +
  698. " on t.deliverygoodsid = s.deliverygoodsid" +
  699. " left join erms_middlegoods mg" +
  700. " on t.middlegoodsid = mg.middlegoodsid" +
  701. " left join wrstandard w" +
  702. " on s.wrstandardid = w.wrstandardid" +
  703. " left join dgfactoryitem dg" +
  704. " on s.spotgoodsbrandid = dg.dgfactoryitemid" +
  705. " left join deliverygoods g" +
  706. " on s.deliverygoodsid = g.deliverygoodsid" +
  707. " where t.wrstandardid = 0" +
  708. " and s.contractstatus in (2, 3)"
  709. sqlId.AndEx("s.reckondate", r.TRADEDATE, len(r.TRADEDATE) > 0)
  710. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  711. sqlId.Join(fmt.Sprintf(" and s.reckondate >= '%v' and s.reckondate <= '%v'", r.BeginDate, r.EndDate))
  712. }
  713. sqlId.And("t.middlegoodsid", r.MIDDLEGOODSID)
  714. sqlId.Join(fmt.Sprintf(" and %v in(s.userid, s.tradeuserid)", r.USERID))
  715. return sqlId.String()
  716. }
  717. // GetDataEx 获取敞口合同明细
  718. func (r *Ermcp3ExposureContractDetail) GetDataEx() (interface{}, error) {
  719. sData := make([]Ermcp3ExposureContractDetail, 0)
  720. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  721. for i := range sData {
  722. sData[i].calc()
  723. }
  724. return sData, err
  725. }
  726. // Ermcp3ExposureHedgeplanDetail 敞口套保计划明细
  727. type Ermcp3ExposureHedgeplanDetail struct {
  728. MIDDLEGOODSID int32 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'"` // 套保品种id
  729. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货商品id
  730. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 折算系数(套保品种)
  731. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构id
  732. HEDGEPLANID string `json:"hedgeplanid" xorm:"'HEDGEPLANID'"` // 套保计划id
  733. HEDGEPLANNO string `json:"hedgeplanno" xorm:"'HEDGEPLANNO'"` // 套保计划名称
  734. BIZTYPE int32 `json:"biztype" xorm:"'BIZTYPE'"` // 业务类型(期现用途,业务类型就是期现用途) 1-套保 2-套利
  735. QTY float64 `json:"qty" xorm:"'QTY'"` // 合同量
  736. PRICEDQTY float64 `json:"pricedqty" xorm:"'PRICEDQTY'"` // 已定价量
  737. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类id
  738. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  739. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  740. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 品类单位id
  741. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保品种名称
  742. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保品种代码
  743. GOODSUNITID int32 `json:"-" xorm:"'GOODSUNITID'"` // 套保品种单位id
  744. TRADEDATE string `json:"tradedate" xorm:"'tradedate'"` // 交易日(登记日)
  745. NEEDHEDGERATIO float64 `json:"needhedgeratio" xorm:"'NEEDHEDGERATIO'"` // 应套保比例
  746. NEEDARBITRAGERATIO float64 `json:"needarbitrageratio" xorm:"'NEEDARBITRAGERATIO'"` // 应套利比例
  747. CONTRACTTYPE int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 计划类型 - 1:采购 -1:销售
  748. HEDGEPLANSTATUS int32 `json:"hedgeplanstatus" xorm:"'HEDGEPLANSTATUS'"` // 套保计划状态 - 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
  749. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货商品代码
  750. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货商吕名称
  751. DGUNITID int32 `json:"dgunitid" xorm:"'DGUNITID'"` // 现货商品单位id
  752. EnumdicName string `json:"enumdicname"` // 单位名称(品类)
  753. MGUNITIDNAME string `json:"mgunitidname"` // 单位名称(套保商品)
  754. DGUNITIDNAME string `json:"dgunitidname"` // 单位名称(现货商品)
  755. CurQty float64 `json:"-"` // 今定价量
  756. DiffQty float64 `json:"diffqty"` // 套保品种今变动量
  757. ChangQty float64 `json:"changqty"` // 应套保总量变化量
  758. BeginDate string `json:"-"`
  759. EndDate string `json:"-"`
  760. }
  761. func (r *Ermcp3ExposureHedgeplanDetail) calc() {
  762. r.EnumdicName = mtpcache.GetEnumDicitemName(r.UNITID)
  763. r.MGUNITIDNAME = mtpcache.GetEnumDicitemName(r.GOODSUNITID)
  764. r.DGUNITIDNAME = mtpcache.GetEnumDicitemName(r.DGUNITID)
  765. r.CurQty = r.QTY
  766. r.DiffQty = r.QTY * r.CONVERTRATIO
  767. r.ChangQty = r.QTY * r.NEEDHEDGERATIO * r.CONVERTRATIO
  768. }
  769. func (r *Ermcp3ExposureHedgeplanDetail) buildSql() string {
  770. var sqlId utils.SQLVal = "select t.middlegoodsid," +
  771. " t.deliverygoodsid," +
  772. " t.convertratio," +
  773. " s.areauserid userid," +
  774. " to_char(s.hedgeplanid) hedgeplanid," +
  775. " s.hedgeplanno," +
  776. " s.contracttype," +
  777. " s.biztype," +
  778. " s.planqty qty," +
  779. " s.wrstandardid," +
  780. " s.audittradedate tradedate," +
  781. " s.hedgeplanstatus," +
  782. " w.wrstandardname," +
  783. " w.wrstandardcode," +
  784. " w.unitid," +
  785. " g.deliverygoodscode," +
  786. " g.deliverygoodsname," +
  787. " g.goodsunitid," +
  788. " mg.middlegoodsname," +
  789. " mg.middlegoodscode," +
  790. " mg.goodsunitid," +
  791. " mg.needhedgeratio," +
  792. " mg.needarbitrageratio" +
  793. " from erms2_wrsconvertdetail t" +
  794. " inner join ermcp_hedgeplan s" +
  795. " on t.deliverygoodsid = s.deliverygoodsid" +
  796. " left join erms_middlegoods mg" +
  797. " on t.middlegoodsid = mg.middlegoodsid" +
  798. " left join wrstandard w" +
  799. " on s.wrstandardid = w.wrstandardid" +
  800. " left join deliverygoods g on s.deliverygoodsid=g.deliverygoodsid" +
  801. " where t.wrstandardid = 0" +
  802. " and s.hedgeplanstatus in (2, 3)"
  803. sqlId.AndEx("s.audittradedate", r.TRADEDATE, len(r.TRADEDATE) > 0)
  804. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  805. sqlId.Join(fmt.Sprintf(" and s.audittradedate >= '%v' and s.audittradedate <= '%v'", r.BeginDate, r.EndDate))
  806. }
  807. sqlId.And("t.middlegoodsid", r.MIDDLEGOODSID)
  808. sqlId.Join(fmt.Sprintf(" and %v in(s.areauserid, s.tradeuserid)", r.USERID))
  809. return sqlId.String()
  810. }
  811. // GetDataEx 获取敞口套保计划明细
  812. func (r *Ermcp3ExposureHedgeplanDetail) GetDataEx() (interface{}, error) {
  813. sData := make([]Ermcp3ExposureHedgeplanDetail, 0)
  814. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  815. for i := range sData {
  816. sData[i].calc()
  817. }
  818. return sData, err
  819. }
  820. // Ermcp3AreaSpotPLReport 现货报表(日/月/周/季/年)
  821. type Ermcp3AreaSpotPLReport struct {
  822. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照时期(yyyyMMdd)
  823. CYCLETIME string `json:"cycletime" xorm:"'CYCLETIME'"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  824. CYCLETYPE int32 `json:"cycletype" xorm:"'CYCLETYPE'"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  825. AREAUSERID int64 `json:"areauserid" xorm:"AREAUSERID"` // 所属机构\交易用户ID
  826. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID"` // 期货账户ID (作废, 默认为0)
  827. WRFACTORTYPEID int64 `json:"wrfactortypeid" xorm:"WRFACTORTYPEID"` // 仓单要素类型ID(212+Unix秒时间戳(10位)+xxxxxx)
  828. CURRENCYID int32 `json:"currencyid" xorm:"CURRENCYID"` // 结算币种ID【原值】
  829. BIZTYPE int32 `json:"biztype" xorm:"BIZTYPE"` // 业务类型 - 1:套保 2:套利
  830. DELIVERYGOODSID int64 `json:"deliverygoodsid" xorm:"DELIVERYGOODSID"` // 现货品种ID
  831. WRSTANDARDID int64 `json:"wrstandardid" xorm:"WRSTANDARDID"` // 现货品类ID
  832. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"SPOTGOODSBRANDID"` // 现货品牌ID
  833. ORIBUYQTY float64 `json:"oribuyqty" xorm:"ORIBUYQTY"` // 期初采购总量
  834. ORIBUYAMOUNT float64 `json:"oribuyamount" xorm:"ORIBUYAMOUNT"` // 期初采购总额
  835. ORISELLQTY float64 `json:"orisellqty" xorm:"ORISELLQTY"` // 期初销售总量
  836. ORISELLAMOUNT float64 `json:"orisellamount" xorm:"ORISELLAMOUNT"` // 期初销售总额
  837. ORIQTY float64 `json:"oriqty" xorm:"ORIQTY"` // 期初量
  838. ORIAVERAGEPRICE float64 `json:"oriaverageprice" xorm:"ORIAVERAGEPRICE"` // 期初均价
  839. ORIAMOUNT float64 `json:"oriamount" xorm:"ORIAMOUNT"` // 期初额
  840. TODAYBUYQTY float64 `json:"todaybuyqty" xorm:"TODAYBUYQTY"` // 今日采购量(今采购量)
  841. TODAYBUYAMOUNT float64 `json:"todaybuyamount" xorm:"TODAYBUYAMOUNT"` // 今日采购额(今采购额)
  842. TODAYBUYAVERAGEPRICE float64 `json:"todaybuyaverageprice" xorm:"TODAYBUYAVERAGEPRICE"` // 今日采购均价
  843. TODAYSELLQTY float64 `json:"todaysellqty" xorm:"TODAYSELLQTY"` // 今日销售量(今销售量)
  844. TODAYSELLAMOUNT float64 `json:"todaysellamount" xorm:"TODAYSELLAMOUNT"` // 今日销售额(今销售额)
  845. TODAYSELLAVERAGEPRICE float64 `json:"todaysellaverageprice" xorm:"TODAYSELLAVERAGEPRICE"` // 今日销售均价
  846. CURBUYQTY float64 `json:"curbuyqty" xorm:"CURBUYQTY"` // 期末采购总量
  847. CURBUYAMOUNT float64 `json:"curbuyamount" xorm:"CURBUYAMOUNT"` // 期末采购总额
  848. CURSELLQTY float64 `json:"cursellqty" xorm:"CURSELLQTY"` // 期末销售总量
  849. CURSELLAMOUNT float64 `json:"cursellamount" xorm:"CURSELLAMOUNT"` // 期末销售总额
  850. CURQTY float64 `json:"curqty" xorm:"CURQTY"` // 期末量
  851. CURAVERAGEPRICE float64 `json:"curaverageprice" xorm:"CURAVERAGEPRICE"` // 期末均价
  852. CURAMOUNT float64 `json:"curamount" xorm:"CURAMOUNT"` // 期末额
  853. CURSPOTPRICE float64 `json:"curspotprice" xorm:"CURSPOTPRICE"` // 参考市价(最新价)
  854. CURMARKETVALUE float64 `json:"curmarketvalue" xorm:"CURMARKETVALUE"` // 参考市值(期末市值)
  855. ACTUALPL float64 `json:"actualpl" xorm:"ACTUALPL"` // 现货损益
  856. FLOATPL float64 `json:"floatpl" xorm:"FLOATPL"` // 浮动损益
  857. UPDATETIME string `json:"updatetime" xorm:"UPDATETIME"` // 更新时间
  858. TODAYINQTY float64 `json:"todayinqty" xorm:"TODAYINQTY"` // 今日入库量(今入库量)
  859. TODAYOUTQTY float64 `json:"todayoutqty" xorm:"TODAYOUTQTY"` // 今日出库量(今出库量)
  860. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货商品代码
  861. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货商品名称
  862. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODSUNITID'"` // 现货商品单位id
  863. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  864. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  865. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 品类单位id
  866. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  867. EnumdicName string `json:"enumdicname"` // 现货商品单位名称
  868. UNITIDNAME string `json:"unitidname"` // 品类单位名称
  869. ACCOUNTNAME string `json:"accountname"` // 机构名称
  870. CURRENCYNAME string `json:"currencyname"` // 币种名称
  871. QueryType int32 `json:"-"` // 查询类型 1-报表 2-报表明细
  872. BeginDate string `json:"begindate"` // 开始交易日
  873. EndDate string `json:"enddate"` // 结束交易日
  874. }
  875. func (r *Ermcp3AreaSpotPLReport) calc() {
  876. r.EnumdicName = mtpcache.GetEnumDicitemName(r.GOODSUNITID)
  877. r.UNITIDNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  878. r.ACCOUNTNAME = mtpcache.GetUserNameByUserId(r.AREAUSERID)
  879. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  880. if r.CURQTY > 0 {
  881. r.CURAVERAGEPRICE = r.CURAMOUNT / r.CURQTY
  882. }
  883. if r.ORIQTY > 0 {
  884. r.ORIAVERAGEPRICE = r.ORIAMOUNT / r.ORIQTY
  885. }
  886. if r.TODAYBUYQTY > 0 {
  887. r.TODAYBUYAVERAGEPRICE = r.TODAYBUYAMOUNT / r.TODAYBUYQTY
  888. }
  889. if r.TODAYSELLQTY > 0 {
  890. r.TODAYSELLAVERAGEPRICE = r.TODAYSELLAMOUNT / r.TODAYSELLQTY
  891. }
  892. }
  893. func (r *Ermcp3AreaSpotPLReport) buildSql() string {
  894. // 日报表
  895. if r.QueryType == 1 && r.CYCLETYPE == 0 {
  896. return r.buildSqlDay()
  897. }
  898. // 日报表明细
  899. if r.QueryType == 2 && r.CYCLETYPE == 0 {
  900. return r.buildSqlDayDetail()
  901. }
  902. return r.buildSqlCycle()
  903. }
  904. // buildSqlDay 现货日报表查询语句
  905. func (r *Ermcp3AreaSpotPLReport) buildSqlDay() string {
  906. var sqlId utils.SQLVal = "select a.*," +
  907. " g.deliverygoodscode," +
  908. " g.deliverygoodsname," +
  909. " g.goodsunitid" +
  910. " from (SELECT t.RECKONDATE," +
  911. " t.AREAUSERID," +
  912. " t.CURRENCYID," +
  913. " t.DELIVERYGOODSID," +
  914. " sum(t.ORIBUYQTY) ORIBUYQTY," +
  915. " sum(t.ORIBUYAMOUNT) ORIBUYAMOUNT," +
  916. " sum(t.ORISELLQTY) ORISELLQTY," +
  917. " sum(t.ORISELLAMOUNT) ORISELLAMOUNT," +
  918. " sum(t.ORIQTY) ORIQTY," +
  919. " sum(t.ORIAMOUNT) ORIAMOUNT," +
  920. " sum(t.TODAYBUYQTY) TODAYBUYQTY," +
  921. " sum(t.TODAYBUYAMOUNT) TODAYBUYAMOUNT," +
  922. " sum(t.TODAYSELLQTY) TODAYSELLQTY," +
  923. " sum(t.TODAYSELLAMOUNT) TODAYSELLAMOUNT," +
  924. " sum(t.CURBUYQTY) CURBUYQTY," +
  925. " sum(t.CURBUYAMOUNT) CURBUYAMOUNT," +
  926. " sum(t.CURSELLQTY) CURSELLQTY," +
  927. " sum(t.CURSELLAMOUNT) CURSELLAMOUNT," +
  928. " sum(t.CURQTY) CURQTY," +
  929. " sum(t.CURAMOUNT) CURAMOUNT," +
  930. " max(t.CURSPOTPRICE) CURSPOTPRICE," +
  931. " sum(t.CURMARKETVALUE) CURMARKETVALUE," +
  932. " sum(t.ACTUALPL) ACTUALPL," +
  933. " sum(t.FLOATPL) FLOATPL," +
  934. " sum(t.TODAYINQTY) TODAYINQTY," +
  935. " sum(t.TODAYOUTQTY) TODAYOUTQTY" +
  936. " FROM RECKON_ERMCP_AREASPOTPL t" +
  937. " WHERE 1=1 %v" +
  938. " GROUP BY t.RECKONDATE," +
  939. " t.AREAUSERID," +
  940. " t.CURRENCYID," +
  941. " t.DELIVERYGOODSID) a" +
  942. " LEFT JOIN DELIVERYGOODS g" +
  943. " on a.deliverygoodsid = g.deliverygoodsid"
  944. var sqlParam utils.SQLVal
  945. sqlParam.And("t.AREAUSERID", r.AREAUSERID)
  946. if r.QueryType == 1 {
  947. sqlParam.And("t.RECKONDATE", r.RECKONDATE)
  948. } else if r.QueryType == 2 {
  949. sqlParam.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  950. }
  951. sqlId.FormatParam(sqlParam.String())
  952. return sqlId.String()
  953. }
  954. func (r *Ermcp3AreaSpotPLReport) buildSqlDayDetail() string {
  955. var sqlId utils.SQLVal = "select a.*," +
  956. " g.deliverygoodscode," +
  957. " g.deliverygoodsname," +
  958. " g.goodsunitid," +
  959. " w.wrstandardcode," +
  960. " w.wrstandardname," +
  961. " w.unitid," +
  962. " dg.dgfactoryitemvalue brandName" +
  963. " from (SELECT t.RECKONDATE," +
  964. " t.AREAUSERID," +
  965. " t.CURRENCYID," +
  966. " t.DELIVERYGOODSID," +
  967. " t.WRSTANDARDID," +
  968. " t.SPOTGOODSBRANDID," +
  969. " sum(t.ORIBUYQTY) ORIBUYQTY," +
  970. " sum(t.ORIBUYAMOUNT) ORIBUYAMOUNT," +
  971. " sum(t.ORISELLQTY) ORISELLQTY," +
  972. " sum(t.ORISELLAMOUNT) ORISELLAMOUNT," +
  973. " sum(t.ORIQTY) ORIQTY," +
  974. " sum(t.ORIAMOUNT) ORIAMOUNT," +
  975. " sum(t.TODAYBUYQTY) TODAYBUYQTY," +
  976. " sum(t.TODAYBUYAMOUNT) TODAYBUYAMOUNT," +
  977. " sum(t.TODAYSELLQTY) TODAYSELLQTY," +
  978. " sum(t.TODAYSELLAMOUNT) TODAYSELLAMOUNT," +
  979. " sum(t.CURBUYQTY) CURBUYQTY," +
  980. " sum(t.CURBUYAMOUNT) CURBUYAMOUNT," +
  981. " sum(t.CURSELLQTY) CURSELLQTY," +
  982. " sum(t.CURSELLAMOUNT) CURSELLAMOUNT," +
  983. " sum(t.CURQTY) CURQTY," +
  984. " sum(t.CURAMOUNT) CURAMOUNT," +
  985. " max(t.CURSPOTPRICE) CURSPOTPRICE," +
  986. " sum(t.CURMARKETVALUE) CURMARKETVALUE," +
  987. " sum(t.ACTUALPL) ACTUALPL," +
  988. " sum(t.FLOATPL) FLOATPL," +
  989. " sum(t.TODAYINQTY) TODAYINQTY," +
  990. " sum(t.TODAYOUTQTY) TODAYOUTQTY" +
  991. " FROM RECKON_ERMCP_AREASPOTPL t" +
  992. " WHERE 1 = 1 %v" +
  993. " GROUP BY t.RECKONDATE," +
  994. " t.AREAUSERID," +
  995. " t.CURRENCYID," +
  996. " t.DELIVERYGOODSID," +
  997. " t.WRSTANDARDID," +
  998. " t.SPOTGOODSBRANDID) a" +
  999. " LEFT JOIN DELIVERYGOODS g" +
  1000. " on a.deliverygoodsid = g.deliverygoodsid" +
  1001. " LEFT JOIN WRSTANDARD w" +
  1002. " on a.wrstandardid = w.wrstandardid" +
  1003. " LEFT JOIN DGFACTORYITEM dg" +
  1004. " on a.spotgoodsbrandid = dg.dgfactoryitemid"
  1005. var sqlParam utils.SQLVal
  1006. sqlParam.And("t.AREAUSERID", r.AREAUSERID)
  1007. sqlParam.AndEx("t.RECKONDATE", r.RECKONDATE, len(r.RECKONDATE) > 0)
  1008. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  1009. sqlParam.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1010. }
  1011. sqlParam.And("t.CURRENCYID", r.CURRENCYID)
  1012. sqlParam.And("t.DELIVERYGOODSID", r.DELIVERYGOODSID)
  1013. sqlId.FormatParam(sqlParam.String())
  1014. return sqlId.String()
  1015. }
  1016. // buildSqlCycle 周期报表查询语句
  1017. func (r *Ermcp3AreaSpotPLReport) buildSqlCycle() string {
  1018. var sqlId utils.SQLVal = "select a.*," +
  1019. " g.deliverygoodscode," +
  1020. " g.deliverygoodsname," +
  1021. " g.goodsunitid" +
  1022. " from (SELECT t.cycletype," +
  1023. " t.cycletime," +
  1024. " t.AREAUSERID," +
  1025. " t.CURRENCYID," +
  1026. " t.DELIVERYGOODSID," +
  1027. " sum(t.ORIBUYQTY) ORIBUYQTY," +
  1028. " sum(t.ORIBUYAMOUNT) ORIBUYAMOUNT," +
  1029. " sum(t.ORISELLQTY) ORISELLQTY," +
  1030. " sum(t.ORISELLAMOUNT) ORISELLAMOUNT," +
  1031. " sum(t.ORIQTY) ORIQTY," +
  1032. " sum(t.ORIAMOUNT) ORIAMOUNT," +
  1033. " sum(t.TODAYBUYQTY) TODAYBUYQTY," +
  1034. " sum(t.TODAYBUYAMOUNT) TODAYBUYAMOUNT," +
  1035. " sum(t.TODAYSELLQTY) TODAYSELLQTY," +
  1036. " sum(t.TODAYSELLAMOUNT) TODAYSELLAMOUNT," +
  1037. " sum(t.CURBUYQTY) CURBUYQTY," +
  1038. " sum(t.CURBUYAMOUNT) CURBUYAMOUNT," +
  1039. " sum(t.CURSELLQTY) CURSELLQTY," +
  1040. " sum(t.CURSELLAMOUNT) CURSELLAMOUNT," +
  1041. " sum(t.CURQTY) CURQTY," +
  1042. " sum(t.CURAMOUNT) CURAMOUNT," +
  1043. " max(t.CURSPOTPRICE) CURSPOTPRICE," +
  1044. " sum(t.CURMARKETVALUE) CURMARKETVALUE," +
  1045. " sum(t.ACTUALPL) ACTUALPL," +
  1046. " sum(t.FLOATPL) FLOATPL," +
  1047. " sum(t.TODAYINQTY) TODAYINQTY," +
  1048. " sum(t.TODAYOUTQTY) TODAYOUTQTY" +
  1049. " FROM REPORT_ERMCP_AREASPOTPL t" +
  1050. " WHERE 1=1 %v" +
  1051. " GROUP BY t.AREAUSERID," +
  1052. " t.cycletype," +
  1053. " t.cycletime," +
  1054. " t.CURRENCYID," +
  1055. " t.DELIVERYGOODSID) a" +
  1056. " LEFT JOIN DELIVERYGOODS g" +
  1057. " on a.deliverygoodsid = g.deliverygoodsid"
  1058. var sqlParam utils.SQLVal
  1059. sqlParam.And("t.AREAUSERID", r.AREAUSERID)
  1060. sqlParam.And("t.Cycletype", r.CYCLETYPE)
  1061. sqlParam.And("t.CYCLETIME", r.CYCLETIME)
  1062. sqlId.FormatParam(sqlParam.String())
  1063. return sqlId.String()
  1064. }
  1065. // GetDataEx 获取现货报表(日/月/周/季/年)
  1066. func (r *Ermcp3AreaSpotPLReport) GetDataEx() (interface{}, error) {
  1067. sData := make([]Ermcp3AreaSpotPLReport, 0)
  1068. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1069. for i := range sData {
  1070. sData[i].calc()
  1071. sData[i].BeginDate = r.BeginDate
  1072. sData[i].EndDate = r.EndDate
  1073. }
  1074. return sData, err
  1075. }
  1076. // Ermcp3FinanceReport 财务报表
  1077. type Ermcp3FinanceReport struct {
  1078. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照时期(yyyyMMdd)
  1079. CYCLETIME string `json:"cycletime" xorm:"CYCLETIME"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  1080. CYCLETYPE int32 `json:"cycletype" xorm:"CYCLETYPE"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  1081. SELLPREINVOICEDAMOUNT float64 `json:"sellpreinvoicedamount" xorm:"SELLPREINVOICEDAMOUNT"` // 销售预开票额(预开票额)
  1082. SELLUNINVOICEDAMOUNT float64 `json:"selluninvoicedamount" xorm:"SELLUNINVOICEDAMOUNT"` // 销售应开票额(应开票额)
  1083. TODAYRECEIVESUM float64 `json:"todayreceivesum" xorm:"TODAYRECEIVESUM"` // 今收款合计
  1084. TODAYPAYSUM float64 `json:"todaypaysum" xorm:"TODAYPAYSUM"` // 今付款合计
  1085. UPDATETIME string `json:"updatetime" xorm:"UPDATETIME"` // 更新时间
  1086. AREAUSERID int64 `json:"areauserid" xorm:"AREAUSERID"` // 所属机构\交易用户ID
  1087. CURRENCYID int32 `json:"currencyid" xorm:"CURRENCYID"` // 结算币种ID
  1088. BIZTYPE int32 `json:"biztype" xorm:"BIZTYPE"` // 业务类型 - 1:套保 2:套利
  1089. BUYTODAYSETTLEAMOUNT float64 `json:"buytodaysettleamount" xorm:"BUYTODAYSETTLEAMOUNT"` // 采购今付款额(今付货款额)
  1090. SELLTODAYSETTLEAMOUNT float64 `json:"selltodaysettleamount" xorm:"SELLTODAYSETTLEAMOUNT"` // 销售今收款额(今收货款额)
  1091. BUYTODAYREFUNDAMOUNT float64 `json:"buytodayrefundamount" xorm:"BUYTODAYREFUNDAMOUNT"` // 采购今收退款额(今收退款额)
  1092. SELLTODAYREFUNDAMOUNT float64 `json:"selltodayrefundamount" xorm:"SELLTODAYREFUNDAMOUNT"` // 销售今付退款额(今付退款额)
  1093. SELLTODAYINVOICEAMOUNT float64 `json:"selltodayinvoiceamount" xorm:"SELLTODAYINVOICEAMOUNT"` // 销售今开票额(今开票额)
  1094. BUYTODAYINVOICEAMOUNT float64 `json:"buytodayinvoiceamount" xorm:"BUYTODAYINVOICEAMOUNT"` // 采购今收票额(今收票额)
  1095. BUYPREPAIDAMOUNT float64 `json:"buyprepaidamount" xorm:"BUYPREPAIDAMOUNT"` // 采购预付款额(预付货款额)
  1096. BUYUNPAIDAMOUNT float64 `json:"buyunpaidamount" xorm:"BUYUNPAIDAMOUNT"` // 采购应付款额(应付货款额)
  1097. BUYPREINVOICEDAMOUNT float64 `json:"buypreinvoicedamount" xorm:"BUYPREINVOICEDAMOUNT"` // 采购预收票额(预收票额)
  1098. BUYUNINVOICEDAMOUNT float64 `json:"buyuninvoicedamount" xorm:"BUYUNINVOICEDAMOUNT"` // 采购应收票额(应收票额)
  1099. SELLPREPAIDAMOUNT float64 `json:"sellprepaidamount" xorm:"SELLPREPAIDAMOUNT"` // 销售预收款额(预收货款额)
  1100. SELLUNPAIDAMOUNT float64 `json:"sellunpaidamount" xorm:"SELLUNPAIDAMOUNT"` // 销售应收款额(应收货款额)
  1101. WRFACTORTYPEID int64 `json:"wrfactortypeid" xorm:"WRFACTORTYPEID"` // 仓单要素类型ID(212+Unix秒时间戳(10位)+xxxxxx)
  1102. DELIVERYGOODSID int64 `json:"deliverygoodsid" xorm:"DELIVERYGOODSID"` // 现货品种ID
  1103. WRSTANDARDID int64 `json:"wrstandardid" xorm:"WRSTANDARDID"` // 现货商品ID
  1104. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"SPOTGOODSBRANDID"` // 现货品牌ID
  1105. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  1106. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  1107. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1108. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1109. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1110. ACCOUNTNAME string `json:"accountname"` // 机构名称
  1111. CURRENCYNAME string `json:"currencyname"` // 币种名称
  1112. QueryType int32 `json:"-"` // 查询类型 1-报表 2-报表明细
  1113. BeginDate string `json:"begindate"` // 开始交易日
  1114. EndDate string `json:"enddate"` // 结束交易日
  1115. SumFields string `json:"-"` // 维度字段
  1116. }
  1117. func (r *Ermcp3FinanceReport) calc() {
  1118. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  1119. r.ACCOUNTNAME = mtpcache.GetUserNameByUserId(r.AREAUSERID)
  1120. f := func(v *float64) {
  1121. if *v < 0 {
  1122. *v = 0
  1123. }
  1124. }
  1125. f(&r.BUYPREPAIDAMOUNT)
  1126. f(&r.SELLPREINVOICEDAMOUNT)
  1127. f(&r.BUYUNINVOICEDAMOUNT)
  1128. f(&r.SELLPREPAIDAMOUNT)
  1129. f(&r.BUYPREINVOICEDAMOUNT)
  1130. }
  1131. // getSumField 获取汇总维度字段
  1132. func (r *Ermcp3FinanceReport) getSumField() (fields []string) {
  1133. fields = append(fields, "areauserid")
  1134. fields = append(fields, "currencyid")
  1135. if r.CYCLETYPE == 0 {
  1136. // 日报表
  1137. fields = append(fields, "reckondate")
  1138. } else {
  1139. // 周期报表
  1140. fields = append(fields, "cycletype")
  1141. fields = append(fields, "cycletime")
  1142. }
  1143. baseField := map[string]string{
  1144. "1": "deliverygoodsid",
  1145. "2": "wrstandardid",
  1146. "3": "spotgoodsbrandid",
  1147. }
  1148. param := []string{}
  1149. if len(r.SumFields) > 0 {
  1150. param = strings.Split(r.SumFields, ",")
  1151. }
  1152. for k, v := range baseField {
  1153. for _, p := range param {
  1154. if k == p {
  1155. fields = append(fields, v)
  1156. }
  1157. }
  1158. }
  1159. return
  1160. }
  1161. func (r *Ermcp3FinanceReport) getExFieldInfo() (strFiled string, strleftJoin string) {
  1162. if len(r.SumFields) > 0 {
  1163. param := strings.Split(r.SumFields, ",")
  1164. fHas := func(v string) bool {
  1165. for i := range param {
  1166. if param[i] == v {
  1167. return true
  1168. }
  1169. }
  1170. return false
  1171. }
  1172. if fHas("1") {
  1173. if len(strFiled) > 0 {
  1174. strFiled += ","
  1175. }
  1176. strFiled += "g.deliverygoodscode, g.deliverygoodsname, g.goodsunitid"
  1177. strleftJoin += " LEFT JOIN deliverygoods g on a.deliverygoodsid = g.deliverygoodsid"
  1178. }
  1179. if fHas("2") {
  1180. if len(strFiled) > 0 {
  1181. strFiled += ","
  1182. }
  1183. strFiled += "w.wrstandardname, w.wrstandardcode, w.unitid"
  1184. strleftJoin += " LEFT JOIN WRSTANDARD w on a.wrstandardid = w.wrstandardid"
  1185. }
  1186. if fHas("3") {
  1187. if len(strFiled) > 0 {
  1188. strFiled += ","
  1189. }
  1190. strFiled += "gb.dgfactoryitemvalue brandname"
  1191. strleftJoin += " LEFT JOIN dgfactoryitem gb on a.spotgoodsbrandid = gb.dgfactoryitemid"
  1192. }
  1193. }
  1194. return
  1195. }
  1196. func (r *Ermcp3FinanceReport) buildSql() string {
  1197. fields := r.getSumField()
  1198. if r.CYCLETYPE == 0 {
  1199. // 查日照报
  1200. return r.buildSqlReckon(fields)
  1201. } else {
  1202. // 查报表表
  1203. return r.buildSqlReport(fields)
  1204. }
  1205. }
  1206. func (r *Ermcp3FinanceReport) buildSqlReckon(fields []string) string {
  1207. var sqlId, sqlParam utils.SQLVal
  1208. var strFileds string
  1209. for _, v := range fields {
  1210. if len(strFileds) > 0 {
  1211. strFileds += ","
  1212. }
  1213. strFileds += "t." + v
  1214. }
  1215. sqlParam.And("t.AREAUSERID", r.AREAUSERID)
  1216. sqlParam.AndEx("t.RECKONDATE", r.RECKONDATE, len(r.RECKONDATE) > 0)
  1217. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  1218. sqlParam.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1219. }
  1220. sqlParam.AndEx("t.CURRENCYID", r.CURRENCYID, r.CURRENCYID > 0)
  1221. exFileds, strleftJoin := r.getExFieldInfo()
  1222. if len(exFileds) > 0 {
  1223. // 有左联查额外信息(RECKON_ERMCP_AFINANCESUB明细表, 注意是不同的表)
  1224. sqlId = "select a.*, %v" +
  1225. " from (SELECT %v," +
  1226. " sum(t.BUYTODAYSETTLEAMOUNT) BUYTODAYSETTLEAMOUNT," +
  1227. " sum(t.BUYTODAYREFUNDAMOUNT) BUYTODAYREFUNDAMOUNT," +
  1228. " sum(t.BUYPREPAIDAMOUNT) BUYPREPAIDAMOUNT," +
  1229. " sum(t.BUYUNPAIDAMOUNT) BUYUNPAIDAMOUNT," +
  1230. " sum(t.BUYTODAYINVOICEAMOUNT) BUYTODAYINVOICEAMOUNT," +
  1231. " sum(t.BUYPREINVOICEDAMOUNT) BUYPREINVOICEDAMOUNT," +
  1232. " sum(t.BUYUNINVOICEDAMOUNT) BUYUNINVOICEDAMOUNT," +
  1233. " sum(t.SELLTODAYSETTLEAMOUNT) SELLTODAYSETTLEAMOUNT," +
  1234. " sum(t.SELLTODAYREFUNDAMOUNT) SELLTODAYREFUNDAMOUNT," +
  1235. " sum(t.SELLPREPAIDAMOUNT) SELLPREPAIDAMOUNT," +
  1236. " sum(t.SELLUNPAIDAMOUNT) SELLUNPAIDAMOUNT," +
  1237. " sum(t.SELLTODAYINVOICEAMOUNT) SELLTODAYINVOICEAMOUNT," +
  1238. " sum(t.SELLPREINVOICEDAMOUNT) SELLPREINVOICEDAMOUNT," +
  1239. " sum(t.SELLUNINVOICEDAMOUNT) SELLUNINVOICEDAMOUNT," +
  1240. " sum(t.TODAYRECEIVESUM) TODAYRECEIVESUM," +
  1241. " sum(t.TODAYPAYSUM) TODAYPAYSUM" +
  1242. " FROM RECKON_ERMCP_AFINANCESUB t" +
  1243. " WHERE 1 = 1 %v" +
  1244. " GROUP BY %v) a %v"
  1245. sqlId.FormatParam(exFileds, strFileds, sqlParam.String(), strFileds, strleftJoin)
  1246. } else {
  1247. sqlId = "SELECT %v," +
  1248. " sum(t.BUYTODAYSETTLEAMOUNT) BUYTODAYSETTLEAMOUNT," +
  1249. " sum(t.BUYTODAYREFUNDAMOUNT) BUYTODAYREFUNDAMOUNT," +
  1250. " sum(t.BUYPREPAIDAMOUNT) BUYPREPAIDAMOUNT," +
  1251. " sum(t.BUYUNPAIDAMOUNT) BUYUNPAIDAMOUNT," +
  1252. " sum(t.BUYTODAYINVOICEAMOUNT) BUYTODAYINVOICEAMOUNT," +
  1253. " sum(t.BUYPREINVOICEDAMOUNT) BUYPREINVOICEDAMOUNT," +
  1254. " sum(t.BUYUNINVOICEDAMOUNT) BUYUNINVOICEDAMOUNT," +
  1255. " sum(t.SELLTODAYSETTLEAMOUNT) SELLTODAYSETTLEAMOUNT," +
  1256. " sum(t.SELLTODAYREFUNDAMOUNT) SELLTODAYREFUNDAMOUNT," +
  1257. " sum(t.SELLPREPAIDAMOUNT) SELLPREPAIDAMOUNT," +
  1258. " sum(t.SELLUNPAIDAMOUNT) SELLUNPAIDAMOUNT," +
  1259. " sum(t.SELLTODAYINVOICEAMOUNT) SELLTODAYINVOICEAMOUNT," +
  1260. " sum(t.SELLPREINVOICEDAMOUNT) SELLPREINVOICEDAMOUNT," +
  1261. " sum(t.SELLUNINVOICEDAMOUNT) SELLUNINVOICEDAMOUNT," +
  1262. " sum(t.TODAYRECEIVESUM) TODAYRECEIVESUM," +
  1263. " sum(t.TODAYPAYSUM) TODAYPAYSUM" +
  1264. " FROM RECKON_ERMCP_AREAFINANCE t" +
  1265. " WHERE 1 = 1 %v" +
  1266. " GROUP BY %v"
  1267. sqlId.FormatParam(strFileds, sqlParam.String(), strFileds)
  1268. }
  1269. return sqlId.String()
  1270. }
  1271. func (r *Ermcp3FinanceReport) buildSqlReport(fields []string) string {
  1272. var sqlId, sqlParam utils.SQLVal
  1273. var strFileds string
  1274. for _, v := range fields {
  1275. if len(strFileds) > 0 {
  1276. strFileds += ","
  1277. }
  1278. strFileds += "t." + v
  1279. }
  1280. sqlParam.And("t.AREAUSERID", r.AREAUSERID)
  1281. sqlParam.And("t.CYCLETYPE", r.CYCLETYPE)
  1282. sqlParam.And("t.CYCLETIME", r.CYCLETIME)
  1283. sqlParam.AndEx("t.CURRENCYID", r.CURRENCYID, r.CURRENCYID > 0)
  1284. exFileds, strleftJoin := r.getExFieldInfo()
  1285. if len(exFileds) > 0 {
  1286. // 有左联查额外信息(REPORT_ERMCP_AFINANCESUB注意表名不同)
  1287. sqlId = "select a.*, %v" +
  1288. " from (SELECT %v," +
  1289. " sum(t.BUYTODAYSETTLEAMOUNT) BUYTODAYSETTLEAMOUNT," +
  1290. " sum(t.BUYTODAYREFUNDAMOUNT) BUYTODAYREFUNDAMOUNT," +
  1291. " sum(t.BUYPREPAIDAMOUNT) BUYPREPAIDAMOUNT," +
  1292. " sum(t.BUYUNPAIDAMOUNT) BUYUNPAIDAMOUNT," +
  1293. " sum(t.BUYTODAYINVOICEAMOUNT) BUYTODAYINVOICEAMOUNT," +
  1294. " sum(t.BUYPREINVOICEDAMOUNT) BUYPREINVOICEDAMOUNT," +
  1295. " sum(t.BUYUNINVOICEDAMOUNT) BUYUNINVOICEDAMOUNT," +
  1296. " sum(t.SELLTODAYSETTLEAMOUNT) SELLTODAYSETTLEAMOUNT," +
  1297. " sum(t.SELLTODAYREFUNDAMOUNT) SELLTODAYREFUNDAMOUNT," +
  1298. " sum(t.SELLPREPAIDAMOUNT) SELLPREPAIDAMOUNT," +
  1299. " sum(t.SELLUNPAIDAMOUNT) SELLUNPAIDAMOUNT," +
  1300. " sum(t.SELLTODAYINVOICEAMOUNT) SELLTODAYINVOICEAMOUNT," +
  1301. " sum(t.SELLPREINVOICEDAMOUNT) SELLPREINVOICEDAMOUNT," +
  1302. " sum(t.SELLUNINVOICEDAMOUNT) SELLUNINVOICEDAMOUNT," +
  1303. " sum(t.TODAYRECEIVESUM) TODAYRECEIVESUM," +
  1304. " sum(t.TODAYPAYSUM) TODAYPAYSUM" +
  1305. " FROM REPORT_ERMCP_AFINANCESUB t" +
  1306. " WHERE 1 = 1 %v" +
  1307. " GROUP BY %v) a %v"
  1308. sqlId.FormatParam(exFileds, strFileds, sqlParam.String(), strFileds, strleftJoin)
  1309. } else {
  1310. sqlId = "SELECT %v," +
  1311. " sum(t.BUYTODAYSETTLEAMOUNT) BUYTODAYSETTLEAMOUNT," +
  1312. " sum(t.BUYTODAYREFUNDAMOUNT) BUYTODAYREFUNDAMOUNT," +
  1313. " sum(t.BUYPREPAIDAMOUNT) BUYPREPAIDAMOUNT," +
  1314. " sum(t.BUYUNPAIDAMOUNT) BUYUNPAIDAMOUNT," +
  1315. " sum(t.BUYTODAYINVOICEAMOUNT) BUYTODAYINVOICEAMOUNT," +
  1316. " sum(t.BUYPREINVOICEDAMOUNT) BUYPREINVOICEDAMOUNT," +
  1317. " sum(t.BUYUNINVOICEDAMOUNT) BUYUNINVOICEDAMOUNT," +
  1318. " sum(t.SELLTODAYSETTLEAMOUNT) SELLTODAYSETTLEAMOUNT," +
  1319. " sum(t.SELLTODAYREFUNDAMOUNT) SELLTODAYREFUNDAMOUNT," +
  1320. " sum(t.SELLPREPAIDAMOUNT) SELLPREPAIDAMOUNT," +
  1321. " sum(t.SELLUNPAIDAMOUNT) SELLUNPAIDAMOUNT," +
  1322. " sum(t.SELLTODAYINVOICEAMOUNT) SELLTODAYINVOICEAMOUNT," +
  1323. " sum(t.SELLPREINVOICEDAMOUNT) SELLPREINVOICEDAMOUNT," +
  1324. " sum(t.SELLUNINVOICEDAMOUNT) SELLUNINVOICEDAMOUNT," +
  1325. " sum(t.TODAYRECEIVESUM) TODAYRECEIVESUM," +
  1326. " sum(t.TODAYPAYSUM) TODAYPAYSUM" +
  1327. " FROM REPORT_ERMCP_AREAFINANCE t" +
  1328. " WHERE 1 = 1 %v" +
  1329. " GROUP BY %v"
  1330. sqlId.FormatParam(strFileds, sqlParam.String(), strFileds)
  1331. }
  1332. return sqlId.String()
  1333. }
  1334. // GetDataEx 获取财务报表
  1335. func (r *Ermcp3FinanceReport) GetDataEx() (interface{}, error) {
  1336. sData := make([]Ermcp3FinanceReport, 0)
  1337. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1338. for i := range sData {
  1339. sData[i].calc()
  1340. sData[i].BeginDate = r.BeginDate
  1341. sData[i].EndDate = r.EndDate
  1342. }
  1343. return sData, err
  1344. }
  1345. // Ermcp3AreaStockReportEx 库存报表
  1346. type Ermcp3AreaStockReportEx struct {
  1347. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照时期(yyyyMMdd)
  1348. CYCLETIME string `json:"cycletime" xorm:"CYCLETIME"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  1349. CYCLETYPE int32 `json:"cycletype" xorm:"CYCLETYPE"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  1350. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构ID
  1351. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类ID
  1352. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID
  1353. WAREHOUSEINFOID string `json:"warehouseinfoid" xorm:"'WAREHOUSEINFOID'"` // 仓库ID
  1354. ORISTOCK float64 `json:"oristock" xorm:"'ORISTOCK'"` // 期初库存量
  1355. CURSTOCK float64 `json:"curstock" xorm:"'CURSTOCK'"` // 期末库存量
  1356. TODAYBUYINQTY float64 `json:"todaybuyinqty" xorm:"'TODAYBUYINQTY'"` // 今采购入库量
  1357. TODAYPRODUCEINQTY float64 `json:"todayproduceinqty" xorm:"'TODAYPRODUCEINQTY'"` // 今内部入库量
  1358. TODAYSELLOUTQTY float64 `json:"todayselloutqty" xorm:"'TODAYSELLOUTQTY'"` // 今销售出库量
  1359. TODAYPRODUCEOUTQTY float64 `json:"todayproduceoutqty" xorm:"'TODAYPRODUCEOUTQTY'"` // 今内部出库量
  1360. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  1361. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1362. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1363. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 品类单位id
  1364. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1365. WAREHOUSENAME string `json:"warehousename" xorm:"'WAREHOUSENAME'"` // 仓库名称
  1366. WAREHOUSECODE string `json:"warehousecode" xorm:"'WAREHOUSECODE'"` // 仓库代码
  1367. WAREHOUSETYPE int32 `json:"warehousetype" xorm:"'WAREHOUSETYPE'"` // 仓库类型 - 1 厂库 2 自有库 3 合作库
  1368. USERNAME string `json:"username"` // 机构名称
  1369. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  1370. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种id
  1371. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  1372. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  1373. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODSUNITID'"` // 现货单位id
  1374. UNBUYINQTY float64 `json:"unbuyinqty"` // 采购未入库量(数据库未找到相关字段?)
  1375. UNSELLOUTQTY float64 `json:"unselloutqty"` // 销售未出库量(数据库未找到相关字段?)
  1376. DiffQty float64 `json:"diffqty"` // 库存变化量 = 期末 - 期初
  1377. BeginDate string `json:"begindate"` // 开始交易日
  1378. EndDate string `json:"enddate"` // 结束交易日
  1379. SumFields string `json:"-"` // 汇总维度(逗号隔开) 1-品种 2-品类 3-品牌 4-仓库
  1380. }
  1381. func (r *Ermcp3AreaStockReportEx) calc() {
  1382. r.USERNAME = mtpcache.GetUserNameByUserId(r.USERID)
  1383. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  1384. r.DiffQty = r.CURSTOCK - r.ORISTOCK
  1385. }
  1386. // getSumField 获取汇总维度字段
  1387. func (r *Ermcp3AreaStockReportEx) getSumField() (fields []string, bSum bool) {
  1388. fields = append(fields, "userid")
  1389. if r.CYCLETYPE == 0 {
  1390. // 日报表
  1391. fields = append(fields, "reckondate")
  1392. } else {
  1393. // 周期报表
  1394. fields = append(fields, "cycletype")
  1395. fields = append(fields, "cycletime")
  1396. }
  1397. baseField := map[string]string{
  1398. "1": "deliverygoodsid",
  1399. "2": "wrstandardid",
  1400. "3": "spotgoodsbrandid",
  1401. "4": "warehouseinfoid",
  1402. }
  1403. bSum = false
  1404. param := []string{}
  1405. if len(r.SumFields) > 0 {
  1406. param = strings.Split(r.SumFields, ",")
  1407. }
  1408. if len(param) == 0 {
  1409. // 全字段, 不需要汇总
  1410. for _, v := range baseField {
  1411. fields = append(fields, v)
  1412. }
  1413. bSum = false
  1414. } else {
  1415. bIncludeAll := true
  1416. // 判断是否包含所有维度
  1417. for k := range baseField {
  1418. bExist := false
  1419. for _, p := range param {
  1420. if k == p {
  1421. bExist = true
  1422. break
  1423. }
  1424. }
  1425. if !bExist {
  1426. bIncludeAll = false
  1427. break
  1428. }
  1429. }
  1430. if bIncludeAll {
  1431. // 包含了所有维度, 不需要汇总
  1432. for _, v := range baseField {
  1433. fields = append(fields, v)
  1434. }
  1435. bSum = false
  1436. } else {
  1437. tmp := map[string]string{}
  1438. for _, p := range param {
  1439. if v, ok := baseField[p]; ok {
  1440. // 利用map特性去重, 先保存在一个临时map中
  1441. tmp[p] = v
  1442. bSum = true
  1443. }
  1444. }
  1445. if bSum {
  1446. for _, v := range tmp {
  1447. fields = append(fields, v)
  1448. }
  1449. }
  1450. }
  1451. }
  1452. return
  1453. }
  1454. func (r *Ermcp3AreaStockReportEx) getExFieldInfo() (strFiled string, strleftJoin string) {
  1455. if len(r.SumFields) > 0 {
  1456. param := strings.Split(r.SumFields, ",")
  1457. fHas := func(v string) bool {
  1458. for i := range param {
  1459. if param[i] == v {
  1460. return true
  1461. }
  1462. }
  1463. return false
  1464. }
  1465. if fHas("1") {
  1466. if len(strFiled) > 0 {
  1467. strFiled += ","
  1468. }
  1469. strFiled += "g.deliverygoodscode, g.deliverygoodsname, g.goodsunitid"
  1470. strleftJoin += " LEFT JOIN deliverygoods g on a.deliverygoodsid = g.deliverygoodsid"
  1471. }
  1472. if fHas("2") {
  1473. if len(strFiled) > 0 {
  1474. strFiled += ","
  1475. }
  1476. strFiled += "w.wrstandardname, w.wrstandardcode, w.unitid"
  1477. strleftJoin += " LEFT JOIN WRSTANDARD w on a.wrstandardid = w.wrstandardid"
  1478. }
  1479. if fHas("3") {
  1480. if len(strFiled) > 0 {
  1481. strFiled += ","
  1482. }
  1483. strFiled += "gb.dgfactoryitemvalue brandname"
  1484. strleftJoin += " LEFT JOIN dgfactoryitem gb on a.spotgoodsbrandid = gb.dgfactoryitemid"
  1485. }
  1486. if fHas("4") {
  1487. if len(strFiled) > 0 {
  1488. strFiled += ","
  1489. }
  1490. strFiled += "h.warehousecode, h.warehousename, h.warehousetype"
  1491. strleftJoin += " LEFT JOIN WAREHOUSEINFO h on a.warehouseinfoid=h.autoid"
  1492. }
  1493. }
  1494. return
  1495. }
  1496. func (r *Ermcp3AreaStockReportEx) buildSql() (sqlId string) {
  1497. fields, bSum := r.getSumField()
  1498. if r.CYCLETYPE == 0 {
  1499. // 日报表
  1500. if bSum {
  1501. sqlId = r.buildSqlReckonSum(fields)
  1502. } else {
  1503. sqlId = r.buildSqlReckon()
  1504. }
  1505. } else {
  1506. // 周期报表
  1507. if bSum {
  1508. sqlId = r.buildSqlReportSum(fields)
  1509. } else {
  1510. sqlId = r.buildSqlReport()
  1511. }
  1512. }
  1513. return
  1514. }
  1515. // GetDataEx 获取库存报表
  1516. func (r *Ermcp3AreaStockReportEx) GetDataEx() (interface{}, error) {
  1517. sData := make([]Ermcp3AreaStockReportEx, 0)
  1518. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1519. for i := range sData {
  1520. sData[i].calc()
  1521. sData[i].BeginDate = r.BeginDate
  1522. sData[i].EndDate = r.EndDate
  1523. }
  1524. return sData, err
  1525. }
  1526. // buildSqlReckon 日照表
  1527. func (r *Ermcp3AreaStockReportEx) buildSqlReckon() string {
  1528. var sqlId utils.SQLVal = "SELECT t.TODAYBUYINQTY," +
  1529. " t.TODAYPRODUCEINQTY," +
  1530. " t.reckondate," +
  1531. " t.TODAYSELLOUTQTY," +
  1532. " t.TODAYPRODUCEOUTQTY," +
  1533. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1534. " t.USERID," +
  1535. " t.WRSTANDARDID," +
  1536. " t.SPOTGOODSBRANDID," +
  1537. " t.WAREHOUSEINFOID," +
  1538. " t.deliverygoodsid," +
  1539. " t.ORISTOCK," +
  1540. " t.CURSTOCK," +
  1541. " w.wrstandardname," +
  1542. " w.wrstandardcode," +
  1543. " w.unitid," +
  1544. " gb.dgfactoryitemvalue brandname," +
  1545. " h.warehousename," +
  1546. " h.warehousecode," +
  1547. " h.warehousetype," +
  1548. " g.deliverygoodscode," +
  1549. " g.deliverygoodsname," +
  1550. " g.goodsunitid" +
  1551. " FROM RECKON_ERMCP_AREASTOCK t" +
  1552. " LEFT JOIN WRSTANDARD w" +
  1553. " on t.wrstandardid = w.wrstandardid" +
  1554. " LEFT JOIN dgfactoryitem gb" +
  1555. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1556. " LEFT JOIN deliverygoods g" +
  1557. " on t.deliverygoodsid = g.deliverygoodsid" +
  1558. " LEFT JOIN WAREHOUSEINFO h" +
  1559. " on t.warehouseinfoid = h.autoid" +
  1560. " WHERE 1 = 1"
  1561. sqlId.And("t.USERID", r.USERID)
  1562. sqlId.AndEx("t.RECKONDATE", r.RECKONDATE, len(r.RECKONDATE) > 0)
  1563. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  1564. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1565. }
  1566. // 现货商品id
  1567. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  1568. // 品类id
  1569. sqlId.AndEx("t.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  1570. // 品牌id
  1571. sqlId.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  1572. // 仓库id
  1573. sqlId.AndEx("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID, len(r.WAREHOUSEINFOID) > 0)
  1574. sqlId.Join(" order by t.reckondate")
  1575. return sqlId.String()
  1576. }
  1577. // buildSqlReckonSum 日照表(汇总维度)
  1578. func (r *Ermcp3AreaStockReportEx) buildSqlReckonSum(fields []string) string {
  1579. var sqlId utils.SQLVal = "SELECT a.*, %v" +
  1580. " from (SELECT %v," +
  1581. " sum(t.ORISTOCK) ORISTOCK," +
  1582. " sum(t.CURSTOCK) CURSTOCK," +
  1583. " sum(t.TODAYBUYINQTY) TODAYBUYINQTY," +
  1584. " sum(t.TODAYPRODUCEINQTY) TODAYPRODUCEINQTY," +
  1585. " sum(t.TODAYSELLOUTQTY) TODAYSELLOUTQTY," +
  1586. " sum(t.TODAYPRODUCEOUTQTY) TODAYPRODUCEOUTQTY" +
  1587. " FROM RECKON_ERMCP_AREASTOCK t" +
  1588. " WHERE 1 = 1 %v" +
  1589. " GROUP BY %v) a %v"
  1590. var sqlParam utils.SQLVal
  1591. sqlParam.And("t.USERID", r.USERID)
  1592. sqlParam.AndEx("t.RECKONDATE", r.RECKONDATE, len(r.RECKONDATE) > 0)
  1593. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  1594. sqlParam.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1595. }
  1596. // 现货商品id
  1597. sqlParam.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  1598. // 品类id
  1599. sqlParam.AndEx("t.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  1600. // 品牌id
  1601. sqlParam.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  1602. // 仓库id
  1603. sqlParam.AndEx("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID, len(r.WAREHOUSEINFOID) > 0)
  1604. var strFileds string
  1605. for _, v := range fields {
  1606. if len(strFileds) > 0 {
  1607. strFileds += ","
  1608. }
  1609. strFileds += "t." + v
  1610. }
  1611. exFields, exLeftJoinTable := r.getExFieldInfo()
  1612. sqlId.FormatParam(exFields, strFileds, sqlParam.String(), strFileds, exLeftJoinTable)
  1613. sqlId.Join(" order by a.reckondate")
  1614. return sqlId.String()
  1615. }
  1616. // buildSqlReport 报表表
  1617. func (r *Ermcp3AreaStockReportEx) buildSqlReport() string {
  1618. var sqlId utils.SQLVal = "SELECT t.TODAYBUYINQTY," +
  1619. " t.TODAYPRODUCEINQTY," +
  1620. " t.cycletype," +
  1621. " t.cycletime," +
  1622. " t.USERID," +
  1623. " t.WRSTANDARDID," +
  1624. " t.SPOTGOODSBRANDID," +
  1625. " t.WAREHOUSEINFOID," +
  1626. " t.deliverygoodsid," +
  1627. " t.TODAYSELLOUTQTY," +
  1628. " t.TODAYPRODUCEOUTQTY," +
  1629. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1630. " t.ORISTOCK," +
  1631. " t.CURSTOCK," +
  1632. " w.wrstandardname," +
  1633. " w.wrstandardcode," +
  1634. " w.unitid," +
  1635. " gb.dgfactoryitemvalue brandname," +
  1636. " h.warehousename," +
  1637. " h.warehousecode," +
  1638. " h.warehousetype," +
  1639. " g.deliverygoodscode," +
  1640. " g.deliverygoodsname," +
  1641. " g.goodsunitid" +
  1642. " FROM REPORT_ERMCP_AREASTOCK t" +
  1643. " LEFT JOIN WRSTANDARD w" +
  1644. " on t.wrstandardid = w.wrstandardid" +
  1645. " LEFT JOIN dgfactoryitem gb" +
  1646. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1647. " LEFT JOIN deliverygoods g" +
  1648. " on t.deliverygoodsid = g.deliverygoodsid" +
  1649. " LEFT JOIN WAREHOUSEINFO h" +
  1650. " on t.warehouseinfoid = h.autoid" +
  1651. " WHERE 1 = 1"
  1652. sqlId.And("t.USERID", r.USERID)
  1653. sqlId.And("t.cycletype", r.CYCLETYPE)
  1654. sqlId.And("t.cycletime", r.CYCLETIME)
  1655. // 现货商品id
  1656. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  1657. // 品类id
  1658. sqlId.AndEx("t.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  1659. // 品牌id
  1660. sqlId.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  1661. // 仓库id
  1662. sqlId.AndEx("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID, len(r.WAREHOUSEINFOID) > 0)
  1663. return sqlId.String()
  1664. }
  1665. // buildSqlReport 报表表(汇总维度)
  1666. func (r *Ermcp3AreaStockReportEx) buildSqlReportSum(fields []string) string {
  1667. var sqlId utils.SQLVal = "SELECT a.*, %v" +
  1668. " from (SELECT %v," +
  1669. " sum(t.ORISTOCK) ORISTOCK," +
  1670. " sum(t.CURSTOCK) CURSTOCK," +
  1671. " sum(t.TODAYBUYINQTY) TODAYBUYINQTY," +
  1672. " sum(t.TODAYPRODUCEINQTY) TODAYPRODUCEINQTY," +
  1673. " sum(t.TODAYSELLOUTQTY) TODAYSELLOUTQTY," +
  1674. " sum(t.TODAYPRODUCEOUTQTY) TODAYPRODUCEOUTQTY" +
  1675. " FROM REPORT_ERMCP_AREASTOCK t" +
  1676. " WHERE 1 = 1 %v" +
  1677. " GROUP BY %v) a %v"
  1678. var sqlParam utils.SQLVal
  1679. sqlParam.And("t.USERID", r.USERID)
  1680. sqlParam.And("t.cycletype", r.CYCLETYPE)
  1681. sqlParam.And("t.cycletime", r.CYCLETIME)
  1682. // 现货商品id
  1683. sqlParam.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  1684. // 品类id
  1685. sqlParam.AndEx("t.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  1686. // 品牌id
  1687. sqlParam.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  1688. // 仓库id
  1689. sqlParam.AndEx("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID, len(r.WAREHOUSEINFOID) > 0)
  1690. var strFileds string
  1691. for _, v := range fields {
  1692. if len(strFileds) > 0 {
  1693. strFileds += ","
  1694. }
  1695. strFileds += "t." + v
  1696. }
  1697. exFields, exLeftJoinTable := r.getExFieldInfo()
  1698. sqlId.FormatParam(exFields, strFileds, sqlParam.String(), strFileds, exLeftJoinTable)
  1699. return sqlId.String()
  1700. }
  1701. // Ermcp3ArealSumPL 损益汇总表
  1702. type Ermcp3ArealSumPL struct {
  1703. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照时期(yyyyMMdd)
  1704. CYCLETIME string `json:"cycletime" xorm:"'CYCLETIME'"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  1705. CYCLETYPE int32 `json:"cycletype" xorm:"'CYCLETYPE'"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  1706. AREAUSERID int64 `json:"areauserid" xorm:"AREAUSERID"` // 所属机构\交易用户ID
  1707. MIDDLEGOODSID int64 `json:"middlegoodsid" xorm:"MIDDLEGOODSID"` // 套保品种ID(SEQ_ERMS_MIDDLEGOODS)
  1708. CURRENCYID int32 `json:"currencyid" xorm:"CURRENCYID"` // 结算币种ID
  1709. SPOTACTUALPL float64 `json:"spotactualpl" xorm:"SPOTACTUALPL"` // 现货损益
  1710. SPOTFLOATPL float64 `json:"spotfloatpl" xorm:"SPOTFLOATPL"` // 现货浮动损益
  1711. FUTUREACTUALPL float64 `json:"futureactualpl" xorm:"FUTUREACTUALPL"` // 期货损益
  1712. FUTUREFLOATPL float64 `json:"futurefloatpl" xorm:"FUTUREFLOATPL"` // 期货浮动损益
  1713. SUMACTUALPL float64 `json:"sumactualpl" xorm:"SUMACTUALPL"` // 实际损益 = (ActualPL + FutureActualPL)
  1714. SUMPL float64 `json:"sumpl" xorm:"'SUMPL'"` // 汇总损益
  1715. UPDATETIME string `json:"updatetime" xorm:"UPDATETIME"` // 更新时间
  1716. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保品种名称
  1717. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保品种代码
  1718. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODSUNITID'"` // 套保品种单位id
  1719. ACCOUNTNAME string `json:"accountname"` // 机构名称
  1720. ENUMDICNAME string `json:"enumdicname"` // 单位名称(套保品种)
  1721. CURRENCYNAME string `json:"currencyname"` // 币种名称
  1722. QueryType int32 `json:"-"` // 查询类型 1-报表 2-报表明细
  1723. BeginDate string `json:"begindate"` // 开始交易日
  1724. EndDate string `json:"enddate"` // 结束交易日
  1725. }
  1726. func (r *Ermcp3ArealSumPL) calc() {
  1727. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  1728. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.GOODSUNITID)
  1729. r.ACCOUNTNAME = mtpcache.GetUserNameByUserId(r.AREAUSERID)
  1730. }
  1731. func (r *Ermcp3ArealSumPL) buildSql() string {
  1732. // 日报表
  1733. if r.QueryType == 1 && r.CYCLETYPE == 0 {
  1734. return r.buildSqlDay()
  1735. }
  1736. // 日报表明细
  1737. if r.QueryType == 2 && r.CYCLETYPE == 0 {
  1738. return r.buildSqlDayDetail()
  1739. }
  1740. return r.buildSqlCycle()
  1741. }
  1742. // buildSqlDay 损益汇总日报表(需要汇总维度)
  1743. func (r *Ermcp3ArealSumPL) buildSqlDay() string {
  1744. var sqlId utils.SQLVal = "SELECT t.RECKONDATE," +
  1745. " t.AREAUSERID," +
  1746. " t.CURRENCYID," +
  1747. " sum(t.SPOTACTUALPL) SPOTACTUALPL," +
  1748. " sum(t.SPOTFLOATPL) SPOTFLOATPL," +
  1749. " sum(t.FUTUREACTUALPL) FUTUREACTUALPL," +
  1750. " sum(t.FUTUREFLOATPL) FUTUREFLOATPL," +
  1751. " sum(t.SUMACTUALPL) SUMACTUALPL," +
  1752. " sum(t.SUMPL) SUMPL" +
  1753. " FROM RECKON_ERMCP_AREASUMPL t" +
  1754. " WHERE 1 = 1"
  1755. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  1756. if r.QueryType == 1 {
  1757. sqlId.And("t.RECKONDATE", r.RECKONDATE)
  1758. } else if r.QueryType == 2 {
  1759. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1760. }
  1761. sqlId.Join(" GROUP BY t.RECKONDATE, t.AREAUSERID, t.CURRENCYID")
  1762. return sqlId.String()
  1763. }
  1764. func (r *Ermcp3ArealSumPL) buildSqlDayDetail() string {
  1765. var sqlId utils.SQLVal = "SELECT t.RECKONDATE," +
  1766. " t.AREAUSERID," +
  1767. " t.MIDDLEGOODSID," +
  1768. " t.CURRENCYID," +
  1769. " t.SPOTACTUALPL," +
  1770. " t.SPOTFLOATPL," +
  1771. " t.FUTUREACTUALPL," +
  1772. " t.FUTUREFLOATPL," +
  1773. " t.SUMACTUALPL," +
  1774. " t.SUMPL," +
  1775. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1776. " g.middlegoodsname," +
  1777. " g.middlegoodscode," +
  1778. " g.goodsunitid" +
  1779. " FROM RECKON_ERMCP_AREASUMPL t" +
  1780. " LEFT JOIN ERMS_MIDDLEGOODS g on t.middlegoodsid=g.middlegoodsid" +
  1781. " WHERE 1 = 1"
  1782. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  1783. sqlId.AndEx("t.RECKONDATE", r.RECKONDATE, len(r.RECKONDATE) > 0)
  1784. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  1785. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1786. }
  1787. sqlId.And("t.CURRENCYID", r.CURRENCYID)
  1788. return sqlId.String()
  1789. }
  1790. func (r *Ermcp3ArealSumPL) buildSqlCycle() string {
  1791. var sqlId utils.SQLVal = "SELECT t.AREAUSERID," +
  1792. " t.CYCLETIME," +
  1793. " t.CYCLETYPE," +
  1794. " t.CURRENCYID," +
  1795. " sum(t.SPOTACTUALPL) SPOTACTUALPL," +
  1796. " sum(t.SPOTFLOATPL) SPOTFLOATPL," +
  1797. " sum(t.FUTUREACTUALPL) FUTUREACTUALPL," +
  1798. " sum(t.FUTUREFLOATPL) FUTUREFLOATPL," +
  1799. " sum(t.SUMACTUALPL) SUMACTUALPL," +
  1800. " sum(t.SUMPL) SUMPL" +
  1801. " FROM REPORT_ERMCP_AREASUMPL t" +
  1802. " WHERE 1 = 1"
  1803. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  1804. sqlId.And("t.cycletype", r.CYCLETYPE)
  1805. sqlId.And("t.cycletime", r.CYCLETIME)
  1806. sqlId.Join(" GROUP BY t.AREAUSERID, t.CURRENCYID, t.CYCLETIME, t.CYCLETYPE")
  1807. return sqlId.String()
  1808. }
  1809. // GetDataEx 获取损益汇总表
  1810. func (r *Ermcp3ArealSumPL) GetDataEx() (interface{}, error) {
  1811. sData := make([]Ermcp3ArealSumPL, 0)
  1812. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1813. for i := range sData {
  1814. sData[i].calc()
  1815. sData[i].BeginDate = r.BeginDate
  1816. sData[i].EndDate = r.EndDate
  1817. }
  1818. return sData, err
  1819. }
  1820. // Ermcp3TaFutuReDataReport 期货报表
  1821. type Ermcp3TaFutuReDataReport struct {
  1822. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照时期(yyyyMMdd)
  1823. CYCLETIME string `json:"cycletime" xorm:"CYCLETIME"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  1824. CYCLETYPE int32 `json:"cycletype" xorm:"CYCLETYPE"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  1825. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID"` // 交易账户
  1826. GOODSID int32 `json:"goodsid" xorm:"GOODSID"` // 交易合约
  1827. BUYORSELL int32 `json:"buyorsell" xorm:"BUYORSELL"` // 持仓方向
  1828. CURRENCYID int32 `json:"currencyid" xorm:"CURRENCYID"` // 结算币种ID
  1829. USERID int64 `json:"userid" xorm:"USERID"` // 账户所属用户ID
  1830. RELATEDUSERID int64 `json:"relateduserid" xorm:"RELATEDUSERID"` // 账户关联用户ID
  1831. GOODSGROUPID int32 `json:"goodsgroupid" xorm:"GOODSGROUPID"` // 交易品种
  1832. ORIHOLDQTY float64 `json:"oriholdqty" xorm:"ORIHOLDQTY"` // 期初持仓量
  1833. ORIOPENAMOUNT float64 `json:"oriopenamount" xorm:"ORIOPENAMOUNT"` // 期初开仓额
  1834. ORIHOLDAMOUNT float64 `json:"oriholdamount" xorm:"ORIHOLDAMOUNT"` // 期初持仓额
  1835. TODAYOPENQTY float64 `json:"todayopenqty" xorm:"TODAYOPENQTY"` // 今开仓量
  1836. TODAYOPENAMOUNT float64 `json:"todayopenamount" xorm:"TODAYOPENAMOUNT"` // 今开仓额
  1837. TODAYCLOSEQTY float64 `json:"todaycloseqty" xorm:"TODAYCLOSEQTY"` // 今平仓量
  1838. TODAYCLOSEAMOUNT float64 `json:"todaycloseamount" xorm:"TODAYCLOSEAMOUNT"` // 今平仓额
  1839. HOLDQTY float64 `json:"holdqty" xorm:"HOLDQTY"` // 期末持仓量
  1840. OPENAMOUNT float64 `json:"openamount" xorm:"OPENAMOUNT"` // 期末开仓额
  1841. HOLDAMOUNT float64 `json:"holdamount" xorm:"HOLDAMOUNT"` // 期末持仓额
  1842. RECKONPRICE float64 `json:"reckonprice" xorm:"RECKONPRICE"` // 结算价
  1843. RECKONHOLDAMOUNT float64 `json:"reckonholdamount" xorm:"RECKONHOLDAMOUNT"` // 结算持仓额
  1844. CHARGE float64 `json:"charge" xorm:"CHARGE"` // 手续费
  1845. CLOSEPL float64 `json:"closepl" xorm:"CLOSEPL"` // 平仓损益
  1846. RECKONPL float64 `json:"reckonpl" xorm:"RECKONPL"` // 结算损益
  1847. LASTPRICE float64 `json:"lastprice" xorm:"LASTPRICE"` // 最新价
  1848. LASTHOLDAMOUNT float64 `json:"lastholdamount" xorm:"LASTHOLDAMOUNT"` // 当前持仓额
  1849. FLOATPL float64 `json:"floatpl" xorm:"FLOATPL"` // 浮动损益
  1850. GOODSGROUPSPOTQTY float64 `json:"goodsgroupspotqty" xorm:"GOODSGROUPSPOTQTY"` // 交易品种期末量 (= 期末持仓量 * 合约乘数)
  1851. TODAYGOODSGROUPSPOTQTY float64 `json:"todaygoodsgroupspotqty" xorm:"TODAYGOODSGROUPSPOTQTY"` // 交易品种变化量 (=(期末持仓量 - 期初持仓量)* 合约乘数)
  1852. MIDDLEGOODSQTY float64 `json:"middlegoodsqty" xorm:"MIDDLEGOODSQTY"` // 套保品种期末量 (=交易品种期末量 * 期货品种折算系数)
  1853. TODAYMIDDLEGOODSQTY float64 `json:"todaymiddlegoodsqty" xorm:"TODAYMIDDLEGOODSQTY"` // 套保品种变化量 (= 交易品种变化量*期货品种折算系数)
  1854. RECKONPL2 float64 `json:"reckonpl2" xorm:"RECKONPL2"` // 结算逐笔盈亏
  1855. OUTERGROUPCODE string `json:"outergroupcode" xorm:"'OUTERGROUPCODE'"` // 交易品种代码
  1856. GOODSGROUPNAME string `json:"goodsgroupname" xorm:"'GOODSGROUPNAME'"` // 交易品种名称
  1857. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 交易合约代码
  1858. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 交易合约名称
  1859. GOODUNITID int32 `json:"goodunitid" xorm:"'GOODUNITID'"` // 交易合约单位id
  1860. ACCOUNTNAME string `json:"accountname" xorm:"'ACCOUNTNAME'"` // 交易账号名称
  1861. ENUMDICNAME string `json:"enumdicname"` // 单位名称(现货商品)
  1862. QueryType int32 `json:"-"` // 查询类型 1-报表 2-报表明细
  1863. BeginDate string `json:"begindate"` // 开始交易日
  1864. EndDate string `json:"enddate"` // 结束交易日
  1865. }
  1866. func (r *Ermcp3TaFutuReDataReport) calc() {
  1867. }
  1868. func (r *Ermcp3TaFutuReDataReport) userType() int32 {
  1869. var userType int32 = 2
  1870. areaUserId := mtpcache.GetAreaUserId(r.USERID, 0)
  1871. if areaUserId == r.USERID {
  1872. userType = 2
  1873. } else {
  1874. userType = 7
  1875. }
  1876. return userType
  1877. }
  1878. func (r *Ermcp3TaFutuReDataReport) buildSql() string {
  1879. userType := r.userType()
  1880. // 日报表
  1881. if r.QueryType == 1 && r.CYCLETYPE == 0 {
  1882. return r.buildSqlReckon(userType)
  1883. }
  1884. // 日报表明细
  1885. if r.QueryType == 2 && r.CYCLETYPE == 0 {
  1886. return r.buildSqlReckonDetail(userType)
  1887. }
  1888. return r.buildSqlReport(userType)
  1889. }
  1890. // buildSqlReckon 日照表
  1891. func (r *Ermcp3TaFutuReDataReport) buildSqlReckon(userType int32) string {
  1892. var sqlId utils.SQLVal = "select a.*," +
  1893. " gp.outergroupcode," +
  1894. " gp.goodsgroupname," +
  1895. " g.goodscode," +
  1896. " g.goodsname," +
  1897. " g.goodunitid" +
  1898. " from (SELECT t.RECKONDATE," +
  1899. " t.GOODSID," +
  1900. " t.BUYORSELL," +
  1901. " t.CURRENCYID," +
  1902. " t.%v USERID," +
  1903. " t.GOODSGROUPID," +
  1904. " sum(t.ORIHOLDQTY) ORIHOLDQTY," +
  1905. " sum(t.ORIOPENAMOUNT) ORIOPENAMOUNT," +
  1906. " sum(t.ORIHOLDAMOUNT) ORIHOLDAMOUNT," +
  1907. " sum(t.TODAYOPENQTY) TODAYOPENQTY," +
  1908. " sum(t.TODAYOPENAMOUNT) TODAYOPENAMOUNT," +
  1909. " sum(t.TODAYCLOSEQTY) TODAYCLOSEQTY," +
  1910. " sum(t.TODAYCLOSEAMOUNT) TODAYCLOSEAMOUNT," +
  1911. " sum(t.HOLDQTY) HOLDQTY," +
  1912. " sum(t.OPENAMOUNT) OPENAMOUNT," +
  1913. " sum(t.HOLDAMOUNT) HOLDAMOUNT," +
  1914. " sum(t.RECKONPRICE) RECKONPRICE," +
  1915. " sum(t.RECKONHOLDAMOUNT) RECKONHOLDAMOUNT," +
  1916. " sum(t.CHARGE) CHARGE," +
  1917. " sum(t.CLOSEPL) CLOSEPL," +
  1918. " sum(t.RECKONPL) RECKONPL," +
  1919. " max(t.LASTPRICE) LASTPRICE," +
  1920. " sum(t.LASTHOLDAMOUNT) LASTHOLDAMOUNT," +
  1921. " sum(t.FLOATPL) FLOATPL," +
  1922. " sum(t.GOODSGROUPSPOTQTY) GOODSGROUPSPOTQTY," +
  1923. " sum(t.TODAYGOODSGROUPSPOTQTY) TODAYGOODSGROUPSPOTQTY," +
  1924. " sum(t.MIDDLEGOODSQTY) MIDDLEGOODSQTY," +
  1925. " sum(t.TODAYMIDDLEGOODSQTY) TODAYMIDDLEGOODSQTY," +
  1926. " sum(t.Reckonpl2) RECKONPL2" +
  1927. " FROM RECKON_ERMCP_TAFUTUREDATA t" +
  1928. " WHERE 1 = 1 %v" +
  1929. " GROUP BY t.RECKONDATE," +
  1930. " t.GOODSID," +
  1931. " t.BUYORSELL," +
  1932. " t.CURRENCYID," +
  1933. " t.%v," +
  1934. " t.GOODSGROUPID) a" +
  1935. " left join goodsgroup gp" +
  1936. " on a.goodsgroupid = gp.goodsgroupid" +
  1937. " left join goods g" +
  1938. " on a.goodsid = g.goodsid"
  1939. userId := "USERID"
  1940. var sqlParam utils.SQLVal
  1941. if userType == 2 {
  1942. sqlParam.AndEx("t.userid", r.USERID, userType == 2)
  1943. userId = "USERID"
  1944. } else if userType == 7 {
  1945. sqlParam.AndEx("t.relateduserid", r.USERID, userType == 7)
  1946. userId = "RELATEDUSERID"
  1947. }
  1948. sqlParam.AndEx("t.reckondate", r.RECKONDATE, len(r.RECKONDATE) > 0)
  1949. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  1950. sqlParam.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1951. }
  1952. sqlId.FormatParam(userId, sqlParam.String(), userId)
  1953. return sqlId.String()
  1954. }
  1955. // buildSqlReckon 日照表明细
  1956. func (r *Ermcp3TaFutuReDataReport) buildSqlReckonDetail(userType int32) string {
  1957. var sqlId utils.SQLVal = "select a.*," +
  1958. " gp.outergroupcode," +
  1959. " gp.goodsgroupname," +
  1960. " g.goodscode," +
  1961. " g.goodsname," +
  1962. " g.goodunitid," +
  1963. " ta.accountname" +
  1964. " from (SELECT t.reckondate," +
  1965. " t.GOODSID," +
  1966. " t.BUYORSELL," +
  1967. " t.CURRENCYID," +
  1968. " t.%v USERID," +
  1969. " t.accountid," +
  1970. " t.GOODSGROUPID," +
  1971. " sum(t.ORIHOLDQTY) ORIHOLDQTY," +
  1972. " sum(t.ORIOPENAMOUNT) ORIOPENAMOUNT," +
  1973. " sum(t.ORIHOLDAMOUNT) ORIHOLDAMOUNT," +
  1974. " sum(t.TODAYOPENQTY) TODAYOPENQTY," +
  1975. " sum(t.TODAYOPENAMOUNT) TODAYOPENAMOUNT," +
  1976. " sum(t.TODAYCLOSEQTY) TODAYCLOSEQTY," +
  1977. " sum(t.TODAYCLOSEAMOUNT) TODAYCLOSEAMOUNT," +
  1978. " sum(t.HOLDQTY) HOLDQTY," +
  1979. " sum(t.OPENAMOUNT) OPENAMOUNT," +
  1980. " sum(t.HOLDAMOUNT) HOLDAMOUNT," +
  1981. " sum(t.RECKONPRICE) RECKONPRICE," +
  1982. " sum(t.RECKONHOLDAMOUNT) RECKONHOLDAMOUNT," +
  1983. " sum(t.CHARGE) CHARGE," +
  1984. " sum(t.CLOSEPL) CLOSEPL," +
  1985. " sum(t.RECKONPL) RECKONPL," +
  1986. " max(t.LASTPRICE) LASTPRICE," +
  1987. " sum(t.LASTHOLDAMOUNT) LASTHOLDAMOUNT," +
  1988. " sum(t.FLOATPL) FLOATPL," +
  1989. " sum(t.GOODSGROUPSPOTQTY) GOODSGROUPSPOTQTY," +
  1990. " sum(t.TODAYGOODSGROUPSPOTQTY) TODAYGOODSGROUPSPOTQTY," +
  1991. " sum(t.MIDDLEGOODSQTY) MIDDLEGOODSQTY," +
  1992. " sum(t.TODAYMIDDLEGOODSQTY) TODAYMIDDLEGOODSQTY," +
  1993. " sum(t.Reckonpl2) RECKONPL2" +
  1994. " FROM RECKON_ERMCP_TAFUTUREDATA t" +
  1995. " WHERE 1 = 1 %v" +
  1996. " GROUP BY t.reckondate," +
  1997. " t.GOODSID," +
  1998. " t.BUYORSELL," +
  1999. " t.CURRENCYID," +
  2000. " t.%v," +
  2001. " t.accountid," +
  2002. " t.GOODSGROUPID) a" +
  2003. " left join goodsgroup gp" +
  2004. " on a.goodsgroupid = gp.goodsgroupid" +
  2005. " left join goods g" +
  2006. " on a.goodsid = g.goodsid" +
  2007. " left join taaccount ta on a.accountid = ta.accountid"
  2008. userId := "USERID"
  2009. var sqlParam utils.SQLVal
  2010. if userType == 2 {
  2011. sqlParam.AndEx("t.userid", r.USERID, userType == 2)
  2012. userId = "USERID"
  2013. } else if userType == 7 {
  2014. sqlParam.AndEx("t.relateduserid", r.USERID, userType == 7)
  2015. userId = "RELATEDUSERID"
  2016. }
  2017. sqlParam.And("t.CURRENCYID", r.CURRENCYID)
  2018. sqlParam.And("t.GOODSGROUPID", r.GOODSGROUPID)
  2019. sqlParam.And("t.GOODSID", r.GOODSID)
  2020. sqlParam.And("t.BUYORSELL", r.BUYORSELL)
  2021. sqlParam.AndEx("t.reckondate", r.RECKONDATE, len(r.RECKONDATE) > 0)
  2022. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  2023. sqlParam.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  2024. }
  2025. sqlId.FormatParam(userId, sqlParam.String(), userId)
  2026. return sqlId.String()
  2027. }
  2028. // buildSqlReckon 报表表
  2029. func (r *Ermcp3TaFutuReDataReport) buildSqlReport(userType int32) string {
  2030. var sqlId utils.SQLVal = "select a.*," +
  2031. " gp.outergroupcode," +
  2032. " gp.goodsgroupname," +
  2033. " g.goodscode," +
  2034. " g.goodsname," +
  2035. " g.goodunitid" +
  2036. " from (SELECT t.cycletype," +
  2037. " t.cycletime," +
  2038. " t.GOODSID," +
  2039. " t.BUYORSELL," +
  2040. " t.CURRENCYID," +
  2041. " t.%v USERID," +
  2042. " t.GOODSGROUPID," +
  2043. " sum(t.ORIHOLDQTY) ORIHOLDQTY," +
  2044. " sum(t.ORIOPENAMOUNT) ORIOPENAMOUNT," +
  2045. " sum(t.ORIHOLDAMOUNT) ORIHOLDAMOUNT," +
  2046. " sum(t.TODAYOPENQTY) TODAYOPENQTY," +
  2047. " sum(t.TODAYOPENAMOUNT) TODAYOPENAMOUNT," +
  2048. " sum(t.TODAYCLOSEQTY) TODAYCLOSEQTY," +
  2049. " sum(t.TODAYCLOSEAMOUNT) TODAYCLOSEAMOUNT," +
  2050. " sum(t.HOLDQTY) HOLDQTY," +
  2051. " sum(t.OPENAMOUNT) OPENAMOUNT," +
  2052. " sum(t.HOLDAMOUNT) HOLDAMOUNT," +
  2053. " sum(t.RECKONPRICE) RECKONPRICE," +
  2054. " sum(t.RECKONHOLDAMOUNT) RECKONHOLDAMOUNT," +
  2055. " sum(t.CHARGE) CHARGE," +
  2056. " sum(t.CLOSEPL) CLOSEPL," +
  2057. " sum(t.RECKONPL) RECKONPL," +
  2058. " max(t.LASTPRICE) LASTPRICE," +
  2059. " sum(t.LASTHOLDAMOUNT) LASTHOLDAMOUNT," +
  2060. " sum(t.FLOATPL) FLOATPL," +
  2061. " sum(t.GOODSGROUPSPOTQTY) GOODSGROUPSPOTQTY," +
  2062. " sum(t.TODAYGOODSGROUPSPOTQTY) TODAYGOODSGROUPSPOTQTY," +
  2063. " sum(t.MIDDLEGOODSQTY) MIDDLEGOODSQTY," +
  2064. " sum(t.TODAYMIDDLEGOODSQTY) TODAYMIDDLEGOODSQTY," +
  2065. " sum(t.Reckonpl2) RECKONPL2" +
  2066. " FROM REPORT_ERMCP_TAFUTUREDATA t" +
  2067. " WHERE 1 = 1 %v" +
  2068. " GROUP BY t.cycletype," +
  2069. " t.cycletime," +
  2070. " t.GOODSID," +
  2071. " t.BUYORSELL," +
  2072. " t.CURRENCYID," +
  2073. " t.%v," +
  2074. " t.GOODSGROUPID) a" +
  2075. " left join goodsgroup gp" +
  2076. " on a.goodsgroupid = gp.goodsgroupid" +
  2077. " left join goods g" +
  2078. " on a.goodsid = g.goodsid"
  2079. userId := "USERID"
  2080. var sqlParam utils.SQLVal
  2081. if userType == 2 {
  2082. sqlParam.AndEx("t.userid", r.USERID, userType == 2)
  2083. userId = "USERID"
  2084. } else if userType == 7 {
  2085. sqlParam.AndEx("t.relateduserid", r.USERID, userType == 7)
  2086. userId = "RELATEDUSERID"
  2087. }
  2088. sqlParam.And("t.cycletype", r.CYCLETYPE)
  2089. sqlParam.And("t.cycletime", r.CYCLETIME)
  2090. sqlId.FormatParam(userId, sqlParam.String(), userId)
  2091. return sqlId.String()
  2092. }
  2093. // GetDataEx 获取期货报表
  2094. func (r *Ermcp3TaFutuReDataReport) GetDataEx() (interface{}, error) {
  2095. sData := make([]Ermcp3TaFutuReDataReport, 0)
  2096. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2097. for i := range sData {
  2098. sData[i].calc()
  2099. sData[i].BeginDate = r.BeginDate
  2100. sData[i].EndDate = r.EndDate
  2101. }
  2102. return sData, err
  2103. }
  2104. // ReckonDayPosition 持仓头寸日照
  2105. type ReckonDayPosition struct {
  2106. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID"` // 账号Id
  2107. GOODSID int32 `json:"goodsid" xorm:"GOODSID"` // 商品Id
  2108. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照日期(yyyyMMdd)
  2109. BUYPOSITIONQTY int32 `json:"buypositionqty" xorm:"BUYPOSITIONQTY"` // 买期初持仓数量
  2110. BUYHOLDERAMOUNT float64 `json:"buyholderamount" xorm:"BUYHOLDERAMOUNT"` // 买持仓期初总金额
  2111. BUYCURPOSITIONQTY int32 `json:"buycurpositionqty" xorm:"BUYCURPOSITIONQTY"` // 买当前持仓总数量
  2112. BUYCURHOLDERAMOUNT float64 `json:"buycurholderamount" xorm:"BUYCURHOLDERAMOUNT"` // 买当前持仓总金额
  2113. BUYFROZENQTY int32 `json:"buyfrozenqty" xorm:"BUYFROZENQTY"` // 买持仓冻结
  2114. BUYOTHERFROZENQTY int32 `json:"buyotherfrozenqty" xorm:"BUYOTHERFROZENQTY"` // 买持仓其他冻结(交割冻结)
  2115. BUYOPENREQQTY int32 `json:"buyopenreqqty" xorm:"BUYOPENREQQTY"` // 买开仓申请数量
  2116. BUYRECKONPL float64 `json:"buyreckonpl" xorm:"BUYRECKONPL"` // 买结算盈亏
  2117. BUYINTEREST float64 `json:"buyinterest" xorm:"BUYINTEREST"` // 买递延费(仓储费)
  2118. BUYUSEDMARGIN float64 `json:"buyusedmargin" xorm:"BUYUSEDMARGIN"` // 占用保证金
  2119. BUYOPENTOTALQTY int32 `json:"buyopentotalqty" xorm:"BUYOPENTOTALQTY"` // 今日买开仓总数量
  2120. BUYCLOSETOTALQTY int32 `json:"buyclosetotalqty" xorm:"BUYCLOSETOTALQTY"` // 今日买平仓总数量
  2121. BUYCLOSETOTALPL float64 `json:"buyclosetotalpl" xorm:"BUYCLOSETOTALPL"` // 买平仓总盈亏
  2122. SELLPOSITIONQTY int32 `json:"sellpositionqty" xorm:"SELLPOSITIONQTY"` // 卖期初持仓数量
  2123. SELLHOLDERAMOUNT float64 `json:"sellholderamount" xorm:"SELLHOLDERAMOUNT"` // 卖持仓期初总金额
  2124. SELLCURPOSITIONQTY int32 `json:"sellcurpositionqty" xorm:"SELLCURPOSITIONQTY"` // 卖当前持仓数量
  2125. SELLCURHOLDERAMOUNT float64 `json:"sellcurholderamount" xorm:"SELLCURHOLDERAMOUNT"` // 卖当前持仓总金额
  2126. SELLFROZENQTY int32 `json:"sellfrozenqty" xorm:"SELLFROZENQTY"` // 卖持仓冻结
  2127. SELLOTHERFROZENQTY int32 `json:"sellotherfrozenqty" xorm:"SELLOTHERFROZENQTY"` // 卖持仓其他冻结(交割冻结)
  2128. SELLOPENREQQTY int32 `json:"sellopenreqqty" xorm:"SELLOPENREQQTY"` // 卖开仓申请数量
  2129. SELLRECKONPL float64 `json:"sellreckonpl" xorm:"SELLRECKONPL"` // 卖结算盈亏
  2130. SELLINTEREST float64 `json:"sellinterest" xorm:"SELLINTEREST"` // 卖递延费
  2131. SELLUSEDMARGIN float64 `json:"sellusedmargin" xorm:"SELLUSEDMARGIN"` // 卖占用保证金-作废
  2132. SELLOPENTOTALQTY int32 `json:"sellopentotalqty" xorm:"SELLOPENTOTALQTY"` // 卖开仓总数量
  2133. SELLCLOSETOTALQTY int32 `json:"sellclosetotalqty" xorm:"SELLCLOSETOTALQTY"` // 卖平仓总数量
  2134. SELLCLOSETOTALPL float64 `json:"sellclosetotalpl" xorm:"SELLCLOSETOTALPL"` // 卖平仓总盈亏
  2135. TRADEPROPERTY int32 `json:"tradeproperty" xorm:"TRADEPROPERTY"` // 交易属性
  2136. MARGINALGORITHM int32 `json:"marginalgorithm" xorm:"MARGINALGORITHM"` // 保证金收取方式 - 1:比率 2:固定
  2137. MARGINVALUE float64 `json:"marginvalue" xorm:"MARGINVALUE"` // 保证金设置值(投资管理系统-多LongMarginValue)
  2138. BUYFEEALGORITHM int32 `json:"buyfeealgorithm" xorm:"BUYFEEALGORITHM"` // 买递延费收取方式 - 1:比率 2:固定
  2139. BUYMEMBERFEEVALUE float64 `json:"buymemberfeevalue" xorm:"BUYMEMBERFEEVALUE"` // 买会员递延费设置值
  2140. BUYEXCHAGEFEEVALUE float64 `json:"buyexchagefeevalue" xorm:"BUYEXCHAGEFEEVALUE"` // 买交易所递延费设置值
  2141. SELLFEEALGORITHM int32 `json:"sellfeealgorithm" xorm:"SELLFEEALGORITHM"` // 卖递延费收取方式 - 1:比率 2:固定
  2142. SELLMEMBERFEEVALUE float64 `json:"sellmemberfeevalue" xorm:"SELLMEMBERFEEVALUE"` // 卖会员递延费设置值
  2143. SELLEXCHAGEFEEVALUE float64 `json:"sellexchagefeevalue" xorm:"SELLEXCHAGEFEEVALUE"` // 卖交易所递延费设置值
  2144. INTEREST2 float64 `json:"interest2" xorm:"INTEREST2"` // 过夜费
  2145. ACCOUNTCURRENCYID int32 `json:"accountcurrencyid" xorm:"ACCOUNTCURRENCYID"` // 账户币种ID
  2146. GOODSCURRENCYID int32 `json:"goodscurrencyid" xorm:"GOODSCURRENCYID"` // 商品币种ID
  2147. CUREXCHANGERATE float64 `json:"curexchangerate" xorm:"CUREXCHANGERATE"` // 当前汇率
  2148. BUYRECKONPL2 float64 `json:"buyreckonpl2" xorm:"BUYRECKONPL2"` // 买结算盈亏(逐笔) - 根据持仓单日照表字段汇率
  2149. SELLRECKONPL2 float64 `json:"sellreckonpl2" xorm:"SELLRECKONPL2"` // 结算盈亏(逐笔) - 根据持仓单日照表字段汇率
  2150. BUYCURTDPOSITION int32 `json:"buycurtdposition" xorm:"BUYCURTDPOSITION"` // 买期末今日头寸
  2151. BUYFRETDPOSITION int32 `json:"buyfretdposition" xorm:"BUYFRETDPOSITION"` // 买冻结今日头寸
  2152. SELLCURTDPOSITION int32 `json:"sellcurtdposition" xorm:"SELLCURTDPOSITION"` // 卖期末今日头寸
  2153. SELLFRETDPOSITION int32 `json:"sellfretdposition" xorm:"SELLFRETDPOSITION"` // 卖冻结今日头寸
  2154. INTERESTCHARGE float64 `json:"interestcharge" xorm:"INTERESTCHARGE"` // 利息(110)
  2155. SHORTMARGINVALUE float64 `json:"shortmarginvalue" xorm:"SHORTMARGINVALUE"` // 保证金设置值(投资管理系统-空)
  2156. BUYDEFERCHARGE float64 `json:"buydefercharge" xorm:"BUYDEFERCHARGE"` // 买递延费2(仓储费2)
  2157. SELLDEFERCHARGE float64 `json:"selldefercharge" xorm:"SELLDEFERCHARGE"` // 卖递延费2
  2158. RELATEDUSERID int64 `json:"relateduserid" xorm:"'RELATEDUSERID'"` // 关联用户id
  2159. MIDDLEGOODSID int32 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'"` // 套保商品id
  2160. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保商品代码
  2161. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保商品名称
  2162. NEEDHEDGERATIO float64 `json:"needhedgeratio" xorm:"'NEEDHEDGERATIO'"` // 应套保比例
  2163. NEEDARBITRAGERATIO float64 `json:"needarbitrageratio" xorm:"'NEEDARBITRAGERATIO'"` // 应套利比例
  2164. GOODSGROUPID int32 `json:"goodsgroupid" xorm:"'GOODSGROUPID'"` // 交易品种id
  2165. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 折算系数
  2166. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 交易商品代码
  2167. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 交易商品名称
  2168. GOODUNITID int32 `json:"goodunitid" xorm:"'GOODUNITID'"` // 交易商品单位id
  2169. AGREEUNIT float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约乘数
  2170. ENUMDICNAME string `json:"enumdicname"` // 单位名称(交易商品)
  2171. ACCOUNTNAME string `json:"accountname" xorm:"'ACCOUNTNAME'"` // 交易账户
  2172. DiffBuyQty float64 `json:"diffbuyqty"` // 交易品种今变化量(买)
  2173. DiffMgBuyQty float64 `json:"diffmgbuyqty"` // 套保品种今变化量(买)
  2174. DiffNeedMgBuyQty float64 `json:"diffneedmgbuyqty"` // 应套保总量变化量(买)
  2175. DiffSellQty float64 `json:"diffsellqty"` // 交易品种今变化量(卖)
  2176. DiffMgSellQty float64 `json:"diffmgsellqty"` // 套保品种今变化量(卖)
  2177. DiffNeedMgSellQty float64 `json:"diffneedmgsellqty"` // 应套保总量变化量(卖)
  2178. BeginDate string `json:"begindate"` // 开始交易日
  2179. EndDate string `json:"enddate"` // 结束交易日
  2180. }
  2181. func (r *ReckonDayPosition) calc() {
  2182. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.GOODUNITID)
  2183. if r.BUYPOSITIONQTY > 0 || r.BUYCURPOSITIONQTY > 0 {
  2184. r.DiffBuyQty = float64(r.BUYCURPOSITIONQTY) * r.AGREEUNIT
  2185. r.DiffMgBuyQty = float64(r.BUYCURPOSITIONQTY) * r.AGREEUNIT * r.CONVERTRATIO
  2186. r.DiffNeedMgBuyQty = float64(r.BUYCURPOSITIONQTY) * r.AGREEUNIT * r.CONVERTRATIO * r.NEEDHEDGERATIO
  2187. }
  2188. if r.SELLPOSITIONQTY > 0 || r.SELLCURPOSITIONQTY > 0 {
  2189. r.DiffBuyQty = float64(r.SELLCURPOSITIONQTY) * r.AGREEUNIT
  2190. r.DiffMgBuyQty = float64(r.SELLCURPOSITIONQTY) * r.AGREEUNIT * r.CONVERTRATIO
  2191. r.DiffNeedMgBuyQty = float64(r.SELLCURPOSITIONQTY) * r.AGREEUNIT * r.CONVERTRATIO * r.NEEDHEDGERATIO
  2192. }
  2193. }
  2194. func (r *ReckonDayPosition) buildSql() string {
  2195. var sqlId utils.SQLVal = "with tmp as" +
  2196. " (select t.middlegoodsid," +
  2197. " t.middlegoodscode," +
  2198. " t.middlegoodsname," +
  2199. " t.needhedgeratio," +
  2200. " t.needarbitrageratio," +
  2201. " t.goodsgroupid," +
  2202. " c.convertratio," +
  2203. " g.goodsid," +
  2204. " g.goodscode," +
  2205. " g.goodsname," +
  2206. " g.agreeunit," +
  2207. " g.goodunitid" +
  2208. " from erms_middlegoods t" +
  2209. " inner join ermcp_ggconvertconfig c" +
  2210. " on t.goodsgroupid = c.destgoodsgroupid" +
  2211. " inner join goods g" +
  2212. " on g.goodsgroupid = c.srcgoodsgroupid)" +
  2213. "SELECT t.ACCOUNTID," +
  2214. " t.GOODSID," +
  2215. " t.RECKONDATE," +
  2216. " t.BUYPOSITIONQTY," +
  2217. " t.BUYHOLDERAMOUNT," +
  2218. " t.BUYCURPOSITIONQTY," +
  2219. " t.BUYCURHOLDERAMOUNT," +
  2220. " t.BUYFROZENQTY," +
  2221. " t.BUYOTHERFROZENQTY," +
  2222. " t.BUYOPENREQQTY," +
  2223. " t.BUYRECKONPL," +
  2224. " t.BUYINTEREST," +
  2225. " t.BUYUSEDMARGIN," +
  2226. " t.BUYOPENTOTALQTY," +
  2227. " t.BUYCLOSETOTALQTY," +
  2228. " t.BUYCLOSETOTALPL," +
  2229. " t.SELLPOSITIONQTY," +
  2230. " t.SELLHOLDERAMOUNT," +
  2231. " t.SELLCURPOSITIONQTY," +
  2232. " t.SELLCURHOLDERAMOUNT," +
  2233. " t.SELLFROZENQTY," +
  2234. " t.SELLOTHERFROZENQTY," +
  2235. " t.SELLOPENREQQTY," +
  2236. " t.SELLRECKONPL," +
  2237. " t.SELLINTEREST," +
  2238. " t.SELLUSEDMARGIN," +
  2239. " t.SELLOPENTOTALQTY," +
  2240. " t.SELLCLOSETOTALQTY," +
  2241. " t.SELLCLOSETOTALPL," +
  2242. " t.TRADEPROPERTY," +
  2243. " t.MARGINALGORITHM," +
  2244. " t.MARGINVALUE," +
  2245. " t.BUYFEEALGORITHM," +
  2246. " t.BUYMEMBERFEEVALUE," +
  2247. " t.BUYEXCHAGEFEEVALUE," +
  2248. " t.SELLFEEALGORITHM," +
  2249. " t.SELLMEMBERFEEVALUE," +
  2250. " t.SELLEXCHAGEFEEVALUE," +
  2251. " t.INTEREST2," +
  2252. " t.ACCOUNTCURRENCYID," +
  2253. " t.GOODSCURRENCYID," +
  2254. " t.CUREXCHANGERATE," +
  2255. " t.BUYRECKONPL2," +
  2256. " t.SELLRECKONPL2," +
  2257. " t.BUYCURTDPOSITION," +
  2258. " t.BUYFRETDPOSITION," +
  2259. " t.SELLCURTDPOSITION," +
  2260. " t.SELLFRETDPOSITION," +
  2261. " t.INTERESTCHARGE," +
  2262. " t.SHORTMARGINVALUE," +
  2263. " t.BUYDEFERCHARGE," +
  2264. " t.SELLDEFERCHARGE," +
  2265. " ta.relateduserid," +
  2266. " ta.accountname," +
  2267. " tmp.*" +
  2268. " FROM RECKON_DAYPOSITION t" +
  2269. " INNER JOIN tmp" +
  2270. " on t.goodsid = tmp.goodsid" +
  2271. " INNER JOIN TAACCOUNT ta" +
  2272. " on t.accountid = ta.accountid" +
  2273. " WHERE 1 = 1" +
  2274. " and (t.buypositionqty > 0 or t.buycurpositionqty > 0 or t.SELLPOSITIONQTY > 0 or t.SELLCURPOSITIONQTY > 0)"
  2275. sqlId.And("ta.RELATEDUSERID", r.RELATEDUSERID)
  2276. sqlId.And("tmp.MIDDLEGOODSID", r.MIDDLEGOODSID)
  2277. sqlId.AndEx("t.RECKONDATE", r.RECKONDATE, len(r.RECKONDATE) > 0)
  2278. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  2279. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  2280. }
  2281. return sqlId.String()
  2282. }
  2283. // GetDataEx 获取持仓头寸日照
  2284. func (r *ReckonDayPosition) GetDataEx() ([]ReckonDayPosition, error) {
  2285. sData := make([]ReckonDayPosition, 0)
  2286. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2287. for i := range sData {
  2288. sData[i].calc()
  2289. }
  2290. return sData, err
  2291. }
  2292. // HISOUTTRADEPOSITION 外部头寸日照
  2293. type HISOUTTRADEPOSITION struct {
  2294. HISTRADEDATE string `json:"histradedate" xorm:"'HISTRADEDATE'"` // 日期
  2295. ISVALIDDATA int32 `json:"isvaliddata" xorm:"'ISVALIDDATA'"` // 是否有效
  2296. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID"` // 资金账号[外部母账户]
  2297. HEDGEGOODSID int32 `json:"hedgegoodsid" xorm:"HEDGEGOODSID"` // 对冲合约ID
  2298. HEDGEACCOUNTCODE string `json:"hedgeaccountcode" xorm:"HEDGEACCOUNTCODE"` // 对冲账号
  2299. TRADEDATE string `json:"tradedate" xorm:"TRADEDATE"` // 交易日(yyyyMMdd)
  2300. MARKETID int32 `json:"marketid" xorm:"MARKETID"` // 市场ID
  2301. YDBUYPOSITION int32 `json:"ydbuyposition" xorm:"YDBUYPOSITION"` // 期初买头寸
  2302. CURBUYPOSITION int32 `json:"curbuyposition" xorm:"CURBUYPOSITION"` // 期末买头寸
  2303. CURYDBUYPOSITION int32 `json:"curydbuyposition" xorm:"CURYDBUYPOSITION"` // 期末上日买头寸
  2304. CURTDBUYPOSITION int32 `json:"curtdbuyposition" xorm:"CURTDBUYPOSITION"` // 期末今日买头寸
  2305. FREYDBUYPOSITION int32 `json:"freydbuyposition" xorm:"FREYDBUYPOSITION"` // 冻结上日买头寸
  2306. FRETDBUYPOSITION int32 `json:"fretdbuyposition" xorm:"FRETDBUYPOSITION"` // 冻结今日买头寸
  2307. YDSELLPOSITION int32 `json:"ydsellposition" xorm:"YDSELLPOSITION"` // 期初卖头寸
  2308. CURSELLPOSITION int32 `json:"cursellposition" xorm:"CURSELLPOSITION"` // 期末卖头寸
  2309. CURYDSELLPOSITION int32 `json:"curydsellposition" xorm:"CURYDSELLPOSITION"` // 期末上日卖头寸
  2310. CURTDSELLPOSITION int32 `json:"curtdsellposition" xorm:"CURTDSELLPOSITION"` // 期末今日卖头寸
  2311. FREYDSELLPOSITION int32 `json:"freydsellposition" xorm:"FREYDSELLPOSITION"` // 冻结上日卖头寸
  2312. FRETDSELLPOSITION int32 `json:"fretdsellposition" xorm:"FRETDSELLPOSITION"` // 冻结今日卖头寸
  2313. BUYOPENCOST float64 `json:"buyopencost" xorm:"BUYOPENCOST"` // 买开仓成本
  2314. BUYPOSITIONCOST float64 `json:"buypositioncost" xorm:"BUYPOSITIONCOST"` // 买持仓成本
  2315. BUYUSEMARGIN float64 `json:"buyusemargin" xorm:"BUYUSEMARGIN"` // 买占用保证金
  2316. SELLOPENCOST float64 `json:"sellopencost" xorm:"SELLOPENCOST"` // 卖开仓成本
  2317. SELLPOSITIONCOST float64 `json:"sellpositioncost" xorm:"SELLPOSITIONCOST"` // 卖持仓成本
  2318. SELLUSEMARGIN float64 `json:"sellusemargin" xorm:"SELLUSEMARGIN"` // 卖占用保证金
  2319. RELATEDUSERID int64 `json:"relateduserid" xorm:"'RELATEDUSERID'"` // 关联用户id
  2320. MIDDLEGOODSID int32 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'"` // 套保商品id
  2321. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保商品代码
  2322. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保商品名称
  2323. NEEDHEDGERATIO float64 `json:"needhedgeratio" xorm:"'NEEDHEDGERATIO'"` // 应套保比例
  2324. NEEDARBITRAGERATIO float64 `json:"needarbitrageratio" xorm:"'NEEDARBITRAGERATIO'"` // 应套利比例
  2325. GOODSGROUPID int32 `json:"goodsgroupid" xorm:"'GOODSGROUPID'"` // 交易品种id
  2326. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 折算系数
  2327. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 交易商品代码
  2328. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 交易商品名称
  2329. GOODUNITID int32 `json:"goodunitid" xorm:"'GOODUNITID'"` // 交易商品单位id
  2330. AGREEUNIT float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约乘数
  2331. ENUMDICNAME string `json:"enumdicname"` // 单位名称(交易商品)
  2332. ACCOUNTNAME string `json:"accountname" xorm:"'ACCOUNTNAME'"` // 交易账户
  2333. DiffBuyQty float64 `json:"diffbuyqty"` // 交易品种今变化量(买)
  2334. DiffMgBuyQty float64 `json:"diffmgbuyqty"` // 套保品种今变化量(买)
  2335. DiffNeedMgBuyQty float64 `json:"diffneedmgbuyqty"` // 应套保总量变化量(买)
  2336. DiffSellQty float64 `json:"diffsellqty"` // 交易品种今变化量(卖)
  2337. DiffMgSellQty float64 `json:"diffmgsellqty"` // 套保品种今变化量(卖)
  2338. DiffNeedMgSellQty float64 `json:"diffneedmgsellqty"` // 应套保总量变化量(卖)
  2339. BeginDate string `json:"begindate"` // 开始交易日
  2340. EndDate string `json:"enddate"` // 结束交易日
  2341. }
  2342. func (r *HISOUTTRADEPOSITION) calc() {
  2343. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.GOODUNITID)
  2344. if r.YDBUYPOSITION > 0 || r.CURBUYPOSITION > 0 {
  2345. r.DiffBuyQty = float64(r.CURBUYPOSITION) * r.AGREEUNIT
  2346. r.DiffMgBuyQty = float64(r.CURBUYPOSITION) * r.AGREEUNIT * r.CONVERTRATIO
  2347. r.DiffNeedMgBuyQty = float64(r.CURBUYPOSITION) * r.AGREEUNIT * r.CONVERTRATIO * r.NEEDHEDGERATIO
  2348. }
  2349. if r.YDSELLPOSITION > 0 || r.CURSELLPOSITION > 0 {
  2350. r.DiffBuyQty = float64(r.CURSELLPOSITION) * r.AGREEUNIT
  2351. r.DiffMgBuyQty = float64(r.CURSELLPOSITION) * r.AGREEUNIT * r.CONVERTRATIO
  2352. r.DiffNeedMgBuyQty = float64(r.CURSELLPOSITION) * r.AGREEUNIT * r.CONVERTRATIO * r.NEEDHEDGERATIO
  2353. }
  2354. }
  2355. func (r *HISOUTTRADEPOSITION) buildSql() string {
  2356. var sqlId utils.SQLVal = "with tmp as" +
  2357. " (select t.middlegoodsid," +
  2358. " t.middlegoodscode," +
  2359. " t.middlegoodsname," +
  2360. " t.needhedgeratio," +
  2361. " t.needarbitrageratio," +
  2362. " t.goodsgroupid," +
  2363. " c.convertratio," +
  2364. " g.goodsid," +
  2365. " g.goodscode," +
  2366. " g.goodsname," +
  2367. " g.agreeunit," +
  2368. " g.goodunitid" +
  2369. " from erms_middlegoods t" +
  2370. " inner join ermcp_ggconvertconfig c" +
  2371. " on t.goodsgroupid = c.destgoodsgroupid" +
  2372. " inner join goods g" +
  2373. " on g.goodsgroupid = c.srcgoodsgroupid)" +
  2374. "SELECT t.HISTRADEDATE," +
  2375. " t.ISVALIDDATA," +
  2376. " t.ACCOUNTID," +
  2377. " t.HEDGEGOODSID," +
  2378. " t.HEDGEACCOUNTCODE," +
  2379. " t.TRADEDATE," +
  2380. " t.MARKETID," +
  2381. " t.YDBUYPOSITION," +
  2382. " t.CURBUYPOSITION," +
  2383. " t.CURYDBUYPOSITION," +
  2384. " t.CURTDBUYPOSITION," +
  2385. " t.FREYDBUYPOSITION," +
  2386. " t.FRETDBUYPOSITION," +
  2387. " t.YDSELLPOSITION," +
  2388. " t.CURSELLPOSITION," +
  2389. " t.CURYDSELLPOSITION," +
  2390. " t.CURTDSELLPOSITION," +
  2391. " t.FREYDSELLPOSITION," +
  2392. " t.FRETDSELLPOSITION," +
  2393. " t.BUYOPENCOST," +
  2394. " t.BUYPOSITIONCOST," +
  2395. " t.BUYUSEMARGIN," +
  2396. " t.SELLOPENCOST," +
  2397. " t.SELLPOSITIONCOST," +
  2398. " t.SELLUSEMARGIN," +
  2399. " ta.relateduserid," +
  2400. " ta.accountname," +
  2401. " tmp.*" +
  2402. " FROM HIS_HEDGE_OUTTRADEPOSITION t" +
  2403. " INNER JOIN tmp" +
  2404. " on t.HEDGEGOODSID = tmp.goodsid" +
  2405. " INNER JOIN TAACCOUNT ta" +
  2406. " on t.accountid = ta.accountid" +
  2407. " WHERE 1 = 1" +
  2408. " and (t.YDBUYPOSITION > 0 or t.CURBUYPOSITION > 0 or t.YDSELLPOSITION > 0 or t.CURSELLPOSITION > 0)"
  2409. sqlId.And("ta.RELATEDUSERID", r.RELATEDUSERID)
  2410. sqlId.And("tmp.MIDDLEGOODSID", r.MIDDLEGOODSID)
  2411. sqlId.AndEx("t.HISTRADEDATE", r.HISTRADEDATE, len(r.HISTRADEDATE) > 0)
  2412. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  2413. sqlId.Join(fmt.Sprintf(" and t.HISTRADEDATE >= '%v' and t.HISTRADEDATE <= '%v' ", r.BeginDate, r.EndDate))
  2414. }
  2415. return sqlId.String()
  2416. }
  2417. // GetDataEx 获取外部头寸日照
  2418. func (r *HISOUTTRADEPOSITION) GetDataEx() ([]HISOUTTRADEPOSITION, error) {
  2419. sData := make([]HISOUTTRADEPOSITION, 0)
  2420. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2421. for i := range sData {
  2422. sData[i].calc()
  2423. }
  2424. return sData, err
  2425. }
  2426. // Ermcp3ExposureFutuDetail 敞口报表期货明细
  2427. type Ermcp3ExposureFutuDetail struct {
  2428. ACCOUNTID int64 `json:"accountid"` // 交易账户id
  2429. ACCOUNTNAME string `json:"accountname"` // 交易账户
  2430. GOODSID int32 `json:"goodsid"` // 商品id
  2431. GOODSCODE string `json:"goodscode"` // 商品代码
  2432. GOODSNAME string `json:"goodsname"` // 商品名称
  2433. BUYORSELL int32 `json:"buyorsell"` // 方向 0-买 1-卖
  2434. ORIHOLDQTY float64 `json:"oriholdqty"` // 期初持仓量
  2435. HOLDQTY float64 `json:"holdqty"` // 期末持仓量
  2436. TODAYOPENQTY float64 `json:"todayopenqty"` // 今开仓量
  2437. TODAYCLOSEQTY float64 `json:"todaycloseqty"` // 今平仓量
  2438. DiffTradeQty float64 `json:"difftradeqty"` // 交易品种今变化量
  2439. DiffMgQty float64 `json:"diffmgqty"` // 套保品种今变化量
  2440. DiffNeedQty float64 `json:"diffneedqty"` // 应套保总量变化量
  2441. MIDDLEGOODSID int32 `json:"middlegoodsid"` // 套保商品id
  2442. MIDDLEGOODSCODE string `json:"middlegoodscode"` // 套保商品代码
  2443. MIDDLEGOODSNAME string `json:"middlegoodsname"` // 套保商品名称
  2444. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  2445. USERID int64 `json:"-"` // 用户id
  2446. RECKONDATE string `json:"tradedate"` // 交易日
  2447. BeginDate string `json:"begindate"` // 开始交易日
  2448. EndDate string `json:"enddate"` // 结束交易日
  2449. }
  2450. func (r *Ermcp3ExposureFutuDetail) fromOutPositionBuy(v *HISOUTTRADEPOSITION) {
  2451. r.ACCOUNTID = v.ACCOUNTID
  2452. r.ACCOUNTNAME = v.ACCOUNTNAME
  2453. r.GOODSID = v.HEDGEGOODSID
  2454. r.GOODSCODE = v.GOODSCODE
  2455. r.GOODSNAME = v.GOODSNAME
  2456. r.BUYORSELL = 0
  2457. r.ORIHOLDQTY = float64(v.YDBUYPOSITION)
  2458. r.HOLDQTY = float64(v.CURBUYPOSITION)
  2459. r.TODAYOPENQTY = 0
  2460. r.TODAYCLOSEQTY = 0
  2461. r.DiffTradeQty = v.DiffBuyQty
  2462. r.DiffMgQty = v.DiffMgBuyQty
  2463. r.DiffNeedQty = v.DiffNeedMgBuyQty
  2464. r.MIDDLEGOODSID = v.MIDDLEGOODSID
  2465. r.MIDDLEGOODSCODE = v.MIDDLEGOODSCODE
  2466. r.MIDDLEGOODSNAME = v.MIDDLEGOODSNAME
  2467. r.ENUMDICNAME = v.ENUMDICNAME
  2468. r.RECKONDATE = v.HISTRADEDATE
  2469. }
  2470. func (r *Ermcp3ExposureFutuDetail) fromOutPositionSell(v *HISOUTTRADEPOSITION) {
  2471. r.ACCOUNTID = v.ACCOUNTID
  2472. r.ACCOUNTNAME = v.ACCOUNTNAME
  2473. r.GOODSID = v.HEDGEGOODSID
  2474. r.GOODSCODE = v.GOODSCODE
  2475. r.GOODSNAME = v.GOODSNAME
  2476. r.BUYORSELL = 1
  2477. r.ORIHOLDQTY = float64(v.YDSELLPOSITION)
  2478. r.HOLDQTY = float64(v.CURSELLPOSITION)
  2479. r.TODAYOPENQTY = 0
  2480. r.TODAYCLOSEQTY = 0
  2481. r.DiffTradeQty = v.DiffSellQty
  2482. r.DiffMgQty = v.DiffMgSellQty
  2483. r.DiffNeedQty = v.DiffNeedMgSellQty
  2484. r.MIDDLEGOODSID = v.MIDDLEGOODSID
  2485. r.MIDDLEGOODSCODE = v.MIDDLEGOODSCODE
  2486. r.MIDDLEGOODSNAME = v.MIDDLEGOODSNAME
  2487. r.ENUMDICNAME = v.ENUMDICNAME
  2488. r.RECKONDATE = v.HISTRADEDATE
  2489. }
  2490. func (r *Ermcp3ExposureFutuDetail) fromTradePositionBuy(v *ReckonDayPosition) {
  2491. r.ACCOUNTID = v.ACCOUNTID
  2492. r.ACCOUNTNAME = v.ACCOUNTNAME
  2493. r.GOODSID = v.GOODSID
  2494. r.GOODSCODE = v.GOODSCODE
  2495. r.GOODSNAME = v.GOODSNAME
  2496. r.BUYORSELL = 0
  2497. r.ORIHOLDQTY = float64(v.BUYPOSITIONQTY)
  2498. r.HOLDQTY = float64(v.BUYCURPOSITIONQTY)
  2499. r.TODAYOPENQTY = 0
  2500. r.TODAYCLOSEQTY = 0
  2501. r.DiffTradeQty = v.DiffBuyQty
  2502. r.DiffMgQty = v.DiffMgBuyQty
  2503. r.DiffNeedQty = v.DiffNeedMgBuyQty
  2504. r.MIDDLEGOODSID = v.MIDDLEGOODSID
  2505. r.MIDDLEGOODSCODE = v.MIDDLEGOODSCODE
  2506. r.MIDDLEGOODSNAME = v.MIDDLEGOODSNAME
  2507. r.ENUMDICNAME = v.ENUMDICNAME
  2508. r.RECKONDATE = v.RECKONDATE
  2509. }
  2510. func (r *Ermcp3ExposureFutuDetail) fromTradePositionSell(v *ReckonDayPosition) {
  2511. r.ACCOUNTID = v.ACCOUNTID
  2512. r.ACCOUNTNAME = v.ACCOUNTNAME
  2513. r.GOODSID = v.GOODSID
  2514. r.GOODSCODE = v.GOODSCODE
  2515. r.GOODSNAME = v.GOODSNAME
  2516. r.BUYORSELL = 1
  2517. r.ORIHOLDQTY = float64(v.SELLPOSITIONQTY)
  2518. r.HOLDQTY = float64(v.SELLCURPOSITIONQTY)
  2519. r.TODAYOPENQTY = 0
  2520. r.TODAYCLOSEQTY = 0
  2521. r.DiffTradeQty = v.DiffSellQty
  2522. r.DiffMgQty = v.DiffMgSellQty
  2523. r.DiffNeedQty = v.DiffNeedMgSellQty
  2524. r.MIDDLEGOODSID = v.MIDDLEGOODSID
  2525. r.MIDDLEGOODSCODE = v.MIDDLEGOODSCODE
  2526. r.MIDDLEGOODSNAME = v.MIDDLEGOODSNAME
  2527. r.ENUMDICNAME = v.ENUMDICNAME
  2528. r.RECKONDATE = v.RECKONDATE
  2529. }
  2530. // GetDataEx 获取敞口报表期货明细
  2531. func (r *Ermcp3ExposureFutuDetail) GetDataEx() (interface{}, error) {
  2532. sData := make([]Ermcp3ExposureFutuDetail, 0)
  2533. if mtpcache.IsAreaUserId(r.USERID) {
  2534. // 查母账户头寸
  2535. m := HISOUTTRADEPOSITION{RELATEDUSERID: r.USERID, HISTRADEDATE: r.RECKONDATE,
  2536. BeginDate: r.BeginDate, EndDate: r.EndDate, MIDDLEGOODSID: r.MIDDLEGOODSID}
  2537. d, _ := m.GetDataEx()
  2538. for _, v := range d {
  2539. if v.YDBUYPOSITION > 0 || v.CURBUYPOSITION > 0 {
  2540. val := Ermcp3ExposureFutuDetail{}
  2541. val.fromOutPositionBuy(&v)
  2542. val.BeginDate = r.BeginDate
  2543. val.EndDate = r.EndDate
  2544. sData = append(sData, val)
  2545. }
  2546. if v.YDSELLPOSITION > 0 || v.CURSELLPOSITION > 0 {
  2547. val := Ermcp3ExposureFutuDetail{}
  2548. val.fromOutPositionSell(&v)
  2549. val.BeginDate = r.BeginDate
  2550. val.EndDate = r.EndDate
  2551. sData = append(sData, val)
  2552. }
  2553. }
  2554. } else {
  2555. // 查子账户头寸
  2556. m := ReckonDayPosition{RELATEDUSERID: r.USERID, RECKONDATE: r.RECKONDATE,
  2557. BeginDate: r.BeginDate, EndDate: r.EndDate, MIDDLEGOODSID: r.MIDDLEGOODSID}
  2558. d, _ := m.GetDataEx()
  2559. for _, v := range d {
  2560. if v.BUYPOSITIONQTY > 0 || v.BUYCURPOSITIONQTY > 0 {
  2561. val := Ermcp3ExposureFutuDetail{}
  2562. val.fromTradePositionBuy(&v)
  2563. val.BeginDate = r.BeginDate
  2564. val.EndDate = r.EndDate
  2565. sData = append(sData, val)
  2566. }
  2567. if v.SELLPOSITIONQTY > 0 || v.SELLCURPOSITIONQTY > 0 {
  2568. val := Ermcp3ExposureFutuDetail{}
  2569. val.fromTradePositionSell(&v)
  2570. val.BeginDate = r.BeginDate
  2571. val.EndDate = r.EndDate
  2572. sData = append(sData, val)
  2573. }
  2574. }
  2575. }
  2576. return sData, nil
  2577. }