ermcp3Report.go 139 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810
  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.ORIQTY > 0 && r.CURQTY > 0 {
  881. // 1.当期初量大于等于0时:期末量也大于等于0时:
  882. // 期末均价 CurAveragePrice=(今采购额+期初额)/(今采购量+期初量)
  883. // 期末额 CurAmoun =(今采购额 + 期初额)- 今销售量 * 期末均价
  884. // 现货损益 ActualPL = 今销售额 - 今销售量 * 期末均价
  885. // 浮动损益 = 期末市值 - 期末额
  886. r.CURAVERAGEPRICE = (r.TODAYBUYAMOUNT + r.ORIAMOUNT) / (r.TODAYBUYQTY + r.ORIQTY)
  887. r.CURAMOUNT = (r.TODAYBUYAMOUNT + r.ORIAMOUNT) - r.TODAYSELLQTY*r.CURAVERAGEPRICE
  888. r.ACTUALPL = r.TODAYSELLAMOUNT - r.TODAYSELLQTY*r.CURAVERAGEPRICE
  889. r.FLOATPL = r.CURMARKETVALUE - r.CURAMOUNT
  890. } else if r.ORIQTY >= 0 && r.CURQTY < 0 {
  891. // 2.当期初量大于等于0时:期末量也小于0时:
  892. //期末均价=今销售额/今销售量
  893. //期末额=(今采购量+期初量)*期末均价-今销售额
  894. //现货损益=(今采购量+期初量)*期末均价-(今采购额+期初额)
  895. //浮动损益 =期末市值 - 期末额
  896. if r.TODAYSELLQTY > 0 {
  897. r.CURAVERAGEPRICE = r.TODAYSELLAMOUNT / r.TODAYSELLQTY
  898. } else {
  899. r.CURAVERAGEPRICE = 0
  900. }
  901. r.CURAMOUNT = (r.TODAYBUYQTY+r.ORIQTY)*r.CURAVERAGEPRICE - r.TODAYSELLAMOUNT
  902. r.ACTUALPL = (r.TODAYBUYQTY+r.ORIQTY)*r.CURAVERAGEPRICE - (r.TODAYBUYAMOUNT + r.ORIAMOUNT)
  903. r.FLOATPL = r.CURMARKETVALUE - r.CURAMOUNT
  904. } else if r.ORIQTY < 0 && r.CURQTY >= 0 {
  905. // 3.当期初量小于0时:期末量也大于等于0时:
  906. //期末均价=今采购额/今采购量
  907. //期末额=今采购额-(今销售量-期初量)*期末均价
  908. //现货损益=(今销售额-期初额)-(今销售量-期初量)*期末均价
  909. //浮动损益 =期末市值 - 期末额
  910. if r.TODAYBUYQTY > 0 {
  911. r.CURAVERAGEPRICE = r.TODAYBUYAMOUNT / r.TODAYBUYQTY
  912. } else {
  913. r.CURAVERAGEPRICE = 0
  914. }
  915. r.CURAMOUNT = r.TODAYBUYAMOUNT - (r.TODAYSELLQTY-r.ORIQTY)*r.CURAVERAGEPRICE
  916. r.ACTUALPL = (r.TODAYSELLAMOUNT - r.ORIAMOUNT) - (r.TODAYSELLQTY-r.ORIQTY)*r.CURAVERAGEPRICE
  917. r.FLOATPL = r.CURMARKETVALUE - r.CURAMOUNT
  918. } else if r.ORIQTY < 0 && r.CURQTY < 0 {
  919. // 4.当期初量小于0时:期末量也小于0时:
  920. //期末均价=(今销售额-期初额)/(今销售量-期初量)
  921. //期末额=今采购量*期末均价-(今销售额-期初额)
  922. //现货损益=今采购量*期末均价-今采购额
  923. //浮动损益 =期末市值 - 期末额
  924. if r.TODAYSELLQTY-r.ORIQTY != 0 {
  925. r.CURAVERAGEPRICE = (r.TODAYSELLAMOUNT - r.ORIAMOUNT) / (r.TODAYSELLQTY - r.ORIQTY)
  926. } else {
  927. r.CURAVERAGEPRICE = 0
  928. }
  929. r.CURAMOUNT = r.TODAYBUYQTY*r.CURAVERAGEPRICE - (r.TODAYSELLAMOUNT - r.ORIAMOUNT)
  930. r.ACTUALPL = r.TODAYBUYQTY*r.CURAVERAGEPRICE - r.TODAYBUYAMOUNT
  931. r.FLOATPL = r.CURMARKETVALUE - r.CURAVERAGEPRICE
  932. }
  933. }
  934. func (r *Ermcp3AreaSpotPLReport) buildSql() string {
  935. // 日报表
  936. if r.QueryType == 1 && r.CYCLETYPE == 0 {
  937. return r.buildSqlDay()
  938. }
  939. // 日报表明细
  940. if r.QueryType == 2 && r.CYCLETYPE == 0 {
  941. return r.buildSqlDayDetail()
  942. }
  943. return r.buildSqlCycle()
  944. }
  945. // buildSqlDay 现货日报表查询语句
  946. func (r *Ermcp3AreaSpotPLReport) buildSqlDay() string {
  947. var sqlId utils.SQLVal = "select a.*," +
  948. " g.deliverygoodscode," +
  949. " g.deliverygoodsname," +
  950. " g.goodsunitid" +
  951. " from (SELECT t.RECKONDATE," +
  952. " t.AREAUSERID," +
  953. " t.CURRENCYID," +
  954. " t.DELIVERYGOODSID," +
  955. " sum(t.ORIBUYQTY) ORIBUYQTY," +
  956. " sum(t.ORIBUYAMOUNT) ORIBUYAMOUNT," +
  957. " sum(t.ORISELLQTY) ORISELLQTY," +
  958. " sum(t.ORISELLAMOUNT) ORISELLAMOUNT," +
  959. " sum(t.ORIQTY) ORIQTY," +
  960. " sum(t.ORIAMOUNT) ORIAMOUNT," +
  961. " sum(t.TODAYBUYQTY) TODAYBUYQTY," +
  962. " sum(t.TODAYBUYAMOUNT) TODAYBUYAMOUNT," +
  963. " sum(t.TODAYSELLQTY) TODAYSELLQTY," +
  964. " sum(t.TODAYSELLAMOUNT) TODAYSELLAMOUNT," +
  965. " sum(t.CURBUYQTY) CURBUYQTY," +
  966. " sum(t.CURBUYAMOUNT) CURBUYAMOUNT," +
  967. " sum(t.CURSELLQTY) CURSELLQTY," +
  968. " sum(t.CURSELLAMOUNT) CURSELLAMOUNT," +
  969. " sum(t.CURQTY) CURQTY," +
  970. " sum(t.CURAMOUNT) CURAMOUNT," +
  971. " max(t.CURSPOTPRICE) CURSPOTPRICE," +
  972. " sum(t.CURMARKETVALUE) CURMARKETVALUE," +
  973. " sum(t.ACTUALPL) ACTUALPL," +
  974. " sum(t.FLOATPL) FLOATPL," +
  975. " sum(t.TODAYINQTY) TODAYINQTY," +
  976. " sum(t.TODAYOUTQTY) TODAYOUTQTY" +
  977. " FROM RECKON_ERMCP_AREASPOTPL t" +
  978. " WHERE 1=1 %v" +
  979. " GROUP BY t.RECKONDATE," +
  980. " t.AREAUSERID," +
  981. " t.CURRENCYID," +
  982. " t.DELIVERYGOODSID) a" +
  983. " LEFT JOIN DELIVERYGOODS g" +
  984. " on a.deliverygoodsid = g.deliverygoodsid"
  985. var sqlParam utils.SQLVal
  986. sqlParam.And("t.AREAUSERID", r.AREAUSERID)
  987. if r.QueryType == 1 {
  988. sqlParam.And("t.RECKONDATE", r.RECKONDATE)
  989. } else if r.QueryType == 2 {
  990. sqlParam.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  991. }
  992. sqlId.FormatParam(sqlParam.String())
  993. return sqlId.String()
  994. }
  995. func (r *Ermcp3AreaSpotPLReport) buildSqlDayDetail() string {
  996. var sqlId utils.SQLVal = "select a.*," +
  997. " g.deliverygoodscode," +
  998. " g.deliverygoodsname," +
  999. " g.goodsunitid," +
  1000. " w.wrstandardcode," +
  1001. " w.wrstandardname," +
  1002. " w.unitid," +
  1003. " dg.dgfactoryitemvalue brandName" +
  1004. " from (SELECT t.RECKONDATE," +
  1005. " t.AREAUSERID," +
  1006. " t.CURRENCYID," +
  1007. " t.DELIVERYGOODSID," +
  1008. " t.WRSTANDARDID," +
  1009. " t.SPOTGOODSBRANDID," +
  1010. " sum(t.ORIBUYQTY) ORIBUYQTY," +
  1011. " sum(t.ORIBUYAMOUNT) ORIBUYAMOUNT," +
  1012. " sum(t.ORISELLQTY) ORISELLQTY," +
  1013. " sum(t.ORISELLAMOUNT) ORISELLAMOUNT," +
  1014. " sum(t.ORIQTY) ORIQTY," +
  1015. " sum(t.ORIAMOUNT) ORIAMOUNT," +
  1016. " sum(t.TODAYBUYQTY) TODAYBUYQTY," +
  1017. " sum(t.TODAYBUYAMOUNT) TODAYBUYAMOUNT," +
  1018. " sum(t.TODAYSELLQTY) TODAYSELLQTY," +
  1019. " sum(t.TODAYSELLAMOUNT) TODAYSELLAMOUNT," +
  1020. " sum(t.CURBUYQTY) CURBUYQTY," +
  1021. " sum(t.CURBUYAMOUNT) CURBUYAMOUNT," +
  1022. " sum(t.CURSELLQTY) CURSELLQTY," +
  1023. " sum(t.CURSELLAMOUNT) CURSELLAMOUNT," +
  1024. " sum(t.CURQTY) CURQTY," +
  1025. " sum(t.CURAMOUNT) CURAMOUNT," +
  1026. " max(t.CURSPOTPRICE) CURSPOTPRICE," +
  1027. " sum(t.CURMARKETVALUE) CURMARKETVALUE," +
  1028. " sum(t.ACTUALPL) ACTUALPL," +
  1029. " sum(t.FLOATPL) FLOATPL," +
  1030. " sum(t.TODAYINQTY) TODAYINQTY," +
  1031. " sum(t.TODAYOUTQTY) TODAYOUTQTY" +
  1032. " FROM RECKON_ERMCP_AREASPOTPL t" +
  1033. " WHERE 1 = 1 %v" +
  1034. " GROUP BY t.RECKONDATE," +
  1035. " t.AREAUSERID," +
  1036. " t.CURRENCYID," +
  1037. " t.DELIVERYGOODSID," +
  1038. " t.WRSTANDARDID," +
  1039. " t.SPOTGOODSBRANDID) a" +
  1040. " LEFT JOIN DELIVERYGOODS g" +
  1041. " on a.deliverygoodsid = g.deliverygoodsid" +
  1042. " LEFT JOIN WRSTANDARD w" +
  1043. " on a.wrstandardid = w.wrstandardid" +
  1044. " LEFT JOIN DGFACTORYITEM dg" +
  1045. " on a.spotgoodsbrandid = dg.dgfactoryitemid"
  1046. var sqlParam utils.SQLVal
  1047. sqlParam.And("t.AREAUSERID", r.AREAUSERID)
  1048. sqlParam.AndEx("t.RECKONDATE", r.RECKONDATE, len(r.RECKONDATE) > 0)
  1049. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  1050. sqlParam.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1051. }
  1052. sqlParam.And("t.CURRENCYID", r.CURRENCYID)
  1053. sqlParam.And("t.DELIVERYGOODSID", r.DELIVERYGOODSID)
  1054. sqlId.FormatParam(sqlParam.String())
  1055. return sqlId.String()
  1056. }
  1057. // buildSqlCycle 周期报表查询语句
  1058. func (r *Ermcp3AreaSpotPLReport) buildSqlCycle() string {
  1059. var sqlId utils.SQLVal = "select a.*," +
  1060. " g.deliverygoodscode," +
  1061. " g.deliverygoodsname," +
  1062. " g.goodsunitid" +
  1063. " from (SELECT t.cycletype," +
  1064. " t.cycletime," +
  1065. " t.AREAUSERID," +
  1066. " t.CURRENCYID," +
  1067. " t.DELIVERYGOODSID," +
  1068. " sum(t.ORIBUYQTY) ORIBUYQTY," +
  1069. " sum(t.ORIBUYAMOUNT) ORIBUYAMOUNT," +
  1070. " sum(t.ORISELLQTY) ORISELLQTY," +
  1071. " sum(t.ORISELLAMOUNT) ORISELLAMOUNT," +
  1072. " sum(t.ORIQTY) ORIQTY," +
  1073. " sum(t.ORIAMOUNT) ORIAMOUNT," +
  1074. " sum(t.TODAYBUYQTY) TODAYBUYQTY," +
  1075. " sum(t.TODAYBUYAMOUNT) TODAYBUYAMOUNT," +
  1076. " sum(t.TODAYSELLQTY) TODAYSELLQTY," +
  1077. " sum(t.TODAYSELLAMOUNT) TODAYSELLAMOUNT," +
  1078. " sum(t.CURBUYQTY) CURBUYQTY," +
  1079. " sum(t.CURBUYAMOUNT) CURBUYAMOUNT," +
  1080. " sum(t.CURSELLQTY) CURSELLQTY," +
  1081. " sum(t.CURSELLAMOUNT) CURSELLAMOUNT," +
  1082. " sum(t.CURQTY) CURQTY," +
  1083. " sum(t.CURAMOUNT) CURAMOUNT," +
  1084. " max(t.CURSPOTPRICE) CURSPOTPRICE," +
  1085. " sum(t.CURMARKETVALUE) CURMARKETVALUE," +
  1086. " sum(t.ACTUALPL) ACTUALPL," +
  1087. " sum(t.FLOATPL) FLOATPL," +
  1088. " sum(t.TODAYINQTY) TODAYINQTY," +
  1089. " sum(t.TODAYOUTQTY) TODAYOUTQTY" +
  1090. " FROM REPORT_ERMCP_AREASPOTPL t" +
  1091. " WHERE 1=1 %v" +
  1092. " GROUP BY t.AREAUSERID," +
  1093. " t.cycletype," +
  1094. " t.cycletime," +
  1095. " t.CURRENCYID," +
  1096. " t.DELIVERYGOODSID) a" +
  1097. " LEFT JOIN DELIVERYGOODS g" +
  1098. " on a.deliverygoodsid = g.deliverygoodsid"
  1099. var sqlParam utils.SQLVal
  1100. sqlParam.And("t.AREAUSERID", r.AREAUSERID)
  1101. sqlParam.And("t.Cycletype", r.CYCLETYPE)
  1102. sqlParam.And("t.CYCLETIME", r.CYCLETIME)
  1103. sqlId.FormatParam(sqlParam.String())
  1104. return sqlId.String()
  1105. }
  1106. // GetDataEx 获取现货报表(日/月/周/季/年)
  1107. func (r *Ermcp3AreaSpotPLReport) GetDataEx() (interface{}, error) {
  1108. sData := make([]Ermcp3AreaSpotPLReport, 0)
  1109. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1110. for i := range sData {
  1111. sData[i].calc()
  1112. sData[i].BeginDate = r.BeginDate
  1113. sData[i].EndDate = r.EndDate
  1114. }
  1115. return sData, err
  1116. }
  1117. // Ermcp3FinanceReport 财务报表
  1118. type Ermcp3FinanceReport struct {
  1119. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照时期(yyyyMMdd)
  1120. CYCLETIME string `json:"cycletime" xorm:"CYCLETIME"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  1121. CYCLETYPE int32 `json:"cycletype" xorm:"CYCLETYPE"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  1122. SELLPREINVOICEDAMOUNT float64 `json:"sellpreinvoicedamount" xorm:"SELLPREINVOICEDAMOUNT"` // 销售预开票额(预开票额)
  1123. SELLUNINVOICEDAMOUNT float64 `json:"selluninvoicedamount" xorm:"SELLUNINVOICEDAMOUNT"` // 销售应开票额(应开票额)
  1124. TODAYRECEIVESUM float64 `json:"todayreceivesum" xorm:"TODAYRECEIVESUM"` // 今收款合计
  1125. TODAYPAYSUM float64 `json:"todaypaysum" xorm:"TODAYPAYSUM"` // 今付款合计
  1126. UPDATETIME string `json:"updatetime" xorm:"UPDATETIME"` // 更新时间
  1127. AREAUSERID int64 `json:"areauserid" xorm:"AREAUSERID"` // 所属机构\交易用户ID
  1128. CURRENCYID int32 `json:"currencyid" xorm:"CURRENCYID"` // 结算币种ID
  1129. BIZTYPE int32 `json:"biztype" xorm:"BIZTYPE"` // 业务类型 - 1:套保 2:套利
  1130. BUYTODAYSETTLEAMOUNT float64 `json:"buytodaysettleamount" xorm:"BUYTODAYSETTLEAMOUNT"` // 采购今付款额(今付货款额)
  1131. SELLTODAYSETTLEAMOUNT float64 `json:"selltodaysettleamount" xorm:"SELLTODAYSETTLEAMOUNT"` // 销售今收款额(今收货款额)
  1132. BUYTODAYREFUNDAMOUNT float64 `json:"buytodayrefundamount" xorm:"BUYTODAYREFUNDAMOUNT"` // 采购今收退款额(今收退款额)
  1133. SELLTODAYREFUNDAMOUNT float64 `json:"selltodayrefundamount" xorm:"SELLTODAYREFUNDAMOUNT"` // 销售今付退款额(今付退款额)
  1134. SELLTODAYINVOICEAMOUNT float64 `json:"selltodayinvoiceamount" xorm:"SELLTODAYINVOICEAMOUNT"` // 销售今开票额(今开票额)
  1135. BUYTODAYINVOICEAMOUNT float64 `json:"buytodayinvoiceamount" xorm:"BUYTODAYINVOICEAMOUNT"` // 采购今收票额(今收票额)
  1136. BUYPREPAIDAMOUNT float64 `json:"buyprepaidamount" xorm:"BUYPREPAIDAMOUNT"` // 采购预付款额(预付货款额)
  1137. BUYUNPAIDAMOUNT float64 `json:"buyunpaidamount" xorm:"BUYUNPAIDAMOUNT"` // 采购应付款额(应付货款额)
  1138. BUYPREINVOICEDAMOUNT float64 `json:"buypreinvoicedamount" xorm:"BUYPREINVOICEDAMOUNT"` // 采购预收票额(预收票额)
  1139. BUYUNINVOICEDAMOUNT float64 `json:"buyuninvoicedamount" xorm:"BUYUNINVOICEDAMOUNT"` // 采购应收票额(应收票额)
  1140. SELLPREPAIDAMOUNT float64 `json:"sellprepaidamount" xorm:"SELLPREPAIDAMOUNT"` // 销售预收款额(预收货款额)
  1141. SELLUNPAIDAMOUNT float64 `json:"sellunpaidamount" xorm:"SELLUNPAIDAMOUNT"` // 销售应收款额(应收货款额)
  1142. WRFACTORTYPEID int64 `json:"wrfactortypeid" xorm:"WRFACTORTYPEID"` // 仓单要素类型ID(212+Unix秒时间戳(10位)+xxxxxx)
  1143. DELIVERYGOODSID int64 `json:"deliverygoodsid" xorm:"DELIVERYGOODSID"` // 现货品种ID
  1144. WRSTANDARDID int64 `json:"wrstandardid" xorm:"WRSTANDARDID"` // 现货商品ID
  1145. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"SPOTGOODSBRANDID"` // 现货品牌ID
  1146. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  1147. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  1148. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1149. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1150. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1151. ACCOUNTNAME string `json:"accountname"` // 机构名称
  1152. CURRENCYNAME string `json:"currencyname"` // 币种名称
  1153. QueryType int32 `json:"-"` // 查询类型 1-报表 2-报表明细
  1154. BeginDate string `json:"begindate"` // 开始交易日
  1155. EndDate string `json:"enddate"` // 结束交易日
  1156. SumFields string `json:"-"` // 维度字段
  1157. }
  1158. func (r *Ermcp3FinanceReport) calc() {
  1159. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  1160. r.ACCOUNTNAME = mtpcache.GetUserNameByUserId(r.AREAUSERID)
  1161. f := func(v *float64) {
  1162. if *v < 0 {
  1163. *v = 0
  1164. }
  1165. }
  1166. f(&r.BUYPREPAIDAMOUNT)
  1167. f(&r.SELLPREINVOICEDAMOUNT)
  1168. f(&r.BUYUNINVOICEDAMOUNT)
  1169. f(&r.SELLPREPAIDAMOUNT)
  1170. f(&r.BUYPREINVOICEDAMOUNT)
  1171. }
  1172. // getSumField 获取汇总维度字段
  1173. func (r *Ermcp3FinanceReport) getSumField() (fields []string) {
  1174. fields = append(fields, "areauserid")
  1175. fields = append(fields, "currencyid")
  1176. if r.CYCLETYPE == 0 {
  1177. // 日报表
  1178. fields = append(fields, "reckondate")
  1179. } else {
  1180. // 周期报表
  1181. fields = append(fields, "cycletype")
  1182. fields = append(fields, "cycletime")
  1183. }
  1184. baseField := map[string]string{
  1185. "1": "deliverygoodsid",
  1186. "2": "wrstandardid",
  1187. "3": "spotgoodsbrandid",
  1188. }
  1189. param := []string{}
  1190. if len(r.SumFields) > 0 {
  1191. param = strings.Split(r.SumFields, ",")
  1192. }
  1193. for k, v := range baseField {
  1194. for _, p := range param {
  1195. if k == p {
  1196. fields = append(fields, v)
  1197. }
  1198. }
  1199. }
  1200. return
  1201. }
  1202. func (r *Ermcp3FinanceReport) getExFieldInfo() (strFiled string, strleftJoin string) {
  1203. if len(r.SumFields) > 0 {
  1204. param := strings.Split(r.SumFields, ",")
  1205. fHas := func(v string) bool {
  1206. for i := range param {
  1207. if param[i] == v {
  1208. return true
  1209. }
  1210. }
  1211. return false
  1212. }
  1213. if fHas("1") {
  1214. if len(strFiled) > 0 {
  1215. strFiled += ","
  1216. }
  1217. strFiled += "g.deliverygoodscode, g.deliverygoodsname, g.goodsunitid"
  1218. strleftJoin += " LEFT JOIN deliverygoods g on a.deliverygoodsid = g.deliverygoodsid"
  1219. }
  1220. if fHas("2") {
  1221. if len(strFiled) > 0 {
  1222. strFiled += ","
  1223. }
  1224. strFiled += "w.wrstandardname, w.wrstandardcode, w.unitid"
  1225. strleftJoin += " LEFT JOIN WRSTANDARD w on a.wrstandardid = w.wrstandardid"
  1226. }
  1227. if fHas("3") {
  1228. if len(strFiled) > 0 {
  1229. strFiled += ","
  1230. }
  1231. strFiled += "gb.dgfactoryitemvalue brandname"
  1232. strleftJoin += " LEFT JOIN dgfactoryitem gb on a.spotgoodsbrandid = gb.dgfactoryitemid"
  1233. }
  1234. }
  1235. return
  1236. }
  1237. func (r *Ermcp3FinanceReport) buildSql() string {
  1238. fields := r.getSumField()
  1239. if r.CYCLETYPE == 0 {
  1240. // 查日照报
  1241. return r.buildSqlReckon(fields)
  1242. } else {
  1243. // 查报表表
  1244. return r.buildSqlReport(fields)
  1245. }
  1246. }
  1247. func (r *Ermcp3FinanceReport) buildSqlReckon(fields []string) string {
  1248. var sqlId, sqlParam utils.SQLVal
  1249. var strFileds string
  1250. for _, v := range fields {
  1251. if len(strFileds) > 0 {
  1252. strFileds += ","
  1253. }
  1254. strFileds += "t." + v
  1255. }
  1256. sqlParam.And("t.AREAUSERID", r.AREAUSERID)
  1257. sqlParam.AndEx("t.RECKONDATE", r.RECKONDATE, len(r.RECKONDATE) > 0)
  1258. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  1259. sqlParam.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1260. }
  1261. sqlParam.AndEx("t.CURRENCYID", r.CURRENCYID, r.CURRENCYID > 0)
  1262. exFileds, strleftJoin := r.getExFieldInfo()
  1263. if len(exFileds) > 0 {
  1264. // 有左联查额外信息(RECKON_ERMCP_AFINANCESUB明细表, 注意是不同的表)
  1265. sqlId = "select a.*, %v" +
  1266. " from (SELECT %v," +
  1267. " sum(t.BUYTODAYSETTLEAMOUNT) BUYTODAYSETTLEAMOUNT," +
  1268. " sum(t.BUYTODAYREFUNDAMOUNT) BUYTODAYREFUNDAMOUNT," +
  1269. " sum(t.BUYPREPAIDAMOUNT) BUYPREPAIDAMOUNT," +
  1270. " sum(t.BUYUNPAIDAMOUNT) BUYUNPAIDAMOUNT," +
  1271. " sum(t.BUYTODAYINVOICEAMOUNT) BUYTODAYINVOICEAMOUNT," +
  1272. " sum(t.BUYPREINVOICEDAMOUNT) BUYPREINVOICEDAMOUNT," +
  1273. " sum(t.BUYUNINVOICEDAMOUNT) BUYUNINVOICEDAMOUNT," +
  1274. " sum(t.SELLTODAYSETTLEAMOUNT) SELLTODAYSETTLEAMOUNT," +
  1275. " sum(t.SELLTODAYREFUNDAMOUNT) SELLTODAYREFUNDAMOUNT," +
  1276. " sum(t.SELLPREPAIDAMOUNT) SELLPREPAIDAMOUNT," +
  1277. " sum(t.SELLUNPAIDAMOUNT) SELLUNPAIDAMOUNT," +
  1278. " sum(t.SELLTODAYINVOICEAMOUNT) SELLTODAYINVOICEAMOUNT," +
  1279. " sum(t.SELLPREINVOICEDAMOUNT) SELLPREINVOICEDAMOUNT," +
  1280. " sum(t.SELLUNINVOICEDAMOUNT) SELLUNINVOICEDAMOUNT," +
  1281. " sum(t.TODAYRECEIVESUM) TODAYRECEIVESUM," +
  1282. " sum(t.TODAYPAYSUM) TODAYPAYSUM" +
  1283. " FROM RECKON_ERMCP_AFINANCESUB t" +
  1284. " WHERE 1 = 1 %v" +
  1285. " GROUP BY %v) a %v"
  1286. sqlId.FormatParam(exFileds, strFileds, sqlParam.String(), strFileds, strleftJoin)
  1287. } else {
  1288. sqlId = "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 RECKON_ERMCP_AREAFINANCE t" +
  1306. " WHERE 1 = 1 %v" +
  1307. " GROUP BY %v"
  1308. sqlId.FormatParam(strFileds, sqlParam.String(), strFileds)
  1309. }
  1310. return sqlId.String()
  1311. }
  1312. func (r *Ermcp3FinanceReport) buildSqlReport(fields []string) string {
  1313. var sqlId, sqlParam utils.SQLVal
  1314. var strFileds string
  1315. for _, v := range fields {
  1316. if len(strFileds) > 0 {
  1317. strFileds += ","
  1318. }
  1319. strFileds += "t." + v
  1320. }
  1321. sqlParam.And("t.AREAUSERID", r.AREAUSERID)
  1322. sqlParam.And("t.CYCLETYPE", r.CYCLETYPE)
  1323. sqlParam.And("t.CYCLETIME", r.CYCLETIME)
  1324. sqlParam.AndEx("t.CURRENCYID", r.CURRENCYID, r.CURRENCYID > 0)
  1325. exFileds, strleftJoin := r.getExFieldInfo()
  1326. if len(exFileds) > 0 {
  1327. // 有左联查额外信息(REPORT_ERMCP_AFINANCESUB注意表名不同)
  1328. sqlId = "select a.*, %v" +
  1329. " from (SELECT %v," +
  1330. " sum(t.BUYTODAYSETTLEAMOUNT) BUYTODAYSETTLEAMOUNT," +
  1331. " sum(t.BUYTODAYREFUNDAMOUNT) BUYTODAYREFUNDAMOUNT," +
  1332. " sum(t.BUYPREPAIDAMOUNT) BUYPREPAIDAMOUNT," +
  1333. " sum(t.BUYUNPAIDAMOUNT) BUYUNPAIDAMOUNT," +
  1334. " sum(t.BUYTODAYINVOICEAMOUNT) BUYTODAYINVOICEAMOUNT," +
  1335. " sum(t.BUYPREINVOICEDAMOUNT) BUYPREINVOICEDAMOUNT," +
  1336. " sum(t.BUYUNINVOICEDAMOUNT) BUYUNINVOICEDAMOUNT," +
  1337. " sum(t.SELLTODAYSETTLEAMOUNT) SELLTODAYSETTLEAMOUNT," +
  1338. " sum(t.SELLTODAYREFUNDAMOUNT) SELLTODAYREFUNDAMOUNT," +
  1339. " sum(t.SELLPREPAIDAMOUNT) SELLPREPAIDAMOUNT," +
  1340. " sum(t.SELLUNPAIDAMOUNT) SELLUNPAIDAMOUNT," +
  1341. " sum(t.SELLTODAYINVOICEAMOUNT) SELLTODAYINVOICEAMOUNT," +
  1342. " sum(t.SELLPREINVOICEDAMOUNT) SELLPREINVOICEDAMOUNT," +
  1343. " sum(t.SELLUNINVOICEDAMOUNT) SELLUNINVOICEDAMOUNT," +
  1344. " sum(t.TODAYRECEIVESUM) TODAYRECEIVESUM," +
  1345. " sum(t.TODAYPAYSUM) TODAYPAYSUM" +
  1346. " FROM REPORT_ERMCP_AFINANCESUB t" +
  1347. " WHERE 1 = 1 %v" +
  1348. " GROUP BY %v) a %v"
  1349. sqlId.FormatParam(exFileds, strFileds, sqlParam.String(), strFileds, strleftJoin)
  1350. } else {
  1351. sqlId = "SELECT %v," +
  1352. " sum(t.BUYTODAYSETTLEAMOUNT) BUYTODAYSETTLEAMOUNT," +
  1353. " sum(t.BUYTODAYREFUNDAMOUNT) BUYTODAYREFUNDAMOUNT," +
  1354. " sum(t.BUYPREPAIDAMOUNT) BUYPREPAIDAMOUNT," +
  1355. " sum(t.BUYUNPAIDAMOUNT) BUYUNPAIDAMOUNT," +
  1356. " sum(t.BUYTODAYINVOICEAMOUNT) BUYTODAYINVOICEAMOUNT," +
  1357. " sum(t.BUYPREINVOICEDAMOUNT) BUYPREINVOICEDAMOUNT," +
  1358. " sum(t.BUYUNINVOICEDAMOUNT) BUYUNINVOICEDAMOUNT," +
  1359. " sum(t.SELLTODAYSETTLEAMOUNT) SELLTODAYSETTLEAMOUNT," +
  1360. " sum(t.SELLTODAYREFUNDAMOUNT) SELLTODAYREFUNDAMOUNT," +
  1361. " sum(t.SELLPREPAIDAMOUNT) SELLPREPAIDAMOUNT," +
  1362. " sum(t.SELLUNPAIDAMOUNT) SELLUNPAIDAMOUNT," +
  1363. " sum(t.SELLTODAYINVOICEAMOUNT) SELLTODAYINVOICEAMOUNT," +
  1364. " sum(t.SELLPREINVOICEDAMOUNT) SELLPREINVOICEDAMOUNT," +
  1365. " sum(t.SELLUNINVOICEDAMOUNT) SELLUNINVOICEDAMOUNT," +
  1366. " sum(t.TODAYRECEIVESUM) TODAYRECEIVESUM," +
  1367. " sum(t.TODAYPAYSUM) TODAYPAYSUM" +
  1368. " FROM REPORT_ERMCP_AREAFINANCE t" +
  1369. " WHERE 1 = 1 %v" +
  1370. " GROUP BY %v"
  1371. sqlId.FormatParam(strFileds, sqlParam.String(), strFileds)
  1372. }
  1373. return sqlId.String()
  1374. }
  1375. // GetDataEx 获取财务报表
  1376. func (r *Ermcp3FinanceReport) GetDataEx() (interface{}, error) {
  1377. sData := make([]Ermcp3FinanceReport, 0)
  1378. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1379. for i := range sData {
  1380. sData[i].calc()
  1381. sData[i].BeginDate = r.BeginDate
  1382. sData[i].EndDate = r.EndDate
  1383. }
  1384. return sData, err
  1385. }
  1386. // Ermcp3AreaStockReportEx 库存报表
  1387. type Ermcp3AreaStockReportEx struct {
  1388. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照时期(yyyyMMdd)
  1389. CYCLETIME string `json:"cycletime" xorm:"CYCLETIME"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  1390. CYCLETYPE int32 `json:"cycletype" xorm:"CYCLETYPE"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  1391. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构ID
  1392. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类ID
  1393. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID
  1394. WAREHOUSEINFOID string `json:"warehouseinfoid" xorm:"'WAREHOUSEINFOID'"` // 仓库ID
  1395. ORISTOCK float64 `json:"oristock" xorm:"'ORISTOCK'"` // 期初库存量
  1396. CURSTOCK float64 `json:"curstock" xorm:"'CURSTOCK'"` // 期末库存量
  1397. TODAYBUYINQTY float64 `json:"todaybuyinqty" xorm:"'TODAYBUYINQTY'"` // 今采购入库量
  1398. TODAYPRODUCEINQTY float64 `json:"todayproduceinqty" xorm:"'TODAYPRODUCEINQTY'"` // 今内部入库量
  1399. TODAYSELLOUTQTY float64 `json:"todayselloutqty" xorm:"'TODAYSELLOUTQTY'"` // 今销售出库量
  1400. TODAYPRODUCEOUTQTY float64 `json:"todayproduceoutqty" xorm:"'TODAYPRODUCEOUTQTY'"` // 今内部出库量
  1401. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  1402. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1403. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1404. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 品类单位id
  1405. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1406. WAREHOUSENAME string `json:"warehousename" xorm:"'WAREHOUSENAME'"` // 仓库名称
  1407. WAREHOUSECODE string `json:"warehousecode" xorm:"'WAREHOUSECODE'"` // 仓库代码
  1408. WAREHOUSETYPE int32 `json:"warehousetype" xorm:"'WAREHOUSETYPE'"` // 仓库类型 - 1 厂库 2 自有库 3 合作库
  1409. USERNAME string `json:"username"` // 机构名称
  1410. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  1411. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种id
  1412. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  1413. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  1414. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODSUNITID'"` // 现货单位id
  1415. UNBUYINQTY float64 `json:"unbuyinqty"` // 采购未入库量(数据库未找到相关字段?)
  1416. UNSELLOUTQTY float64 `json:"unselloutqty"` // 销售未出库量(数据库未找到相关字段?)
  1417. DiffQty float64 `json:"diffqty"` // 库存变化量 = 期末 - 期初
  1418. BeginDate string `json:"begindate"` // 开始交易日
  1419. EndDate string `json:"enddate"` // 结束交易日
  1420. SumFields string `json:"-"` // 汇总维度(逗号隔开) 1-品种 2-品类 3-品牌 4-仓库
  1421. }
  1422. func (r *Ermcp3AreaStockReportEx) calc() {
  1423. r.USERNAME = mtpcache.GetUserNameByUserId(r.USERID)
  1424. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  1425. r.DiffQty = r.CURSTOCK - r.ORISTOCK
  1426. }
  1427. // getSumField 获取汇总维度字段
  1428. func (r *Ermcp3AreaStockReportEx) getSumField() (fields []string, bSum bool) {
  1429. fields = append(fields, "userid")
  1430. if r.CYCLETYPE == 0 {
  1431. // 日报表
  1432. fields = append(fields, "reckondate")
  1433. } else {
  1434. // 周期报表
  1435. fields = append(fields, "cycletype")
  1436. fields = append(fields, "cycletime")
  1437. }
  1438. baseField := map[string]string{
  1439. "1": "deliverygoodsid",
  1440. "2": "wrstandardid",
  1441. "3": "spotgoodsbrandid",
  1442. "4": "warehouseinfoid",
  1443. }
  1444. bSum = false
  1445. param := []string{}
  1446. if len(r.SumFields) > 0 {
  1447. param = strings.Split(r.SumFields, ",")
  1448. }
  1449. if len(param) == 0 {
  1450. // 全字段, 不需要汇总
  1451. for _, v := range baseField {
  1452. fields = append(fields, v)
  1453. }
  1454. bSum = false
  1455. } else {
  1456. bIncludeAll := true
  1457. // 判断是否包含所有维度
  1458. for k := range baseField {
  1459. bExist := false
  1460. for _, p := range param {
  1461. if k == p {
  1462. bExist = true
  1463. break
  1464. }
  1465. }
  1466. if !bExist {
  1467. bIncludeAll = false
  1468. break
  1469. }
  1470. }
  1471. if bIncludeAll {
  1472. // 包含了所有维度, 不需要汇总
  1473. for _, v := range baseField {
  1474. fields = append(fields, v)
  1475. }
  1476. bSum = false
  1477. } else {
  1478. tmp := map[string]string{}
  1479. for _, p := range param {
  1480. if v, ok := baseField[p]; ok {
  1481. // 利用map特性去重, 先保存在一个临时map中
  1482. tmp[p] = v
  1483. bSum = true
  1484. }
  1485. }
  1486. if bSum {
  1487. for _, v := range tmp {
  1488. fields = append(fields, v)
  1489. }
  1490. }
  1491. }
  1492. }
  1493. return
  1494. }
  1495. func (r *Ermcp3AreaStockReportEx) getExFieldInfo() (strFiled string, strleftJoin string) {
  1496. if len(r.SumFields) > 0 {
  1497. param := strings.Split(r.SumFields, ",")
  1498. fHas := func(v string) bool {
  1499. for i := range param {
  1500. if param[i] == v {
  1501. return true
  1502. }
  1503. }
  1504. return false
  1505. }
  1506. if fHas("1") {
  1507. if len(strFiled) > 0 {
  1508. strFiled += ","
  1509. }
  1510. strFiled += "g.deliverygoodscode, g.deliverygoodsname, g.goodsunitid"
  1511. strleftJoin += " LEFT JOIN deliverygoods g on a.deliverygoodsid = g.deliverygoodsid"
  1512. }
  1513. if fHas("2") {
  1514. if len(strFiled) > 0 {
  1515. strFiled += ","
  1516. }
  1517. strFiled += "w.wrstandardname, w.wrstandardcode, w.unitid"
  1518. strleftJoin += " LEFT JOIN WRSTANDARD w on a.wrstandardid = w.wrstandardid"
  1519. }
  1520. if fHas("3") {
  1521. if len(strFiled) > 0 {
  1522. strFiled += ","
  1523. }
  1524. strFiled += "gb.dgfactoryitemvalue brandname"
  1525. strleftJoin += " LEFT JOIN dgfactoryitem gb on a.spotgoodsbrandid = gb.dgfactoryitemid"
  1526. }
  1527. if fHas("4") {
  1528. if len(strFiled) > 0 {
  1529. strFiled += ","
  1530. }
  1531. strFiled += "h.warehousecode, h.warehousename, h.warehousetype"
  1532. strleftJoin += " LEFT JOIN WAREHOUSEINFO h on a.warehouseinfoid=h.autoid"
  1533. }
  1534. }
  1535. return
  1536. }
  1537. func (r *Ermcp3AreaStockReportEx) buildSql() (sqlId string) {
  1538. fields, bSum := r.getSumField()
  1539. if r.CYCLETYPE == 0 {
  1540. // 日报表
  1541. if bSum {
  1542. sqlId = r.buildSqlReckonSum(fields)
  1543. } else {
  1544. sqlId = r.buildSqlReckon()
  1545. }
  1546. } else {
  1547. // 周期报表
  1548. if bSum {
  1549. sqlId = r.buildSqlReportSum(fields)
  1550. } else {
  1551. sqlId = r.buildSqlReport()
  1552. }
  1553. }
  1554. return
  1555. }
  1556. // GetDataEx 获取库存报表
  1557. func (r *Ermcp3AreaStockReportEx) GetDataEx() (interface{}, error) {
  1558. sData := make([]Ermcp3AreaStockReportEx, 0)
  1559. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1560. for i := range sData {
  1561. sData[i].calc()
  1562. sData[i].BeginDate = r.BeginDate
  1563. sData[i].EndDate = r.EndDate
  1564. }
  1565. return sData, err
  1566. }
  1567. // buildSqlReckon 日照表
  1568. func (r *Ermcp3AreaStockReportEx) buildSqlReckon() string {
  1569. var sqlId utils.SQLVal = "SELECT t.TODAYBUYINQTY," +
  1570. " t.TODAYPRODUCEINQTY," +
  1571. " t.reckondate," +
  1572. " t.TODAYSELLOUTQTY," +
  1573. " t.TODAYPRODUCEOUTQTY," +
  1574. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1575. " t.USERID," +
  1576. " t.WRSTANDARDID," +
  1577. " t.SPOTGOODSBRANDID," +
  1578. " t.WAREHOUSEINFOID," +
  1579. " t.deliverygoodsid," +
  1580. " t.ORISTOCK," +
  1581. " t.CURSTOCK," +
  1582. " w.wrstandardname," +
  1583. " w.wrstandardcode," +
  1584. " w.unitid," +
  1585. " gb.dgfactoryitemvalue brandname," +
  1586. " h.warehousename," +
  1587. " h.warehousecode," +
  1588. " h.warehousetype," +
  1589. " g.deliverygoodscode," +
  1590. " g.deliverygoodsname," +
  1591. " g.goodsunitid" +
  1592. " FROM RECKON_ERMCP_AREASTOCK t" +
  1593. " LEFT JOIN WRSTANDARD w" +
  1594. " on t.wrstandardid = w.wrstandardid" +
  1595. " LEFT JOIN dgfactoryitem gb" +
  1596. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1597. " LEFT JOIN deliverygoods g" +
  1598. " on t.deliverygoodsid = g.deliverygoodsid" +
  1599. " LEFT JOIN WAREHOUSEINFO h" +
  1600. " on t.warehouseinfoid = h.autoid" +
  1601. " WHERE 1 = 1"
  1602. sqlId.And("t.USERID", r.USERID)
  1603. sqlId.AndEx("t.RECKONDATE", r.RECKONDATE, len(r.RECKONDATE) > 0)
  1604. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  1605. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1606. }
  1607. // 现货商品id
  1608. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  1609. // 品类id
  1610. sqlId.AndEx("t.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  1611. // 品牌id
  1612. sqlId.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  1613. // 仓库id
  1614. sqlId.AndEx("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID, len(r.WAREHOUSEINFOID) > 0)
  1615. sqlId.Join(" order by t.reckondate")
  1616. return sqlId.String()
  1617. }
  1618. // buildSqlReckonSum 日照表(汇总维度)
  1619. func (r *Ermcp3AreaStockReportEx) buildSqlReckonSum(fields []string) string {
  1620. var sqlId utils.SQLVal = "SELECT a.*, %v" +
  1621. " from (SELECT %v," +
  1622. " sum(t.ORISTOCK) ORISTOCK," +
  1623. " sum(t.CURSTOCK) CURSTOCK," +
  1624. " sum(t.TODAYBUYINQTY) TODAYBUYINQTY," +
  1625. " sum(t.TODAYPRODUCEINQTY) TODAYPRODUCEINQTY," +
  1626. " sum(t.TODAYSELLOUTQTY) TODAYSELLOUTQTY," +
  1627. " sum(t.TODAYPRODUCEOUTQTY) TODAYPRODUCEOUTQTY" +
  1628. " FROM RECKON_ERMCP_AREASTOCK t" +
  1629. " WHERE 1 = 1 %v" +
  1630. " GROUP BY %v) a %v"
  1631. var sqlParam utils.SQLVal
  1632. sqlParam.And("t.USERID", r.USERID)
  1633. sqlParam.AndEx("t.RECKONDATE", r.RECKONDATE, len(r.RECKONDATE) > 0)
  1634. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  1635. sqlParam.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1636. }
  1637. // 现货商品id
  1638. sqlParam.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  1639. // 品类id
  1640. sqlParam.AndEx("t.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  1641. // 品牌id
  1642. sqlParam.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  1643. // 仓库id
  1644. sqlParam.AndEx("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID, len(r.WAREHOUSEINFOID) > 0)
  1645. var strFileds string
  1646. for _, v := range fields {
  1647. if len(strFileds) > 0 {
  1648. strFileds += ","
  1649. }
  1650. strFileds += "t." + v
  1651. }
  1652. exFields, exLeftJoinTable := r.getExFieldInfo()
  1653. sqlId.FormatParam(exFields, strFileds, sqlParam.String(), strFileds, exLeftJoinTable)
  1654. sqlId.Join(" order by a.reckondate")
  1655. return sqlId.String()
  1656. }
  1657. // buildSqlReport 报表表
  1658. func (r *Ermcp3AreaStockReportEx) buildSqlReport() string {
  1659. var sqlId utils.SQLVal = "SELECT t.TODAYBUYINQTY," +
  1660. " t.TODAYPRODUCEINQTY," +
  1661. " t.cycletype," +
  1662. " t.cycletime," +
  1663. " t.USERID," +
  1664. " t.WRSTANDARDID," +
  1665. " t.SPOTGOODSBRANDID," +
  1666. " t.WAREHOUSEINFOID," +
  1667. " t.deliverygoodsid," +
  1668. " t.TODAYSELLOUTQTY," +
  1669. " t.TODAYPRODUCEOUTQTY," +
  1670. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1671. " t.ORISTOCK," +
  1672. " t.CURSTOCK," +
  1673. " w.wrstandardname," +
  1674. " w.wrstandardcode," +
  1675. " w.unitid," +
  1676. " gb.dgfactoryitemvalue brandname," +
  1677. " h.warehousename," +
  1678. " h.warehousecode," +
  1679. " h.warehousetype," +
  1680. " g.deliverygoodscode," +
  1681. " g.deliverygoodsname," +
  1682. " g.goodsunitid" +
  1683. " FROM REPORT_ERMCP_AREASTOCK t" +
  1684. " LEFT JOIN WRSTANDARD w" +
  1685. " on t.wrstandardid = w.wrstandardid" +
  1686. " LEFT JOIN dgfactoryitem gb" +
  1687. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1688. " LEFT JOIN deliverygoods g" +
  1689. " on t.deliverygoodsid = g.deliverygoodsid" +
  1690. " LEFT JOIN WAREHOUSEINFO h" +
  1691. " on t.warehouseinfoid = h.autoid" +
  1692. " WHERE 1 = 1"
  1693. sqlId.And("t.USERID", r.USERID)
  1694. sqlId.And("t.cycletype", r.CYCLETYPE)
  1695. sqlId.And("t.cycletime", r.CYCLETIME)
  1696. // 现货商品id
  1697. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  1698. // 品类id
  1699. sqlId.AndEx("t.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  1700. // 品牌id
  1701. sqlId.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  1702. // 仓库id
  1703. sqlId.AndEx("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID, len(r.WAREHOUSEINFOID) > 0)
  1704. return sqlId.String()
  1705. }
  1706. // buildSqlReport 报表表(汇总维度)
  1707. func (r *Ermcp3AreaStockReportEx) buildSqlReportSum(fields []string) string {
  1708. var sqlId utils.SQLVal = "SELECT a.*, %v" +
  1709. " from (SELECT %v," +
  1710. " sum(t.ORISTOCK) ORISTOCK," +
  1711. " sum(t.CURSTOCK) CURSTOCK," +
  1712. " sum(t.TODAYBUYINQTY) TODAYBUYINQTY," +
  1713. " sum(t.TODAYPRODUCEINQTY) TODAYPRODUCEINQTY," +
  1714. " sum(t.TODAYSELLOUTQTY) TODAYSELLOUTQTY," +
  1715. " sum(t.TODAYPRODUCEOUTQTY) TODAYPRODUCEOUTQTY" +
  1716. " FROM REPORT_ERMCP_AREASTOCK t" +
  1717. " WHERE 1 = 1 %v" +
  1718. " GROUP BY %v) a %v"
  1719. var sqlParam utils.SQLVal
  1720. sqlParam.And("t.USERID", r.USERID)
  1721. sqlParam.And("t.cycletype", r.CYCLETYPE)
  1722. sqlParam.And("t.cycletime", r.CYCLETIME)
  1723. // 现货商品id
  1724. sqlParam.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  1725. // 品类id
  1726. sqlParam.AndEx("t.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  1727. // 品牌id
  1728. sqlParam.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  1729. // 仓库id
  1730. sqlParam.AndEx("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID, len(r.WAREHOUSEINFOID) > 0)
  1731. var strFileds string
  1732. for _, v := range fields {
  1733. if len(strFileds) > 0 {
  1734. strFileds += ","
  1735. }
  1736. strFileds += "t." + v
  1737. }
  1738. exFields, exLeftJoinTable := r.getExFieldInfo()
  1739. sqlId.FormatParam(exFields, strFileds, sqlParam.String(), strFileds, exLeftJoinTable)
  1740. return sqlId.String()
  1741. }
  1742. // Ermcp3ArealSumPL 损益汇总表
  1743. type Ermcp3ArealSumPL struct {
  1744. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照时期(yyyyMMdd)
  1745. CYCLETIME string `json:"cycletime" xorm:"'CYCLETIME'"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  1746. CYCLETYPE int32 `json:"cycletype" xorm:"'CYCLETYPE'"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  1747. AREAUSERID int64 `json:"areauserid" xorm:"AREAUSERID"` // 所属机构\交易用户ID
  1748. MIDDLEGOODSID int64 `json:"middlegoodsid" xorm:"MIDDLEGOODSID"` // 套保品种ID(SEQ_ERMS_MIDDLEGOODS)
  1749. CURRENCYID int32 `json:"currencyid" xorm:"CURRENCYID"` // 结算币种ID
  1750. SPOTACTUALPL float64 `json:"spotactualpl" xorm:"SPOTACTUALPL"` // 现货损益
  1751. SPOTFLOATPL float64 `json:"spotfloatpl" xorm:"SPOTFLOATPL"` // 现货浮动损益
  1752. FUTUREACTUALPL float64 `json:"futureactualpl" xorm:"FUTUREACTUALPL"` // 期货损益
  1753. FUTUREFLOATPL float64 `json:"futurefloatpl" xorm:"FUTUREFLOATPL"` // 期货浮动损益
  1754. SUMACTUALPL float64 `json:"sumactualpl" xorm:"SUMACTUALPL"` // 实际损益 = (ActualPL + FutureActualPL)
  1755. SUMPL float64 `json:"sumpl" xorm:"'SUMPL'"` // 汇总损益
  1756. UPDATETIME string `json:"updatetime" xorm:"UPDATETIME"` // 更新时间
  1757. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保品种名称
  1758. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保品种代码
  1759. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODSUNITID'"` // 套保品种单位id
  1760. ACCOUNTNAME string `json:"accountname"` // 机构名称
  1761. ENUMDICNAME string `json:"enumdicname"` // 单位名称(套保品种)
  1762. CURRENCYNAME string `json:"currencyname"` // 币种名称
  1763. QueryType int32 `json:"-"` // 查询类型 1-报表 2-报表明细
  1764. BeginDate string `json:"begindate"` // 开始交易日
  1765. EndDate string `json:"enddate"` // 结束交易日
  1766. }
  1767. func (r *Ermcp3ArealSumPL) calc() {
  1768. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  1769. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.GOODSUNITID)
  1770. r.ACCOUNTNAME = mtpcache.GetUserNameByUserId(r.AREAUSERID)
  1771. }
  1772. func (r *Ermcp3ArealSumPL) buildSql() string {
  1773. // 日报表
  1774. if r.QueryType == 1 && r.CYCLETYPE == 0 {
  1775. return r.buildSqlDay()
  1776. }
  1777. // 日报表明细
  1778. if r.QueryType == 2 && r.CYCLETYPE == 0 {
  1779. return r.buildSqlDayDetail()
  1780. }
  1781. return r.buildSqlCycle()
  1782. }
  1783. // buildSqlDay 损益汇总日报表(需要汇总维度)
  1784. func (r *Ermcp3ArealSumPL) buildSqlDay() string {
  1785. var sqlId utils.SQLVal = "SELECT t.RECKONDATE," +
  1786. " t.AREAUSERID," +
  1787. " t.CURRENCYID," +
  1788. " sum(t.SPOTACTUALPL) SPOTACTUALPL," +
  1789. " sum(t.SPOTFLOATPL) SPOTFLOATPL," +
  1790. " sum(t.FUTUREACTUALPL) FUTUREACTUALPL," +
  1791. " sum(t.FUTUREFLOATPL) FUTUREFLOATPL," +
  1792. " sum(t.SUMACTUALPL) SUMACTUALPL," +
  1793. " sum(t.SUMPL) SUMPL" +
  1794. " FROM RECKON_ERMCP_AREASUMPL t" +
  1795. " WHERE 1 = 1"
  1796. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  1797. if r.QueryType == 1 {
  1798. sqlId.And("t.RECKONDATE", r.RECKONDATE)
  1799. } else if r.QueryType == 2 {
  1800. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1801. }
  1802. sqlId.Join(" GROUP BY t.RECKONDATE, t.AREAUSERID, t.CURRENCYID")
  1803. return sqlId.String()
  1804. }
  1805. func (r *Ermcp3ArealSumPL) buildSqlDayDetail() string {
  1806. var sqlId utils.SQLVal = "SELECT t.RECKONDATE," +
  1807. " t.AREAUSERID," +
  1808. " t.MIDDLEGOODSID," +
  1809. " t.CURRENCYID," +
  1810. " t.SPOTACTUALPL," +
  1811. " t.SPOTFLOATPL," +
  1812. " t.FUTUREACTUALPL," +
  1813. " t.FUTUREFLOATPL," +
  1814. " t.SUMACTUALPL," +
  1815. " t.SUMPL," +
  1816. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1817. " g.middlegoodsname," +
  1818. " g.middlegoodscode," +
  1819. " g.goodsunitid" +
  1820. " FROM RECKON_ERMCP_AREASUMPL t" +
  1821. " LEFT JOIN ERMS_MIDDLEGOODS g on t.middlegoodsid=g.middlegoodsid" +
  1822. " WHERE 1 = 1"
  1823. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  1824. sqlId.AndEx("t.RECKONDATE", r.RECKONDATE, len(r.RECKONDATE) > 0)
  1825. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  1826. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1827. }
  1828. sqlId.And("t.CURRENCYID", r.CURRENCYID)
  1829. return sqlId.String()
  1830. }
  1831. func (r *Ermcp3ArealSumPL) buildSqlCycle() string {
  1832. var sqlId utils.SQLVal = "SELECT t.AREAUSERID," +
  1833. " t.CYCLETIME," +
  1834. " t.CYCLETYPE," +
  1835. " t.CURRENCYID," +
  1836. " sum(t.SPOTACTUALPL) SPOTACTUALPL," +
  1837. " sum(t.SPOTFLOATPL) SPOTFLOATPL," +
  1838. " sum(t.FUTUREACTUALPL) FUTUREACTUALPL," +
  1839. " sum(t.FUTUREFLOATPL) FUTUREFLOATPL," +
  1840. " sum(t.SUMACTUALPL) SUMACTUALPL," +
  1841. " sum(t.SUMPL) SUMPL" +
  1842. " FROM REPORT_ERMCP_AREASUMPL t" +
  1843. " WHERE 1 = 1"
  1844. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  1845. sqlId.And("t.cycletype", r.CYCLETYPE)
  1846. sqlId.And("t.cycletime", r.CYCLETIME)
  1847. sqlId.Join(" GROUP BY t.AREAUSERID, t.CURRENCYID, t.CYCLETIME, t.CYCLETYPE")
  1848. return sqlId.String()
  1849. }
  1850. // GetDataEx 获取损益汇总表
  1851. func (r *Ermcp3ArealSumPL) GetDataEx() (interface{}, error) {
  1852. sData := make([]Ermcp3ArealSumPL, 0)
  1853. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1854. for i := range sData {
  1855. sData[i].calc()
  1856. sData[i].BeginDate = r.BeginDate
  1857. sData[i].EndDate = r.EndDate
  1858. }
  1859. return sData, err
  1860. }
  1861. // Ermcp3TaFutuReDataReport 期货报表
  1862. type Ermcp3TaFutuReDataReport struct {
  1863. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照时期(yyyyMMdd)
  1864. CYCLETIME string `json:"cycletime" xorm:"CYCLETIME"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  1865. CYCLETYPE int32 `json:"cycletype" xorm:"CYCLETYPE"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  1866. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID"` // 交易账户
  1867. GOODSID int32 `json:"goodsid" xorm:"GOODSID"` // 交易合约
  1868. BUYORSELL int32 `json:"buyorsell" xorm:"BUYORSELL"` // 持仓方向
  1869. CURRENCYID int32 `json:"currencyid" xorm:"CURRENCYID"` // 结算币种ID
  1870. USERID int64 `json:"userid" xorm:"USERID"` // 账户所属用户ID
  1871. RELATEDUSERID int64 `json:"relateduserid" xorm:"RELATEDUSERID"` // 账户关联用户ID
  1872. GOODSGROUPID int32 `json:"goodsgroupid" xorm:"GOODSGROUPID"` // 交易品种
  1873. ORIHOLDQTY float64 `json:"oriholdqty" xorm:"ORIHOLDQTY"` // 期初持仓量
  1874. ORIOPENAMOUNT float64 `json:"oriopenamount" xorm:"ORIOPENAMOUNT"` // 期初开仓额
  1875. ORIHOLDAMOUNT float64 `json:"oriholdamount" xorm:"ORIHOLDAMOUNT"` // 期初持仓额
  1876. TODAYOPENQTY float64 `json:"todayopenqty" xorm:"TODAYOPENQTY"` // 今开仓量
  1877. TODAYOPENAMOUNT float64 `json:"todayopenamount" xorm:"TODAYOPENAMOUNT"` // 今开仓额
  1878. TODAYCLOSEQTY float64 `json:"todaycloseqty" xorm:"TODAYCLOSEQTY"` // 今平仓量
  1879. TODAYCLOSEAMOUNT float64 `json:"todaycloseamount" xorm:"TODAYCLOSEAMOUNT"` // 今平仓额
  1880. HOLDQTY float64 `json:"holdqty" xorm:"HOLDQTY"` // 期末持仓量
  1881. OPENAMOUNT float64 `json:"openamount" xorm:"OPENAMOUNT"` // 期末开仓额
  1882. HOLDAMOUNT float64 `json:"holdamount" xorm:"HOLDAMOUNT"` // 期末持仓额
  1883. RECKONPRICE float64 `json:"reckonprice" xorm:"RECKONPRICE"` // 结算价
  1884. RECKONHOLDAMOUNT float64 `json:"reckonholdamount" xorm:"RECKONHOLDAMOUNT"` // 结算持仓额
  1885. CHARGE float64 `json:"charge" xorm:"CHARGE"` // 手续费
  1886. CLOSEPL float64 `json:"closepl" xorm:"CLOSEPL"` // 平仓损益
  1887. RECKONPL float64 `json:"reckonpl" xorm:"RECKONPL"` // 结算损益
  1888. LASTPRICE float64 `json:"lastprice" xorm:"LASTPRICE"` // 最新价
  1889. LASTHOLDAMOUNT float64 `json:"lastholdamount" xorm:"LASTHOLDAMOUNT"` // 当前持仓额
  1890. FLOATPL float64 `json:"floatpl" xorm:"FLOATPL"` // 浮动损益
  1891. GOODSGROUPSPOTQTY float64 `json:"goodsgroupspotqty" xorm:"GOODSGROUPSPOTQTY"` // 交易品种期末量 (= 期末持仓量 * 合约乘数)
  1892. TODAYGOODSGROUPSPOTQTY float64 `json:"todaygoodsgroupspotqty" xorm:"TODAYGOODSGROUPSPOTQTY"` // 交易品种变化量 (=(期末持仓量 - 期初持仓量)* 合约乘数)
  1893. MIDDLEGOODSQTY float64 `json:"middlegoodsqty" xorm:"MIDDLEGOODSQTY"` // 套保品种期末量 (=交易品种期末量 * 期货品种折算系数)
  1894. TODAYMIDDLEGOODSQTY float64 `json:"todaymiddlegoodsqty" xorm:"TODAYMIDDLEGOODSQTY"` // 套保品种变化量 (= 交易品种变化量*期货品种折算系数)
  1895. RECKONPL2 float64 `json:"reckonpl2" xorm:"RECKONPL2"` // 结算逐笔盈亏
  1896. OUTERGROUPCODE string `json:"outergroupcode" xorm:"'OUTERGROUPCODE'"` // 交易品种代码
  1897. GOODSGROUPNAME string `json:"goodsgroupname" xorm:"'GOODSGROUPNAME'"` // 交易品种名称
  1898. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 交易合约代码
  1899. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 交易合约名称
  1900. GOODUNITID int32 `json:"goodunitid" xorm:"'GOODUNITID'"` // 交易合约单位id
  1901. ACCOUNTNAME string `json:"accountname" xorm:"'ACCOUNTNAME'"` // 交易账号名称
  1902. OPENAVGPRICE float64 `json:"openavgprice"` // 开仓均价
  1903. CLOSEAVGPRICE float64 `json:"closeavgprice"` // 平仓均价
  1904. ENUMDICNAME string `json:"enumdicname"` // 单位名称(现货商品)
  1905. QueryType int32 `json:"-"` // 查询类型 1-报表 2-报表明细
  1906. BeginDate string `json:"begindate"` // 开始交易日
  1907. EndDate string `json:"enddate"` // 结束交易日
  1908. }
  1909. func (r *Ermcp3TaFutuReDataReport) calc() {
  1910. if r.TODAYOPENQTY != 0 {
  1911. r.OPENAVGPRICE = r.TODAYOPENAMOUNT / r.TODAYOPENQTY
  1912. }
  1913. if r.TODAYCLOSEQTY != 0 {
  1914. r.CLOSEAVGPRICE = r.TODAYCLOSEAMOUNT / r.TODAYCLOSEQTY
  1915. }
  1916. }
  1917. func (r *Ermcp3TaFutuReDataReport) userType() int32 {
  1918. var userType int32 = 2
  1919. areaUserId := mtpcache.GetAreaUserId(r.USERID, 0)
  1920. if areaUserId == r.USERID {
  1921. userType = 2
  1922. } else {
  1923. userType = 7
  1924. }
  1925. return userType
  1926. }
  1927. func (r *Ermcp3TaFutuReDataReport) buildSql() string {
  1928. userType := r.userType()
  1929. // 日报表
  1930. if r.QueryType == 1 && r.CYCLETYPE == 0 {
  1931. return r.buildSqlReckon(userType)
  1932. }
  1933. // 日报表明细
  1934. if r.QueryType == 2 && r.CYCLETYPE == 0 {
  1935. return r.buildSqlReckonDetail(userType)
  1936. }
  1937. return r.buildSqlReport(userType)
  1938. }
  1939. // buildSqlReckon 日照表
  1940. func (r *Ermcp3TaFutuReDataReport) buildSqlReckon(userType int32) string {
  1941. var sqlId utils.SQLVal = "select a.*," +
  1942. " gp.outergroupcode," +
  1943. " gp.goodsgroupname," +
  1944. " g.goodscode," +
  1945. " g.goodsname," +
  1946. " g.goodunitid" +
  1947. " from (SELECT t.RECKONDATE," +
  1948. " t.GOODSID," +
  1949. " t.BUYORSELL," +
  1950. " t.CURRENCYID," +
  1951. " t.%v USERID," +
  1952. " t.GOODSGROUPID," +
  1953. " sum(t.ORIHOLDQTY) ORIHOLDQTY," +
  1954. " sum(t.ORIOPENAMOUNT) ORIOPENAMOUNT," +
  1955. " sum(t.ORIHOLDAMOUNT) ORIHOLDAMOUNT," +
  1956. " sum(t.TODAYOPENQTY) TODAYOPENQTY," +
  1957. " sum(t.TODAYOPENAMOUNT) TODAYOPENAMOUNT," +
  1958. " sum(t.TODAYCLOSEQTY) TODAYCLOSEQTY," +
  1959. " sum(t.TODAYCLOSEAMOUNT) TODAYCLOSEAMOUNT," +
  1960. " sum(t.HOLDQTY) HOLDQTY," +
  1961. " sum(t.OPENAMOUNT) OPENAMOUNT," +
  1962. " sum(t.HOLDAMOUNT) HOLDAMOUNT," +
  1963. " sum(t.RECKONPRICE) RECKONPRICE," +
  1964. " sum(t.RECKONHOLDAMOUNT) RECKONHOLDAMOUNT," +
  1965. " sum(t.CHARGE) CHARGE," +
  1966. " sum(t.CLOSEPL) CLOSEPL," +
  1967. " sum(t.RECKONPL) RECKONPL," +
  1968. " max(t.LASTPRICE) LASTPRICE," +
  1969. " sum(t.LASTHOLDAMOUNT) LASTHOLDAMOUNT," +
  1970. " sum(t.FLOATPL) FLOATPL," +
  1971. " sum(t.GOODSGROUPSPOTQTY) GOODSGROUPSPOTQTY," +
  1972. " sum(t.TODAYGOODSGROUPSPOTQTY) TODAYGOODSGROUPSPOTQTY," +
  1973. " sum(t.MIDDLEGOODSQTY) MIDDLEGOODSQTY," +
  1974. " sum(t.TODAYMIDDLEGOODSQTY) TODAYMIDDLEGOODSQTY," +
  1975. " sum(t.Reckonpl2) RECKONPL2" +
  1976. " FROM RECKON_ERMCP_TAFUTUREDATA t" +
  1977. " WHERE 1 = 1 %v" +
  1978. " GROUP BY t.RECKONDATE," +
  1979. " t.GOODSID," +
  1980. " t.BUYORSELL," +
  1981. " t.CURRENCYID," +
  1982. " t.%v," +
  1983. " t.GOODSGROUPID) a" +
  1984. " left join goodsgroup gp" +
  1985. " on a.goodsgroupid = gp.goodsgroupid" +
  1986. " left join goods g" +
  1987. " on a.goodsid = g.goodsid"
  1988. userId := "USERID"
  1989. var sqlParam utils.SQLVal
  1990. if userType == 2 {
  1991. sqlParam.AndEx("t.userid", r.USERID, userType == 2)
  1992. userId = "USERID"
  1993. } else if userType == 7 {
  1994. sqlParam.AndEx("t.relateduserid", r.USERID, userType == 7)
  1995. userId = "RELATEDUSERID"
  1996. }
  1997. sqlParam.AndEx("t.reckondate", r.RECKONDATE, len(r.RECKONDATE) > 0)
  1998. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  1999. sqlParam.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  2000. }
  2001. sqlId.FormatParam(userId, sqlParam.String(), userId)
  2002. return sqlId.String()
  2003. }
  2004. // buildSqlReckon 日照表明细
  2005. func (r *Ermcp3TaFutuReDataReport) buildSqlReckonDetail(userType int32) string {
  2006. var sqlId utils.SQLVal = "select a.*," +
  2007. " gp.outergroupcode," +
  2008. " gp.goodsgroupname," +
  2009. " g.goodscode," +
  2010. " g.goodsname," +
  2011. " g.goodunitid," +
  2012. " ta.accountname" +
  2013. " from (SELECT t.reckondate," +
  2014. " t.GOODSID," +
  2015. " t.BUYORSELL," +
  2016. " t.CURRENCYID," +
  2017. " t.%v USERID," +
  2018. " t.accountid," +
  2019. " t.GOODSGROUPID," +
  2020. " sum(t.ORIHOLDQTY) ORIHOLDQTY," +
  2021. " sum(t.ORIOPENAMOUNT) ORIOPENAMOUNT," +
  2022. " sum(t.ORIHOLDAMOUNT) ORIHOLDAMOUNT," +
  2023. " sum(t.TODAYOPENQTY) TODAYOPENQTY," +
  2024. " sum(t.TODAYOPENAMOUNT) TODAYOPENAMOUNT," +
  2025. " sum(t.TODAYCLOSEQTY) TODAYCLOSEQTY," +
  2026. " sum(t.TODAYCLOSEAMOUNT) TODAYCLOSEAMOUNT," +
  2027. " sum(t.HOLDQTY) HOLDQTY," +
  2028. " sum(t.OPENAMOUNT) OPENAMOUNT," +
  2029. " sum(t.HOLDAMOUNT) HOLDAMOUNT," +
  2030. " sum(t.RECKONPRICE) RECKONPRICE," +
  2031. " sum(t.RECKONHOLDAMOUNT) RECKONHOLDAMOUNT," +
  2032. " sum(t.CHARGE) CHARGE," +
  2033. " sum(t.CLOSEPL) CLOSEPL," +
  2034. " sum(t.RECKONPL) RECKONPL," +
  2035. " max(t.LASTPRICE) LASTPRICE," +
  2036. " sum(t.LASTHOLDAMOUNT) LASTHOLDAMOUNT," +
  2037. " sum(t.FLOATPL) FLOATPL," +
  2038. " sum(t.GOODSGROUPSPOTQTY) GOODSGROUPSPOTQTY," +
  2039. " sum(t.TODAYGOODSGROUPSPOTQTY) TODAYGOODSGROUPSPOTQTY," +
  2040. " sum(t.MIDDLEGOODSQTY) MIDDLEGOODSQTY," +
  2041. " sum(t.TODAYMIDDLEGOODSQTY) TODAYMIDDLEGOODSQTY," +
  2042. " sum(t.Reckonpl2) RECKONPL2" +
  2043. " FROM RECKON_ERMCP_TAFUTUREDATA t" +
  2044. " WHERE 1 = 1 %v" +
  2045. " GROUP BY t.reckondate," +
  2046. " t.GOODSID," +
  2047. " t.BUYORSELL," +
  2048. " t.CURRENCYID," +
  2049. " t.%v," +
  2050. " t.accountid," +
  2051. " t.GOODSGROUPID) a" +
  2052. " left join goodsgroup gp" +
  2053. " on a.goodsgroupid = gp.goodsgroupid" +
  2054. " left join goods g" +
  2055. " on a.goodsid = g.goodsid" +
  2056. " left join taaccount ta on a.accountid = ta.accountid"
  2057. userId := "USERID"
  2058. var sqlParam utils.SQLVal
  2059. if userType == 2 {
  2060. sqlParam.AndEx("t.userid", r.USERID, userType == 2)
  2061. userId = "USERID"
  2062. } else if userType == 7 {
  2063. sqlParam.AndEx("t.relateduserid", r.USERID, userType == 7)
  2064. userId = "RELATEDUSERID"
  2065. }
  2066. sqlParam.And("t.CURRENCYID", r.CURRENCYID)
  2067. sqlParam.And("t.GOODSGROUPID", r.GOODSGROUPID)
  2068. sqlParam.And("t.GOODSID", r.GOODSID)
  2069. sqlParam.And("t.BUYORSELL", r.BUYORSELL)
  2070. sqlParam.AndEx("t.reckondate", r.RECKONDATE, len(r.RECKONDATE) > 0)
  2071. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  2072. sqlParam.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  2073. }
  2074. sqlId.FormatParam(userId, sqlParam.String(), userId)
  2075. return sqlId.String()
  2076. }
  2077. // buildSqlReckon 报表表
  2078. func (r *Ermcp3TaFutuReDataReport) buildSqlReport(userType int32) string {
  2079. var sqlId utils.SQLVal = "select a.*," +
  2080. " gp.outergroupcode," +
  2081. " gp.goodsgroupname," +
  2082. " g.goodscode," +
  2083. " g.goodsname," +
  2084. " g.goodunitid" +
  2085. " from (SELECT t.cycletype," +
  2086. " t.cycletime," +
  2087. " t.GOODSID," +
  2088. " t.BUYORSELL," +
  2089. " t.CURRENCYID," +
  2090. " t.%v USERID," +
  2091. " t.GOODSGROUPID," +
  2092. " sum(t.ORIHOLDQTY) ORIHOLDQTY," +
  2093. " sum(t.ORIOPENAMOUNT) ORIOPENAMOUNT," +
  2094. " sum(t.ORIHOLDAMOUNT) ORIHOLDAMOUNT," +
  2095. " sum(t.TODAYOPENQTY) TODAYOPENQTY," +
  2096. " sum(t.TODAYOPENAMOUNT) TODAYOPENAMOUNT," +
  2097. " sum(t.TODAYCLOSEQTY) TODAYCLOSEQTY," +
  2098. " sum(t.TODAYCLOSEAMOUNT) TODAYCLOSEAMOUNT," +
  2099. " sum(t.HOLDQTY) HOLDQTY," +
  2100. " sum(t.OPENAMOUNT) OPENAMOUNT," +
  2101. " sum(t.HOLDAMOUNT) HOLDAMOUNT," +
  2102. " sum(t.RECKONPRICE) RECKONPRICE," +
  2103. " sum(t.RECKONHOLDAMOUNT) RECKONHOLDAMOUNT," +
  2104. " sum(t.CHARGE) CHARGE," +
  2105. " sum(t.CLOSEPL) CLOSEPL," +
  2106. " sum(t.RECKONPL) RECKONPL," +
  2107. " max(t.LASTPRICE) LASTPRICE," +
  2108. " sum(t.LASTHOLDAMOUNT) LASTHOLDAMOUNT," +
  2109. " sum(t.FLOATPL) FLOATPL," +
  2110. " sum(t.GOODSGROUPSPOTQTY) GOODSGROUPSPOTQTY," +
  2111. " sum(t.TODAYGOODSGROUPSPOTQTY) TODAYGOODSGROUPSPOTQTY," +
  2112. " sum(t.MIDDLEGOODSQTY) MIDDLEGOODSQTY," +
  2113. " sum(t.TODAYMIDDLEGOODSQTY) TODAYMIDDLEGOODSQTY," +
  2114. " sum(t.Reckonpl2) RECKONPL2" +
  2115. " FROM REPORT_ERMCP_TAFUTUREDATA t" +
  2116. " WHERE 1 = 1 %v" +
  2117. " GROUP BY t.cycletype," +
  2118. " t.cycletime," +
  2119. " t.GOODSID," +
  2120. " t.BUYORSELL," +
  2121. " t.CURRENCYID," +
  2122. " t.%v," +
  2123. " t.GOODSGROUPID) a" +
  2124. " left join goodsgroup gp" +
  2125. " on a.goodsgroupid = gp.goodsgroupid" +
  2126. " left join goods g" +
  2127. " on a.goodsid = g.goodsid"
  2128. userId := "USERID"
  2129. var sqlParam utils.SQLVal
  2130. if userType == 2 {
  2131. sqlParam.AndEx("t.userid", r.USERID, userType == 2)
  2132. userId = "USERID"
  2133. } else if userType == 7 {
  2134. sqlParam.AndEx("t.relateduserid", r.USERID, userType == 7)
  2135. userId = "RELATEDUSERID"
  2136. }
  2137. sqlParam.And("t.cycletype", r.CYCLETYPE)
  2138. sqlParam.And("t.cycletime", r.CYCLETIME)
  2139. sqlId.FormatParam(userId, sqlParam.String(), userId)
  2140. return sqlId.String()
  2141. }
  2142. // GetDataEx 获取期货报表
  2143. func (r *Ermcp3TaFutuReDataReport) GetDataEx() (interface{}, error) {
  2144. sData := make([]Ermcp3TaFutuReDataReport, 0)
  2145. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2146. for i := range sData {
  2147. sData[i].calc()
  2148. sData[i].BeginDate = r.BeginDate
  2149. sData[i].EndDate = r.EndDate
  2150. }
  2151. return sData, err
  2152. }
  2153. // ReckonDayPosition 持仓头寸日照
  2154. type ReckonDayPosition struct {
  2155. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID"` // 账号Id
  2156. GOODSID int32 `json:"goodsid" xorm:"GOODSID"` // 商品Id
  2157. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照日期(yyyyMMdd)
  2158. BUYPOSITIONQTY int32 `json:"buypositionqty" xorm:"BUYPOSITIONQTY"` // 买期初持仓数量
  2159. BUYHOLDERAMOUNT float64 `json:"buyholderamount" xorm:"BUYHOLDERAMOUNT"` // 买持仓期初总金额
  2160. BUYCURPOSITIONQTY int32 `json:"buycurpositionqty" xorm:"BUYCURPOSITIONQTY"` // 买当前持仓总数量
  2161. BUYCURHOLDERAMOUNT float64 `json:"buycurholderamount" xorm:"BUYCURHOLDERAMOUNT"` // 买当前持仓总金额
  2162. BUYFROZENQTY int32 `json:"buyfrozenqty" xorm:"BUYFROZENQTY"` // 买持仓冻结
  2163. BUYOTHERFROZENQTY int32 `json:"buyotherfrozenqty" xorm:"BUYOTHERFROZENQTY"` // 买持仓其他冻结(交割冻结)
  2164. BUYOPENREQQTY int32 `json:"buyopenreqqty" xorm:"BUYOPENREQQTY"` // 买开仓申请数量
  2165. BUYRECKONPL float64 `json:"buyreckonpl" xorm:"BUYRECKONPL"` // 买结算盈亏
  2166. BUYINTEREST float64 `json:"buyinterest" xorm:"BUYINTEREST"` // 买递延费(仓储费)
  2167. BUYUSEDMARGIN float64 `json:"buyusedmargin" xorm:"BUYUSEDMARGIN"` // 占用保证金
  2168. BUYOPENTOTALQTY int32 `json:"buyopentotalqty" xorm:"BUYOPENTOTALQTY"` // 今日买开仓总数量
  2169. BUYCLOSETOTALQTY int32 `json:"buyclosetotalqty" xorm:"BUYCLOSETOTALQTY"` // 今日买平仓总数量
  2170. BUYCLOSETOTALPL float64 `json:"buyclosetotalpl" xorm:"BUYCLOSETOTALPL"` // 买平仓总盈亏
  2171. SELLPOSITIONQTY int32 `json:"sellpositionqty" xorm:"SELLPOSITIONQTY"` // 卖期初持仓数量
  2172. SELLHOLDERAMOUNT float64 `json:"sellholderamount" xorm:"SELLHOLDERAMOUNT"` // 卖持仓期初总金额
  2173. SELLCURPOSITIONQTY int32 `json:"sellcurpositionqty" xorm:"SELLCURPOSITIONQTY"` // 卖当前持仓数量
  2174. SELLCURHOLDERAMOUNT float64 `json:"sellcurholderamount" xorm:"SELLCURHOLDERAMOUNT"` // 卖当前持仓总金额
  2175. SELLFROZENQTY int32 `json:"sellfrozenqty" xorm:"SELLFROZENQTY"` // 卖持仓冻结
  2176. SELLOTHERFROZENQTY int32 `json:"sellotherfrozenqty" xorm:"SELLOTHERFROZENQTY"` // 卖持仓其他冻结(交割冻结)
  2177. SELLOPENREQQTY int32 `json:"sellopenreqqty" xorm:"SELLOPENREQQTY"` // 卖开仓申请数量
  2178. SELLRECKONPL float64 `json:"sellreckonpl" xorm:"SELLRECKONPL"` // 卖结算盈亏
  2179. SELLINTEREST float64 `json:"sellinterest" xorm:"SELLINTEREST"` // 卖递延费
  2180. SELLUSEDMARGIN float64 `json:"sellusedmargin" xorm:"SELLUSEDMARGIN"` // 卖占用保证金-作废
  2181. SELLOPENTOTALQTY int32 `json:"sellopentotalqty" xorm:"SELLOPENTOTALQTY"` // 卖开仓总数量
  2182. SELLCLOSETOTALQTY int32 `json:"sellclosetotalqty" xorm:"SELLCLOSETOTALQTY"` // 卖平仓总数量
  2183. SELLCLOSETOTALPL float64 `json:"sellclosetotalpl" xorm:"SELLCLOSETOTALPL"` // 卖平仓总盈亏
  2184. TRADEPROPERTY int32 `json:"tradeproperty" xorm:"TRADEPROPERTY"` // 交易属性
  2185. MARGINALGORITHM int32 `json:"marginalgorithm" xorm:"MARGINALGORITHM"` // 保证金收取方式 - 1:比率 2:固定
  2186. MARGINVALUE float64 `json:"marginvalue" xorm:"MARGINVALUE"` // 保证金设置值(投资管理系统-多LongMarginValue)
  2187. BUYFEEALGORITHM int32 `json:"buyfeealgorithm" xorm:"BUYFEEALGORITHM"` // 买递延费收取方式 - 1:比率 2:固定
  2188. BUYMEMBERFEEVALUE float64 `json:"buymemberfeevalue" xorm:"BUYMEMBERFEEVALUE"` // 买会员递延费设置值
  2189. BUYEXCHAGEFEEVALUE float64 `json:"buyexchagefeevalue" xorm:"BUYEXCHAGEFEEVALUE"` // 买交易所递延费设置值
  2190. SELLFEEALGORITHM int32 `json:"sellfeealgorithm" xorm:"SELLFEEALGORITHM"` // 卖递延费收取方式 - 1:比率 2:固定
  2191. SELLMEMBERFEEVALUE float64 `json:"sellmemberfeevalue" xorm:"SELLMEMBERFEEVALUE"` // 卖会员递延费设置值
  2192. SELLEXCHAGEFEEVALUE float64 `json:"sellexchagefeevalue" xorm:"SELLEXCHAGEFEEVALUE"` // 卖交易所递延费设置值
  2193. INTEREST2 float64 `json:"interest2" xorm:"INTEREST2"` // 过夜费
  2194. ACCOUNTCURRENCYID int32 `json:"accountcurrencyid" xorm:"ACCOUNTCURRENCYID"` // 账户币种ID
  2195. GOODSCURRENCYID int32 `json:"goodscurrencyid" xorm:"GOODSCURRENCYID"` // 商品币种ID
  2196. CUREXCHANGERATE float64 `json:"curexchangerate" xorm:"CUREXCHANGERATE"` // 当前汇率
  2197. BUYRECKONPL2 float64 `json:"buyreckonpl2" xorm:"BUYRECKONPL2"` // 买结算盈亏(逐笔) - 根据持仓单日照表字段汇率
  2198. SELLRECKONPL2 float64 `json:"sellreckonpl2" xorm:"SELLRECKONPL2"` // 结算盈亏(逐笔) - 根据持仓单日照表字段汇率
  2199. BUYCURTDPOSITION int32 `json:"buycurtdposition" xorm:"BUYCURTDPOSITION"` // 买期末今日头寸
  2200. BUYFRETDPOSITION int32 `json:"buyfretdposition" xorm:"BUYFRETDPOSITION"` // 买冻结今日头寸
  2201. SELLCURTDPOSITION int32 `json:"sellcurtdposition" xorm:"SELLCURTDPOSITION"` // 卖期末今日头寸
  2202. SELLFRETDPOSITION int32 `json:"sellfretdposition" xorm:"SELLFRETDPOSITION"` // 卖冻结今日头寸
  2203. INTERESTCHARGE float64 `json:"interestcharge" xorm:"INTERESTCHARGE"` // 利息(110)
  2204. SHORTMARGINVALUE float64 `json:"shortmarginvalue" xorm:"SHORTMARGINVALUE"` // 保证金设置值(投资管理系统-空)
  2205. BUYDEFERCHARGE float64 `json:"buydefercharge" xorm:"BUYDEFERCHARGE"` // 买递延费2(仓储费2)
  2206. SELLDEFERCHARGE float64 `json:"selldefercharge" xorm:"SELLDEFERCHARGE"` // 卖递延费2
  2207. RELATEDUSERID int64 `json:"relateduserid" xorm:"'RELATEDUSERID'"` // 关联用户id
  2208. MIDDLEGOODSID int32 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'"` // 套保商品id
  2209. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保商品代码
  2210. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保商品名称
  2211. NEEDHEDGERATIO float64 `json:"needhedgeratio" xorm:"'NEEDHEDGERATIO'"` // 应套保比例
  2212. NEEDARBITRAGERATIO float64 `json:"needarbitrageratio" xorm:"'NEEDARBITRAGERATIO'"` // 应套利比例
  2213. GOODSGROUPID int32 `json:"goodsgroupid" xorm:"'GOODSGROUPID'"` // 交易品种id
  2214. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 折算系数
  2215. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 交易商品代码
  2216. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 交易商品名称
  2217. GOODUNITID int32 `json:"goodunitid" xorm:"'GOODUNITID'"` // 交易商品单位id
  2218. AGREEUNIT float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约乘数
  2219. ENUMDICNAME string `json:"enumdicname"` // 单位名称(交易商品)
  2220. ACCOUNTNAME string `json:"accountname" xorm:"'ACCOUNTNAME'"` // 交易账户
  2221. DiffBuyQty float64 `json:"diffbuyqty"` // 交易品种今变化量(买)
  2222. DiffMgBuyQty float64 `json:"diffmgbuyqty"` // 套保品种今变化量(买)
  2223. DiffNeedMgBuyQty float64 `json:"diffneedmgbuyqty"` // 应套保总量变化量(买)
  2224. DiffSellQty float64 `json:"diffsellqty"` // 交易品种今变化量(卖)
  2225. DiffMgSellQty float64 `json:"diffmgsellqty"` // 套保品种今变化量(卖)
  2226. DiffNeedMgSellQty float64 `json:"diffneedmgsellqty"` // 应套保总量变化量(卖)
  2227. BeginDate string `json:"begindate"` // 开始交易日
  2228. EndDate string `json:"enddate"` // 结束交易日
  2229. }
  2230. func (r *ReckonDayPosition) calc() {
  2231. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.GOODUNITID)
  2232. if r.BUYPOSITIONQTY > 0 || r.BUYCURPOSITIONQTY > 0 {
  2233. r.DiffBuyQty = float64(r.BUYCURPOSITIONQTY) * r.AGREEUNIT
  2234. r.DiffMgBuyQty = float64(r.BUYCURPOSITIONQTY) * r.AGREEUNIT * r.CONVERTRATIO
  2235. r.DiffNeedMgBuyQty = float64(r.BUYCURPOSITIONQTY) * r.AGREEUNIT * r.CONVERTRATIO * r.NEEDHEDGERATIO
  2236. }
  2237. if r.SELLPOSITIONQTY > 0 || r.SELLCURPOSITIONQTY > 0 {
  2238. r.DiffBuyQty = float64(r.SELLCURPOSITIONQTY) * r.AGREEUNIT
  2239. r.DiffMgBuyQty = float64(r.SELLCURPOSITIONQTY) * r.AGREEUNIT * r.CONVERTRATIO
  2240. r.DiffNeedMgBuyQty = float64(r.SELLCURPOSITIONQTY) * r.AGREEUNIT * r.CONVERTRATIO * r.NEEDHEDGERATIO
  2241. }
  2242. }
  2243. func (r *ReckonDayPosition) buildSql() string {
  2244. var sqlId utils.SQLVal = "with tmp as" +
  2245. " (select t.middlegoodsid," +
  2246. " t.middlegoodscode," +
  2247. " t.middlegoodsname," +
  2248. " t.needhedgeratio," +
  2249. " t.needarbitrageratio," +
  2250. " t.goodsgroupid," +
  2251. " c.convertratio," +
  2252. " g.goodsid," +
  2253. " g.goodscode," +
  2254. " g.goodsname," +
  2255. " g.agreeunit," +
  2256. " g.goodunitid" +
  2257. " from erms_middlegoods t" +
  2258. " inner join ermcp_ggconvertconfig c" +
  2259. " on t.goodsgroupid = c.destgoodsgroupid" +
  2260. " inner join goods g" +
  2261. " on g.goodsgroupid = c.srcgoodsgroupid)" +
  2262. "SELECT t.ACCOUNTID," +
  2263. " t.GOODSID," +
  2264. " t.RECKONDATE," +
  2265. " t.BUYPOSITIONQTY," +
  2266. " t.BUYHOLDERAMOUNT," +
  2267. " t.BUYCURPOSITIONQTY," +
  2268. " t.BUYCURHOLDERAMOUNT," +
  2269. " t.BUYFROZENQTY," +
  2270. " t.BUYOTHERFROZENQTY," +
  2271. " t.BUYOPENREQQTY," +
  2272. " t.BUYRECKONPL," +
  2273. " t.BUYINTEREST," +
  2274. " t.BUYUSEDMARGIN," +
  2275. " t.BUYOPENTOTALQTY," +
  2276. " t.BUYCLOSETOTALQTY," +
  2277. " t.BUYCLOSETOTALPL," +
  2278. " t.SELLPOSITIONQTY," +
  2279. " t.SELLHOLDERAMOUNT," +
  2280. " t.SELLCURPOSITIONQTY," +
  2281. " t.SELLCURHOLDERAMOUNT," +
  2282. " t.SELLFROZENQTY," +
  2283. " t.SELLOTHERFROZENQTY," +
  2284. " t.SELLOPENREQQTY," +
  2285. " t.SELLRECKONPL," +
  2286. " t.SELLINTEREST," +
  2287. " t.SELLUSEDMARGIN," +
  2288. " t.SELLOPENTOTALQTY," +
  2289. " t.SELLCLOSETOTALQTY," +
  2290. " t.SELLCLOSETOTALPL," +
  2291. " t.TRADEPROPERTY," +
  2292. " t.MARGINALGORITHM," +
  2293. " t.MARGINVALUE," +
  2294. " t.BUYFEEALGORITHM," +
  2295. " t.BUYMEMBERFEEVALUE," +
  2296. " t.BUYEXCHAGEFEEVALUE," +
  2297. " t.SELLFEEALGORITHM," +
  2298. " t.SELLMEMBERFEEVALUE," +
  2299. " t.SELLEXCHAGEFEEVALUE," +
  2300. " t.INTEREST2," +
  2301. " t.ACCOUNTCURRENCYID," +
  2302. " t.GOODSCURRENCYID," +
  2303. " t.CUREXCHANGERATE," +
  2304. " t.BUYRECKONPL2," +
  2305. " t.SELLRECKONPL2," +
  2306. " t.BUYCURTDPOSITION," +
  2307. " t.BUYFRETDPOSITION," +
  2308. " t.SELLCURTDPOSITION," +
  2309. " t.SELLFRETDPOSITION," +
  2310. " t.INTERESTCHARGE," +
  2311. " t.SHORTMARGINVALUE," +
  2312. " t.BUYDEFERCHARGE," +
  2313. " t.SELLDEFERCHARGE," +
  2314. " ta.relateduserid," +
  2315. " ta.accountname," +
  2316. " tmp.*" +
  2317. " FROM RECKON_DAYPOSITION t" +
  2318. " INNER JOIN tmp" +
  2319. " on t.goodsid = tmp.goodsid" +
  2320. " INNER JOIN TAACCOUNT ta" +
  2321. " on t.accountid = ta.accountid" +
  2322. " WHERE 1 = 1" +
  2323. " and (t.buypositionqty > 0 or t.buycurpositionqty > 0 or t.SELLPOSITIONQTY > 0 or t.SELLCURPOSITIONQTY > 0)"
  2324. sqlId.And("ta.RELATEDUSERID", r.RELATEDUSERID)
  2325. sqlId.And("tmp.MIDDLEGOODSID", r.MIDDLEGOODSID)
  2326. sqlId.AndEx("t.RECKONDATE", r.RECKONDATE, len(r.RECKONDATE) > 0)
  2327. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  2328. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  2329. }
  2330. return sqlId.String()
  2331. }
  2332. // GetDataEx 获取持仓头寸日照
  2333. func (r *ReckonDayPosition) GetDataEx() ([]ReckonDayPosition, error) {
  2334. sData := make([]ReckonDayPosition, 0)
  2335. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2336. for i := range sData {
  2337. sData[i].calc()
  2338. }
  2339. return sData, err
  2340. }
  2341. // HISOUTTRADEPOSITION 外部头寸日照
  2342. type HISOUTTRADEPOSITION struct {
  2343. HISTRADEDATE string `json:"histradedate" xorm:"'HISTRADEDATE'"` // 日期
  2344. ISVALIDDATA int32 `json:"isvaliddata" xorm:"'ISVALIDDATA'"` // 是否有效
  2345. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID"` // 资金账号[外部母账户]
  2346. HEDGEGOODSID int32 `json:"hedgegoodsid" xorm:"HEDGEGOODSID"` // 对冲合约ID
  2347. HEDGEACCOUNTCODE string `json:"hedgeaccountcode" xorm:"HEDGEACCOUNTCODE"` // 对冲账号
  2348. TRADEDATE string `json:"tradedate" xorm:"TRADEDATE"` // 交易日(yyyyMMdd)
  2349. MARKETID int32 `json:"marketid" xorm:"MARKETID"` // 市场ID
  2350. YDBUYPOSITION int32 `json:"ydbuyposition" xorm:"YDBUYPOSITION"` // 期初买头寸
  2351. CURBUYPOSITION int32 `json:"curbuyposition" xorm:"CURBUYPOSITION"` // 期末买头寸
  2352. CURYDBUYPOSITION int32 `json:"curydbuyposition" xorm:"CURYDBUYPOSITION"` // 期末上日买头寸
  2353. CURTDBUYPOSITION int32 `json:"curtdbuyposition" xorm:"CURTDBUYPOSITION"` // 期末今日买头寸
  2354. FREYDBUYPOSITION int32 `json:"freydbuyposition" xorm:"FREYDBUYPOSITION"` // 冻结上日买头寸
  2355. FRETDBUYPOSITION int32 `json:"fretdbuyposition" xorm:"FRETDBUYPOSITION"` // 冻结今日买头寸
  2356. YDSELLPOSITION int32 `json:"ydsellposition" xorm:"YDSELLPOSITION"` // 期初卖头寸
  2357. CURSELLPOSITION int32 `json:"cursellposition" xorm:"CURSELLPOSITION"` // 期末卖头寸
  2358. CURYDSELLPOSITION int32 `json:"curydsellposition" xorm:"CURYDSELLPOSITION"` // 期末上日卖头寸
  2359. CURTDSELLPOSITION int32 `json:"curtdsellposition" xorm:"CURTDSELLPOSITION"` // 期末今日卖头寸
  2360. FREYDSELLPOSITION int32 `json:"freydsellposition" xorm:"FREYDSELLPOSITION"` // 冻结上日卖头寸
  2361. FRETDSELLPOSITION int32 `json:"fretdsellposition" xorm:"FRETDSELLPOSITION"` // 冻结今日卖头寸
  2362. BUYOPENCOST float64 `json:"buyopencost" xorm:"BUYOPENCOST"` // 买开仓成本
  2363. BUYPOSITIONCOST float64 `json:"buypositioncost" xorm:"BUYPOSITIONCOST"` // 买持仓成本
  2364. BUYUSEMARGIN float64 `json:"buyusemargin" xorm:"BUYUSEMARGIN"` // 买占用保证金
  2365. SELLOPENCOST float64 `json:"sellopencost" xorm:"SELLOPENCOST"` // 卖开仓成本
  2366. SELLPOSITIONCOST float64 `json:"sellpositioncost" xorm:"SELLPOSITIONCOST"` // 卖持仓成本
  2367. SELLUSEMARGIN float64 `json:"sellusemargin" xorm:"SELLUSEMARGIN"` // 卖占用保证金
  2368. RELATEDUSERID int64 `json:"relateduserid" xorm:"'RELATEDUSERID'"` // 关联用户id
  2369. MIDDLEGOODSID int32 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'"` // 套保商品id
  2370. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保商品代码
  2371. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保商品名称
  2372. NEEDHEDGERATIO float64 `json:"needhedgeratio" xorm:"'NEEDHEDGERATIO'"` // 应套保比例
  2373. NEEDARBITRAGERATIO float64 `json:"needarbitrageratio" xorm:"'NEEDARBITRAGERATIO'"` // 应套利比例
  2374. GOODSGROUPID int32 `json:"goodsgroupid" xorm:"'GOODSGROUPID'"` // 交易品种id
  2375. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 折算系数
  2376. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 交易商品代码
  2377. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 交易商品名称
  2378. GOODUNITID int32 `json:"goodunitid" xorm:"'GOODUNITID'"` // 交易商品单位id
  2379. AGREEUNIT float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约乘数
  2380. ENUMDICNAME string `json:"enumdicname"` // 单位名称(交易商品)
  2381. ACCOUNTNAME string `json:"accountname" xorm:"'ACCOUNTNAME'"` // 交易账户
  2382. DiffBuyQty float64 `json:"diffbuyqty"` // 交易品种今变化量(买)
  2383. DiffMgBuyQty float64 `json:"diffmgbuyqty"` // 套保品种今变化量(买)
  2384. DiffNeedMgBuyQty float64 `json:"diffneedmgbuyqty"` // 应套保总量变化量(买)
  2385. DiffSellQty float64 `json:"diffsellqty"` // 交易品种今变化量(卖)
  2386. DiffMgSellQty float64 `json:"diffmgsellqty"` // 套保品种今变化量(卖)
  2387. DiffNeedMgSellQty float64 `json:"diffneedmgsellqty"` // 应套保总量变化量(卖)
  2388. BeginDate string `json:"begindate"` // 开始交易日
  2389. EndDate string `json:"enddate"` // 结束交易日
  2390. }
  2391. func (r *HISOUTTRADEPOSITION) calc() {
  2392. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.GOODUNITID)
  2393. if r.YDBUYPOSITION > 0 || r.CURBUYPOSITION > 0 {
  2394. r.DiffBuyQty = float64(r.CURBUYPOSITION) * r.AGREEUNIT
  2395. r.DiffMgBuyQty = float64(r.CURBUYPOSITION) * r.AGREEUNIT * r.CONVERTRATIO
  2396. r.DiffNeedMgBuyQty = float64(r.CURBUYPOSITION) * r.AGREEUNIT * r.CONVERTRATIO * r.NEEDHEDGERATIO
  2397. }
  2398. if r.YDSELLPOSITION > 0 || r.CURSELLPOSITION > 0 {
  2399. r.DiffBuyQty = float64(r.CURSELLPOSITION) * r.AGREEUNIT
  2400. r.DiffMgBuyQty = float64(r.CURSELLPOSITION) * r.AGREEUNIT * r.CONVERTRATIO
  2401. r.DiffNeedMgBuyQty = float64(r.CURSELLPOSITION) * r.AGREEUNIT * r.CONVERTRATIO * r.NEEDHEDGERATIO
  2402. }
  2403. }
  2404. func (r *HISOUTTRADEPOSITION) buildSql() string {
  2405. var sqlId utils.SQLVal = "with tmp as" +
  2406. " (select t.middlegoodsid," +
  2407. " t.middlegoodscode," +
  2408. " t.middlegoodsname," +
  2409. " t.needhedgeratio," +
  2410. " t.needarbitrageratio," +
  2411. " t.goodsgroupid," +
  2412. " c.convertratio," +
  2413. " g.goodsid," +
  2414. " g.goodscode," +
  2415. " g.goodsname," +
  2416. " g.agreeunit," +
  2417. " g.goodunitid" +
  2418. " from erms_middlegoods t" +
  2419. " inner join ermcp_ggconvertconfig c" +
  2420. " on t.goodsgroupid = c.destgoodsgroupid" +
  2421. " inner join goods g" +
  2422. " on g.goodsgroupid = c.srcgoodsgroupid)" +
  2423. "SELECT t.HISTRADEDATE," +
  2424. " t.ISVALIDDATA," +
  2425. " t.ACCOUNTID," +
  2426. " t.HEDGEGOODSID," +
  2427. " t.HEDGEACCOUNTCODE," +
  2428. " t.TRADEDATE," +
  2429. " t.MARKETID," +
  2430. " t.YDBUYPOSITION," +
  2431. " t.CURBUYPOSITION," +
  2432. " t.CURYDBUYPOSITION," +
  2433. " t.CURTDBUYPOSITION," +
  2434. " t.FREYDBUYPOSITION," +
  2435. " t.FRETDBUYPOSITION," +
  2436. " t.YDSELLPOSITION," +
  2437. " t.CURSELLPOSITION," +
  2438. " t.CURYDSELLPOSITION," +
  2439. " t.CURTDSELLPOSITION," +
  2440. " t.FREYDSELLPOSITION," +
  2441. " t.FRETDSELLPOSITION," +
  2442. " t.BUYOPENCOST," +
  2443. " t.BUYPOSITIONCOST," +
  2444. " t.BUYUSEMARGIN," +
  2445. " t.SELLOPENCOST," +
  2446. " t.SELLPOSITIONCOST," +
  2447. " t.SELLUSEMARGIN," +
  2448. " ta.relateduserid," +
  2449. " ta.accountname," +
  2450. " tmp.*" +
  2451. " FROM HIS_HEDGE_OUTTRADEPOSITION t" +
  2452. " INNER JOIN tmp" +
  2453. " on t.HEDGEGOODSID = tmp.goodsid" +
  2454. " INNER JOIN TAACCOUNT ta" +
  2455. " on t.accountid = ta.accountid" +
  2456. " WHERE 1 = 1" +
  2457. " and (t.YDBUYPOSITION > 0 or t.CURBUYPOSITION > 0 or t.YDSELLPOSITION > 0 or t.CURSELLPOSITION > 0)"
  2458. sqlId.And("ta.RELATEDUSERID", r.RELATEDUSERID)
  2459. sqlId.And("tmp.MIDDLEGOODSID", r.MIDDLEGOODSID)
  2460. sqlId.AndEx("t.HISTRADEDATE", r.HISTRADEDATE, len(r.HISTRADEDATE) > 0)
  2461. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  2462. sqlId.Join(fmt.Sprintf(" and t.HISTRADEDATE >= '%v' and t.HISTRADEDATE <= '%v' ", r.BeginDate, r.EndDate))
  2463. }
  2464. return sqlId.String()
  2465. }
  2466. // GetDataEx 获取外部头寸日照
  2467. func (r *HISOUTTRADEPOSITION) GetDataEx() ([]HISOUTTRADEPOSITION, error) {
  2468. sData := make([]HISOUTTRADEPOSITION, 0)
  2469. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2470. for i := range sData {
  2471. sData[i].calc()
  2472. }
  2473. return sData, err
  2474. }
  2475. // Ermcp3ExposureFutuDetail 敞口报表期货明细
  2476. type Ermcp3ExposureFutuDetail struct {
  2477. ACCOUNTID int64 `json:"accountid"` // 交易账户id
  2478. ACCOUNTNAME string `json:"accountname"` // 交易账户
  2479. GOODSID int32 `json:"goodsid"` // 商品id
  2480. GOODSCODE string `json:"goodscode"` // 商品代码
  2481. GOODSNAME string `json:"goodsname"` // 商品名称
  2482. BUYORSELL int32 `json:"buyorsell"` // 方向 0-买 1-卖
  2483. ORIHOLDQTY float64 `json:"oriholdqty"` // 期初持仓量
  2484. HOLDQTY float64 `json:"holdqty"` // 期末持仓量
  2485. TODAYOPENQTY float64 `json:"todayopenqty"` // 今开仓量
  2486. TODAYCLOSEQTY float64 `json:"todaycloseqty"` // 今平仓量
  2487. DiffTradeQty float64 `json:"difftradeqty"` // 交易品种今变化量
  2488. DiffMgQty float64 `json:"diffmgqty"` // 套保品种今变化量
  2489. DiffNeedQty float64 `json:"diffneedqty"` // 应套保总量变化量
  2490. MIDDLEGOODSID int32 `json:"middlegoodsid"` // 套保商品id
  2491. MIDDLEGOODSCODE string `json:"middlegoodscode"` // 套保商品代码
  2492. MIDDLEGOODSNAME string `json:"middlegoodsname"` // 套保商品名称
  2493. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  2494. USERID int64 `json:"-"` // 用户id
  2495. RECKONDATE string `json:"tradedate"` // 交易日
  2496. BeginDate string `json:"begindate"` // 开始交易日
  2497. EndDate string `json:"enddate"` // 结束交易日
  2498. }
  2499. func (r *Ermcp3ExposureFutuDetail) fromOutPositionBuy(v *HISOUTTRADEPOSITION) {
  2500. r.ACCOUNTID = v.ACCOUNTID
  2501. r.ACCOUNTNAME = v.ACCOUNTNAME
  2502. r.GOODSID = v.HEDGEGOODSID
  2503. r.GOODSCODE = v.GOODSCODE
  2504. r.GOODSNAME = v.GOODSNAME
  2505. r.BUYORSELL = 0
  2506. r.ORIHOLDQTY = float64(v.YDBUYPOSITION)
  2507. r.HOLDQTY = float64(v.CURBUYPOSITION)
  2508. r.TODAYOPENQTY = 0
  2509. r.TODAYCLOSEQTY = 0
  2510. r.DiffTradeQty = v.DiffBuyQty
  2511. r.DiffMgQty = v.DiffMgBuyQty
  2512. r.DiffNeedQty = v.DiffNeedMgBuyQty
  2513. r.MIDDLEGOODSID = v.MIDDLEGOODSID
  2514. r.MIDDLEGOODSCODE = v.MIDDLEGOODSCODE
  2515. r.MIDDLEGOODSNAME = v.MIDDLEGOODSNAME
  2516. r.ENUMDICNAME = v.ENUMDICNAME
  2517. r.RECKONDATE = v.HISTRADEDATE
  2518. }
  2519. func (r *Ermcp3ExposureFutuDetail) fromOutPositionSell(v *HISOUTTRADEPOSITION) {
  2520. r.ACCOUNTID = v.ACCOUNTID
  2521. r.ACCOUNTNAME = v.ACCOUNTNAME
  2522. r.GOODSID = v.HEDGEGOODSID
  2523. r.GOODSCODE = v.GOODSCODE
  2524. r.GOODSNAME = v.GOODSNAME
  2525. r.BUYORSELL = 1
  2526. r.ORIHOLDQTY = float64(v.YDSELLPOSITION)
  2527. r.HOLDQTY = float64(v.CURSELLPOSITION)
  2528. r.TODAYOPENQTY = 0
  2529. r.TODAYCLOSEQTY = 0
  2530. r.DiffTradeQty = v.DiffSellQty
  2531. r.DiffMgQty = v.DiffMgSellQty
  2532. r.DiffNeedQty = v.DiffNeedMgSellQty
  2533. r.MIDDLEGOODSID = v.MIDDLEGOODSID
  2534. r.MIDDLEGOODSCODE = v.MIDDLEGOODSCODE
  2535. r.MIDDLEGOODSNAME = v.MIDDLEGOODSNAME
  2536. r.ENUMDICNAME = v.ENUMDICNAME
  2537. r.RECKONDATE = v.HISTRADEDATE
  2538. }
  2539. func (r *Ermcp3ExposureFutuDetail) fromTradePositionBuy(v *ReckonDayPosition) {
  2540. r.ACCOUNTID = v.ACCOUNTID
  2541. r.ACCOUNTNAME = v.ACCOUNTNAME
  2542. r.GOODSID = v.GOODSID
  2543. r.GOODSCODE = v.GOODSCODE
  2544. r.GOODSNAME = v.GOODSNAME
  2545. r.BUYORSELL = 0
  2546. r.ORIHOLDQTY = float64(v.BUYPOSITIONQTY)
  2547. r.HOLDQTY = float64(v.BUYCURPOSITIONQTY)
  2548. r.TODAYOPENQTY = 0
  2549. r.TODAYCLOSEQTY = 0
  2550. r.DiffTradeQty = v.DiffBuyQty
  2551. r.DiffMgQty = v.DiffMgBuyQty
  2552. r.DiffNeedQty = v.DiffNeedMgBuyQty
  2553. r.MIDDLEGOODSID = v.MIDDLEGOODSID
  2554. r.MIDDLEGOODSCODE = v.MIDDLEGOODSCODE
  2555. r.MIDDLEGOODSNAME = v.MIDDLEGOODSNAME
  2556. r.ENUMDICNAME = v.ENUMDICNAME
  2557. r.RECKONDATE = v.RECKONDATE
  2558. }
  2559. func (r *Ermcp3ExposureFutuDetail) fromTradePositionSell(v *ReckonDayPosition) {
  2560. r.ACCOUNTID = v.ACCOUNTID
  2561. r.ACCOUNTNAME = v.ACCOUNTNAME
  2562. r.GOODSID = v.GOODSID
  2563. r.GOODSCODE = v.GOODSCODE
  2564. r.GOODSNAME = v.GOODSNAME
  2565. r.BUYORSELL = 1
  2566. r.ORIHOLDQTY = float64(v.SELLPOSITIONQTY)
  2567. r.HOLDQTY = float64(v.SELLCURPOSITIONQTY)
  2568. r.TODAYOPENQTY = 0
  2569. r.TODAYCLOSEQTY = 0
  2570. r.DiffTradeQty = v.DiffSellQty
  2571. r.DiffMgQty = v.DiffMgSellQty
  2572. r.DiffNeedQty = v.DiffNeedMgSellQty
  2573. r.MIDDLEGOODSID = v.MIDDLEGOODSID
  2574. r.MIDDLEGOODSCODE = v.MIDDLEGOODSCODE
  2575. r.MIDDLEGOODSNAME = v.MIDDLEGOODSNAME
  2576. r.ENUMDICNAME = v.ENUMDICNAME
  2577. r.RECKONDATE = v.RECKONDATE
  2578. }
  2579. // GetDataEx 获取敞口报表期货明细
  2580. func (r *Ermcp3ExposureFutuDetail) GetDataEx() (interface{}, error) {
  2581. sData := make([]Ermcp3ExposureFutuDetail, 0)
  2582. if mtpcache.IsAreaUserId(r.USERID) {
  2583. // 查母账户头寸
  2584. m := HISOUTTRADEPOSITION{RELATEDUSERID: r.USERID, HISTRADEDATE: r.RECKONDATE,
  2585. BeginDate: r.BeginDate, EndDate: r.EndDate, MIDDLEGOODSID: r.MIDDLEGOODSID}
  2586. d, _ := m.GetDataEx()
  2587. for _, v := range d {
  2588. if v.YDBUYPOSITION > 0 || v.CURBUYPOSITION > 0 {
  2589. val := Ermcp3ExposureFutuDetail{}
  2590. val.fromOutPositionBuy(&v)
  2591. val.BeginDate = r.BeginDate
  2592. val.EndDate = r.EndDate
  2593. sData = append(sData, val)
  2594. }
  2595. if v.YDSELLPOSITION > 0 || v.CURSELLPOSITION > 0 {
  2596. val := Ermcp3ExposureFutuDetail{}
  2597. val.fromOutPositionSell(&v)
  2598. val.BeginDate = r.BeginDate
  2599. val.EndDate = r.EndDate
  2600. sData = append(sData, val)
  2601. }
  2602. }
  2603. } else {
  2604. // 查子账户头寸
  2605. m := ReckonDayPosition{RELATEDUSERID: r.USERID, RECKONDATE: r.RECKONDATE,
  2606. BeginDate: r.BeginDate, EndDate: r.EndDate, MIDDLEGOODSID: r.MIDDLEGOODSID}
  2607. d, _ := m.GetDataEx()
  2608. for _, v := range d {
  2609. if v.BUYPOSITIONQTY > 0 || v.BUYCURPOSITIONQTY > 0 {
  2610. val := Ermcp3ExposureFutuDetail{}
  2611. val.fromTradePositionBuy(&v)
  2612. val.BeginDate = r.BeginDate
  2613. val.EndDate = r.EndDate
  2614. sData = append(sData, val)
  2615. }
  2616. if v.SELLPOSITIONQTY > 0 || v.SELLCURPOSITIONQTY > 0 {
  2617. val := Ermcp3ExposureFutuDetail{}
  2618. val.fromTradePositionSell(&v)
  2619. val.BeginDate = r.BeginDate
  2620. val.EndDate = r.EndDate
  2621. sData = append(sData, val)
  2622. }
  2623. }
  2624. }
  2625. return sData, nil
  2626. }
  2627. // Ermcp3ExposureParamChLog 敞口报表参数调整明细
  2628. type Ermcp3ExposureParamChLog struct {
  2629. LOGID string `json:"logid" xorm:"LOGID"` // 流水ID(609+Unix秒时间戳(10位)+xxxxxx)
  2630. MIDDLEGOODSID int64 `json:"middlegoodsid" xorm:"MIDDLEGOODSID"` // 套保品种
  2631. AREAUSERID int64 `json:"areauserid" xorm:"AREAUSERID"` // 所属机构\交易用户ID
  2632. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID"` // 期货账户ID (作废, 默认为0)
  2633. CHANGELOGTYPE int32 `json:"changelogtype" xorm:"CHANGELOGTYPE"` // 变更流水类型 - 1:套保比率变更 2:套利比率变更
  2634. PARAMBEFOREVALUE float64 `json:"parambeforevalue" xorm:"PARAMBEFOREVALUE"` // 参数变更前(调整前参数值)
  2635. PARAMAFTERVALUE float64 `json:"paramaftervalue" xorm:"PARAMAFTERVALUE"` // 参数变更后(调整后参数值)
  2636. CURVALUE float64 `json:"curvalue" xorm:"CURVALUE"` // 当前套保\套利量(套保总量|套利总量)
  2637. BEFOREVALUE float64 `json:"beforevalue" xorm:"BEFOREVALUE"` // 应套保\套利量变更前(调整前应套保总量)
  2638. AFTERVALUE float64 `json:"aftervalue" xorm:"AFTERVALUE"` // 应套保\套利量变更后(调整后应套保总量)
  2639. LOGDATETIME string `json:"logdatetime" xorm:"LOGDATETIME"` // 流水日期
  2640. TRADEDATE string `json:"tradedate" xorm:"TRADEDATE"` // 交易日(yyyyMMdd)
  2641. MIDDLEGOODSCODE string `json:"middlegoodscode"` // 套保品种代码
  2642. MIDDLEGOODSNAME string `json:"middlegoodsname"` // 套保品种名称
  2643. DiffValue float64 `json:"diffvalue"` // 应套保总量变化量
  2644. BeginDate string `json:"begindate"` // 开始交易日
  2645. EndDate string `json:"enddate"` // 结束交易日
  2646. }
  2647. func (r *Ermcp3ExposureParamChLog) calc() {
  2648. r.DiffValue = r.AFTERVALUE - r.BEFOREVALUE
  2649. }
  2650. func (r *Ermcp3ExposureParamChLog) buildSql() string {
  2651. var sqlId utils.SQLVal = "SELECT t.LOGID," +
  2652. " t.MIDDLEGOODSID," +
  2653. " t.AREAUSERID," +
  2654. " t.ACCOUNTID," +
  2655. " t.CHANGELOGTYPE," +
  2656. " t.PARAMBEFOREVALUE," +
  2657. " t.PARAMAFTERVALUE," +
  2658. " t.CURVALUE," +
  2659. " t.BEFOREVALUE," +
  2660. " t.AFTERVALUE," +
  2661. " to_char(t.LOGDATETIME, 'yyyy-mm-dd hh24:mi:ss') LOGDATETIME," +
  2662. " t.TRADEDATE," +
  2663. " g.middlegoodscode," +
  2664. " g.middlegoodsname" +
  2665. " FROM ERMCP_EXPOSUREPARAMCHGLOG t" +
  2666. " LEFT JOIN ERMS_MIDDLEGOODS g on t.middlegoodsid=g.middlegoodsid" +
  2667. " WHERE 1 = 1"
  2668. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  2669. sqlId.AndEx("t.TRADEDATE", r.TRADEDATE, len(r.TRADEDATE) > 0)
  2670. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  2671. sqlId.Join(fmt.Sprintf(" and t.TRADEDATE >= '%v' and t.TRADEDATE <= '%v' ", r.BeginDate, r.EndDate))
  2672. }
  2673. return sqlId.String()
  2674. }
  2675. // GetDataEx 获取敞口报表参数调整明细
  2676. func (r *Ermcp3ExposureParamChLog) GetDataEx() (interface{}, error) {
  2677. sData := make([]Ermcp3ExposureParamChLog, 0)
  2678. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2679. for i := range sData {
  2680. sData[i].calc()
  2681. sData[i].BeginDate = r.BeginDate
  2682. sData[i].EndDate = r.EndDate
  2683. }
  2684. return sData, err
  2685. }