wrTrade2.go 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. /**
  2. * @Author: zou.yingbin
  3. * @Create : 2021/7/20 18:31
  4. * @Modify : 2021/7/20 18:31
  5. */
  6. package models
  7. import (
  8. "fmt"
  9. "mtp2_if/db"
  10. "mtp2_if/utils"
  11. "strings"
  12. )
  13. // WrOrderQuote 买卖大厅行情
  14. type WrOrderQuote struct {
  15. WRFACTORTYPEID string `json:"wrfactortypeid" xorm:"'WRFACTORTYPEID'" form:"wrfactortypeid"` // 仓单要素id
  16. BUYPRICE float64 `json:"buyprice" xorm:"'BUYPRICE'"` // 买价
  17. BUYQTY float64 `json:"buyqty" xorm:"'BUYQTY'"` // 买量
  18. SELLPRICE float64 `json:"sellprice" xorm:"'SELLPRICE'"` // 卖价
  19. SELLQTY float64 `json:"sellqty" xorm:"'SELLQTY'"` // 卖量
  20. WRFACTORTYPENAME string `json:"wrfactortypename" xorm:"'WRFACTORTYPENAME'"` // 仓单要素类型名称(选择项要素的名称合并显示,逗号分隔)
  21. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 品种id
  22. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 商品id
  23. WAREHOUSEID int32 `json:"warehouseid" xorm:"'WAREHOUSEID'" form:"warehouseid"` // 仓库id
  24. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 品种名称
  25. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 品种代码
  26. WAREHOUSENAME string `json:"warehousename" xorm:"'WAREHOUSENAME'"` // 仓库名称
  27. WAREHOUSECODE string `json:"warehousecode" xorm:"'WAREHOUSECODE'"` // 仓库代码
  28. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'" form:"wrstandardname"` // 商品名称
  29. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 商品代码
  30. WRGOODSNAME string `json:"wrgoodsname" xorm:"'WRGOODSNAME'"` // 商品(商品名称+仓库要素名称 拼接)
  31. BUYPRICEMOVE float64 `json:"buypricemove" xorm:"'BUYPRICEMOVE'"` // 买升贴水(基差)
  32. SELLPRICEMOVE float64 `json:"sellpricemove" xorm:"'SELLPRICEMOVE'"` // 卖升贴水(基差)
  33. GOODSID int32 `json:"goodsid" xorm:"'GOODSID'"` // 商品id(浮动价列表才有)
  34. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 商品合约(浮动价列表才有)
  35. DELIVERYMONTH string `json:"deliverymonth" xorm:"'DELIVERYMONTH'"` // 交收月
  36. PageEx `xorm:"extends"` // 页码信息
  37. HasWr int32 `json:"-" form:"haswr"` // 是否有仓单 0-没有 1-有
  38. WrPriceType int32 `json:"-" form:"wrpricetype"` // 价格方式 - 1:固定价 2-浮动价 - [挂牌]
  39. MarketId int32 `json:"-" form:"marketid"` // 市场id
  40. BRANDNAME string `json:"-" form:"brandname"` // 品牌名称
  41. }
  42. func (r *WrOrderQuote) calc() {
  43. r.WRGOODSNAME = strings.ReplaceAll(r.WRGOODSNAME, ",", "-")
  44. }
  45. // buildSqlFixedPrice 一口价
  46. func (r *WrOrderQuote) buildSqlFixedPrice() string {
  47. var sqlId utils.SQLVal = "with buyQuote as" +
  48. " (" +
  49. " select to_char(a.wrfactortypeid) wrfactortypeid, a.fixedprice buyPrice, sum(a.orderqty) buyQty" +
  50. " from (select t.wrfactortypeid," +
  51. " t.marketid," +
  52. " t.buyorsell," +
  53. " t.orderqty - t.tradeqty as orderqty," +
  54. " t.fixedprice," +
  55. " rank() over(partition by t.wrfactortypeid order by t.fixedprice desc) cnt" +
  56. " from wrtrade_orderdetail t" +
  57. " where t.wrtradetype = 1 and t.buyorsell = 0 and t.wrtradeorderstatus in(3,10) %v" +
  58. " ) a" +
  59. " where a.cnt = 1" +
  60. " group by a.wrfactortypeid, a.fixedprice)," +
  61. "sellQuote as" +
  62. " (" +
  63. " select to_char(a.wrfactortypeid) wrfactortypeid, a.fixedprice sellPrice, sum(a.orderqty) sellQty" +
  64. " from (select t.wrfactortypeid," +
  65. " t.marketid," +
  66. " t.buyorsell," +
  67. " t.orderqty - t.tradeqty as orderqty," +
  68. " t.fixedprice," +
  69. " rank() over(partition by t.wrfactortypeid order by t.fixedprice asc) cnt" +
  70. " from wrtrade_orderdetail t" +
  71. " where t.wrtradetype = 1 and t.buyorsell = 1 and t.wrtradeorderstatus in(3,10) %v " +
  72. " ) a" +
  73. " where a.cnt = 1" +
  74. " group by a.wrfactortypeid, a.fixedprice)," +
  75. "wrQuote as (" +
  76. " select nvl(k1.wrfactortypeid, k2.wrfactortypeid) wrfactortypeid," +
  77. " k1.buyPrice," +
  78. " k1.buyQty," +
  79. " k2.sellPrice," +
  80. " k2.sellQty" +
  81. " from buyQuote k1" +
  82. " full join sellQuote k2" +
  83. " on k1.wrfactortypeid = k2.wrfactortypeid" +
  84. ")" +
  85. "select k.*," +
  86. " w.wrfactortypename," +
  87. " w.deliverygoodsid," +
  88. " w.wrstandardid," +
  89. " w.warehouseid," +
  90. " dg.deliverygoodsname," +
  91. " dg.deliverygoodscode," +
  92. " h.warehousename," +
  93. " h.warehousecode," +
  94. " wg.wrstandardname," +
  95. " wg.wrstandardcode," +
  96. " wg.wrstandardname || w.wrfactortypename2 as wrgoodsname" +
  97. " from wrQuote k" +
  98. " left join WRFactorType w" +
  99. " on k.wrfactortypeid = w.wrfactortypeid" +
  100. " left join deliverygoods dg" +
  101. " on w.deliverygoodsid = dg.deliverygoodsid" +
  102. " left join wrstandard wg" +
  103. " on w.wrstandardid = wg.wrstandardid" +
  104. " left join warehouseinfo h" +
  105. " on w.warehouseid = h.autoid" +
  106. " where 1=1 %v " +
  107. "order by deliverygoodsname"
  108. var param, param2 utils.SQLVal
  109. param.And("t.haswr", r.HasWr)
  110. param.And("t.wrpricetype", r.WrPriceType)
  111. param.AndEx("t.marketid", r.MarketId, r.MarketId > 0)
  112. param2.AndLike("w.wrfactortypename", r.WRSTANDARDNAME)
  113. param2.AndLike("w.wrfactortypename", r.BRANDNAME)
  114. param2.AndEx("w.warehouseid", r.WAREHOUSEID, r.WAREHOUSEID > 0)
  115. param2.AndEx("w.deliverygoodsid", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  116. sqlId.FormatParam(param.String(), param.String(), param2.String())
  117. sqlId.Page(r.Page, r.PageSize)
  118. return sqlId.String()
  119. }
  120. // buildSqlPriceMove 浮动价
  121. func (r *WrOrderQuote) buildSqlPriceMove() string {
  122. var sqlId utils.SQLVal = "with buyQuote as" +
  123. " (select to_char(a.wrfactortypeid) wrfactortypeid," +
  124. " a.pricemove buypricemove," +
  125. " sum(a.orderqty) buyQty," +
  126. " a.goodsid," +
  127. " a.goodscode," +
  128. " a.deliverymonth" +
  129. " from (select t.wrfactortypeid," +
  130. " t.marketid," +
  131. " t.buyorsell," +
  132. " t.orderqty - t.tradeqty as orderqty," +
  133. " t.pricemove," +
  134. " t.wrtradeorderid," +
  135. " t.deliverymonth," +
  136. " c.goodsid," +
  137. " c.goodscode," +
  138. " rank() over(partition by t.wrfactortypeid, nvl(t.deliverymonth,'0'), c.goodsid order by t.pricemove desc) cnt" +
  139. " from wrtrade_orderdetail t" +
  140. " left join WRTrade_GoodsConfig c" +
  141. " on t.wrtradeorderid = c.wrtradeorderid" +
  142. " where t.wrtradetype = 1 and t.wrtradeorderstatus in(3,10)" +
  143. " and t.buyorsell = 0 %v) a" +
  144. " where a.cnt = 1" +
  145. " group by a.wrfactortypeid, a.pricemove, a.goodsid, a.goodscode, a.deliverymonth)," +
  146. "sellQuote as" +
  147. " (select to_char(a.wrfactortypeid) wrfactortypeid," +
  148. " a.pricemove sellpricemove," +
  149. " a.goodsid," +
  150. " a.goodscode," +
  151. " a.deliverymonth," +
  152. " sum(a.orderqty) sellQty" +
  153. " from (select t.wrfactortypeid," +
  154. " t.marketid," +
  155. " t.buyorsell," +
  156. " t.orderqty - t.tradeqty as orderqty," +
  157. " t.pricemove," +
  158. " t.deliverymonth," +
  159. " c.goodsid," +
  160. " c.goodscode," +
  161. " rank() over(partition by t.wrfactortypeid, nvl(t.deliverymonth,'0'), c.goodsid order by t.pricemove asc) cnt" +
  162. " from wrtrade_orderdetail t" +
  163. " left join WRTrade_GoodsConfig c" +
  164. " on t.wrtradeorderid = c.wrtradeorderid" +
  165. " where t.wrtradetype = 1 and t.wrtradeorderstatus in(3,10)" +
  166. " and t.buyorsell = 1 %v) a" +
  167. " where a.cnt = 1" +
  168. " group by a.wrfactortypeid, a.pricemove, a.goodsid, a.goodscode, a.deliverymonth)," +
  169. "wrQuote as" +
  170. " (select nvl(k1.wrfactortypeid, k2.wrfactortypeid) wrfactortypeid," +
  171. " nvl(k1.goodsid, k2.goodsid) goodsid," +
  172. " nvl(k1.goodscode, k2.goodscode) goodscode," +
  173. " nvl(k1.deliverymonth, k2.deliverymonth) deliverymonth," +
  174. " k1.buypricemove," +
  175. " k1.buyQty," +
  176. " k2.sellpricemove," +
  177. " k2.sellQty" +
  178. " from buyQuote k1" +
  179. " full join sellQuote k2" +
  180. " on k1.wrfactortypeid = k2.wrfactortypeid and k1.deliverymonth = k2.deliverymonth)" +
  181. "select k.*," +
  182. " w.wrfactortypename," +
  183. " w.deliverygoodsid," +
  184. " w.wrstandardid," +
  185. " w.warehouseid," +
  186. " dg.deliverygoodsname," +
  187. " dg.deliverygoodscode," +
  188. " h.warehousename," +
  189. " h.warehousecode," +
  190. " wg.wrstandardname," +
  191. " wg.wrstandardcode," +
  192. " wg.wrstandardname || w.wrfactortypename2 as wrgoodsname" +
  193. " from wrQuote k" +
  194. " left join WRFactorType w" +
  195. " on k.wrfactortypeid = w.wrfactortypeid" +
  196. " left join deliverygoods dg" +
  197. " on w.deliverygoodsid = dg.deliverygoodsid" +
  198. " left join wrstandard wg" +
  199. " on w.wrstandardid = wg.wrstandardid" +
  200. " left join warehouseinfo h" +
  201. " on w.warehouseid = h.autoid" +
  202. " where 1 = 1 %v" +
  203. " order by deliverygoodsname"
  204. var param, param2 utils.SQLVal
  205. param.And("t.haswr", r.HasWr)
  206. param.And("t.wrpricetype", r.WrPriceType)
  207. param.AndEx("t.marketid", r.MarketId, r.MarketId > 0)
  208. param2.AndLike("w.wrfactortypename", r.WRSTANDARDNAME)
  209. param2.AndLike("w.wrfactortypename", r.BRANDNAME)
  210. param2.AndEx("w.warehouseid", r.WAREHOUSEID, r.WAREHOUSEID > 0)
  211. sqlId.FormatParam(param.String(), param.String(), param2.String())
  212. sqlId.Page(r.Page, r.PageSize)
  213. return sqlId.String()
  214. }
  215. func (r *WrOrderQuote) buildSql() string {
  216. if r.WrPriceType == 1 {
  217. return r.buildSqlFixedPrice()
  218. } else {
  219. return r.buildSqlPriceMove()
  220. }
  221. }
  222. // GetDataByPage 获取买卖大厅行情
  223. func (r *WrOrderQuote) GetDataByPage() (interface{}, error, int, int, int) {
  224. sData := make([]WrOrderQuote, 0)
  225. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  226. total := 0
  227. for i := range sData {
  228. sData[i].calc()
  229. total = sData[i].Total
  230. }
  231. return sData, err, r.Page, r.PageSize, total
  232. }
  233. // WrOrderQuoteDetail 行情大厅明细(买卖大厅)
  234. type WrOrderQuoteDetail struct {
  235. WRTRADEORDERID string `json:"wrtradeorderid" xorm:"WRTRADEORDERID"` // 仓单贸易委托单ID(320+Unix秒时间戳(10位)+xxxxxx)
  236. BUYORSELL int32 `json:"buyorsell" xorm:"BUYORSELL" form:"buyorsell"` // 买卖 - 0:买 1:卖
  237. USERID int64 `json:"userid" xorm:"USERID" form:"userid"` // 购买方(销售方)用户ID
  238. QTY float64 `json:"orderqty" xorm:"qty"` // 买(卖)量(=委托量-成交量)
  239. PRICE float64 `json:"fixedprice" xorm:"price"` // 买(卖)价
  240. PRICEMOVE float64 `json:"pricemove" xorm:"PRICEMOVE"` // 买(卖)升贴水- 基差
  241. USERNAME string `json:"username" xorm:"'USERNAME'"` // 购买方(销售方)
  242. PageEx `xorm:"extends"`
  243. WRFACTORTYPEID int64 `json:"-" form:"wrfactortypeid"` // 仓单要素id
  244. HasWr int32 `json:"-" form:"haswr"` // 是否有仓单 0-没有 1-有
  245. WrPriceType int32 `json:"-" form:"wrpricetype"` // 价格方式 - 1:固定价 2-浮动价 - [挂牌]
  246. MarketId int32 `json:"-" form:"marketid"` // 市场id
  247. GOODSID int32 `json:"-" form:"goodsid"` // 商品id
  248. }
  249. func (r *WrOrderQuoteDetail) calc() {
  250. }
  251. func (r *WrOrderQuoteDetail) buildSql() string {
  252. var sqlId utils.SQLVal = "select to_char(t.wrtradeorderid) wrtradeorderid," +
  253. " to_char(t.wrfactortypeid) wrfactortypeid," +
  254. " t.userid," +
  255. " t.orderqty - t.tradeqty as qty," +
  256. " t.fixedprice as price," +
  257. " t.pricemove," +
  258. " t.wrpricetype," +
  259. " t.buyorsell," +
  260. " u.accountname as username," +
  261. " c.goodsid," +
  262. " c.goodscode" +
  263. " from WRTRADE_ORDERDETAIL t" +
  264. " left join WRTRADE_GOODSCONFIG c" +
  265. " on t.wrtradeorderid = c.wrtradeorderid" +
  266. " left join Useraccount u" +
  267. " on t.userid = u.userid" +
  268. " where 1 = 1" +
  269. " and t.wrtradetype = 1" +
  270. " and t.wrtradeorderstatus in (3, 10)"
  271. sqlId.And("t.haswr", r.HasWr)
  272. sqlId.And("t.wrpricetype", r.WrPriceType)
  273. sqlId.And("t.wrfactortypeid", r.WRFACTORTYPEID)
  274. sqlId.AndEx("c.goodsid", r.GOODSID, r.GOODSID > 0)
  275. sqlId.And("t.buyorsell", r.BUYORSELL)
  276. sqlId.AndEx("t.marketid", r.MarketId, r.MarketId > 0)
  277. if r.BUYORSELL == 0 {
  278. if r.WrPriceType == 1 {
  279. sqlId.Join(" order by t.fixedprice desc")
  280. } else {
  281. sqlId.Join(" order by t.pricemove desc")
  282. }
  283. } else {
  284. if r.WrPriceType == 1 {
  285. sqlId.Join(" order by t.fixedprice asc")
  286. } else {
  287. sqlId.Join(" order by t.pricemove asc")
  288. }
  289. }
  290. sqlId.Page(r.Page, r.PageSize)
  291. return sqlId.String()
  292. }
  293. // GetDataByPage 获取行情大厅明细(买卖大厅)
  294. func (r *WrOrderQuoteDetail) GetDataByPage() (interface{}, error, int, int, int) {
  295. sData := make([]WrOrderQuoteDetail, 0)
  296. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  297. total := 0
  298. for i := range sData {
  299. sData[i].calc()
  300. total = sData[i].Total
  301. }
  302. return sData, err, r.Page, r.PageSize, total
  303. }
  304. // Wr2FactorType 仓单要素类型
  305. type Wr2FactorType struct {
  306. WRFACTORTYPEID string `json:"wrfactortypeid" xorm:"WRFACTORTYPEID" form:"wrfactortypeid"` // 仓单要素类型ID(212+Unix秒时间戳(10位)+xxxxxx)
  307. WRFACTORTYPENAME string `json:"wrfactortypename" xorm:"WRFACTORTYPENAME" form:"wrfactortypename"` // 仓单要素类型名称(选择项要素的名称合并显示,逗号分隔)
  308. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"DELIVERYGOODSID" form:"deliverygoodsid"` // 品种ID
  309. WAREHOUSEID int64 `json:"warehouseid" xorm:"WAREHOUSEID" form:"warehouseid"` // 仓库ID
  310. WRSTANDARDID int64 `json:"wrstandardid" xorm:"WRSTANDARDID" form:"wrstandardid"` // 现货商品ID
  311. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"WRSTANDARDCODE" form:"wrstandardcode"` // 现货商品代码
  312. OPTIONCOMPARE string `json:"optioncompare" xorm:"OPTIONCOMPARE" form:"optioncompare"` // 选择项比较串【{选择项ID}+{冒号}+选择项值 } ,逗号分隔,头尾加逗号】-- 所有选择项拼接,用于比较
  313. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 商品名称
  314. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 品种代码
  315. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 品种名称
  316. ENUMDICNAME string `json:"enumdicname" xorm:"'enumdicname'"` // 单位名称
  317. WAREHOUSENAME string `json:"warehousename" xorm:"'WAREHOUSENAME'"` // 仓库名称
  318. WRTYPENAME string `json:"wrtypename" xorm:"'WRTYPENAME'"` // 商品
  319. }
  320. // Calc
  321. func (r *Wr2FactorType) Calc() {
  322. if r.WRTYPENAME == "" {
  323. if len(r.WRFACTORTYPENAME) > 0 {
  324. r.WRTYPENAME = r.WRSTANDARDNAME + r.WRFACTORTYPENAME
  325. } else {
  326. r.WRTYPENAME = r.WRSTANDARDNAME
  327. }
  328. }
  329. r.WRTYPENAME = strings.ReplaceAll(r.WRTYPENAME, ",", "-")
  330. }
  331. // WrPosition 仓单贸易持仓汇总
  332. type WrPosition struct {
  333. WRPOSITIONID string `json:"wrpositionid" xorm:"WRPOSITIONID" form:"wrpositionid"` // 仓单头寸ID(201+Unix秒时间戳(10位)+xxxxxx)
  334. WRUSERID int64 `json:"wruserid" xorm:"WRUSERID" form:"userid"` // 持有人ID
  335. TOTALQTY float64 `json:"totalqty" xorm:"TOTALQTY" form:"totalqty"` // 总数量(库存数量)
  336. INCREASEQTY float64 `json:"-" xorm:"INCREASEQTY" form:"increaseqty"` // 今日增加数量
  337. DECREASEQTY float64 `json:"-" xorm:"DECREASEQTY" form:"decreaseqty"` // 今日减少数量
  338. FREEZEQTY float64 `json:"freezeqty" xorm:"FREEZEQTY" form:"freezeqty"` // 总冻结数量
  339. RELATEDQTY float64 `json:"-" xorm:"RELATEDQTY" form:"relatedqty"` // 关联数量
  340. MODIFYTIME string `json:"modifytime" xorm:"MODIFYTIME" form:"modifytime"` // 修改时间(过户时间)
  341. MODIFIERID int64 `json:"-" xorm:"MODIFIERID" form:"modifierid"` // 修改人
  342. TRADEWRQTY float64 `json:"-" xorm:"TRADEWRQTY" form:"tradewrqty"` // 订单仓单头寸总数量
  343. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID" form:"accountid"` // 帐户ID
  344. CREDITTOTALQTY float64 `json:"credittotalqty" xorm:"CREDITTOTALQTY" form:"credittotalqty"` // 授信总数量(预售仓单:预售总量)
  345. CREDITCURQTY float64 `json:"creditcurqty" xorm:"CREDITCURQTY" form:"creditcurqty"` // 授信当前数量(预售仓单:预售当前量)
  346. CREDITFREEZEQTY float64 `json:"creditfreezeqty" xorm:"CREDITFREEZEQTY" form:"creditfreezeqty"` // 授信冻结数量(预售仓单: 当前数量)
  347. CREDITTRADEQTY float64 `json:"credittradeqty" xorm:"CREDITTRADEQTY" form:"credittradeqty"` // 授信成交数量(预售仓单:成交总量)
  348. Wr2FactorType `xorm:"extends"`
  349. ENALBEQTY float64 `json:"enalbeqty" xorm:"'ENALBEQTY'"` // 可用数量
  350. CREDITENABLEQTY float64 `json:"creditenableqty" xorm:"'CREDITENABLEQTY'"` // 授信可用数量(预售仓单: 可用数量)
  351. PLEDGEQTY float64 `json:"pledgeqty" xorm:"'-'"` // 质押数量
  352. BUYEXCUTEQTY float64 `json:"buyexcuteqty"` // 采购履约中数量
  353. SELLEXCUTEQTY float64 `json:"sellexcuteqty"` // 销售履约中数量
  354. WRPOSITIONNO string `json:"wrpositionno"` // 仓单编号
  355. QueryType int32 `json:"querytype" form:"querytype"` // 查询类型 1-现货汇总 2-库存汇总(订单汇总)
  356. }
  357. func (r *WrPosition) calc() {
  358. r.ENALBEQTY = r.TOTALQTY - r.FREEZEQTY
  359. r.CREDITENABLEQTY = r.CREDITCURQTY - r.CREDITFREEZEQTY
  360. r.WRPOSITIONNO = fmt.Sprintf("%v", r.WRFACTORTYPEID)
  361. r.Wr2FactorType.Calc()
  362. }
  363. // 添加履约中数量
  364. func (r *WrPosition) addPerformancePlanQty(sum WrPerformanPlanSum) {
  365. if r.DELIVERYGOODSID == sum.DELIVERYGOODSID &&
  366. r.WRSTANDARDID == sum.WRSTANDARDID &&
  367. r.WRTYPENAME == sum.WRTYPENAME {
  368. if r.QueryType == 1 || (r.QueryType == 2 && r.WAREHOUSEID == sum.WAREHOUSEID) {
  369. if sum.BUYORSELL == 0 {
  370. r.BUYEXCUTEQTY = sum.QTY
  371. } else {
  372. r.SELLEXCUTEQTY = sum.QTY
  373. }
  374. }
  375. }
  376. }
  377. func (r *WrPosition) buildSql() string {
  378. if r.QueryType == 1 {
  379. // 现货汇总 品种 + 品类 + 其它要素, 根据品种汇总
  380. return r.buildSql1()
  381. } else if r.QueryType == 2 {
  382. // 库存汇总 品种 + 仓库 + 品类 + 其它要素, 即全维度
  383. return r.buildSql2()
  384. } else {
  385. // 现货明细 仓单编号 + 品种 + 仓库 + 仓单要素 即全维度 跟库存汇总一样
  386. return r.buildSql3()
  387. }
  388. }
  389. func (r *WrPosition) buildSql1() string {
  390. var sqlId utils.SQLVal = "with tmp as" +
  391. " (select t.*," +
  392. " w.wrstandardid," +
  393. " w.warehouseid," +
  394. " wd.wrstandardname," +
  395. " wd.wrstandardname || w.wrfactortypename2 as wrtypename" +
  396. " from WRPOSITION t" +
  397. " left join WRFACTORTYPE w" +
  398. " on t.wrfactortypeid = w.wrfactortypeid" +
  399. " left join wrstandard wd" +
  400. " on w.wrstandardid = wd.wrstandardid" +
  401. " where t.wruserid = %v)," +
  402. "hld as" +
  403. " (select k.deliverygoodsid," +
  404. " k.wrtypename," +
  405. " k.wrstandardid," +
  406. " sum(k.TOTALQTY) TOTALQTY," +
  407. " sum(k.INCREASEQTY) INCREASEQTY," +
  408. " sum(k.DECREASEQTY) DECREASEQTY," +
  409. " sum(k.FREEZEQTY) FREEZEQTY," +
  410. " sum(k.RELATEDQTY) RELATEDQTY," +
  411. " sum(k.TRADEWRQTY) TRADEWRQTY," +
  412. " sum(k.CREDITTOTALQTY) CREDITTOTALQTY," +
  413. " sum(k.CREDITCURQTY) CREDITCURQTY," +
  414. " sum(k.CREDITFREEZEQTY) CREDITFREEZEQTY," +
  415. " sum(k.CREDITTRADEQTY) CREDITTRADEQTY" +
  416. " from tmp k" +
  417. " where 1 = 1" +
  418. " group by k.deliverygoodsid, k.wrstandardname, k.wrtypename, k.wrstandardid)" +
  419. "select h.*, g.deliverygoodsname, g.deliverygoodscode, e.enumdicname" +
  420. " from hld h" +
  421. " left join deliverygoods g" +
  422. " on h.deliverygoodsid = g.deliverygoodsid" +
  423. " left join wrstandard wd" +
  424. " on h.wrstandardid = wd.wrstandardid" +
  425. " left join enumdicitem e" +
  426. " on e.enumitemname = wd.unitid" +
  427. " and e.enumdiccode = 'goodsunit'"
  428. sqlId.FormatParam(r.WRUSERID)
  429. return sqlId.String()
  430. }
  431. func (r *WrPosition) buildSql2() string {
  432. return r.buildSql3()
  433. }
  434. func (r *WrPosition) buildSql3() string {
  435. var sqlId utils.SQLVal = "SELECT to_char(t.WRPOSITIONID) WRPOSITIONID," +
  436. " t.WRUSERID," +
  437. " t.TOTALQTY," +
  438. " t.INCREASEQTY," +
  439. " t.DECREASEQTY," +
  440. " t.FREEZEQTY," +
  441. " t.RELATEDQTY," +
  442. " to_char(t.MODIFYTIME, 'yyyy-mm-dd hh24:mi:ss') MODIFYTIME," +
  443. " t.MODIFIERID," +
  444. " t.TRADEWRQTY," +
  445. " t.ACCOUNTID," +
  446. " t.CREDITTOTALQTY," +
  447. " t.CREDITCURQTY," +
  448. " t.CREDITFREEZEQTY," +
  449. " t.CREDITTRADEQTY," +
  450. " to_char(w.WRFACTORTYPEID) WRFACTORTYPEID," +
  451. " w.WRFACTORTYPENAME2 WRFACTORTYPENAME," +
  452. " w.DELIVERYGOODSID," +
  453. " w.WAREHOUSEID," +
  454. " w.WRSTANDARDID," +
  455. " w.WRSTANDARDCODE," +
  456. " w.OPTIONCOMPARE," +
  457. " dg.deliverygoodscode," +
  458. " dg.deliverygoodsname," +
  459. " wd.wrstandardcode," +
  460. " wd.wrstandardname," +
  461. " e.enumdicname" +
  462. " FROM WRPOSITION t" +
  463. " LEFT JOIN WRFACTORTYPE w" +
  464. " on t.wrfactortypeid = w.wrfactortypeid" +
  465. " LEFT JOIN DELIVERYGOODS dg" +
  466. " on t.deliverygoodsid = dg.deliverygoodsid" +
  467. " LEFT JOIN WRSTANDARD wd" +
  468. " on w.wrstandardid = wd.wrstandardid" +
  469. " left join enumdicitem e" +
  470. " on e.enumitemname = wd.unitid" +
  471. " and e.enumdiccode = 'goodsunit'" +
  472. " WHERE 1 = 1"
  473. sqlId.And("t.WRUSERID", r.WRUSERID)
  474. return sqlId.String()
  475. }
  476. // GetDataEx 获取仓单贸易持仓汇总
  477. func (r *WrPosition) GetDataEx() (interface{}, error) {
  478. sData := make([]WrPosition, 0)
  479. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  480. for i := range sData {
  481. sData[i].calc()
  482. sData[i].QueryType = r.QueryType
  483. }
  484. // 查履约中的数量
  485. if len(sData) > 0 {
  486. if r.QueryType == 1 || r.QueryType == 2 {
  487. m := WrPerformanPlanSum{QueryType: r.QueryType, USERID: r.WRUSERID}
  488. if d, err := m.GetData(); err == nil {
  489. for i := range sData {
  490. for _, sum := range d {
  491. sData[i].addPerformancePlanQty(sum)
  492. }
  493. }
  494. }
  495. }
  496. }
  497. return sData, err
  498. }
  499. // WrOrderDetail 仓单贸易委托单
  500. type WrOrderDetail struct {
  501. HASWR int32 `json:"haswr" xorm:"HASWR" form:"haswr"` // 是否有仓单-卖方 - 0:无仓单 1:有仓单
  502. WRTRADEORDERID string `json:"wrtradeorderid" xorm:"WRTRADEORDERID"` // 仓单贸易委托单ID(320+Unix秒时间戳(10位)+xxxxxx)
  503. TRADEDATE string `json:"tradedate" xorm:"TRADEDATE" form:"tradedate"` // 交易日(yyyyMMdd)
  504. WRTRADETYPE int32 `json:"wrtradetype" xorm:"WRTRADETYPE" form:"wrtradetype"` // 仓单贸易类型 - 1:挂牌 2:摘牌 3:提货卖(文化中国) 4:提货买(文化中国)
  505. BUYORSELL int32 `json:"buyorsell" xorm:"BUYORSELL" form:"buyorsell"` // 买卖 - 0:买 1:卖(挂牌类型 买为采购 卖为销售)
  506. MARKETID int64 `json:"marketid" xorm:"MARKETID" form:"marketid"` // 市场ID
  507. USERID int64 `json:"userid" xorm:"USERID" form:"userid"` // 用户ID
  508. WRFACTORTYPEID string `json:"wrfactortypeid" xorm:"WRFACTORTYPEID"` // 仓单要素类型ID/商品ID(77)
  509. ORDERQTY float64 `json:"orderqty" xorm:"ORDERQTY" form:"orderqty"` // 委托数量
  510. TRADEQTY float64 `json:"tradeqty" xorm:"TRADEQTY" form:"tradeqty"` // 成交数量
  511. CANCELQTY float64 `json:"cancelqty" xorm:"CANCELQTY" form:"cancelqty"` // 撤销数量
  512. WRPRICETYPE int32 `json:"wrpricetype" xorm:"WRPRICETYPE" form:"wrpricetype"` // 价格方式 - 1:固定价 2-浮动价 - [挂牌]
  513. FIXEDPRICE float64 `json:"fixedprice" xorm:"FIXEDPRICE" form:"fixedprice"` // 固定价格 - [挂牌]
  514. PRICEMOVE float64 `json:"pricemove" xorm:"PRICEMOVE" form:"pricemove"` // 升贴水(浮动价时填写) - [挂牌]
  515. TRADEPRICE float64 `json:"tradeprice" xorm:"TRADEPRICE" form:"tradeprice"` // 成交价格 - [摘牌] (浮动价 ((商品1价格*商品1价格系数+升贴水)* 商品1重量系数 + 商品2价格*商品2价格系数+商品2升贴水)* 商品2重量系数 ...)* 委托单价格系数 + 委托单升贴水)
  516. ORDERTIME string `json:"ordertime" xorm:"ORDERTIME" form:"ordertime"` // 委托时间(挂牌时间)
  517. WRTRADEORDERSTATUS int32 `json:"wrtradeorderstatus" xorm:"WRTRADEORDERSTATUS"` // 委托状态 - 1:委托请求 2:待冻结 3:委托成功 4:委托失败 5:配对成功 6:已撤 7:已成 8:成交失败 9:委托拒绝 10:挂牌部分成交 11:仓单生成失败 12:首付扣款失败 13:履约失败 14:撤单解冻贷款失败 15:部成部撤
  518. GOODSID int32 `json:"goodsid" xorm:"'GOODSID'"` // 期货商品id
  519. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 期货合约
  520. FUTUPRICEMOVE float64 `json:"futupricemove" xorm:"'FUTUPRICEMOVE'"` // 期货升贴水
  521. DELIVERYMONTH string `json:"deliverymonth" xorm:"'DELIVERYMONTH'"` // 交收月
  522. Wr2FactorType `xorm:"extends"`
  523. PageEx `xorm:"extends"`
  524. FUTUSHOW string `json:"futushow"` // 期货合约/升贴水
  525. }
  526. func (r *WrOrderDetail) calc() {
  527. if r.GOODSCODE == "" {
  528. r.FUTUSHOW = "-"
  529. } else {
  530. r.FUTUSHOW = fmt.Sprintf("%v//%.2f", r.GOODSCODE, r.FUTUPRICEMOVE)
  531. }
  532. r.Wr2FactorType.Calc()
  533. }
  534. func (r *WrOrderDetail) buildSql() string {
  535. var sqlId utils.SQLVal = "with tmp as" +
  536. " (select to_char(t.wrfactortypeid) wrfactortypeid," +
  537. " t.wrfactortypename2 wrfactortypename," +
  538. " t.wrstandardid," +
  539. " t.deliverygoodsid," +
  540. " g.deliverygoodscode," +
  541. " g.deliverygoodsname," +
  542. " w.wrstandardcode," +
  543. " w.wrstandardname," +
  544. " h.warehousecode," +
  545. " h.warehousename," +
  546. " e.enumdicname," +
  547. " w.wrstandardname || t.wrfactortypename2 as wrtypename" +
  548. " from wrfactortype t" +
  549. " left join deliverygoods g" +
  550. " on t.deliverygoodsid = g.deliverygoodsid" +
  551. " left join wrstandard w" +
  552. " on t.wrstandardid = w.wrstandardid" +
  553. " left join warehouseinfo h" +
  554. " on t.warehouseid = h.autoid" +
  555. " left join enumdicitem e on w.unitid = e.enumitemname and e.enumdiccode='goodsunit')" +
  556. "select t.HASWR," +
  557. " to_char(t.WRTRADEORDERID) WRTRADEORDERID," +
  558. " t.TRADEDATE," +
  559. " t.DELIVERYMONTH," +
  560. " t.WRTRADETYPE," +
  561. " t.BUYORSELL," +
  562. " t.MARKETID," +
  563. " t.USERID," +
  564. " t.ORDERQTY," +
  565. " t.TRADEQTY," +
  566. " t.CANCELQTY," +
  567. " t.WRPRICETYPE," +
  568. " t.FIXEDPRICE," +
  569. " t.PRICEMOVE," +
  570. " t.TRADEPRICE," +
  571. " to_char(t.ORDERTIME,'yyyy-mm-dd hh24:mi:ss') ORDERTIME," +
  572. " t.WRTRADEORDERSTATUS," +
  573. " c.GOODSID," +
  574. " c.GOODSCODE," +
  575. " c.pricemove FUTUPRICEMOVE," +
  576. " k.*" +
  577. " from wrtrade_orderdetail t" +
  578. " left join wrtrade_goodsconfig c" +
  579. " on t.wrtradeorderid = c.wrtradeorderid" +
  580. " left join tmp k" +
  581. " on t.wrfactortypeid = k.wrfactortypeid" +
  582. " where t.WRTRADETYPE = 1 and t.isspecified=0"
  583. sqlId.And("t.USERID", r.USERID)
  584. sqlId.And("t.haswr", r.HASWR)
  585. sqlId.AndEx("t.marketid", r.MARKETID, r.MARKETID > 0)
  586. sqlId.Join(" order by t.ORDERTIME desc")
  587. sqlId.Page(r.Page, r.PageSize)
  588. return sqlId.String()
  589. }
  590. // GetDataByPage 获取仓单贸易委托单
  591. func (r *WrOrderDetail) GetDataByPage() (interface{}, error, int, int, int) {
  592. sData := make([]WrOrderDetail, 0)
  593. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  594. total := 0
  595. for i := range sData {
  596. sData[i].calc()
  597. total = sData[i].Total
  598. }
  599. return sData, err, r.Page, r.PageSize, total
  600. }
  601. // WrTradeDetail 仓单贸易成交明细
  602. type WrTradeDetail struct {
  603. HASWR int32 `json:"haswr" xorm:"HASWR" form:"haswr"` // 是否有仓单-卖方 - 0:无仓单 1:有仓单
  604. WRTRADEDETAILID string `json:"wrtradedetailid" xorm:"WRTRADEDETAILID"` // 仓单贸易成交单ID(321+Unix秒时间戳(10位)+xxxxxx)
  605. TRADEDATE string `json:"tradedate" xorm:"TRADEDATE" form:"tradedate"` // 交易日(yyyyMMdd)
  606. WRTRADETYPE int32 `json:"wrtradetype" xorm:"WRTRADETYPE" form:"wrtradetype"` // 仓单贸易类型 - 1:挂牌 2:摘牌 3:提货卖(文化中国) 4:提货买(文化中国)
  607. BUYORSELL int32 `json:"buyorsell" xorm:"BUYORSELL" form:"buyorsell"` // 买卖 - 0:买 1:卖(挂牌类型 买为采购 卖为销售)
  608. MARKETID int64 `json:"marketid" xorm:"MARKETID" form:"marketid"` // 市场ID
  609. USERID int64 `json:"userid" xorm:"USERID" form:"userid"` // 用户ID
  610. WRFACTORTYPEID int64 `json:"wrfactortypeid" xorm:"WRFACTORTYPEID"` // 仓单要素类型ID/商品ID(77)
  611. TRADEQTY float64 `json:"tradeqty" xorm:"TRADEQTY" form:"tradeqty"` // 成交数量
  612. TRADEPRICE float64 `json:"tradeprice" xorm:"TRADEPRICE" form:"tradeprice"` // 成交价格
  613. TRADETIME string `json:"tradetime" xorm:"TRADETIME" form:"tradetime"` // 成交时间
  614. DELIVERYMONTH string `json:"deliverymonth" xorm:"'DELIVERYMONTH'"` // 交收月
  615. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID"` // 资金账号
  616. MATCHACCOUNTID int64 `json:"matchaccountid" xorm:"MATCHACCOUNTID"` // 对手账号
  617. MATCHUSERNAME string `json:"matchusername" xorm:"'MATCHUSERNAME'"` // 对手方
  618. Wr2FactorType `xorm:"extends"`
  619. PageEx `xorm:"extends"`
  620. }
  621. func (r *WrTradeDetail) calc() {
  622. r.Wr2FactorType.Calc()
  623. }
  624. func (r *WrTradeDetail) buildSql() string {
  625. var sqlId utils.SQLVal = "with tmp as" +
  626. " (select to_char(t.wrfactortypeid) wrfactortypeid," +
  627. " t.wrfactortypename," +
  628. " t.wrstandardid," +
  629. " t.deliverygoodsid," +
  630. " g.deliverygoodscode," +
  631. " g.deliverygoodsname," +
  632. " w.wrstandardcode," +
  633. " w.wrstandardname," +
  634. " h.warehousecode," +
  635. " h.warehousename," +
  636. " e.enumdicname," +
  637. " w.wrstandardname || t.wrfactortypename2 as wrtypename" +
  638. " from wrfactortype t" +
  639. " left join deliverygoods g" +
  640. " on t.deliverygoodsid = g.deliverygoodsid" +
  641. " left join wrstandard w" +
  642. " on t.wrstandardid = w.wrstandardid" +
  643. " left join warehouseinfo h" +
  644. " on t.warehouseid = h.autoid" +
  645. " left join enumdicitem e on w.unitid = e.enumitemname and e.enumdiccode='goodsunit')" +
  646. "select t.HASWR," +
  647. " to_char(t.WRTRADEDETAILID) WRTRADEDETAILID," +
  648. " t.TRADEDATE," +
  649. " t.DELIVERYMONTH," +
  650. " t.WRTRADETYPE," +
  651. " t.BUYORSELL," +
  652. " t.MARKETID," +
  653. " t.TRADEQTY," +
  654. " t.TRADEPRICE," +
  655. " to_char(t.TRADETIME,'yyyy-mm-dd hh24:mi:ss') TRADETIME," +
  656. " u2.accountname matchusername," +
  657. " ta1.userid," +
  658. " k.*" +
  659. " from wrtrade_tradedetail t" +
  660. " left join tmp k" +
  661. " on t.wrfactortypeid = k.wrfactortypeid" +
  662. " left join taaccount ta1 on t.accountid=ta1.accountid" +
  663. " left join taaccount ta2 on t.matchaccountid=ta2.accountid" +
  664. " left join useraccount u2 on ta2.userid=u2.userid" +
  665. " where 1=1"
  666. sqlId.And("ta1.userid", r.USERID)
  667. sqlId.And("t.haswr", r.HASWR)
  668. sqlId.AndEx("t.marketid", r.MARKETID, r.MARKETID > 0)
  669. sqlId.Join(" order by t.tradetime desc")
  670. sqlId.Page(r.Page, r.PageSize)
  671. return sqlId.String()
  672. }
  673. // GetDataByPage 获取仓单贸易成交明细
  674. func (r *WrTradeDetail) GetDataByPage() (interface{}, error, int, int, int) {
  675. sData := make([]WrTradeDetail, 0)
  676. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  677. total := 0
  678. for i := range sData {
  679. sData[i].calc()
  680. total = sData[i].Total
  681. }
  682. return sData, err, r.Page, r.PageSize, total
  683. }
  684. // WrSpecialMatchOrder 指定对手委托
  685. type WrSpecialMatchOrder struct {
  686. HASWR int32 `json:"haswr" xorm:"HASWR" form:"haswr"` // 是否有仓单-卖方 - 0:无仓单 1:有仓单
  687. WRTRADEORDERID string `json:"wrtradeorderid" xorm:"WRTRADEORDERID"` // 仓单贸易委托单ID(320+Unix秒时间戳(10位)+xxxxxx)
  688. TRADEDATE string `json:"tradedate" xorm:"TRADEDATE" form:"tradedate"` // 交易日(yyyyMMdd)
  689. WRTRADETYPE int32 `json:"wrtradetype" xorm:"WRTRADETYPE" form:"wrtradetype"` // 仓单贸易类型 - 1:挂牌 2:摘牌 3:提货卖(文化中国) 4:提货买(文化中国)
  690. BUYORSELL int32 `json:"buyorsell" xorm:"BUYORSELL" form:"buyorsell"` // 买卖 - 0:买 1:卖(挂牌类型 买为采购 卖为销售)
  691. MARKETID int64 `json:"marketid" xorm:"MARKETID" form:"marketid"` // 市场ID
  692. USERID int64 `json:"userid" xorm:"USERID" form:"userid"` // 用户ID
  693. WRFACTORTYPEID int64 `json:"wrfactortypeid" xorm:"WRFACTORTYPEID"` // 仓单要素类型ID/商品ID(77)
  694. ORDERQTY float64 `json:"orderqty" xorm:"ORDERQTY" form:"orderqty"` // 委托数量
  695. TRADEQTY float64 `json:"tradeqty" xorm:"TRADEQTY" form:"tradeqty"` // 成交数量
  696. CANCELQTY float64 `json:"cancelqty" xorm:"CANCELQTY" form:"cancelqty"` // 撤销数量
  697. WRPRICETYPE int32 `json:"wrpricetype" xorm:"WRPRICETYPE" form:"wrpricetype"` // 价格方式 - 1:固定价 2-浮动价 - [挂牌]
  698. FIXEDPRICE float64 `json:"fixedprice" xorm:"FIXEDPRICE" form:"fixedprice"` // 固定价格(价格)
  699. PRICEMOVE float64 `json:"pricemove" xorm:"PRICEMOVE" form:"pricemove"` // 升贴水(浮动价时填写) - [挂牌]
  700. TRADEPRICE float64 `json:"tradeprice" xorm:"TRADEPRICE" form:"tradeprice"` // 成交价格
  701. ORDERTIME string `json:"ordertime" xorm:"ORDERTIME" form:"ordertime"` // 委托时间
  702. WRTRADEORDERSTATUS int32 `json:"wrtradeorderstatus" xorm:"WRTRADEORDERSTATUS"` // 委托状态 - 1:委托请求 2:待冻结 3:委托成功 4:委托失败 5:配对成功 6:已撤 7:已成 8:成交失败 9:委托拒绝 10:挂牌部分成交 11:仓单生成失败 12:首付扣款失败 13:履约失败 14:撤单解冻贷款失败 15:部成部撤
  703. USERNAME string `json:"username" xorm:"'USERNAME'"` // 发起方
  704. MATCHUSERNAME string `json:"matchusername" xorm:"'MATCHUSERNAME'"` // 指定方
  705. MATCHUSERID int64 `json:"matchuserid" xorm:"'MATCHUSERID'"` // 指定方资金账号(字段名为userid,实际存的是accountid by:邓工)
  706. Wr2FactorType `xorm:"extends"`
  707. PageEx `xorm:"extends"`
  708. AMOUNT float64 `json:"amount"` // 金额 = 数量 * 价格
  709. }
  710. func (r *WrSpecialMatchOrder) calc() {
  711. r.AMOUNT = r.FIXEDPRICE * r.ORDERQTY
  712. r.Wr2FactorType.Calc()
  713. }
  714. func (r *WrSpecialMatchOrder) buildSql() string {
  715. var sqlId utils.SQLVal = "with tmp as" +
  716. " (select t.wrfactortypeid," +
  717. " t.wrfactortypename2 wrfactortypename," +
  718. " t.wrstandardid," +
  719. " t.deliverygoodsid," +
  720. " g.deliverygoodscode," +
  721. " g.deliverygoodsname," +
  722. " w.wrstandardcode," +
  723. " w.wrstandardname," +
  724. " h.warehousecode," +
  725. " h.warehousename," +
  726. " e.enumdicname," +
  727. " w.wrstandardname || t.wrfactortypename2 as wrtypename" +
  728. " from wrfactortype t" +
  729. " left join deliverygoods g" +
  730. " on t.deliverygoodsid = g.deliverygoodsid" +
  731. " left join wrstandard w" +
  732. " on t.wrstandardid = w.wrstandardid" +
  733. " left join warehouseinfo h" +
  734. " on t.warehouseid = h.autoid" +
  735. " left join enumdicitem e" +
  736. " on w.unitid = e.enumitemname" +
  737. " and e.enumdiccode = 'goodsunit')" +
  738. "select t.HASWR," +
  739. " to_char(t.WRTRADEORDERID) WRTRADEORDERID," +
  740. " t.WRTRADETYPE," +
  741. " t.BUYORSELL," +
  742. " t.MARKETID," +
  743. " t.USERID," +
  744. " t.ORDERQTY," +
  745. " t.TRADEQTY," +
  746. " t.CANCELQTY," +
  747. " t.WRPRICETYPE," +
  748. " t.FIXEDPRICE," +
  749. " t.PRICEMOVE," +
  750. " t.TRADEPRICE," +
  751. " to_char(t.ORDERTIME, 'yyyy-mm-dd hh24:mi:ss') ORDERTIME," +
  752. " t.WRTRADEORDERSTATUS," +
  753. " m.matchuserid," +
  754. " u1.accountname username," +
  755. " u2.accountname matchusername," +
  756. " k.*" +
  757. " from wrtrade_orderdetail t" +
  758. " left join WRTrade_MatchDetail m on t.wrtradeorderid=m.wrtradeorderid" +
  759. " left join useraccount u1 on t.userid=u1.userid" +
  760. " left join taaccount ta2 on m.matchuserid=ta2.accountid" +
  761. " left join useraccount u2 on ta2.userid=u2.userid" +
  762. " left join tmp k" +
  763. " on t.wrfactortypeid = k.wrfactortypeid" +
  764. " where t.WRTRADETYPE = 1" +
  765. " and t.isspecified = 1" +
  766. " and t.WRTRADEORDERSTATUS = 3"
  767. sqlId.And("t.USERID", r.USERID)
  768. sqlId.And("t.haswr", r.HASWR)
  769. sqlId.AndEx("t.marketid", r.MARKETID, r.MARKETID > 0)
  770. sqlId.Join(" order by t.ORDERTIME desc")
  771. sqlId.Page(r.Page, r.PageSize)
  772. return sqlId.String()
  773. }
  774. // GetDataEx 获取指定对手委托
  775. func (r *WrSpecialMatchOrder) GetDataByPage() (interface{}, error, int, int, int) {
  776. sData := make([]WrSpecialMatchOrder, 0)
  777. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  778. total := 0
  779. for i := range sData {
  780. sData[i].calc()
  781. total = sData[i].Total
  782. }
  783. return sData, err, r.Page, r.PageSize, total
  784. }
  785. // WrFactorTypeInfo 仓单要素选择项
  786. type WrFactorTypeInfo struct {
  787. WRFACTORTYPEID string `json:"wrfactortypeid" xorm:"'WRFACTORTYPEID'" form:"wrfactortypeid"` // 仓单要素ID
  788. DGFACTORYITEMTYPEID int64 `json:"dgfactoryitemtypeid" xorm:"'DGFACTORYITEMTYPEID'"` // 要素选择项ID (1-仓库 2-品牌)
  789. ITEMTYPENAME string `json:"itemtypename" xorm:"'ITEMTYPENAME'"` // 要素项名称
  790. DGFACTORYITEMVALUE string `json:"dgfactoryitemvalue" xorm:"'DGFACTORYITEMVALUE'"` // 要素项值
  791. OPTIONCOMPARE string `json:"optioncompare" xorm:"'OPTIONCOMPARE'"` // 选择比较串
  792. WAREHOUSEID int64 `json:"warehouseid" xorm:"'WAREHOUSEID'"` // 仓库id
  793. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 品种名称
  794. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 品种代码
  795. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  796. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  797. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类id
  798. DGFACTORYITEMID int64 `json:"dgfactoryitemid" xorm:"'DGFACTORYITEMID'"` // 选择项id
  799. WRFACTORTYPENAME string `json:"wrfactortypename" xorm:"'WRFACTORTYPENAME'"` // 要素名称
  800. WRFACTORTYPENAME2 string `json:"wrfactortypename2" xorm:"'WRFACTORTYPENAME2'"` // 要素名称(不含仓库)
  801. }
  802. func (r *WrFactorTypeInfo) calc() {
  803. }
  804. func (r *WrFactorTypeInfo) buildSql() string {
  805. var sqlId utils.SQLVal = "select to_char(t.wrfactortypeid) wrfactortypeid," +
  806. " b.dgfactoryitemtypeid," +
  807. " b.itemtypename," +
  808. " c.dgfactoryitemvalue," +
  809. " t.optioncompare," +
  810. " t.warehouseid," +
  811. " t.wrstandardid," +
  812. " g.deliverygoodsname," +
  813. " g.deliverygoodscode," +
  814. " w.wrstandardcode," +
  815. " w.wrstandardname," +
  816. " c.dgfactoryitemid," +
  817. " t.wrfactortypename," +
  818. " t.wrfactortypename2" +
  819. " from wrfactortype t" +
  820. " inner join wrfactortypeitem a" +
  821. " on t.wrfactortypeid = a.wrfactortypeid" +
  822. " inner join dgfactoryitemtype b" +
  823. " on t.deliverygoodsid = b.deliverygoodsid" +
  824. " and a.dgfactoryitemtypeid = b.dgfactoryitemtypeid" +
  825. " left join dgfactoryitem c" +
  826. " on a.dgfactoryitemid = c.dgfactoryitemid" +
  827. " and b.deliverygoodsid = c.deliverygoodsid" +
  828. " left join deliverygoods g on t.deliverygoodsid=g.deliverygoodsid" +
  829. " left join wrstandard w on t.wrstandardid=w.wrstandardid" +
  830. " where 1 = 1"
  831. sqlId.And("t.wrfactortypeid", r.WRFACTORTYPEID)
  832. return sqlId.String()
  833. }
  834. // GetData 获取仓单要素选择项
  835. func (r *WrFactorTypeInfo) GetData() ([]WrFactorTypeInfo, error) {
  836. sData := make([]WrFactorTypeInfo, 0)
  837. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  838. for i := range sData {
  839. sData[i].calc()
  840. }
  841. return sData, err
  842. }
  843. // GetDataEx 获取仓单要素选择项
  844. func (r *WrFactorTypeInfo) GetDataEx() (interface{}, error) {
  845. return r.GetData()
  846. }
  847. // WrGoodsPerformanceStep 商品履约步骤
  848. type WrGoodsPerformanceStep struct {
  849. AUTOID int64 `json:"autoid" xorm:"AUTOID" form:"autoid"` // AutoID
  850. TEMPLATEID int64 `json:"templateid" xorm:"TEMPLATEID" form:"templateid"` // 履约计划模板ID
  851. STEPINDEX int32 `json:"stepindex" xorm:"STEPINDEX" form:"stepindex"` // 步骤序号
  852. STEPDAYS int32 `json:"stepdays" xorm:"STEPDAYS" form:"stepdays"` // 距离上一步天数
  853. STEPTYPEID int32 `json:"steptypeid" xorm:"STEPTYPEID" form:"steptypeid"` // 履约步骤类型ID - 1:买方支付 2:卖方收款 3:买方自提 4:卖方发货 5:买方确认货 6:卖方发票 7:买方确认票 8:仓单转移 9:释放卖方冻结 10:货款溢短 11:生成合同[中江] 12:运费 90:确认支付 91. 确认放行 92买方支付(直接扣款) 用于-1模板“
  854. STEPVALUE SFLOAT64 `json:"stepvalue" xorm:"STEPVALUE" form:"stepvalue"` // 步骤值
  855. ISAUTO int32 `json:"isauto" xorm:"ISAUTO" form:"isauto"` // 是否自动 - 0:不自动 1:自动
  856. REMARK string `json:"remark" xorm:"REMARK" form:"remark"` // 备注
  857. STEPTYPENAME string `json:"steptypename" xorm:"'STEPTYPENAME'"` // 步骤名称
  858. STEPINFO string `json:"stepinfo"` // 步骤信息(步骤名称+步骤值)
  859. STEPDAYSINFO string `json:"stepdate"` // 天数信息(T+N)
  860. WRSTANDARDID int64 `json:"-" form:"wrstandardid"` // 商品id
  861. MARKETID int64 `json:"-" form:"marketid"` // 市场id
  862. HASWR int32 `json:"-" form:"haswr"` // 是否有仓单-卖方 - 0:无仓单 1:有仓单
  863. }
  864. func (r *WrGoodsPerformanceStep) calc() {
  865. switch r.STEPTYPEID {
  866. case 1, 2:
  867. r.STEPINFO = fmt.Sprintf("%v% v%%", r.STEPTYPENAME, r.STEPVALUE*100)
  868. default:
  869. if r.STEPVALUE == 0 {
  870. r.STEPINFO = r.STEPTYPENAME
  871. } else {
  872. r.STEPINFO = fmt.Sprintf("%v %v", r.STEPTYPENAME, r.STEPVALUE)
  873. }
  874. }
  875. r.STEPDAYSINFO = fmt.Sprintf("T+%v", r.STEPDAYS)
  876. }
  877. func (r *WrGoodsPerformanceStep) buildSql() string {
  878. var sqlId utils.SQLVal = "with tmp as" +
  879. " (select decode(%v, 1, t.performancetempid, t.performancetempid2) tempid" +
  880. " from marketspotgoodsconfig t" +
  881. " where rownum <= 1 %v)" +
  882. "select t.*, s.steptypename" +
  883. " from PerformanceStepTemplate t" +
  884. " inner join PerformanceStepType s" +
  885. " on t.steptypeid = s.steptypeid" +
  886. " inner join tmp" +
  887. " on t.templateid = tmp.tempid" +
  888. " order by t.stepindex"
  889. var param utils.SQLVal
  890. param.And("t.wrstandardid", r.WRSTANDARDID)
  891. param.AndEx("t.marketid", r.MARKETID, r.MARKETID > 0)
  892. sqlId.FormatParam(r.HASWR, param.String())
  893. return sqlId.String()
  894. }
  895. // GetDataEx 获取商品履约步骤
  896. func (r *WrGoodsPerformanceStep) GetData() ([]WrGoodsPerformanceStep, error) {
  897. sData := make([]WrGoodsPerformanceStep, 0)
  898. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  899. for i := range sData {
  900. sData[i].calc()
  901. sData[i].MARKETID = r.MARKETID
  902. sData[i].WRSTANDARDID = r.WRSTANDARDID
  903. }
  904. return sData, err
  905. }
  906. // WrTypeItem 要素项
  907. type WrTypeItem struct {
  908. NAME string `json:"name"` // 名称
  909. VALUE string `json:"value"` // 值
  910. }
  911. // WrGoodsInfo 仓单商品信息(仓单要素列展开+履约计划步骤)
  912. type WrGoodsInfo struct {
  913. WRFACTORTYPEID string `json:"-" form:"wrfactortypeid"` // 仓单要素ID
  914. MARKETID int64 `json:"marketid" form:"marketid"` // 市场id
  915. HASWR int32 `json:"haswr" form:"haswr"` // 0:仓单预售 1:仓单贸易
  916. LstItem []WrTypeItem `json:"lstitem"` // 商品信息项
  917. LstStep []WrGoodsPerformanceStep `json:"lststep"` // 履约规则
  918. }
  919. // GetDataEx 获取仓单商品信息(仓单要素列展开+履约计划步骤)
  920. func (r *WrGoodsInfo) GetDataEx() (interface{}, error) {
  921. sData := make([]WrGoodsInfo, 0)
  922. mWrType := WrFactorTypeInfo{WRFACTORTYPEID: r.WRFACTORTYPEID}
  923. // 获取要素项
  924. if sType, err := mWrType.GetData(); err == nil {
  925. v := WrGoodsInfo{LstItem: make([]WrTypeItem, 0), LstStep: make([]WrGoodsPerformanceStep, 0)}
  926. if len(sType) > 0 {
  927. a := sType[0]
  928. v.LstItem = append(v.LstItem, WrTypeItem{NAME: "品种", VALUE: a.DELIVERYGOODSNAME})
  929. v.LstItem = append(v.LstItem, WrTypeItem{NAME: "品类", VALUE: a.WRSTANDARDNAME})
  930. for _, b := range sType {
  931. v.LstItem = append(v.LstItem, WrTypeItem{NAME: b.ITEMTYPENAME, VALUE: b.DGFACTORYITEMVALUE})
  932. }
  933. // 获取履约规则
  934. mStep := WrGoodsPerformanceStep{MARKETID: r.MARKETID, WRSTANDARDID: a.WRSTANDARDID, HASWR: r.HASWR}
  935. if sStep, err := mStep.GetData(); err == nil && len(sStep) > 0 {
  936. v.LstStep = append(v.LstStep, sStep...)
  937. }
  938. }
  939. v.MARKETID = r.MARKETID
  940. v.HASWR = r.HASWR
  941. v.WRFACTORTYPEID = r.WRFACTORTYPEID
  942. sData = append(sData, v)
  943. }
  944. return sData, nil
  945. }
  946. // WrPerformancePlan 履约信息(履约计划)
  947. type WrPerformancePlan struct {
  948. PERFORMANCEPLANID int64 `json:"performanceplanid" xorm:"PERFORMANCEPLANID" form:"performanceplanid"` // 履约计划ID(130+yyMMddHHmmss+xxxx)
  949. PERFORMANCETYPE int32 `json:"performancetype" xorm:"PERFORMANCETYPE" form:"performancetype"` // 履约类型 - 0:通用 1:交割 2:仓单贸易 3:预售集采 4:竞拍-降价式 (无仓单) 5:挂牌期权 6:竞拍-降价式 7:竞拍-竞价式 8:竞拍-大宗式 9:荷兰式 10:法币C2C 11:报价系统 12:挂牌系统
  950. RELATEDORDERID string `json:"relatedorderid" xorm:"RELATEDORDERID" form:"relatedorderid"` // 关联单号(履约类型的关联单号)
  951. AMOUNT float64 `json:"amount" xorm:"AMOUNT" form:"amount"` // 履约金额(总金额)
  952. PAYMENTTYPE int32 `json:"paymenttype" xorm:"PAYMENTTYPE" form:"paymenttype"` // 付款方式 - 1:冻结 2:扣款
  953. PERFORMANCESTATUS int32 `json:"performancestatus" xorm:"PERFORMANCESTATUS" form:"performancestatus"` // 履约状态 - 1:初始化 2:正常 3:处理错误 4:违约待处理 5:违约处理中 6:完成 7.违约已完成 8:释放冻结失败 9:超时待处理 10:超时关闭
  954. CURSTEPID int64 `json:"curstepid" xorm:"CURSTEPID" form:"curstepid"` // 当前步骤ID
  955. BUYACCOUNTID int64 `json:"buyaccountid" xorm:"BUYACCOUNTID" form:"buyaccountid"` // 买方账号
  956. WRTRANSFERUSERID int64 `json:"wrtransferuserid" xorm:"WRTRANSFERUSERID" form:"wrtransferuserid"` // 仓单受让用户
  957. BUYPAIDAMOUNT float64 `json:"buypaidamount" xorm:"BUYPAIDAMOUNT" form:"buypaidamount"` // 买方已冻/已扣金额 (已付金额)
  958. SELLACCOUNTID int64 `json:"sellaccountid" xorm:"SELLACCOUNTID" form:"sellaccountid"` // 卖方账号
  959. SELLRECEIVEDAMOUNT float64 `json:"sellreceivedamount" xorm:"SELLRECEIVEDAMOUNT"` // 卖方已收金额
  960. CREATETIME string `json:"createtime" xorm:"CREATETIME" form:"createtime"` // 创建时间(开始时间)
  961. CREATORID int64 `json:"creatorid" xorm:"CREATORID" form:"creatorid"` // 创建人
  962. REMARK string `json:"remark" xorm:"REMARK" form:"remark"` // 备注
  963. BUYERFREEZEAMOUNT float64 `json:"buyerfreezeamount" xorm:"BUYERFREEZEAMOUNT"` // 履约冻结(买履约)
  964. BUYERFREEZEAMOUNTREMAIN float64 `json:"buyerfreezeamountremain" xorm:"BUYERFREEZEAMOUNTREMAIN"` // 履约冻结剩余(买履约)
  965. SELLERFREEZEAMOUNT float64 `json:"sellerfreezeamount" xorm:"SELLERFREEZEAMOUNT"` // 履约冻结(卖履约)
  966. SELLERFREEZEAMOUNTREMAIN float64 `json:"sellerfreezeamountremain" xorm:"SELLERFREEZEAMOUNTREMAIN"` // 履约冻结剩余(卖履约)
  967. OVERSHORTAMOUNT float64 `json:"overshortamount" xorm:"OVERSHORTAMOUNT" form:"overshortamount"` // 溢短金额
  968. HASOVERSHORT int32 `json:"hasovershort" xorm:"HASOVERSHORT" form:"hasovershort"` // 是否确认溢短 - 0:没有 1:买方确认 2:卖方确认
  969. SHIPREMARK string `json:"shipremark" xorm:"SHIPREMARK" form:"shipremark"` // 发货备注[物流单号]
  970. MIDDLEUSERID int64 `json:"middleuserid" xorm:"MIDDLEUSERID" form:"middleuserid"` // 中间商用户ID
  971. MIDDLEACCOUNTID int64 `json:"middleaccountid" xorm:"MIDDLEACCOUNTID" form:"middleaccountid"` // 中间商资金账号ID
  972. HASWR int32 `json:"haswr" xorm:"HASWR" form:"haswr"` // 类型 0-仓单预售 1-仓单贸易
  973. OVERSHORTQTY float64 `json:"overshortqty" xorm:"OVERSHORTQTY" form:"overshortqty"` // 溢短数量
  974. MARKETID int32 `json:"marketid" xorm:"MARKETID" form:"marketid"` // 市场ID
  975. EXPRESSFEECONFIRMED int32 `json:"expressfeeconfirmed" xorm:"EXPRESSFEECONFIRMED"` // 是否确认运费 - 0:无 1:买方确认 2:卖方确认
  976. EXPRESSFEE float64 `json:"expressfee" xorm:"EXPRESSFEE" form:"expressfee"` // 运费
  977. CONTRACTID int64 `json:"contractid" xorm:"CONTRACTID" form:"contractid"` // 合同ID
  978. CURSTEPDEADLINE string `json:"curstepdeadline" xorm:"CURSTEPDEADLINE" form:"curstepdeadline"` // 当前步骤到期时间
  979. QTY float64 `json:"qty" xorm:"QTY" form:"qty"` // 履约数量
  980. Wr2FactorType `xorm:"extends"`
  981. CURSTEPNAME string `json:"curstepname" xorm:"'CURSTEPNAME'"` // 当前步骤名称
  982. BUYUSERNAME string `json:"buyusername" xorm:"'BUYUSERNAME'"` // 买方名字
  983. SELLUSERNAME string `json:"sellusername" xorm:"'SELLUSERNAME'"` // 卖方名字
  984. USERID int64 `json:"userid" form:"userid" binding:"required"` // 用户id
  985. BUYORSELL int32 `json:"buyorsell" form:"buyorsell"` // 买卖方向 0-买 1-卖
  986. }
  987. func (r *WrPerformancePlan) calc() {
  988. r.Wr2FactorType.Calc()
  989. }
  990. func (r *WrPerformancePlan) buildSql() string {
  991. var sqlId utils.SQLVal = "with tmp as" +
  992. " (select to_char(t.wrfactortypeid) wrfactortypeid," +
  993. " t.wrfactortypename," +
  994. " t.wrstandardid," +
  995. " t.deliverygoodsid," +
  996. " g.deliverygoodscode," +
  997. " g.deliverygoodsname," +
  998. " w.wrstandardcode," +
  999. " w.wrstandardname," +
  1000. " h.warehousecode," +
  1001. " h.warehousename," +
  1002. " e.enumdicname," +
  1003. " w.wrstandardname || t.wrfactortypename2 as wrtypename" +
  1004. " from wrfactortype t" +
  1005. " left join deliverygoods g" +
  1006. " on t.deliverygoodsid = g.deliverygoodsid" +
  1007. " left join wrstandard w" +
  1008. " on t.wrstandardid = w.wrstandardid" +
  1009. " left join warehouseinfo h" +
  1010. " on t.warehouseid = h.autoid" +
  1011. " left join enumdicitem e" +
  1012. " on w.unitid = e.enumitemname" +
  1013. " and e.enumdiccode = 'goodsunit'" +
  1014. ")" +
  1015. "select to_char(t.PERFORMANCEPLANID) PERFORMANCEPLANID," +
  1016. " t.PERFORMANCETYPE," +
  1017. " to_char(t.RELATEDORDERID) RELATEDORDERID," +
  1018. " t.AMOUNT," +
  1019. " t.PAYMENTTYPE," +
  1020. " t.PERFORMANCESTATUS," +
  1021. " t.CURSTEPID," +
  1022. " t.BUYACCOUNTID," +
  1023. " t.WRTRANSFERUSERID," +
  1024. " t.BUYPAIDAMOUNT," +
  1025. " t.SELLACCOUNTID," +
  1026. " t.SELLRECEIVEDAMOUNT," +
  1027. " t.BUYTODAYAMOUNT," +
  1028. " t.SELLTODAYAMOUNT," +
  1029. " to_char(t.CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME," +
  1030. " t.CREATORID," +
  1031. " t.REMARK," +
  1032. " t.BUYERFREEZEAMOUNT," +
  1033. " t.BUYERFREEZEAMOUNTREMAIN," +
  1034. " t.SELLERFREEZEAMOUNT," +
  1035. " t.SELLERFREEZEAMOUNTREMAIN," +
  1036. " t.OVERSHORTAMOUNT," +
  1037. " t.HASOVERSHORT," +
  1038. " t.SHIPREMARK," +
  1039. " t.MIDDLEUSERID," +
  1040. " t.MIDDLEACCOUNTID," +
  1041. " t.HASWR," +
  1042. " t.BUYERINFO," +
  1043. " t.SELLERINFO," +
  1044. " t.OVERSHORTQTY," +
  1045. " t.MARKETID," +
  1046. " t.EXPRESSFEECONFIRMED," +
  1047. " t.EXPRESSFEE," +
  1048. " t.CONTRACTID," +
  1049. " to_char(t.CURSTEPDEADLINE, 'yyyy-mm-dd hh24:mi:ss') CURSTEPDEADLINE," +
  1050. " t.QTY," +
  1051. " u1.accountname buyusername," +
  1052. " u2.accountname sellusername," +
  1053. " s.steptypename CURSTEPNAME," +
  1054. " tmp.*" +
  1055. " from performanceplan t" +
  1056. " left join taaccount a1" +
  1057. " on t.buyaccountid = a1.accountid" +
  1058. " left join useraccount u1" +
  1059. " on a1.userid = u1.userid" +
  1060. " left join taaccount a2" +
  1061. " on t.sellaccountid = a2.accountid" +
  1062. " left join useraccount u2" +
  1063. " on a2.userid = u2.userid" +
  1064. " left join performancesteptype s" +
  1065. " on t.curstepid = s.steptypeid" +
  1066. " left join performanceplanwr p on t.performanceplanid = p.performanceplanid" +
  1067. " left join warehousereciept f on p.wrid= f.wrid" +
  1068. " left join tmp on f.wrfactortypeid=tmp.wrfactortypeid" +
  1069. " where 1 = 1"
  1070. if r.BUYORSELL == 0 {
  1071. sqlId.And("u1.userid", r.USERID)
  1072. } else {
  1073. sqlId.And("u2.userid", r.USERID)
  1074. }
  1075. sqlId.Join(" order by t.CREATETIME desc")
  1076. return sqlId.String()
  1077. }
  1078. // GetDataEx 获取履约信息(履约计划)
  1079. func (r *WrPerformancePlan) GetDataEx() (interface{}, error) {
  1080. sData := make([]WrPerformancePlan, 0)
  1081. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1082. for i := range sData {
  1083. sData[i].calc()
  1084. }
  1085. return sData, err
  1086. }
  1087. // WrHoldLB 仓单持有记录
  1088. type WrHoldLB struct {
  1089. LADINGBILLID string `json:"ladingbillid" xorm:"LADINGBILLID" form:"ladingbillid"` // 提单ID(208+Unix秒时间戳(10位)+xxxxxx)
  1090. SUBNUM int32 `json:"subnum" xorm:"SUBNUM" form:"subnum"` // 提单子单号
  1091. WRFACTORTYPEID string `json:"wrfactortypeid" xorm:"WRFACTORTYPEID" form:"wrfactortypeid"` // 仓单要素类型ID
  1092. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"DELIVERYGOODSID" form:"deliverygoodsid"` // 品种ID
  1093. QTY float64 `json:"qty" xorm:"QTY" form:"qty"` // 数量
  1094. FREEZERQTY float64 `json:"freezerqty" xorm:"FREEZERQTY" form:"freezerqty"` // 冻结数量
  1095. USERID int64 `json:"userid" xorm:"USERID" form:"userid"` // 持有人ID
  1096. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID" form:"accountid"` // 持有人帐户ID
  1097. CREATETIME string `json:"createtime" xorm:"CREATETIME" form:"createtime"` // 创建时间(过户时间)
  1098. TRADEDATE string `json:"tradedate" xorm:"TRADEDATE" form:"tradedate"` // 交易日(yyyyMMdd)
  1099. WRID string `json:"wrid" xorm:"WRID" form:"wrid"` // 仓单变更ID
  1100. PARENTSUBNUM int32 `json:"parentsubnum" xorm:"PARENTSUBNUM" form:"parentsubnum"` // 拆分父子单号
  1101. PLEDGEQTY float64 `json:"pledgeqty" xorm:"'-'"` // 质押数量
  1102. Wr2FactorType `xorm:"extends"`
  1103. WRHOLDENO string `json:"wrholdeno"` // 仓单编号
  1104. ENABLEQTY float64 `json:"enableqty"` // 可用数量
  1105. }
  1106. func (r *WrHoldLB) calc() {
  1107. r.WRHOLDENO = fmt.Sprintf("%v_%v", r.LADINGBILLID, r.SUBNUM)
  1108. r.ENABLEQTY = r.QTY - r.FREEZERQTY
  1109. r.Wr2FactorType.Calc()
  1110. }
  1111. func (r *WrHoldLB) buildSql() string {
  1112. var sqlId utils.SQLVal = "with tmp as" +
  1113. " (select t.wrfactortypeid," +
  1114. " t.wrfactortypename," +
  1115. " t.wrstandardid," +
  1116. " t.deliverygoodsid," +
  1117. " t.warehouseid," +
  1118. " g.deliverygoodscode," +
  1119. " g.deliverygoodsname," +
  1120. " w.wrstandardcode," +
  1121. " w.wrstandardname," +
  1122. " h.warehousecode," +
  1123. " h.warehousename," +
  1124. " e.enumdicname," +
  1125. " w.wrstandardname || t.wrfactortypename2 as wrtypename" +
  1126. " from wrfactortype t" +
  1127. " left join deliverygoods g" +
  1128. " on t.deliverygoodsid = g.deliverygoodsid" +
  1129. " left join wrstandard w" +
  1130. " on t.wrstandardid = w.wrstandardid" +
  1131. " left join warehouseinfo h" +
  1132. " on t.warehouseid = h.autoid" +
  1133. " left join enumdicitem e" +
  1134. " on w.unitid = e.enumitemname" +
  1135. " and e.enumdiccode = 'goodsunit'" +
  1136. ")" +
  1137. "SELECT to_char(t.LADINGBILLID) LADINGBILLID," +
  1138. " t.SUBNUM," +
  1139. " to_char(t.WRFACTORTYPEID) WRFACTORTYPEID," +
  1140. " t.DELIVERYGOODSID," +
  1141. " t.QTY," +
  1142. " t.FREEZERQTY," +
  1143. " t.USERID," +
  1144. " t.ACCOUNTID," +
  1145. " to_char(t.CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME," +
  1146. " t.TRADEDATE," +
  1147. " t.WRID," +
  1148. " t.PARENTSUBNUM," +
  1149. " tmp.*" +
  1150. " FROM WRHOLDLB t" +
  1151. " LEFT JOIN tmp on t.wrfactortypeid= tmp.wrfactortypeid" +
  1152. " WHERE 1 = 1"
  1153. sqlId.And("t.ACCOUNTID", r.ACCOUNTID)
  1154. sqlId.AndEx("tmp.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  1155. sqlId.AndEx("tmp.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  1156. sqlId.AndEx("tmp.WAREHOUSEID", r.WAREHOUSEID, r.WAREHOUSEID > 0)
  1157. return sqlId.String()
  1158. }
  1159. // GetDataEx 获取仓单持有记录
  1160. func (r *WrHoldLB) GetDataEx() (interface{}, error) {
  1161. sData := make([]WrHoldLB, 0)
  1162. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1163. for i := range sData {
  1164. sData[i].calc()
  1165. }
  1166. return sData, err
  1167. }
  1168. // WrFilterItem 筛选框列表项
  1169. type WrFilterItem struct {
  1170. ITEMTYPE int32 `json:"itemtype" xorm:"'ITEMTYPE'"` // 类型 1-品种 2-品类(商品) 3-仓库
  1171. ITEMNAME string `json:"itemname" xorm:"'ITEMNAME'"` // 名称
  1172. ITEMCODE string `json:"itemcode" xorm:"'ITEMCODE'"` // 代码
  1173. ITEMID int64 `json:"itemid" xorm:"'ITEMID'"` // 项id
  1174. IType string `json:"-" form:"itype"` // 类型 逗号隔开, 如 1,2
  1175. }
  1176. func (r *WrFilterItem) calc() {
  1177. }
  1178. func (r *WrFilterItem) buildSql() string {
  1179. if r.IType == "" {
  1180. r.IType = "1, 2, 3"
  1181. }
  1182. r.IType = strings.ReplaceAll(r.IType, " ", "")
  1183. sType := strings.Split(r.IType, ",")
  1184. sqls := make([]string, 0)
  1185. for _, v := range sType {
  1186. switch v {
  1187. case "1":
  1188. sqls = append(sqls, "select 1 itemtype, t.deliverygoodsid itemid, t.deliverygoodscode itemcode, to_char(t.deliverygoodsname) itemname from deliverygoods t")
  1189. case "2":
  1190. sqls = append(sqls, "select 2 itemtype, t.wrstandardid itemid, to_char(t.wrstandardcode) itemcode, to_char(t.wrstandardname) itemname from wrstandard t")
  1191. case "3":
  1192. sqls = append(sqls, "select 3 itemtype, t.autoid itemid, t.warehousecode itemcode, to_char(t.warehousename) itemname from warehouseinfo t")
  1193. default:
  1194. }
  1195. }
  1196. var sqlId string
  1197. for _, v := range sqls {
  1198. if len(sqlId) > 0 {
  1199. sqlId += " union all " + v
  1200. } else {
  1201. sqlId = v
  1202. }
  1203. }
  1204. sqlId += " order by itemtype, itemid"
  1205. return sqlId
  1206. }
  1207. // GetDataEx 获取筛选框列表项
  1208. func (r *WrFilterItem) GetDataEx() (interface{}, error) {
  1209. sData := make([]WrFilterItem, 0)
  1210. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1211. for i := range sData {
  1212. sData[i].calc()
  1213. }
  1214. return sData, err
  1215. }
  1216. // WrPerformanPlanSum 履约中数量
  1217. type WrPerformanPlanSum struct {
  1218. USERID int64 `json:"userid" xorm:"'USERID'"` // 用户id
  1219. BUYORSELL int32 `json:"buyorsell" xorm:"'BUYORSELL'"` // 买卖方向
  1220. QTY float64 `json:"qty" xorm:"'QTY'"` // 履约中的数量
  1221. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 品种id
  1222. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类id
  1223. WAREHOUSEID int64 `json:"warehouseid" xorm:"'WAREHOUSEID'"` // 仓库id
  1224. WRTYPENAME string `json:"wrtypename" xorm:"'WRTYPENAME'"` // 商品
  1225. QueryType int32 `json:"querytype" form:"querytype"` // 查询类型 1-现货汇总 2-库存汇总
  1226. }
  1227. func (r *WrPerformanPlanSum) calc() {
  1228. if len(r.WRTYPENAME) > 0 {
  1229. r.WRTYPENAME = strings.ReplaceAll(r.WRTYPENAME, ",", "-")
  1230. }
  1231. }
  1232. // buildSql_XHHZ 现货汇总 履约中的数量
  1233. func (r *WrPerformanPlanSum) buildSql_XHHZ() string {
  1234. var sqlId utils.SQLVal = "with tmp as" +
  1235. " (select t.buyaccountid," +
  1236. " t.sellaccountid," +
  1237. " t.qty," +
  1238. " w.wrfactortypeid," +
  1239. " w.deliverygoodsid," +
  1240. " w.wrstandardid," +
  1241. " w.warehouseid," +
  1242. " wd.wrstandardname || w.wrfactortypename2 wrtypename," +
  1243. " u1.userid buyuserid," +
  1244. " u2.userid selluserid" +
  1245. " from performanceplan t" +
  1246. " left join performanceplanwr p" +
  1247. " on t.performanceplanid = p.performanceplanid" +
  1248. " left join warehousereciept f" +
  1249. " on p.wrid = f.wrid" +
  1250. " left join wrfactortype w" +
  1251. " on f.wrfactortypeid = w.wrfactortypeid" +
  1252. " left join wrstandard wd" +
  1253. " on w.wrstandardid = wd.wrstandardid" +
  1254. " left join taaccount ta1" +
  1255. " on t.buyaccountid = ta1.accountid" +
  1256. " left join useraccount u1" +
  1257. " on ta1.userid = u1.userid" +
  1258. " left join taaccount ta2" +
  1259. " on t.sellaccountid = ta2.accountid" +
  1260. " left join useraccount u2" +
  1261. " on ta2.userid = u2.userid" +
  1262. " where t.performancestatus in (2) and %v in(u1.userid, u2.userid))" +
  1263. "select k.buyuserid userid," +
  1264. " 0 as buyorsell," +
  1265. " k.deliverygoodsid," +
  1266. " k.wrstandardid," +
  1267. " k.wrtypename," +
  1268. " sum(k.qty) qty" +
  1269. " from tmp k" +
  1270. " group by k.buyuserid, k.deliverygoodsid, k.wrstandardid, k.wrtypename" +
  1271. "union all" +
  1272. "select k.selluserid userid," +
  1273. " 1 as buyorsell," +
  1274. " k.deliverygoodsid," +
  1275. " k.wrstandardid," +
  1276. " k.wrtypename," +
  1277. " sum(k.qty) qty" +
  1278. " from tmp k" +
  1279. " group by k.selluserid, k.deliverygoodsid, k.wrstandardid, k.wrtypename"
  1280. sqlId.FormatParam(r.USERID, r.USERID)
  1281. return sqlId.String()
  1282. }
  1283. // buildSql_KCHZ 库存汇总 履约中的数量
  1284. func (r *WrPerformanPlanSum) buildSql_KCHZ() string {
  1285. var sqlId utils.SQLVal = "with tmp as" +
  1286. " (select t.buyaccountid," +
  1287. " t.sellaccountid," +
  1288. " t.qty," +
  1289. " w.wrfactortypeid," +
  1290. " w.deliverygoodsid," +
  1291. " w.wrstandardid," +
  1292. " w.warehouseid," +
  1293. " wd.wrstandardname || w.wrfactortypename2 wrtypename," +
  1294. " u1.userid buyuserid," +
  1295. " u2.userid selluserid" +
  1296. " from performanceplan t" +
  1297. " left join performanceplanwr p" +
  1298. " on t.performanceplanid = p.performanceplanid" +
  1299. " left join warehousereciept f" +
  1300. " on p.wrid = f.wrid" +
  1301. " left join wrfactortype w" +
  1302. " on f.wrfactortypeid = w.wrfactortypeid" +
  1303. " left join wrstandard wd" +
  1304. " on w.wrstandardid = wd.wrstandardid" +
  1305. " left join taaccount ta1" +
  1306. " on t.buyaccountid = ta1.accountid" +
  1307. " left join useraccount u1" +
  1308. " on ta1.userid = u1.userid" +
  1309. " left join taaccount ta2" +
  1310. " on t.sellaccountid = ta2.accountid" +
  1311. " left join useraccount u2" +
  1312. " on ta2.userid = u2.userid" +
  1313. " where t.performancestatus in (2) and %v in(u1.userid, u2.userid)" +
  1314. "select k.buyuserid userid," +
  1315. " 0 as buyorsell," +
  1316. " k.deliverygoodsid," +
  1317. " k.wrstandardid," +
  1318. " k.warehouseid," +
  1319. " k.wrtypename," +
  1320. " sum(k.qty) qty" +
  1321. " from tmp k" +
  1322. " group by k.buyuserid, k.deliverygoodsid, k.wrstandardid, k.warehouseid, k.wrtypename" +
  1323. "union all" +
  1324. "select k.selluserid userid," +
  1325. " 1 as buyorsell," +
  1326. " k.deliverygoodsid," +
  1327. " k.wrstandardid," +
  1328. " k.warehouseid," +
  1329. " k.wrtypename," +
  1330. " sum(k.qty) qty" +
  1331. " from tmp k" +
  1332. " group by k.selluserid, k.deliverygoodsid, k.wrstandardid, k.warehouseid, k.wrtypename"
  1333. sqlId.FormatParam(r.USERID, r.USERID)
  1334. return sqlId.String()
  1335. }
  1336. func (r *WrPerformanPlanSum) buildSql() string {
  1337. if r.QueryType == 1 {
  1338. return r.buildSql_XHHZ()
  1339. } else {
  1340. return r.buildSql_KCHZ()
  1341. }
  1342. }
  1343. // GetData 获取履约中数量
  1344. func (r *WrPerformanPlanSum) GetData() ([]WrPerformanPlanSum, error) {
  1345. sData := make([]WrPerformanPlanSum, 0)
  1346. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1347. for i := range sData {
  1348. sData[i].calc()
  1349. }
  1350. return sData, err
  1351. }
  1352. // WrFAProductDetail 融资产品列表
  1353. type WrFAProductDetail struct {
  1354. PRODUCTDETAILID int64 `json:"productdetailid" xorm:"PRODUCTDETAILID"` // 产品组合ID(SEQ_FINANCEAREAPRODUCTDETAIL)
  1355. PRODUCTID int64 `json:"productid" xorm:"PRODUCTID"` // 金融产品ID
  1356. AREAUSERID int64 `json:"areauserid" xorm:"AREAUSERID"` // 金融机构ID
  1357. FINANCINGDAYS int32 `json:"financingdays" xorm:"FINANCINGDAYS"` // 天数(天)
  1358. MARGINRATIO float64 `json:"marginratio" xorm:"MARGINRATIO"` // 保证金比率质押率 - [仓单质押]
  1359. INTERESTRATEMODE int32 `json:"interestratemode" xorm:"INTERESTRATEMODE"` // 利率方式 - 1:年利率 2:日利率
  1360. INTERESTRATE float64 `json:"interestrate" xorm:"INTERESTRATE"` // 利率
  1361. INTERESTMINLEN int32 `json:"interestminlen" xorm:"INTERESTMINLEN"` // 最小起息天数
  1362. INTERESTSETTLEMODE int32 `json:"interestsettlemode" xorm:"INTERESTSETTLEMODE"` // 结息方式 - 1:按日结息 3:按月结息
  1363. INTERESTSETTLEVALUE int32 `json:"interestsettlevalue" xorm:"INTERESTSETTLEVALUE"` // 结息方式值
  1364. UPDATETIME string `json:"updatetime" xorm:"UPDATETIME"` // 更新时间
  1365. ISAUTOLOAN int32 `json:"isautoloan" xorm:"'ISAUTOLOAN'"` // 是否自动放款 0-手动 1-自动 (当为自动放款时,显示"放款快")
  1366. PRODUCTNAME string `json:"productname" xorm:"'PRODUCTNAME'"` // 产品名称
  1367. USERNAME string `json:"username" xorm:"'USERNAME'"` // 融出方
  1368. ACCOUNTID int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 资金账号
  1369. LOANTAG string `json:"-"` // 放款标识: "放款快" 或者为空
  1370. WRSTANDARDID int64 `json:"-"` // 品类(商品)id
  1371. BRANDID int64 `json:"-"` // 品牌id
  1372. WAREHOUSEID int64 `json:"-"` // 仓库id
  1373. WRFACTORTYPEID string `json:"wrfactortypeid" form:"wrfactortypeid"` // 仓单要素ID
  1374. }
  1375. func (r *WrFAProductDetail) calc() {
  1376. r.LOANTAG = IFValue(r.ISAUTOLOAN == 1, "放款快", "").(string)
  1377. }
  1378. func (r *WrFAProductDetail) buildSql() string {
  1379. var sqlId utils.SQLVal = "with tmp as" +
  1380. " (select distinct f.productid, f.isautoloan" +
  1381. " from FinanceAreaProductLimit f" +
  1382. " where 1 = 1" +
  1383. " and f.wrstandardid = %v" +
  1384. " and (f.spotgoodsbrandid like '%%,%v,%%' or f.spotgoodsbrandid like '%%,0,%%')" +
  1385. " and (f.warehouseinfoid like '%%,%v,%%' or f.warehouseinfoid like '%%,0,%%'))" +
  1386. "select t.PRODUCTDETAILID," +
  1387. " t.PRODUCTID," +
  1388. " t.AREAUSERID," +
  1389. " t.FINANCINGDAYS," +
  1390. " t.MARGINRATIO," +
  1391. " t.INTERESTRATEMODE," +
  1392. " t.INTERESTRATE," +
  1393. " t.INTERESTMINLEN," +
  1394. " t.INTERESTSETTLEMODE," +
  1395. " t.INTERESTSETTLEVALUE," +
  1396. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1397. " p.productname," +
  1398. " p.accountid," +
  1399. " u.accountname username," +
  1400. " k.isautoloan" +
  1401. " from FinanceAreaProductDetail t" +
  1402. " left join FinanceAreaProduct p" +
  1403. " on t.productid = p.productid" +
  1404. " left join useraccount u" +
  1405. " on t.areauserid = u.userid" +
  1406. " inner join tmp k" +
  1407. " on t.productid = k.productid"
  1408. sqlId.FormatParam(r.WRSTANDARDID, r.BRANDID, r.WAREHOUSEID)
  1409. return sqlId.String()
  1410. }
  1411. // GetDataEx 获取融资产品列表
  1412. func (r *WrFAProductDetail) GetDataEx() (interface{}, error) {
  1413. sData := make([]WrFAProductDetail, 0)
  1414. m := WrFactorTypeInfo{WRFACTORTYPEID: r.WRFACTORTYPEID}
  1415. if d, err := m.GetData(); err == nil {
  1416. var brandid int64 = 0
  1417. for _, v := range d {
  1418. if v.DGFACTORYITEMTYPEID == 2 {
  1419. brandid = v.DGFACTORYITEMID
  1420. }
  1421. }
  1422. if brandid > 0 && len(d) > 0 {
  1423. r.BRANDID = brandid
  1424. r.WRSTANDARDID = d[0].WRSTANDARDID
  1425. r.WAREHOUSEID = d[0].WAREHOUSEID
  1426. _ = db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1427. for i := range sData {
  1428. sData[i].calc()
  1429. sData[i].WRSTANDARDID = r.WRSTANDARDID
  1430. sData[i].BRANDID = r.BRANDID
  1431. sData[i].WAREHOUSEID = r.WAREHOUSEID
  1432. sData[i].WRFACTORTYPEID = r.WRFACTORTYPEID
  1433. }
  1434. }
  1435. }
  1436. return sData, nil
  1437. }