wrTrade2.go 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  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:"ORDERQTY"` // 买(卖)量(=委托量-成交量)
  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 t.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. func (r *Wr2FactorType) Calc() {
  321. if r.WRTYPENAME == "" {
  322. if len(r.WRFACTORTYPENAME) > 0 {
  323. r.WRTYPENAME = r.WRSTANDARDNAME + r.WRFACTORTYPENAME
  324. } else {
  325. r.WRTYPENAME = r.WRSTANDARDNAME
  326. }
  327. }
  328. r.WRTYPENAME = strings.ReplaceAll(r.WRTYPENAME, ",", "-")
  329. }
  330. // WrPosition 仓单贸易持仓汇总
  331. type WrPosition struct {
  332. WRPOSITIONID string `json:"wrpositionid" xorm:"WRPOSITIONID" form:"wrpositionid"` // 仓单头寸ID(201+Unix秒时间戳(10位)+xxxxxx)
  333. WRUSERID int64 `json:"wruserid" xorm:"WRUSERID" form:"userid"` // 持有人ID
  334. TOTALQTY float64 `json:"totalqty" xorm:"TOTALQTY" form:"totalqty"` // 总数量(库存数量)
  335. INCREASEQTY float64 `json:"-" xorm:"INCREASEQTY" form:"increaseqty"` // 今日增加数量
  336. DECREASEQTY float64 `json:"-" xorm:"DECREASEQTY" form:"decreaseqty"` // 今日减少数量
  337. FREEZEQTY float64 `json:"freezeqty" xorm:"FREEZEQTY" form:"freezeqty"` // 总冻结数量
  338. RELATEDQTY float64 `json:"-" xorm:"RELATEDQTY" form:"relatedqty"` // 关联数量
  339. MODIFYTIME string `json:"modifytime" xorm:"MODIFYTIME" form:"modifytime"` // 修改时间(过户时间)
  340. MODIFIERID int64 `json:"-" xorm:"MODIFIERID" form:"modifierid"` // 修改人
  341. TRADEWRQTY float64 `json:"-" xorm:"TRADEWRQTY" form:"tradewrqty"` // 订单仓单头寸总数量
  342. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID" form:"accountid"` // 帐户ID
  343. CREDITTOTALQTY float64 `json:"credittotalqty" xorm:"CREDITTOTALQTY" form:"credittotalqty"` // 授信总数量(预售仓单:预售总量)
  344. CREDITCURQTY float64 `json:"creditcurqty" xorm:"CREDITCURQTY" form:"creditcurqty"` // 授信当前数量(预售仓单:预售当前量)
  345. CREDITFREEZEQTY float64 `json:"creditfreezeqty" xorm:"CREDITFREEZEQTY" form:"creditfreezeqty"` // 授信冻结数量(预售仓单: 当前数量)
  346. CREDITTRADEQTY float64 `json:"credittradeqty" xorm:"CREDITTRADEQTY" form:"credittradeqty"` // 授信成交数量(预售仓单:成交总量)
  347. Wr2FactorType `xorm:"extends"`
  348. ENALBEQTY float64 `json:"enalbeqty" xorm:"'ENALBEQTY'"` // 可用数量
  349. CREDITENABLEQTY float64 `json:"creditenableqty" xorm:"'CREDITENABLEQTY'"` // 授信可用数量(预售仓单: 可用数量)
  350. PLEDGEQTY float64 `json:"pledgeqty" xorm:"'-'"` // 质押数量
  351. BUYEXCUTEQTY float64 `json:"buyexcuteqty"` // 采购履约中数量
  352. SELLEXCUTEQTY float64 `json:"sellexcuteqty"` // 销售履约中数量
  353. WRPOSITIONNO string `json:"wrpositionno"` // 仓单编号
  354. QueryType int32 `json:"querytype" form:"querytype"` // 查询类型 1-现货汇总 2-库存汇总(订单汇总) 3-现货明细
  355. }
  356. func (r *WrPosition) calc() {
  357. r.ENALBEQTY = r.TOTALQTY - r.FREEZEQTY
  358. r.CREDITENABLEQTY = r.CREDITCURQTY - r.CREDITFREEZEQTY
  359. r.WRPOSITIONNO = fmt.Sprintf("%v", r.WRFACTORTYPEID)
  360. r.Wr2FactorType.Calc()
  361. }
  362. func (r *WrPosition) buildSql() string {
  363. if r.QueryType == 1 {
  364. // 现货汇总 品种 + 品类 + 其它要素, 根据品种汇总
  365. return r.buildSql1()
  366. } else if r.QueryType == 2 {
  367. // 库存汇总 品种 + 仓库 + 品类 + 其它要素, 即全维度
  368. return r.buildSql2()
  369. } else {
  370. // 现货明细 仓单编号 + 品种 + 仓库 + 仓单要素 即全维度 跟库存汇总一样
  371. return r.buildSql3()
  372. }
  373. }
  374. func (r *WrPosition) buildSql1() string {
  375. var sqlId utils.SQLVal = "with tmp as" +
  376. " (select t.*," +
  377. " w.wrstandardid," +
  378. " w.warehouseid," +
  379. " wd.wrstandardname," +
  380. " wd.wrstandardname || w.wrfactortypename2 as wrtypename" +
  381. " from WRPOSITION t" +
  382. " left join WRFACTORTYPE w" +
  383. " on t.wrfactortypeid = w.wrfactortypeid" +
  384. " left join wrstandard wd" +
  385. " on w.wrstandardid = wd.wrstandardid" +
  386. " where t.wruserid = %v)," +
  387. "hld as" +
  388. " (select k.deliverygoodsid," +
  389. " k.wrtypename," +
  390. " k.wrstandardid," +
  391. " sum(k.TOTALQTY) TOTALQTY," +
  392. " sum(k.INCREASEQTY) INCREASEQTY," +
  393. " sum(k.DECREASEQTY) DECREASEQTY," +
  394. " sum(k.FREEZEQTY) FREEZEQTY," +
  395. " sum(k.RELATEDQTY) RELATEDQTY," +
  396. " sum(k.TRADEWRQTY) TRADEWRQTY," +
  397. " sum(k.CREDITTOTALQTY) CREDITTOTALQTY," +
  398. " sum(k.CREDITCURQTY) CREDITCURQTY," +
  399. " sum(k.CREDITFREEZEQTY) CREDITFREEZEQTY," +
  400. " sum(k.CREDITTRADEQTY) CREDITTRADEQTY" +
  401. " from tmp k" +
  402. " where 1 = 1" +
  403. " group by k.deliverygoodsid, k.wrstandardname, k.wrtypename, k.wrstandardid)" +
  404. "select h.*, g.deliverygoodsname, g.deliverygoodscode, e.enumdicname" +
  405. " from hld h" +
  406. " left join deliverygoods g" +
  407. " on h.deliverygoodsid = g.deliverygoodsid" +
  408. " left join wrstandard wd" +
  409. " on h.wrstandardid = wd.wrstandardid" +
  410. " left join enumdicitem e" +
  411. " on e.enumitemname = wd.unitid" +
  412. " and e.enumdiccode = 'goodsunit'"
  413. sqlId.FormatParam(r.WRUSERID)
  414. return sqlId.String()
  415. }
  416. func (r *WrPosition) buildSql2() string {
  417. return r.buildSql3()
  418. }
  419. func (r *WrPosition) buildSql3() string {
  420. var sqlId utils.SQLVal = "SELECT to_char(t.WRPOSITIONID) WRPOSITIONID," +
  421. " t.WRUSERID," +
  422. " t.TOTALQTY," +
  423. " t.INCREASEQTY," +
  424. " t.DECREASEQTY," +
  425. " t.FREEZEQTY," +
  426. " t.RELATEDQTY," +
  427. " to_char(t.MODIFYTIME, 'yyyy-mm-dd hh24:mi:ss') MODIFYTIME," +
  428. " t.MODIFIERID," +
  429. " t.TRADEWRQTY," +
  430. " t.ACCOUNTID," +
  431. " t.CREDITTOTALQTY," +
  432. " t.CREDITCURQTY," +
  433. " t.CREDITFREEZEQTY," +
  434. " t.CREDITTRADEQTY," +
  435. " to_char(w.WRFACTORTYPEID) WRFACTORTYPEID," +
  436. " w.WRFACTORTYPENAME2 WRFACTORTYPENAME," +
  437. " w.DELIVERYGOODSID," +
  438. " w.WAREHOUSEID," +
  439. " w.WRSTANDARDID," +
  440. " w.WRSTANDARDCODE," +
  441. " w.OPTIONCOMPARE," +
  442. " dg.deliverygoodscode," +
  443. " dg.deliverygoodsname," +
  444. " wd.wrstandardcode," +
  445. " wd.wrstandardname," +
  446. " e.enumdicname" +
  447. " FROM WRPOSITION t" +
  448. " LEFT JOIN WRFACTORTYPE w" +
  449. " on t.wrfactortypeid = w.wrfactortypeid" +
  450. " LEFT JOIN DELIVERYGOODS dg" +
  451. " on t.deliverygoodsid = dg.deliverygoodsid" +
  452. " LEFT JOIN WRSTANDARD wd" +
  453. " on w.wrstandardid = wd.wrstandardid" +
  454. " left join enumdicitem e" +
  455. " on e.enumitemname = wd.unitid" +
  456. " and e.enumdiccode = 'goodsunit'" +
  457. " WHERE 1 = 1"
  458. sqlId.And("t.WRUSERID", r.WRUSERID)
  459. return sqlId.String()
  460. }
  461. // GetDataEx 获取仓单贸易持仓汇总
  462. func (r *WrPosition) GetDataEx() (interface{}, error) {
  463. sData := make([]WrPosition, 0)
  464. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  465. for i := range sData {
  466. sData[i].calc()
  467. sData[i].QueryType = r.QueryType
  468. }
  469. return sData, err
  470. }
  471. // WrOrderDetail 仓单贸易委托单
  472. type WrOrderDetail struct {
  473. HASWR int32 `json:"haswr" xorm:"HASWR" form:"haswr"` // 是否有仓单-卖方 - 0:无仓单 1:有仓单
  474. WRTRADEORDERID string `json:"wrtradeorderid" xorm:"WRTRADEORDERID"` // 仓单贸易委托单ID(320+Unix秒时间戳(10位)+xxxxxx)
  475. TRADEDATE string `json:"tradedate" xorm:"TRADEDATE" form:"tradedate"` // 交易日(yyyyMMdd)
  476. WRTRADETYPE int32 `json:"wrtradetype" xorm:"WRTRADETYPE" form:"wrtradetype"` // 仓单贸易类型 - 1:挂牌 2:摘牌 3:提货卖(文化中国) 4:提货买(文化中国)
  477. BUYORSELL int32 `json:"buyorsell" xorm:"BUYORSELL" form:"buyorsell"` // 买卖 - 0:买 1:卖(挂牌类型 买为采购 卖为销售)
  478. MARKETID int64 `json:"marketid" xorm:"MARKETID" form:"marketid"` // 市场ID
  479. USERID int64 `json:"userid" xorm:"USERID" form:"userid"` // 用户ID
  480. WRFACTORTYPEID string `json:"wrfactortypeid" xorm:"WRFACTORTYPEID"` // 仓单要素类型ID/商品ID(77)
  481. ORDERQTY float64 `json:"orderqty" xorm:"ORDERQTY" form:"orderqty"` // 委托数量
  482. TRADEQTY float64 `json:"tradeqty" xorm:"TRADEQTY" form:"tradeqty"` // 成交数量
  483. CANCELQTY float64 `json:"cancelqty" xorm:"CANCELQTY" form:"cancelqty"` // 撤销数量
  484. WRPRICETYPE int32 `json:"wrpricetype" xorm:"WRPRICETYPE" form:"wrpricetype"` // 价格方式 - 1:固定价 2-浮动价 - [挂牌]
  485. FIXEDPRICE float64 `json:"fixedprice" xorm:"FIXEDPRICE" form:"fixedprice"` // 固定价格 - [挂牌]
  486. PRICEMOVE float64 `json:"pricemove" xorm:"PRICEMOVE" form:"pricemove"` // 升贴水(浮动价时填写) - [挂牌]
  487. TRADEPRICE float64 `json:"tradeprice" xorm:"TRADEPRICE" form:"tradeprice"` // 成交价格 - [摘牌] (浮动价 ((商品1价格*商品1价格系数+升贴水)* 商品1重量系数 + 商品2价格*商品2价格系数+商品2升贴水)* 商品2重量系数 ...)* 委托单价格系数 + 委托单升贴水)
  488. ORDERTIME string `json:"ordertime" xorm:"ORDERTIME" form:"ordertime"` // 委托时间(挂牌时间)
  489. WRTRADEORDERSTATUS int32 `json:"wrtradeorderstatus" xorm:"WRTRADEORDERSTATUS"` // 委托状态 - 1:委托请求 2:待冻结 3:委托成功 4:委托失败 5:配对成功 6:已撤 7:已成 8:成交失败 9:委托拒绝 10:挂牌部分成交 11:仓单生成失败 12:首付扣款失败 13:履约失败 14:撤单解冻贷款失败 15:部成部撤
  490. GOODSID int32 `json:"goodsid" xorm:"'GOODSID'"` // 期货商品id
  491. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 期货合约
  492. FUTUPRICEMOVE float64 `json:"futupricemove" xorm:"'FUTUPRICEMOVE'"` // 期货升贴水
  493. DELIVERYMONTH string `json:"deliverymonth" xorm:"'DELIVERYMONTH'"` // 交收月
  494. Wr2FactorType `xorm:"extends"`
  495. PageEx `xorm:"extends"`
  496. FUTUSHOW string `json:"futushow"` // 期货合约/升贴水
  497. }
  498. func (r *WrOrderDetail) calc() {
  499. if r.GOODSCODE == "" {
  500. r.FUTUSHOW = "-"
  501. } else {
  502. r.FUTUSHOW = fmt.Sprintf("%v//%.2f", r.GOODSCODE, r.FUTUPRICEMOVE)
  503. }
  504. r.Wr2FactorType.Calc()
  505. }
  506. func (r *WrOrderDetail) buildSql() string {
  507. var sqlId utils.SQLVal = "with tmp as" +
  508. " (select to_char(t.wrfactortypeid) wrfactortypeid," +
  509. " t.wrfactortypename2 wrfactortypename," +
  510. " t.wrstandardid," +
  511. " t.deliverygoodsid," +
  512. " g.deliverygoodscode," +
  513. " g.deliverygoodsname," +
  514. " w.wrstandardcode," +
  515. " w.wrstandardname," +
  516. " h.warehousecode," +
  517. " h.warehousename," +
  518. " e.enumdicname," +
  519. " w.wrstandardname || t.wrfactortypename2 as wrtypename" +
  520. " from wrfactortype t" +
  521. " left join deliverygoods g" +
  522. " on t.deliverygoodsid = g.deliverygoodsid" +
  523. " left join wrstandard w" +
  524. " on t.wrstandardid = w.wrstandardid" +
  525. " left join warehouseinfo h" +
  526. " on t.warehouseid = h.autoid" +
  527. " left join enumdicitem e on w.unitid = e.enumitemname and e.enumdiccode='goodsunit')" +
  528. "select t.HASWR," +
  529. " to_char(t.WRTRADEORDERID) WRTRADEORDERID," +
  530. " t.TRADEDATE," +
  531. " t.DELIVERYMONTH," +
  532. " t.WRTRADETYPE," +
  533. " t.BUYORSELL," +
  534. " t.MARKETID," +
  535. " t.USERID," +
  536. " t.ORDERQTY," +
  537. " t.TRADEQTY," +
  538. " t.CANCELQTY," +
  539. " t.WRPRICETYPE," +
  540. " t.FIXEDPRICE," +
  541. " t.PRICEMOVE," +
  542. " t.TRADEPRICE," +
  543. " to_char(t.ORDERTIME,'yyyy-mm-dd hh24:mi:ss') ORDERTIME," +
  544. " t.WRTRADEORDERSTATUS," +
  545. " c.GOODSID," +
  546. " c.GOODSCODE," +
  547. " c.pricemove FUTUPRICEMOVE," +
  548. " k.*" +
  549. " from wrtrade_orderdetail t" +
  550. " left join wrtrade_goodsconfig c" +
  551. " on t.wrtradeorderid = c.wrtradeorderid" +
  552. " left join tmp k" +
  553. " on t.wrfactortypeid = k.wrfactortypeid" +
  554. " where t.WRTRADETYPE = 1 and t.isspecified=0"
  555. sqlId.And("t.USERID", r.USERID)
  556. sqlId.And("t.haswr", r.HASWR)
  557. sqlId.AndEx("t.marketid", r.MARKETID, r.MARKETID > 0)
  558. sqlId.Join(" order by t.ORDERTIME desc")
  559. sqlId.Page(r.Page, r.PageSize)
  560. return sqlId.String()
  561. }
  562. // GetDataByPage 获取仓单贸易委托单
  563. func (r *WrOrderDetail) GetDataByPage() (interface{}, error, int, int, int) {
  564. sData := make([]WrOrderDetail, 0)
  565. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  566. total := 0
  567. for i := range sData {
  568. sData[i].calc()
  569. total = sData[i].Total
  570. }
  571. return sData, err, r.Page, r.PageSize, total
  572. }
  573. // WrTradeDetail 仓单贸易成交明细
  574. type WrTradeDetail struct {
  575. HASWR int32 `json:"haswr" xorm:"HASWR" form:"haswr"` // 是否有仓单-卖方 - 0:无仓单 1:有仓单
  576. WRTRADEDETAILID string `json:"wrtradedetailid" xorm:"WRTRADEDETAILID"` // 仓单贸易成交单ID(321+Unix秒时间戳(10位)+xxxxxx)
  577. TRADEDATE string `json:"tradedate" xorm:"TRADEDATE" form:"tradedate"` // 交易日(yyyyMMdd)
  578. WRTRADETYPE int32 `json:"wrtradetype" xorm:"WRTRADETYPE" form:"wrtradetype"` // 仓单贸易类型 - 1:挂牌 2:摘牌 3:提货卖(文化中国) 4:提货买(文化中国)
  579. BUYORSELL int32 `json:"buyorsell" xorm:"BUYORSELL" form:"buyorsell"` // 买卖 - 0:买 1:卖(挂牌类型 买为采购 卖为销售)
  580. MARKETID int64 `json:"marketid" xorm:"MARKETID" form:"marketid"` // 市场ID
  581. USERID int64 `json:"userid" xorm:"USERID" form:"userid"` // 用户ID
  582. WRFACTORTYPEID int64 `json:"wrfactortypeid" xorm:"WRFACTORTYPEID"` // 仓单要素类型ID/商品ID(77)
  583. TRADEQTY float64 `json:"tradeqty" xorm:"TRADEQTY" form:"tradeqty"` // 成交数量
  584. TRADEPRICE float64 `json:"tradeprice" xorm:"TRADEPRICE" form:"tradeprice"` // 成交价格
  585. TRADETIME string `json:"tradetime" xorm:"TRADETIME" form:"tradetime"` // 成交时间
  586. DELIVERYMONTH string `json:"deliverymonth" xorm:"'DELIVERYMONTH'"` // 交收月
  587. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID"` // 资金账号
  588. MATCHACCOUNTID int64 `json:"matchaccountid" xorm:"MATCHACCOUNTID"` // 对手账号
  589. MATCHUSERNAME string `json:"matchusername" xorm:"'MATCHUSERNAME'"` // 对手方
  590. Wr2FactorType `xorm:"extends"`
  591. PageEx `xorm:"extends"`
  592. }
  593. func (r *WrTradeDetail) calc() {
  594. r.Wr2FactorType.Calc()
  595. }
  596. func (r *WrTradeDetail) buildSql() string {
  597. var sqlId utils.SQLVal = "with tmp as" +
  598. " (select to_char(t.wrfactortypeid) wrfactortypeid," +
  599. " t.wrfactortypename," +
  600. " t.wrstandardid," +
  601. " t.deliverygoodsid," +
  602. " g.deliverygoodscode," +
  603. " g.deliverygoodsname," +
  604. " w.wrstandardcode," +
  605. " w.wrstandardname," +
  606. " h.warehousecode," +
  607. " h.warehousename," +
  608. " e.enumdicname," +
  609. " w.wrstandardname || t.wrfactortypename2 as wrtypename" +
  610. " from wrfactortype t" +
  611. " left join deliverygoods g" +
  612. " on t.deliverygoodsid = g.deliverygoodsid" +
  613. " left join wrstandard w" +
  614. " on t.wrstandardid = w.wrstandardid" +
  615. " left join warehouseinfo h" +
  616. " on t.warehouseid = h.autoid" +
  617. " left join enumdicitem e on w.unitid = e.enumitemname and e.enumdiccode='goodsunit')" +
  618. "select t.HASWR," +
  619. " to_char(t.WRTRADEDETAILID) WRTRADEDETAILID," +
  620. " t.TRADEDATE," +
  621. " t.DELIVERYMONTH," +
  622. " t.WRTRADETYPE," +
  623. " t.BUYORSELL," +
  624. " t.MARKETID," +
  625. " t.TRADEQTY," +
  626. " t.TRADEPRICE," +
  627. " to_char(t.TRADETIME,'yyyy-mm-dd hh24:mi:ss') TRADETIME," +
  628. " u2.accountname matchusername," +
  629. " ta1.userid," +
  630. " k.*" +
  631. " from wrtrade_tradedetail t" +
  632. " left join tmp k" +
  633. " on t.wrfactortypeid = k.wrfactortypeid" +
  634. " left join taaccount ta1 on t.accountid=ta1.accountid" +
  635. " left join taaccount ta2 on t.matchaccountid=ta2.accountid" +
  636. " left join useraccount u2 on ta2.userid=u2.userid" +
  637. " where 1=1"
  638. sqlId.And("ta1.userid", r.USERID)
  639. sqlId.And("t.haswr", r.HASWR)
  640. sqlId.AndEx("t.marketid", r.MARKETID, r.MARKETID > 0)
  641. sqlId.Join(" order by t.tradetime desc")
  642. sqlId.Page(r.Page, r.PageSize)
  643. return sqlId.String()
  644. }
  645. // GetDataByPage 获取仓单贸易成交明细
  646. func (r *WrTradeDetail) GetDataByPage() (interface{}, error, int, int, int) {
  647. sData := make([]WrTradeDetail, 0)
  648. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  649. total := 0
  650. for i := range sData {
  651. sData[i].calc()
  652. total = sData[i].Total
  653. }
  654. return sData, err, r.Page, r.PageSize, total
  655. }
  656. // WrSpecialMatchOrder 指定对手委托
  657. type WrSpecialMatchOrder struct {
  658. HASWR int32 `json:"haswr" xorm:"HASWR" form:"haswr"` // 是否有仓单-卖方 - 0:无仓单 1:有仓单
  659. WRTRADEORDERID string `json:"wrtradeorderid" xorm:"WRTRADEORDERID"` // 仓单贸易委托单ID(320+Unix秒时间戳(10位)+xxxxxx)
  660. TRADEDATE string `json:"tradedate" xorm:"TRADEDATE" form:"tradedate"` // 交易日(yyyyMMdd)
  661. WRTRADETYPE int32 `json:"wrtradetype" xorm:"WRTRADETYPE" form:"wrtradetype"` // 仓单贸易类型 - 1:挂牌 2:摘牌 3:提货卖(文化中国) 4:提货买(文化中国)
  662. BUYORSELL int32 `json:"buyorsell" xorm:"BUYORSELL" form:"buyorsell"` // 买卖 - 0:买 1:卖(挂牌类型 买为采购 卖为销售)
  663. MARKETID int64 `json:"marketid" xorm:"MARKETID" form:"marketid"` // 市场ID
  664. USERID int64 `json:"userid" xorm:"USERID" form:"userid"` // 用户ID
  665. WRFACTORTYPEID int64 `json:"wrfactortypeid" xorm:"WRFACTORTYPEID"` // 仓单要素类型ID/商品ID(77)
  666. ORDERQTY float64 `json:"orderqty" xorm:"ORDERQTY" form:"orderqty"` // 委托数量
  667. TRADEQTY float64 `json:"tradeqty" xorm:"TRADEQTY" form:"tradeqty"` // 成交数量
  668. CANCELQTY float64 `json:"cancelqty" xorm:"CANCELQTY" form:"cancelqty"` // 撤销数量
  669. WRPRICETYPE int32 `json:"wrpricetype" xorm:"WRPRICETYPE" form:"wrpricetype"` // 价格方式 - 1:固定价 2-浮动价 - [挂牌]
  670. FIXEDPRICE float64 `json:"fixedprice" xorm:"FIXEDPRICE" form:"fixedprice"` // 固定价格(价格)
  671. PRICEMOVE float64 `json:"pricemove" xorm:"PRICEMOVE" form:"pricemove"` // 升贴水(浮动价时填写) - [挂牌]
  672. TRADEPRICE float64 `json:"tradeprice" xorm:"TRADEPRICE" form:"tradeprice"` // 成交价格
  673. ORDERTIME string `json:"ordertime" xorm:"ORDERTIME" form:"ordertime"` // 委托时间
  674. WRTRADEORDERSTATUS int32 `json:"wrtradeorderstatus" xorm:"WRTRADEORDERSTATUS"` // 委托状态 - 1:委托请求 2:待冻结 3:委托成功 4:委托失败 5:配对成功 6:已撤 7:已成 8:成交失败 9:委托拒绝 10:挂牌部分成交 11:仓单生成失败 12:首付扣款失败 13:履约失败 14:撤单解冻贷款失败 15:部成部撤
  675. USERNAME string `json:"username" xorm:"'USERNAME'"` // 发起方
  676. MATCHUSERNAME string `json:"matchusername" xorm:"'MATCHUSERNAME'"` // 指定方
  677. MATCHUSERID int64 `json:"matchuserid" xorm:"'MATCHUSERID'"` // 指定方资金账号(字段名为userid,实际存的是accountid by:邓工)
  678. Wr2FactorType `xorm:"extends"`
  679. PageEx `xorm:"extends"`
  680. AMOUNT float64 `json:"amount"` // 金额 = 数量 * 价格
  681. }
  682. func (r *WrSpecialMatchOrder) calc() {
  683. r.AMOUNT = r.FIXEDPRICE * r.ORDERQTY
  684. r.Wr2FactorType.Calc()
  685. }
  686. func (r *WrSpecialMatchOrder) buildSql() string {
  687. var sqlId utils.SQLVal = "with tmp as" +
  688. " (select t.wrfactortypeid," +
  689. " t.wrfactortypename2 wrfactortypename," +
  690. " t.wrstandardid," +
  691. " t.deliverygoodsid," +
  692. " g.deliverygoodscode," +
  693. " g.deliverygoodsname," +
  694. " w.wrstandardcode," +
  695. " w.wrstandardname," +
  696. " h.warehousecode," +
  697. " h.warehousename," +
  698. " e.enumdicname," +
  699. " w.wrstandardname || t.wrfactortypename2 as wrtypename" +
  700. " from wrfactortype t" +
  701. " left join deliverygoods g" +
  702. " on t.deliverygoodsid = g.deliverygoodsid" +
  703. " left join wrstandard w" +
  704. " on t.wrstandardid = w.wrstandardid" +
  705. " left join warehouseinfo h" +
  706. " on t.warehouseid = h.autoid" +
  707. " left join enumdicitem e" +
  708. " on w.unitid = e.enumitemname" +
  709. " and e.enumdiccode = 'goodsunit')" +
  710. "select t.HASWR," +
  711. " to_char(t.WRTRADEORDERID) WRTRADEORDERID," +
  712. " t.WRTRADETYPE," +
  713. " t.BUYORSELL," +
  714. " t.MARKETID," +
  715. " t.USERID," +
  716. " t.ORDERQTY," +
  717. " t.TRADEQTY," +
  718. " t.CANCELQTY," +
  719. " t.WRPRICETYPE," +
  720. " t.FIXEDPRICE," +
  721. " t.PRICEMOVE," +
  722. " t.TRADEPRICE," +
  723. " to_char(t.ORDERTIME, 'yyyy-mm-dd hh24:mi:ss') ORDERTIME," +
  724. " t.WRTRADEORDERSTATUS," +
  725. " m.matchuserid," +
  726. " u1.accountname username," +
  727. " u2.accountname matchusername," +
  728. " k.*" +
  729. " from wrtrade_orderdetail t" +
  730. " left join WRTrade_MatchDetail m on t.wrtradeorderid=m.wrtradeorderid" +
  731. " left join useraccount u1 on t.userid=u1.userid" +
  732. " left join taaccount ta2 on m.matchuserid=ta2.accountid" +
  733. " left join useraccount u2 on ta2.userid=u2.userid" +
  734. " left join tmp k" +
  735. " on t.wrfactortypeid = k.wrfactortypeid" +
  736. " where t.WRTRADETYPE = 1" +
  737. " and t.isspecified = 1" +
  738. " and t.WRTRADEORDERSTATUS = 3"
  739. sqlId.And("t.USERID", r.USERID)
  740. sqlId.And("t.haswr", r.HASWR)
  741. sqlId.AndEx("t.marketid", r.MARKETID, r.MARKETID > 0)
  742. sqlId.Join(" order by t.ORDERTIME desc")
  743. sqlId.Page(r.Page, r.PageSize)
  744. return sqlId.String()
  745. }
  746. // GetDataEx 获取指定对手委托
  747. func (r *WrSpecialMatchOrder) GetDataByPage() (interface{}, error, int, int, int) {
  748. sData := make([]WrSpecialMatchOrder, 0)
  749. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  750. total := 0
  751. for i := range sData {
  752. sData[i].calc()
  753. total = sData[i].Total
  754. }
  755. return sData, err, r.Page, r.PageSize, total
  756. }
  757. // WrFactorTypeInfo 仓单要素选择项
  758. type WrFactorTypeInfo struct {
  759. WRFACTORTYPEID int64 `json:"wrfactortypeid" xorm:"'WRFACTORTYPEID'" form:"wrfactortypeid"` // 仓单要素ID
  760. DGFACTORYITEMTYPEID int64 `json:"dgfactoryitemtypeid" xorm:"'DGFACTORYITEMTYPEID'"` // 要素选择项ID
  761. ITEMTYPENAME string `json:"itemtypename" xorm:"'ITEMTYPENAME'"` // 要素项名称
  762. DGFACTORYITEMVALUE string `json:"dgfactoryitemvalue" xorm:"'DGFACTORYITEMVALUE'"` // 要素项值
  763. OPTIONCOMPARE string `json:"optioncompare" xorm:"'OPTIONCOMPARE'"` // 选择比较串
  764. WAREHOUSEID int64 `json:"warehouseid" xorm:"'WAREHOUSEID'"` // 仓库id
  765. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 品种名称
  766. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 品种代码
  767. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  768. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  769. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类id
  770. }
  771. func (r *WrFactorTypeInfo) calc() {
  772. }
  773. func (r *WrFactorTypeInfo) buildSql() string {
  774. var sqlId utils.SQLVal = "select to_char(t.wrfactortypeid) wrfactortypeid," +
  775. " b.dgfactoryitemtypeid," +
  776. " b.itemtypename," +
  777. " c.dgfactoryitemvalue," +
  778. " t.optioncompare," +
  779. " t.warehouseid," +
  780. " t.wrstandardid," +
  781. " g.deliverygoodsname," +
  782. " g.deliverygoodscode," +
  783. " w.wrstandardcode," +
  784. " w.wrstandardname" +
  785. " from wrfactortype t" +
  786. " inner join wrfactortypeitem a" +
  787. " on t.wrfactortypeid = a.wrfactortypeid" +
  788. " inner join dgfactoryitemtype b" +
  789. " on t.deliverygoodsid = b.deliverygoodsid" +
  790. " and a.dgfactoryitemtypeid = b.dgfactoryitemtypeid" +
  791. " left join dgfactoryitem c" +
  792. " on a.dgfactoryitemid = c.dgfactoryitemid" +
  793. " and b.deliverygoodsid = c.deliverygoodsid" +
  794. " left join deliverygoods g on t.deliverygoodsid=g.deliverygoodsid" +
  795. " left join wrstandard w on t.wrstandardid=w.wrstandardid" +
  796. " where 1 = 1"
  797. sqlId.And("t.wrfactortypeid", r.WRFACTORTYPEID)
  798. return sqlId.String()
  799. }
  800. // GetData 获取仓单要素选择项
  801. func (r *WrFactorTypeInfo) GetData() ([]WrFactorTypeInfo, error) {
  802. sData := make([]WrFactorTypeInfo, 0)
  803. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  804. for i := range sData {
  805. sData[i].calc()
  806. }
  807. return sData, err
  808. }
  809. // WrGoodsPerformanceStep 商品履约步骤
  810. type WrGoodsPerformanceStep struct {
  811. AUTOID int64 `json:"autoid" xorm:"AUTOID" form:"autoid"` // AutoID
  812. TEMPLATEID int64 `json:"templateid" xorm:"TEMPLATEID" form:"templateid"` // 履约计划模板ID
  813. STEPINDEX int32 `json:"stepindex" xorm:"STEPINDEX" form:"stepindex"` // 步骤序号
  814. STEPDAYS int32 `json:"stepdays" xorm:"STEPDAYS" form:"stepdays"` // 距离上一步天数
  815. 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模板“
  816. STEPVALUE SFLOAT64 `json:"stepvalue" xorm:"STEPVALUE" form:"stepvalue"` // 步骤值
  817. ISAUTO int32 `json:"isauto" xorm:"ISAUTO" form:"isauto"` // 是否自动 - 0:不自动 1:自动
  818. REMARK string `json:"remark" xorm:"REMARK" form:"remark"` // 备注
  819. STEPTYPENAME string `json:"steptypename" xorm:"'STEPTYPENAME'"` // 步骤名称
  820. STEPINFO string `json:"stepinfo"` // 步骤信息(步骤名称+步骤值)
  821. STEPDAYSINFO string `json:"stepdate"` // 天数信息(T+N)
  822. WRSTANDARDID int64 `json:"-" form:"wrstandardid"` // 商品id
  823. MARKETID int64 `json:"-" form:"marketid"` // 市场id
  824. HASWR int32 `json:"-" form:"haswr"` // 是否有仓单-卖方 - 0:无仓单 1:有仓单
  825. }
  826. func (r *WrGoodsPerformanceStep) calc() {
  827. switch r.STEPTYPEID {
  828. case 1, 2:
  829. r.STEPINFO = fmt.Sprintf("%v% v%%", r.STEPTYPENAME, r.STEPVALUE*100)
  830. default:
  831. if r.STEPVALUE == 0 {
  832. r.STEPINFO = r.STEPTYPENAME
  833. } else {
  834. r.STEPINFO = fmt.Sprintf("%v %v", r.STEPTYPENAME, r.STEPVALUE)
  835. }
  836. }
  837. r.STEPDAYSINFO = fmt.Sprintf("T+%v", r.STEPDAYS)
  838. }
  839. func (r *WrGoodsPerformanceStep) buildSql() string {
  840. var sqlId utils.SQLVal = "with tmp as" +
  841. " (select decode(%v, 1, t.performancetempid, t.performancetempid2) tempid" +
  842. " from marketspotgoodsconfig t" +
  843. " where rownum <= 1 %v)" +
  844. "select t.*, s.steptypename" +
  845. " from PerformanceStepTemplate t" +
  846. " inner join PerformanceStepType s" +
  847. " on t.steptypeid = s.steptypeid" +
  848. " inner join tmp" +
  849. " on t.templateid = tmp.tempid" +
  850. " order by t.stepindex"
  851. var param utils.SQLVal
  852. param.And("t.wrstandardid", r.WRSTANDARDID)
  853. param.AndEx("t.marketid", r.MARKETID, r.MARKETID > 0)
  854. sqlId.FormatParam(r.HASWR, param.String())
  855. return sqlId.String()
  856. }
  857. // GetDataEx 获取商品履约步骤
  858. func (r *WrGoodsPerformanceStep) GetData() ([]WrGoodsPerformanceStep, error) {
  859. sData := make([]WrGoodsPerformanceStep, 0)
  860. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  861. for i := range sData {
  862. sData[i].calc()
  863. sData[i].MARKETID = r.MARKETID
  864. sData[i].WRSTANDARDID = r.WRSTANDARDID
  865. }
  866. return sData, err
  867. }
  868. // WrTypeItem 要素项
  869. type WrTypeItem struct {
  870. NAME string `json:"name"` // 名称
  871. VALUE string `json:"value"` // 值
  872. }
  873. // WrGoodsInfo 仓单商品信息(仓单要素列展开+履约计划步骤)
  874. type WrGoodsInfo struct {
  875. WRFACTORTYPEID int64 `json:"-" form:"wrfactortypeid"` // 仓单要素ID
  876. MARKETID int64 `json:"marketid" form:"marketid"` // 市场id
  877. HASWR int32 `json:"haswr" form:"haswr"` // 0:仓单预售 1:仓单贸易
  878. LstItem []WrTypeItem `json:"lstitem"` // 商品信息项
  879. LstStep []WrGoodsPerformanceStep `json:"lststep"` // 履约规则
  880. }
  881. // GetDataEx 获取仓单商品信息(仓单要素列展开+履约计划步骤)
  882. func (r *WrGoodsInfo) GetDataEx() (interface{}, error) {
  883. sData := make([]WrGoodsInfo, 0)
  884. mWrType := WrFactorTypeInfo{WRFACTORTYPEID: r.WRFACTORTYPEID}
  885. // 获取要素项
  886. if sType, err := mWrType.GetData(); err == nil {
  887. v := WrGoodsInfo{LstItem: make([]WrTypeItem, 0), LstStep: make([]WrGoodsPerformanceStep, 0)}
  888. if len(sType) > 0 {
  889. a := sType[0]
  890. v.LstItem = append(v.LstItem, WrTypeItem{NAME: "品种", VALUE: a.DELIVERYGOODSNAME})
  891. v.LstItem = append(v.LstItem, WrTypeItem{NAME: "品类", VALUE: a.WRSTANDARDNAME})
  892. for _, b := range sType {
  893. v.LstItem = append(v.LstItem, WrTypeItem{NAME: b.ITEMTYPENAME, VALUE: b.DGFACTORYITEMVALUE})
  894. }
  895. // 获取履约规则
  896. mStep := WrGoodsPerformanceStep{MARKETID: r.MARKETID, WRSTANDARDID: a.WRSTANDARDID, HASWR: r.HASWR}
  897. if sStep, err := mStep.GetData(); err == nil && len(sStep) > 0 {
  898. v.LstStep = append(v.LstStep, sStep...)
  899. }
  900. }
  901. v.MARKETID = r.MARKETID
  902. v.HASWR = r.HASWR
  903. v.WRFACTORTYPEID = r.WRFACTORTYPEID
  904. sData = append(sData, v)
  905. }
  906. return sData, nil
  907. }
  908. // WrPerformancePlan 履约信息(履约计划)
  909. type WrPerformancePlan struct {
  910. PERFORMANCEPLANID int64 `json:"performanceplanid" xorm:"PERFORMANCEPLANID" form:"performanceplanid"` // 履约计划ID(130+yyMMddHHmmss+xxxx)
  911. PERFORMANCETYPE int32 `json:"performancetype" xorm:"PERFORMANCETYPE" form:"performancetype"` // 履约类型 - 0:通用 1:交割 2:仓单贸易 3:预售集采 4:竞拍-降价式 (无仓单) 5:挂牌期权 6:竞拍-降价式 7:竞拍-竞价式 8:竞拍-大宗式 9:荷兰式 10:法币C2C 11:报价系统 12:挂牌系统
  912. RELATEDORDERID string `json:"relatedorderid" xorm:"RELATEDORDERID" form:"relatedorderid"` // 关联单号(履约类型的关联单号)
  913. AMOUNT float64 `json:"amount" xorm:"AMOUNT" form:"amount"` // 履约金额(总金额)
  914. PAYMENTTYPE int32 `json:"paymenttype" xorm:"PAYMENTTYPE" form:"paymenttype"` // 付款方式 - 1:冻结 2:扣款
  915. PERFORMANCESTATUS int32 `json:"performancestatus" xorm:"PERFORMANCESTATUS" form:"performancestatus"` // 履约状态 - 1:初始化 2:正常 3:处理错误 4:违约待处理 5:违约处理中 6:完成 7.违约已完成 8:释放冻结失败 9:超时待处理 10:超时关闭
  916. CURSTEPID int64 `json:"curstepid" xorm:"CURSTEPID" form:"curstepid"` // 当前步骤ID
  917. BUYACCOUNTID int64 `json:"buyaccountid" xorm:"BUYACCOUNTID" form:"buyaccountid"` // 买方账号
  918. WRTRANSFERUSERID int64 `json:"wrtransferuserid" xorm:"WRTRANSFERUSERID" form:"wrtransferuserid"` // 仓单受让用户
  919. BUYPAIDAMOUNT float64 `json:"buypaidamount" xorm:"BUYPAIDAMOUNT" form:"buypaidamount"` // 买方已冻/已扣金额 (已付金额)
  920. SELLACCOUNTID int64 `json:"sellaccountid" xorm:"SELLACCOUNTID" form:"sellaccountid"` // 卖方账号
  921. SELLRECEIVEDAMOUNT float64 `json:"sellreceivedamount" xorm:"SELLRECEIVEDAMOUNT"` // 卖方已收金额
  922. CREATETIME string `json:"createtime" xorm:"CREATETIME" form:"createtime"` // 创建时间(开始时间)
  923. CREATORID int64 `json:"creatorid" xorm:"CREATORID" form:"creatorid"` // 创建人
  924. REMARK string `json:"remark" xorm:"REMARK" form:"remark"` // 备注
  925. BUYERFREEZEAMOUNT float64 `json:"buyerfreezeamount" xorm:"BUYERFREEZEAMOUNT"` // 履约冻结(买履约)
  926. BUYERFREEZEAMOUNTREMAIN float64 `json:"buyerfreezeamountremain" xorm:"BUYERFREEZEAMOUNTREMAIN"` // 履约冻结剩余(买履约)
  927. SELLERFREEZEAMOUNT float64 `json:"sellerfreezeamount" xorm:"SELLERFREEZEAMOUNT"` // 履约冻结(卖履约)
  928. SELLERFREEZEAMOUNTREMAIN float64 `json:"sellerfreezeamountremain" xorm:"SELLERFREEZEAMOUNTREMAIN"` // 履约冻结剩余(卖履约)
  929. OVERSHORTAMOUNT float64 `json:"overshortamount" xorm:"OVERSHORTAMOUNT" form:"overshortamount"` // 溢短金额
  930. HASOVERSHORT int32 `json:"hasovershort" xorm:"HASOVERSHORT" form:"hasovershort"` // 是否确认溢短 - 0:没有 1:买方确认 2:卖方确认
  931. SHIPREMARK string `json:"shipremark" xorm:"SHIPREMARK" form:"shipremark"` // 发货备注[物流单号]
  932. MIDDLEUSERID int64 `json:"middleuserid" xorm:"MIDDLEUSERID" form:"middleuserid"` // 中间商用户ID
  933. MIDDLEACCOUNTID int64 `json:"middleaccountid" xorm:"MIDDLEACCOUNTID" form:"middleaccountid"` // 中间商资金账号ID
  934. HASWR int32 `json:"haswr" xorm:"HASWR" form:"haswr"` // 类型 0-仓单预售 1-仓单贸易
  935. OVERSHORTQTY float64 `json:"overshortqty" xorm:"OVERSHORTQTY" form:"overshortqty"` // 溢短数量
  936. MARKETID int32 `json:"marketid" xorm:"MARKETID" form:"marketid"` // 市场ID
  937. EXPRESSFEECONFIRMED int32 `json:"expressfeeconfirmed" xorm:"EXPRESSFEECONFIRMED"` // 是否确认运费 - 0:无 1:买方确认 2:卖方确认
  938. EXPRESSFEE float64 `json:"expressfee" xorm:"EXPRESSFEE" form:"expressfee"` // 运费
  939. CONTRACTID int64 `json:"contractid" xorm:"CONTRACTID" form:"contractid"` // 合同ID
  940. CURSTEPDEADLINE string `json:"curstepdeadline" xorm:"CURSTEPDEADLINE" form:"curstepdeadline"` // 当前步骤到期时间
  941. QTY float64 `json:"qty" xorm:"QTY" form:"qty"` // 履约数量
  942. Wr2FactorType `xorm:"extends"`
  943. CURSTEPNAME string `json:"curstepname" xorm:"'CURSTEPNAME'"` // 当前步骤名称
  944. BUYUSERNAME string `json:"buyusername" xorm:"'BUYUSERNAME'"` // 买方名字
  945. SELLUSERNAME string `json:"sellusername" xorm:"'SELLUSERNAME'"` // 卖方名字
  946. USERID int64 `json:"userid" form:"userid" binding:"required"` // 用户id
  947. BUYORSELL int32 `json:"buyorsell" form:"buyorsell"` // 买卖方向 0-买 1-卖
  948. }
  949. func (r *WrPerformancePlan) calc() {
  950. r.Wr2FactorType.Calc()
  951. }
  952. func (r *WrPerformancePlan) buildSql() string {
  953. var sqlId utils.SQLVal = "with tmp as" +
  954. " (select t.wrfactortypeid," +
  955. " t.wrfactortypename," +
  956. " t.wrstandardid," +
  957. " t.deliverygoodsid," +
  958. " g.deliverygoodscode," +
  959. " g.deliverygoodsname," +
  960. " w.wrstandardcode," +
  961. " w.wrstandardname," +
  962. " h.warehousecode," +
  963. " h.warehousename," +
  964. " e.enumdicname," +
  965. " w.wrstandardname || t.wrfactortypename2 as wrtypename" +
  966. " from wrfactortype t" +
  967. " left join deliverygoods g" +
  968. " on t.deliverygoodsid = g.deliverygoodsid" +
  969. " left join wrstandard w" +
  970. " on t.wrstandardid = w.wrstandardid" +
  971. " left join warehouseinfo h" +
  972. " on t.warehouseid = h.autoid" +
  973. " left join enumdicitem e" +
  974. " on w.unitid = e.enumitemname" +
  975. " and e.enumdiccode = 'goodsunit'" +
  976. ")" +
  977. "select to_char(t.PERFORMANCEPLANID) PERFORMANCEPLANID," +
  978. " t.PERFORMANCETYPE," +
  979. " to_char(t.RELATEDORDERID) RELATEDORDERID," +
  980. " t.AMOUNT," +
  981. " t.PAYMENTTYPE," +
  982. " t.PERFORMANCESTATUS," +
  983. " t.CURSTEPID," +
  984. " t.BUYACCOUNTID," +
  985. " t.WRTRANSFERUSERID," +
  986. " t.BUYPAIDAMOUNT," +
  987. " t.SELLACCOUNTID," +
  988. " t.SELLRECEIVEDAMOUNT," +
  989. " t.BUYTODAYAMOUNT," +
  990. " t.SELLTODAYAMOUNT," +
  991. " to_char(t.CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME," +
  992. " t.CREATORID," +
  993. " t.REMARK," +
  994. " t.BUYERFREEZEAMOUNT," +
  995. " t.BUYERFREEZEAMOUNTREMAIN," +
  996. " t.SELLERFREEZEAMOUNT," +
  997. " t.SELLERFREEZEAMOUNTREMAIN," +
  998. " t.OVERSHORTAMOUNT," +
  999. " t.HASOVERSHORT," +
  1000. " t.SHIPREMARK," +
  1001. " t.MIDDLEUSERID," +
  1002. " t.MIDDLEACCOUNTID," +
  1003. " t.HASWR," +
  1004. " t.BUYERINFO," +
  1005. " t.SELLERINFO," +
  1006. " t.OVERSHORTQTY," +
  1007. " t.MARKETID," +
  1008. " t.EXPRESSFEECONFIRMED," +
  1009. " t.EXPRESSFEE," +
  1010. " t.CONTRACTID," +
  1011. " to_char(t.CURSTEPDEADLINE, 'yyyy-mm-dd hh24:mi:ss') CURSTEPDEADLINE," +
  1012. " t.QTY," +
  1013. " u1.accountname buyusername," +
  1014. " u2.accountname sellusername," +
  1015. " s.steptypename CURSTEPNAME," +
  1016. " tmp.*" +
  1017. " from performanceplan t" +
  1018. " left join taaccount a1" +
  1019. " on t.buyaccountid = a1.accountid" +
  1020. " left join useraccount u1" +
  1021. " on a1.userid = u1.userid" +
  1022. " left join taaccount a2" +
  1023. " on t.sellaccountid = a2.accountid" +
  1024. " left join useraccount u2" +
  1025. " on a2.userid = u2.userid" +
  1026. " left join performancesteptype s" +
  1027. " on t.curstepid = s.steptypeid" +
  1028. " left join performanceplanwr p on t.performanceplanid = p.performanceplanid" +
  1029. " left join warehousereciept f on p.wrid= f.wrid" +
  1030. " left join tmp on f.wrfactortypeid=tmp.wrfactortypeid" +
  1031. " where 1 = 1"
  1032. if r.BUYORSELL == 0 {
  1033. sqlId.And("u1.userid", r.USERID)
  1034. } else {
  1035. sqlId.And("u2.userid", r.USERID)
  1036. }
  1037. sqlId.Join(" order by t.CREATETIME desc")
  1038. return sqlId.String()
  1039. }
  1040. // GetDataEx 获取履约信息(履约计划)
  1041. func (r *WrPerformancePlan) GetDataEx() (interface{}, error) {
  1042. sData := make([]WrPerformancePlan, 0)
  1043. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1044. for i := range sData {
  1045. sData[i].calc()
  1046. }
  1047. return sData, err
  1048. }
  1049. // WrHoldLB 仓单持有记录
  1050. type WrHoldLB struct {
  1051. LADINGBILLID string `json:"ladingbillid" xorm:"LADINGBILLID" form:"ladingbillid"` // 提单ID(208+Unix秒时间戳(10位)+xxxxxx)
  1052. SUBNUM int32 `json:"subnum" xorm:"SUBNUM" form:"subnum"` // 提单子单号
  1053. WRFACTORTYPEID string `json:"wrfactortypeid" xorm:"WRFACTORTYPEID" form:"wrfactortypeid"` // 仓单要素类型ID
  1054. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"DELIVERYGOODSID" form:"deliverygoodsid"` // 品种ID
  1055. QTY float64 `json:"qty" xorm:"QTY" form:"qty"` // 数量
  1056. FREEZERQTY float64 `json:"freezerqty" xorm:"FREEZERQTY" form:"freezerqty"` // 冻结数量
  1057. USERID int64 `json:"userid" xorm:"USERID" form:"userid"` // 持有人ID
  1058. ACCOUNTID int64 `json:"accountid" xorm:"ACCOUNTID" form:"accountid"` // 持有人帐户ID
  1059. CREATETIME string `json:"createtime" xorm:"CREATETIME" form:"createtime"` // 创建时间(过户时间)
  1060. TRADEDATE string `json:"tradedate" xorm:"TRADEDATE" form:"tradedate"` // 交易日(yyyyMMdd)
  1061. WRID string `json:"wrid" xorm:"WRID" form:"wrid"` // 仓单变更ID
  1062. PARENTSUBNUM int32 `json:"parentsubnum" xorm:"PARENTSUBNUM" form:"parentsubnum"` // 拆分父子单号
  1063. PLEDGEQTY float64 `json:"pledgeqty" xorm:"'-'"` // 质押数量
  1064. Wr2FactorType `xorm:"extends"`
  1065. WRHOLDENO string `json:"wrholdeno"` // 仓单编号
  1066. ENABLEQTY float64 `json:"enableqty"` // 可用数量
  1067. }
  1068. func (r *WrHoldLB) calc() {
  1069. r.WRHOLDENO = fmt.Sprintf("%v_%v", r.LADINGBILLID, r.SUBNUM)
  1070. r.ENABLEQTY = r.QTY - r.FREEZERQTY
  1071. r.Wr2FactorType.Calc()
  1072. }
  1073. func (r *WrHoldLB) buildSql() string {
  1074. var sqlId utils.SQLVal = "with tmp as" +
  1075. " (select t.wrfactortypeid," +
  1076. " t.wrfactortypename," +
  1077. " t.wrstandardid," +
  1078. " t.deliverygoodsid," +
  1079. " g.deliverygoodscode," +
  1080. " g.deliverygoodsname," +
  1081. " w.wrstandardcode," +
  1082. " w.wrstandardname," +
  1083. " h.warehousecode," +
  1084. " h.warehousename," +
  1085. " e.enumdicname," +
  1086. " w.wrstandardname || t.wrfactortypename2 as wrtypename" +
  1087. " from wrfactortype t" +
  1088. " left join deliverygoods g" +
  1089. " on t.deliverygoodsid = g.deliverygoodsid" +
  1090. " left join wrstandard w" +
  1091. " on t.wrstandardid = w.wrstandardid" +
  1092. " left join warehouseinfo h" +
  1093. " on t.warehouseid = h.autoid" +
  1094. " left join enumdicitem e" +
  1095. " on w.unitid = e.enumitemname" +
  1096. " and e.enumdiccode = 'goodsunit'" +
  1097. ")" +
  1098. "SELECT to_char(t.LADINGBILLID) LADINGBILLID," +
  1099. " t.SUBNUM," +
  1100. " to_char(t.WRFACTORTYPEID) WRFACTORTYPEID," +
  1101. " t.DELIVERYGOODSID," +
  1102. " t.QTY," +
  1103. " t.FREEZERQTY," +
  1104. " t.USERID," +
  1105. " t.ACCOUNTID," +
  1106. " to_char(t.CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME," +
  1107. " t.TRADEDATE," +
  1108. " t.WRID," +
  1109. " t.PARENTSUBNUM," +
  1110. " tmp.*" +
  1111. " FROM WRHOLDLB t" +
  1112. " LEFT JOIN tmp on t.wrfactortypeid= tmp.wrfactortypeid" +
  1113. " WHERE 1 = 1"
  1114. sqlId.And("t.ACCOUNTID", r.ACCOUNTID)
  1115. return sqlId.String()
  1116. }
  1117. // GetDataEx 获取仓单持有记录
  1118. func (r *WrHoldLB) GetDataEx() (interface{}, error) {
  1119. sData := make([]WrHoldLB, 0)
  1120. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1121. for i := range sData {
  1122. sData[i].calc()
  1123. }
  1124. return sData, err
  1125. }
  1126. // WrFilterItem 筛选框列表项
  1127. type WrFilterItem struct {
  1128. ITEMTYPE int32 `json:"itemtype" xorm:"'ITEMTYPE'"` // 类型 1-品种 2-品类(商品) 3-仓库
  1129. ITEMNAME string `json:"itemname" xorm:"'ITEMNAME'"` // 名称
  1130. ITEMCODE string `json:"itemcode" xorm:"'ITEMCODE'"` // 代码
  1131. ITEMID int64 `json:"itemid" xorm:"'ITEMID'"` // 项id
  1132. IType string `json:"-" form:"itype"` // 类型 逗号隔开, 如 1,2
  1133. }
  1134. func (r *WrFilterItem) calc() {
  1135. }
  1136. func (r *WrFilterItem) buildSql() string {
  1137. if r.IType == "" {
  1138. r.IType = "1, 2, 3"
  1139. }
  1140. r.IType = strings.ReplaceAll(r.IType, " ", "")
  1141. sType := strings.Split(r.IType, ",")
  1142. sqls := make([]string, 0)
  1143. for _, v := range sType {
  1144. switch v {
  1145. case "1":
  1146. sqls = append(sqls, "select 1 itemtype, t.deliverygoodsid itemid, t.deliverygoodscode itemcode, to_char(t.deliverygoodsname) itemname from deliverygoods t")
  1147. case "2":
  1148. sqls = append(sqls, "select 2 itemtype, t.wrstandardid itemid, to_char(t.wrstandardcode) itemcode, to_char(t.wrstandardname) itemname from wrstandard t")
  1149. case "3":
  1150. sqls = append(sqls, "select 3 itemtype, t.autoid itemid, t.warehousecode itemcode, to_char(t.warehousename) itemname from warehouseinfo t")
  1151. default:
  1152. }
  1153. }
  1154. var sqlId string
  1155. for _, v := range sqls {
  1156. if len(sqlId) > 0 {
  1157. sqlId += " union all " + v
  1158. } else {
  1159. sqlId = v
  1160. }
  1161. }
  1162. sqlId += " order by itemtype, itemid"
  1163. return sqlId
  1164. }
  1165. // GetDataEx 获取筛选框列表项
  1166. func (r *WrFilterItem) GetDataEx() (interface{}, error) {
  1167. sData := make([]WrFilterItem, 0)
  1168. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1169. for i := range sData {
  1170. sData[i].calc()
  1171. }
  1172. return sData, err
  1173. }