ermcp3.go 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860
  1. /**
  2. * @Author: zou.yingbin
  3. * @Create : 2021/4/14 11:11
  4. * @Modify : 2021/4/14 11:11
  5. */
  6. package models
  7. import (
  8. "encoding/json"
  9. "fmt"
  10. "mtp2_if/db"
  11. "mtp2_if/logger"
  12. "mtp2_if/mtpcache"
  13. "mtp2_if/utils"
  14. "strings"
  15. )
  16. // ErmcpDeliveryGoods 现货品种(交割品种)
  17. type ErmcpDeliveryGoods struct {
  18. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种ID(SEQ_DELIVERYGOODS)
  19. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  20. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  21. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODSUNITID'"` // 现货品种单位ID
  22. DELIVERYGOODSTYPE int32 `json:"deliverygoodstype" xorm:"'DELIVERYGOODSTYPE'"` // 现货品种类型: 1-整装不拆分 2-散装记录明细 3:整装拆分 4:散装不记录明细
  23. STANDARDQTY int32 `json:"standardqty" xorm:"'STANDARDQTY'"` // 标准数量(库位数量) [标准品特有]
  24. STANDARDQTYRANGE int32 `json:"standardqtyrange" xorm:"'STANDARDQTYRANGE'"` // 标准数量偏差范围 [标准品特有]
  25. AUDITFLAG int32 `json:"auditflag" xorm:"'AUDITFLAG'"` // 交割是否需要审核 - 0:不需要 1:需要审核 默认为0
  26. ISVALID int32 `json:"isvalid" xorm:"'ISVALID'"` // 是否有效 - 0:无效 1:有效
  27. AGREEUNIT int32 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约单位[散货时默认为1, 整装时默认为标准数量]
  28. QTYDECIMALPLACE int32 `json:"qtydecimalplace" xorm:"'QTYDECIMALPLACE'"` // 成交量小数位
  29. CATEGORYID int32 `json:"categoryid" xorm:"'CATEGORYID'"` // 类别ID(SEQ_WRCATEGORY)
  30. DGSTATUS int32 `json:"dgstatus" xorm:"'DGSTATUS'"` // 品种状态 - 作废 - 0:未激活 1:正常
  31. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  32. REMARK string `json:"remark" xorm:"'REMARK'"` // 备注
  33. EnumdicName string `json:"enumdicname"` // 现货品种单位名称
  34. ExcludeCfg int32 `json:"-"` // 排除已配置的业务类型商品 1-套保 2-套利
  35. }
  36. func (r *ErmcpDeliveryGoods) calc() {
  37. r.EnumdicName = mtpcache.GetEnumDicitemName(r.GOODSUNITID)
  38. }
  39. func (r *ErmcpDeliveryGoods) buildSql() string {
  40. var sqlId utils.SQLVal = "SELECT t.DELIVERYGOODSID," +
  41. " t.DELIVERYGOODSCODE," +
  42. " t.DELIVERYGOODSNAME," +
  43. " t.GOODSUNITID," +
  44. " t.DELIVERYGOODSTYPE," +
  45. " t.STANDARDQTY," +
  46. " t.STANDARDQTYRANGE," +
  47. " t.AUDITFLAG," +
  48. " t.ISVALID," +
  49. " t.ISSPLIT," +
  50. " t.AGREEUNIT," +
  51. " t.QTYDECIMALPLACE," +
  52. " t.CATEGORYID," +
  53. " t.DGSTATUS," +
  54. " t.REMARK," +
  55. " t.AREAUSERID" +
  56. " FROM DELIVERYGOODS t" +
  57. " WHERE 1 = 1"
  58. sqlId.AndEx("t.AREAUSERID", r.AREAUSERID, r.AREAUSERID > 0)
  59. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  60. if r.ExcludeCfg > 0 {
  61. sqlId.Join(fmt.Sprintf(" and t.deliverygoodsid not in(select distinct deliverygoodsid from ermcp_bizgroupspotgoods where biztype = %v)", r.ExcludeCfg))
  62. }
  63. return sqlId.String()
  64. }
  65. // GetDataEx 获取现货商品(交割商品)
  66. func (r *ErmcpDeliveryGoods) GetDataEx() (interface{}, error) {
  67. return r.GetData()
  68. }
  69. // GetData
  70. func (r *ErmcpDeliveryGoods) GetData() ([]ErmcpDeliveryGoods, error) {
  71. sData := make([]ErmcpDeliveryGoods, 0)
  72. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  73. for i := range sData {
  74. sData[i].calc()
  75. }
  76. return sData, err
  77. }
  78. // ErmcpDGFactoryItem 品种选择项定义表
  79. type ErmcpDGFactoryItem struct {
  80. DGFACTORYITEMID int64 `json:"dgfactoryitemid" xorm:"'DGFACTORYITEMID'"` // 选择项ID(SEQ_DGFACTORYITEM)
  81. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种ID
  82. DGFACTORYITEMTYPEID int64 `json:"dgfactoryitemtypeid" xorm:"'DGFACTORYITEMTYPEID'"` // 要素项类型
  83. DGFACTORYITEMVALUE string `json:"dgfactoryitemvalue" xorm:"'DGFACTORYITEMVALUE'"` // 要素项值(类型为仓库时填写仓库名称)
  84. WAREHOUSEID int64 `json:"warehouseid" xorm:"'WAREHOUSEID'"` // 仓库ID(类型为仓库时填写)
  85. ISVALID int32 `json:"isvalid" xorm:"'ISVALID'"` // 是否有效 - 0:无效 1:有效
  86. ORDERINDEX int32 `json:"orderindex" xorm:"'ORDERINDEX'"` // 顺序
  87. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 用户id
  88. }
  89. func (r *ErmcpDGFactoryItem) buildSql() string {
  90. var sqlId utils.SQLVal = "SELECT DGFACTORYITEMID," +
  91. " t.DELIVERYGOODSID," +
  92. " to_char(t.DGFACTORYITEMTYPEID) DGFACTORYITEMTYPEID," +
  93. " t.DGFACTORYITEMVALUE," +
  94. " to_char(t.WAREHOUSEID) WAREHOUSEID," +
  95. " t.ISVALID," +
  96. " t.ORDERINDEX," +
  97. " g.areauserid" +
  98. " FROM DGFACTORYITEM t" +
  99. " left join deliverygoods g" +
  100. " on t.deliverygoodsid = g.deliverygoodsid" +
  101. " WHERE t.ISVALID = 1"
  102. sqlId.AndEx("g.areauserid", r.AREAUSERID, r.AREAUSERID > 0)
  103. sqlId.AndEx("t.DGFACTORYITEMID", r.DGFACTORYITEMID, r.DGFACTORYITEMID > 0)
  104. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  105. sqlId.AndEx("t.DGFACTORYITEMTYPEID", r.DGFACTORYITEMTYPEID, r.DGFACTORYITEMTYPEID > 0)
  106. sqlId.AndEx("t.ORDERINDEX", r.ORDERINDEX, r.ORDERINDEX > 0)
  107. return sqlId.String()
  108. }
  109. // GetDataEx 获取品种选择项定义
  110. func (r *ErmcpDGFactoryItem) GetDataEx() (interface{}, error) {
  111. return r.GetData()
  112. }
  113. // GetData
  114. func (r *ErmcpDGFactoryItem) GetData() (interface{}, error) {
  115. sData := make([]ErmcpDGFactoryItem, 0)
  116. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  117. return sData, err
  118. }
  119. // Ermcp3Brand 品牌(从品种选择项定义表中查)
  120. type Ermcp3Brand struct {
  121. BRANDID int64 `json:"brandid"` // 品牌id
  122. BRANDNAME string `json:"brandname"` // 品牌名称
  123. DELIVERYGOODSID int64 `json:"deliverygoodsid"` // 品种ID
  124. AREAUSERID int64 `json:"areauserid"` // 用户id
  125. }
  126. // GetDataEx
  127. func (r *Ermcp3Brand) GetDataEx() (interface{}, error) {
  128. return r.GetData()
  129. }
  130. // GetData 获取品牌数据
  131. func (r *Ermcp3Brand) GetData() ([]Ermcp3Brand, error) {
  132. m := ErmcpDGFactoryItem{
  133. DELIVERYGOODSID: int32(r.DELIVERYGOODSID),
  134. DGFACTORYITEMTYPEID: 2,
  135. ORDERINDEX: 1,
  136. }
  137. sData := make([]Ermcp3Brand, 0)
  138. if d, err := m.GetDataEx(); err == nil {
  139. if dgLst, ok := d.([]ErmcpDGFactoryItem); ok {
  140. for _, val := range dgLst {
  141. sData = append(sData, Ermcp3Brand{BRANDID: val.DGFACTORYITEMID, AREAUSERID: val.AREAUSERID,
  142. BRANDNAME: val.DGFACTORYITEMVALUE, DELIVERYGOODSID: int64(val.DELIVERYGOODSID)})
  143. }
  144. }
  145. }
  146. return sData, nil
  147. }
  148. // ErmcpRelatedGoods 现货品种的关联交易商品
  149. type ErmcpRelatedGoods struct {
  150. GoodsId int `json:"goodsid" xorm:"'GoodsId'"` // 商品id
  151. GoodsCode string `json:"goodscode" xorm:"'GoodsCode'"` // 商品代码
  152. GoodsName string `json:"goodsname" xorm:"'GoodsName'"` // 商品名称
  153. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 品种ID
  154. AREAUSERID int64 `json:"-"` // 所属机构
  155. }
  156. func (r *ErmcpRelatedGoods) buildSql() string {
  157. var sqlId utils.SQLVal = "select distinct t.deliverygoodsid, g.goodsid, g.goodscode, g.goodsname" +
  158. " from deliverygoods t" +
  159. " inner join ERMS2_WRSConvertDetail w" +
  160. " on t.deliverygoodsid = w.deliverygoodsid" +
  161. " inner join erms_middlegoods m" +
  162. " on w.middlegoodsid = m.middlegoodsid" +
  163. " inner join ERMCP_GGConvertConfig gc" +
  164. " on m.goodsgroupid = gc.destgoodsgroupid" +
  165. " inner join goods g" +
  166. " on gc.srcgoodsgroupid = g.goodsgroupid" +
  167. " where 1 = 1"
  168. sqlId.AndEx("t.AREAUSERID", r.AREAUSERID, r.AREAUSERID > 0)
  169. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  170. return sqlId.String()
  171. }
  172. // GetDataEx 获取现货关联交易商品
  173. func (r *ErmcpRelatedGoods) GetDataEx() (interface{}, error) {
  174. return r.GetData()
  175. }
  176. // GetData 获取现货关联交易商品
  177. func (r *ErmcpRelatedGoods) GetData() ([]ErmcpRelatedGoods, error) {
  178. sData := make([]ErmcpRelatedGoods, 0)
  179. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  180. return sData, err
  181. }
  182. // ErmcpDeliveryGoodsDetail2 现货商品详情(含转换系数)
  183. type ErmcpDeliveryGoodsDetail2 struct {
  184. ErmcpDeliveryGoodsDetail
  185. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 套保系数(折算系数)
  186. }
  187. // ErmcpDeliveryGoodsDetail 现货商品详情
  188. type ErmcpDeliveryGoodsDetail struct {
  189. Data ErmcpDeliveryGoods `json:"data"` // 现货商品基本信息(交割品种)
  190. GmList []Ermcp3Wrstandard `json:"gmlist"` // 品类列表(仓单标准)
  191. GbList []Ermcp3Brand `json:"gblist"` // 品牌列表
  192. }
  193. func (r *ErmcpDeliveryGoodsDetail) addGmList(lst []Ermcp3Wrstandard) {
  194. for i := range lst {
  195. if lst[i].DELIVERYGOODSID == r.Data.DELIVERYGOODSID && lst[i].ISVALID == 1 {
  196. r.GmList = append(r.GmList, lst[i])
  197. }
  198. }
  199. }
  200. func (r *ErmcpDeliveryGoodsDetail) addGbList(lst []Ermcp3Brand) {
  201. for i := range lst {
  202. if lst[i].DELIVERYGOODSID == int64(r.Data.DELIVERYGOODSID) {
  203. r.GbList = append(r.GbList, lst[i])
  204. }
  205. }
  206. }
  207. // GetData
  208. func (r *ErmcpDeliveryGoodsDetail) GetData() ([]ErmcpDeliveryGoodsDetail, error) {
  209. sData := make([]ErmcpDeliveryGoodsDetail, 0)
  210. m := ErmcpDeliveryGoods{AREAUSERID: r.Data.AREAUSERID, DELIVERYGOODSID: r.Data.DELIVERYGOODSID}
  211. data, err := m.GetData()
  212. if err != nil {
  213. return sData, err
  214. }
  215. if data == nil || len(data) == 0 {
  216. return sData, nil
  217. }
  218. // 基本信息
  219. for i := range data {
  220. val := ErmcpDeliveryGoodsDetail{Data: data[i],
  221. GmList: make([]Ermcp3Wrstandard, 0), GbList: make([]Ermcp3Brand, 0),
  222. }
  223. sData = append(sData, val)
  224. }
  225. //品类/品牌/套保信息
  226. gm := Ermcp3Wrstandard{AREAUSERID: r.Data.AREAUSERID, DELIVERYGOODSID: r.Data.DELIVERYGOODSID, ISVALID: 1}
  227. gb := Ermcp3Brand{DELIVERYGOODSID: int64(r.Data.DELIVERYGOODSID)}
  228. gmLst, _ := gm.GetDataEx()
  229. gbLst, _ := gb.GetData()
  230. for i := range sData {
  231. sData[i].addGmList(gmLst.([]Ermcp3Wrstandard))
  232. sData[i].addGbList(gbLst)
  233. }
  234. return sData, nil
  235. }
  236. // GetDataEx
  237. func (r *ErmcpDeliveryGoodsDetail) GetDataEx() (interface{}, error) {
  238. return r.GetData()
  239. }
  240. // ErmcpDeliveryGoodsDetailEx 现货商品详情(含套保列表)
  241. type ErmcpDeliveryGoodsDetailEx struct {
  242. ErmcpDeliveryGoodsDetail
  243. MgList []Ermcp3MiddleGoodsDetail2 `json:"mgList"` // 套保列表
  244. }
  245. // GetData 获取现货商品详情(含套保信息列表)
  246. func (r *ErmcpDeliveryGoodsDetailEx) GetDataEx() (interface{}, error) {
  247. sData := make([]ErmcpDeliveryGoodsDetailEx, 0)
  248. mDg := r.ErmcpDeliveryGoodsDetail
  249. // 查现货商品列表
  250. if dgList, err := mDg.GetData(); err == nil {
  251. for i := range dgList {
  252. sData = append(sData, ErmcpDeliveryGoodsDetailEx{ErmcpDeliveryGoodsDetail: dgList[i],
  253. MgList: make([]Ermcp3MiddleGoodsDetail2, 0)})
  254. }
  255. }
  256. if len(sData) == 0 {
  257. return sData, nil
  258. }
  259. // 查转换关系
  260. mWc := WRSConverTDetail{}
  261. if wc, err := mWc.GetData(); err == nil {
  262. // 查套保商品列表
  263. mMg := Ermcp3MiddleGoodsDetail{}
  264. mMg.Mg.AREAUSERID = r.Data.AREAUSERID
  265. if mgLst, err := mMg.GetData(); err == nil {
  266. for i := range sData {
  267. sData[i].addMgList(mgLst, wc)
  268. }
  269. }
  270. }
  271. return sData, nil
  272. }
  273. // addMgList 添加关联的套保信息列表
  274. func (r *ErmcpDeliveryGoodsDetailEx) addMgList(lst []Ermcp3MiddleGoodsDetail, wc []WRSConverTDetail) {
  275. for i := range wc {
  276. if r.Data.DELIVERYGOODSID == wc[i].DELIVERYGOODSID {
  277. for k := range lst {
  278. if int64(lst[k].Mg.MIDDLEGOODSID) == wc[i].MIDDLEGOODSID {
  279. val := Ermcp3MiddleGoodsDetail2{Ermcp3MiddleGoodsDetail: lst[k], CONVERTRATIO: wc[i].CONVERTRATIO}
  280. r.MgList = append(r.MgList, val)
  281. }
  282. }
  283. }
  284. }
  285. }
  286. // Ermcp3MiddleGoodsDetail2 套保品种详情(含转换系数)
  287. type Ermcp3MiddleGoodsDetail2 struct {
  288. Ermcp3MiddleGoodsDetail
  289. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 套保系数(折算系数)
  290. }
  291. // Ermcp3MiddleGoodsDetail 套保品种详情
  292. type Ermcp3MiddleGoodsDetail struct {
  293. Mg ErmcpMiddleGoodsModel `json:"mg"` // 套保商品信息
  294. GPList []Ermcp3GoodsGroupEx `json:"gplist"` // 关联商品组列表
  295. }
  296. // GetData 获取套保品种详情(不含现货列表)
  297. func (r *Ermcp3MiddleGoodsDetail) GetData() ([]Ermcp3MiddleGoodsDetail, error) {
  298. sData := make([]Ermcp3MiddleGoodsDetail, 0)
  299. // 查基本信息
  300. mMg := ErmcpMiddleGoodsModel{AREAUSERID: r.Mg.AREAUSERID, MIDDLEGOODSID: r.Mg.MIDDLEGOODSID, ISVALID: 1}
  301. if mgLst, err := mMg.GetData2(); err == nil {
  302. for i := range mgLst {
  303. sData = append(sData, Ermcp3MiddleGoodsDetail{Mg: mgLst[i], GPList: make([]Ermcp3GoodsGroupEx, 0)})
  304. }
  305. }
  306. if len(sData) == 0 {
  307. return sData, nil
  308. }
  309. bExist := false
  310. // 查关联期货商品组
  311. mGp := Ermcp3MiddleGoodsRelateGroup{AREAUSERID: r.Mg.AREAUSERID}
  312. if gpLst, err := mGp.GetData(); err == nil {
  313. for i := range sData {
  314. sData[i].addGoodsGroup(gpLst)
  315. }
  316. bExist = len(gpLst) > 0
  317. }
  318. if bExist {
  319. mGoods := Ermcp3GoodsEx{AREAUSERID: r.Mg.AREAUSERID}
  320. if gLst, err := mGoods.GetData(); err == nil {
  321. for i := range sData {
  322. sData[i].addGoods(gLst)
  323. }
  324. }
  325. }
  326. return sData, nil
  327. }
  328. func (r *Ermcp3MiddleGoodsDetail) addGoodsGroup(gpLst []Ermcp3MiddleGoodsRelateGroup) {
  329. for i := range gpLst {
  330. if gpLst[i].MIDDLEGOODSID == r.Mg.MIDDLEGOODSID {
  331. bExist := false
  332. for _, v := range r.GPList {
  333. if v.GOODSGROUPID == gpLst[i].GOODSGROUPID {
  334. bExist = true
  335. break
  336. }
  337. }
  338. if !bExist {
  339. val := Ermcp3GoodsGroupEx{
  340. GOODSGROUPID: gpLst[i].GOODSGROUPID,
  341. MIDDLEGOODSID: gpLst[i].MIDDLEGOODSID,
  342. CONVERTRATIO: gpLst[i].CONVERTRATIO,
  343. EXEXCHANGECODE: gpLst[i].EXEXCHANGECODE,
  344. GOODSGROUPNAME: gpLst[i].GOODSGROUPNAME,
  345. ENUMDICNAME: gpLst[i].ENUMDICNAME,
  346. GOODSUNITID: gpLst[i].GOODSUNITID,
  347. GList: make([]Ermcp3Goods, 0),
  348. }
  349. r.GPList = append(r.GPList, val)
  350. }
  351. }
  352. }
  353. }
  354. func (r *Ermcp3MiddleGoodsDetail) addGoods(gpLst []Ermcp3GoodsEx) {
  355. for i := range gpLst {
  356. if gpLst[i].MIDDLEGOODSID == r.Mg.MIDDLEGOODSID {
  357. for k := range r.GPList {
  358. if r.GPList[k].GOODSGROUPID == gpLst[i].GOODSGROUPID {
  359. r.GPList[k].GList = append(r.GPList[k].GList, Ermcp3Goods{
  360. GOODSID: gpLst[i].GOODSID,
  361. GOODSNAME: gpLst[i].GOODSNAME,
  362. GOODSCODE: gpLst[i].GOODSCODE,
  363. AGREEUNIT: gpLst[i].AGREEUNIT,
  364. EXEXCHANGECODE: gpLst[i].EXEXCHANGECODE,
  365. ENUMDICNAME: gpLst[i].ENUMDICNAME,
  366. })
  367. }
  368. }
  369. }
  370. }
  371. }
  372. // Ermcp3MiddleGoodsDetailEx 套保品种详情(含现货列表)
  373. type Ermcp3MiddleGoodsDetailEx struct {
  374. Ermcp3MiddleGoodsDetail
  375. DgList []ErmcpDeliveryGoodsDetail2 `json:"dglist"` // 关联的现货品种列表
  376. }
  377. func (r *Ermcp3MiddleGoodsDetailEx) addDgList(dglst []ErmcpDeliveryGoodsDetail, wc []WRSConverTDetail) {
  378. for i := range wc {
  379. if wc[i].MIDDLEGOODSID == int64(r.Mg.MIDDLEGOODSID) {
  380. for _, detail := range dglst {
  381. if wc[i].DELIVERYGOODSID == detail.Data.DELIVERYGOODSID && detail.Data.ISVALID == 1 {
  382. val := ErmcpDeliveryGoodsDetail2{ErmcpDeliveryGoodsDetail: detail, CONVERTRATIO: wc[i].CONVERTRATIO}
  383. r.DgList = append(r.DgList, val)
  384. }
  385. }
  386. }
  387. }
  388. }
  389. // GetDataEx
  390. func (r *Ermcp3MiddleGoodsDetailEx) GetDataEx() (interface{}, error) {
  391. sData := make([]Ermcp3MiddleGoodsDetailEx, 0)
  392. // 查套保品种基本信息
  393. m := r.Ermcp3MiddleGoodsDetail
  394. if mgLst, err := m.GetData(); err == nil {
  395. for i := range mgLst {
  396. sData = append(sData, Ermcp3MiddleGoodsDetailEx{Ermcp3MiddleGoodsDetail: mgLst[i],
  397. DgList: make([]ErmcpDeliveryGoodsDetail2, 0)})
  398. }
  399. }
  400. // 查转换关系
  401. mWc := WRSConverTDetail{}
  402. if wc, err := mWc.GetData(); err == nil {
  403. // 查现货商品列表
  404. mDg := ErmcpDeliveryGoodsDetail{}
  405. mDg.Data.AREAUSERID = r.Mg.AREAUSERID
  406. if dgLst, err := mDg.GetData(); err == nil {
  407. for i := range sData {
  408. sData[i].addDgList(dgLst, wc)
  409. }
  410. }
  411. }
  412. return sData, nil
  413. }
  414. // Ermcp3MiddleGoodsRelateGroup 套保商品关联的期货商品组
  415. type Ermcp3MiddleGoodsRelateGroup struct {
  416. MIDDLEGOODSID uint32 `json:"middlegoodsid" xorm:"'middlegoodsid'"` // 套保商品id
  417. GOODSGROUPID int32 `json:"goodsgroupid" xorm:"'goodsgroupid'"` // 商品组id
  418. CONVERTRATIO float64 `json:"convertratio" xorm:"'convertratio'"` // 折算系数(品种系数)
  419. EXEXCHANGECODE string `json:"exexchangecode" xorm:"'exexchangecode'"` // 交易所代码
  420. GOODSGROUPNAME string `json:"goodsgroupname" xorm:"'goodsgroupname'"` // 商品组名称
  421. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODUNITID'"` // 单位id
  422. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  423. AREAUSERID int64 `json:"-"` // 所属机构id
  424. }
  425. func (r *Ermcp3MiddleGoodsRelateGroup) calc() {
  426. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.GOODSUNITID)
  427. }
  428. func (r *Ermcp3MiddleGoodsRelateGroup) buildSql() string {
  429. var sqlId utils.SQLVal = "select mg.areauserid," +
  430. " mg.middlegoodsid," +
  431. " gc.convertratio," +
  432. " ex.exexchangecode," +
  433. " gp.goodsgroupname," +
  434. " gp.goodunitid," +
  435. " gp.goodsgroupid" +
  436. " from erms_middlegoods mg" +
  437. " inner join ERMCP_GGConvertConfig gc" +
  438. " on mg.goodsgroupid = gc.destgoodsgroupid" +
  439. " inner join goodsgroup gp" +
  440. " on gc.srcgoodsgroupid = gp.goodsgroupid" +
  441. " left join externalexchange ex" +
  442. " on gp.exexchangeid = ex.autoid" +
  443. " where mg.isvalid = 1" +
  444. " and gp.goodsgroupstatus = 1"
  445. sqlId.AndEx("mg.AREAUSERID", r.AREAUSERID, r.AREAUSERID > 0)
  446. return sqlId.String()
  447. }
  448. // GetData 套保品关联的期货商品组信息
  449. func (r *Ermcp3MiddleGoodsRelateGroup) GetData() ([]Ermcp3MiddleGoodsRelateGroup, error) {
  450. sData := make([]Ermcp3MiddleGoodsRelateGroup, 0)
  451. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  452. for i := range sData {
  453. sData[i].calc()
  454. }
  455. return sData, err
  456. }
  457. // GetDataEx 套保品关联的期货商品组信息
  458. func (r *Ermcp3MiddleGoodsRelateGroup) GetDataEx() (interface{}, error) {
  459. return r.GetData()
  460. }
  461. // Ermcp3GoodsGroupEx 商品组(含商品列表)
  462. type Ermcp3GoodsGroupEx struct {
  463. MIDDLEGOODSID uint32 `json:"middlegoodsid" xorm:"'middlegoodsid'"` // 套保商品id
  464. GOODSGROUPID int32 `json:"goodsgroupid" xorm:"'goodsgroupid'"` // 商品组id
  465. CONVERTRATIO float64 `json:"convertratio" xorm:"'convertratio'"` // 折算系数(品种系数)
  466. EXEXCHANGECODE string `json:"exexchangecode" xorm:"'exexchangecode'"` // 交易所代码
  467. GOODSGROUPNAME string `json:"goodsgroupname" xorm:"'goodsgroupname'"` // 商品组名称
  468. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODSUNITID'"` // 单位id
  469. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  470. GList []Ermcp3Goods `json:"glist"` // 合约列表
  471. }
  472. // Ermcp3Goods 商品信息
  473. type Ermcp3Goods struct {
  474. GOODSID int32 `json:"goodsid" xorm:"'goodsid'"` // 期货商品id
  475. GOODSCODE string `json:"goodscode" xorm:"'goodscode'"` // 期货商品代码
  476. GOODSNAME string `json:"goodsname" xorm:"'goodsname'"` // 期货商品名称
  477. AGREEUNIT float32 `json:"agreeunit" xorm:"'agreeunit'"` // 合约单位(合约乘数)
  478. EXEXCHANGECODE string `json:"exexchangecode" xorm:"'exexchangecode'"` // 交易所代码
  479. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  480. }
  481. // Ermcp3GoodsEx 套保品关联的期货商品(少量字段)
  482. type Ermcp3GoodsEx struct {
  483. MIDDLEGOODSID uint32 `json:"middlegoodsid" xorm:"'middlegoodsid'"` // 套保商品id
  484. GOODSID int32 `json:"goodsid" xorm:"'goodsid'"` // 期货商品id
  485. GOODSCODE string `json:"goodscode" xorm:"'goodscode'"` // 期货商品代码
  486. GOODSNAME string `json:"goodsname" xorm:"'goodsname'"` // 期货商品名称
  487. GOODUNITID int32 `json:"goodunitid" xorm:"'goodunitid'"` // 期货商品单位id
  488. CONVERTRATIO float64 `json:"convertratio" xorm:"'convertratio'"` // 折算系数(品种系数)
  489. GOODSGROUPID int32 `json:"goodsgroupid" xorm:"'goodsgroupid'"` // 商品组id
  490. EXEXCHANGECODE string `json:"exexchangecode" xorm:"'exexchangecode'"` // 交易所代码
  491. GOODSGROUPNAME string `json:"goodsgroupname" xorm:"'goodsgroupname'"` // 商品组名称
  492. GPUNITID int32 `json:"gpunitid" xorm:"'gpunitid'"` // 商品组单位id
  493. AGREEUNIT float32 `json:"agreeunit" xorm:"'agreeunit'"` // 合约单位(合约乘数)
  494. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  495. GPENUMDICNAME string `json:"enumdicname"` // 商品组单位名称
  496. AREAUSERID int64 `json:"-"` // 所属机构id
  497. }
  498. func (r *Ermcp3GoodsEx) calc() {
  499. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.GOODUNITID)
  500. r.GPENUMDICNAME = mtpcache.GetEnumDicitemName(r.GPUNITID)
  501. }
  502. func (r *Ermcp3GoodsEx) buildSql() string {
  503. var sqlId utils.SQLVal = "select mg.areauserid," +
  504. " mg.middlegoodsid," +
  505. " g.goodsid," +
  506. " g.goodscode," +
  507. " g.goodsname," +
  508. " g.goodunitid," +
  509. " g.goodsgroupid," +
  510. " g.agreeunit," +
  511. " gc.convertratio," +
  512. " ex.exexchangecode," +
  513. " gp.goodsgroupname," +
  514. " gp.goodunitid gpunitid" +
  515. " from erms_middlegoods mg" +
  516. " inner join ERMCP_GGConvertConfig gc" +
  517. " on mg.goodsgroupid = gc.destgoodsgroupid" +
  518. " inner join goodsgroup gp" +
  519. " on gc.srcgoodsgroupid = gp.goodsgroupid" +
  520. " inner join goods g" +
  521. " on gp.goodsgroupid = g.goodsgroupid" +
  522. " left join externalexchange ex" +
  523. " on gp.exexchangeid = ex.autoid" +
  524. " where g.goodsstatus not in(4,5,6) and mg.isvalid=1 and gp.goodsgroupstatus=1"
  525. sqlId.AndEx("mg.areauserid", r.AREAUSERID, r.AREAUSERID > 0)
  526. sqlId.Join(" order by mg.middlegoodsid")
  527. return sqlId.String()
  528. }
  529. // GetData
  530. func (r *Ermcp3GoodsEx) GetData() ([]Ermcp3GoodsEx, error) {
  531. sData := make([]Ermcp3GoodsEx, 0)
  532. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  533. for i := range sData {
  534. sData[i].calc()
  535. }
  536. return sData, err
  537. }
  538. // GetDataEx
  539. func (r *Ermcp3GoodsEx) GetDataEx() (interface{}, error) {
  540. return r.GetData()
  541. }
  542. // Ermcp3Contract 现货合同
  543. type Ermcp3Contract struct {
  544. SPOTCONTRACTID string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` // 现货合同ID(602+Unix秒时间戳(10位)+xxxxxx)
  545. CONTRACTNO string `json:"contractno" xorm:"'CONTRACTNO'"` // 现货合同编号
  546. CONTRACTTYPE int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 现货合同类型 - 1:采购 -1:销售
  547. USERID int64 `json:"userid" xorm:"'USERID'"` // 所属机构ID
  548. BUYUSERID int64 `json:"buyuserid" xorm:"'BUYUSERID'"` // 采购方ID
  549. BUYUSERName string `json:"buyusername" xorm:"'BUYUSERName'"` // 采购方名称
  550. SELLUSERID int64 `json:"selluserid" xorm:"'SELLUSERID'"` // 销售方ID
  551. SELLUSERNAME string `json:"sellusername" xorm:"'SELLUSERNAME'"` // 销售方名称
  552. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种ID
  553. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'wrstandardid'"` // 品类ID
  554. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  555. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  556. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  557. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  558. PRODUCTTYPE int32 `json:"producttype" xorm:"'PRODUCTTYPE'"` // 产品类型 - 1:标准仓单 2:等标 3:非标
  559. CONVERTFACTOR float64 `json:"convertfactor" xorm:"'CONVERTFACTOR'"` // 标仓系数(品类)
  560. SPOTGOODSDESC string `json:"spotgoodsdesc" xorm:"'SPOTGOODSDESC'"` // 商品型号
  561. PRICETYPE int32 `json:"pricetype" xorm:"'PRICETYPE'"` // 定价类型 - 1:一口价 2:点价 3:暂定价
  562. QTY float64 `json:"qty" xorm:"'QTY'"` // 数量
  563. PRICE float64 `json:"price" xorm:"'PRICE'"` // 价格\暂定价 [1:一口价、3:暂定价]
  564. AMOUNT float64 `json:"amount" xorm:"'AMOUNT'"` // 金额 [1:一口价、3:暂定价]
  565. DELIVERYSTARTDATE string `json:"deliverystartdate" xorm:"'DELIVERYSTARTDATE'"` // 交收期(开始)
  566. DELIVERYENDDATE string `json:"deliveryenddate" xorm:"'DELIVERYENDDATE'"` // 交收期(结束)
  567. GOODSID int32 `json:"goodsid" xorm:"'GOODSID'"` // 点价合约ID - 0:为现货,其它为期货商品合约ID [2:点价 3:暂定价]
  568. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 点价合约代码
  569. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 点价商品名称
  570. PRICEMOVE float64 `json:"pricemove" xorm:"'PRICEMOVE'"` // 升贴水 [2:点价 3:暂定价]
  571. STARTDATE string `json:"startdate" xorm:"'STARTDATE'"` // 点价开始日期 [2:点价 3:暂定价]
  572. ENDDATE string `json:"enddate" xorm:"'ENDDATE'"` // 点价结束日期 [2:点价 3:暂定价]
  573. MARGIN float64 `json:"margin" xorm:"'MARGIN'"` // 当前保证金
  574. CONTRACTMARGIN float64 `json:"contractmargin" xorm:"'CONTRACTMARGIN'"` // 合同保证金
  575. EnumdicName string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  576. CONTRACCTSTATUS int32 `json:"contracctstatus" xorm:"'CONTRACTSTATUS'"` // 合同状态 - 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
  577. Remark string `json:"remark" xorm:"'Remark'"` // 备注
  578. AUDITREMARK string `json:"auditremark" xorm:"'AUDITREMARK'"` // 审核意见
  579. AUDITTIME string `json:"audittime" xorm:"'AUDITTIME'"` // 审核时间
  580. CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  581. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  582. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID(DGFactoryItem表的ID)
  583. BRANDNAME string `json:"brandname" xorm:"'brandname'"` // 品牌名称
  584. ATTACHMENT string `json:"attachment" xorm:"'ATTACHMENT'"` // 附件
  585. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 单位id(取品类上的单位id)
  586. TRADEUSERID int64 `json:"tradeuserid" xorm:"'TRADEUSERID'"` // 交易员id
  587. SALEUSERID int64 `json:"saleuserid" xorm:"'SALEUSERID'"` // 业务员id
  588. MERUSERID int64 `json:"meruserid" xorm:"'MERUSERID'"` // 跟单员id
  589. ACCOUNTID string `json:"accountid" xorm:"'ACCOUNTID'"` // 期货账户id
  590. BIZTYPE int32 `json:"biztype" xorm:"'BIZTYPE'"` // 业务类型 1-套保 2-套利
  591. CURRENCYID int32 `json:"currencyid" xorm:"'CURRENCYID'"` // 币种id
  592. SUBJECTID string `json:"subjectid" xorm:"'SUBJECTID'"` // 交易主体ID
  593. SUBJECTNAME string `json:"subjectname" xorm:"'SUBJECTNAME'"` // 交易主体名称
  594. VATRATE float64 `json:"vatrate" xorm:"'VATRATE'"` // 增值税率
  595. SELLNICKNAME string `json:"sellnickname"` // 采购方昵称
  596. BUYNICKNAME string `json:"buynickname"` // 销售方昵称
  597. TRADEUSERLOGINCODE string `json:"tradeuserlogincode"` // 交易员登录代码
  598. SALEUSERLOGINCODE string `json:"saleuserlogincode"` // 业务员登录代码
  599. MERUSERLOGINCODE string `json:"meruserlogincode"` // 跟单员登录代码
  600. CURRENCYNAME string `json:"currencyname"` // 币种名称
  601. TRADEUSERNAME string `json:"tradeusername"` // 交易员名称
  602. SALEUSERNAME string `json:"saleusername"` // 业务员名称
  603. MERUSERNAME string `json:"merusername"` // 跟单员名称
  604. // 筛选条件
  605. QryType int32 `json:"-"` // 查询类型 1-未提交 2-待审核 3-履约中 4-已完成
  606. UserType int32 `json:"-"` // 用户类型 2-管理员 7-企业成员
  607. OwnUserId int64 `json:"-"` // 登录用户id
  608. }
  609. func (r *Ermcp3Contract) getQryTypeStatus() string {
  610. var status string
  611. switch r.QryType {
  612. case 1: // 未提交
  613. status = "0,4,6"
  614. case 2: // 待审核
  615. status = "1"
  616. case 3: // 履约中
  617. status = "2"
  618. case 4: // 已完成
  619. status = "3,5"
  620. }
  621. return status
  622. }
  623. func (r *Ermcp3Contract) calc() {
  624. r.BUYUSERName, r.BUYNICKNAME = mtpcache.GetUserNameAndNickName(r.BUYUSERID)
  625. r.SELLUSERNAME, r.SELLNICKNAME = mtpcache.GetUserNameAndNickName(r.SELLUSERID)
  626. r.EnumdicName = mtpcache.GetEnumDicitemName(r.UNITID)
  627. r.TRADEUSERLOGINCODE = mtpcache.GetLoginCodeByUserId(r.TRADEUSERID)
  628. r.SALEUSERLOGINCODE = mtpcache.GetLoginCodeByUserId(r.SALEUSERID)
  629. r.MERUSERLOGINCODE = mtpcache.GetLoginCodeByUserId(r.MERUSERID)
  630. r.TRADEUSERNAME = mtpcache.GetUserNameByUserId(r.TRADEUSERID)
  631. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  632. r.SALEUSERNAME = mtpcache.GetUserNameByUserId(r.SALEUSERID)
  633. r.MERUSERNAME = mtpcache.GetUserNameByUserId(r.MERUSERID)
  634. }
  635. func (r *Ermcp3Contract) buildSql() string {
  636. var sqlId utils.SQLVal = `
  637. SELECT to_char(t.SPOTCONTRACTID) SPOTCONTRACTID,
  638. t.CONTRACTNO,
  639. t.CONTRACTTYPE,
  640. t.USERID,
  641. t.BUYUSERID,
  642. t.SELLUSERID,
  643. t.DELIVERYGOODSID,
  644. t.wrstandardid,
  645. t.PRODUCTTYPE,
  646. w.CONVERTFACTOR,
  647. t.SPOTGOODSDESC,
  648. t.PRICETYPE,
  649. t.QTY,
  650. t.PRICE,
  651. t.AMOUNT,
  652. to_char(t.DELIVERYSTARTDATE, 'yyyy-mm-dd hh24:mi:ss') DELIVERYSTARTDATE,
  653. to_char(t.DELIVERYENDDATE, 'yyyy-mm-dd hh24:mi:ss') DELIVERYENDDATE,
  654. t.GOODSID,
  655. t.PRICEMOVE,
  656. to_char(t.STARTDATE, 'yyyy-mm-dd hh24:mi:ss') STARTDATE,
  657. to_char(t.ENDDATE, 'yyyy-mm-dd hh24:mi:ss') ENDDATE,
  658. t.MARGIN,
  659. t.contractmargin,
  660. t.remark,
  661. t.auditremark,
  662. to_char(t.contractattachment) attachment,
  663. t.contractstatus,
  664. t.SPOTGOODSBRANDID,
  665. to_char(t.audittime, 'yyyy-mm-dd hh24:mi:ss') audittime,
  666. to_char(t.createtime, 'yyyy-mm-dd hh24:mi:ss') createtime,
  667. to_char(t.updatetime, 'yyyy-mm-dd hh24:mi:ss') updatetime,
  668. g.deliverygoodscode,
  669. g.deliverygoodsname,
  670. g2.goodscode,
  671. g2.goodsname,
  672. gb.dgfactoryitemvalue brandname,
  673. w.wrstandardname,
  674. w.wrstandardcode,
  675. w.vatrate,
  676. w.unitid,
  677. t.tradeuserid,
  678. t.saleuserid,
  679. t.meruserid,
  680. t.currencyid,
  681. to_char(t.accountid) accountid,
  682. t.biztype,
  683. to_char(t.subjectid) subjectid,
  684. b.subjectname
  685. FROM ERMCP_SPOTCONTRACT t
  686. left join ermcp_pa_areasubject b on t.subjectid=b.subjectid
  687. left join deliverygoods g
  688. on t.deliverygoodsid = g.deliverygoodsid
  689. left join goods g2
  690. on t.goodsid = g2.goodsid
  691. left join DGFACTORYITEM gb
  692. on t.spotgoodsbrandid = gb.dgfactoryitemid
  693. left join wrstandard w
  694. on t.wrstandardid = w.wrstandardid
  695. left join loginaccount l
  696. on t.applyid = l.loginid
  697. where 1 = 1
  698. `
  699. sqlId.AndEx("t.SPOTCONTRACTID", r.SPOTCONTRACTID, len(r.SPOTCONTRACTID) > 0)
  700. sqlId.AndEx("t.CONTRACTTYPE", r.CONTRACTTYPE, r.CONTRACTTYPE != 0)
  701. // 用户条件
  702. sqlId.Join(fmt.Sprintf(" and %v in(t.userid, t.saleuserid, t.tradeuserid, t.meruserid, l.userid)", r.OwnUserId))
  703. status := r.getQryTypeStatus()
  704. if len(status) > 0 {
  705. sqlId.Join(fmt.Sprintf(" and t.contractstatus in (%v)", status))
  706. }
  707. return sqlId.String()
  708. }
  709. // GetDataEx
  710. func (r *Ermcp3Contract) GetDataEx() (interface{}, error) {
  711. sData := make([]Ermcp3Contract, 0)
  712. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  713. for i := range sData {
  714. sData[i].calc()
  715. }
  716. return sData, err
  717. }
  718. // Ermcp3SellBuyContract 采购/销售合同
  719. type Ermcp3SellBuyContract struct {
  720. UserID int64 `json:"userid" xorm:"'UserID'"` // 机构ID
  721. SpotContractId string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` // 合同ID
  722. AccountName string `json:"accountname" xorm:"'ACCOUNTNAME'"` // 账户名称
  723. DeliveryGoodsId int `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货商品ID
  724. DeliveryGoodsCode string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  725. DeliveryGoodsName string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  726. SpotGoodsdesc string `json:"spotgoodsdesc" xorm:"'SPOTGOODSDESC'"` // 商品型号(商品规格)
  727. GoodsId int `json:"goodsid" xorm:"'GOODSID'"` // 点价商品ID
  728. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 点价商品名称
  729. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'"` // 点价商品代码
  730. Pricemove float64 `json:"pricemove" xorm:"'PRICEMOVE'"` // 升贴水
  731. Qty float64 `json:"qty" xorm:"'QTY'"` // 合同量
  732. UnpricedQty float64 `json:"unpricedqty" xorm:"'UNPRICEDQTY'"` // 未定价量
  733. PricedQty float64 `json:"pricedqty" xorm:"'PRICEDQTY'"` // 已定价量
  734. UnsureQty float64 `json:"unsureqty" xorm:"'UNSUREQTY'"` // 未确定量
  735. PayAmount float64 `json:"payamount" xorm:"'PAYAMOUNT'"` // 已收付额(收款或付款)
  736. UnpayAmount float64 `json:"unpayamount"` // 应收付款额(应支付或应收款)
  737. PrePayAmount float64 `json:"prepayamount"` // 预收付额
  738. InvoiceAmount float64 `json:"invoiceamount" xorm:"'INVOICEAMOUNT'"` // 已开票额
  739. DaikaiAmount float64 `json:"daikaiamount" xorm:"'DAIKAIAMOUNT'"` // 应收(开)票额
  740. PreInvoiceAmount float64 `json:"preinvoiceamount"` // 预收(开)票额
  741. StartDate string `json:"startdate" xorm:"'STARTDATE'"` // 点价开始日
  742. EndDate string `json:"enddate" xorm:"'ENDDATE'"` // 点价结束日
  743. DeliveryStartDate string `json:"deliverystartdate" xorm:"'DELIVERYSTARTDATE'"` // 交割开始日
  744. DeliveryendDate string `json:"deliveryenddate" xorm:"'DELIVERYENDDATE'"` // 交割结束日
  745. Convertfactor float64 `json:"convertfactor" xorm:"'CONVERTFACTOR'"` // 标仓系数(品类)
  746. EnumdicName string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  747. Contracctstatus uint `json:"contracctstatus" xorm:"'CONTRACTSTATUS'"` // 合同状态- 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
  748. PriceType int `json:"pricetype" xorm:"'PRICETYPE'"` // 定价类型 - 1:一口价 2:点价 3:暂定价
  749. ProductType int `json:"producttype" xorm:"'PRODUCTTYPE'"` // 产品类型 - 1:标准仓单 2:等标 3:非标
  750. Contracttype int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 合同类型 1-采购, -1-销售
  751. Pricedamount float64 `json:"pricedamount" xorm:"'PRICEDAMOUNT'"` // 已定价额
  752. PricedAvg float64 `json:"pricedavg" xorm:"'PRICEDAVG'"` // 已点均价
  753. Margin float64 `json:"margin" xorm:"'MARGIN'"` // 保证金
  754. CONTRACTMARGIN float64 `json:"contractmargin" xorm:"'CONTRACTMARGIN'"` // 合同保证金
  755. Remark string `json:"remark" xorm:"'Remark'"` // 备注
  756. ATTACHMENT string `json:"attachment" xorm:"'ATTACHMENT'"` // 附件
  757. ReckonRealQty float64 `json:"reckonrealqty" xorm:"'ReckonRealQty'"` // 已确定量
  758. ReckonOtherAmount float64 `json:"reckonotheramount" xorm:"'ReckonOtherAmount'"` // 其它费用
  759. ReckonAdjustAmount float64 `json:"reckonadjustamount" xorm:"'ReckonAdjustAmount'"` // 调整金额
  760. Price float64 `json:"price" xorm:"'Price'"` // 价格
  761. LoanAmount float64 `json:"loanamount" xorm:"'LoanAmount'"` // 贷款总额=已定价额+调整金额
  762. Contractno string `json:"contractno" xorm:"'Contractno'"` // 合同编号
  763. TotalAmount float64 `json:"totalamount" xorm:"'-'"` // 合计总额
  764. ReckonedAmount float64 `json:"reckonedamount" xorm:"'ReckonedAmount'"` // 实际已收付额(已确定额,已收付总额-已退款总额)
  765. BUYUSERID int64 `json:"-" xorm:"'BUYUSERID'"` // 采购方ID
  766. SELLUSERID int64 `json:"-" xorm:"'SELLUSERID'"` // 销售方ID
  767. AUDITTIME string `json:"audittime" xorm:"'AUDITTIME'"` // 审核时间
  768. CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  769. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID(DGFactoryItem表的ID)
  770. BRANDNAME string `json:"brandname" xorm:"'brandname'"` // 品牌名称
  771. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类ID
  772. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  773. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  774. TRADEUSERID int64 `json:"tradeuserid" xorm:"'TRADEUSERID'"` // 交易员id
  775. SALEUSERID int64 `json:"saleuserid" xorm:"'SALEUSERID'"` // 业务员id
  776. MERUSERID int64 `json:"meruserid" xorm:"'MERUSERID'"` // 跟单员id
  777. ACCOUNTID string `json:"accountid" xorm:"'ACCOUNTID'"` // 期货账户id
  778. BIZTYPE int32 `json:"biztype" xorm:"'BIZTYPE'"` // 业务类型 1-套保 2-套利
  779. CURRENCYID int32 `json:"currencyid" xorm:"'CURRENCYID'"` // 币种id
  780. SUBJECTID string `json:"subjectid" xorm:"'SUBJECTID'"` // 交易主体ID
  781. SUBJECTNAME string `json:"subjectname" xorm:"'SUBJECTNAME'"` // 交易主体名称
  782. VATRATE float64 `json:"vatrate" xorm:"'VATRATE'"` // 增值税率
  783. NICKNAME string `json:"nickname"` // 账户昵称
  784. TRADEUSERLOGINCODE string `json:"tradeuserlogincode"` // 交易员登录代码
  785. SALEUSERLOGINCODE string `json:"saleuserlogincode"` // 业务员登录代码
  786. MERUSERLOGINCODE string `json:"meruserlogincode"` // 跟单员登录代码
  787. CURRENCYNAME string `json:"currencyname"` // 币种名称
  788. TRADEUSERNAME string `json:"tradeusername"` // 交易员名称
  789. SALEUSERNAME string `json:"saleusername"` // 业务员名称
  790. MERUSERNAME string `json:"merusername"` // 跟单员名称
  791. // 筛选条件
  792. QryType int32 `json:"-"` // 查询类型 1-未提交 2-待审核 3-履约中 4-已完成
  793. UserType int32 `json:"-"` // 用户类型 2-管理员 7-企业成员
  794. OwnUserId int64 `json:"-"` // 登录用户id
  795. }
  796. func (r *Ermcp3SellBuyContract) calc() {
  797. r.TotalAmount = r.LoanAmount + r.ReckonOtherAmount
  798. // #95629 公式变更 modify:2021-06-16 09:54:08
  799. // 应付款额=货款总额-已付款额+已退款额
  800. // 应收款额=货款总额-已收款额+已退款额
  801. r.UnpayAmount = r.LoanAmount - r.ReckonedAmount
  802. // 预付款额=已付款额-已退款额-货款总额
  803. // 预收款额=已收款额-已退款额-货款总额
  804. r.PrePayAmount = r.ReckonedAmount - r.LoanAmount
  805. // 预收票额=已收票额-货款总额
  806. // 预开票额=已开票额-货款总额
  807. r.PreInvoiceAmount = r.InvoiceAmount - r.LoanAmount
  808. // 应收票额=货款总额-已收票额
  809. // 应开票额=货款总额-已开票额
  810. r.DaikaiAmount = r.LoanAmount - r.InvoiceAmount
  811. if r.Contracttype == 1 {
  812. //采购合同, 取销售方名称
  813. r.AccountName, r.NICKNAME = mtpcache.GetUserNameAndNickName(r.SELLUSERID)
  814. } else {
  815. //销售合同, 取采购方名称
  816. r.AccountName, r.NICKNAME = mtpcache.GetUserNameAndNickName(r.BUYUSERID)
  817. }
  818. r.TRADEUSERLOGINCODE = mtpcache.GetLoginCodeByUserId(r.TRADEUSERID)
  819. r.SALEUSERLOGINCODE = mtpcache.GetLoginCodeByUserId(r.SALEUSERID)
  820. r.MERUSERLOGINCODE = mtpcache.GetLoginCodeByUserId(r.MERUSERID)
  821. r.TRADEUSERNAME = mtpcache.GetUserNameByUserId(r.TRADEUSERID)
  822. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  823. r.SALEUSERNAME = mtpcache.GetUserNameByUserId(r.SALEUSERID)
  824. r.MERUSERNAME = mtpcache.GetUserNameByUserId(r.MERUSERID)
  825. }
  826. func (r *Ermcp3SellBuyContract) buildSql() string {
  827. var sqlId utils.SQLVal = `
  828. select t.contractno,
  829. to_char(t.SpotContractId) SpotContractId,
  830. t.deliverygoodsid,
  831. t.spotgoodsdesc,
  832. t.goodsid,
  833. t.pricemove,
  834. t.userid,
  835. t.BUYUSERID,
  836. t.SELLUSERID,
  837. t.qty,
  838. t.qty - t.pricedqty unpricedqty,
  839. t.pricedqty,
  840. (case
  841. when t.pricetype = 3 then
  842. t.qty - t.RECKONREALQTY
  843. else
  844. t.pricedqty - nvl(t.ReckonRealQty, 0)
  845. end) unsureqty,
  846. t.reckonedamount PayAmount,
  847. t.invoiceamount,
  848. (t.pricedamount + t.RECKONADJUSTAMOUNT - t.invoiceamount) as daikaiAmount,
  849. to_char(t.startdate, 'yyyy-mm-dd hh24:mi:ss') startdate,
  850. to_char(t.enddate, 'yyyy-mm-dd hh24:mi:ss') enddate,
  851. to_char(t.deliverystartdate, 'yyyy-mm-dd hh24:mi:ss') deliverystartdate,
  852. to_char(t.deliveryenddate, 'yyyy-mm-dd hh24:mi:ss') deliveryenddate,
  853. wr.convertfactor,
  854. t.contractstatus,
  855. t.pricetype,
  856. t.producttype,
  857. t.contracttype,
  858. t.remark,
  859. to_char(t.contractattachment) attachment,
  860. t.margin,
  861. t.contractmargin,
  862. t.SPOTGOODSBRANDID,
  863. t.tradeuserid,
  864. t.saleuserid,
  865. t.meruserid,
  866. t.currencyid,
  867. t.biztype,
  868. g.deliverygoodscode,
  869. g.deliverygoodsname,
  870. wr.wrstandardid,
  871. wr.wrstandardname,
  872. wr.wrstandardcode,
  873. wr.vatrate,
  874. g2.goodscode,
  875. g2.goodsname,
  876. e.enumdicname,
  877. t.contracttype,
  878. t.pricedamount,
  879. case
  880. when t.pricedqty > 0 then
  881. (t.pricedamount) / t.pricedqty
  882. else
  883. 0
  884. end pricedAvg,
  885. t.ReckonRealQty,
  886. t.ReckonOtherAmount,
  887. t.ReckonAdjustAmount,
  888. t.Price,
  889. t.ReckonedAmount,
  890. to_char(t.audittime, 'yyyy-mm-dd hh24:mi:ss') audittime,
  891. to_char(t.createtime, 'yyyy-mm-dd hh24:mi:ss') createtime,
  892. t.pricedamount + t.ReckonAdjustAmount as LoanAmount,
  893. gb.dgfactoryitemvalue BRANDNAME,
  894. to_char(t.subjectid) subjectid,
  895. b.subjectname
  896. from ermcp_spotcontract t
  897. left join ermcp_pa_areasubject b on t.subjectid=b.subjectid
  898. left join deliverygoods g
  899. on t.deliverygoodsid = g.deliverygoodsid
  900. left join goods g2
  901. on t.goodsid = g2.goodsid
  902. left join wrstandard wr
  903. on t.wrstandardid = wr.wrstandardid
  904. left join DGFactoryItem gb
  905. on t.spotgoodsbrandid = gb.dgfactoryitemid
  906. left join enumdicitem e
  907. on wr.unitid = e.enumitemname
  908. and e.enumdiccode = 'goodsunit'
  909. where 1 = 1
  910. `
  911. sqlId.AndEx("t.userid", r.UserID, r.UserID > 0)
  912. sqlId.AndEx("t.contracttype", r.Contracttype, r.Contracttype != 0)
  913. sqlId.AndEx("t.SpotContractId", r.SpotContractId, len(r.SpotContractId) > 0)
  914. if r.UserType == 7 {
  915. // 企业成员
  916. sqlId.Join(fmt.Sprintf(" and %v in(t.saleuserid, t.tradeuserid, t.meruserid)", r.OwnUserId))
  917. }
  918. var status string
  919. nQueryType := r.QryType
  920. if 1 == nQueryType {
  921. // 全部
  922. status = "2,3"
  923. } else if 2 == nQueryType {
  924. // 待点价
  925. status = "2"
  926. } else {
  927. // 履约
  928. status = "2"
  929. }
  930. if len(status) > 0 {
  931. sqlId.Join(fmt.Sprintf(" and t.contractstatus in (%v)", status))
  932. }
  933. sqlId.JoinEx(r.QryType == 2, " and t.qty - t.pricedqty > 0 and t.pricetype !=1")
  934. return sqlId.String()
  935. }
  936. // GetDataEx
  937. func (r *Ermcp3SellBuyContract) GetDataEx() (interface{}, error) {
  938. sData := make([]Ermcp3SellBuyContract, 0)
  939. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  940. for i := range sData {
  941. sData[i].calc()
  942. }
  943. return sData, err
  944. }
  945. // Ermcp3ExposureDetail 敞口现货明细
  946. type Ermcp3ExposureDetail struct {
  947. Createtime string `json:"createtime" xorm:"'createtime'"` // 时间
  948. Areauserid int64 `json:"areauserid" xorm:"'areauserid'"` // 机构ID
  949. Logtype int32 `json:"logtype" xorm:"'logtype'"` // 类型 - 1:套保计划 2:现货合同
  950. Contracttype int32 `json:"contracttype" xorm:"'contracttype'"` // 现货合同类型 - 1:采购 -1:销售
  951. Qty float64 `json:"qty" xorm:"'qty'"` // 数量
  952. RelateNo string `json:"relateNo" xorm:"'relateNo'"` // 现货合同/套保计划编号
  953. Middlegoodsname string `json:"middlegoodsname" xorm:"'middlegoodsname'"` // 套保商品名称
  954. Middlegoodscode string `json:"middlegoodscode" xorm:"'middlegoodscode'"` // 套保商品代码
  955. MiddlegoodsId int32 `json:"middlegoodsId" xorm:"'middlegoodsId'"` // 套保商品id
  956. ChangeQty float64 `json:"changeQty" xorm:"'changeQty'"` // 套保变动量
  957. Convertfactor float64 `json:"convertfactor" xorm:"'convertfactor'"` // 标仓系数
  958. Convertratio float64 `json:"convertratio" xorm:"'convertratio'"` // 套保系数
  959. DELIVERYGOODSID int32 `json:"-" xorm:"'DELIVERYGOODSID'"` // 现货品种id
  960. DELIVERYGOODSCODE string `json:"-" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  961. DELIVERYGOODSNAME string `json:"-" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  962. MGUNITID int32 `json:"mgunitid" xorm:"'MGUNITID'"` // 套保品种单位id
  963. MGUNITIDNAME string `json:"mgunitidname"` // 套保品种单位名称
  964. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID(SEQ_WRSTANDARD)
  965. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货商品代码
  966. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 现货商品名称
  967. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 现货商品单位id
  968. ENUMDICNAME string `json:"enumdicname" xorm:"'enumdicname'"` // 现货商品单位名称
  969. }
  970. func (r *Ermcp3ExposureDetail) calc() {
  971. // 销售合同转换为负数
  972. if r.Logtype == 2 && r.Contracttype == -1 {
  973. r.ChangeQty *= -1
  974. r.Qty *= -1
  975. }
  976. // 采购计划 转换为负数
  977. if r.Logtype == 1 && r.Contracttype == 1 {
  978. r.ChangeQty *= -1
  979. r.Qty *= -1
  980. }
  981. r.MGUNITIDNAME = mtpcache.GetEnumDicitemName(r.MGUNITID)
  982. }
  983. func (r *Ermcp3ExposureDetail) buildSql() string {
  984. var sqlId utils.SQLVal = `
  985. select to_char(t.updatetime, 'yyyy-mm-dd hh24:mi:ss') createtime,
  986. s.userid as areauserid,
  987. 2 as LogType,
  988. s.contracttype,
  989. t.todaypricedqty qty,
  990. to_char(s.spotcontractid) as relateid,
  991. s.contractno as relateNo,
  992. t.todaypricedqty * c.convertratio * (1 / (1 + nvl(w.vatrate, 0))) as changeQty,
  993. m.middlegoodsid,
  994. m.middlegoodsname,
  995. m.middlegoodscode,
  996. m.goodsunitid mgunitid,
  997. w.wrstandardid,
  998. w.wrstandardcode,
  999. w.wrstandardname,
  1000. w.unitid,
  1001. e.enumdicname
  1002. from ERMCP_SCMiddleGoods t
  1003. inner join ermcp_spotcontract s
  1004. on t.spotcontractid = s.spotcontractid
  1005. left join erms_middlegoods m
  1006. on t.middlegoodsid = m.middlegoodsid
  1007. left join erms2_wrsconvertdetail c
  1008. on t.middlegoodsid = c.middlegoodsid
  1009. and t.wrstandardid = c.wrstandardid
  1010. left join wrstandard w
  1011. on t.wrstandardid = w.wrstandardid
  1012. left join enumdicitem e
  1013. on w.unitid = e.enumitemname
  1014. and e.enumdiccode = 'goodsunit'
  1015. where 1=1
  1016. `
  1017. sqlId.And("s.userid", r.Areauserid)
  1018. sqlId.And("t.middlegoodsid", r.MiddlegoodsId)
  1019. return sqlId.String()
  1020. }
  1021. // GetDataEx
  1022. func (r *Ermcp3ExposureDetail) GetDataEx() (interface{}, error) {
  1023. e := db.GetEngine()
  1024. s := e.SQL(r.buildSql())
  1025. sData := make([]Ermcp3ExposureDetail, 0)
  1026. if err := s.Find(&sData); err != nil {
  1027. return nil, err
  1028. }
  1029. for i := range sData {
  1030. sData[i].calc()
  1031. }
  1032. return sData, nil
  1033. }
  1034. // Ermcp3AreaSpot 敞口现货头寸
  1035. type Ermcp3AreaSpot struct {
  1036. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  1037. ORIBUYPLANQTY float64 `json:"-" xorm:"'ORIBUYPLANQTY'"` // 期初采购计划数量
  1038. ORIBUYPRICEDQTY float64 `json:"-" xorm:"'ORIBUYPRICEDQTY'"` // 期初采购合同已定价数量
  1039. ORISELLPLANQTY float64 `json:"-" xorm:"'ORISELLPLANQTY'"` // 期初销售计划数量
  1040. ORISELLPRICEDQTY float64 `json:"-" xorm:"'ORISELLPRICEDQTY'"` // 期初销售合同已定价数量
  1041. BUYPLANQTY float64 `json:"-" xorm:"'BUYPLANQTY'"` // 采购计划数量
  1042. BUYPRICEDQTY float64 `json:"-" xorm:"'BUYPRICEDQTY'"` // 采购合同已定价数量
  1043. SELLPLANQTY float64 `json:"-" xorm:"'SELLPLANQTY'"` // 销售计划数量
  1044. SELLPRICEDQTY float64 `json:"-" xorm:"'SELLPRICEDQTY'"` // 销售合同已定价数量
  1045. TOTALSPOTQTY float64 `json:"totalspotqty" xorm:"'TOTALSPOTQTY'"` // 当前数量(现货头寸总量) = (销售计划数量 - 销售已定价数量) - (采购计划数量 - 采购已定价数量)
  1046. OriToalSpotQty float64 `json:"oritoalspotqty" xorm:"'OriToalSpotQty'"` // 昨日数量
  1047. IncreaseQty float64 `json:"increaseqty" xorm:"'IncreaseQty'"` // 增加数量=销售计划数量+采购已定价数量
  1048. DecreaseQty float64 `json:"decreaseqty" xorm:"'DecreaseQty'"` // 减少数量=-(销售已定价数量+采购计划数量)
  1049. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  1050. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 套保系数
  1051. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID(SEQ_WRSTANDARD)
  1052. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货商品代码
  1053. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 现货商品名称
  1054. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 现货商品单位id
  1055. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  1056. }
  1057. func (r *Ermcp3AreaSpot) calc() {
  1058. /*
  1059. 增加数量=今日销售计划数量+今日采购已定价数量
  1060. 减少数量=-(今日销售已定价数量+今日采购计划数量)
  1061. 其中,
  1062. 今日销售计划数量=期末销售计划数量-期初销售计划数量
  1063. 今日采购已定价数量=期末采购已定价数量-期初采购已定价数量
  1064. 今日销售已定价数量=期末销售已定价数量-期初销售已定价数量
  1065. 今日采购计划数量=期末采购计划数量-期初采购计划数量
  1066. */
  1067. r.IncreaseQty = r.SELLPLANQTY - r.ORISELLPLANQTY + r.BUYPRICEDQTY - r.ORIBUYPRICEDQTY
  1068. r.DecreaseQty = (r.SELLPRICEDQTY - r.ORISELLPRICEDQTY + r.BUYPLANQTY - r.ORIBUYPLANQTY) * -1
  1069. r.OriToalSpotQty = (r.ORISELLPLANQTY - r.ORISELLPRICEDQTY) - (r.ORIBUYPLANQTY - r.ORIBUYPRICEDQTY)
  1070. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  1071. }
  1072. func (r *Ermcp3AreaSpot) buildSql() string {
  1073. var sqlId utils.SQLVal = `
  1074. select t.AREAUSERID,
  1075. t.ORIBUYPLANQTY,
  1076. t.ORIBUYPRICEDQTY,
  1077. t.ORISELLPLANQTY,
  1078. t.ORISELLPRICEDQTY,
  1079. t.BUYPLANQTY,
  1080. t.BUYPRICEDQTY,
  1081. t.SELLPLANQTY,
  1082. t.SELLPRICEDQTY,
  1083. t.hedgeqty,
  1084. t.arbitrageqty,
  1085. t.hedgeqty + t.arbitrageqty TOTALSPOTQTY,
  1086. t.convertratio,
  1087. w.wrstandardid,
  1088. w.wrstandardcode,
  1089. w.wrstandardname,
  1090. w.unitid,
  1091. to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME
  1092. from ermcp_areamiddlesum t
  1093. left join wrstandard w
  1094. on t.wrstandardid = w.wrstandardid
  1095. where 1 = 1
  1096. `
  1097. sqlId.And("t.areauserid", r.AREAUSERID)
  1098. return sqlId.String()
  1099. }
  1100. // GetDataEx
  1101. func (r *Ermcp3AreaSpot) GetDataEx() (interface{}, error) {
  1102. e := db.GetEngine()
  1103. s := e.SQL(r.buildSql())
  1104. sData := make([]Ermcp3AreaSpot, 0)
  1105. if err := s.Find(&sData); err != nil {
  1106. return nil, err
  1107. }
  1108. for i := range sData {
  1109. sData[i].calc()
  1110. }
  1111. return sData, nil
  1112. }
  1113. // Ermcp3AreaSpotDetail 敞口->现货头寸->现货明细
  1114. type Ermcp3AreaSpotDetail struct {
  1115. Relatedid string `json:"relatedid" xorm:"'relatedid'"` // 套保计划ID/现货合同ID
  1116. Relatedno string `json:"relatedno" xorm:"'relatedno'"` // 编号
  1117. LogType int32 `json:"logtype" xorm:"'logType'"` // 记录类型 1-套保 2-现货合同
  1118. Contracttype int32 `json:"contracttype" xorm:"'contracttype'"` // 合同类型 1-采购 -1-销售
  1119. Qty float64 `json:"qty" xorm:"'qty'"` // 数量
  1120. Strtime string `json:"strtime" xorm:"'strtime'"` // 时间
  1121. Enumdicname string `json:"enumdicname"` // 现货商品单位名称
  1122. Recordname string `json:"recordname"` // 类型名称
  1123. CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  1124. Unitid int32 `json:"-" xorm:"'UNITID'"` // 单位ID
  1125. UserId int64 `json:"-"` // 所属用户ID
  1126. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID(SEQ_WRSTANDARD)
  1127. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货商品代码
  1128. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 现货商品名称
  1129. }
  1130. func (r *Ermcp3AreaSpotDetail) calc() {
  1131. var logTypeName, contractTypeName string
  1132. if r.LogType == 1 {
  1133. logTypeName = "计划"
  1134. } else {
  1135. logTypeName = "合同"
  1136. }
  1137. if r.Contracttype == 1 {
  1138. contractTypeName = "采购"
  1139. } else {
  1140. contractTypeName = "销售"
  1141. }
  1142. // 销售合同 数量转为负数
  1143. if r.LogType == 2 && r.Contracttype == -1 {
  1144. if r.Qty > 0 {
  1145. r.Qty = r.Qty * -1
  1146. }
  1147. }
  1148. // 采购计划 数量转为负数
  1149. if r.LogType == 1 && r.Contracttype == 1 {
  1150. if r.Qty > 0 {
  1151. r.Qty = r.Qty * -1
  1152. }
  1153. }
  1154. r.Recordname = contractTypeName + logTypeName
  1155. r.Enumdicname = mtpcache.GetEnumDicitemName(r.Unitid)
  1156. }
  1157. func (r *Ermcp3AreaSpotDetail) buildSql() string {
  1158. var sqlId utils.SQLVal = `
  1159. select *
  1160. from (select to_char(t.hedgeplanid) as relatedid,
  1161. t.hedgeplanno relatedno,
  1162. 1 as LogType,
  1163. t.contracttype,
  1164. t.planqty qty,
  1165. to_char(t.audittime, 'yyyy-mm-dd hh24:mi:ss') createtime,
  1166. w.wrstandardid,
  1167. w.wrstandardcode,
  1168. w.wrstandardname,
  1169. w.unitid,
  1170. t.areauserid as userid
  1171. from ermcp_hedgeplan t
  1172. inner join wrstandard w
  1173. on t.wrstandardid = w.wrstandardid
  1174. where t.hedgeplanstatus in (2, 3, 5)
  1175. and t.tradedate = to_char(sysdate, 'yyyymmdd')
  1176. and t.areauserid = %v
  1177. and t.wrstandardid = %v
  1178. union all
  1179. select to_char(t.spotcontractid) as relatedid,
  1180. t.contractno relatedno,
  1181. 2 as LogType,
  1182. t.contracttype,
  1183. t.qty,
  1184. to_char(t.audittime, 'yyyy-mm-dd hh24:mi:ss') createtime,
  1185. w.wrstandardid,
  1186. w.wrstandardcode,
  1187. w.wrstandardname,
  1188. w.unitid,
  1189. t.userid
  1190. from ermcp_spotcontract t
  1191. inner join wrstandard w
  1192. on t.wrstandardid = w.wrstandardid
  1193. where t.userid = %v
  1194. and t.wrstandardid = %v
  1195. and t.contractstatus in (2, 3, 5)
  1196. and t.tradedate = to_char(sysdate, 'yyyymmdd')) a
  1197. order by a.createtime desc
  1198. `
  1199. sqlId.FormatParam(r.UserId, r.WRSTANDARDID, r.UserId, r.WRSTANDARDID)
  1200. return sqlId.String()
  1201. }
  1202. // GetDataEx
  1203. func (r *Ermcp3AreaSpotDetail) GetDataEx() (interface{}, error) {
  1204. e := db.GetEngine()
  1205. s := e.SQL(r.buildSql())
  1206. sData := make([]Ermcp3AreaSpotDetail, 0)
  1207. if err := s.Find(&sData); err != nil {
  1208. return nil, err
  1209. }
  1210. for i := range sData {
  1211. sData[i].calc()
  1212. }
  1213. return sData, nil
  1214. }
  1215. // Ermcp3Wrstandard 现货品类
  1216. type Ermcp3Wrstandard struct {
  1217. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类ID(SEQ_WRSTANDARD)
  1218. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1219. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种ID
  1220. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 品类单位ID
  1221. MINIVALUE int32 `json:"minivalue" xorm:"'MINIVALUE'"` // 最小变动值
  1222. MINIVALUEDP int32 `json:"minivaluedp" xorm:"'MINIVALUEDP'"` // 最小变动值小数位
  1223. REALMINIVALUE int32 `json:"realminivalue" xorm:"'REALMINIVALUE'"` // 实际最小变动值
  1224. REALMINIVALUEDP int32 `json:"realminivaluedp" xorm:"'REALMINIVALUEDP'"` // 实际最小变动值小数位
  1225. CREATORID int64 `json:"creatorid" xorm:"'CREATORID'"` // 创建人
  1226. CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  1227. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1228. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  1229. EnumdicName string `json:"enumdicname"` // 品类单位名称
  1230. ISVALID int32 `json:"isvalid" xorm:"'ISVALID'"` // 是否有效 0-无效(停用) 1-有效(正常)
  1231. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  1232. REMARK string `json:"remark" xorm:"'REMARK'"` // 备注
  1233. CONVERTFACTOR float64 `json:"convertfactor" xorm:"'CONVERTFACTOR'"` // 标仓系数
  1234. CANTRADE int32 `json:"cantrade" xorm:"'CANTRADE'"` // 是否允许交易 - 0:不允许 1:允许 -1:未配置(数据库里没有记录)
  1235. }
  1236. func (r *Ermcp3Wrstandard) calc() {
  1237. r.EnumdicName = mtpcache.GetEnumDicitemName(r.UNITID)
  1238. }
  1239. func (r *Ermcp3Wrstandard) buildSql() string {
  1240. sqlId := "select t.WRSTANDARDID," +
  1241. " t.WRSTANDARDCODE," +
  1242. " t.DELIVERYGOODSID," +
  1243. " t.UNITID," +
  1244. " t.MINIVALUE," +
  1245. " t.MINIVALUEDP," +
  1246. " t.REALMINIVALUE," +
  1247. " t.REALMINIVALUEDP," +
  1248. " t.CREATORID," +
  1249. " to_char(t.CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME," +
  1250. " t.WRSTANDARDNAME," +
  1251. " t.ISVALID," +
  1252. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1253. " t.REMARK," +
  1254. " t.CONVERTFACTOR," +
  1255. " t.AREAUSERID," +
  1256. " nvl(m.cantrade,-1) cantrade" +
  1257. " from wrstandard t" +
  1258. " left join marketspotgoodsconfig m on t.wrstandardid = m.wrstandardid" +
  1259. " where 1 = 1"
  1260. if r.AREAUSERID > 0 {
  1261. sqlId += fmt.Sprintf(" and t.AREAUSERID=%v", r.AREAUSERID)
  1262. }
  1263. if r.DELIVERYGOODSID > 0 {
  1264. sqlId = sqlId + fmt.Sprintf(" and t.DELIVERYGOODSID=%v", r.DELIVERYGOODSID)
  1265. }
  1266. sqlId += " order by createtime desc"
  1267. return sqlId
  1268. }
  1269. // GetDataEx
  1270. func (r *Ermcp3Wrstandard) GetDataEx() (interface{}, error) {
  1271. e := db.GetEngine()
  1272. s := e.SQL(r.buildSql())
  1273. sData := make([]Ermcp3Wrstandard, 0)
  1274. if err := s.Find(&sData); err != nil {
  1275. return nil, err
  1276. }
  1277. for i := range sData {
  1278. sData[i].calc()
  1279. }
  1280. return sData, nil
  1281. }
  1282. // Ermcp3AreaStockApply 出入库申请
  1283. type Ermcp3AreaStockApply struct {
  1284. INOUTAPPLYID string `json:"inoutapplyid" xorm:"'INOUTAPPLYID'"` // 申请ID(607+Unix秒时间戳(10位)+xxxxxx)
  1285. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构ID
  1286. INOUTTYPE int32 `json:"inouttype" xorm:"'INOUTTYPE'"` // 出入库类型 - 5:采购入库 6:销售出库 7:生产入库 8:生产出库
  1287. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类ID
  1288. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID
  1289. SPOTCONTRACTID string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` // 关联现货合同ID
  1290. WAREHOUSEINFOID string `json:"warehouseinfoid" xorm:"'WAREHOUSEINFOID'"` // 现货仓库ID
  1291. APPLYSTATUS int32 `json:"applystatus" xorm:"'APPLYSTATUS'"` // 申请状态 - 1:待审核 2:审核通过 3:审核拒绝 4:处理失败 5:已撤回
  1292. APPLYSRC int32 `json:"applysrc" xorm:"'APPLYSRC'"` // 申请来源 - 1:管理端 2:终端
  1293. APPLYID int64 `json:"applyid" xorm:"'APPLYID'"` // 申请人
  1294. APPLYREMARK string `json:"applyremark" xorm:"'APPLYREMARK'"` // 申请备注
  1295. APPLYTIME string `json:"applytime" xorm:"'APPLYTIME'"` // 申请时间
  1296. AUDITSRC int32 `json:"auditsrc" xorm:"'AUDITSRC'"` // 审核来源 - 1:管理端 2:终端
  1297. AUDITID int64 `json:"auditid" xorm:"'AUDITID'"` // 审核人
  1298. AUDITTIME string `json:"audittime" xorm:"'AUDITTIME'"` // 审核时间
  1299. AUDITREMARK string `json:"auditremark" xorm:"'AUDITREMARK'"` // 审核备注
  1300. AUDITTRADEDATE string `json:"audittradedate" xorm:"'AUDITTRADEDATE'"` // 审核交易日(yyyyMMdd)
  1301. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1302. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1303. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 单位id
  1304. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1305. WAREHOUSENAME string `json:"warehousename" xorm:"'WAREHOUSENAME'"` // 仓库名称
  1306. WAREHOUSECODE string `json:"warehousecode" xorm:"'WAREHOUSECODE'"` // 仓库代码
  1307. WAREHOUSETYPE int32 `json:"warehousetype" xorm:"'WAREHOUSETYPE'"` // 仓库类型 - 1 厂库 2 自有库 3 合作库
  1308. CONTRACTNO string `json:"contractno" xorm:"'CONTRACTNO'"` // 合同编号
  1309. CONTRACTTYPE int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 现货合同类型 - 1:采购 -1:销售
  1310. BUYUSERID int64 `json:"buyuserid" xorm:"'BUYUSERID'"` // 采购方userid
  1311. SELLUSERID int64 `json:"selluserid" xorm:"'SELLUSERID'"` // 销售方userid
  1312. PRICETYPE int32 `json:"pricetype" xorm:"'PRICETYPE'"` // 定价类型 - 1:一口价 2:点价 3:暂定价
  1313. CONTRACTQTY float64 `json:"contractqty" xorm:"'CONTRACTQTY'"` // 合同量
  1314. DETAILJSON string `json:"detailjson" xorm:"'DETAILJSON'"` // 明细数据
  1315. BUYUSERNAME string `json:"buyusername"` // 采购方名称
  1316. SELLUSERNAME string `json:"sellusername"` // 销售方名称
  1317. APPLYNAME string `json:"applyname"` // 申请人名称
  1318. AUDITNAME string `json:"auditname"` // 审核人名称
  1319. ENUMDICNAME string `json:"enumdicname"` // 现货商品单位名称
  1320. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种id
  1321. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  1322. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  1323. BUYNICKNAME string `json:"buynickname"` // 采购方昵称
  1324. SELLNICKNAME string `json:"sellnickname"` // 销售方昵称
  1325. QTY float64 `json:"qty"` // 数量
  1326. FilterStatus string `json:"-"` // 查询条件, 申请状态, 逗号隔开
  1327. FilterType string `json:"-"` // 查询条件, 出入库类型, 逗号隔开
  1328. BeginDate string `json:"-"` // 开始日期
  1329. EndDate string `json:"-"` // 结束日期
  1330. UserType int32 `json:"-"` // 用户类型
  1331. }
  1332. func (r *Ermcp3AreaStockApply) calc() {
  1333. // 采购方名称
  1334. r.BUYUSERNAME, r.BUYNICKNAME = mtpcache.GetUserNameAndNickName(r.BUYUSERID)
  1335. // 销售方名称
  1336. r.SELLUSERNAME, r.SELLNICKNAME = mtpcache.GetUserNameAndNickName(r.SELLUSERID)
  1337. // 申请人名称
  1338. if r.APPLYSRC == 1 {
  1339. r.APPLYNAME = mtpcache.GetSystemmangerLoginCode(r.APPLYID)
  1340. } else {
  1341. r.APPLYNAME = mtpcache.GetLoginCodeByLoginId(r.APPLYID)
  1342. }
  1343. // 审核人名称
  1344. if r.AUDITSRC == 1 {
  1345. r.AUDITNAME = mtpcache.GetSystemmangerLoginCode(r.AUDITID)
  1346. } else {
  1347. r.AUDITNAME = mtpcache.GetLoginCodeByLoginId(r.AUDITID)
  1348. }
  1349. // 单位名称
  1350. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  1351. if len(r.DETAILJSON) > 0 {
  1352. // 解析qty字段
  1353. req := struct {
  1354. QTY float64 `json:"InOutQty"` // 数量
  1355. }{}
  1356. if json.Unmarshal([]byte(r.DETAILJSON), &req) == nil {
  1357. r.QTY = req.QTY
  1358. }
  1359. }
  1360. }
  1361. func (r *Ermcp3AreaStockApply) buildSql() string {
  1362. var sqlId utils.SQLVal = "SELECT to_char(t.operateapplyid) INOUTAPPLYID," +
  1363. " t.USERID," +
  1364. " t.operateapplytype INOUTTYPE," +
  1365. " t.WRSTANDARDID," +
  1366. " t.SPOTGOODSBRANDID," +
  1367. " t.DELIVERYGOODSID," +
  1368. " to_char(t.relatedid) SPOTCONTRACTID," +
  1369. " to_char(t.WAREHOUSEINFOID) WAREHOUSEINFOID," +
  1370. " to_char(t.detailjson) detailjson," +
  1371. " t.APPLYSTATUS," +
  1372. " t.APPLYSRC," +
  1373. " t.APPLYID," +
  1374. " t.remark APPLYREMARK," +
  1375. " to_char(t.APPLYTIME, 'yyyy-mm-dd hh24:mi:ss') APPLYTIME," +
  1376. " t.AUDITSRC," +
  1377. " t.AUDITID," +
  1378. " to_char(t.AUDITTIME, 'yyyy-mm-dd hh24:mi:ss') AUDITTIME," +
  1379. " t.AUDITREMARK," +
  1380. " t.AUDITTRADEDATE," +
  1381. " w.wrstandardname," +
  1382. " w.wrstandardcode," +
  1383. " g.goodsunitid unitid," +
  1384. " gb.dgfactoryitemvalue brandname," +
  1385. " h.warehousename," +
  1386. " h.warehousecode," +
  1387. " h.warehousetype," +
  1388. " s.contractno," +
  1389. " s.contracttype," +
  1390. " s.buyuserid," +
  1391. " s.selluserid," +
  1392. " s.pricetype," +
  1393. " s.qty CONTRACTQTY," +
  1394. " g.deliverygoodscode," +
  1395. " g.deliverygoodsname" +
  1396. " FROM ermcp_contractoperateapply t" +
  1397. " left join wrstandard w" +
  1398. " on t.wrstandardid = w.wrstandardid" +
  1399. " left join dgfactoryitem gb" +
  1400. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1401. " left join deliverygoods g" +
  1402. " on t.deliverygoodsid = g.deliverygoodsid" +
  1403. " left join warehouseinfo h" +
  1404. " on t.warehouseinfoid = h.autoid" +
  1405. " left join ermcp_spotcontract s" +
  1406. " on t.relatedid = s.spotcontractid" +
  1407. " WHERE t.operateapplytype in(5,6,7,8)"
  1408. areaUserId := mtpcache.GetAreaUserId(r.USERID, r.UserType)
  1409. if areaUserId == r.USERID {
  1410. // 机构用户
  1411. sqlId.And("t.USERID", r.USERID)
  1412. } else {
  1413. if r.INOUTTYPE == 7 || r.INOUTTYPE == 8 {
  1414. // 生产出入库, 没有相关合同
  1415. sqlId.And("t.USERID", r.USERID)
  1416. } else {
  1417. sqlId.Join(fmt.Sprintf(" and %v in(s.saleuserid, s.tradeuserid, s.meruserid)", r.USERID))
  1418. }
  1419. }
  1420. // 合同id
  1421. sqlId.AndInterEx("t.RELATEDID", r.SPOTCONTRACTID, len(r.SPOTCONTRACTID) > 0)
  1422. // 现货品种id
  1423. if r.DELIVERYGOODSID > 0 {
  1424. sqlId.And("t.DELIVERYGOODSID", r.DELIVERYGOODSID)
  1425. }
  1426. // 品类id
  1427. if r.WRSTANDARDID > 0 {
  1428. sqlId.And("t.WRSTANDARDID", r.WRSTANDARDID)
  1429. }
  1430. // 品牌id
  1431. if r.SPOTGOODSBRANDID > 0 {
  1432. sqlId.And("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID)
  1433. }
  1434. // 仓库id
  1435. sqlId.AndInterEx("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID, len(r.WAREHOUSEINFOID) > 0)
  1436. // 日期范围
  1437. if len(r.BeginDate) > 0 {
  1438. if r.BeginDate == r.EndDate {
  1439. sqlId.And("t.tradedate", r.BeginDate)
  1440. } else if r.EndDate > r.BeginDate {
  1441. sqlId.BiggerOrEq("t.tradedate", r.BeginDate)
  1442. sqlId.LessOrEq("t.tradedate", r.EndDate)
  1443. }
  1444. }
  1445. // 出入库类型
  1446. if len(r.FilterType) > 0 {
  1447. // 原INOUTTYPE 1,2,3,4 转为 新表 5,6,7,8
  1448. r.FilterType = strings.ReplaceAll(r.FilterType, "1", "5")
  1449. r.FilterType = strings.ReplaceAll(r.FilterType, "2", "6")
  1450. r.FilterType = strings.ReplaceAll(r.FilterType, "3", "7")
  1451. r.FilterType = strings.ReplaceAll(r.FilterType, "4", "8")
  1452. if len(r.FilterType) == 1 {
  1453. sqlId.AndInterEx("t.OPERATEAPPLYTYPE", r.FilterType, true)
  1454. } else {
  1455. sqlId.Join(fmt.Sprintf(" and t.OPERATEAPPLYTYPE in(%v)", r.FilterType))
  1456. }
  1457. }
  1458. // 出入库状态
  1459. if len(r.FilterStatus) > 0 {
  1460. sqlId.Join(fmt.Sprintf(" and t.APPLYSTATUS in(%v)", r.FilterStatus))
  1461. }
  1462. // 排序
  1463. sqlId.Join(" order by t.APPLYTIME desc")
  1464. return sqlId.String()
  1465. }
  1466. // GetDataEx 查询库存申请(记录)
  1467. func (r *Ermcp3AreaStockApply) GetDataEx() (interface{}, error) {
  1468. sData := make([]Ermcp3AreaStockApply, 0)
  1469. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1470. if err == nil {
  1471. for i := range sData {
  1472. sData[i].calc()
  1473. }
  1474. }
  1475. return sData, err
  1476. }
  1477. // Ermcp3AreaStockApplySum 合同出入库数量汇总
  1478. type Ermcp3AreaStockApplySum struct {
  1479. INOUTTYPE int32 `json:"inouttype" xorm:"'INOUTTYPE'"` // 出入库类型 - 5:采购入库 6:销售出库 7:生产入库 8:生产出库
  1480. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类id
  1481. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 品牌id
  1482. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1483. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1484. TOTALQTY float64 `json:"totalqty" xorm:"'TOTALQTY'"` // 总数量
  1485. SPOTCONTRACTID string `json:"spotcontractid" xorm:"'relatedid'"` // 关联现货合同ID
  1486. DETAILJSON string `json:"-" xorm:"'DETAILJSON'"` // 出入库数量
  1487. Key string `json:"-"` // 汇总主键
  1488. }
  1489. // Calc
  1490. func (r *Ermcp3AreaStockApplySum) Calc() {
  1491. if len(r.DETAILJSON) > 0 {
  1492. // 解析qty字段
  1493. req := struct {
  1494. QTY float64 `json:"InOutQty"` // 数量
  1495. }{}
  1496. if json.Unmarshal([]byte(r.DETAILJSON), &req) == nil {
  1497. r.TOTALQTY = req.QTY
  1498. }
  1499. }
  1500. r.Key = fmt.Sprintf("%v_%v_%v", r.INOUTTYPE, r.WRSTANDARDID, r.SPOTGOODSBRANDID)
  1501. }
  1502. func (r *Ermcp3AreaStockApplySum) buildSql() string {
  1503. var sqlId utils.SQLVal = "select t.detailjson," +
  1504. " t.operateapplytype INOUTTYPE," +
  1505. " t.wrstandardid," +
  1506. " t.relatedid," +
  1507. " t.spotgoodsbrandid," +
  1508. " gb.dgfactoryitemvalue brandname," +
  1509. " w.wrstandardcode," +
  1510. " w.wrstandardname" +
  1511. " from ermcp_contractoperateapply t" +
  1512. " left join dgfactoryitem gb" +
  1513. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1514. " left join wrstandard w" +
  1515. " on t.wrstandardid = w.wrstandardid" +
  1516. " where t.operateapplytype in (5, 6)"
  1517. sqlId.And("t.relatedid", r.SPOTCONTRACTID)
  1518. return sqlId.String()
  1519. }
  1520. // GetDataEx 获取合同出入库汇总信息
  1521. func (r *Ermcp3AreaStockApplySum) GetDataEx() (interface{}, error) {
  1522. sData := make([]Ermcp3AreaStockApplySum, 0)
  1523. if err := db.GetEngine().SQL(r.buildSql()).Find(&sData); err == nil {
  1524. mSum := map[string]Ermcp3AreaStockApplySum{}
  1525. for i := range sData {
  1526. sData[i].Calc()
  1527. // 按品牌+品类+出入库类型 汇总
  1528. if d, ok := mSum[sData[i].Key]; ok {
  1529. d.TOTALQTY += sData[i].TOTALQTY
  1530. mSum[sData[i].Key] = d
  1531. } else {
  1532. mSum[sData[i].Key] = sData[i]
  1533. }
  1534. }
  1535. if len(mSum) > 0 {
  1536. sData = make([]Ermcp3AreaStockApplySum, 0)
  1537. for _, v := range mSum {
  1538. sData = append(sData, v)
  1539. }
  1540. }
  1541. }
  1542. return sData, nil
  1543. }
  1544. // Ermcp3AreaStock 机构库存表
  1545. type Ermcp3AreaStock struct {
  1546. TODAYBUYINQTY float64 `json:"todaybuyinqty" xorm:"'TODAYBUYINQTY'"` // 今日采购入库量
  1547. TODAYPRODUCEINQTY float64 `json:"todayproduceinqty" xorm:"'TODAYPRODUCEINQTY'"` // 今日生产入库量
  1548. TODAYSELLOUTQTY float64 `json:"todayselloutqty" xorm:"'TODAYSELLOUTQTY'"` // 今日销售出库量
  1549. TODAYPRODUCEOUTQTY float64 `json:"todayproduceoutqty" xorm:"'TODAYPRODUCEOUTQTY'"` // 今日生产出库量
  1550. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  1551. USERID int64 `json:"userid" xorm:"'USERID'" form:"userid" binding:"required"` // 机构ID
  1552. WRSTANDARDID string `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类ID
  1553. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID
  1554. WAREHOUSEINFOID string `json:"warehouseinfoid" xorm:"'WAREHOUSEINFOID'"` // 仓库ID
  1555. ORISTOCK float64 `json:"oristock" xorm:"'ORISTOCK'"` // 期初库存量(昨日量)
  1556. CURSTOCK float64 `json:"curstock" xorm:"'CURSTOCK'"` // 期末库存量(今日量)
  1557. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1558. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1559. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 单位id
  1560. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1561. WAREHOUSENAME string `json:"warehousename" xorm:"'WAREHOUSENAME'"` // 仓库名称
  1562. WAREHOUSECODE string `json:"warehousecode" xorm:"'WAREHOUSECODE'"` // 仓库代码
  1563. WAREHOUSETYPE int32 `json:"warehousetype" xorm:"'WAREHOUSETYPE'"` // 仓库类型 - 1 厂库 2 自有库 3 合作库
  1564. USERNAME string `json:"username"` // 机构名称
  1565. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  1566. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种id
  1567. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  1568. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  1569. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODSUNITID'"` // 现货商品单位id
  1570. }
  1571. func (r *Ermcp3AreaStock) calc() {
  1572. r.USERNAME = mtpcache.GetUserNameByUserId(r.USERID)
  1573. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  1574. }
  1575. func (r *Ermcp3AreaStock) buildSql() string {
  1576. var sqlId utils.SQLVal = "SELECT t.TODAYBUYINQTY," +
  1577. " t.TODAYPRODUCEINQTY," +
  1578. " t.TODAYSELLOUTQTY," +
  1579. " t.TODAYPRODUCEOUTQTY," +
  1580. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1581. " t.USERID," +
  1582. " t.WRSTANDARDID," +
  1583. " t.SPOTGOODSBRANDID," +
  1584. " t.WAREHOUSEINFOID," +
  1585. " t.DELIVERYGOODSID," +
  1586. " t.ORISTOCK," +
  1587. " t.CURSTOCK," +
  1588. " w.wrstandardname," +
  1589. " w.wrstandardcode," +
  1590. " w.unitid," +
  1591. " gb.dgfactoryitemvalue brandname," +
  1592. " h.warehousename," +
  1593. " h.warehousecode," +
  1594. " h.warehousetype," +
  1595. " g.deliverygoodsid," +
  1596. " g.deliverygoodscode," +
  1597. " g.deliverygoodsname," +
  1598. " g.goodsunitid" +
  1599. " FROM ERMCP_AREASTOCK t" +
  1600. " LEFT JOIN WRSTANDARD w" +
  1601. " on t.wrstandardid = w.wrstandardid" +
  1602. " LEFT JOIN dgfactoryitem gb" +
  1603. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1604. " LEFT JOIN deliverygoods g" +
  1605. " on t.deliverygoodsid = g.deliverygoodsid" +
  1606. " LEFT JOIN WAREHOUSEINFO h" +
  1607. " on t.warehouseinfoid = h.autoid" +
  1608. " WHERE 1 = 1"
  1609. sqlId.And("t.USERID", r.USERID)
  1610. sqlId.Join(" order by t.WRSTANDARDID, t.UPDATETIME desc")
  1611. return sqlId.String()
  1612. }
  1613. // GetDataEx 获取机构库存信息
  1614. func (r *Ermcp3AreaStock) GetDataEx() (interface{}, error) {
  1615. sData := make([]Ermcp3AreaStock, 0)
  1616. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1617. if err == nil {
  1618. for i := range sData {
  1619. sData[i].calc()
  1620. }
  1621. }
  1622. return sData, err
  1623. }
  1624. // Ermcp3SpotGoodsPrice 现货市价
  1625. type Ermcp3SpotGoodsPrice struct {
  1626. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货品类ID(通用则为0)
  1627. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID(通用则为0, 不为0则须先有品类ID)
  1628. CURRENCYID int32 `json:"currencyid" xorm:"'CURRENCYID'"` // 报价货币ID
  1629. SPOTGOODSPRICE float64 `json:"spotgoodsprice" xorm:"'SPOTGOODSPRICE'"` // 现货价格
  1630. TRADEDATE string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd)
  1631. OPERATESRC int32 `json:"operatesrc" xorm:"'OPERATESRC'"` // 最后操作来源 - 1:管理端 2:终端
  1632. OPERATEID int64 `json:"operateid" xorm:"'OPERATEID'"` // 最后操作人
  1633. OPERATETIME string `json:"operatetime" xorm:"'OPERATETIME'"` // 最后操作时间
  1634. ISVALID int32 `json:"isvalid" xorm:"'ISVALID'"` // 是否有效 - 0:无效 1:有效
  1635. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构id
  1636. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1637. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1638. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 现货商品单位id
  1639. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1640. GMUNITID int32 `json:"gmunitid" xorm:"'GMUNITID'"` // 品类单位id
  1641. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货商品id
  1642. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货商品代码
  1643. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货商品名称
  1644. OPERATORNAME string `json:"operatorname"` // 操作人名称
  1645. ENUMDICNAME string `json:"enumdicname"` // 现货商品单位名称
  1646. GBENUMDICNAME string `json:"gbenumdicname"` // 品类单位名称
  1647. CURRENCYNAME string `json:"currencyname"` // 币种名称
  1648. }
  1649. func (r *Ermcp3SpotGoodsPrice) calc() {
  1650. if r.OPERATESRC == 1 {
  1651. r.OPERATORNAME = mtpcache.GetSystemmangerLoginCode(r.OPERATEID)
  1652. } else {
  1653. r.OPERATORNAME = mtpcache.GetLoginCodeByLoginId(r.OPERATEID)
  1654. }
  1655. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  1656. r.GBENUMDICNAME = mtpcache.GetEnumDicitemName(r.GMUNITID)
  1657. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  1658. }
  1659. func (r *Ermcp3SpotGoodsPrice) buildSql() string {
  1660. var sqlId utils.SQLVal = "SELECT t.WRSTANDARDID," +
  1661. " t.SPOTGOODSBRANDID," +
  1662. " t.CURRENCYID," +
  1663. " t.SPOTGOODSPRICE," +
  1664. " t.TRADEDATE," +
  1665. " t.OPERATESRC," +
  1666. " t.OPERATEID," +
  1667. " to_char(t.OPERATETIME, 'yyyy-mm-dd hh24:mi:ss') OPERATETIME," +
  1668. " t.ISVALID," +
  1669. " w.areauserid," +
  1670. " w.wrstandardcode," +
  1671. " w.wrstandardname," +
  1672. " w.unitid gmunitid," +
  1673. " gb.dgfactoryitemvalue brandname," +
  1674. " g.deliverygoodsid," +
  1675. " g.deliverygoodscode," +
  1676. " g.deliverygoodsname," +
  1677. " g.goodsunitid unitid" +
  1678. " FROM ERMCP_SPOTGOODSPRICE t" +
  1679. " left join wrstandard w" +
  1680. " on t.wrstandardid = w.wrstandardid" +
  1681. " left join dgfactoryitem gb" +
  1682. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1683. " left join deliverygoods g" +
  1684. " on t.deliverygoodsid = g.deliverygoodsid" +
  1685. " WHERE 1 = 1"
  1686. sqlId.And("w.areauserid", r.AREAUSERID)
  1687. return sqlId.String()
  1688. }
  1689. // GetDataEx 获取现货市价
  1690. func (r *Ermcp3SpotGoodsPrice) GetDataEx() (interface{}, error) {
  1691. sData := make([]Ermcp3SpotGoodsPrice, 0)
  1692. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1693. if err == nil {
  1694. for i := range sData {
  1695. sData[i].calc()
  1696. }
  1697. }
  1698. return sData, err
  1699. }
  1700. // Ermcp3SpotGoodsPriceLog 现货市价操作日志
  1701. type Ermcp3SpotGoodsPriceLog struct {
  1702. LOGID int64 `json:"logid" xorm:"'LOGID'"` // 日志id
  1703. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货品类ID(通用则为0)
  1704. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID(通用则为0, 不为0则须先有品类ID)
  1705. CURRENCYID int64 `json:"currencyid" xorm:"'CURRENCYID'"` // 报价货币ID
  1706. SPOTGOODSPRICE float64 `json:"spotgoodsprice" xorm:"'SPOTGOODSPRICE'"` // 现货价格
  1707. TRADEDATE string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd)
  1708. OPERATESRC int32 `json:"operatesrc" xorm:"'OPERATESRC'"` // 最后操作来源 - 1:管理端 2:终端
  1709. OPERATEID int64 `json:"operateid" xorm:"'OPERATEID'"` // 最后操作人
  1710. OPERATETIME string `json:"operatetime" xorm:"'OPERATETIME'"` // 最后操作时间
  1711. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构id
  1712. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1713. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1714. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 现货商品单位id
  1715. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1716. GMUNITID int32 `json:"gmunitid" xorm:"'GMUNITID'"` // 品类单位id
  1717. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货商品id
  1718. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货商品代码
  1719. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货商品名称
  1720. OPERATORNAME string `json:"operatorname"` // 操作人名称
  1721. ENUMDICNAME string `json:"enumdicname"` // 现货商品单位名称
  1722. GBENUMDICNAME string `json:"gbenumdicname"` // 品类单位名称
  1723. }
  1724. func (r *Ermcp3SpotGoodsPriceLog) calc() {
  1725. if r.OPERATESRC == 1 {
  1726. r.OPERATORNAME = mtpcache.GetSystemmangerLoginCode(r.OPERATEID)
  1727. } else {
  1728. r.OPERATORNAME = mtpcache.GetLoginCodeByLoginId(r.OPERATEID)
  1729. }
  1730. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  1731. r.GBENUMDICNAME = mtpcache.GetEnumDicitemName(r.GMUNITID)
  1732. }
  1733. func (r *Ermcp3SpotGoodsPriceLog) buildSql() string {
  1734. var sqlId utils.SQLVal = "SELECT t.WRSTANDARDID," +
  1735. " t.SPOTGOODSBRANDID," +
  1736. " t.CURRENCYID," +
  1737. " t.SPOTGOODSPRICE," +
  1738. " t.TRADEDATE," +
  1739. " t.OPERATESRC," +
  1740. " t.OPERATEID," +
  1741. " to_char(t.OPERATETIME, 'yyyy-mm-dd hh24:mi:ss') OPERATETIME," +
  1742. " t.LOGID," +
  1743. " w.areauserid," +
  1744. " w.wrstandardcode," +
  1745. " w.wrstandardname," +
  1746. " w.unitid gmunitid," +
  1747. " gb.dgfactoryitemvalue brandname," +
  1748. " g.goodsunitid unitid ," +
  1749. " g.deliverygoodsid," +
  1750. " g.deliverygoodscode," +
  1751. " g.deliverygoodsname" +
  1752. " FROM ERMCP_SPOTGOODSPRICELOG t" +
  1753. " left join wrstandard w" +
  1754. " on t.wrstandardid = w.wrstandardid" +
  1755. " left join dgfactoryitem gb" +
  1756. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1757. " left join deliverygoods g" +
  1758. " on t.deliverygoodsid = g.deliverygoodsid" +
  1759. " WHERE 1 = 1"
  1760. sqlId.And("w.areauserid", r.AREAUSERID)
  1761. sqlId.And("t.deliverygoodsid", r.DELIVERYGOODSID)
  1762. sqlId.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  1763. sqlId.AndEx("t.wrstandardid", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  1764. return sqlId.String()
  1765. }
  1766. // GetDataEx 获取现货市价信息日志
  1767. func (r *Ermcp3SpotGoodsPriceLog) GetDataEx() (interface{}, error) {
  1768. sData := make([]Ermcp3SpotGoodsPriceLog, 0)
  1769. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1770. if err == nil {
  1771. for i := range sData {
  1772. sData[i].calc()
  1773. }
  1774. }
  1775. return sData, err
  1776. }
  1777. // Ermcp3HedgePlan 套保计划表结构
  1778. type Ermcp3HedgePlan struct {
  1779. Areauserid int64 `json:"areauserid" xorm:"'Areauserid'"` // 用户ID
  1780. Hedgeplanid string `json:"hedgeplanid" xorm:"'Hedgeplanid'"` // 套保计划ID(601+Unix秒时间戳(10位)+xxxxxx)
  1781. Hedgeplanno string `json:"hedgeplanno" xorm:"'Hedgeplanno'"` // 套保计划编号(名称)
  1782. Contracttype int `json:"contracttype" xorm:"'Contracttype'"` // 计划类型 - 1:采购 -1:销售
  1783. Deliverygoodsid int `json:"deliverygoodsid" xorm:"'Deliverygoodsid'"` // 现货品种ID
  1784. Deliverygoodsname string `json:"deliverygoodsname" xorm:"'Deliverygoodsname'"` // 现货品种名称
  1785. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  1786. Producttype int `json:"producttype" xorm:"'Producttype'"` // 产品类型 - 1:标准仓单 2:等标 3:非标
  1787. Spotgoodsdesc string `json:"spotgoodsdesc" xorm:"'Spotgoodsdesc'"` // 商品型号
  1788. Planqty float64 `json:"planqty" xorm:"'Planqty'"` // 计划数量
  1789. Convertfactor float64 `json:"convertfactor" xorm:"'Convertfactor'"` // 标仓系数
  1790. Plantime string `json:"plantime" xorm:"'Plantime'"` // 计划时间
  1791. Hedgeplanstatus int `json:"hedgeplanstatus" xorm:"'Hedgeplanstatus'"` // 套保计划状态 - 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
  1792. Remark string `json:"remark" xorm:"'Remark'"` // 备注
  1793. CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  1794. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  1795. AUDITTIME string `json:"audittime" xorm:"'AUDITTIME'"` // 审核时间
  1796. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 单位id(现货商品)
  1797. ENUMDICNAME string `json:"enumdicname"` // 单位名称(现货商品)
  1798. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类id
  1799. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1800. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1801. ACCOUNTID int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 资金账号
  1802. TAACCOUNTNAME string `json:"taaccountname" xorm:"'TAACCOUNTNAME'"` // 资金账号名称
  1803. TRADEUSERID int64 `json:"tradeuserid" xorm:"'TRADEUSERID'"` // 交易用户id
  1804. CURRENCYID int32 `json:"currencyid" xorm:"'CURRENCYID'"` // 币种id
  1805. WRUNITID int32 `json:"wrunitid" xorm:"'WRUNITID'"` // 品类单位id
  1806. AUDITID int64 `json:"auditid" xorm:"'auditid'"` // 审核id
  1807. AUDITSRC int32 `json:"auditsrc" xorm:"'AUDITSRC'"` // 审核来源 1-管理端 2-终端
  1808. AUDITREMARK string `json:"auditremark" xorm:"'auditremark'"` // 审核备注
  1809. AUDITNAME string `json:"auditname" xorm:"'AUDITNAME'"` // 审核人名称
  1810. VATRATE float64 `json:"vatrate" xorm:"'VATRATE'"` // 增值税率
  1811. WRUNITIDNAME string `json:"wrunitidame"` // 品类单位名称
  1812. TRADEUSERNAME string `json:"tradeusername"` // 交易用户名称
  1813. CURRENCYNAME string `json:"currencyname"` // 币种名称
  1814. UserType int32 `json:"-"` // 过滤条件, 用户类型
  1815. }
  1816. func (r *Ermcp3HedgePlan) calc() {
  1817. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  1818. r.TRADEUSERNAME = mtpcache.GetUserNameByUserId(r.TRADEUSERID)
  1819. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  1820. if r.WRUNITID > 0 {
  1821. r.WRUNITIDNAME = mtpcache.GetEnumDicitemName(r.WRUNITID)
  1822. }
  1823. if r.AUDITSRC == 1 {
  1824. if r.AUDITNAME == "" && r.AUDITID > 0 {
  1825. r.AUDITNAME = mtpcache.GetSystemmangerLoginCode(r.AUDITID)
  1826. }
  1827. }
  1828. }
  1829. func (r *Ermcp3HedgePlan) buildSql(status string) string {
  1830. str := "select to_char(t.Hedgeplanid) Hedgeplanid," +
  1831. " t.Hedgeplanno," +
  1832. " t.Contracttype," +
  1833. " t.Deliverygoodsid," +
  1834. " g.Deliverygoodsname," +
  1835. " g.deliverygoodscode," +
  1836. " g.goodsunitid unitid," +
  1837. " t.Producttype," +
  1838. " t.Spotgoodsdesc," +
  1839. " t.Planqty," +
  1840. " t.Convertfactor," +
  1841. " to_char(t.Plantime, 'yyyy-mm-dd hh24:mi:ss') Plantime," +
  1842. " to_char(t.createtime, 'yyyy-mm-dd hh24:mi:ss') createtime," +
  1843. " to_char(t.updatetime, 'yyyy-mm-dd hh24:mi:ss') updatetime," +
  1844. " to_char(t.audittime, 'yyyy-mm-dd hh24:mi:ss') audittime," +
  1845. " t.Hedgeplanstatus," +
  1846. " t.Remark," +
  1847. " t.wrstandardid," +
  1848. " w.wrstandardcode," +
  1849. " w.wrstandardname," +
  1850. " w.unitid wrunitid," +
  1851. " w.vatrate," +
  1852. " t.accountid," +
  1853. " t.tradeuserid," +
  1854. " t.currencyid," +
  1855. " t.auditid," +
  1856. " t.auditsrc," +
  1857. " t.auditremark," +
  1858. " ta.accountname taaccountname," +
  1859. " l.accountname auditname" +
  1860. " from ermcp_hedgeplan t" +
  1861. " left join deliverygoods g" +
  1862. " on t.deliverygoodsid = g.deliverygoodsid" +
  1863. " left join wrstandard w" +
  1864. " on t.wrstandardid = w.wrstandardid" +
  1865. " left join taaccount ta" +
  1866. " on t.accountid = ta.accountid" +
  1867. " left join loginaccount l on t.auditid=l.loginid" +
  1868. " where t.hedgeplanstatus in (%v)"
  1869. if r.UserType == 0 {
  1870. // 终端没传值, 则自己去找
  1871. userId := mtpcache.GetAreaUserId(r.Areauserid, 0)
  1872. if userId == r.Areauserid {
  1873. r.UserType = 2
  1874. } else {
  1875. r.UserType = 7
  1876. }
  1877. }
  1878. if r.UserType == 2 {
  1879. str += " and t.areauserid = %v"
  1880. } else if r.UserType == 7 {
  1881. // 企业成员用tradeuserid匹配
  1882. str += " and t.tradeuserid = %v"
  1883. } else {
  1884. str += " and t.areauserid = %v"
  1885. }
  1886. if status == "2" {
  1887. // 执行中状态,按审核时间倒序
  1888. str += " order by t.audittime desc"
  1889. } else {
  1890. str += " order by t.updatetime desc"
  1891. }
  1892. return fmt.Sprintf(str, status, r.Areauserid)
  1893. }
  1894. // GetData 从数据库中查询套保计划数据
  1895. func (r *Ermcp3HedgePlan) GetData(status string) ([]Ermcp3HedgePlan, error) {
  1896. e := db.GetEngine()
  1897. sData := make([]Ermcp3HedgePlan, 0)
  1898. if err := e.SQL(r.buildSql(status)).Find(&sData); err != nil {
  1899. logger.GetLogger().Errorf("query hedgeplan:%v", err)
  1900. return sData, err
  1901. }
  1902. for i := range sData {
  1903. sData[i].calc()
  1904. }
  1905. return sData, nil
  1906. }
  1907. // Ermcp3GoodsGroup 商品组
  1908. type Ermcp3GoodsGroup struct {
  1909. GOODSGROUPID int32 `json:"goodsgroupid" xorm:"'GOODSGROUPID'"` // 商品组ID(自增ID)
  1910. GOODSGROUPNAME string `json:"goodsgroupname" xorm:"'GOODSGROUPNAME'"` // 商品组名称
  1911. GOODSGROUPSTATUS int32 `json:"goodsgroupstatus" xorm:"'GOODSGROUPSTATUS'"` // 商品组状态 - 1:正常 2:注销
  1912. MARKETID int32 `json:"marketid" xorm:"'MARKETID'"` // 所属市场ID
  1913. CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  1914. CREATORID int32 `json:"creatorid" xorm:"'CREATORID'"` // 创建人
  1915. MODIFYTIME string `json:"modifytime" xorm:"'MODIFYTIME'"` // 修改时间
  1916. MODIFIERID int32 `json:"modifierid" xorm:"'MODIFIERID'"` // 修改人
  1917. QUOTESOURCEGROUPID int64 `json:"quotesourcegroupid" xorm:"'QUOTESOURCEGROUPID'"` // 所属行情源分组ID[参考行情市场用\通道交易]
  1918. OUTERGROUPCODE string `json:"outergroupcode" xorm:"'OUTERGROUPCODE'"` // 外部商品组代码[通道交易]
  1919. CANSHORT int32 `json:"canshort" xorm:"'CANSHORT'"` // 是否允许做空[通道交易] - 0:不能做空 1:可以做空
  1920. EXEXCHANGEID int64 `json:"exexchangeid" xorm:"'EXEXCHANGEID'"` // 外部交易所ID[通道交易]
  1921. CURRENCYID int32 `json:"currencyid" xorm:"'CURRENCYID'"` // 报价货币ID
  1922. GOODUNITID int32 `json:"goodunitid" xorm:"'GOODUNITID'"` // 报价单位ID
  1923. AGREEUNIT float32 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约单位
  1924. DECIMALPLACE int32 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 报价小数位
  1925. QUOTEMINUNIT int32 `json:"quoteminunit" xorm:"'QUOTEMINUNIT'"` // 行情最小变动单位 [整数,报价小数位一起使用]
  1926. INNERDEALMODE int32 `json:"innerdealmode" xorm:"'INNERDEALMODE'"` // 内部成交方式[通道交易] - 1:净头寸 2:开平 3:平今
  1927. OUTERDEALMODE int32 `json:"outerdealmode" xorm:"'OUTERDEALMODE'"` // 外部成交方式[通道交易]- 1:净头寸 2:开平 3:平今
  1928. SYNCGOODSQTY int32 `json:"syncgoodsqty" xorm:"'SYNCGOODSQTY'"` // 同步合约数[通道交易-投资管理用] - 0表示不限
  1929. CLOSEPRICEMODE int32 `json:"closepricemode" xorm:"'CLOSEPRICEMODE'"` // 强平价格方式 - 1:市价 2:最新价 3:涨跌停(未实现)
  1930. GROUPCATEGROYID int32 `json:"groupcategroyid" xorm:"'GROUPCATEGROYID'"` // 品种分类ID
  1931. EXEXCHANGECODE string `json:"exexchangecode" xorm:"'EXEXCHANGECODE'"` // 交易所代码
  1932. EXEXCHANGENAME string `json:"exexchangename" xorm:"'EXEXCHANGENAME'"` // 交易所名称
  1933. EXCHANGEFULLNAME string `json:"exchangefullname" xorm:"'EXCHANGEFULLNAME'"` // 交易所全称
  1934. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  1935. AreaUserId int64 `json:"areauserid" form:"areauserid" binding:"required"` // 所属机构id(套保品是有areauserid的,当要排除已配置商品组时需要areauserid过滤)
  1936. ExcludeCfg int32 `json:"excludecfg" form:"excludecfg"` // 排除套保品已配置期货分类下的商品组(但显示已配置商品组) 1-排除
  1937. }
  1938. func (r *Ermcp3GoodsGroup) calc() {
  1939. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.GOODUNITID)
  1940. }
  1941. func (r *Ermcp3GoodsGroup) buildSql() string {
  1942. var sqlId utils.SQLVal = "SELECT t.GOODSGROUPID," +
  1943. " t.GOODSGROUPNAME," +
  1944. " t.GOODSGROUPSTATUS," +
  1945. " t.MARKETID," +
  1946. " to_char(t.CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME," +
  1947. " t.CREATORID," +
  1948. " to_char(t.MODIFYTIME, 'yyyy-mm-dd hh24:mi:ss') MODIFYTIME," +
  1949. " t.MODIFIERID," +
  1950. " t.QUOTESOURCEGROUPID," +
  1951. " t.OUTERGROUPCODE," +
  1952. " t.CANSHORT," +
  1953. " t.EXEXCHANGEID," +
  1954. " t.CURRENCYID," +
  1955. " t.GOODUNITID," +
  1956. " t.AGREEUNIT," +
  1957. " t.DECIMALPLACE," +
  1958. " t.QUOTEMINUNIT," +
  1959. " t.INNERDEALMODE," +
  1960. " t.OUTERDEALMODE," +
  1961. " t.SYNCGOODSQTY," +
  1962. " t.CLOSEPRICEMODE," +
  1963. " t.GROUPCATEGROYID," +
  1964. " e.exexchangecode," +
  1965. " e.exexchangename," +
  1966. " e.exchangefullname" +
  1967. " FROM GOODSGROUP t" +
  1968. " LEFT JOIN market m" +
  1969. " on t.marketid = m.marketid" +
  1970. " LEFT JOIN externalexchange e on t.exexchangeid=e.autoid" +
  1971. " WHERE 1 = 1" +
  1972. " and m.trademode = 15"
  1973. if r.ExcludeCfg == 1 {
  1974. str := "select t.goodsgroupid" +
  1975. " from ERMS_MiddleGoods t" +
  1976. " where t.isvalid=1 and t.areauserid = %v" +
  1977. " union all " +
  1978. "select goodsgroupid" +
  1979. " from goodsgroup t" +
  1980. " where t.groupcategroyid not in" +
  1981. " (select distinct g.groupcategroyid" +
  1982. " from erms_middlegoods t" +
  1983. " left join goodsgroup g" +
  1984. " on t.goodsgroupid = g.goodsgroupid" +
  1985. " where t.isvalid=1 and t.areauserid = %v)"
  1986. str = fmt.Sprintf(str, r.AreaUserId, r.AreaUserId)
  1987. sqlId.Join(fmt.Sprintf(" and t.goodsgroupid in(%v)", str))
  1988. }
  1989. return sqlId.String()
  1990. }
  1991. // GetData
  1992. func (r *Ermcp3GoodsGroup) GetData() ([]Ermcp3GoodsGroup, error) {
  1993. sData := make([]Ermcp3GoodsGroup, 0)
  1994. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1995. for i := range sData {
  1996. sData[i].calc()
  1997. }
  1998. return sData, err
  1999. }
  2000. // GetDataEx 商品组信息
  2001. func (r *Ermcp3GoodsGroup) GetDataEx() (interface{}, error) {
  2002. return r.GetData()
  2003. }
  2004. // Ermcp3RootUserAccount 顶级用户
  2005. type Ermcp3RootUserAccount struct {
  2006. ROOTUSERID int64 `json:"rootuserid" xorm:"ROOTUSERID"` // 根用户ID
  2007. CANRECOMMEND int32 `json:"canrecommend" xorm:"CANRECOMMEND"` // 是否可推荐 - 0:不可 1;可
  2008. USERID int64 `json:"userid" xorm:"USERID"` // 用户ID
  2009. USERTYPE int32 `json:"usertype" xorm:"USERTYPE"` // 账户类型 - 1:交易所 2:机构 3:会员子机构 4:经纪人 5:投资者 6:客户 7:企业成员(云平台)
  2010. MODIFYSTATUS int32 `json:"modifystatus" xorm:"MODIFYSTATUS"` // 变更状态 1 未变更 2 变更中 3 变更待审核 4 变更待复核(投资者)
  2011. ACCOUNTNAME string `json:"accountname" xorm:"ACCOUNTNAME"` // 账户名称(机构名称)
  2012. PARENTUSERID int64 `json:"parentuserid" xorm:"PARENTUSERID"` // 所属机构ID
  2013. BROKER int64 `json:"broker" xorm:"BROKER"` // 所属经纪人ID
  2014. SUBAREALEVELPATH string `json:"subarealevelpath" xorm:"SUBAREALEVELPATH"` // 子机构层级路径(逗号分隔,首尾加逗号)
  2015. REFEREEUSERID int64 `json:"refereeuserid" xorm:"REFEREEUSERID"` // 推荐人ID
  2016. MEMBERUSERID int64 `json:"memberuserid" xorm:"MEMBERUSERID"` // 所属会员ID
  2017. ACCOUNTSTATUS int32 `json:"accountstatus" xorm:"ACCOUNTSTATUS"` // 账户状态 - 1:待激活 2:待审核 3:待复审 4:正常 5:审核拒绝 6:停用(注销) 7:注销(删除)
  2018. RECKONACCOUNTID int64 `json:"reckonaccountid" xorm:"RECKONACCOUNTID"` // 默认结算资金账号ID(机构分润使用) 作废
  2019. CREATETIME string `json:"createtime" xorm:"CREATETIME"` // 创建时间
  2020. CREATORID int64 `json:"creatorid" xorm:"CREATORID"` // 创建人
  2021. AUDITTIME string `json:"audittime" xorm:"AUDITTIME"` // 审核时间
  2022. AUDITUSERID int64 `json:"audituserid" xorm:"AUDITUSERID"` // 审核人
  2023. AUDITREMARK string `json:"auditremark" xorm:"AUDITREMARK"` // 审核备注
  2024. CANCELTIME string `json:"canceltime" xorm:"CANCELTIME"` // 销户时间
  2025. CANCELUSERID int64 `json:"canceluserid" xorm:"CANCELUSERID"` // 销户人
  2026. MODIFYTIME string `json:"modifytime" xorm:"MODIFYTIME"` // 修改时间
  2027. MODIFIERID int64 `json:"modifierid" xorm:"MODIFIERID"` // 修改人
  2028. MODIFYREMARK string `json:"modifyremark" xorm:"MODIFYREMARK"` // 变更备注
  2029. MAXINVESTORNUM int64 `json:"maxinvestornum" xorm:"MAXINVESTORNUM"` // 最大用户数(经纪会员下投资者个数)
  2030. HASAUTH int32 `json:"hasauth" xorm:"HASAUTH"` // 是否已实名认证 - 0:未认证 1:已认证 2:已提交(待审核) 3:已拒绝
  2031. REFERCOUNT int32 `json:"refercount" xorm:"REFERCOUNT"` // 推荐总人数
  2032. REFERNUM string `json:"refernum" xorm:"REFERNUM"` // 推荐码
  2033. ISANONYMOUS int32 `json:"isanonymous" xorm:"ISANONYMOUS"` // 是否匿名下单 - 0:否 1:是
  2034. PARENTTOPUSER string `json:"parenttopuser" xorm:"PARENTTOPUSER"` // 上级顶级机构 [092=0,1时,默认为1, 092=2时若自已为顶级,则填入自己,自己不为顶级,填入ParentUserID的"ParentTopUser"]
  2035. SUBACCOUNTLEVEL int32 `json:"subaccountlevel" xorm:"SUBACCOUNTLEVEL"` // 子账户层数
  2036. APPLYSRC int32 `json:"applysrc" xorm:"APPLYSRC"` // 申请来源 - 1:管理端 2:终端
  2037. AUDITSRC int32 `json:"auditsrc" xorm:"AUDITSRC"` // 审核来源 - 1:管理端 2:终端
  2038. }
  2039. func (r *Ermcp3RootUserAccount) calc() {
  2040. }
  2041. func (r *Ermcp3RootUserAccount) buildSql() string {
  2042. var sqlId utils.SQLVal = "SELECT t.ROOTUSERID," +
  2043. " t.CANRECOMMEND," +
  2044. " t.USERID," +
  2045. " t.USERTYPE," +
  2046. " t.MODIFYSTATUS," +
  2047. " t.ACCOUNTNAME," +
  2048. " t.PARENTUSERID," +
  2049. " t.BROKER," +
  2050. " t.SUBAREALEVELPATH," +
  2051. " t.REFEREEUSERID," +
  2052. " t.MEMBERUSERID," +
  2053. " t.ACCOUNTSTATUS," +
  2054. " t.RECKONACCOUNTID," +
  2055. " to_char(t.CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME," +
  2056. " t.CREATORID," +
  2057. " to_char(t.AUDITTIME, 'yyyy-mm-dd hh24:mi:ss') AUDITTIME," +
  2058. " t.AUDITUSERID," +
  2059. " t.AUDITREMARK," +
  2060. " to_char(t.CANCELTIME, 'yyyy-mm-dd hh24:mi:ss') CANCELTIME," +
  2061. " t.CANCELUSERID," +
  2062. " to_char(t.MODIFYTIME, 'yyyy-mm-dd hh24:mi:ss') MODIFYTIME," +
  2063. " t.MODIFIERID," +
  2064. " t.MODIFYREMARK," +
  2065. " t.MAXINVESTORNUM," +
  2066. " t.HASAUTH," +
  2067. " t.REFERCOUNT," +
  2068. " t.REFERNUM," +
  2069. " t.ISANONYMOUS," +
  2070. " t.PARENTTOPUSER," +
  2071. " t.SUBACCOUNTLEVEL," +
  2072. " t.APPLYSRC," +
  2073. " t.AUDITSRC" +
  2074. " FROM USERACCOUNT t" +
  2075. " WHERE 1 = 1 and t.userid in(select rootuserid from useraccount where userid=%v)"
  2076. sqlId.FormatParam(r.USERID)
  2077. return sqlId.String()
  2078. }
  2079. // GetDataEx 获取顶级用户
  2080. func (r *Ermcp3RootUserAccount) GetDataEx() (interface{}, error) {
  2081. sData := make([]Ermcp3RootUserAccount, 0)
  2082. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2083. for i := range sData {
  2084. sData[i].calc()
  2085. }
  2086. return sData, err
  2087. }
  2088. // Ermcp3PaAreaSubject 交易主体
  2089. type Ermcp3PaAreaSubject struct {
  2090. SUBJECTID int64 `json:"subjectid" xorm:"SUBJECTID" form:"subjectid"` // 主体ID(SEQ_ERMCP_PA_AREASUBJECT)
  2091. SUBJECTTYPE int32 `json:"subjecttype" xorm:"SUBJECTTYPE"` // 主体类型 - 1:交易主体 2:业务部门
  2092. SUBJECTNAME string `json:"subjectname" xorm:"SUBJECTNAME"` // 主体名称
  2093. PARENTSUBJECTID string `json:"parentsubjectid" xorm:"PARENTSUBJECTID" form:"parentsubjectid"` // 上级主体ID
  2094. AREAUSERID int64 `json:"areauserid" xorm:"AREAUSERID" form:"userid"` // 所属机构
  2095. CREATETIME string `json:"createtime" xorm:"CREATETIME"` // 创建时间
  2096. CREATORSRC int32 `json:"creatorsrc" xorm:"CREATORSRC"` // 创建人来源 - 1:管理端 2:终端 3:交易
  2097. CREATORID string `json:"creatorid" xorm:"CREATORID" form:"creatorid"` // 创建人ID
  2098. MODIFIERSRC int32 `json:"modifiersrc" xorm:"MODIFIERSRC"` // 修改人来源 - 1:管理端 2:终端
  2099. MODIFIERID string `json:"modifierid" xorm:"MODIFIERID" form:"modifierid"` // 修改人
  2100. MODIFYTIME string `json:"modifytime" xorm:"MODIFYTIME"` // 修改时间
  2101. SUBJECTSTATUS int32 `json:"subjectstatus" xorm:"SUBJECTSTATUS" form:"subjectstatus"` // 主体状态 - 1:正常 2: 停用
  2102. CANMODIFY int32 `json:"canmodify" xorm:"CANMODIFY"` // 是否可修改 - 0:否 1:是
  2103. REMARK string `json:"remark" xorm:"REMARK"` // 备注
  2104. }
  2105. func (r *Ermcp3PaAreaSubject) calc() {
  2106. }
  2107. func (r *Ermcp3PaAreaSubject) buildSql() string {
  2108. var sqlId utils.SQLVal = `
  2109. SELECT t.SUBJECTID,
  2110. t.SUBJECTTYPE,
  2111. t.SUBJECTNAME,
  2112. t.PARENTSUBJECTID,
  2113. t.AREAUSERID,
  2114. to_char(t.CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME,
  2115. t.CREATORSRC,
  2116. t.CREATORID,
  2117. t.MODIFIERSRC,
  2118. t.MODIFIERID,
  2119. to_char(t.MODIFYTIME, 'yyyy-mm-dd hh24:mi:ss') MODIFYTIME,
  2120. t.SUBJECTSTATUS,
  2121. t.CANMODIFY,
  2122. t.REMARK
  2123. FROM ERMCP_PA_AREASUBJECT t
  2124. WHERE 1 = 1
  2125. `
  2126. areaUserId := mtpcache.GetAreaUserId(r.AREAUSERID, 0)
  2127. sqlId.And("t.AREAUSERID", areaUserId)
  2128. sqlId.AndEx("t.SUBJECTID", r.SUBJECTID, r.SUBJECTID > 0)
  2129. sqlId.AndEx("t.SUBJECTTYPE", r.SUBJECTTYPE, r.SUBJECTTYPE > 0)
  2130. sqlId.AndEx("t.SUBJECTSTATUS", r.SUBJECTSTATUS, r.SUBJECTSTATUS > 0)
  2131. return sqlId.String()
  2132. }
  2133. // GetDataEx 获取交易主体
  2134. func (r *Ermcp3PaAreaSubject) GetDataEx() (interface{}, error) {
  2135. sData := make([]Ermcp3PaAreaSubject, 0)
  2136. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2137. for i := range sData {
  2138. sData[i].calc()
  2139. }
  2140. return sData, err
  2141. }
  2142. // Ermcp3PaAreaAuditCfg 审核设置
  2143. type Ermcp3PaAreaAuditCfg struct {
  2144. AREAUSERID int64 `json:"areauserid" xorm:"AREAUSERID" form:"userid"` // 用户ID
  2145. CUSTOMERAUDITFLAG int32 `json:"customerauditflag" xorm:"CUSTOMERAUDITFLAG"` // 客户资料是否需要审核 - 0:不需审核 1:需要审核
  2146. SPOTCONTRACTAUDITFLAG int32 `json:"spotcontractauditflag" xorm:"SPOTCONTRACTAUDITFLAG"` // 现货合同是否需要审核 - 0:不需审核 1:需要审核
  2147. POINTPRICEAUDITFLAG int32 `json:"pointpriceauditflag" xorm:"POINTPRICEAUDITFLAG"` // 点价是否需要审核 - 0:不需审核 1:需要审核
  2148. DELIVERYAUDITFLAG int32 `json:"deliveryauditflag" xorm:"DELIVERYAUDITFLAG"` // 交收是否需要审核 - 0:不需审核 1:需要审核
  2149. MONEYAUDITFLAG int32 `json:"moneyauditflag" xorm:"MONEYAUDITFLAG"` // 款项是否需要审核 - 0:不需审核 1:需要审核
  2150. INVOICEAUDITFLAG int32 `json:"invoiceauditflag" xorm:"INVOICEAUDITFLAG"` // 发票是否需要审核 - 0:不需审核 1:需要审核
  2151. BUYINAUDITFLAG int32 `json:"buyinauditflag" xorm:"BUYINAUDITFLAG"` // 采购入库是否需要审核 - 0:不需审核 1:需要审核
  2152. SELLOUTAUDITFLAG int32 `json:"selloutauditflag" xorm:"SELLOUTAUDITFLAG"` // 销售出库是否需要审核 - 0:不需审核 1:需要审核
  2153. PRODUCEINAUDITFLAG int32 `json:"produceinauditflag" xorm:"PRODUCEINAUDITFLAG"` // 生产入库是否需要审核 - 0:不需审核 1:需要审核
  2154. PRODUCEOUTAUDITFLAG int32 `json:"produceoutauditflag" xorm:"PRODUCEOUTAUDITFLAG"` // 生产出库是否需要审核 - 0:不需审核 1:需要审核
  2155. OPERATORSRC int32 `json:"operatorsrc" xorm:"OPERATORSRC"` // 操作来源 - 1:管理端 2:终端
  2156. OPERATORID int64 `json:"operatorid" xorm:"OPERATORID" form:"operatorid"` // 操作人
  2157. UPDATETIME string `json:"updatetime" xorm:"UPDATETIME"` // 更新时间
  2158. }
  2159. func (r *Ermcp3PaAreaAuditCfg) calc() {
  2160. }
  2161. func (r *Ermcp3PaAreaAuditCfg) buildSql() string {
  2162. var sqlId utils.SQLVal = `
  2163. SELECT t.AREAUSERID,
  2164. t.CUSTOMERAUDITFLAG,
  2165. t.SPOTCONTRACTAUDITFLAG,
  2166. t.POINTPRICEAUDITFLAG,
  2167. t.DELIVERYAUDITFLAG,
  2168. t.MONEYAUDITFLAG,
  2169. t.INVOICEAUDITFLAG,
  2170. t.BUYINAUDITFLAG,
  2171. t.SELLOUTAUDITFLAG,
  2172. t.PRODUCEINAUDITFLAG,
  2173. t.PRODUCEOUTAUDITFLAG,
  2174. t.OPERATORSRC,
  2175. t.OPERATORID,
  2176. to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME
  2177. FROM ERMCP_PA_AREAAUDITCONFIG t
  2178. WHERE 1 = 1
  2179. `
  2180. areaUserId := mtpcache.GetAreaUserId(r.AREAUSERID, 0)
  2181. sqlId.And("t.AREAUSERID", areaUserId)
  2182. return sqlId.String()
  2183. }
  2184. // GetDataEx 获取审核设置
  2185. func (r *Ermcp3PaAreaAuditCfg) GetDataEx() (interface{}, error) {
  2186. sData := make([]Ermcp3PaAreaAuditCfg, 0)
  2187. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2188. for i := range sData {
  2189. sData[i].calc()
  2190. }
  2191. return sData, err
  2192. }
  2193. // XHDeliveryGoods 商品信息-现货品种
  2194. type XHDeliveryGoods struct {
  2195. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'" form:"areauserid"` // 所属机构
  2196. DELIVERYGOODSID int64 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'" form:"deliverygoodsid"` // 现货品种ID(SEQ_DELIVERYGOODS)
  2197. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  2198. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  2199. ISVALID int32 `json:"isvalid" xorm:"'ISVALID'"` // 是否有效 - 0:无效 1:有效
  2200. DGSTATUS int32 `json:"dgstatus" xorm:"'DGSTATUS'"` // 品种状态 - 作废 - 0:未激活 1:正常
  2201. REMARK string `json:"remark" xorm:"'REMARK'"` // 备注
  2202. UNITID int64 `json:"unitid" xorm:"'UNITID'"` // 单位id(目前看,数据库中是空值)
  2203. WDLST []XHWrstandard `json:"wdlst"` // 品类(现货商品)列表
  2204. BDLST []XHBRAND `json:"bdlst"` // 品牌列表
  2205. }
  2206. func (r *XHDeliveryGoods) calc() {
  2207. if r.WDLST == nil {
  2208. r.WDLST = make([]XHWrstandard, 0)
  2209. r.BDLST = make([]XHBRAND, 0)
  2210. }
  2211. }
  2212. func (r *XHDeliveryGoods) buildSql() string {
  2213. var sqlId utils.SQLVal = `
  2214. SELECT t.AREAUSERID,
  2215. t.DELIVERYGOODSID,
  2216. t.DELIVERYGOODSCODE,
  2217. t.DELIVERYGOODSNAME,
  2218. t.ISVALID,
  2219. t.DGSTATUS,
  2220. t.GOODSUNITID UNITID,
  2221. t.REMARK
  2222. FROM DELIVERYGOODS t
  2223. WHERE 1 = 1
  2224. `
  2225. sqlId.AndEx("t.AREAUSERID", r.AREAUSERID, r.AREAUSERID > 0)
  2226. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  2227. return sqlId.String()
  2228. }
  2229. // GetDataEx 获取商品信息-现货品种
  2230. func (r *XHDeliveryGoods) GetDataEx() (interface{}, error) {
  2231. sData := make([]XHDeliveryGoods, 0)
  2232. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2233. for i := range sData {
  2234. sData[i].calc()
  2235. }
  2236. if len(sData) > 0 {
  2237. // 获取品类列表
  2238. m1 := XHWrstandard{AREAUSERID: r.AREAUSERID, DELIVERYGOODSID: r.DELIVERYGOODSID}
  2239. if d1, err := m1.GetDataEx(); err == nil {
  2240. lstWd := d1.([]XHWrstandard)
  2241. for i := range sData {
  2242. sData[i].addWdlst(lstWd)
  2243. }
  2244. }
  2245. // 获取品牌列表
  2246. m2 := XHBRAND{DELIVERYGOODSID: r.DELIVERYGOODSID, AREAUSERID: r.AREAUSERID}
  2247. if d2, err := m2.GetDataEx(); err == nil {
  2248. lstBrand := d2.([]XHBRAND)
  2249. for i := range sData {
  2250. sData[i].addBrandlst(lstBrand)
  2251. }
  2252. }
  2253. }
  2254. return sData, err
  2255. }
  2256. // 增加品类列表
  2257. func (r *XHDeliveryGoods) addWdlst(lstWd []XHWrstandard) {
  2258. for i := range lstWd {
  2259. if lstWd[i].DELIVERYGOODSID == r.DELIVERYGOODSID {
  2260. r.WDLST = append(r.WDLST, lstWd[i])
  2261. }
  2262. }
  2263. }
  2264. // 增加品牌列表
  2265. func (r *XHDeliveryGoods) addBrandlst(lstBrand []XHBRAND) {
  2266. for i := range lstBrand {
  2267. if lstBrand[i].DELIVERYGOODSID == r.DELIVERYGOODSID {
  2268. r.BDLST = append(r.BDLST, lstBrand[i])
  2269. }
  2270. }
  2271. }
  2272. // XHBRAND 现货品种/品牌
  2273. type XHBRAND struct {
  2274. BRANDID int64 `json:"brandid"` // 品牌id
  2275. BRANDNAME string `json:"brandname"` // 品牌名称
  2276. DELIVERYGOODSID int64 `json:"-"` // 品种ID
  2277. AREAUSERID int64 `json:"-"` // 用户id
  2278. }
  2279. // GetDataEx 获取现货品种/品牌
  2280. func (r *XHBRAND) GetDataEx() (interface{}, error) {
  2281. sData := make([]XHBRAND, 0)
  2282. m := Ermcp3Brand{AREAUSERID: r.AREAUSERID, DELIVERYGOODSID: r.DELIVERYGOODSID}
  2283. d, err := m.GetData()
  2284. if err == nil {
  2285. for _, v := range d {
  2286. sData = append(sData, XHBRAND{DELIVERYGOODSID: v.DELIVERYGOODSID, BRANDID: v.BRANDID, BRANDNAME: v.BRANDNAME})
  2287. }
  2288. }
  2289. return sData, err
  2290. }
  2291. // XHWrstandard 现货品种/品类
  2292. type XHWrstandard struct {
  2293. AREAUSERID int64 `json:"-" xorm:"'AREAUSERID'" form:"areauserid"` // 所属机构
  2294. DELIVERYGOODSID int64 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'" form:"deliverygoodsid"` // 品种ID
  2295. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID(SEQ_WRSTANDARD)
  2296. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货商品代码
  2297. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 现货商品名称
  2298. VATRATE float64 `json:"vatrate" xorm:"'VATRATE'"` // 增值税率
  2299. ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  2300. UNITID int64 `json:"unitid" xorm:"'UNITID'"` // 单位id
  2301. MGLST []XHMiddleGoods `json:"mglst"` // 套保品列表
  2302. }
  2303. func (r *XHWrstandard) calc() {
  2304. if r.MGLST == nil {
  2305. r.MGLST = make([]XHMiddleGoods, 0)
  2306. }
  2307. }
  2308. func (r *XHWrstandard) buildSql() string {
  2309. var sqlId utils.SQLVal = `
  2310. select t.areauserid,
  2311. t.deliverygoodsid,
  2312. t.wrstandardid,
  2313. t.wrstandardcode,
  2314. t.wrstandardname,
  2315. t.vatrate,
  2316. t.unitid,
  2317. e.enumdicname
  2318. from wrstandard t
  2319. left join enumdicitem e
  2320. on t.unitid = e.enumitemname
  2321. and e.enumdiccode = 'goodsunit'
  2322. where 1 = 1
  2323. `
  2324. sqlId.AndEx("t.AREAUSERID", r.AREAUSERID, r.AREAUSERID > 0)
  2325. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  2326. return sqlId.String()
  2327. }
  2328. // GetDataEx 获取现货品种/品类
  2329. func (r *XHWrstandard) GetDataEx() (interface{}, error) {
  2330. sData := make([]XHWrstandard, 0)
  2331. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2332. for i := range sData {
  2333. sData[i].calc()
  2334. }
  2335. if len(sData) > 0 {
  2336. // 获取套保品列表
  2337. m := XHMiddleGoods{AREAUSERID: r.AREAUSERID, WRSTANDARDID: r.WRSTANDARDID}
  2338. if d, err := m.GetDataEx(); err == nil {
  2339. lstMidd := d.([]XHMiddleGoods)
  2340. for i := range sData {
  2341. sData[i].addMgLst(lstMidd)
  2342. }
  2343. }
  2344. }
  2345. return sData, err
  2346. }
  2347. func (r *XHWrstandard) addMgLst(midd []XHMiddleGoods) {
  2348. for i := range midd {
  2349. if r.WRSTANDARDID == midd[i].WRSTANDARDID {
  2350. r.MGLST = append(r.MGLST, midd[i])
  2351. }
  2352. }
  2353. }
  2354. // XHMiddleGoods 现货品种/品类/套保品
  2355. type XHMiddleGoods struct {
  2356. AREAUSERID int64 `json:"-" xorm:"'AREAUSERID'" form:"areauserid"` // 所属机构
  2357. WRSTANDARDID int64 `json:"-" xorm:"'WRSTANDARDID'" form:"wrstandardid"` // 现货商品ID(SEQ_WRSTANDARD)
  2358. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 拆算系数
  2359. MIDDLEGOODSID int64 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'"` // 套保品种id
  2360. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保品种代码
  2361. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保品种名称
  2362. ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  2363. UNITID int64 `json:"unitid" xorm:"'UNITID'"` // 单位id
  2364. GOODSGROUPID int64 `json:"-" xorm:"'GOODSGROUPID'"` // 关联期货商品组id
  2365. GPLST []TBGoodsGroup // 期货品种列表
  2366. }
  2367. func (r *XHMiddleGoods) calc() {
  2368. r.GPLST = make([]TBGoodsGroup, 0)
  2369. }
  2370. func (r *XHMiddleGoods) buildSql() string {
  2371. var sqlId utils.SQLVal = `
  2372. select w.wrstandardid,
  2373. c.convertratio,
  2374. m.areauserid,
  2375. m.middlegoodsid,
  2376. m.middlegoodscode,
  2377. m.middlegoodsname,
  2378. m.goodsunitid unitid,
  2379. m.goodsgroupid,
  2380. e.enumdicname
  2381. from wrstandard w
  2382. inner join erms2_wrsconvertdetail c
  2383. on w.wrstandardid = c.wrstandardid
  2384. inner join erms_middlegoods m
  2385. on c.middlegoodsid = m.middlegoodsid
  2386. left join enumdicitem e
  2387. on m.goodsunitid = e.enumitemname
  2388. and e.enumdiccode = 'goodsunit'
  2389. where 1 = 1 and w.isvalid = 1
  2390. `
  2391. sqlId.AndEx("m.areauserid", r.AREAUSERID, r.AREAUSERID > 0)
  2392. sqlId.AndEx("w.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  2393. return sqlId.String()
  2394. }
  2395. // GetDataEx 获取现货品种/品类/套保品
  2396. func (r *XHMiddleGoods) GetDataEx() (interface{}, error) {
  2397. sData := make([]XHMiddleGoods, 0)
  2398. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2399. for i := range sData {
  2400. sData[i].calc()
  2401. }
  2402. if len(sData) > 0 {
  2403. // 获取关联期货品种
  2404. m1 := TBGoodsGroup{}
  2405. if d1, err := m1.GetDataEx(); err == nil {
  2406. gpLst := d1.([]TBGoodsGroup)
  2407. for i := range sData {
  2408. sData[i].addGpLst(gpLst)
  2409. }
  2410. }
  2411. }
  2412. return sData, err
  2413. }
  2414. func (r *XHMiddleGoods) addGpLst(lst []TBGoodsGroup) {
  2415. for i := range lst {
  2416. if r.GOODSGROUPID == lst[i].DESTGOODSGROUPID {
  2417. r.GPLST = append(r.GPLST, lst[i])
  2418. }
  2419. }
  2420. }
  2421. // TBMiddleGoods 商品信息-套保品种
  2422. type TBMiddleGoods struct {
  2423. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'" form:"areauserid"` // 机构用户ID
  2424. MIDDLEGOODSID int64 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'" form:"middlegoodsid"` // 套保品种ID(SEQ_ERMS_MIDDLEGOODS)
  2425. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保品种代码
  2426. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保品种名称
  2427. NEEDHEDGERATIO float64 `json:"needhedgeratio" xorm:"'NEEDHEDGERATIO'"` // 套保比率
  2428. NEEDARBITRAGERATIO float64 `json:"needarbitrageratio" xorm:"'NEEDARBITRAGERATIO'"` // 套利比率
  2429. GOODSGROUPID int64 `json:"-" xorm:"'GOODSGROUPID'"` // 关联期货商品组id
  2430. ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  2431. GPLST []TBGoodsGroup // 期货品种列表
  2432. WDLST []TBWrstandard // 商品信息列表(现货品类)
  2433. }
  2434. func (r *TBMiddleGoods) calc() {
  2435. r.GPLST = make([]TBGoodsGroup, 0)
  2436. r.WDLST = make([]TBWrstandard, 0)
  2437. }
  2438. func (r *TBMiddleGoods) buildSql() string {
  2439. var sqlId utils.SQLVal = `
  2440. select t.areauserid,
  2441. t.middlegoodsid,
  2442. t.middlegoodscode,
  2443. t.middlegoodsname,
  2444. t.needhedgeratio,
  2445. t.needarbitrageratio,
  2446. t.goodsgroupid,
  2447. e.enumdicname
  2448. from erms_middlegoods t
  2449. left join enumdicitem e
  2450. on t.goodsunitid = e.enumitemname
  2451. and e.enumdiccode = 'goodsunit'
  2452. where 1 = 1 and t.isvalid=1
  2453. `
  2454. sqlId.AndEx("t.areauserid", r.AREAUSERID, r.AREAUSERID > 0)
  2455. sqlId.AndEx("t.middlegoodsid", r.MIDDLEGOODSID, r.MIDDLEGOODSID > 0)
  2456. return sqlId.String()
  2457. }
  2458. // GetDataEx 获取商品信息-套保品种
  2459. func (r *TBMiddleGoods) GetDataEx() (interface{}, error) {
  2460. sData := make([]TBMiddleGoods, 0)
  2461. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2462. for i := range sData {
  2463. sData[i].calc()
  2464. }
  2465. if len(sData) > 0 {
  2466. // 获取关联期货品种
  2467. m1 := TBGoodsGroup{}
  2468. if d1, err := m1.GetDataEx(); err == nil {
  2469. gpLst := d1.([]TBGoodsGroup)
  2470. for i := range sData {
  2471. sData[i].addGpLst(gpLst)
  2472. }
  2473. }
  2474. // 获取关联现货商品
  2475. m2 := TBWrstandard{}
  2476. if d2, err := m2.GetDataEx(); err == nil {
  2477. wdLst := d2.([]TBWrstandard)
  2478. for i := range sData {
  2479. sData[i].addWdLst(wdLst)
  2480. }
  2481. }
  2482. }
  2483. return sData, err
  2484. }
  2485. func (r *TBMiddleGoods) addGpLst(lst []TBGoodsGroup) {
  2486. for i := range lst {
  2487. if r.GOODSGROUPID == lst[i].DESTGOODSGROUPID {
  2488. r.GPLST = append(r.GPLST, lst[i])
  2489. }
  2490. }
  2491. }
  2492. func (r *TBMiddleGoods) addWdLst(lst []TBWrstandard) {
  2493. for i := range lst {
  2494. if r.MIDDLEGOODSID == lst[i].MIDDLEGOODSID {
  2495. r.WDLST = append(r.WDLST, lst[i])
  2496. }
  2497. }
  2498. }
  2499. // TBGoodsGroup 套保品种/期货品种
  2500. type TBGoodsGroup struct {
  2501. GOODSGROUPID int64 `json:"goodsgroupid" xorm:"'GOODSGROUPID'"` // 商品组ID(自增ID)
  2502. GOODSGROUPNAME string `json:"goodsgroupname" xorm:"'GOODSGROUPNAME'"` // 商品组名称
  2503. GOODUNITID int32 `json:"goodunitid" xorm:"'GOODUNITID'"` // 报价单位ID
  2504. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 折算系数
  2505. DESTGOODSGROUPID int64 `json:"-" xorm:"'DESTGOODSGROUPID'"` // 目标商品组id
  2506. EXEXCHANGECODE string `json:"exexchangecode" xorm:"'EXEXCHANGECODE'"` // 交易所代码
  2507. EXEXCHANGENAME string `json:"exexchangename" xorm:"'EXEXCHANGENAME'"` // 交易所名称
  2508. ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  2509. GDLST []TBGoods // 商品列表
  2510. }
  2511. func (r *TBGoodsGroup) calc() {
  2512. r.GDLST = make([]TBGoods, 0)
  2513. }
  2514. func (r *TBGoodsGroup) buildSql() string {
  2515. var sqlId utils.SQLVal = `
  2516. select t.goodsgroupid,
  2517. t.goodsgroupname,
  2518. t.goodunitid,
  2519. c.convertratio,
  2520. c.destgoodsgroupid,
  2521. ex.exexchangecode,
  2522. ex.exexchangename,
  2523. e.enumdicname
  2524. from goodsgroup t
  2525. inner join ermcp_ggconvertconfig c
  2526. on t.goodsgroupid = c.srcgoodsgroupid
  2527. left join externalexchange ex on t.exexchangeid=ex.autoid
  2528. left join enumdicitem e
  2529. on t.goodunitid = e.enumitemname
  2530. and e.enumdiccode = 'goodsunit'
  2531. where 1 = 1 and t.goodsgroupstatus=1
  2532. `
  2533. return sqlId.String()
  2534. }
  2535. // GetDataEx 获取套保品种/期货品种
  2536. func (r *TBGoodsGroup) GetDataEx() (interface{}, error) {
  2537. sData := make([]TBGoodsGroup, 0)
  2538. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2539. for i := range sData {
  2540. sData[i].calc()
  2541. }
  2542. if len(sData) > 0 {
  2543. // 获取关联商品
  2544. m := TBGoods{}
  2545. if d, err := m.GetDataEx(); err == nil {
  2546. gdLst := d.([]TBGoods)
  2547. for i := range sData {
  2548. sData[i].addGdLst(gdLst)
  2549. }
  2550. }
  2551. }
  2552. return sData, err
  2553. }
  2554. func (r *TBGoodsGroup) addGdLst(lst []TBGoods) {
  2555. for i := range lst {
  2556. if r.GOODSGROUPID == lst[i].GOODSGROUPID {
  2557. r.GDLST = append(r.GDLST, lst[i])
  2558. }
  2559. }
  2560. }
  2561. // TBGoods 套保品种/期货品种/期货合约
  2562. type TBGoods struct {
  2563. GOODSID int64 `json:"goodsid" xorm:"'GOODSID'"` // 商品ID(自增ID SEQ_GOODS)
  2564. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 商品代码(内部)
  2565. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 商品名称
  2566. GOODSGROUPID int64 `json:"-" xorm:"'GOODSGROUPID'"` // 所属商品组ID
  2567. AGREEUNIT float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约乘数
  2568. ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  2569. }
  2570. func (r *TBGoods) calc() {
  2571. }
  2572. func (r *TBGoods) buildSql() string {
  2573. var sqlId utils.SQLVal = `
  2574. select t.goodsid,
  2575. t.goodscode,
  2576. t.goodsname,
  2577. t.goodsgroupid,
  2578. t.agreeunit,
  2579. e.enumdicname
  2580. from goods t
  2581. left join enumdicitem e
  2582. on t.goodunitid = e.enumitemname
  2583. and e.enumdiccode = 'goodsunit'
  2584. where 1 = 1
  2585. and t.goodsstatus not in (4, 5, 6)
  2586. and t.goodsgroupid in
  2587. (select srcgoodsgroupid from ermcp_ggconvertconfig)
  2588. `
  2589. return sqlId.String()
  2590. }
  2591. // GetDataEx 获取套保品种/期货品种/期货合约
  2592. func (r *TBGoods) GetDataEx() (interface{}, error) {
  2593. sData := make([]TBGoods, 0)
  2594. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2595. for i := range sData {
  2596. sData[i].calc()
  2597. }
  2598. return sData, err
  2599. }
  2600. // TBWrstandard 套保品种/商品信息(现货商品)
  2601. type TBWrstandard struct {
  2602. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID(SEQ_WRSTANDARD)
  2603. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货商品代码
  2604. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 现货商品名称
  2605. DELIVERYGOODSID int64 `json:"-" xorm:"'DELIVERYGOODSID'"` // 品种ID
  2606. VATRATE float64 `json:"vatrate" xorm:"'VATRATE'"` // 增值税率
  2607. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 品种名称
  2608. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 折算系数
  2609. MIDDLEGOODSID int64 `json:"-" xorm:"'MIDDLEGOODSID'"` // 品种id
  2610. ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  2611. }
  2612. func (r *TBWrstandard) calc() {
  2613. }
  2614. func (r *TBWrstandard) buildSql() string {
  2615. var sqlId utils.SQLVal = `
  2616. select t.wrstandardid,
  2617. t.wrstandardcode,
  2618. t.wrstandardname,
  2619. t.deliverygoodsid,
  2620. t.vatrate,
  2621. g.deliverygoodsname,
  2622. c.convertratio,
  2623. c.middlegoodsid,
  2624. e.enumdicname
  2625. from wrstandard t
  2626. left join deliverygoods g
  2627. on t.deliverygoodsid = g.deliverygoodsid
  2628. inner join erms2_wrsconvertdetail c
  2629. on t.wrstandardid = c.wrstandardid
  2630. left join enumdicitem e
  2631. on t.unitid = e.enumitemname
  2632. and e.enumdiccode = 'goodsunit'
  2633. where 1 = 1 and t.isvalid=1
  2634. order by c.middlegoodsid
  2635. `
  2636. return sqlId.String()
  2637. }
  2638. // GetDataEx 获取套保品种/商品信息(现货商品)
  2639. func (r *TBWrstandard) GetDataEx() (interface{}, error) {
  2640. sData := make([]TBWrstandard, 0)
  2641. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2642. for i := range sData {
  2643. sData[i].calc()
  2644. }
  2645. return sData, err
  2646. }