ermcp3Report.go 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  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. )
  13. // Ermcp3ReportOPLog 报表合同操作记录通用查询
  14. type Ermcp3ReportOPLog struct {
  15. LOGID string `json:"logid" xorm:"'lOGID'"` // 流水ID(604+Unix秒时间戳(10位)+xxxxxx)
  16. BIZTYPE int32 `json:"biztype" xorm:"'BIZTYPE'"` // 业务类型 - 1:套保计划 2:现货合同
  17. OPERATELOGTYPE int32 `json:"operatelogtype" xorm:"'OPERATELOGTYPE'"` // 操作流水类型 -
  18. RELATEDID string `json:"relatedid" xorm:"'RELATEDID'"` // 现货合同ID\套保计划
  19. LOGVALUE string `json:"logvalue" xorm:"'LOGVALUE'"` // 数值
  20. LOGDATETIME string `json:"logdatetime" xorm:"'LOGDATETIME'"` // 流水日期(时间)
  21. TRADEDATE string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd)
  22. APPLYID int64 `json:"applyid" xorm:"'APPLYID'"` // 操作人
  23. CONTRACTTYPE int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 现货合同类型 - 1:采购 -1:销售
  24. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构ID
  25. RELATEDNO string `json:"relatedno" xorm:"'RELATEDNO'"` // 合同编号
  26. UNITID int32 `json:"-" xorm:"'UNITID'"` // 现货商品单位id
  27. ENUMDICNAME string `json:"enumdicName"` // 单位名称
  28. OPTYPENAME string `json:"optypename"` // 流水类型名称
  29. LOGTYPENAME string `json:"logtypename"` // 合同类型(名称)
  30. APPLYNAME string `json:"applyname"` // 操作人名称
  31. BRANDNAME string `json:"brandname" xorm:"'brandname'"` // 品牌名称
  32. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种id
  33. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  34. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  35. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'wrstandardid'"` // 品类id
  36. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'wrstandardcode'"` // 品类代码
  37. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'wrstandardname'"` // 品类名称
  38. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 品牌id
  39. LogTypeFilter string `json:"-"` // 查询日志类型, 逗号隔开(如 1,2,4)
  40. }
  41. // Calc 处理数据
  42. func (r *Ermcp3ReportOPLog) Calc() {
  43. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  44. r.APPLYNAME = mtpcache.GetUserNameByUserId(r.APPLYID)
  45. if r.APPLYNAME == "" {
  46. // APPLYID 可能存的是loginId
  47. r.APPLYNAME = mtpcache.GetLoginCodeByLoginId(r.APPLYID)
  48. }
  49. //1:点价价格 2:点价数量 3:结算量 4:其它费用 5:追加保证金 6溢短金额 7:调整金额 8:付款金额 9:收款金额 10:退款金额
  50. //11:收票金额 12:开票金额 13:提交审核(合同) 14:审核通过(合同) 15:审核拒绝(合同) 16:合同撤回 17:提交审核(计划)
  51. //18:审核通过(计划) 19:审核拒绝(计划) 20:计划撤回 21:正常完结(合同) 22:异常终止(合同) 23:退还保证金
  52. //24:采购入库 25:销售出库 26:生产入库 27:生产出库
  53. // 数据库注释与返回值映身关系: 结算量->确定量, 收款->收款金额, 退款->退款金额, 付款->付款金额, 收票->收票金额, 开票->开票金额
  54. sDes := []string{"点价价格", "点价数量", "确定量", "其它费用", "追加保证金", "溢短金额", "调整金额", "付款", "收款", "退款",
  55. "收票", "开票", "提交审核(合同)", "审核通过(合同)", "审核拒绝(合同)", "合同撤回", "提交审核(计划)",
  56. "审核通过(计划)", "审核拒绝(计划)", "计划撤回", "正常完结(合同)", "异常终止(合同)", "退还保证金",
  57. "采购入库", "销售出库", "生产入库", "生产出库"}
  58. if r.OPERATELOGTYPE >= 1 && r.OPERATELOGTYPE <= 27 {
  59. r.OPTYPENAME = sDes[r.OPERATELOGTYPE-1]
  60. }
  61. // 收款款项:采购合同的退款、销售合同的收款应用正数显示,付款款项:采购合同的付款、销售合同的退款应用负数显示
  62. if r.CONTRACTTYPE == 1 {
  63. r.LOGTYPENAME = "采购"
  64. // 采购合同/退款 正数
  65. if r.OPERATELOGTYPE == 10 && len(r.LOGVALUE) > 0 {
  66. if r.LOGVALUE[0] == '-' {
  67. r.LOGVALUE = r.LOGVALUE[1:]
  68. }
  69. }
  70. // 采购合同/付款 负数
  71. if r.OPERATELOGTYPE == 8 && len(r.LOGVALUE) > 0 {
  72. if r.LOGVALUE[0] != '-' {
  73. r.LOGVALUE = "-" + r.LOGVALUE
  74. }
  75. }
  76. } else if r.CONTRACTTYPE == -1 {
  77. r.LOGTYPENAME = "销售"
  78. // 销售合同/收款 正数
  79. if r.OPERATELOGTYPE == 9 && len(r.LOGVALUE) > 0 {
  80. if r.LOGVALUE[0] == '-' {
  81. r.LOGVALUE = r.LOGVALUE[1:]
  82. }
  83. }
  84. // 销售合同/退款 负数
  85. if r.OPERATELOGTYPE == 10 && len(r.LOGVALUE) > 0 {
  86. if r.LOGVALUE[0] != '-' {
  87. r.LOGVALUE = "-" + r.LOGVALUE
  88. }
  89. }
  90. }
  91. // 去除多余的0,最后4个0
  92. if r.LOGVALUE[len(r.LOGVALUE)-4:] == "0000" {
  93. r.LOGVALUE = r.LOGVALUE[:len(r.LOGVALUE)-4]
  94. }
  95. if r.BIZTYPE == 1 {
  96. r.LOGTYPENAME += "计划"
  97. } else if r.BIZTYPE == 2 {
  98. r.LOGTYPENAME += "合同"
  99. }
  100. }
  101. func (r *Ermcp3ReportOPLog) buildSql() string {
  102. var sqlId utils.SQLVal = "SELECT to_char(t.LOGID) LOGID," +
  103. " t.BIZTYPE," +
  104. " t.OPERATELOGTYPE," +
  105. " to_char(t.RELATEDID) RELATEDID," +
  106. " t.LOGVALUE," +
  107. " to_char(t.LOGDATETIME, 'yyyy-mm-dd hh24:mi:ss') LOGDATETIME," +
  108. " t.TRADEDATE," +
  109. " t.APPLYID," +
  110. " t.CONTRACTTYPE," +
  111. " t.USERID," +
  112. " t.wrstandardid," +
  113. " s.contractno relatedno," +
  114. " s.spotgoodsbrandid," +
  115. " g.deliverygoodsid," +
  116. " g.deliverygoodscode," +
  117. " g.deliverygoodsname," +
  118. " g.goodsunitid unitid," +
  119. " gb.dgfactoryitemvalue brandname," +
  120. " s.accountid," +
  121. " w.wrstandardcode," +
  122. " w.wrstandardname" +
  123. " FROM ERMCP_CONTRACTOPERATELOG t" +
  124. " inner join ermcp_spotcontract s" +
  125. " on t.RELATEDID = s.spotcontractid" +
  126. " left join deliverygoods g" +
  127. " on t.deliverygoodsid = g.deliverygoodsid" +
  128. " left join wrstandard w on t.wrstandardid=w.wrstandardid" +
  129. " left join dgfactoryitem gb" +
  130. " on s.spotgoodsbrandid = gb.dgfactoryitemid" +
  131. " WHERE t.biztype = 2"
  132. // 筛选条件
  133. sqlId.And("t.userid", r.USERID)
  134. sqlId.And("t.TRADEDATE", r.TRADEDATE)
  135. sqlId.AndEx("t.deliverygoodsid", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  136. sqlId.AndEx("t.wrstandardid", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  137. sqlId.AndEx("s.spotgoodsbrandid", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  138. if r.LogTypeFilter != "" {
  139. sqlId.Join(fmt.Sprintf(" and t.OPERATELOGTYPE in(%v)", r.LogTypeFilter))
  140. }
  141. return sqlId.String()
  142. }
  143. // GetDataEx 获取日志记录
  144. func (r *Ermcp3ReportOPLog) GetDataEx() (interface{}, error) {
  145. sData := make([]Ermcp3ReportOPLog, 0)
  146. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  147. for i := range sData {
  148. sData[i].Calc()
  149. }
  150. return sData, err
  151. }
  152. // Ermcp3ReckonAreaSpotSub 现货日报表(作废)
  153. type Ermcp3ReckonAreaSpotSub struct {
  154. RECKONDATE string `json:"reckondate" xorm:"'RECKONDATE'" form:"reckondate" binding:"required"` // 日照时期(yyyyMMdd)
  155. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'" form:"userid" binding:"required"` // 账户所属机构
  156. ACCOUNTID int64 `json:"accountid" xorm:"'ACCOUNTID'" form:"accountid"` // 期货账户ID
  157. WRFACTORTYPEID int64 `json:"wrfactortypeid" xorm:"'WRFACTORTYPEID'"` // 仓单要素类型ID(212+Unix秒时间戳(10位)+xxxxxx)
  158. DELIVERYGOODSID int64 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'" form:"deliverygoodsid"` // 现货品种ID
  159. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'" form:"wrstandardid"` // 现货品类ID
  160. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'" form:"spotgoodsbrandid"` // 现货品牌ID
  161. ORIBUYPRICEDQTY float64 `json:"oribuypricedqty" xorm:"'ORIBUYPRICEDQTY'"` // 期初采购合同已定价数量
  162. ORISELLPRICEDQTY float64 `json:"orisellpricedqty" xorm:"'ORISELLPRICEDQTY'"` // 期初销售合同已定价数量
  163. BUYPRICEDQTY float64 `json:"buypricedqty" xorm:"'BUYPRICEDQTY'"` // 采购合同已定价数量
  164. SELLPRICEDQTY float64 `json:"sellpricedqty" xorm:"'SELLPRICEDQTY'"` // 销售合同已定价数量
  165. TOTALSPOTQTY float64 `json:"totalspotqty" xorm:"'TOTALSPOTQTY'"` // 现货头寸总量 = 采购合同已定价数量 - 销售合同已定价数量
  166. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  167. TODAYBUYRECKONQTY float64 `json:"todaybuyreckonqty" xorm:"'TODAYBUYRECKONQTY'"` // 今日采购合同已确定数量
  168. TODAYSELLRECKONQTY float64 `json:"todaysellreckonqty" xorm:"'TODAYSELLRECKONQTY'"` // 今日销售合同已确定数量
  169. ORIBUYPRICEDAMOUNT float64 `json:"oribuypricedamount" xorm:"'ORIBUYPRICEDAMOUNT'"` // 期初采购合同已定价金额
  170. ORISELLPRICEDAMOUNT float64 `json:"orisellpricedamount" xorm:"'ORISELLPRICEDAMOUNT'"` // 期初销售合同已定价金额
  171. BUYPRICEDAMOUNT float64 `json:"buypricedamount" xorm:"'BUYPRICEDAMOUNT'"` // 采购合同已定价金额
  172. SELLPRICEDAMOUNT float64 `json:"sellpricedamount" xorm:"'SELLPRICEDAMOUNT'"` // 销售合同已定价金额
  173. ORIBUYRECKONQTY float64 `json:"oribuyreckonqty" xorm:"'ORIBUYRECKONQTY'"` // 期初采购合同交收量
  174. ORISELLRECKONQTY float64 `json:"orisellreckonqty" xorm:"'ORISELLRECKONQTY'"` // 期初销售合同交收量
  175. ORIBUYCONTRACTINQTY float64 `json:"oribuycontractinqty" xorm:"'ORIBUYCONTRACTINQTY'"` // 期初采购合同入库量
  176. ORISELLCONTRACTOUTQTY float64 `json:"orisellcontractoutqty" xorm:"'ORISELLCONTRACTOUTQTY'"` // 期初销售合同出库量
  177. ORIPRODUCEINQTY float64 `json:"oriproduceinqty" xorm:"'ORIPRODUCEINQTY'"` // 期初生产入库量
  178. ORIPRODUCEOUTQTY float64 `json:"oriproduceoutqty" xorm:"'ORIPRODUCEOUTQTY'"` // 期初生产出库量
  179. BUYRECKONQTY float64 `json:"buyreckonqty" xorm:"'BUYRECKONQTY'"` // 采购合同交收量
  180. SELLRECKONQTY float64 `json:"sellreckonqty" xorm:"'SELLRECKONQTY'"` // 销售合同交收量
  181. BUYCONTRACTINQTY float64 `json:"buycontractinqty" xorm:"'BUYCONTRACTINQTY'"` // 采购合同入库量
  182. SELLCONTRACTOUTQTY float64 `json:"sellcontractoutqty" xorm:"'SELLCONTRACTOUTQTY'"` // 销售合同出库量
  183. PRODUCEINQTY float64 `json:"produceinqty" xorm:"'PRODUCEINQTY'"` // 生产入库量
  184. PRODUCEOUTQTY float64 `json:"produceoutqty" xorm:"'PRODUCEOUTQTY'"` // 生产出库量
  185. ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 现货商品单位名称
  186. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'wrstandardname'"` // 品类名称
  187. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'wrstandardcode'"` // 品类代码
  188. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'deliverygoodscode'"` // 现货商品代码
  189. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'deliverygoodsname'"` // 现货商品名称
  190. GOODSUNITID int32 `json:"goodsunitid" xorm:"'goodsunitid'"` // 现货商品单位id
  191. BRANDNAME string `json:"brandname" xorm:"'brandname'"` // 品牌名称
  192. ACCOUNTNAME string `json:"accountname" xorm:"'accountname'"` // 所属机构名称
  193. BIZTYPE int32 `json:"biztype" xorm:"'BIZTYPE'"` // 业务类型 1-套保 2-套利
  194. CURRENCYID int32 `json:"currencyid" xorm:"'CURRENCYID'"` // 币种id
  195. CURRENCYNAME string `json:"currencyname"` // 币种名称
  196. TOTALBUYPRICEDQTY float64 `json:"totalbuypricedqty"` // 采购定价量 = 期末 - 期初
  197. TOTALSELLPRICEDQTY float64 `json:"totalsellpricedqty"` // 销售定价量 = 期末 - 期初
  198. BeginDate string `json:"-"` // 开始日期
  199. EndDate string `json:"-"` // 结束日期
  200. }
  201. func (r *Ermcp3ReckonAreaSpotSub) calc() {
  202. r.TOTALBUYPRICEDQTY = r.BUYPRICEDQTY - r.ORIBUYPRICEDQTY
  203. r.TOTALSELLPRICEDQTY = r.SELLPRICEDQTY - r.ORISELLPRICEDQTY
  204. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  205. }
  206. func (r *Ermcp3ReckonAreaSpotSub) buildSql() string {
  207. var sqlId utils.SQLVal = "SELECT t.ORIBUYRECKONQTY," +
  208. " t.ORISELLRECKONQTY," +
  209. " t.ORIBUYCONTRACTINQTY," +
  210. " t.ORISELLCONTRACTOUTQTY," +
  211. " t.ORIPRODUCEINQTY," +
  212. " t.ORIPRODUCEOUTQTY," +
  213. " t.BUYRECKONQTY," +
  214. " t.SELLRECKONQTY," +
  215. " t.BUYCONTRACTINQTY," +
  216. " t.SELLCONTRACTOUTQTY," +
  217. " t.PRODUCEINQTY," +
  218. " t.PRODUCEOUTQTY," +
  219. " t.Reckondate," +
  220. " t.AREAUSERID," +
  221. " t.ACCOUNTID," +
  222. " t.WRFACTORTYPEID," +
  223. " t.DELIVERYGOODSID," +
  224. " t.WRSTANDARDID," +
  225. " t.SPOTGOODSBRANDID," +
  226. " t.ORIBUYPRICEDQTY," +
  227. " t.ORISELLPRICEDQTY," +
  228. " t.BUYPRICEDQTY," +
  229. " t.SELLPRICEDQTY," +
  230. " t.TOTALSPOTQTY," +
  231. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  232. " t.TODAYBUYRECKONQTY," +
  233. " t.TODAYSELLRECKONQTY," +
  234. " t.ORIBUYPRICEDAMOUNT," +
  235. " t.ORISELLPRICEDAMOUNT," +
  236. " t.BUYPRICEDAMOUNT," +
  237. " t.SELLPRICEDAMOUNT," +
  238. " t.currencyid," +
  239. " t.biztype," +
  240. " w.wrstandardname," +
  241. " w.wrstandardcode," +
  242. " g.deliverygoodscode," +
  243. " g.deliverygoodsname," +
  244. " g.goodsunitid," +
  245. " d.dgfactoryitemvalue brandname," +
  246. " u.accountname," +
  247. " e.enumdicname" +
  248. " FROM RECKON_ERMCP_AREASPOTSUB t" +
  249. " LEFT JOIN WRSTANDARD w" +
  250. " on t.wrstandardid = w.wrstandardid" +
  251. " LEFT JOIN DELIVERYGOODS g" +
  252. " on t.deliverygoodsid = g.deliverygoodsid" +
  253. " LEFT JOIN DGFACTORYITEM d" +
  254. " on t.spotgoodsbrandid = d.dgfactoryitemid" +
  255. " LEFT JOIN USERACCOUNT u" +
  256. " on t.areauserid = u.userid" +
  257. " LEFT JOIN ENUMDICITEM e" +
  258. " on g.goodsunitid = e.enumitemname" +
  259. " and e.enumdiccode = 'goodsunit'" +
  260. " and e.enumitemstatus = 1" +
  261. " WHERE 1 = 1"
  262. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  263. sqlId.AndEx("t.ACCOUNTID", r.ACCOUNTID, r.ACCOUNTID > 0)
  264. sqlId.AndEx("t.deliverygoodsid", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  265. sqlId.AndEx("t.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  266. sqlId.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  267. if r.RECKONDATE != "" {
  268. sqlId.And("t.RECKONDATE", r.RECKONDATE)
  269. } else if r.BeginDate != "" && r.BeginDate == r.EndDate {
  270. sqlId.And("t.RECKONDATE", r.BeginDate)
  271. } else {
  272. if r.BeginDate != "" {
  273. sqlId.BiggerOrEq("t.RECKONDATE", r.BeginDate)
  274. }
  275. if r.EndDate != "" {
  276. sqlId.LessOrEq("t.RECKONDATE", r.EndDate)
  277. }
  278. }
  279. sqlId.Join(" order by t.RECKONDATE")
  280. return sqlId.String()
  281. }
  282. // GetDataEx 获取现货日报表
  283. func (r *Ermcp3ReckonAreaSpotSub) GetDataEx() (interface{}, error) {
  284. sData := make([]Ermcp3ReckonAreaSpotSub, 0)
  285. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  286. for i := range sData {
  287. sData[i].calc()
  288. }
  289. return sData, err
  290. }
  291. // Ermcp3ReportAreaSpotSub 现货分类月报表(作废)
  292. type Ermcp3ReportAreaSpotSub struct {
  293. ORIBUYRECKONQTY float64 `json:"oribuyreckonqty" xorm:"'ORIBUYRECKONQTY'"` // 期初采购合同交收量【期初】
  294. ORISELLRECKONQTY float64 `json:"orisellreckonqty" xorm:"'ORISELLRECKONQTY'"` // 期初销售合同交收量【期初】
  295. ORIBUYCONTRACTINQTY float64 `json:"oribuycontractinqty" xorm:"'ORIBUYCONTRACTINQTY'"` // 期初采购合同入库量【期初】
  296. ORISELLCONTRACTOUTQTY float64 `json:"orisellcontractoutqty" xorm:"'ORISELLCONTRACTOUTQTY'"` // 期初销售合同出库量【期初】
  297. ORIPRODUCEINQTY float64 `json:"oriproduceinqty" xorm:"'ORIPRODUCEINQTY'"` // 期初生产入库量【期初】
  298. ORIPRODUCEOUTQTY float64 `json:"oriproduceoutqty" xorm:"'ORIPRODUCEOUTQTY'"` // 期初生产出库量【期初】
  299. BUYRECKONQTY float64 `json:"buyreckonqty" xorm:"'BUYRECKONQTY'"` // 采购合同交收量【期末】
  300. SELLRECKONQTY float64 `json:"sellreckonqty" xorm:"'SELLRECKONQTY'"` // 销售合同交收量【期末】
  301. BUYCONTRACTINQTY float64 `json:"buycontractinqty" xorm:"'BUYCONTRACTINQTY'"` // 采购合同入库量【期末】
  302. SELLCONTRACTOUTQTY float64 `json:"sellcontractoutqty" xorm:"'SELLCONTRACTOUTQTY'"` // 销售合同出库量【期末】
  303. PRODUCEINQTY float64 `json:"produceinqty" xorm:"'PRODUCEINQTY'"` // 生产入库量【期末】
  304. PRODUCEOUTQTY float64 `json:"produceoutqty" xorm:"'PRODUCEOUTQTY'"` // 生产出库量【期末】
  305. CYCLETIME string `json:"cycletime" xorm:"'CYCLETIME'" form:"cycletime"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  306. CYCLETYPE int32 `json:"cycletype" xorm:"'CYCLETYPE'" form:"cycletype"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  307. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'" form:"userid" binding:"required"` // 账户所属机构
  308. ACCOUNTID int64 `json:"accountid" xorm:"'ACCOUNTID'" form:"accountid"` // 期货账户ID
  309. WRFACTORTYPEID int64 `json:"wrfactortypeid" xorm:"'WRFACTORTYPEID'"` // 仓单要素类型ID(212+Unix秒时间戳(10位)+xxxxxx)
  310. DELIVERYGOODSID int64 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'" form:"deliverygoodsid"` // 现货品种ID
  311. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'" form:"wrstandardid"` // 品类ID
  312. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'" form:"spotgoodsbrandid"` // 现货品牌ID
  313. ORIBUYPRICEDQTY float64 `json:"oribuypricedqty" xorm:"'ORIBUYPRICEDQTY'"` // 期初采购合同已定价数量【期初】
  314. ORISELLPRICEDQTY float64 `json:"orisellpricedqty" xorm:"'ORISELLPRICEDQTY'"` // 期初销售合同已定价数量【期初】
  315. BUYPRICEDQTY float64 `json:"buypricedqty" xorm:"'BUYPRICEDQTY'"` // 采购合同已定价数量【期末】
  316. SELLPRICEDQTY float64 `json:"sellpricedqty" xorm:"'SELLPRICEDQTY'"` // 销售合同已定价数量【期末】
  317. TOTALSPOTQTY float64 `json:"totalspotqty" xorm:"'TOTALSPOTQTY'"` // 现货头寸总量 = 采购合同已定价数量 - 销售合同已定价数量【期末】
  318. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  319. TODAYBUYRECKONQTY float64 `json:"todaybuyreckonqty" xorm:"'TODAYBUYRECKONQTY'"` // 今日采购合同已确定数量【汇总】
  320. TODAYSELLRECKONQTY float64 `json:"todaysellreckonqty" xorm:"'TODAYSELLRECKONQTY'"` // 今日销售合同已确定数量【汇总】
  321. ORIBUYPRICEDAMOUNT float64 `json:"oribuypricedamount" xorm:"'ORIBUYPRICEDAMOUNT'"` // 期初采购合同已定价金额【期初】
  322. ORISELLPRICEDAMOUNT float64 `json:"orisellpricedamount" xorm:"'ORISELLPRICEDAMOUNT'"` // 期初销售合同已定价金额【期初】
  323. BUYPRICEDAMOUNT float64 `json:"buypricedamount" xorm:"'BUYPRICEDAMOUNT'"` // 采购合同已定价金额【期末】
  324. SELLPRICEDAMOUNT float64 `json:"sellpricedamount" xorm:"'SELLPRICEDAMOUNT'"` // 销售合同已定价金额【期末】
  325. ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 现货商品单位名称
  326. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'wrstandardname'"` // 品类名称
  327. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'wrstandardcode'"` // 品类代码
  328. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'deliverygoodscode'"` // 现货商品代码
  329. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'deliverygoodsname'"` // 现货商品名称
  330. GOODSUNITID int32 `json:"goodsunitid" xorm:"'goodsunitid'"` // 现货商品单位id
  331. BRANDNAME string `json:"brandname" xorm:"'brandname'"` // 品牌名称
  332. ACCOUNTNAME string `json:"accountname" xorm:"'accountname'"` // 所属机构名称
  333. BIZTYPE int32 `json:"biztype" xorm:"'BIZTYPE'"` // 业务类型 1-套保 2-套利
  334. CURRENCYID int32 `json:"currencyid" xorm:"'CURRENCYID'"` // 币种id
  335. CURRENCYNAME string `json:"currencyname"` // 币种名称
  336. TOTALBUYPRICEDQTY float64 `json:"totalbuypricedqty"` // 采购定价量 = 期末 - 期初
  337. TOTALSELLPRICEDQTY float64 `json:"totalsellpricedqty"` // 销售定价量 = 期末 - 期初
  338. }
  339. func (r *Ermcp3ReportAreaSpotSub) calc() {
  340. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  341. r.TOTALBUYPRICEDQTY = r.BUYPRICEDQTY - r.ORIBUYPRICEDQTY
  342. r.TOTALSELLPRICEDQTY = r.SELLPRICEDQTY - r.ORISELLPRICEDQTY
  343. }
  344. func (r *Ermcp3ReportAreaSpotSub) buildSql() string {
  345. var sqlId utils.SQLVal = "SELECT t.ORIBUYRECKONQTY," +
  346. " t.ORISELLRECKONQTY," +
  347. " t.ORIBUYCONTRACTINQTY," +
  348. " t.ORISELLCONTRACTOUTQTY," +
  349. " t.ORIPRODUCEINQTY," +
  350. " t.ORIPRODUCEOUTQTY," +
  351. " t.BUYRECKONQTY," +
  352. " t.SELLRECKONQTY," +
  353. " t.BUYCONTRACTINQTY," +
  354. " t.SELLCONTRACTOUTQTY," +
  355. " t.PRODUCEINQTY," +
  356. " t.PRODUCEOUTQTY," +
  357. " t.CYCLETIME," +
  358. " t.CYCLETYPE," +
  359. " t.AREAUSERID," +
  360. " t.ACCOUNTID," +
  361. " t.WRFACTORTYPEID," +
  362. " t.DELIVERYGOODSID," +
  363. " t.WRSTANDARDID," +
  364. " t.SPOTGOODSBRANDID," +
  365. " t.ORIBUYPRICEDQTY," +
  366. " t.ORISELLPRICEDQTY," +
  367. " t.BUYPRICEDQTY," +
  368. " t.SELLPRICEDQTY," +
  369. " t.TOTALSPOTQTY," +
  370. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  371. " t.TODAYBUYRECKONQTY," +
  372. " t.TODAYSELLRECKONQTY," +
  373. " t.ORIBUYPRICEDAMOUNT," +
  374. " t.ORISELLPRICEDAMOUNT," +
  375. " t.BUYPRICEDAMOUNT," +
  376. " t.SELLPRICEDAMOUNT," +
  377. " t.currencyid," +
  378. " t.biztype," +
  379. " w.wrstandardname," +
  380. " w.wrstandardcode," +
  381. " g.deliverygoodscode," +
  382. " g.deliverygoodsname," +
  383. " g.goodsunitid," +
  384. " d.dgfactoryitemvalue brandname," +
  385. " u.accountname," +
  386. " e.enumdicname" +
  387. " FROM REPORT_ERMCP_AREASPOTSUB t" +
  388. " LEFT JOIN WRSTANDARD w on t.wrstandardid=w.wrstandardid" +
  389. " LEFT JOIN DELIVERYGOODS g on t.deliverygoodsid=g.deliverygoodsid" +
  390. " LEFT JOIN DGFACTORYITEM d on t.spotgoodsbrandid=d.dgfactoryitemid" +
  391. " LEFT JOIN USERACCOUNT u on t.areauserid=u.userid" +
  392. " LEFT JOIN ENUMDICITEM e on g.goodsunitid=e.enumitemname and e.enumdiccode='goodsunit' and e.enumitemstatus=1" +
  393. " WHERE 1 = 1"
  394. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  395. sqlId.And("t.cycletype", r.CYCLETYPE)
  396. sqlId.And("t.cycletime", r.CYCLETIME)
  397. sqlId.AndEx("t.ACCOUNTID", r.ACCOUNTID, r.ACCOUNTID > 0)
  398. sqlId.AndEx("t.deliverygoodsid", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  399. sqlId.AndEx("t.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  400. sqlId.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  401. return sqlId.String()
  402. }
  403. // GetDataEx 获取现货分类报表
  404. func (r *Ermcp3ReportAreaSpotSub) GetDataEx() (interface{}, error) {
  405. sData := make([]Ermcp3ReportAreaSpotSub, 0)
  406. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  407. for i := range sData {
  408. sData[i].calc()
  409. }
  410. return sData, err
  411. }
  412. // Ermcp3ExpourseReport 敞口报表
  413. type Ermcp3ExpourseReport struct {
  414. RECKONDATE string `json:"reckondate" xorm:"'RECKONDATE'"` // 日照时期(yyyyMMdd)
  415. CYCLETIME string `json:"cycletime" xorm:"'CYCLETIME'"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  416. CYCLETYPE int32 `json:"cycletype" xorm:"'CYCLETYPE'"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  417. ORITOTALSPOTQTY float64 `json:"oritotalspotqty" xorm:"'ORITOTALSPOTQTY'"` // 期初现货头寸总量
  418. ORITOTALFUTUREQTY float64 `json:"oritotalfutureqty" xorm:"'ORITOTALFUTUREQTY'"` // 期初期货头寸总量
  419. ORITOTALEXPOSURE float64 `json:"oritotalexposure" xorm:"'ORITOTALEXPOSURE'"` // 期初实时总敞口
  420. ORINEEDHEDGEEXPOSOURE float64 `json:"orineedhedgeexposoure" xorm:"'ORINEEDHEDGEEXPOSOURE'"` // 期初应套保敞口
  421. ORIHEDGEQTY float64 `json:"orihedgeqty" xorm:"'ORIHEDGEQTY'"` // 期初套保量
  422. ORIARBITRAGEQTY float64 `json:"oriarbitrageqty" xorm:"'ORIARBITRAGEQTY'"` // 期初套利量
  423. ORINEEDHEDGEQTY float64 `json:"orineedhedgeqty" xorm:"'ORINEEDHEDGEQTY'"` // 期初应套保量
  424. ORINEEDARBITRAGEQTY float64 `json:"orineedarbitrageqty" xorm:"'ORINEEDARBITRAGEQTY'"` // 期初应套利量
  425. ORITOTALNEEDHEDGEQTY float64 `json:"oritotalneedhedgeqty" xorm:"'ORITOTALNEEDHEDGEQTY'"` // 期初应套保总量
  426. MIDDLEGOODSID int64 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'"` // 套保品种ID
  427. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构\交易用户ID
  428. ACCOUNTID int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 期货账户ID (作废, 默认为0)
  429. ORIBUYPLANQTY float64 `json:"oribuyplanqty" xorm:"'ORIBUYPLANQTY'"` // 期初采购计划数量
  430. ORIBUYPRICEDQTY float64 `json:"oribuypricedqty" xorm:"'ORIBUYPRICEDQTY'"` // 期初采购合同已定价数量
  431. ORISELLPLANQTY float64 `json:"orisellplanqty" xorm:"'ORISELLPLANQTY'"` // 期初销售计划数量
  432. ORISELLPRICEDQTY float64 `json:"orisellpricedqty" xorm:"'ORISELLPRICEDQTY'"` // 期初销售合同已定价数量
  433. ORIBUYFUTUREQTY float64 `json:"oribuyfutureqty" xorm:"'ORIBUYFUTUREQTY'"` // 期初买入期货数量
  434. ORISELLFUTUREQTY float64 `json:"orisellfutureqty" xorm:"'ORISELLFUTUREQTY'"` // 期初卖出期货数量
  435. BUYPLANQTY float64 `json:"buyplanqty" xorm:"'BUYPLANQTY'"` // 采购计划数量
  436. BUYPRICEDQTY float64 `json:"buypricedqty" xorm:"'BUYPRICEDQTY'"` // 采购合同已定价数量
  437. SELLPLANQTY float64 `json:"sellplanqty" xorm:"'SELLPLANQTY'"` // 销售计划数量
  438. SELLPRICEDQTY float64 `json:"sellpricedqty" xorm:"'SELLPRICEDQTY'"` // 销售合同已定价数量
  439. BUYFUTUREQTY float64 `json:"buyfutureqty" xorm:"'BUYFUTUREQTY'"` // 买入期货数量
  440. SELLFUTUREQTY float64 `json:"sellfutureqty" xorm:"'SELLFUTUREQTY'"` // 卖出期货数量
  441. TOTALSPOTQTY float64 `json:"totalspotqty" xorm:"'TOTALSPOTQTY'"` // 现货头寸总量
  442. TOTALFUTUREQTY float64 `json:"totalfutureqty" xorm:"'TOTALFUTUREQTY'"` // 期货头寸总量(期货总量)
  443. TOTALEXPOSURE float64 `json:"totalexposure" xorm:"'TOTALEXPOSURE'"` // 实时总敞口(总敞口)
  444. TOTALHEDGERATIO float64 `json:"totalhedgeratio" xorm:"'TOTALHEDGERATIO'"` // 敞口比例
  445. TOTALNEEDHEDGEQTY float64 `json:"totalneedhedgeqty" xorm:"'TOTALNEEDHEDGEQTY'"` // 应套保总量(现货应套保总量)
  446. NEEDHEDGEEXPOSOURE float64 `json:"needhedgeexposoure" xorm:"'NEEDHEDGEEXPOSOURE'"` // 应套保敞口(套保敞口)
  447. NEEDHEDGERATIO float64 `json:"needhedgeratio" xorm:"'NEEDHEDGERATIO'"` // 应套保敞口比例
  448. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  449. HEDGEQTY float64 `json:"hedgeqty" xorm:"'HEDGEQTY'"` // 套保量
  450. ARBITRAGEQTY float64 `json:"arbitrageqty" xorm:"'ARBITRAGEQTY'"` // 套利量
  451. NEEDHEDGEQTY float64 `json:"needhedgeqty" xorm:"'NEEDHEDGEQTY'"` // 应套保量
  452. NEEDARBITRAGEQTY float64 `json:"needarbitrageqty" xorm:"'NEEDARBITRAGEQTY'"` // 应套利量
  453. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保商品名称
  454. MIDDLEGOODSCODE string `json:"middlgoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保商品代码
  455. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 单位id
  456. MGNEEDHEDGERATIO float64 `json:"mgneedhedgeratio" xorm:"'MGNEEDHEDGERATIO'"` // 套保比例(套保品的)
  457. NEEDARBITRAGERATIO float64 `json:"needarbitrageratio" xorm:"'NEEDARBITRAGERATIO'"` // 套利比例(套保品的)
  458. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  459. ACCOUNTNAME string `json:"accountname"` // 机构名称
  460. DiffSpotQty float64 `json:"diffspotqty"` // 变动量(现货)
  461. DiffMgQtyA float64 `json:"diffmgqtya"` // 套保变动量
  462. DiffMgQtyB float64 `json:"diffmgqtyb"` // 套利变动量
  463. DiffFutuQty float64 `json:"difffutuqty"` // 变动量(期货)
  464. DiffQty float64 `json:"diffqty"` // 变动量(总敞口)
  465. DiffHedgeQty float64 `json:"diffhedgeqty"` // 变动量(现货应套保总量)
  466. DiffExposure float64 `json:"diffexposure"` // 变动量(套保敞口)
  467. QueryType int32 `json:"-"` // 查询类型 1-日报表 2-周期报表 3-日报表(范围,[开始日期,结束日期])
  468. BeginDate string `json:"-"` // 开始交易日
  469. EndDate string `json:"-"` // 结束交易日
  470. }
  471. func (r *Ermcp3ExpourseReport) calc() {
  472. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  473. r.ACCOUNTNAME = mtpcache.GetUserNameByUserId(r.AREAUSERID)
  474. // 变动量(现货) = 现货头寸总量 - 期初现货头寸总量
  475. r.DiffSpotQty = r.TOTALSPOTQTY - r.ORITOTALSPOTQTY
  476. // 套保变动量 = 套保量 - 期初套保量
  477. r.DiffMgQtyA = r.HEDGEQTY - r.ORIHEDGEQTY
  478. // 套利变动量 = 套利量 - 期初套利量
  479. r.DiffMgQtyB = r.ARBITRAGEQTY - r.ORIARBITRAGEQTY
  480. // 变动量(期货) = (买入 - 买入期初) - (卖出 - 卖出期初)
  481. r.DiffFutuQty = (r.BUYFUTUREQTY - r.ORIBUYFUTUREQTY) - (r.SELLFUTUREQTY - r.ORISELLFUTUREQTY)
  482. // 变动量(敞口) = 实时敞口 - 期初实时敞口
  483. r.DiffQty = r.TOTALEXPOSURE - r.ORITOTALEXPOSURE
  484. // 变动量(现货应套保总量)
  485. r.DiffHedgeQty = r.TOTALNEEDHEDGEQTY - r.ORITOTALNEEDHEDGEQTY
  486. // 变动量(套保敞口)
  487. r.DiffExposure = r.NEEDHEDGEEXPOSOURE - r.ORINEEDHEDGEEXPOSOURE
  488. }
  489. func (r *Ermcp3ExpourseReport) buildSql() string {
  490. if r.QueryType == 1 || r.QueryType == 3 {
  491. return r.buildSqlDay()
  492. }
  493. return r.buildSqlCycle()
  494. }
  495. func (r *Ermcp3ExpourseReport) buildSqlDay() string {
  496. var sqlId utils.SQLVal = "SELECT t.ORITOTALSPOTQTY," +
  497. " t.ORITOTALFUTUREQTY," +
  498. " t.ORITOTALEXPOSURE," +
  499. " t.ORINEEDHEDGEEXPOSOURE," +
  500. " t.ORIHEDGEQTY," +
  501. " t.ORIARBITRAGEQTY," +
  502. " t.ORINEEDHEDGEQTY," +
  503. " t.ORINEEDARBITRAGEQTY," +
  504. " t.ORITOTALNEEDHEDGEQTY," +
  505. " t.RECKONDATE," +
  506. " t.MIDDLEGOODSID," +
  507. " t.AREAUSERID," +
  508. " t.ACCOUNTID," +
  509. " t.ORIBUYPLANQTY," +
  510. " t.ORIBUYPRICEDQTY," +
  511. " t.ORISELLPLANQTY," +
  512. " t.ORISELLPRICEDQTY," +
  513. " t.ORIBUYFUTUREQTY," +
  514. " t.ORISELLFUTUREQTY," +
  515. " t.BUYPLANQTY," +
  516. " t.BUYPRICEDQTY," +
  517. " t.SELLPLANQTY," +
  518. " t.SELLPRICEDQTY," +
  519. " t.BUYFUTUREQTY," +
  520. " t.SELLFUTUREQTY," +
  521. " t.TOTALSPOTQTY," +
  522. " t.TOTALFUTUREQTY," +
  523. " t.TOTALEXPOSURE," +
  524. " t.TOTALHEDGERATIO," +
  525. " t.TOTALNEEDHEDGEQTY," +
  526. " t.NEEDHEDGEEXPOSOURE," +
  527. " t.NEEDHEDGERATIO," +
  528. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  529. " t.HEDGEQTY," +
  530. " t.ARBITRAGEQTY," +
  531. " t.NEEDHEDGEQTY," +
  532. " t.NEEDARBITRAGEQTY," +
  533. " g.middlegoodsname," +
  534. " g.middlegoodscode," +
  535. " g.goodsunitid unitid" +
  536. " FROM RECKON_ERMCP_AREAEXPOSURE t" +
  537. " LEFT JOIN ERMS_MIDDLEGOODS g" +
  538. " on t.middlegoodsid = g.middlegoodsid" +
  539. " WHERE 1 = 1"
  540. sqlId.AndEx("t.AREAUSERID", r.AREAUSERID, r.AREAUSERID > 0)
  541. if r.QueryType == 1 {
  542. sqlId.And("t.RECKONDATE", r.RECKONDATE)
  543. } else if r.QueryType == 3 {
  544. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  545. }
  546. return sqlId.String()
  547. }
  548. func (r *Ermcp3ExpourseReport) buildSqlCycle() string {
  549. var sqlId utils.SQLVal = "SELECT t.cycletime," +
  550. " t.cycletype," +
  551. " t.ORITOTALSPOTQTY," +
  552. " t.ORITOTALFUTUREQTY," +
  553. " t.ORITOTALEXPOSURE," +
  554. " t.ORINEEDHEDGEEXPOSOURE," +
  555. " t.ORIHEDGEQTY," +
  556. " t.ORIARBITRAGEQTY," +
  557. " t.ORINEEDHEDGEQTY," +
  558. " t.ORINEEDARBITRAGEQTY," +
  559. " t.ORITOTALNEEDHEDGEQTY," +
  560. " t.MIDDLEGOODSID," +
  561. " t.AREAUSERID," +
  562. " t.ACCOUNTID," +
  563. " t.ORIBUYPLANQTY," +
  564. " t.ORIBUYPRICEDQTY," +
  565. " t.ORISELLPLANQTY," +
  566. " t.ORISELLPRICEDQTY," +
  567. " t.ORIBUYFUTUREQTY," +
  568. " t.ORISELLFUTUREQTY," +
  569. " t.BUYPLANQTY," +
  570. " t.BUYPRICEDQTY," +
  571. " t.SELLPLANQTY," +
  572. " t.SELLPRICEDQTY," +
  573. " t.BUYFUTUREQTY," +
  574. " t.SELLFUTUREQTY," +
  575. " t.TOTALSPOTQTY," +
  576. " t.TOTALFUTUREQTY," +
  577. " t.TOTALEXPOSURE," +
  578. " t.TOTALHEDGERATIO," +
  579. " t.TOTALNEEDHEDGEQTY," +
  580. " t.NEEDHEDGEEXPOSOURE," +
  581. " t.NEEDHEDGERATIO," +
  582. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  583. " t.HEDGEQTY," +
  584. " t.ARBITRAGEQTY," +
  585. " t.NEEDHEDGEQTY," +
  586. " t.NEEDARBITRAGEQTY," +
  587. " g.middlegoodsname," +
  588. " g.middlegoodscode," +
  589. " g.goodsunitid unitid," +
  590. " g.needhedgeratio," +
  591. " g.needarbitrageratio" +
  592. " FROM REPORT_ERMCP_AREAEXPOSURE t" +
  593. " LEFT JOIN ERMS_MIDDLEGOODS g" +
  594. " on t.middlegoodsid = g.middlegoodsid" +
  595. " WHERE 1 = 1"
  596. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  597. sqlId.And("t.CYCLETYPE", r.CYCLETYPE)
  598. sqlId.And("t.CYCLETIME", r.CYCLETIME)
  599. return sqlId.String()
  600. }
  601. // GetDataEx 获取敞报表
  602. func (r *Ermcp3ExpourseReport) GetDataEx() (interface{}, error) {
  603. sData := make([]Ermcp3ExpourseReport, 0)
  604. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  605. for i := range sData {
  606. sData[i].calc()
  607. }
  608. return sData, err
  609. }
  610. // Ermcp3ExposureContractDetail 敞口合同明细
  611. type Ermcp3ExposureContractDetail struct {
  612. MIDDLEGOODSID int32 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'"` // 套保品种id
  613. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` //现货商品id
  614. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 折算系数(套保品种)
  615. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构id
  616. SPOTCONTRACTID string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` // 合同id
  617. CONTRACTNO string `json:"contractno" xorm:"'CONTRACTNO'"` // 合同编号
  618. CONTRACTTYPE int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 现货合同类型 - 1:采购 -1:销售
  619. BIZTYPE int32 `json:"biztype" xorm:"'BIZTYPE'"` // 业务类型 1-套保 2-套利
  620. BUYUSERID int64 `json:"buyuserid" xorm:"'BUYUSERID'"` // 采购方id
  621. SELLUSERID int64 `json:"selluserid" xorm:"'SELLUSERID'"` // 销售方id
  622. QTY float64 `json:"qty" xorm:"'QTY'"` // 合同量
  623. PRICEDQTY float64 `json:"pricedqty" xorm:"'PRICEDQTY'"` // 已定价量
  624. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类id
  625. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  626. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  627. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 品类单位id
  628. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保品种名称
  629. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保品种代码
  630. GOODSUNITID int32 `json:"-" xorm:"'GOODSUNITID'"` // 套保品种单位id
  631. TRADEDATE string `json:"tradedate" xorm:"'tradedate'"` // 交易日
  632. NEEDHEDGERATIO float64 `json:"needhedgeratio" xorm:"'NEEDHEDGERATIO'"` // 应套保比例
  633. NEEDARBITRAGERATIO float64 `json:"needarbitrageratio" xorm:"'NEEDARBITRAGERATIO'"` // 应套利比例
  634. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  635. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货商品代码
  636. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货商品名称
  637. EnumdicName string `json:"enumdicname"` // 单位名称
  638. BUYUSERNAME string `json:"buyusername"` // 采购方名称
  639. SELLUSERNAME string `json:"sellusername"` // 销售方名称
  640. CurQty float64 `json:"curqty"` // 今定价量
  641. DiffQty float64 `json:"diffqty"` // 套保品种今变动量
  642. }
  643. func (r *Ermcp3ExposureContractDetail) calc() {
  644. r.CurQty = r.QTY
  645. r.DiffQty = r.QTY * r.CONVERTRATIO
  646. r.EnumdicName = mtpcache.GetEnumDicitemName(r.UNITID)
  647. r.BUYUSERNAME = mtpcache.GetUserNameByUserId(r.BUYUSERID)
  648. r.SELLUSERNAME = mtpcache.GetUserNameByUserId(r.SELLUSERID)
  649. }
  650. func (r *Ermcp3ExposureContractDetail) buildSql() string {
  651. var sqlId utils.SQLVal = "select t.middlegoodsid," +
  652. " t.deliverygoodsid," +
  653. " t.convertratio," +
  654. " s.userid," +
  655. " to_char(s.spotcontractid) spotcontractid," +
  656. " s.contractno," +
  657. " s.contracttype," +
  658. " s.biztype," +
  659. " s.buyuserid," +
  660. " s.selluserid," +
  661. " s.qty," +
  662. " s.pricedqty," +
  663. " s.wrstandardid," +
  664. " s.audittradedate tradedate," +
  665. " w.wrstandardname," +
  666. " w.wrstandardcode," +
  667. " w.unitid," +
  668. " mg.middlegoodsname," +
  669. " mg.middlegoodscode," +
  670. " mg.goodsunitid," +
  671. " mg.needhedgeratio," +
  672. " mg.needarbitrageratio," +
  673. " dg.dgfactoryitemid brandid," +
  674. " dg.dgfactoryitemvalue brandName," +
  675. " g.deliverygoodscode," +
  676. " g.deliverygoodsname" +
  677. " from erms2_wrsconvertdetail t" +
  678. " inner join ermcp_spotcontract s" +
  679. " on t.deliverygoodsid = s.deliverygoodsid" +
  680. " left join erms_middlegoods mg" +
  681. " on t.middlegoodsid = mg.middlegoodsid" +
  682. " left join wrstandard w" +
  683. " on s.wrstandardid = w.wrstandardid" +
  684. " left join dgfactoryitem dg" +
  685. " on s.spotgoodsbrandid = dg.dgfactoryitemid" +
  686. " left join deliverygoods g on s.deliverygoodsid=g.deliverygoodsid" +
  687. " where t.wrstandardid = 0" +
  688. " and s.contractstatus in (2, 3)"
  689. sqlId.And("s.audittradedate", r.TRADEDATE)
  690. sqlId.And("t.middlegoodsid", r.MIDDLEGOODSID)
  691. sqlId.Join(fmt.Sprintf(" and %v in(s.userid, s.tradeuserid)", r.USERID))
  692. return sqlId.String()
  693. }
  694. // GetDataEx 获取敞口合同明细
  695. func (r *Ermcp3ExposureContractDetail) GetDataEx() (interface{}, error) {
  696. sData := make([]Ermcp3ExposureContractDetail, 0)
  697. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  698. for i := range sData {
  699. sData[i].calc()
  700. }
  701. return sData, err
  702. }
  703. // Ermcp3ExposureHedgeplanDetail 敞口套保计划明细
  704. type Ermcp3ExposureHedgeplanDetail struct {
  705. MIDDLEGOODSID int32 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'"` // 套保品种id
  706. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` //现货商品id
  707. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 折算系数(套保品种)
  708. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构id
  709. HEDGEPLANID string `json:"hedgeplanid" xorm:"'HEDGEPLANID'"` // 套保计划id
  710. HEDGEPLANNO string `json:"hedgeplanno" xorm:"'HEDGEPLANNO'"` // 套保计划编号
  711. BIZTYPE int32 `json:"biztype" xorm:"'BIZTYPE'"` // 业务类型 1-套保 2-套利
  712. QTY float64 `json:"qty" xorm:"'QTY'"` // 合同量
  713. PRICEDQTY float64 `json:"pricedqty" xorm:"'PRICEDQTY'"` // 已定价量
  714. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类id
  715. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  716. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  717. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 品类单位id
  718. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保品种名称
  719. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保品种代码
  720. GOODSUNITID int32 `json:"-" xorm:"'GOODSUNITID'"` // 套保品种单位id
  721. TRADEDATE string `json:"tradedate" xorm:"'tradedate'"` // 交易日(登记日)
  722. NEEDHEDGERATIO float64 `json:"needhedgeratio" xorm:"'NEEDHEDGERATIO'"` // 应套保比例
  723. NEEDARBITRAGERATIO float64 `json:"needarbitrageratio" xorm:"'NEEDARBITRAGERATIO'"` // 应套利比例
  724. CONTRACTTYPE int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 计划类型 - 1:采购 -1:销售
  725. HEDGEPLANSTATUS int32 `json:"hedgeplanstatus" xorm:"'HEDGEPLANSTATUS'"` // 套保计划状态 - 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
  726. EnumdicName string `json:"enumdicname"` // 单位名称(品类)
  727. MGUNITIDNAME string `json:"mgunitidname"` // 单位名称(套保商品)
  728. CurQty float64 `json:"-"` // 今定价量
  729. DiffQty float64 `json:"diffqty"` // 套保品种今变动量
  730. ChangQty float64 `json:"changqty"` // 应套保总量变化量
  731. }
  732. func (r *Ermcp3ExposureHedgeplanDetail) calc() {
  733. r.EnumdicName = mtpcache.GetEnumDicitemName(r.UNITID)
  734. r.MGUNITIDNAME = mtpcache.GetEnumDicitemName(r.GOODSUNITID)
  735. r.CurQty = r.QTY
  736. r.DiffQty = r.QTY * r.CONVERTRATIO
  737. r.ChangQty = r.QTY * r.NEEDHEDGERATIO * r.CONVERTRATIO
  738. }
  739. func (r *Ermcp3ExposureHedgeplanDetail) buildSql() string {
  740. var sqlId utils.SQLVal = "select t.middlegoodsid," +
  741. " t.deliverygoodsid," +
  742. " t.convertratio," +
  743. " s.areauserid userid," +
  744. " to_char(s.hedgeplanid) hedgeplanid," +
  745. " s.hedgeplanno," +
  746. " s.contracttype," +
  747. " s.biztype," +
  748. " s.planqty qty," +
  749. " s.wrstandardid," +
  750. " s.audittradedate tradedate," +
  751. " s.hedgeplanstatus," +
  752. " w.wrstandardname," +
  753. " w.wrstandardcode," +
  754. " w.unitid," +
  755. " mg.middlegoodsname," +
  756. " mg.middlegoodscode," +
  757. " mg.goodsunitid," +
  758. " mg.needhedgeratio," +
  759. " mg.needarbitrageratio" +
  760. " from erms2_wrsconvertdetail t" +
  761. " inner join ermcp_hedgeplan s" +
  762. " on t.deliverygoodsid = s.deliverygoodsid" +
  763. " left join erms_middlegoods mg" +
  764. " on t.middlegoodsid = mg.middlegoodsid" +
  765. " left join wrstandard w" +
  766. " on s.wrstandardid = w.wrstandardid" +
  767. " where t.wrstandardid = 0 and s.hedgeplanstatus in(2,3)"
  768. sqlId.And("s.audittradedate", r.TRADEDATE)
  769. sqlId.And("t.middlegoodsid", r.MIDDLEGOODSID)
  770. sqlId.Join(fmt.Sprintf(" and %v in(s.areauserid, s.tradeuserid)", r.USERID))
  771. return sqlId.String()
  772. }
  773. // GetDataEx 获取敞口套保计划明细
  774. func (r *Ermcp3ExposureHedgeplanDetail) GetDataEx() (interface{}, error) {
  775. sData := make([]Ermcp3ExposureHedgeplanDetail, 0)
  776. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  777. for i := range sData {
  778. sData[i].calc()
  779. }
  780. return sData, err
  781. }
  782. // Ermcp3AreaSpotPLReport 现货报表(日/月/周/季/年)
  783. type Ermcp3AreaSpotPLReport struct {
  784. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照时期(yyyyMMdd)
  785. CYCLETIME string `json:"cycletime" xorm:"'CYCLETIME'"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  786. CYCLETYPE int32 `json:"cycletype" xorm:"'CYCLETYPE'"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  787. AREAUSERID int64 `json:"areauserid" xorm:"AREAUSERID"` // 所属机构\交易用户ID
  788. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID"` // 期货账户ID (作废, 默认为0)
  789. WRFACTORTYPEID int64 `json:"wrfactortypeid" xorm:"WRFACTORTYPEID"` // 仓单要素类型ID(212+Unix秒时间戳(10位)+xxxxxx)
  790. CURRENCYID int32 `json:"currencyid" xorm:"CURRENCYID"` // 结算币种ID【原值】
  791. BIZTYPE int32 `json:"biztype" xorm:"BIZTYPE"` // 业务类型 - 1:套保 2:套利
  792. DELIVERYGOODSID int64 `json:"deliverygoodsid" xorm:"DELIVERYGOODSID"` // 现货品种ID
  793. WRSTANDARDID string `json:"wrstandardid" xorm:"WRSTANDARDID"` // 现货品类ID
  794. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"SPOTGOODSBRANDID"` // 现货品牌ID
  795. ORIBUYQTY float64 `json:"oribuyqty" xorm:"ORIBUYQTY"` // 期初采购总量
  796. ORIBUYAMOUNT float64 `json:"oribuyamount" xorm:"ORIBUYAMOUNT"` // 期初采购总额
  797. ORISELLQTY float64 `json:"orisellqty" xorm:"ORISELLQTY"` // 期初销售总量
  798. ORISELLAMOUNT float64 `json:"orisellamount" xorm:"ORISELLAMOUNT"` // 期初销售总额
  799. ORIQTY float64 `json:"oriqty" xorm:"ORIQTY"` // 期初量
  800. ORIAVERAGEPRICE float64 `json:"oriaverageprice" xorm:"ORIAVERAGEPRICE"` // 期初均价
  801. ORIAMOUNT float64 `json:"oriamount" xorm:"ORIAMOUNT"` // 期初额
  802. TODAYBUYQTY float64 `json:"todaybuyqty" xorm:"TODAYBUYQTY"` // 今日采购量(今采购量)
  803. TODAYBUYAMOUNT float64 `json:"todaybuyamount" xorm:"TODAYBUYAMOUNT"` // 今日采购额(今采购额)
  804. TODAYBUYAVERAGEPRICE float64 `json:"todaybuyaverageprice" xorm:"TODAYBUYAVERAGEPRICE"` // 今日采购均价
  805. TODAYSELLQTY float64 `json:"todaysellqty" xorm:"TODAYSELLQTY"` // 今日销售量(今销售量)
  806. TODAYSELLAMOUNT float64 `json:"todaysellamount" xorm:"TODAYSELLAMOUNT"` // 今日销售额(今销售额)
  807. TODAYSELLAVERAGEPRICE float64 `json:"todaysellaverageprice" xorm:"TODAYSELLAVERAGEPRICE"` // 今日销售均价
  808. CURBUYQTY float64 `json:"curbuyqty" xorm:"CURBUYQTY"` // 期末采购总量
  809. CURBUYAMOUNT float64 `json:"curbuyamount" xorm:"CURBUYAMOUNT"` // 期末采购总额
  810. CURSELLQTY float64 `json:"cursellqty" xorm:"CURSELLQTY"` // 期末销售总量
  811. CURSELLAMOUNT float64 `json:"cursellamount" xorm:"CURSELLAMOUNT"` // 期末销售总额
  812. CURQTY float64 `json:"curqty" xorm:"CURQTY"` // 期末量
  813. CURAVERAGEPRICE float64 `json:"curaverageprice" xorm:"CURAVERAGEPRICE"` // 期末均价
  814. CURAMOUNT float64 `json:"curamount" xorm:"CURAMOUNT"` // 期末额
  815. CURSPOTPRICE float64 `json:"curspotprice" xorm:"CURSPOTPRICE"` // 参考市价(最新价)
  816. CURMARKETVALUE float64 `json:"curmarketvalue" xorm:"CURMARKETVALUE"` // 参考市值(期末市值)
  817. ACTUALPL float64 `json:"actualpl" xorm:"ACTUALPL"` // 现货损益
  818. FLOATPL float64 `json:"floatpl" xorm:"FLOATPL"` // 浮动损益
  819. UPDATETIME string `json:"updatetime" xorm:"UPDATETIME"` // 更新时间
  820. TODAYINQTY float64 `json:"todayinqty" xorm:"TODAYINQTY"` // 今日入库量(今入库量)
  821. TODAYOUTQTY float64 `json:"todayoutqty" xorm:"TODAYOUTQTY"` // 今日出库量(今出库量)
  822. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货商品代码
  823. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货商品名称
  824. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODSUNITID'"` // 现货商品单位id
  825. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  826. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  827. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 品类单位id
  828. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  829. EnumdicName string `json:"enumdicname"` // 现货商品单位名称
  830. UNITIDNAME string `json:"unitidname"` // 品类单位名称
  831. ACCOUNTNAME string `json:"accountname"` // 机构名称
  832. CURRENCYNAME string `json:"currencyname"` // 币种名称
  833. QueryType int32 `json:"-"` // 查询类型 1-日报表 2-周期报表 3-日报表(范围,[开始日期,结束日期])
  834. BeginDate string `json:"-"` // 开始交易日
  835. EndDate string `json:"-"` // 结束交易日
  836. }
  837. func (r *Ermcp3AreaSpotPLReport) calc() {
  838. r.EnumdicName = mtpcache.GetEnumDicitemName(r.GOODSUNITID)
  839. r.UNITIDNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  840. r.ACCOUNTNAME = mtpcache.GetUserNameByUserId(r.AREAUSERID)
  841. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  842. }
  843. func (r *Ermcp3AreaSpotPLReport) buildSql() string {
  844. if r.QueryType == 1 || r.QueryType == 3 {
  845. return r.buildSqlDay()
  846. }
  847. return r.buildSqlCycle()
  848. }
  849. // buildSqlDay 日报表查询语句
  850. func (r *Ermcp3AreaSpotPLReport) buildSqlDay() string {
  851. var sqlId utils.SQLVal = "SELECT t.RECKONDATE," +
  852. " t.AREAUSERID," +
  853. " t.ACCOUNTID," +
  854. " t.WRFACTORTYPEID," +
  855. " t.CURRENCYID," +
  856. " t.BIZTYPE," +
  857. " t.DELIVERYGOODSID," +
  858. " t.WRSTANDARDID," +
  859. " t.SPOTGOODSBRANDID," +
  860. " t.ORIBUYQTY," +
  861. " t.ORIBUYAMOUNT," +
  862. " t.ORISELLQTY," +
  863. " t.ORISELLAMOUNT," +
  864. " t.ORIQTY," +
  865. " t.ORIAVERAGEPRICE," +
  866. " t.ORIAMOUNT," +
  867. " t.TODAYBUYQTY," +
  868. " t.TODAYBUYAMOUNT," +
  869. " t.TODAYBUYAVERAGEPRICE," +
  870. " t.TODAYSELLQTY," +
  871. " t.TODAYSELLAMOUNT," +
  872. " t.TODAYSELLAVERAGEPRICE," +
  873. " t.CURBUYQTY," +
  874. " t.CURBUYAMOUNT," +
  875. " t.CURSELLQTY," +
  876. " t.CURSELLAMOUNT," +
  877. " t.CURQTY," +
  878. " t.CURAVERAGEPRICE," +
  879. " t.CURAMOUNT," +
  880. " t.CURSPOTPRICE," +
  881. " t.CURMARKETVALUE," +
  882. " t.ACTUALPL," +
  883. " t.FLOATPL," +
  884. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  885. " t.TODAYINQTY," +
  886. " t.TODAYOUTQTY," +
  887. " g.deliverygoodscode," +
  888. " g.deliverygoodsname," +
  889. " g.goodsunitid," +
  890. " w.wrstandardcode," +
  891. " w.wrstandardname," +
  892. " w.unitid," +
  893. " dg.dgfactoryitemvalue brandName" +
  894. " FROM RECKON_ERMCP_AREASPOTPL t" +
  895. " LEFT JOIN DELIVERYGOODS g on t.deliverygoodsid=g.deliverygoodsid" +
  896. " LEFT JOIN WRSTANDARD w on t.wrstandardid = w.wrstandardid" +
  897. " LEFT JOIN DGFACTORYITEM dg on t.spotgoodsbrandid=dg.dgfactoryitemid" +
  898. " WHERE 1 = 1"
  899. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  900. if r.QueryType == 1 {
  901. sqlId.And("t.RECKONDATE", r.RECKONDATE)
  902. } else if r.QueryType == 3 {
  903. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  904. }
  905. return sqlId.String()
  906. }
  907. // buildSqlCycle 周期报表查询语句
  908. func (r *Ermcp3AreaSpotPLReport) buildSqlCycle() string {
  909. var sqlId utils.SQLVal = "SELECT t.Cycletype," +
  910. " t.Cycletime," +
  911. " t.AREAUSERID," +
  912. " t.ACCOUNTID," +
  913. " t.WRFACTORTYPEID," +
  914. " t.CURRENCYID," +
  915. " t.BIZTYPE," +
  916. " t.DELIVERYGOODSID," +
  917. " t.WRSTANDARDID," +
  918. " t.SPOTGOODSBRANDID," +
  919. " t.ORIBUYQTY," +
  920. " t.ORIBUYAMOUNT," +
  921. " t.ORISELLQTY," +
  922. " t.ORISELLAMOUNT," +
  923. " t.ORIQTY," +
  924. " t.ORIAVERAGEPRICE," +
  925. " t.ORIAMOUNT," +
  926. " t.TODAYBUYQTY," +
  927. " t.TODAYBUYAMOUNT," +
  928. " t.TODAYBUYAVERAGEPRICE," +
  929. " t.TODAYSELLQTY," +
  930. " t.TODAYSELLAMOUNT," +
  931. " t.TODAYSELLAVERAGEPRICE," +
  932. " t.CURBUYQTY," +
  933. " t.CURBUYAMOUNT," +
  934. " t.CURSELLQTY," +
  935. " t.CURSELLAMOUNT," +
  936. " t.CURQTY," +
  937. " t.CURAVERAGEPRICE," +
  938. " t.CURAMOUNT," +
  939. " t.CURSPOTPRICE," +
  940. " t.CURMARKETVALUE," +
  941. " t.ACTUALPL," +
  942. " t.FLOATPL," +
  943. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  944. " t.TODAYINQTY," +
  945. " t.TODAYOUTQTY," +
  946. " g.deliverygoodscode," +
  947. " g.deliverygoodsname," +
  948. " g.goodsunitid," +
  949. " w.wrstandardcode," +
  950. " w.wrstandardname," +
  951. " w.unitid," +
  952. " dg.dgfactoryitemvalue brandName" +
  953. " FROM REPORT_ERMCP_AREASPOTPL t" +
  954. " LEFT JOIN DELIVERYGOODS g on t.deliverygoodsid=g.deliverygoodsid" +
  955. " LEFT JOIN WRSTANDARD w on t.wrstandardid = w.wrstandardid" +
  956. " LEFT JOIN DGFACTORYITEM dg on t.spotgoodsbrandid=dg.dgfactoryitemid" +
  957. " WHERE 1 = 1"
  958. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  959. sqlId.And("t.Cycletype", r.CYCLETYPE)
  960. sqlId.And("t.CYCLETIME", r.CYCLETIME)
  961. return sqlId.String()
  962. }
  963. // GetDataEx 获取现货报表(日/月/周/季/年)
  964. func (r *Ermcp3AreaSpotPLReport) GetDataEx() (interface{}, error) {
  965. sData := make([]Ermcp3AreaSpotPLReport, 0)
  966. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  967. for i := range sData {
  968. sData[i].calc()
  969. }
  970. return sData, err
  971. }
  972. // Ermcp3FinanceReport 财务报表
  973. type Ermcp3FinanceReport struct {
  974. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照时期(yyyyMMdd)
  975. CYCLETIME string `json:"cycletime" xorm:"CYCLETIME"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  976. CYCLETYPE int32 `json:"cycletype" xorm:"CYCLETYPE"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  977. SELLPREINVOICEDAMOUNT float64 `json:"sellpreinvoicedamount" xorm:"SELLPREINVOICEDAMOUNT"` // 销售预开票额(预开票额)
  978. SELLUNINVOICEDAMOUNT float64 `json:"selluninvoicedamount" xorm:"SELLUNINVOICEDAMOUNT"` // 销售应开票额(应开票额)
  979. TODAYRECEIVESUM float64 `json:"todayreceivesum" xorm:"TODAYRECEIVESUM"` // 今收款合计
  980. TODAYPAYSUM float64 `json:"todaypaysum" xorm:"TODAYPAYSUM"` // 今付款合计
  981. UPDATETIME string `json:"updatetime" xorm:"UPDATETIME"` // 更新时间
  982. AREAUSERID int64 `json:"areauserid" xorm:"AREAUSERID"` // 所属机构\交易用户ID
  983. CURRENCYID int32 `json:"currencyid" xorm:"CURRENCYID"` // 结算币种ID
  984. BIZTYPE int32 `json:"biztype" xorm:"BIZTYPE"` // 业务类型 - 1:套保 2:套利
  985. BUYTODAYSETTLEAMOUNT float64 `json:"buytodaysettleamount" xorm:"BUYTODAYSETTLEAMOUNT"` // 采购今付款额(今付货款额)
  986. SELLTODAYSETTLEAMOUNT float64 `json:"selltodaysettleamount" xorm:"SELLTODAYSETTLEAMOUNT"` // 销售今收款额(今收货款额)
  987. BUYTODAYREFUNDAMOUNT float64 `json:"buytodayrefundamount" xorm:"BUYTODAYREFUNDAMOUNT"` // 采购今收退款额(今收退款额)
  988. SELLTODAYREFUNDAMOUNT float64 `json:"selltodayrefundamount" xorm:"SELLTODAYREFUNDAMOUNT"` // 销售今付退款额(今付退款额)
  989. SELLTODAYINVOICEAMOUNT float64 `json:"selltodayinvoiceamount" xorm:"SELLTODAYINVOICEAMOUNT"` // 销售今开票额(今开票额)
  990. BUYTODAYINVOICEAMOUNT float64 `json:"buytodayinvoiceamount" xorm:"BUYTODAYINVOICEAMOUNT"` // 采购今收票额(今收票额)
  991. BUYPREPAIDAMOUNT float64 `json:"buyprepaidamount" xorm:"BUYPREPAIDAMOUNT"` // 采购预付款额(预付货款额)
  992. BUYUNPAIDAMOUNT float64 `json:"buyunpaidamount" xorm:"BUYUNPAIDAMOUNT"` // 采购应付款额(应付货款额)
  993. BUYPREINVOICEDAMOUNT float64 `json:"buypreinvoicedamount" xorm:"BUYPREINVOICEDAMOUNT"` // 采购预收票额(预收票额)
  994. BUYUNINVOICEDAMOUNT float64 `json:"buyuninvoicedamount" xorm:"BUYUNINVOICEDAMOUNT"` // 采购应收票额(应收票额)
  995. SELLPREPAIDAMOUNT float64 `json:"sellprepaidamount" xorm:"SELLPREPAIDAMOUNT"` // 销售预收款额(预收货款额)
  996. SELLUNPAIDAMOUNT float64 `json:"sellunpaidamount" xorm:"SELLUNPAIDAMOUNT"` // 销售应收款额(应收货款额)
  997. WRFACTORTYPEID int64 `json:"wrfactortypeid" xorm:"WRFACTORTYPEID"` // 仓单要素类型ID(212+Unix秒时间戳(10位)+xxxxxx)
  998. DELIVERYGOODSID int64 `json:"deliverygoodsid" xorm:"DELIVERYGOODSID"` // 现货品种ID
  999. WRSTANDARDID int64 `json:"wrstandardid" xorm:"WRSTANDARDID"` // 现货商品ID
  1000. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"SPOTGOODSBRANDID"` // 现货品牌ID
  1001. CURRENCYNAME string `json:"currencyname"` // 币种名称
  1002. QueryType int32 `json:"-"` // 查询类型 1-日报表 2-周期报表 3-日报表(范围,[开始日期,结束日期])
  1003. BeginDate string `json:"-"` // 开始交易日
  1004. EndDate string `json:"-"` // 结束交易日
  1005. }
  1006. func (r *Ermcp3FinanceReport) calc() {
  1007. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  1008. }
  1009. func (r *Ermcp3FinanceReport) buildSql() string {
  1010. if r.QueryType == 1 || r.QueryType == 3 {
  1011. return r.buildSqlDay()
  1012. } else if r.QueryType == 4 {
  1013. return r.buildSqlDayDetail()
  1014. }
  1015. return r.buildSqlCycle()
  1016. }
  1017. // buildSqlDay 财务日报表查询语句
  1018. func (r *Ermcp3FinanceReport) buildSqlDay() string {
  1019. var sqlId utils.SQLVal = "SELECT t.SELLPREINVOICEDAMOUNT," +
  1020. " t.SELLUNINVOICEDAMOUNT," +
  1021. " t.TODAYRECEIVESUM," +
  1022. " t.TODAYPAYSUM," +
  1023. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1024. " t.RECKONDATE," +
  1025. " t.AREAUSERID," +
  1026. " t.CURRENCYID," +
  1027. " t.BIZTYPE," +
  1028. " t.BUYTODAYSETTLEAMOUNT," +
  1029. " t.BUYTODAYREFUNDAMOUNT," +
  1030. " t.BUYPREPAIDAMOUNT," +
  1031. " t.BUYUNPAIDAMOUNT," +
  1032. " t.BUYTODAYINVOICEAMOUNT," +
  1033. " t.BUYPREINVOICEDAMOUNT," +
  1034. " t.BUYUNINVOICEDAMOUNT," +
  1035. " t.SELLTODAYSETTLEAMOUNT," +
  1036. " t.SELLTODAYREFUNDAMOUNT," +
  1037. " t.SELLPREPAIDAMOUNT," +
  1038. " t.SELLUNPAIDAMOUNT," +
  1039. " t.SELLTODAYINVOICEAMOUNT" +
  1040. " FROM RECKON_ERMCP_AREAFINANCE t" +
  1041. " WHERE 1 = 1"
  1042. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  1043. if r.QueryType == 1 {
  1044. sqlId.And("t.RECKONDATE", r.RECKONDATE)
  1045. } else if r.QueryType == 3 {
  1046. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1047. }
  1048. return sqlId.String()
  1049. }
  1050. // buildSqlDayDetail 财务日报表明细查询语句
  1051. func (r *Ermcp3FinanceReport) buildSqlDayDetail() string {
  1052. var sqlId utils.SQLVal = "SELECT t.RECKONDATE," +
  1053. " t.AREAUSERID," +
  1054. " t.CURRENCYID," +
  1055. " t.BIZTYPE," +
  1056. " t.WRFACTORTYPEID," +
  1057. " t.DELIVERYGOODSID," +
  1058. " t.WRSTANDARDID," +
  1059. " t.SPOTGOODSBRANDID," +
  1060. " t.BUYTODAYSETTLEAMOUNT," +
  1061. " t.BUYTODAYREFUNDAMOUNT," +
  1062. " t.BUYPREPAIDAMOUNT," +
  1063. " t.BUYUNPAIDAMOUNT," +
  1064. " t.BUYTODAYINVOICEAMOUNT," +
  1065. " t.BUYPREINVOICEDAMOUNT," +
  1066. " t.BUYUNINVOICEDAMOUNT," +
  1067. " t.SELLTODAYSETTLEAMOUNT," +
  1068. " t.SELLTODAYREFUNDAMOUNT," +
  1069. " t.SELLPREPAIDAMOUNT," +
  1070. " t.SELLUNPAIDAMOUNT," +
  1071. " t.SELLTODAYINVOICEAMOUNT," +
  1072. " t.SELLPREINVOICEDAMOUNT," +
  1073. " t.SELLUNINVOICEDAMOUNT," +
  1074. " t.TODAYRECEIVESUM," +
  1075. " t.TODAYPAYSUM," +
  1076. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME" +
  1077. " FROM RECKON_ERMCP_AFINANCESUB t" +
  1078. " WHERE 1 = 1"
  1079. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  1080. sqlId.And("t.RECKONDATE", r.RECKONDATE)
  1081. sqlId.And("t.CURRENCYID", r.CURRENCYID)
  1082. return sqlId.String()
  1083. }
  1084. // buildSqlCycle 财务周期报表查询语句
  1085. func (r *Ermcp3FinanceReport) buildSqlCycle() string {
  1086. var sqlId utils.SQLVal = "SELECT t.SELLPREINVOICEDAMOUNT," +
  1087. " t.SELLUNINVOICEDAMOUNT," +
  1088. " t.TODAYRECEIVESUM," +
  1089. " t.TODAYPAYSUM," +
  1090. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1091. " t.CYCLETIME," +
  1092. " t.CYCLETYPE," +
  1093. " t.AREAUSERID," +
  1094. " t.CURRENCYID," +
  1095. " t.BIZTYPE," +
  1096. " t.BUYTODAYSETTLEAMOUNT," +
  1097. " t.BUYTODAYREFUNDAMOUNT," +
  1098. " t.BUYPREPAIDAMOUNT," +
  1099. " t.BUYUNPAIDAMOUNT," +
  1100. " t.BUYTODAYINVOICEAMOUNT," +
  1101. " t.BUYPREINVOICEDAMOUNT," +
  1102. " t.BUYUNINVOICEDAMOUNT," +
  1103. " t.SELLTODAYSETTLEAMOUNT," +
  1104. " t.SELLTODAYREFUNDAMOUNT," +
  1105. " t.SELLPREPAIDAMOUNT," +
  1106. " t.SELLUNPAIDAMOUNT," +
  1107. " t.SELLTODAYINVOICEAMOUNT" +
  1108. " FROM REPORT_ERMCP_AREAFINANCE t" +
  1109. " WHERE 1 = 1"
  1110. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  1111. sqlId.And("t.CYCLETYPE", r.CYCLETYPE)
  1112. sqlId.And("t.CYCLETIME", r.CYCLETIME)
  1113. return sqlId.String()
  1114. }
  1115. // GetDataEx 获取财务报表
  1116. func (r *Ermcp3FinanceReport) GetDataEx() (interface{}, error) {
  1117. sData := make([]Ermcp3FinanceReport, 0)
  1118. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1119. for i := range sData {
  1120. sData[i].calc()
  1121. }
  1122. return sData, err
  1123. }
  1124. // Ermcp3AreaStockReport 库存报表
  1125. type Ermcp3AreaStockReport struct {
  1126. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照时期(yyyyMMdd)
  1127. CYCLETIME string `json:"cycletime" xorm:"CYCLETIME"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  1128. CYCLETYPE int32 `json:"cycletype" xorm:"CYCLETYPE"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  1129. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构ID
  1130. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类ID
  1131. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID
  1132. WAREHOUSEINFOID string `json:"warehouseinfoid" xorm:"'WAREHOUSEINFOID'"` // 仓库ID
  1133. ORISTOCK float64 `json:"oristock" xorm:"'ORISTOCK'"` // 期初库存量
  1134. CURSTOCK float64 `json:"curstock" xorm:"'CURSTOCK'"` // 期末库存量
  1135. TODAYBUYINQTY float64 `json:"todaybuyinqty" xorm:"'TODAYBUYINQTY'"` // 今采购入库量
  1136. TODAYPRODUCEINQTY float64 `json:"todayproduceinqty" xorm:"'TODAYPRODUCEINQTY'"` // 今内部入库量
  1137. TODAYSELLOUTQTY float64 `json:"todayselloutqty" xorm:"'TODAYSELLOUTQTY'"` // 今销售出库量
  1138. TODAYPRODUCEOUTQTY float64 `json:"todayproduceoutqty" xorm:"'TODAYPRODUCEOUTQTY'"` // 今内部出库量
  1139. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  1140. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1141. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1142. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 品类单位id
  1143. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1144. WAREHOUSENAME string `json:"warehousename" xorm:"'WAREHOUSENAME'"` // 仓库名称
  1145. WAREHOUSECODE string `json:"warehousecode" xorm:"'WAREHOUSECODE'"` // 仓库代码
  1146. WAREHOUSETYPE int32 `json:"warehousetype" xorm:"'WAREHOUSETYPE'"` // 仓库类型 - 1 厂库 2 自有库 3 合作库
  1147. USERNAME string `json:"username"` // 机构名称
  1148. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  1149. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种id
  1150. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  1151. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  1152. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODSUNITID'"` // 现货单位id
  1153. UNBUYINQTY float64 `json:"unbuyinqty"` // 采购未入库量(数据库未找到相关字段?)
  1154. UNSELLOUTQTY float64 `json:"unselloutqty"` // 销售未出库量(数据库未找到相关字段?)
  1155. DiffQty float64 `json:"diffqty"` // 库存变化量 = 期末 - 期初
  1156. QueryType int32 `json:"-"` // 查询类型 1-日报表 2-周期报表 3-日报表(范围,[开始日期,结束日期])
  1157. BeginDate string `json:"-"` // 开始交易日
  1158. EndDate string `json:"-"` // 结束交易日
  1159. }
  1160. func (r *Ermcp3AreaStockReport) calc() {
  1161. r.USERNAME = mtpcache.GetUserNameByUserId(r.USERID)
  1162. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  1163. r.DiffQty = r.CURSTOCK - r.ORISTOCK
  1164. }
  1165. func (r *Ermcp3AreaStockReport) buildSql() string {
  1166. if r.QueryType == 1 || r.QueryType == 3 {
  1167. return r.buildSqlDay()
  1168. }
  1169. return r.buildSqlCycle()
  1170. }
  1171. func (r *Ermcp3AreaStockReport) buildSqlDay() string {
  1172. var sqlId utils.SQLVal = "SELECT t.TODAYBUYINQTY," +
  1173. " t.TODAYPRODUCEINQTY," +
  1174. " t.reckondate," +
  1175. " t.TODAYSELLOUTQTY," +
  1176. " t.TODAYPRODUCEOUTQTY," +
  1177. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1178. " t.USERID," +
  1179. " t.WRSTANDARDID," +
  1180. " t.SPOTGOODSBRANDID," +
  1181. " t.WAREHOUSEINFOID," +
  1182. " t.ORISTOCK," +
  1183. " t.CURSTOCK," +
  1184. " w.wrstandardname," +
  1185. " w.wrstandardcode," +
  1186. " w.unitid," +
  1187. " gb.dgfactoryitemvalue brandname," +
  1188. " h.warehousename," +
  1189. " h.warehousecode," +
  1190. " h.warehousetype," +
  1191. " g.deliverygoodsid," +
  1192. " g.deliverygoodscode," +
  1193. " g.deliverygoodsname," +
  1194. " g.goodsunitid" +
  1195. " FROM RECKON_ERMCP_AREASTOCK t" +
  1196. " LEFT JOIN WRSTANDARD w" +
  1197. " on t.wrstandardid = w.wrstandardid" +
  1198. " LEFT JOIN dgfactoryitem gb" +
  1199. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1200. " LEFT JOIN deliverygoods g" +
  1201. " on t.deliverygoodsid = g.deliverygoodsid" +
  1202. " LEFT JOIN WAREHOUSEINFO h" +
  1203. " on t.warehouseinfoid = h.autoid" +
  1204. " WHERE 1 = 1"
  1205. sqlId.And("t.USERID", r.USERID)
  1206. if r.QueryType == 1 {
  1207. sqlId.And("t.RECKONDATE", r.RECKONDATE)
  1208. } else if r.QueryType == 3 {
  1209. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1210. }
  1211. // 现货商品id
  1212. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  1213. // 品类id
  1214. sqlId.AndEx("t.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  1215. // 品牌id
  1216. sqlId.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  1217. // 仓库id
  1218. sqlId.AndEx("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID, len(r.WAREHOUSEINFOID) > 0)
  1219. return sqlId.String()
  1220. }
  1221. func (r *Ermcp3AreaStockReport) buildSqlCycle() string {
  1222. var sqlId utils.SQLVal = "SELECT t.TODAYBUYINQTY," +
  1223. " t.TODAYPRODUCEINQTY," +
  1224. " t.CYCLETIME," +
  1225. " t.CYCLETYPE," +
  1226. " t.TODAYSELLOUTQTY," +
  1227. " t.TODAYPRODUCEOUTQTY," +
  1228. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1229. " t.USERID," +
  1230. " t.WRSTANDARDID," +
  1231. " t.SPOTGOODSBRANDID," +
  1232. " t.WAREHOUSEINFOID," +
  1233. " t.ORISTOCK," +
  1234. " t.CURSTOCK," +
  1235. " w.wrstandardname," +
  1236. " w.wrstandardcode," +
  1237. " w.unitid," +
  1238. " gb.dgfactoryitemvalue brandname," +
  1239. " h.warehousename," +
  1240. " h.warehousecode," +
  1241. " h.warehousetype," +
  1242. " g.deliverygoodsid," +
  1243. " g.deliverygoodscode," +
  1244. " g.deliverygoodsname," +
  1245. " g.goodsunitid" +
  1246. " FROM REPORT_ERMCP_AREASTOCK t" +
  1247. " LEFT JOIN WRSTANDARD w" +
  1248. " on t.wrstandardid = w.wrstandardid" +
  1249. " LEFT JOIN dgfactoryitem gb" +
  1250. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1251. " LEFT JOIN deliverygoods g" +
  1252. " on t.deliverygoodsid = g.deliverygoodsid" +
  1253. " LEFT JOIN WAREHOUSEINFO h" +
  1254. " on t.warehouseinfoid = h.autoid" +
  1255. " WHERE 1 = 1"
  1256. sqlId.And("t.USERID", r.USERID)
  1257. sqlId.And("t.cycletype", r.CYCLETYPE)
  1258. sqlId.And("t.cycletime", r.CYCLETIME)
  1259. // 现货商品id
  1260. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  1261. // 品类id
  1262. sqlId.AndEx("t.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  1263. // 品牌id
  1264. sqlId.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  1265. // 仓库id
  1266. sqlId.AndEx("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID, len(r.WAREHOUSEINFOID) > 0)
  1267. return sqlId.String()
  1268. }
  1269. // GetDataEx 查询库存报表
  1270. func (r *Ermcp3AreaStockReport) GetDataEx() (interface{}, error) {
  1271. sData := make([]ErmcpAreaStockReport, 0)
  1272. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1273. if err == nil {
  1274. for i := range sData {
  1275. sData[i].calc()
  1276. }
  1277. }
  1278. return sData, err
  1279. }
  1280. // Ermcp3ArealSumPL 损益汇总表
  1281. type Ermcp3ArealSumPL struct {
  1282. RECKONDATE string `json:"reckondate" xorm:"RECKONDATE"` // 日照时期(yyyyMMdd)
  1283. CYCLETIME string `json:"cycletime" xorm:"'CYCLETIME'"` // 周期时间 月(YYYYMM) 季(YYYYQ) 年(YYYY) 周(YYYYIW) 全(0)【原值】
  1284. CYCLETYPE int32 `json:"cycletype" xorm:"'CYCLETYPE'"` // 周期类型 - 1:月 2:季 3:年 4:周 5:全报表【原值】
  1285. AREAUSERID int64 `json:"areauserid" xorm:"AREAUSERID"` // 所属机构\交易用户ID
  1286. MIDDLEGOODSID int64 `json:"middlegoodsid" xorm:"MIDDLEGOODSID"` // 套保品种ID(SEQ_ERMS_MIDDLEGOODS)
  1287. CURRENCYID int32 `json:"currencyid" xorm:"CURRENCYID"` // 结算币种ID
  1288. SPOTACTUALPL float64 `json:"spotactualpl" xorm:"SPOTACTUALPL"` // 现货损益
  1289. SPOTFLOATPL float64 `json:"spotfloatpl" xorm:"SPOTFLOATPL"` // 现货浮动损益
  1290. FUTUREACTUALPL float64 `json:"futureactualpl" xorm:"FUTUREACTUALPL"` // 期货损益
  1291. FUTUREFLOATPL float64 `json:"futurefloatpl" xorm:"FUTUREFLOATPL"` // 期货浮动损益
  1292. SUMACTUALPL float64 `json:"sumactualpl" xorm:"SUMACTUALPL"` // 实际损益 = (ActualPL + FutureActualPL)
  1293. SUMPL float64 `json:"sumpl" xorm:"'SUMPL'"` // 汇总损益
  1294. UPDATETIME string `json:"updatetime" xorm:"UPDATETIME"` // 更新时间
  1295. CURRENCYNAME string `json:"currencyname"` // 币种名称
  1296. QueryType int32 `json:"-"` // 查询类型 1-日报表 2-周期报表 3-日报表(范围,[开始日期,结束日期])
  1297. BeginDate string `json:"-"` // 开始交易日
  1298. EndDate string `json:"-"` // 结束交易日
  1299. }
  1300. func (r *Ermcp3ArealSumPL) calc() {
  1301. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  1302. }
  1303. func (r *Ermcp3ArealSumPL) buildSql() string {
  1304. if r.QueryType == 1 || r.QueryType == 3 {
  1305. return r.buildSqlDay()
  1306. } else if r.QueryType == 4 {
  1307. return r.buildSqlDayDetail()
  1308. }
  1309. return r.buildSqlCycle()
  1310. }
  1311. // buildSqlDay 损益汇总日报表(需要汇总维度)
  1312. func (r *Ermcp3ArealSumPL) buildSqlDay() string {
  1313. var sqlId utils.SQLVal = "SELECT t.RECKONDATE," +
  1314. " t.AREAUSERID," +
  1315. " t.CURRENCYID," +
  1316. " sum(t.SPOTACTUALPL) SPOTACTUALPL," +
  1317. " sum(t.SPOTFLOATPL) SPOTFLOATPL," +
  1318. " sum(t.FUTUREACTUALPL) FUTUREACTUALPL," +
  1319. " sum(t.FUTUREFLOATPL) FUTUREFLOATPL," +
  1320. " sum(t.SUMACTUALPL) SUMACTUALPL," +
  1321. " sum(t.SUMPL) SUMPL" +
  1322. " FROM RECKON_ERMCP_AREASUMPL t" +
  1323. " WHERE 1 = 1"
  1324. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  1325. if r.QueryType == 1 {
  1326. sqlId.And("t.RECKONDATE", r.RECKONDATE)
  1327. } else if r.QueryType == 3 {
  1328. sqlId.Join(fmt.Sprintf(" and t.RECKONDATE >= '%v' and t.RECKONDATE <= '%v' ", r.BeginDate, r.EndDate))
  1329. }
  1330. sqlId.Join(" GROUP BY t.RECKONDATE, t.AREAUSERID, t.CURRENCYID")
  1331. return sqlId.String()
  1332. }
  1333. func (r *Ermcp3ArealSumPL) buildSqlDayDetail() string {
  1334. var sqlId utils.SQLVal = "SELECT t.RECKONDATE," +
  1335. " t.AREAUSERID," +
  1336. " t.MIDDLEGOODSID," +
  1337. " t.CURRENCYID," +
  1338. " t.SPOTACTUALPL," +
  1339. " t.SPOTFLOATPL," +
  1340. " t.FUTUREACTUALPL," +
  1341. " t.FUTUREFLOATPL," +
  1342. " t.SUMACTUALPL," +
  1343. " t.SUMPL," +
  1344. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME" +
  1345. " FROM RECKON_ERMCP_AREASUMPL t" +
  1346. " WHERE 1 = 1"
  1347. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  1348. sqlId.And("t.RECKONDATE", r.RECKONDATE)
  1349. sqlId.And("t.CURRENCYID", r.CURRENCYID)
  1350. return sqlId.String()
  1351. }
  1352. func (r *Ermcp3ArealSumPL) buildSqlCycle() string {
  1353. var sqlId utils.SQLVal = "SELECT t.AREAUSERID," +
  1354. " t.CYCLETIME," +
  1355. " t.CYCLETYPE," +
  1356. " t.CURRENCYID," +
  1357. " sum(t.SPOTACTUALPL) SPOTACTUALPL," +
  1358. " sum(t.SPOTFLOATPL) SPOTFLOATPL," +
  1359. " sum(t.FUTUREACTUALPL) FUTUREACTUALPL," +
  1360. " sum(t.FUTUREFLOATPL) FUTUREFLOATPL," +
  1361. " sum(t.SUMACTUALPL) SUMACTUALPL," +
  1362. " sum(t.SUMPL) SUMPL" +
  1363. " FROM REPORT_ERMCP_AREASUMPL t" +
  1364. " WHERE 1 = 1"
  1365. sqlId.And("t.AREAUSERID", r.AREAUSERID)
  1366. sqlId.And("t.cycletype", r.CYCLETYPE)
  1367. sqlId.And("t.cycletime", r.CYCLETIME)
  1368. sqlId.Join(" GROUP BY t.AREAUSERID, t.CURRENCYID, t.CYCLETIME, t.CYCLETYPE")
  1369. return sqlId.String()
  1370. }
  1371. // GetDataEx 获取损益汇总表
  1372. func (r *Ermcp3ArealSumPL) GetDataEx() (interface{}, error) {
  1373. sData := make([]Ermcp3ArealSumPL, 0)
  1374. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1375. for i := range sData {
  1376. sData[i].calc()
  1377. }
  1378. return sData, err
  1379. }