ermcp3Report.go 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  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. " FROM RECKON_ERMCP_AREAEXPOSURE t" +
  538. " LEFT JOIN ERMS_MIDDLEGOODS g" +
  539. " on t.middlegoodsid = g.middlegoodsid" +
  540. " WHERE 1 = 1"
  541. sqlId.AndEx("t.AREAUSERID", r.AREAUSERID, r.AREAUSERID > 0)
  542. if r.QueryType == 1 {
  543. sqlId.And("t.RECKONDATE", r.RECKONDATE)
  544. } else if r.QueryType == 2 {
  545. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  546. }
  547. return sqlId.String()
  548. }
  549. func (r *Ermcp3ExpourseReport) buildSqlCycle() string {
  550. var sqlId utils.SQLVal = "SELECT t.cycletime," +
  551. " t.cycletype," +
  552. " t.ORITOTALSPOTQTY," +
  553. " t.ORITOTALFUTUREQTY," +
  554. " t.ORITOTALEXPOSURE," +
  555. " t.ORINEEDHEDGEEXPOSOURE," +
  556. " t.ORIHEDGEQTY," +
  557. " t.ORIARBITRAGEQTY," +
  558. " t.ORINEEDHEDGEQTY," +
  559. " t.ORINEEDARBITRAGEQTY," +
  560. " t.ORITOTALNEEDHEDGEQTY," +
  561. " t.MIDDLEGOODSID," +
  562. " t.AREAUSERID," +
  563. " t.ACCOUNTID," +
  564. " t.ORIBUYPLANQTY," +
  565. " t.ORIBUYPRICEDQTY," +
  566. " t.ORISELLPLANQTY," +
  567. " t.ORISELLPRICEDQTY," +
  568. " t.ORIBUYFUTUREQTY," +
  569. " t.ORISELLFUTUREQTY," +
  570. " t.BUYPLANQTY," +
  571. " t.BUYPRICEDQTY," +
  572. " t.SELLPLANQTY," +
  573. " t.SELLPRICEDQTY," +
  574. " t.BUYFUTUREQTY," +
  575. " t.SELLFUTUREQTY," +
  576. " t.TOTALSPOTQTY," +
  577. " t.TOTALFUTUREQTY," +
  578. " t.TOTALEXPOSURE," +
  579. " t.TOTALHEDGERATIO," +
  580. " t.TOTALNEEDHEDGEQTY," +
  581. " t.NEEDHEDGEEXPOSOURE," +
  582. " t.NEEDHEDGERATIO," +
  583. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  584. " t.HEDGEQTY," +
  585. " t.ARBITRAGEQTY," +
  586. " t.NEEDHEDGEQTY," +
  587. " t.NEEDARBITRAGEQTY," +
  588. " g.middlegoodsname," +
  589. " g.middlegoodscode," +
  590. " g.goodsunitid unitid," +
  591. " g.needhedgeratio MGNEEDHEDGERATIO," +
  592. " g.needarbitrageratio" +
  593. " FROM REPORT_ERMCP_AREAEXPOSURE t" +
  594. " LEFT JOIN ERMS_MIDDLEGOODS g" +
  595. " on t.middlegoodsid = g.middlegoodsid" +
  596. " WHERE 1 = 1"
  597. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  598. sqlId.And("t.CYCLETYPE", r.CYCLETYPE)
  599. sqlId.And("t.CYCLETIME", r.CYCLETIME)
  600. return sqlId.String()
  601. }
  602. // GetDataEx 获取敞报表
  603. func (r *Ermcp3ExpourseReport) GetDataEx() (interface{}, error) {
  604. sData := make([]Ermcp3ExpourseReport, 0)
  605. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  606. for i := range sData {
  607. sData[i].calc()
  608. sData[i].BeginDate = r.BeginDate
  609. sData[i].EndDate = r.EndDate
  610. }
  611. return sData, err
  612. }
  613. // Ermcp3ExposureContractDetail 敞口合同明细
  614. type Ermcp3ExposureContractDetail struct {
  615. MIDDLEGOODSID int32 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'"` // 套保品种id
  616. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货商品id
  617. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 折算系数(套保品种)
  618. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构id
  619. SPOTCONTRACTID string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` // 合同id
  620. CONTRACTNO string `json:"contractno" xorm:"'CONTRACTNO'"` // 合同编号
  621. CONTRACTTYPE int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 现货合同类型 - 1:采购 -1:销售
  622. BIZTYPE int32 `json:"biztype" xorm:"'BIZTYPE'"` // 业务类型 1-套保 2-套利
  623. BUYUSERID int64 `json:"buyuserid" xorm:"'BUYUSERID'"` // 采购方id
  624. SELLUSERID int64 `json:"selluserid" xorm:"'SELLUSERID'"` // 销售方id
  625. QTY float64 `json:"qty" xorm:"'QTY'"` // 合同量
  626. PRICEDQTY float64 `json:"pricedqty" xorm:"'PRICEDQTY'"` // 已定价量
  627. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类id
  628. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  629. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  630. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 品类单位id
  631. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保品种名称
  632. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保品种代码
  633. GOODSUNITID int32 `json:"-" xorm:"'GOODSUNITID'"` // 套保品种单位id
  634. TRADEDATE string `json:"tradedate" xorm:"'tradedate'"` // 交易日
  635. NEEDHEDGERATIO float64 `json:"needhedgeratio" xorm:"'NEEDHEDGERATIO'"` // 应套保比例
  636. NEEDARBITRAGERATIO float64 `json:"needarbitrageratio" xorm:"'NEEDARBITRAGERATIO'"` // 应套利比例
  637. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  638. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货商品代码
  639. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货商品名称
  640. EnumdicName string `json:"enumdicname"` // 单位名称
  641. BUYUSERNAME string `json:"buyusername"` // 采购方名称
  642. SELLUSERNAME string `json:"sellusername"` // 销售方名称
  643. CurQty float64 `json:"curqty"` // 今定价量
  644. DiffQty float64 `json:"diffqty"` // 套保品种今变动量
  645. BeginDate string `json:"-"` // 开始交易日
  646. EndDate string `json:"-"` // 结束交易日
  647. }
  648. func (r *Ermcp3ExposureContractDetail) calc() {
  649. r.CurQty = r.QTY
  650. r.DiffQty = r.QTY * r.CONVERTRATIO
  651. r.EnumdicName = mtpcache.GetEnumDicitemName(r.UNITID)
  652. r.BUYUSERNAME = mtpcache.GetUserNameByUserId(r.BUYUSERID)
  653. r.SELLUSERNAME = mtpcache.GetUserNameByUserId(r.SELLUSERID)
  654. }
  655. func (r *Ermcp3ExposureContractDetail) buildSql() string {
  656. var sqlId utils.SQLVal = "select t.middlegoodsid," +
  657. " t.deliverygoodsid," +
  658. " t.convertratio," +
  659. " s.userid," +
  660. " to_char(s.spotcontractid) spotcontractid," +
  661. " s.contractno," +
  662. " s.contracttype," +
  663. " s.biztype," +
  664. " s.buyuserid," +
  665. " s.selluserid," +
  666. " s.qty," +
  667. " s.pricedqty," +
  668. " s.wrstandardid," +
  669. " s.audittradedate tradedate," +
  670. " w.wrstandardname," +
  671. " w.wrstandardcode," +
  672. " w.unitid," +
  673. " mg.middlegoodsname," +
  674. " mg.middlegoodscode," +
  675. " mg.goodsunitid," +
  676. " mg.needhedgeratio," +
  677. " mg.needarbitrageratio," +
  678. " dg.dgfactoryitemid brandid," +
  679. " dg.dgfactoryitemvalue brandName," +
  680. " g.deliverygoodscode," +
  681. " g.deliverygoodsname" +
  682. " from erms2_wrsconvertdetail t" +
  683. " inner join ermcp_spotcontract s" +
  684. " on t.deliverygoodsid = s.deliverygoodsid" +
  685. " left join erms_middlegoods mg" +
  686. " on t.middlegoodsid = mg.middlegoodsid" +
  687. " left join wrstandard w" +
  688. " on s.wrstandardid = w.wrstandardid" +
  689. " left join dgfactoryitem dg" +
  690. " on s.spotgoodsbrandid = dg.dgfactoryitemid" +
  691. " left join deliverygoods g on s.deliverygoodsid=g.deliverygoodsid" +
  692. " where t.wrstandardid = 0" +
  693. " and s.contractstatus in (2, 3)"
  694. sqlId.AndEx("s.audittradedate", r.TRADEDATE, len(r.TRADEDATE) > 0)
  695. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  696. sqlId.Join(fmt.Sprintf(" and s.audittradedate >= '%v' and s.audittradedate <= '%v'", r.BeginDate, r.EndDate))
  697. }
  698. sqlId.And("t.middlegoodsid", r.MIDDLEGOODSID)
  699. sqlId.Join(fmt.Sprintf(" and %v in(s.userid, s.tradeuserid)", r.USERID))
  700. return sqlId.String()
  701. }
  702. // GetDataEx 获取敞口合同明细
  703. func (r *Ermcp3ExposureContractDetail) GetDataEx() (interface{}, error) {
  704. sData := make([]Ermcp3ExposureContractDetail, 0)
  705. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  706. for i := range sData {
  707. sData[i].calc()
  708. }
  709. return sData, err
  710. }
  711. // Ermcp3ExposureHedgeplanDetail 敞口套保计划明细
  712. type Ermcp3ExposureHedgeplanDetail struct {
  713. MIDDLEGOODSID int32 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'"` // 套保品种id
  714. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货商品id
  715. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 折算系数(套保品种)
  716. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构id
  717. HEDGEPLANID string `json:"hedgeplanid" xorm:"'HEDGEPLANID'"` // 套保计划id
  718. HEDGEPLANNO string `json:"hedgeplanno" xorm:"'HEDGEPLANNO'"` // 套保计划编号
  719. BIZTYPE int32 `json:"biztype" xorm:"'BIZTYPE'"` // 业务类型(期现用途,业务类型就是期现用途) 1-套保 2-套利
  720. QTY float64 `json:"qty" xorm:"'QTY'"` // 合同量
  721. PRICEDQTY float64 `json:"pricedqty" xorm:"'PRICEDQTY'"` // 已定价量
  722. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类id
  723. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  724. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  725. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 品类单位id
  726. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保品种名称
  727. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保品种代码
  728. GOODSUNITID int32 `json:"-" xorm:"'GOODSUNITID'"` // 套保品种单位id
  729. TRADEDATE string `json:"tradedate" xorm:"'tradedate'"` // 交易日(登记日)
  730. NEEDHEDGERATIO float64 `json:"needhedgeratio" xorm:"'NEEDHEDGERATIO'"` // 应套保比例
  731. NEEDARBITRAGERATIO float64 `json:"needarbitrageratio" xorm:"'NEEDARBITRAGERATIO'"` // 应套利比例
  732. CONTRACTTYPE int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 计划类型 - 1:采购 -1:销售
  733. HEDGEPLANSTATUS int32 `json:"hedgeplanstatus" xorm:"'HEDGEPLANSTATUS'"` // 套保计划状态 - 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
  734. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货商品代码
  735. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货商吕名称
  736. DGUNITID int32 `json:"dgunitid" xorm:"'DGUNITID'"` // 现货商品单位id
  737. EnumdicName string `json:"enumdicname"` // 单位名称(品类)
  738. MGUNITIDNAME string `json:"mgunitidname"` // 单位名称(套保商品)
  739. DGUNITIDNAME string `json:"dgunitidname"` // 单位名称(现货商品)
  740. CurQty float64 `json:"-"` // 今定价量
  741. DiffQty float64 `json:"diffqty"` // 套保品种今变动量
  742. ChangQty float64 `json:"changqty"` // 应套保总量变化量
  743. BeginDate string `json:"-"`
  744. EndDate string `json:"-"`
  745. }
  746. func (r *Ermcp3ExposureHedgeplanDetail) calc() {
  747. r.EnumdicName = mtpcache.GetEnumDicitemName(r.UNITID)
  748. r.MGUNITIDNAME = mtpcache.GetEnumDicitemName(r.GOODSUNITID)
  749. r.DGUNITIDNAME = mtpcache.GetEnumDicitemName(r.DGUNITID)
  750. r.CurQty = r.QTY
  751. r.DiffQty = r.QTY * r.CONVERTRATIO
  752. r.ChangQty = r.QTY * r.NEEDHEDGERATIO * r.CONVERTRATIO
  753. }
  754. func (r *Ermcp3ExposureHedgeplanDetail) buildSql() string {
  755. var sqlId utils.SQLVal = "select t.middlegoodsid," +
  756. " t.deliverygoodsid," +
  757. " t.convertratio," +
  758. " s.areauserid userid," +
  759. " to_char(s.hedgeplanid) hedgeplanid," +
  760. " s.hedgeplanno," +
  761. " s.contracttype," +
  762. " s.biztype," +
  763. " s.planqty qty," +
  764. " s.wrstandardid," +
  765. " s.audittradedate tradedate," +
  766. " s.hedgeplanstatus," +
  767. " w.wrstandardname," +
  768. " w.wrstandardcode," +
  769. " w.unitid," +
  770. " g.deliverygoodscode," +
  771. " g.deliverygoodsname," +
  772. " g.goodsunitid," +
  773. " mg.middlegoodsname," +
  774. " mg.middlegoodscode," +
  775. " mg.goodsunitid," +
  776. " mg.needhedgeratio," +
  777. " mg.needarbitrageratio" +
  778. " from erms2_wrsconvertdetail t" +
  779. " inner join ermcp_hedgeplan s" +
  780. " on t.deliverygoodsid = s.deliverygoodsid" +
  781. " left join erms_middlegoods mg" +
  782. " on t.middlegoodsid = mg.middlegoodsid" +
  783. " left join wrstandard w" +
  784. " on s.wrstandardid = w.wrstandardid" +
  785. " left join deliverygoods g on s.deliverygoodsid=g.deliverygoodsid" +
  786. " where t.wrstandardid = 0" +
  787. " and s.hedgeplanstatus in (2, 3)"
  788. sqlId.AndEx("s.audittradedate", r.TRADEDATE, len(r.TRADEDATE) > 0)
  789. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  790. sqlId.Join(fmt.Sprintf(" and s.audittradedate >= '%v' and s.audittradedate <= '%v'", r.BeginDate, r.EndDate))
  791. }
  792. sqlId.And("t.middlegoodsid", r.MIDDLEGOODSID)
  793. sqlId.Join(fmt.Sprintf(" and %v in(s.areauserid, s.tradeuserid)", r.USERID))
  794. return sqlId.String()
  795. }
  796. // GetDataEx 获取敞口套保计划明细
  797. func (r *Ermcp3ExposureHedgeplanDetail) GetDataEx() (interface{}, error) {
  798. sData := make([]Ermcp3ExposureHedgeplanDetail, 0)
  799. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  800. for i := range sData {
  801. sData[i].calc()
  802. }
  803. return sData, err
  804. }
  805. // Ermcp3AreaSpotPLReport 现货报表(日/月/周/季/年)
  806. type Ermcp3AreaSpotPLReport struct {
  807. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照时期(yyyyMMdd)
  808. CYCLETIME string `json:"cycletime" xorm:"'CYCLETIME'"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  809. CYCLETYPE int32 `json:"cycletype" xorm:"'CYCLETYPE'"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  810. AREAUSERID int64 `json:"areauserid" xorm:"AREAUSERID"` // 所属机构\交易用户ID
  811. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID"` // 期货账户ID (作废, 默认为0)
  812. WRFACTORTYPEID int64 `json:"wrfactortypeid" xorm:"WRFACTORTYPEID"` // 仓单要素类型ID(212+Unix秒时间戳(10位)+xxxxxx)
  813. CURRENCYID int32 `json:"currencyid" xorm:"CURRENCYID"` // 结算币种ID【原值】
  814. BIZTYPE int32 `json:"biztype" xorm:"BIZTYPE"` // 业务类型 - 1:套保 2:套利
  815. DELIVERYGOODSID int64 `json:"deliverygoodsid" xorm:"DELIVERYGOODSID"` // 现货品种ID
  816. WRSTANDARDID int64 `json:"wrstandardid" xorm:"WRSTANDARDID"` // 现货品类ID
  817. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"SPOTGOODSBRANDID"` // 现货品牌ID
  818. ORIBUYQTY float64 `json:"oribuyqty" xorm:"ORIBUYQTY"` // 期初采购总量
  819. ORIBUYAMOUNT float64 `json:"oribuyamount" xorm:"ORIBUYAMOUNT"` // 期初采购总额
  820. ORISELLQTY float64 `json:"orisellqty" xorm:"ORISELLQTY"` // 期初销售总量
  821. ORISELLAMOUNT float64 `json:"orisellamount" xorm:"ORISELLAMOUNT"` // 期初销售总额
  822. ORIQTY float64 `json:"oriqty" xorm:"ORIQTY"` // 期初量
  823. ORIAVERAGEPRICE float64 `json:"oriaverageprice" xorm:"ORIAVERAGEPRICE"` // 期初均价
  824. ORIAMOUNT float64 `json:"oriamount" xorm:"ORIAMOUNT"` // 期初额
  825. TODAYBUYQTY float64 `json:"todaybuyqty" xorm:"TODAYBUYQTY"` // 今日采购量(今采购量)
  826. TODAYBUYAMOUNT float64 `json:"todaybuyamount" xorm:"TODAYBUYAMOUNT"` // 今日采购额(今采购额)
  827. TODAYBUYAVERAGEPRICE float64 `json:"todaybuyaverageprice" xorm:"TODAYBUYAVERAGEPRICE"` // 今日采购均价
  828. TODAYSELLQTY float64 `json:"todaysellqty" xorm:"TODAYSELLQTY"` // 今日销售量(今销售量)
  829. TODAYSELLAMOUNT float64 `json:"todaysellamount" xorm:"TODAYSELLAMOUNT"` // 今日销售额(今销售额)
  830. TODAYSELLAVERAGEPRICE float64 `json:"todaysellaverageprice" xorm:"TODAYSELLAVERAGEPRICE"` // 今日销售均价
  831. CURBUYQTY float64 `json:"curbuyqty" xorm:"CURBUYQTY"` // 期末采购总量
  832. CURBUYAMOUNT float64 `json:"curbuyamount" xorm:"CURBUYAMOUNT"` // 期末采购总额
  833. CURSELLQTY float64 `json:"cursellqty" xorm:"CURSELLQTY"` // 期末销售总量
  834. CURSELLAMOUNT float64 `json:"cursellamount" xorm:"CURSELLAMOUNT"` // 期末销售总额
  835. CURQTY float64 `json:"curqty" xorm:"CURQTY"` // 期末量
  836. CURAVERAGEPRICE float64 `json:"curaverageprice" xorm:"CURAVERAGEPRICE"` // 期末均价
  837. CURAMOUNT float64 `json:"curamount" xorm:"CURAMOUNT"` // 期末额
  838. CURSPOTPRICE float64 `json:"curspotprice" xorm:"CURSPOTPRICE"` // 参考市价(最新价)
  839. CURMARKETVALUE float64 `json:"curmarketvalue" xorm:"CURMARKETVALUE"` // 参考市值(期末市值)
  840. ACTUALPL float64 `json:"actualpl" xorm:"ACTUALPL"` // 现货损益
  841. FLOATPL float64 `json:"floatpl" xorm:"FLOATPL"` // 浮动损益
  842. UPDATETIME string `json:"updatetime" xorm:"UPDATETIME"` // 更新时间
  843. TODAYINQTY float64 `json:"todayinqty" xorm:"TODAYINQTY"` // 今日入库量(今入库量)
  844. TODAYOUTQTY float64 `json:"todayoutqty" xorm:"TODAYOUTQTY"` // 今日出库量(今出库量)
  845. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货商品代码
  846. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货商品名称
  847. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODSUNITID'"` // 现货商品单位id
  848. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  849. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  850. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 品类单位id
  851. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  852. EnumdicName string `json:"enumdicname"` // 现货商品单位名称
  853. UNITIDNAME string `json:"unitidname"` // 品类单位名称
  854. ACCOUNTNAME string `json:"accountname"` // 机构名称
  855. CURRENCYNAME string `json:"currencyname"` // 币种名称
  856. QueryType int32 `json:"-"` // 查询类型 1-报表 2-报表明细
  857. BeginDate string `json:"begindate"` // 开始交易日
  858. EndDate string `json:"enddate"` // 结束交易日
  859. }
  860. func (r *Ermcp3AreaSpotPLReport) calc() {
  861. r.EnumdicName = mtpcache.GetEnumDicitemName(r.GOODSUNITID)
  862. r.UNITIDNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  863. r.ACCOUNTNAME = mtpcache.GetUserNameByUserId(r.AREAUSERID)
  864. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  865. }
  866. func (r *Ermcp3AreaSpotPLReport) buildSql() string {
  867. // 日报表
  868. if r.QueryType == 1 && r.CYCLETYPE == 0 {
  869. return r.buildSqlDay()
  870. }
  871. // 日报表明细
  872. if r.QueryType == 2 && r.CYCLETYPE == 0 {
  873. return r.buildSqlDayDetail()
  874. }
  875. return r.buildSqlCycle()
  876. }
  877. // buildSqlDay 现货日报表查询语句
  878. func (r *Ermcp3AreaSpotPLReport) buildSqlDay() string {
  879. var sqlId utils.SQLVal = "select a.*," +
  880. " g.deliverygoodscode," +
  881. " g.deliverygoodsname," +
  882. " g.goodsunitid," +
  883. " w.wrstandardcode," +
  884. " w.wrstandardname," +
  885. " w.unitid" +
  886. " from (SELECT t.RECKONDATE," +
  887. " t.AREAUSERID," +
  888. " t.CURRENCYID," +
  889. " t.DELIVERYGOODSID," +
  890. " t.WRSTANDARDID," +
  891. " sum(t.ORIBUYQTY) ORIBUYQTY," +
  892. " sum(t.ORIBUYAMOUNT) ORIBUYAMOUNT," +
  893. " sum(t.ORISELLQTY) ORISELLQTY," +
  894. " sum(t.ORISELLAMOUNT) ORISELLAMOUNT," +
  895. " sum(t.ORIQTY) ORIQTY," +
  896. " sum(t.ORIAVERAGEPRICE) ORIAVERAGEPRICE," +
  897. " sum(t.ORIAMOUNT) ORIAMOUNT," +
  898. " sum(t.TODAYBUYQTY) TODAYBUYQTY," +
  899. " sum(t.TODAYBUYAMOUNT) TODAYBUYAMOUNT," +
  900. " sum(t.TODAYBUYAVERAGEPRICE) TODAYBUYAVERAGEPRICE," +
  901. " sum(t.TODAYSELLQTY) TODAYSELLQTY," +
  902. " sum(t.TODAYSELLAMOUNT) TODAYSELLAMOUNT," +
  903. " sum(t.TODAYSELLAVERAGEPRICE) TODAYSELLAVERAGEPRICE," +
  904. " sum(t.CURBUYQTY) CURBUYQTY," +
  905. " sum(t.CURBUYAMOUNT) CURBUYAMOUNT," +
  906. " sum(t.CURSELLQTY) CURSELLQTY," +
  907. " sum(t.CURSELLAMOUNT) CURSELLAMOUNT," +
  908. " sum(t.CURQTY) CURQTY," +
  909. " sum(t.CURAVERAGEPRICE) CURAVERAGEPRICE," +
  910. " sum(t.CURAMOUNT) CURAMOUNT," +
  911. " sum(t.CURSPOTPRICE) CURSPOTPRICE," +
  912. " sum(t.CURMARKETVALUE) CURMARKETVALUE," +
  913. " sum(t.ACTUALPL) ACTUALPL," +
  914. " sum(t.FLOATPL) FLOATPL," +
  915. " sum(t.TODAYINQTY) TODAYINQTY," +
  916. " sum(t.TODAYOUTQTY) TODAYOUTQTY" +
  917. " FROM RECKON_ERMCP_AREASPOTPL t" +
  918. " WHERE 1=1 %v" +
  919. " GROUP BY t.RECKONDATE," +
  920. " t.AREAUSERID," +
  921. " t.CURRENCYID," +
  922. " t.DELIVERYGOODSID," +
  923. " t.WRSTANDARDID) a" +
  924. " LEFT JOIN DELIVERYGOODS g" +
  925. " on a.deliverygoodsid = g.deliverygoodsid" +
  926. " LEFT JOIN WRSTANDARD w" +
  927. " on a.wrstandardid = w.wrstandardid"
  928. var sqlParam utils.SQLVal
  929. sqlParam.And("t.AREAUSERID", r.AREAUSERID)
  930. if r.QueryType == 1 {
  931. sqlParam.And("t.RECKONDATE", r.RECKONDATE)
  932. } else if r.QueryType == 2 {
  933. sqlParam.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  934. }
  935. sqlId.FormatParam(sqlParam.String())
  936. return sqlId.String()
  937. }
  938. func (r *Ermcp3AreaSpotPLReport) buildSqlDayDetail() string {
  939. var sqlId utils.SQLVal = "SELECT t.RECKONDATE," +
  940. " t.AREAUSERID," +
  941. " t.ACCOUNTID," +
  942. " t.WRFACTORTYPEID," +
  943. " t.CURRENCYID," +
  944. " t.BIZTYPE," +
  945. " t.DELIVERYGOODSID," +
  946. " t.WRSTANDARDID," +
  947. " t.SPOTGOODSBRANDID," +
  948. " t.ORIBUYQTY," +
  949. " t.ORIBUYAMOUNT," +
  950. " t.ORISELLQTY," +
  951. " t.ORISELLAMOUNT," +
  952. " t.ORIQTY," +
  953. " t.ORIAVERAGEPRICE," +
  954. " t.ORIAMOUNT," +
  955. " t.TODAYBUYQTY," +
  956. " t.TODAYBUYAMOUNT," +
  957. " t.TODAYBUYAVERAGEPRICE," +
  958. " t.TODAYSELLQTY," +
  959. " t.TODAYSELLAMOUNT," +
  960. " t.TODAYSELLAVERAGEPRICE," +
  961. " t.CURBUYQTY," +
  962. " t.CURBUYAMOUNT," +
  963. " t.CURSELLQTY," +
  964. " t.CURSELLAMOUNT," +
  965. " t.CURQTY," +
  966. " t.CURAVERAGEPRICE," +
  967. " t.CURAMOUNT," +
  968. " t.CURSPOTPRICE," +
  969. " t.CURMARKETVALUE," +
  970. " t.ACTUALPL," +
  971. " t.FLOATPL," +
  972. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  973. " t.TODAYINQTY," +
  974. " t.TODAYOUTQTY," +
  975. " g.deliverygoodscode," +
  976. " g.deliverygoodsname," +
  977. " g.goodsunitid," +
  978. " w.wrstandardcode," +
  979. " w.wrstandardname," +
  980. " w.unitid," +
  981. " dg.dgfactoryitemvalue brandName" +
  982. " FROM RECKON_ERMCP_AREASPOTPL t" +
  983. " LEFT JOIN DELIVERYGOODS g on t.deliverygoodsid=g.deliverygoodsid" +
  984. " LEFT JOIN WRSTANDARD w on t.wrstandardid = w.wrstandardid" +
  985. " LEFT JOIN DGFACTORYITEM dg on t.spotgoodsbrandid=dg.dgfactoryitemid" +
  986. " WHERE 1 = 1"
  987. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  988. sqlId.AndEx("t.RECKONDATE", r.RECKONDATE, len(r.RECKONDATE) > 0)
  989. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  990. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  991. }
  992. sqlId.And("t.CURRENCYID", r.CURRENCYID)
  993. sqlId.And("t.DELIVERYGOODSID", r.DELIVERYGOODSID)
  994. sqlId.And("t.WRSTANDARDID", r.WRSTANDARDID)
  995. return sqlId.String()
  996. }
  997. // buildSqlCycle 周期报表查询语句
  998. func (r *Ermcp3AreaSpotPLReport) buildSqlCycle() string {
  999. var sqlId utils.SQLVal = "select a.*," +
  1000. " g.deliverygoodscode," +
  1001. " g.deliverygoodsname," +
  1002. " g.goodsunitid," +
  1003. " w.wrstandardcode," +
  1004. " w.wrstandardname," +
  1005. " w.unitid" +
  1006. " from (SELECT t.cycletype," +
  1007. " t.cycletime," +
  1008. " t.AREAUSERID," +
  1009. " t.CURRENCYID," +
  1010. " t.DELIVERYGOODSID," +
  1011. " t.WRSTANDARDID," +
  1012. " sum(t.ORIBUYQTY) ORIBUYQTY," +
  1013. " sum(t.ORIBUYAMOUNT) ORIBUYAMOUNT," +
  1014. " sum(t.ORISELLQTY) ORISELLQTY," +
  1015. " sum(t.ORISELLAMOUNT) ORISELLAMOUNT," +
  1016. " sum(t.ORIQTY) ORIQTY," +
  1017. " sum(t.ORIAVERAGEPRICE) ORIAVERAGEPRICE," +
  1018. " sum(t.ORIAMOUNT) ORIAMOUNT," +
  1019. " sum(t.TODAYBUYQTY) TODAYBUYQTY," +
  1020. " sum(t.TODAYBUYAMOUNT) TODAYBUYAMOUNT," +
  1021. " sum(t.TODAYBUYAVERAGEPRICE) TODAYBUYAVERAGEPRICE," +
  1022. " sum(t.TODAYSELLQTY) TODAYSELLQTY," +
  1023. " sum(t.TODAYSELLAMOUNT) TODAYSELLAMOUNT," +
  1024. " sum(t.TODAYSELLAVERAGEPRICE) TODAYSELLAVERAGEPRICE," +
  1025. " sum(t.CURBUYQTY) CURBUYQTY," +
  1026. " sum(t.CURBUYAMOUNT) CURBUYAMOUNT," +
  1027. " sum(t.CURSELLQTY) CURSELLQTY," +
  1028. " sum(t.CURSELLAMOUNT) CURSELLAMOUNT," +
  1029. " sum(t.CURQTY) CURQTY," +
  1030. " sum(t.CURAVERAGEPRICE) CURAVERAGEPRICE," +
  1031. " sum(t.CURAMOUNT) CURAMOUNT," +
  1032. " sum(t.CURSPOTPRICE) CURSPOTPRICE," +
  1033. " sum(t.CURMARKETVALUE) CURMARKETVALUE," +
  1034. " sum(t.ACTUALPL) ACTUALPL," +
  1035. " sum(t.FLOATPL) FLOATPL," +
  1036. " sum(t.TODAYINQTY) TODAYINQTY," +
  1037. " sum(t.TODAYOUTQTY) TODAYOUTQTY" +
  1038. " FROM REPORT_ERMCP_AREASPOTPL t" +
  1039. " WHERE 1=1 %v" +
  1040. " GROUP BY t.AREAUSERID," +
  1041. " t.cycletype," +
  1042. " t.cycletime," +
  1043. " t.CURRENCYID," +
  1044. " t.DELIVERYGOODSID," +
  1045. " t.WRSTANDARDID) a" +
  1046. " LEFT JOIN DELIVERYGOODS g" +
  1047. " on a.deliverygoodsid = g.deliverygoodsid" +
  1048. " LEFT JOIN WRSTANDARD w" +
  1049. " on a.wrstandardid = w.wrstandardid"
  1050. var sqlParam utils.SQLVal
  1051. sqlParam.And("t.AREAUSERID", r.AREAUSERID)
  1052. sqlParam.And("t.Cycletype", r.CYCLETYPE)
  1053. sqlParam.And("t.CYCLETIME", r.CYCLETIME)
  1054. sqlId.FormatParam(sqlParam.String())
  1055. return sqlId.String()
  1056. }
  1057. // GetDataEx 获取现货报表(日/月/周/季/年)
  1058. func (r *Ermcp3AreaSpotPLReport) GetDataEx() (interface{}, error) {
  1059. sData := make([]Ermcp3AreaSpotPLReport, 0)
  1060. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1061. for i := range sData {
  1062. sData[i].calc()
  1063. sData[i].BeginDate = r.BeginDate
  1064. sData[i].EndDate = r.EndDate
  1065. }
  1066. return sData, err
  1067. }
  1068. // Ermcp3FinanceReport 财务报表
  1069. type Ermcp3FinanceReport struct {
  1070. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照时期(yyyyMMdd)
  1071. CYCLETIME string `json:"cycletime" xorm:"CYCLETIME"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  1072. CYCLETYPE int32 `json:"cycletype" xorm:"CYCLETYPE"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  1073. SELLPREINVOICEDAMOUNT float64 `json:"sellpreinvoicedamount" xorm:"SELLPREINVOICEDAMOUNT"` // 销售预开票额(预开票额)
  1074. SELLUNINVOICEDAMOUNT float64 `json:"selluninvoicedamount" xorm:"SELLUNINVOICEDAMOUNT"` // 销售应开票额(应开票额)
  1075. TODAYRECEIVESUM float64 `json:"todayreceivesum" xorm:"TODAYRECEIVESUM"` // 今收款合计
  1076. TODAYPAYSUM float64 `json:"todaypaysum" xorm:"TODAYPAYSUM"` // 今付款合计
  1077. UPDATETIME string `json:"updatetime" xorm:"UPDATETIME"` // 更新时间
  1078. AREAUSERID int64 `json:"areauserid" xorm:"AREAUSERID"` // 所属机构\交易用户ID
  1079. CURRENCYID int32 `json:"currencyid" xorm:"CURRENCYID"` // 结算币种ID
  1080. BIZTYPE int32 `json:"biztype" xorm:"BIZTYPE"` // 业务类型 - 1:套保 2:套利
  1081. BUYTODAYSETTLEAMOUNT float64 `json:"buytodaysettleamount" xorm:"BUYTODAYSETTLEAMOUNT"` // 采购今付款额(今付货款额)
  1082. SELLTODAYSETTLEAMOUNT float64 `json:"selltodaysettleamount" xorm:"SELLTODAYSETTLEAMOUNT"` // 销售今收款额(今收货款额)
  1083. BUYTODAYREFUNDAMOUNT float64 `json:"buytodayrefundamount" xorm:"BUYTODAYREFUNDAMOUNT"` // 采购今收退款额(今收退款额)
  1084. SELLTODAYREFUNDAMOUNT float64 `json:"selltodayrefundamount" xorm:"SELLTODAYREFUNDAMOUNT"` // 销售今付退款额(今付退款额)
  1085. SELLTODAYINVOICEAMOUNT float64 `json:"selltodayinvoiceamount" xorm:"SELLTODAYINVOICEAMOUNT"` // 销售今开票额(今开票额)
  1086. BUYTODAYINVOICEAMOUNT float64 `json:"buytodayinvoiceamount" xorm:"BUYTODAYINVOICEAMOUNT"` // 采购今收票额(今收票额)
  1087. BUYPREPAIDAMOUNT float64 `json:"buyprepaidamount" xorm:"BUYPREPAIDAMOUNT"` // 采购预付款额(预付货款额)
  1088. BUYUNPAIDAMOUNT float64 `json:"buyunpaidamount" xorm:"BUYUNPAIDAMOUNT"` // 采购应付款额(应付货款额)
  1089. BUYPREINVOICEDAMOUNT float64 `json:"buypreinvoicedamount" xorm:"BUYPREINVOICEDAMOUNT"` // 采购预收票额(预收票额)
  1090. BUYUNINVOICEDAMOUNT float64 `json:"buyuninvoicedamount" xorm:"BUYUNINVOICEDAMOUNT"` // 采购应收票额(应收票额)
  1091. SELLPREPAIDAMOUNT float64 `json:"sellprepaidamount" xorm:"SELLPREPAIDAMOUNT"` // 销售预收款额(预收货款额)
  1092. SELLUNPAIDAMOUNT float64 `json:"sellunpaidamount" xorm:"SELLUNPAIDAMOUNT"` // 销售应收款额(应收货款额)
  1093. WRFACTORTYPEID int64 `json:"wrfactortypeid" xorm:"WRFACTORTYPEID"` // 仓单要素类型ID(212+Unix秒时间戳(10位)+xxxxxx)
  1094. DELIVERYGOODSID int64 `json:"deliverygoodsid" xorm:"DELIVERYGOODSID"` // 现货品种ID
  1095. WRSTANDARDID int64 `json:"wrstandardid" xorm:"WRSTANDARDID"` // 现货商品ID
  1096. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"SPOTGOODSBRANDID"` // 现货品牌ID
  1097. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  1098. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  1099. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1100. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1101. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1102. ACCOUNTNAME string `json:"accountname"` // 机构名称
  1103. CURRENCYNAME string `json:"currencyname"` // 币种名称
  1104. QueryType int32 `json:"-"` // 查询类型 1-报表 2-报表明细
  1105. BeginDate string `json:"begindate"` // 开始交易日
  1106. EndDate string `json:"enddate"` // 结束交易日
  1107. }
  1108. func (r *Ermcp3FinanceReport) calc() {
  1109. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  1110. r.ACCOUNTNAME = mtpcache.GetUserNameByUserId(r.AREAUSERID)
  1111. }
  1112. func (r *Ermcp3FinanceReport) buildSql() string {
  1113. // 日报表
  1114. if r.QueryType == 1 && r.CYCLETYPE == 0 {
  1115. return r.buildSqlDay()
  1116. }
  1117. // 日报表明细
  1118. if r.QueryType == 2 && r.CYCLETYPE == 0 {
  1119. return r.buildSqlDayDetail()
  1120. }
  1121. return r.buildSqlCycle()
  1122. }
  1123. // buildSqlDay 财务日报表查询语句
  1124. func (r *Ermcp3FinanceReport) buildSqlDay() string {
  1125. var sqlId utils.SQLVal = "SELECT t.SELLPREINVOICEDAMOUNT," +
  1126. " t.SELLUNINVOICEDAMOUNT," +
  1127. " t.TODAYRECEIVESUM," +
  1128. " t.TODAYPAYSUM," +
  1129. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1130. " t.RECKONDATE," +
  1131. " t.AREAUSERID," +
  1132. " t.CURRENCYID," +
  1133. " t.BIZTYPE," +
  1134. " t.BUYTODAYSETTLEAMOUNT," +
  1135. " t.BUYTODAYREFUNDAMOUNT," +
  1136. " t.BUYPREPAIDAMOUNT," +
  1137. " t.BUYUNPAIDAMOUNT," +
  1138. " t.BUYTODAYINVOICEAMOUNT," +
  1139. " t.BUYPREINVOICEDAMOUNT," +
  1140. " t.BUYUNINVOICEDAMOUNT," +
  1141. " t.SELLTODAYSETTLEAMOUNT," +
  1142. " t.SELLTODAYREFUNDAMOUNT," +
  1143. " t.SELLPREPAIDAMOUNT," +
  1144. " t.SELLUNPAIDAMOUNT," +
  1145. " t.SELLTODAYINVOICEAMOUNT" +
  1146. " FROM RECKON_ERMCP_AREAFINANCE t" +
  1147. " WHERE 1 = 1"
  1148. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  1149. if r.QueryType == 1 {
  1150. sqlId.And("t.RECKONDATE", r.RECKONDATE)
  1151. } else if r.QueryType == 2 {
  1152. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1153. }
  1154. return sqlId.String()
  1155. }
  1156. // buildSqlDayDetail 财务日报表明细查询语句
  1157. func (r *Ermcp3FinanceReport) buildSqlDayDetail() string {
  1158. var sqlId utils.SQLVal = "SELECT t.RECKONDATE," +
  1159. " t.AREAUSERID," +
  1160. " t.CURRENCYID," +
  1161. " t.BIZTYPE," +
  1162. " t.WRFACTORTYPEID," +
  1163. " t.DELIVERYGOODSID," +
  1164. " t.WRSTANDARDID," +
  1165. " t.SPOTGOODSBRANDID," +
  1166. " t.BUYTODAYSETTLEAMOUNT," +
  1167. " t.BUYTODAYREFUNDAMOUNT," +
  1168. " t.BUYPREPAIDAMOUNT," +
  1169. " t.BUYUNPAIDAMOUNT," +
  1170. " t.BUYTODAYINVOICEAMOUNT," +
  1171. " t.BUYPREINVOICEDAMOUNT," +
  1172. " t.BUYUNINVOICEDAMOUNT," +
  1173. " t.SELLTODAYSETTLEAMOUNT," +
  1174. " t.SELLTODAYREFUNDAMOUNT," +
  1175. " t.SELLPREPAIDAMOUNT," +
  1176. " t.SELLUNPAIDAMOUNT," +
  1177. " t.SELLTODAYINVOICEAMOUNT," +
  1178. " t.SELLPREINVOICEDAMOUNT," +
  1179. " t.SELLUNINVOICEDAMOUNT," +
  1180. " t.TODAYRECEIVESUM," +
  1181. " t.TODAYPAYSUM," +
  1182. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1183. " g.deliverygoodscode," +
  1184. " g.deliverygoodsname," +
  1185. " w.wrstandardcode," +
  1186. " w.wrstandardname," +
  1187. " dg.dgfactoryitemvalue brandName" +
  1188. " FROM RECKON_ERMCP_AFINANCESUB t" +
  1189. " LEFT JOIN DELIVERYGOODS g on t.deliverygoodsid=g.deliverygoodsid" +
  1190. " LEFT JOIN WRSTANDARD w on t.wrstandardid=w.wrstandardid" +
  1191. " LEFT JOIN DGFACTORYITEM dg on t.spotgoodsbrandid=dg.dgfactoryitemid" +
  1192. " WHERE 1 = 1"
  1193. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  1194. sqlId.AndEx("t.RECKONDATE", r.RECKONDATE, len(r.RECKONDATE) > 0)
  1195. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  1196. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1197. }
  1198. sqlId.And("t.CURRENCYID", r.CURRENCYID)
  1199. return sqlId.String()
  1200. }
  1201. // buildSqlCycle 财务周期报表查询语句
  1202. func (r *Ermcp3FinanceReport) buildSqlCycle() string {
  1203. var sqlId utils.SQLVal = "SELECT t.SELLPREINVOICEDAMOUNT," +
  1204. " t.SELLUNINVOICEDAMOUNT," +
  1205. " t.TODAYRECEIVESUM," +
  1206. " t.TODAYPAYSUM," +
  1207. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1208. " t.CYCLETIME," +
  1209. " t.CYCLETYPE," +
  1210. " t.AREAUSERID," +
  1211. " t.CURRENCYID," +
  1212. " t.BIZTYPE," +
  1213. " t.BUYTODAYSETTLEAMOUNT," +
  1214. " t.BUYTODAYREFUNDAMOUNT," +
  1215. " t.BUYPREPAIDAMOUNT," +
  1216. " t.BUYUNPAIDAMOUNT," +
  1217. " t.BUYTODAYINVOICEAMOUNT," +
  1218. " t.BUYPREINVOICEDAMOUNT," +
  1219. " t.BUYUNINVOICEDAMOUNT," +
  1220. " t.SELLTODAYSETTLEAMOUNT," +
  1221. " t.SELLTODAYREFUNDAMOUNT," +
  1222. " t.SELLPREPAIDAMOUNT," +
  1223. " t.SELLUNPAIDAMOUNT," +
  1224. " t.SELLTODAYINVOICEAMOUNT" +
  1225. " FROM REPORT_ERMCP_AREAFINANCE t" +
  1226. " WHERE 1 = 1"
  1227. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  1228. sqlId.And("t.CYCLETYPE", r.CYCLETYPE)
  1229. sqlId.And("t.CYCLETIME", r.CYCLETIME)
  1230. return sqlId.String()
  1231. }
  1232. // GetDataEx 获取财务报表
  1233. func (r *Ermcp3FinanceReport) GetDataEx() (interface{}, error) {
  1234. sData := make([]Ermcp3FinanceReport, 0)
  1235. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1236. for i := range sData {
  1237. sData[i].calc()
  1238. sData[i].BeginDate = r.BeginDate
  1239. sData[i].EndDate = r.EndDate
  1240. }
  1241. return sData, err
  1242. }
  1243. // Ermcp3AreaStockReportEx 库存报表
  1244. type Ermcp3AreaStockReportEx struct {
  1245. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照时期(yyyyMMdd)
  1246. CYCLETIME string `json:"cycletime" xorm:"CYCLETIME"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  1247. CYCLETYPE int32 `json:"cycletype" xorm:"CYCLETYPE"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  1248. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构ID
  1249. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类ID
  1250. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID
  1251. WAREHOUSEINFOID string `json:"warehouseinfoid" xorm:"'WAREHOUSEINFOID'"` // 仓库ID
  1252. ORISTOCK float64 `json:"oristock" xorm:"'ORISTOCK'"` // 期初库存量
  1253. CURSTOCK float64 `json:"curstock" xorm:"'CURSTOCK'"` // 期末库存量
  1254. TODAYBUYINQTY float64 `json:"todaybuyinqty" xorm:"'TODAYBUYINQTY'"` // 今采购入库量
  1255. TODAYPRODUCEINQTY float64 `json:"todayproduceinqty" xorm:"'TODAYPRODUCEINQTY'"` // 今内部入库量
  1256. TODAYSELLOUTQTY float64 `json:"todayselloutqty" xorm:"'TODAYSELLOUTQTY'"` // 今销售出库量
  1257. TODAYPRODUCEOUTQTY float64 `json:"todayproduceoutqty" xorm:"'TODAYPRODUCEOUTQTY'"` // 今内部出库量
  1258. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  1259. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1260. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1261. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 品类单位id
  1262. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1263. WAREHOUSENAME string `json:"warehousename" xorm:"'WAREHOUSENAME'"` // 仓库名称
  1264. WAREHOUSECODE string `json:"warehousecode" xorm:"'WAREHOUSECODE'"` // 仓库代码
  1265. WAREHOUSETYPE int32 `json:"warehousetype" xorm:"'WAREHOUSETYPE'"` // 仓库类型 - 1 厂库 2 自有库 3 合作库
  1266. USERNAME string `json:"username"` // 机构名称
  1267. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  1268. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种id
  1269. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  1270. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  1271. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODSUNITID'"` // 现货单位id
  1272. UNBUYINQTY float64 `json:"unbuyinqty"` // 采购未入库量(数据库未找到相关字段?)
  1273. UNSELLOUTQTY float64 `json:"unselloutqty"` // 销售未出库量(数据库未找到相关字段?)
  1274. DiffQty float64 `json:"diffqty"` // 库存变化量 = 期末 - 期初
  1275. BeginDate string `json:"begindate"` // 开始交易日
  1276. EndDate string `json:"enddate"` // 结束交易日
  1277. SumFields string `json:"-"` // 汇总维度(逗号隔开) 1-品种 2-品类 3-品牌 4-仓库
  1278. }
  1279. func (r *Ermcp3AreaStockReportEx) calc() {
  1280. r.USERNAME = mtpcache.GetUserNameByUserId(r.USERID)
  1281. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  1282. r.DiffQty = r.CURSTOCK - r.ORISTOCK
  1283. }
  1284. // getSumField 获取汇总维度字段
  1285. func (r *Ermcp3AreaStockReportEx) getSumField() (fields []string, bSum bool) {
  1286. fields = append(fields, "userid")
  1287. if r.CYCLETYPE == 0 {
  1288. // 日报表
  1289. fields = append(fields, "reckondate")
  1290. } else {
  1291. // 周期报表
  1292. fields = append(fields, "cycletype")
  1293. fields = append(fields, "cycletime")
  1294. }
  1295. baseField := map[string]string{
  1296. "1": "deliverygoodsid",
  1297. "2": "wrstandardid",
  1298. "3": "spotgoodsbrandid",
  1299. "4": "warehouseinfoid",
  1300. }
  1301. bSum = false
  1302. param := []string{}
  1303. if len(r.SumFields) > 0 {
  1304. param = strings.Split(r.SumFields, ",")
  1305. }
  1306. if len(param) == 0 {
  1307. // 全字段, 不需要汇总
  1308. for _, v := range baseField {
  1309. fields = append(fields, v)
  1310. }
  1311. bSum = false
  1312. } else {
  1313. bIncludeAll := true
  1314. // 判断是否包含所有维度
  1315. for k := range baseField {
  1316. bExist := false
  1317. for _, p := range param {
  1318. if k == p {
  1319. bExist = true
  1320. break
  1321. }
  1322. }
  1323. if !bExist {
  1324. bIncludeAll = false
  1325. break
  1326. }
  1327. }
  1328. if bIncludeAll {
  1329. // 包含了所有维度, 不需要汇总
  1330. for _, v := range baseField {
  1331. fields = append(fields, v)
  1332. }
  1333. bSum = false
  1334. } else {
  1335. tmp := map[string]string{}
  1336. for _, p := range param {
  1337. if v, ok := baseField[p]; ok {
  1338. // 利用map特性去重, 先保存在一个临时map中
  1339. tmp[p] = v
  1340. bSum = true
  1341. }
  1342. }
  1343. if bSum {
  1344. for _, v := range tmp {
  1345. fields = append(fields, v)
  1346. }
  1347. }
  1348. }
  1349. }
  1350. return
  1351. }
  1352. func (r *Ermcp3AreaStockReportEx) getExFieldInfo() (strFiled string, strleftJoin string) {
  1353. if len(r.SumFields) > 0 {
  1354. param := strings.Split(r.SumFields, ",")
  1355. fHas := func(v string) bool {
  1356. for i := range param {
  1357. if param[i] == v {
  1358. return true
  1359. }
  1360. }
  1361. return false
  1362. }
  1363. if fHas("1") {
  1364. if len(strFiled) > 0 {
  1365. strFiled += ","
  1366. }
  1367. strFiled += "g.deliverygoodscode, g.deliverygoodsname, g.goodsunitid"
  1368. strleftJoin += " LEFT JOIN deliverygoods g on a.deliverygoodsid = g.deliverygoodsid"
  1369. }
  1370. if fHas("2") {
  1371. if len(strFiled) > 0 {
  1372. strFiled += ","
  1373. }
  1374. strFiled += "w.wrstandardname, w.wrstandardcode, w.unitid"
  1375. strleftJoin += " LEFT JOIN WRSTANDARD w on a.wrstandardid = w.wrstandardid"
  1376. }
  1377. if fHas("3") {
  1378. if len(strFiled) > 0 {
  1379. strFiled += ","
  1380. }
  1381. strFiled += "gb.dgfactoryitemvalue brandname"
  1382. strleftJoin += " LEFT JOIN dgfactoryitem gb on a.spotgoodsbrandid = gb.dgfactoryitemid"
  1383. }
  1384. if fHas("4") {
  1385. if len(strFiled) > 0 {
  1386. strFiled += ","
  1387. }
  1388. strFiled += "h.warehousecode, h.warehousename, h.warehousetype"
  1389. strleftJoin += " LEFT JOIN WAREHOUSEINFO h on a.warehouseinfoid=h.autoid"
  1390. }
  1391. }
  1392. return
  1393. }
  1394. func (r *Ermcp3AreaStockReportEx) buildSql() (sqlId string) {
  1395. fields, bSum := r.getSumField()
  1396. if r.CYCLETYPE == 0 {
  1397. // 日报表
  1398. if bSum {
  1399. sqlId = r.buildSqlReckonSum(fields)
  1400. } else {
  1401. sqlId = r.buildSqlReckon()
  1402. }
  1403. } else {
  1404. // 周期报表
  1405. if bSum {
  1406. sqlId = r.buildSqlReportSum(fields)
  1407. } else {
  1408. sqlId = r.buildSqlReport()
  1409. }
  1410. }
  1411. return
  1412. }
  1413. // GetDataEx 获取库存报表
  1414. func (r *Ermcp3AreaStockReportEx) GetDataEx() (interface{}, error) {
  1415. sData := make([]Ermcp3AreaStockReportEx, 0)
  1416. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1417. for i := range sData {
  1418. sData[i].calc()
  1419. sData[i].BeginDate = r.BeginDate
  1420. sData[i].EndDate = r.EndDate
  1421. }
  1422. return sData, err
  1423. }
  1424. // buildSqlReckon 日照表
  1425. func (r *Ermcp3AreaStockReportEx) buildSqlReckon() string {
  1426. var sqlId utils.SQLVal = "SELECT t.TODAYBUYINQTY," +
  1427. " t.TODAYPRODUCEINQTY," +
  1428. " t.reckondate," +
  1429. " t.TODAYSELLOUTQTY," +
  1430. " t.TODAYPRODUCEOUTQTY," +
  1431. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1432. " t.USERID," +
  1433. " t.WRSTANDARDID," +
  1434. " t.SPOTGOODSBRANDID," +
  1435. " t.WAREHOUSEINFOID," +
  1436. " t.deliverygoodsid," +
  1437. " t.ORISTOCK," +
  1438. " t.CURSTOCK," +
  1439. " w.wrstandardname," +
  1440. " w.wrstandardcode," +
  1441. " w.unitid," +
  1442. " gb.dgfactoryitemvalue brandname," +
  1443. " h.warehousename," +
  1444. " h.warehousecode," +
  1445. " h.warehousetype," +
  1446. " g.deliverygoodscode," +
  1447. " g.deliverygoodsname," +
  1448. " g.goodsunitid" +
  1449. " FROM RECKON_ERMCP_AREASTOCK t" +
  1450. " LEFT JOIN WRSTANDARD w" +
  1451. " on t.wrstandardid = w.wrstandardid" +
  1452. " LEFT JOIN dgfactoryitem gb" +
  1453. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1454. " LEFT JOIN deliverygoods g" +
  1455. " on t.deliverygoodsid = g.deliverygoodsid" +
  1456. " LEFT JOIN WAREHOUSEINFO h" +
  1457. " on t.warehouseinfoid = h.autoid" +
  1458. " WHERE 1 = 1"
  1459. sqlId.And("t.USERID", r.USERID)
  1460. sqlId.AndEx("t.RECKONDATE", r.RECKONDATE, len(r.RECKONDATE) > 0)
  1461. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  1462. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1463. }
  1464. // 现货商品id
  1465. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  1466. // 品类id
  1467. sqlId.AndEx("t.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  1468. // 品牌id
  1469. sqlId.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  1470. // 仓库id
  1471. sqlId.AndEx("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID, len(r.WAREHOUSEINFOID) > 0)
  1472. sqlId.Join(" order by t.reckondate")
  1473. return sqlId.String()
  1474. }
  1475. // buildSqlReckonSum 日照表(汇总维度)
  1476. func (r *Ermcp3AreaStockReportEx) buildSqlReckonSum(fields []string) string {
  1477. var sqlId utils.SQLVal = "SELECT a.*, %v" +
  1478. " from (SELECT %v," +
  1479. " sum(t.ORISTOCK) ORISTOCK," +
  1480. " sum(t.CURSTOCK) CURSTOCK," +
  1481. " sum(t.TODAYBUYINQTY) TODAYBUYINQTY," +
  1482. " sum(t.TODAYPRODUCEINQTY) TODAYPRODUCEINQTY," +
  1483. " sum(t.TODAYSELLOUTQTY) TODAYSELLOUTQTY," +
  1484. " sum(t.TODAYPRODUCEOUTQTY) TODAYPRODUCEOUTQTY" +
  1485. " FROM RECKON_ERMCP_AREASTOCK t" +
  1486. " WHERE 1 = 1 %v" +
  1487. " GROUP BY %v) a %v"
  1488. var sqlParam utils.SQLVal
  1489. sqlParam.And("t.USERID", r.USERID)
  1490. sqlParam.AndEx("t.RECKONDATE", r.RECKONDATE, len(r.RECKONDATE) > 0)
  1491. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  1492. sqlParam.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1493. }
  1494. // 现货商品id
  1495. sqlParam.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  1496. // 品类id
  1497. sqlParam.AndEx("t.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  1498. // 品牌id
  1499. sqlParam.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  1500. // 仓库id
  1501. sqlId.AndEx("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID, len(r.WAREHOUSEINFOID) > 0)
  1502. var strFileds string
  1503. for _, v := range fields {
  1504. if len(strFileds) > 0 {
  1505. strFileds += ","
  1506. }
  1507. strFileds += "t." + v
  1508. }
  1509. exFields, exLeftJoinTable := r.getExFieldInfo()
  1510. sqlId.FormatParam(exFields, strFileds, sqlParam.String(), strFileds, exLeftJoinTable)
  1511. sqlId.Join(" order by a.reckondate")
  1512. return sqlId.String()
  1513. }
  1514. // buildSqlReport 报表表
  1515. func (r *Ermcp3AreaStockReportEx) buildSqlReport() string {
  1516. var sqlId utils.SQLVal = "SELECT t.TODAYBUYINQTY," +
  1517. " t.TODAYPRODUCEINQTY," +
  1518. " t.cycletype," +
  1519. " t.cycletime," +
  1520. " t.USERID," +
  1521. " t.WRSTANDARDID," +
  1522. " t.SPOTGOODSBRANDID," +
  1523. " t.WAREHOUSEINFOID," +
  1524. " t.deliverygoodsid," +
  1525. " t.TODAYSELLOUTQTY," +
  1526. " t.TODAYPRODUCEOUTQTY," +
  1527. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1528. " t.ORISTOCK," +
  1529. " t.CURSTOCK," +
  1530. " w.wrstandardname," +
  1531. " w.wrstandardcode," +
  1532. " w.unitid," +
  1533. " gb.dgfactoryitemvalue brandname," +
  1534. " h.warehousename," +
  1535. " h.warehousecode," +
  1536. " h.warehousetype," +
  1537. " g.deliverygoodscode," +
  1538. " g.deliverygoodsname," +
  1539. " g.goodsunitid" +
  1540. " FROM RECKON_ERMCP_AREASTOCK t" +
  1541. " LEFT JOIN WRSTANDARD w" +
  1542. " on t.wrstandardid = w.wrstandardid" +
  1543. " LEFT JOIN dgfactoryitem gb" +
  1544. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1545. " LEFT JOIN deliverygoods g" +
  1546. " on t.deliverygoodsid = g.deliverygoodsid" +
  1547. " LEFT JOIN WAREHOUSEINFO h" +
  1548. " on t.warehouseinfoid = h.autoid" +
  1549. " WHERE 1 = 1"
  1550. sqlId.And("t.USERID", r.USERID)
  1551. sqlId.And("t.cycletype", r.CYCLETYPE)
  1552. sqlId.And("t.cycletime", r.CYCLETIME)
  1553. // 现货商品id
  1554. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  1555. // 品类id
  1556. sqlId.AndEx("t.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  1557. // 品牌id
  1558. sqlId.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  1559. // 仓库id
  1560. sqlId.AndEx("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID, len(r.WAREHOUSEINFOID) > 0)
  1561. return sqlId.String()
  1562. }
  1563. // buildSqlReport 报表表(汇总维度)
  1564. func (r *Ermcp3AreaStockReportEx) buildSqlReportSum(fields []string) string {
  1565. var sqlId utils.SQLVal = "SELECT a.*, %v" +
  1566. " from (SELECT %v," +
  1567. " sum(t.ORISTOCK) ORISTOCK," +
  1568. " sum(t.CURSTOCK) CURSTOCK," +
  1569. " sum(t.TODAYBUYINQTY) TODAYBUYINQTY," +
  1570. " sum(t.TODAYPRODUCEINQTY) TODAYPRODUCEINQTY," +
  1571. " sum(t.TODAYSELLOUTQTY) TODAYSELLOUTQTY," +
  1572. " sum(t.TODAYPRODUCEOUTQTY) TODAYPRODUCEOUTQTY" +
  1573. " FROM REPORT_ERMCP_AREASTOCK t" +
  1574. " WHERE 1 = 1 %v" +
  1575. " GROUP BY %v) a %v"
  1576. var sqlParam utils.SQLVal
  1577. sqlParam.And("t.USERID", r.USERID)
  1578. sqlParam.And("t.cycletype", r.CYCLETYPE)
  1579. sqlParam.And("t.cycletime", r.CYCLETIME)
  1580. // 现货商品id
  1581. sqlParam.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  1582. // 品类id
  1583. sqlParam.AndEx("t.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  1584. // 品牌id
  1585. sqlParam.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  1586. // 仓库id
  1587. sqlId.AndEx("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID, len(r.WAREHOUSEINFOID) > 0)
  1588. var strFileds string
  1589. for _, v := range fields {
  1590. if len(strFileds) > 0 {
  1591. strFileds += ","
  1592. }
  1593. strFileds += "t." + v
  1594. }
  1595. exFields, exLeftJoinTable := r.getExFieldInfo()
  1596. sqlId.FormatParam(exFields, strFileds, sqlParam.String(), strFileds, exLeftJoinTable)
  1597. return sqlId.String()
  1598. }
  1599. // Ermcp3ArealSumPL 损益汇总表
  1600. type Ermcp3ArealSumPL struct {
  1601. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照时期(yyyyMMdd)
  1602. CYCLETIME string `json:"cycletime" xorm:"'CYCLETIME'"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  1603. CYCLETYPE int32 `json:"cycletype" xorm:"'CYCLETYPE'"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  1604. AREAUSERID int64 `json:"areauserid" xorm:"AREAUSERID"` // 所属机构\交易用户ID
  1605. MIDDLEGOODSID int64 `json:"middlegoodsid" xorm:"MIDDLEGOODSID"` // 套保品种ID(SEQ_ERMS_MIDDLEGOODS)
  1606. CURRENCYID int32 `json:"currencyid" xorm:"CURRENCYID"` // 结算币种ID
  1607. SPOTACTUALPL float64 `json:"spotactualpl" xorm:"SPOTACTUALPL"` // 现货损益
  1608. SPOTFLOATPL float64 `json:"spotfloatpl" xorm:"SPOTFLOATPL"` // 现货浮动损益
  1609. FUTUREACTUALPL float64 `json:"futureactualpl" xorm:"FUTUREACTUALPL"` // 期货损益
  1610. FUTUREFLOATPL float64 `json:"futurefloatpl" xorm:"FUTUREFLOATPL"` // 期货浮动损益
  1611. SUMACTUALPL float64 `json:"sumactualpl" xorm:"SUMACTUALPL"` // 实际损益 = (ActualPL + FutureActualPL)
  1612. SUMPL float64 `json:"sumpl" xorm:"'SUMPL'"` // 汇总损益
  1613. UPDATETIME string `json:"updatetime" xorm:"UPDATETIME"` // 更新时间
  1614. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保品种名称
  1615. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保品种代码
  1616. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODSUNITID'"` // 套保品种单位id
  1617. ACCOUNTNAME string `json:"accountname"` // 机构名称
  1618. ENUMDICNAME string `json:"enumdicname"` // 单位名称(套保品种)
  1619. CURRENCYNAME string `json:"currencyname"` // 币种名称
  1620. QueryType int32 `json:"-"` // 查询类型 1-报表 2-报表明细
  1621. BeginDate string `json:"begindate"` // 开始交易日
  1622. EndDate string `json:"enddate"` // 结束交易日
  1623. }
  1624. func (r *Ermcp3ArealSumPL) calc() {
  1625. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  1626. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.GOODSUNITID)
  1627. r.ACCOUNTNAME = mtpcache.GetUserNameByUserId(r.AREAUSERID)
  1628. }
  1629. func (r *Ermcp3ArealSumPL) buildSql() string {
  1630. // 日报表
  1631. if r.QueryType == 1 && r.CYCLETYPE == 0 {
  1632. return r.buildSqlDay()
  1633. }
  1634. // 日报表明细
  1635. if r.QueryType == 2 && r.CYCLETYPE == 0 {
  1636. return r.buildSqlDayDetail()
  1637. }
  1638. return r.buildSqlCycle()
  1639. }
  1640. // buildSqlDay 损益汇总日报表(需要汇总维度)
  1641. func (r *Ermcp3ArealSumPL) buildSqlDay() string {
  1642. var sqlId utils.SQLVal = "SELECT t.RECKONDATE," +
  1643. " t.AREAUSERID," +
  1644. " t.CURRENCYID," +
  1645. " sum(t.SPOTACTUALPL) SPOTACTUALPL," +
  1646. " sum(t.SPOTFLOATPL) SPOTFLOATPL," +
  1647. " sum(t.FUTUREACTUALPL) FUTUREACTUALPL," +
  1648. " sum(t.FUTUREFLOATPL) FUTUREFLOATPL," +
  1649. " sum(t.SUMACTUALPL) SUMACTUALPL," +
  1650. " sum(t.SUMPL) SUMPL" +
  1651. " FROM RECKON_ERMCP_AREASUMPL t" +
  1652. " WHERE 1 = 1"
  1653. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  1654. if r.QueryType == 1 {
  1655. sqlId.And("t.RECKONDATE", r.RECKONDATE)
  1656. } else if r.QueryType == 2 {
  1657. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1658. }
  1659. sqlId.Join(" GROUP BY t.RECKONDATE, t.AREAUSERID, t.CURRENCYID")
  1660. return sqlId.String()
  1661. }
  1662. func (r *Ermcp3ArealSumPL) buildSqlDayDetail() string {
  1663. var sqlId utils.SQLVal = "SELECT t.RECKONDATE," +
  1664. " t.AREAUSERID," +
  1665. " t.MIDDLEGOODSID," +
  1666. " t.CURRENCYID," +
  1667. " t.SPOTACTUALPL," +
  1668. " t.SPOTFLOATPL," +
  1669. " t.FUTUREACTUALPL," +
  1670. " t.FUTUREFLOATPL," +
  1671. " t.SUMACTUALPL," +
  1672. " t.SUMPL," +
  1673. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1674. " g.middlegoodsname," +
  1675. " g.middlegoodscode," +
  1676. " g.goodsunitid" +
  1677. " FROM RECKON_ERMCP_AREASUMPL t" +
  1678. " LEFT JOIN ERMS_MIDDLEGOODS g on t.middlegoodsid=g.middlegoodsid" +
  1679. " WHERE 1 = 1"
  1680. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  1681. sqlId.AndEx("t.RECKONDATE", r.RECKONDATE, len(r.RECKONDATE) > 0)
  1682. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  1683. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1684. }
  1685. sqlId.And("t.CURRENCYID", r.CURRENCYID)
  1686. return sqlId.String()
  1687. }
  1688. func (r *Ermcp3ArealSumPL) buildSqlCycle() string {
  1689. var sqlId utils.SQLVal = "SELECT t.AREAUSERID," +
  1690. " t.CYCLETIME," +
  1691. " t.CYCLETYPE," +
  1692. " t.CURRENCYID," +
  1693. " sum(t.SPOTACTUALPL) SPOTACTUALPL," +
  1694. " sum(t.SPOTFLOATPL) SPOTFLOATPL," +
  1695. " sum(t.FUTUREACTUALPL) FUTUREACTUALPL," +
  1696. " sum(t.FUTUREFLOATPL) FUTUREFLOATPL," +
  1697. " sum(t.SUMACTUALPL) SUMACTUALPL," +
  1698. " sum(t.SUMPL) SUMPL" +
  1699. " FROM REPORT_ERMCP_AREASUMPL t" +
  1700. " WHERE 1 = 1"
  1701. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  1702. sqlId.And("t.cycletype", r.CYCLETYPE)
  1703. sqlId.And("t.cycletime", r.CYCLETIME)
  1704. sqlId.Join(" GROUP BY t.AREAUSERID, t.CURRENCYID, t.CYCLETIME, t.CYCLETYPE")
  1705. return sqlId.String()
  1706. }
  1707. // GetDataEx 获取损益汇总表
  1708. func (r *Ermcp3ArealSumPL) GetDataEx() (interface{}, error) {
  1709. sData := make([]Ermcp3ArealSumPL, 0)
  1710. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1711. for i := range sData {
  1712. sData[i].calc()
  1713. sData[i].BeginDate = r.BeginDate
  1714. sData[i].EndDate = r.EndDate
  1715. }
  1716. return sData, err
  1717. }
  1718. // Ermcp3TaFutuReDataReport 期货报表
  1719. type Ermcp3TaFutuReDataReport struct {
  1720. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照时期(yyyyMMdd)
  1721. CYCLETIME string `json:"cycletime" xorm:"CYCLETIME"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  1722. CYCLETYPE int32 `json:"cycletype" xorm:"CYCLETYPE"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  1723. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID"` // 交易账户
  1724. GOODSID string `json:"goodsid" xorm:"GOODSID"` // 交易合约
  1725. BUYORSELL int32 `json:"buyorsell" xorm:"BUYORSELL"` // 持仓方向
  1726. CURRENCYID string `json:"currencyid" xorm:"CURRENCYID"` // 结算币种ID
  1727. USERID int64 `json:"userid" xorm:"USERID"` // 账户所属用户ID
  1728. RELATEDUSERID string `json:"relateduserid" xorm:"RELATEDUSERID"` // 账户关联用户ID
  1729. GOODSGROUPID string `json:"goodsgroupid" xorm:"GOODSGROUPID"` // 交易品种
  1730. ORIHOLDQTY float64 `json:"oriholdqty" xorm:"ORIHOLDQTY"` // 期初持仓量
  1731. ORIOPENAMOUNT float64 `json:"oriopenamount" xorm:"ORIOPENAMOUNT"` // 期初开仓额
  1732. ORIHOLDAMOUNT float64 `json:"oriholdamount" xorm:"ORIHOLDAMOUNT"` // 期初持仓额
  1733. TODAYOPENQTY float64 `json:"todayopenqty" xorm:"TODAYOPENQTY"` // 今开仓量
  1734. TODAYOPENAMOUNT float64 `json:"todayopenamount" xorm:"TODAYOPENAMOUNT"` // 今开仓额
  1735. TODAYCLOSEQTY float64 `json:"todaycloseqty" xorm:"TODAYCLOSEQTY"` // 今平仓量
  1736. TODAYCLOSEAMOUNT float64 `json:"todaycloseamount" xorm:"TODAYCLOSEAMOUNT"` // 今平仓额
  1737. HOLDQTY float64 `json:"holdqty" xorm:"HOLDQTY"` // 期末持仓量
  1738. OPENAMOUNT float64 `json:"openamount" xorm:"OPENAMOUNT"` // 期末开仓额
  1739. HOLDAMOUNT float64 `json:"holdamount" xorm:"HOLDAMOUNT"` // 期末持仓额
  1740. RECKONPRICE float64 `json:"reckonprice" xorm:"RECKONPRICE"` // 结算价
  1741. RECKONHOLDAMOUNT float64 `json:"reckonholdamount" xorm:"RECKONHOLDAMOUNT"` // 结算持仓额
  1742. CHARGE float64 `json:"charge" xorm:"CHARGE"` // 手续费
  1743. CLOSEPL float64 `json:"closepl" xorm:"CLOSEPL"` // 平仓损益
  1744. RECKONPL float64 `json:"reckonpl" xorm:"RECKONPL"` // 结算损益
  1745. LASTPRICE float64 `json:"lastprice" xorm:"LASTPRICE"` // 最新价
  1746. LASTHOLDAMOUNT float64 `json:"lastholdamount" xorm:"LASTHOLDAMOUNT"` // 当前持仓额
  1747. FLOATPL float64 `json:"floatpl" xorm:"FLOATPL"` // 浮动损益
  1748. GOODSGROUPSPOTQTY float64 `json:"goodsgroupspotqty" xorm:"GOODSGROUPSPOTQTY"` // 交易品种期末量 (= 期末持仓量 * 合约乘数)
  1749. TODAYGOODSGROUPSPOTQTY float64 `json:"todaygoodsgroupspotqty" xorm:"TODAYGOODSGROUPSPOTQTY"` // 交易品种变化量 (=(期末持仓量 - 期初持仓量)* 合约乘数)
  1750. MIDDLEGOODSQTY float64 `json:"middlegoodsqty" xorm:"MIDDLEGOODSQTY"` // 套保品种期末量 (=交易品种期末量 * 期货品种折算系数)
  1751. TODAYMIDDLEGOODSQTY float64 `json:"todaymiddlegoodsqty" xorm:"TODAYMIDDLEGOODSQTY"` // 套保品种变化量 (= 交易品种变化量*期货品种折算系数)
  1752. RECKONPL2 float64 `json:"reckonpl2" xorm:"RECKONPL2"` // 结算逐笔盈亏
  1753. OUTERGROUPCODE string `json:"outergroupcode" xorm:"'OUTERGROUPCODE'"` // 交易品种代码
  1754. GOODSGROUPNAME string `json:"goodsgroupname" xorm:"'GOODSGROUPNAME'"` // 交易品种名称
  1755. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 交易合约代码
  1756. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 交易合约名称
  1757. GOODUNITID int32 `json:"goodunitid" xorm:"'GOODUNITID'"` // 交易合约单位id
  1758. ACCOUNTNAME string `json:"accountname" xorm:"'ACCOUNTNAME'"` // 交易账号名称
  1759. ENUMDICNAME string `json:"enumdicname"` // 单位名称(现货商品)
  1760. QueryType int32 `json:"-"` // 查询类型 1-报表 2-报表明细
  1761. BeginDate string `json:"begindate"` // 开始交易日
  1762. EndDate string `json:"enddate"` // 结束交易日
  1763. }
  1764. func (r *Ermcp3TaFutuReDataReport) calc() {
  1765. }
  1766. func (r *Ermcp3TaFutuReDataReport) userType() int32 {
  1767. var userType int32 = 2
  1768. areaUserId := mtpcache.GetAreaUserId(r.USERID, 0)
  1769. if areaUserId == r.USERID {
  1770. userType = 2
  1771. } else {
  1772. userType = 7
  1773. }
  1774. return userType
  1775. }
  1776. func (r *Ermcp3TaFutuReDataReport) buildSql() string {
  1777. userType := r.userType()
  1778. // 日报表
  1779. if r.QueryType == 1 && r.CYCLETYPE == 0 {
  1780. return r.buildSqlReckon(userType)
  1781. }
  1782. // 日报表明细
  1783. if r.QueryType == 2 && r.CYCLETYPE == 0 {
  1784. return r.buildSqlReckonDetail(userType)
  1785. }
  1786. return r.buildSqlReport(userType)
  1787. }
  1788. // buildSqlReckon 日照表
  1789. func (r *Ermcp3TaFutuReDataReport) buildSqlReckon(userType int32) string {
  1790. var sqlId utils.SQLVal = "select a.*," +
  1791. " gp.outergroupcode," +
  1792. " gp.goodsgroupname," +
  1793. " g.goodscode," +
  1794. " g.goodsname," +
  1795. " g.goodunitid" +
  1796. " from (SELECT t.RECKONDATE," +
  1797. " t.GOODSID," +
  1798. " t.BUYORSELL," +
  1799. " t.CURRENCYID," +
  1800. " t.%v USERID," +
  1801. " t.GOODSGROUPID," +
  1802. " sum(t.ORIHOLDQTY) ORIHOLDQTY," +
  1803. " sum(t.ORIOPENAMOUNT) ORIOPENAMOUNT," +
  1804. " sum(t.ORIHOLDAMOUNT) ORIHOLDAMOUNT," +
  1805. " sum(t.TODAYOPENQTY) TODAYOPENQTY," +
  1806. " sum(t.TODAYOPENAMOUNT) TODAYOPENAMOUNT," +
  1807. " sum(t.TODAYCLOSEQTY) TODAYCLOSEQTY," +
  1808. " sum(t.TODAYCLOSEAMOUNT) TODAYCLOSEAMOUNT," +
  1809. " sum(t.HOLDQTY) HOLDQTY," +
  1810. " sum(t.OPENAMOUNT) OPENAMOUNT," +
  1811. " sum(t.HOLDAMOUNT) HOLDAMOUNT," +
  1812. " sum(t.RECKONPRICE) RECKONPRICE," +
  1813. " sum(t.RECKONHOLDAMOUNT) RECKONHOLDAMOUNT," +
  1814. " sum(t.CHARGE) CHARGE," +
  1815. " sum(t.CLOSEPL) CLOSEPL," +
  1816. " sum(t.RECKONPL) RECKONPL," +
  1817. " sum(t.LASTPRICE) LASTPRICE," +
  1818. " sum(t.LASTHOLDAMOUNT) LASTHOLDAMOUNT," +
  1819. " sum(t.FLOATPL) FLOATPL," +
  1820. " sum(t.GOODSGROUPSPOTQTY) GOODSGROUPSPOTQTY," +
  1821. " sum(t.TODAYGOODSGROUPSPOTQTY) TODAYGOODSGROUPSPOTQTY," +
  1822. " sum(t.MIDDLEGOODSQTY) MIDDLEGOODSQTY," +
  1823. " sum(t.TODAYMIDDLEGOODSQTY) TODAYMIDDLEGOODSQTY," +
  1824. " sum(t.Reckonpl2) RECKONPL2" +
  1825. " FROM RECKON_ERMCP_TAFUTUREDATA t" +
  1826. " WHERE 1 = 1 %v" +
  1827. " GROUP BY t.RECKONDATE," +
  1828. " t.GOODSID," +
  1829. " t.BUYORSELL," +
  1830. " t.CURRENCYID," +
  1831. " t.%v," +
  1832. " t.GOODSGROUPID) a" +
  1833. " left join goodsgroup gp" +
  1834. " on a.goodsgroupid = gp.goodsgroupid" +
  1835. " left join goods g" +
  1836. " on a.goodsid = g.goodsid"
  1837. userId := "USERID"
  1838. var sqlParam utils.SQLVal
  1839. if userType == 2 {
  1840. sqlParam.AndEx("t.userid", r.USERID, userType == 2)
  1841. userId = "USERID"
  1842. } else if userType == 7 {
  1843. sqlParam.AndEx("t.relateduserid", r.USERID, userType == 7)
  1844. userId = "RELATEDUSERID"
  1845. }
  1846. sqlParam.AndEx("t.reckondate", r.RECKONDATE, len(r.RECKONDATE) > 0)
  1847. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  1848. sqlParam.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1849. }
  1850. sqlId.FormatParam(userId, sqlParam.String(), userId)
  1851. return sqlId.String()
  1852. }
  1853. // buildSqlReckon 日照表明细
  1854. func (r *Ermcp3TaFutuReDataReport) buildSqlReckonDetail(userType int32) string {
  1855. var sqlId utils.SQLVal = "select a.*," +
  1856. " gp.outergroupcode," +
  1857. " gp.goodsgroupname," +
  1858. " g.goodscode," +
  1859. " g.goodsname," +
  1860. " g.goodunitid," +
  1861. " ta.accountname" +
  1862. " from (SELECT t.reckondate," +
  1863. " t.GOODSID," +
  1864. " t.BUYORSELL," +
  1865. " t.CURRENCYID," +
  1866. " t.%v USERID," +
  1867. " t.accountid," +
  1868. " t.GOODSGROUPID," +
  1869. " sum(t.ORIHOLDQTY) ORIHOLDQTY," +
  1870. " sum(t.ORIOPENAMOUNT) ORIOPENAMOUNT," +
  1871. " sum(t.ORIHOLDAMOUNT) ORIHOLDAMOUNT," +
  1872. " sum(t.TODAYOPENQTY) TODAYOPENQTY," +
  1873. " sum(t.TODAYOPENAMOUNT) TODAYOPENAMOUNT," +
  1874. " sum(t.TODAYCLOSEQTY) TODAYCLOSEQTY," +
  1875. " sum(t.TODAYCLOSEAMOUNT) TODAYCLOSEAMOUNT," +
  1876. " sum(t.HOLDQTY) HOLDQTY," +
  1877. " sum(t.OPENAMOUNT) OPENAMOUNT," +
  1878. " sum(t.HOLDAMOUNT) HOLDAMOUNT," +
  1879. " sum(t.RECKONPRICE) RECKONPRICE," +
  1880. " sum(t.RECKONHOLDAMOUNT) RECKONHOLDAMOUNT," +
  1881. " sum(t.CHARGE) CHARGE," +
  1882. " sum(t.CLOSEPL) CLOSEPL," +
  1883. " sum(t.RECKONPL) RECKONPL," +
  1884. " sum(t.LASTPRICE) LASTPRICE," +
  1885. " sum(t.LASTHOLDAMOUNT) LASTHOLDAMOUNT," +
  1886. " sum(t.FLOATPL) FLOATPL," +
  1887. " sum(t.GOODSGROUPSPOTQTY) GOODSGROUPSPOTQTY," +
  1888. " sum(t.TODAYGOODSGROUPSPOTQTY) TODAYGOODSGROUPSPOTQTY," +
  1889. " sum(t.MIDDLEGOODSQTY) MIDDLEGOODSQTY," +
  1890. " sum(t.TODAYMIDDLEGOODSQTY) TODAYMIDDLEGOODSQTY," +
  1891. " sum(t.Reckonpl2) RECKONPL2" +
  1892. " FROM RECKON_ERMCP_TAFUTUREDATA t" +
  1893. " WHERE 1 = 1 %v" +
  1894. " GROUP BY t.reckondate," +
  1895. " t.GOODSID," +
  1896. " t.BUYORSELL," +
  1897. " t.CURRENCYID," +
  1898. " t.%v," +
  1899. " t.accountid," +
  1900. " t.GOODSGROUPID) a" +
  1901. " left join goodsgroup gp" +
  1902. " on a.goodsgroupid = gp.goodsgroupid" +
  1903. " left join goods g" +
  1904. " on a.goodsid = g.goodsid" +
  1905. " left join taaccount ta on a.accountid = ta.accountid"
  1906. userId := "USERID"
  1907. var sqlParam utils.SQLVal
  1908. if userType == 2 {
  1909. sqlParam.AndEx("t.userid", r.USERID, userType == 2)
  1910. userId = "USERID"
  1911. } else if userType == 7 {
  1912. sqlParam.AndEx("t.relateduserid", r.USERID, userType == 7)
  1913. userId = "RELATEDUSERID"
  1914. }
  1915. sqlParam.AndEx("t.reckondate", r.RECKONDATE, len(r.RECKONDATE) > 0)
  1916. if len(r.BeginDate) > 0 && len(r.EndDate) > 0 {
  1917. sqlParam.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1918. }
  1919. sqlId.FormatParam(userId, sqlParam.String(), userId)
  1920. return sqlId.String()
  1921. }
  1922. // buildSqlReckon 报表表
  1923. func (r *Ermcp3TaFutuReDataReport) buildSqlReport(userType int32) string {
  1924. var sqlId utils.SQLVal = "select a.*," +
  1925. " gp.outergroupcode," +
  1926. " gp.goodsgroupname," +
  1927. " g.goodscode," +
  1928. " g.goodsname," +
  1929. " g.goodunitid" +
  1930. " from (SELECT t.cycletype," +
  1931. " t.cycletime," +
  1932. " t.GOODSID," +
  1933. " t.BUYORSELL," +
  1934. " t.CURRENCYID," +
  1935. " t.%v USERID," +
  1936. " t.GOODSGROUPID," +
  1937. " sum(t.ORIHOLDQTY) ORIHOLDQTY," +
  1938. " sum(t.ORIOPENAMOUNT) ORIOPENAMOUNT," +
  1939. " sum(t.ORIHOLDAMOUNT) ORIHOLDAMOUNT," +
  1940. " sum(t.TODAYOPENQTY) TODAYOPENQTY," +
  1941. " sum(t.TODAYOPENAMOUNT) TODAYOPENAMOUNT," +
  1942. " sum(t.TODAYCLOSEQTY) TODAYCLOSEQTY," +
  1943. " sum(t.TODAYCLOSEAMOUNT) TODAYCLOSEAMOUNT," +
  1944. " sum(t.HOLDQTY) HOLDQTY," +
  1945. " sum(t.OPENAMOUNT) OPENAMOUNT," +
  1946. " sum(t.HOLDAMOUNT) HOLDAMOUNT," +
  1947. " sum(t.RECKONPRICE) RECKONPRICE," +
  1948. " sum(t.RECKONHOLDAMOUNT) RECKONHOLDAMOUNT," +
  1949. " sum(t.CHARGE) CHARGE," +
  1950. " sum(t.CLOSEPL) CLOSEPL," +
  1951. " sum(t.RECKONPL) RECKONPL," +
  1952. " sum(t.LASTPRICE) LASTPRICE," +
  1953. " sum(t.LASTHOLDAMOUNT) LASTHOLDAMOUNT," +
  1954. " sum(t.FLOATPL) FLOATPL," +
  1955. " sum(t.GOODSGROUPSPOTQTY) GOODSGROUPSPOTQTY," +
  1956. " sum(t.TODAYGOODSGROUPSPOTQTY) TODAYGOODSGROUPSPOTQTY," +
  1957. " sum(t.MIDDLEGOODSQTY) MIDDLEGOODSQTY," +
  1958. " sum(t.TODAYMIDDLEGOODSQTY) TODAYMIDDLEGOODSQTY," +
  1959. " sum(t.Reckonpl2) RECKONPL2" +
  1960. " FROM REPORT_ERMCP_TAFUTUREDATA t" +
  1961. " WHERE 1 = 1 %v" +
  1962. " and t.userid = 8888" +
  1963. " and t.cycletype = 1" +
  1964. " and t.cycletime = '202105'" +
  1965. " GROUP BY t.cycletype," +
  1966. " t.cycletime," +
  1967. " t.GOODSID," +
  1968. " t.BUYORSELL," +
  1969. " t.CURRENCYID," +
  1970. " t.%v," +
  1971. " t.GOODSGROUPID) a" +
  1972. " left join goodsgroup gp" +
  1973. " on a.goodsgroupid = gp.goodsgroupid" +
  1974. " left join goods g" +
  1975. " on a.goodsid = g.goodsid"
  1976. userId := "USERID"
  1977. var sqlParam utils.SQLVal
  1978. if userType == 2 {
  1979. sqlParam.AndEx("t.userid", r.USERID, userType == 2)
  1980. userId = "USERID"
  1981. } else if userType == 7 {
  1982. sqlParam.AndEx("t.relateduserid", r.USERID, userType == 7)
  1983. userId = "RELATEDUSERID"
  1984. }
  1985. sqlParam.And("t.cycletype", r.CYCLETYPE)
  1986. sqlParam.And("t.cycletime", r.CYCLETIME)
  1987. sqlId.FormatParam(userId, sqlParam.String(), userId)
  1988. return sqlId.String()
  1989. }
  1990. // GetDataEx 获取期货报表
  1991. func (r *Ermcp3TaFutuReDataReport) GetDataEx() (interface{}, error) {
  1992. sData := make([]Ermcp3TaFutuReDataReport, 0)
  1993. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1994. for i := range sData {
  1995. sData[i].calc()
  1996. sData[i].BeginDate = r.BeginDate
  1997. sData[i].EndDate = r.EndDate
  1998. }
  1999. return sData, err
  2000. }