ermcp3.go 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859
  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. `
  1016. sqlId.And("s.userid", r.Areauserid)
  1017. sqlId.And("t.middlegoodsid", r.MiddlegoodsId)
  1018. return sqlId.String()
  1019. }
  1020. // GetDataEx
  1021. func (r *Ermcp3ExposureDetail) GetDataEx() (interface{}, error) {
  1022. e := db.GetEngine()
  1023. s := e.SQL(r.buildSql())
  1024. sData := make([]Ermcp3ExposureDetail, 0)
  1025. if err := s.Find(&sData); err != nil {
  1026. return nil, err
  1027. }
  1028. for i := range sData {
  1029. sData[i].calc()
  1030. }
  1031. return sData, nil
  1032. }
  1033. // Ermcp3AreaSpot 敞口现货头寸
  1034. type Ermcp3AreaSpot struct {
  1035. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  1036. ORIBUYPLANQTY float64 `json:"-" xorm:"'ORIBUYPLANQTY'"` // 期初采购计划数量
  1037. ORIBUYPRICEDQTY float64 `json:"-" xorm:"'ORIBUYPRICEDQTY'"` // 期初采购合同已定价数量
  1038. ORISELLPLANQTY float64 `json:"-" xorm:"'ORISELLPLANQTY'"` // 期初销售计划数量
  1039. ORISELLPRICEDQTY float64 `json:"-" xorm:"'ORISELLPRICEDQTY'"` // 期初销售合同已定价数量
  1040. BUYPLANQTY float64 `json:"-" xorm:"'BUYPLANQTY'"` // 采购计划数量
  1041. BUYPRICEDQTY float64 `json:"-" xorm:"'BUYPRICEDQTY'"` // 采购合同已定价数量
  1042. SELLPLANQTY float64 `json:"-" xorm:"'SELLPLANQTY'"` // 销售计划数量
  1043. SELLPRICEDQTY float64 `json:"-" xorm:"'SELLPRICEDQTY'"` // 销售合同已定价数量
  1044. TOTALSPOTQTY float64 `json:"totalspotqty" xorm:"'TOTALSPOTQTY'"` // 当前数量(现货头寸总量) = (销售计划数量 - 销售已定价数量) - (采购计划数量 - 采购已定价数量)
  1045. OriToalSpotQty float64 `json:"oritoalspotqty" xorm:"'OriToalSpotQty'"` // 昨日数量
  1046. IncreaseQty float64 `json:"increaseqty" xorm:"'IncreaseQty'"` // 增加数量=销售计划数量+采购已定价数量
  1047. DecreaseQty float64 `json:"decreaseqty" xorm:"'DecreaseQty'"` // 减少数量=-(销售已定价数量+采购计划数量)
  1048. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  1049. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 套保系数
  1050. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID(SEQ_WRSTANDARD)
  1051. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货商品代码
  1052. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 现货商品名称
  1053. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 现货商品单位id
  1054. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  1055. }
  1056. func (r *Ermcp3AreaSpot) calc() {
  1057. /*
  1058. 增加数量=今日销售计划数量+今日采购已定价数量
  1059. 减少数量=-(今日销售已定价数量+今日采购计划数量)
  1060. 其中,
  1061. 今日销售计划数量=期末销售计划数量-期初销售计划数量
  1062. 今日采购已定价数量=期末采购已定价数量-期初采购已定价数量
  1063. 今日销售已定价数量=期末销售已定价数量-期初销售已定价数量
  1064. 今日采购计划数量=期末采购计划数量-期初采购计划数量
  1065. */
  1066. r.IncreaseQty = r.SELLPLANQTY - r.ORISELLPLANQTY + r.BUYPRICEDQTY - r.ORIBUYPRICEDQTY
  1067. r.DecreaseQty = (r.SELLPRICEDQTY - r.ORISELLPRICEDQTY + r.BUYPLANQTY - r.ORIBUYPLANQTY) * -1
  1068. r.OriToalSpotQty = (r.ORISELLPLANQTY - r.ORISELLPRICEDQTY) - (r.ORIBUYPLANQTY - r.ORIBUYPRICEDQTY)
  1069. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  1070. }
  1071. func (r *Ermcp3AreaSpot) buildSql() string {
  1072. var sqlId utils.SQLVal = `
  1073. select t.AREAUSERID,
  1074. t.ORIBUYPLANQTY,
  1075. t.ORIBUYPRICEDQTY,
  1076. t.ORISELLPLANQTY,
  1077. t.ORISELLPRICEDQTY,
  1078. t.BUYPLANQTY,
  1079. t.BUYPRICEDQTY,
  1080. t.SELLPLANQTY,
  1081. t.SELLPRICEDQTY,
  1082. t.hedgeqty,
  1083. t.arbitrageqty,
  1084. t.hedgeqty + t.arbitrageqty TOTALSPOTQTY,
  1085. t.convertratio,
  1086. w.wrstandardid,
  1087. w.wrstandardcode,
  1088. w.wrstandardname,
  1089. w.unitid,
  1090. to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME
  1091. from ermcp_areamiddlesum t
  1092. left join wrstandard w
  1093. on t.wrstandardid = w.wrstandardid
  1094. where 1 = 1
  1095. `
  1096. sqlId.And("t.areauserid", r.AREAUSERID)
  1097. return sqlId.String()
  1098. }
  1099. // GetDataEx
  1100. func (r *Ermcp3AreaSpot) GetDataEx() (interface{}, error) {
  1101. e := db.GetEngine()
  1102. s := e.SQL(r.buildSql())
  1103. sData := make([]Ermcp3AreaSpot, 0)
  1104. if err := s.Find(&sData); err != nil {
  1105. return nil, err
  1106. }
  1107. for i := range sData {
  1108. sData[i].calc()
  1109. }
  1110. return sData, nil
  1111. }
  1112. // Ermcp3AreaSpotDetail 敞口->现货头寸->现货明细
  1113. type Ermcp3AreaSpotDetail struct {
  1114. Relatedid string `json:"relatedid" xorm:"'relatedid'"` // 套保计划ID/现货合同ID
  1115. Relatedno string `json:"relatedno" xorm:"'relatedno'"` // 编号
  1116. LogType int32 `json:"logtype" xorm:"'logType'"` // 记录类型 1-套保 2-现货合同
  1117. Contracttype int32 `json:"contracttype" xorm:"'contracttype'"` // 合同类型 1-采购 -1-销售
  1118. Qty float64 `json:"qty" xorm:"'qty'"` // 数量
  1119. Strtime string `json:"strtime" xorm:"'strtime'"` // 时间
  1120. Enumdicname string `json:"enumdicname"` // 现货商品单位名称
  1121. Recordname string `json:"recordname"` // 类型名称
  1122. CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  1123. Unitid int32 `json:"-" xorm:"'UNITID'"` // 单位ID
  1124. UserId int64 `json:"-"` // 所属用户ID
  1125. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID(SEQ_WRSTANDARD)
  1126. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货商品代码
  1127. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 现货商品名称
  1128. }
  1129. func (r *Ermcp3AreaSpotDetail) calc() {
  1130. var logTypeName, contractTypeName string
  1131. if r.LogType == 1 {
  1132. logTypeName = "计划"
  1133. } else {
  1134. logTypeName = "合同"
  1135. }
  1136. if r.Contracttype == 1 {
  1137. contractTypeName = "采购"
  1138. } else {
  1139. contractTypeName = "销售"
  1140. }
  1141. // 销售合同 数量转为负数
  1142. if r.LogType == 2 && r.Contracttype == -1 {
  1143. if r.Qty > 0 {
  1144. r.Qty = r.Qty * -1
  1145. }
  1146. }
  1147. // 采购计划 数量转为负数
  1148. if r.LogType == 1 && r.Contracttype == 1 {
  1149. if r.Qty > 0 {
  1150. r.Qty = r.Qty * -1
  1151. }
  1152. }
  1153. r.Recordname = contractTypeName + logTypeName
  1154. r.Enumdicname = mtpcache.GetEnumDicitemName(r.Unitid)
  1155. }
  1156. func (r *Ermcp3AreaSpotDetail) buildSql() string {
  1157. var sqlId utils.SQLVal = `
  1158. select *
  1159. from (select to_char(t.hedgeplanid) as relatedid,
  1160. t.hedgeplanno relatedno,
  1161. 1 as LogType,
  1162. t.contracttype,
  1163. t.planqty qty,
  1164. to_char(t.audittime, 'yyyy-mm-dd hh24:mi:ss') createtime,
  1165. w.wrstandardid,
  1166. w.wrstandardcode,
  1167. w.wrstandardname,
  1168. w.unitid,
  1169. t.areauserid as userid
  1170. from ermcp_hedgeplan t
  1171. inner join wrstandard w
  1172. on t.wrstandardid = w.wrstandardid
  1173. where t.hedgeplanstatus in (2, 3, 5)
  1174. and t.tradedate = to_char(sysdate, 'yyyymmdd')
  1175. and t.areauserid = %v
  1176. and t.wrstandardid = %v
  1177. union all
  1178. select to_char(t.spotcontractid) as relatedid,
  1179. t.contractno relatedno,
  1180. 2 as LogType,
  1181. t.contracttype,
  1182. t.qty,
  1183. to_char(t.audittime, 'yyyy-mm-dd hh24:mi:ss') createtime,
  1184. w.wrstandardid,
  1185. w.wrstandardcode,
  1186. w.wrstandardname,
  1187. w.unitid,
  1188. t.userid
  1189. from ermcp_spotcontract t
  1190. inner join wrstandard w
  1191. on t.wrstandardid = w.wrstandardid
  1192. where t.userid = %v
  1193. and t.wrstandardid = %v
  1194. and t.contractstatus in (2, 3, 5)
  1195. and t.tradedate = to_char(sysdate, 'yyyymmdd')) a
  1196. order by a.createtime desc
  1197. `
  1198. sqlId.FormatParam(r.UserId, r.WRSTANDARDID, r.UserId, r.WRSTANDARDID)
  1199. return sqlId.String()
  1200. }
  1201. // GetDataEx
  1202. func (r *Ermcp3AreaSpotDetail) GetDataEx() (interface{}, error) {
  1203. e := db.GetEngine()
  1204. s := e.SQL(r.buildSql())
  1205. sData := make([]Ermcp3AreaSpotDetail, 0)
  1206. if err := s.Find(&sData); err != nil {
  1207. return nil, err
  1208. }
  1209. for i := range sData {
  1210. sData[i].calc()
  1211. }
  1212. return sData, nil
  1213. }
  1214. // Ermcp3Wrstandard 现货品类
  1215. type Ermcp3Wrstandard struct {
  1216. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类ID(SEQ_WRSTANDARD)
  1217. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1218. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种ID
  1219. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 品类单位ID
  1220. MINIVALUE int32 `json:"minivalue" xorm:"'MINIVALUE'"` // 最小变动值
  1221. MINIVALUEDP int32 `json:"minivaluedp" xorm:"'MINIVALUEDP'"` // 最小变动值小数位
  1222. REALMINIVALUE int32 `json:"realminivalue" xorm:"'REALMINIVALUE'"` // 实际最小变动值
  1223. REALMINIVALUEDP int32 `json:"realminivaluedp" xorm:"'REALMINIVALUEDP'"` // 实际最小变动值小数位
  1224. CREATORID int64 `json:"creatorid" xorm:"'CREATORID'"` // 创建人
  1225. CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  1226. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1227. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  1228. EnumdicName string `json:"enumdicname"` // 品类单位名称
  1229. ISVALID int32 `json:"isvalid" xorm:"'ISVALID'"` // 是否有效 0-无效(停用) 1-有效(正常)
  1230. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  1231. REMARK string `json:"remark" xorm:"'REMARK'"` // 备注
  1232. CONVERTFACTOR float64 `json:"convertfactor" xorm:"'CONVERTFACTOR'"` // 标仓系数
  1233. CANTRADE int32 `json:"cantrade" xorm:"'CANTRADE'"` // 是否允许交易 - 0:不允许 1:允许 -1:未配置(数据库里没有记录)
  1234. }
  1235. func (r *Ermcp3Wrstandard) calc() {
  1236. r.EnumdicName = mtpcache.GetEnumDicitemName(r.UNITID)
  1237. }
  1238. func (r *Ermcp3Wrstandard) buildSql() string {
  1239. sqlId := "select t.WRSTANDARDID," +
  1240. " t.WRSTANDARDCODE," +
  1241. " t.DELIVERYGOODSID," +
  1242. " t.UNITID," +
  1243. " t.MINIVALUE," +
  1244. " t.MINIVALUEDP," +
  1245. " t.REALMINIVALUE," +
  1246. " t.REALMINIVALUEDP," +
  1247. " t.CREATORID," +
  1248. " to_char(t.CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME," +
  1249. " t.WRSTANDARDNAME," +
  1250. " t.ISVALID," +
  1251. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1252. " t.REMARK," +
  1253. " t.CONVERTFACTOR," +
  1254. " t.AREAUSERID," +
  1255. " nvl(m.cantrade,-1) cantrade" +
  1256. " from wrstandard t" +
  1257. " left join marketspotgoodsconfig m on t.wrstandardid = m.wrstandardid" +
  1258. " where 1 = 1"
  1259. if r.AREAUSERID > 0 {
  1260. sqlId += fmt.Sprintf(" and t.AREAUSERID=%v", r.AREAUSERID)
  1261. }
  1262. if r.DELIVERYGOODSID > 0 {
  1263. sqlId = sqlId + fmt.Sprintf(" and t.DELIVERYGOODSID=%v", r.DELIVERYGOODSID)
  1264. }
  1265. sqlId += " order by createtime desc"
  1266. return sqlId
  1267. }
  1268. // GetDataEx
  1269. func (r *Ermcp3Wrstandard) GetDataEx() (interface{}, error) {
  1270. e := db.GetEngine()
  1271. s := e.SQL(r.buildSql())
  1272. sData := make([]Ermcp3Wrstandard, 0)
  1273. if err := s.Find(&sData); err != nil {
  1274. return nil, err
  1275. }
  1276. for i := range sData {
  1277. sData[i].calc()
  1278. }
  1279. return sData, nil
  1280. }
  1281. // Ermcp3AreaStockApply 出入库申请
  1282. type Ermcp3AreaStockApply struct {
  1283. INOUTAPPLYID string `json:"inoutapplyid" xorm:"'INOUTAPPLYID'"` // 申请ID(607+Unix秒时间戳(10位)+xxxxxx)
  1284. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构ID
  1285. INOUTTYPE int32 `json:"inouttype" xorm:"'INOUTTYPE'"` // 出入库类型 - 5:采购入库 6:销售出库 7:生产入库 8:生产出库
  1286. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类ID
  1287. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID
  1288. SPOTCONTRACTID string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` // 关联现货合同ID
  1289. WAREHOUSEINFOID string `json:"warehouseinfoid" xorm:"'WAREHOUSEINFOID'"` // 现货仓库ID
  1290. APPLYSTATUS int32 `json:"applystatus" xorm:"'APPLYSTATUS'"` // 申请状态 - 1:待审核 2:审核通过 3:审核拒绝 4:处理失败 5:已撤回
  1291. APPLYSRC int32 `json:"applysrc" xorm:"'APPLYSRC'"` // 申请来源 - 1:管理端 2:终端
  1292. APPLYID int64 `json:"applyid" xorm:"'APPLYID'"` // 申请人
  1293. APPLYREMARK string `json:"applyremark" xorm:"'APPLYREMARK'"` // 申请备注
  1294. APPLYTIME string `json:"applytime" xorm:"'APPLYTIME'"` // 申请时间
  1295. AUDITSRC int32 `json:"auditsrc" xorm:"'AUDITSRC'"` // 审核来源 - 1:管理端 2:终端
  1296. AUDITID int64 `json:"auditid" xorm:"'AUDITID'"` // 审核人
  1297. AUDITTIME string `json:"audittime" xorm:"'AUDITTIME'"` // 审核时间
  1298. AUDITREMARK string `json:"auditremark" xorm:"'AUDITREMARK'"` // 审核备注
  1299. AUDITTRADEDATE string `json:"audittradedate" xorm:"'AUDITTRADEDATE'"` // 审核交易日(yyyyMMdd)
  1300. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1301. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1302. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 单位id
  1303. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1304. WAREHOUSENAME string `json:"warehousename" xorm:"'WAREHOUSENAME'"` // 仓库名称
  1305. WAREHOUSECODE string `json:"warehousecode" xorm:"'WAREHOUSECODE'"` // 仓库代码
  1306. WAREHOUSETYPE int32 `json:"warehousetype" xorm:"'WAREHOUSETYPE'"` // 仓库类型 - 1 厂库 2 自有库 3 合作库
  1307. CONTRACTNO string `json:"contractno" xorm:"'CONTRACTNO'"` // 合同编号
  1308. CONTRACTTYPE int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 现货合同类型 - 1:采购 -1:销售
  1309. BUYUSERID int64 `json:"buyuserid" xorm:"'BUYUSERID'"` // 采购方userid
  1310. SELLUSERID int64 `json:"selluserid" xorm:"'SELLUSERID'"` // 销售方userid
  1311. PRICETYPE int32 `json:"pricetype" xorm:"'PRICETYPE'"` // 定价类型 - 1:一口价 2:点价 3:暂定价
  1312. CONTRACTQTY float64 `json:"contractqty" xorm:"'CONTRACTQTY'"` // 合同量
  1313. DETAILJSON string `json:"detailjson" xorm:"'DETAILJSON'"` // 明细数据
  1314. BUYUSERNAME string `json:"buyusername"` // 采购方名称
  1315. SELLUSERNAME string `json:"sellusername"` // 销售方名称
  1316. APPLYNAME string `json:"applyname"` // 申请人名称
  1317. AUDITNAME string `json:"auditname"` // 审核人名称
  1318. ENUMDICNAME string `json:"enumdicname"` // 现货商品单位名称
  1319. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种id
  1320. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  1321. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  1322. BUYNICKNAME string `json:"buynickname"` // 采购方昵称
  1323. SELLNICKNAME string `json:"sellnickname"` // 销售方昵称
  1324. QTY float64 `json:"qty"` // 数量
  1325. FilterStatus string `json:"-"` // 查询条件, 申请状态, 逗号隔开
  1326. FilterType string `json:"-"` // 查询条件, 出入库类型, 逗号隔开
  1327. BeginDate string `json:"-"` // 开始日期
  1328. EndDate string `json:"-"` // 结束日期
  1329. UserType int32 `json:"-"` // 用户类型
  1330. }
  1331. func (r *Ermcp3AreaStockApply) calc() {
  1332. // 采购方名称
  1333. r.BUYUSERNAME, r.BUYNICKNAME = mtpcache.GetUserNameAndNickName(r.BUYUSERID)
  1334. // 销售方名称
  1335. r.SELLUSERNAME, r.SELLNICKNAME = mtpcache.GetUserNameAndNickName(r.SELLUSERID)
  1336. // 申请人名称
  1337. if r.APPLYSRC == 1 {
  1338. r.APPLYNAME = mtpcache.GetSystemmangerLoginCode(r.APPLYID)
  1339. } else {
  1340. r.APPLYNAME = mtpcache.GetLoginCodeByLoginId(r.APPLYID)
  1341. }
  1342. // 审核人名称
  1343. if r.AUDITSRC == 1 {
  1344. r.AUDITNAME = mtpcache.GetSystemmangerLoginCode(r.AUDITID)
  1345. } else {
  1346. r.AUDITNAME = mtpcache.GetLoginCodeByLoginId(r.AUDITID)
  1347. }
  1348. // 单位名称
  1349. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  1350. if len(r.DETAILJSON) > 0 {
  1351. // 解析qty字段
  1352. req := struct {
  1353. QTY float64 `json:"InOutQty"` // 数量
  1354. }{}
  1355. if json.Unmarshal([]byte(r.DETAILJSON), &req) == nil {
  1356. r.QTY = req.QTY
  1357. }
  1358. }
  1359. }
  1360. func (r *Ermcp3AreaStockApply) buildSql() string {
  1361. var sqlId utils.SQLVal = "SELECT to_char(t.operateapplyid) INOUTAPPLYID," +
  1362. " t.USERID," +
  1363. " t.operateapplytype INOUTTYPE," +
  1364. " t.WRSTANDARDID," +
  1365. " t.SPOTGOODSBRANDID," +
  1366. " t.DELIVERYGOODSID," +
  1367. " to_char(t.relatedid) SPOTCONTRACTID," +
  1368. " to_char(t.WAREHOUSEINFOID) WAREHOUSEINFOID," +
  1369. " to_char(t.detailjson) detailjson," +
  1370. " t.APPLYSTATUS," +
  1371. " t.APPLYSRC," +
  1372. " t.APPLYID," +
  1373. " t.remark APPLYREMARK," +
  1374. " to_char(t.APPLYTIME, 'yyyy-mm-dd hh24:mi:ss') APPLYTIME," +
  1375. " t.AUDITSRC," +
  1376. " t.AUDITID," +
  1377. " to_char(t.AUDITTIME, 'yyyy-mm-dd hh24:mi:ss') AUDITTIME," +
  1378. " t.AUDITREMARK," +
  1379. " t.AUDITTRADEDATE," +
  1380. " w.wrstandardname," +
  1381. " w.wrstandardcode," +
  1382. " g.goodsunitid unitid," +
  1383. " gb.dgfactoryitemvalue brandname," +
  1384. " h.warehousename," +
  1385. " h.warehousecode," +
  1386. " h.warehousetype," +
  1387. " s.contractno," +
  1388. " s.contracttype," +
  1389. " s.buyuserid," +
  1390. " s.selluserid," +
  1391. " s.pricetype," +
  1392. " s.qty CONTRACTQTY," +
  1393. " g.deliverygoodscode," +
  1394. " g.deliverygoodsname" +
  1395. " FROM ermcp_contractoperateapply t" +
  1396. " left join wrstandard w" +
  1397. " on t.wrstandardid = w.wrstandardid" +
  1398. " left join dgfactoryitem gb" +
  1399. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1400. " left join deliverygoods g" +
  1401. " on t.deliverygoodsid = g.deliverygoodsid" +
  1402. " left join warehouseinfo h" +
  1403. " on t.warehouseinfoid = h.autoid" +
  1404. " left join ermcp_spotcontract s" +
  1405. " on t.relatedid = s.spotcontractid" +
  1406. " WHERE t.operateapplytype in(5,6,7,8)"
  1407. areaUserId := mtpcache.GetAreaUserId(r.USERID, r.UserType)
  1408. if areaUserId == r.USERID {
  1409. // 机构用户
  1410. sqlId.And("t.USERID", r.USERID)
  1411. } else {
  1412. if r.INOUTTYPE == 7 || r.INOUTTYPE == 8 {
  1413. // 生产出入库, 没有相关合同
  1414. sqlId.And("t.USERID", r.USERID)
  1415. } else {
  1416. sqlId.Join(fmt.Sprintf(" and %v in(s.saleuserid, s.tradeuserid, s.meruserid)", r.USERID))
  1417. }
  1418. }
  1419. // 合同id
  1420. sqlId.AndInterEx("t.RELATEDID", r.SPOTCONTRACTID, len(r.SPOTCONTRACTID) > 0)
  1421. // 现货品种id
  1422. if r.DELIVERYGOODSID > 0 {
  1423. sqlId.And("t.DELIVERYGOODSID", r.DELIVERYGOODSID)
  1424. }
  1425. // 品类id
  1426. if r.WRSTANDARDID > 0 {
  1427. sqlId.And("t.WRSTANDARDID", r.WRSTANDARDID)
  1428. }
  1429. // 品牌id
  1430. if r.SPOTGOODSBRANDID > 0 {
  1431. sqlId.And("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID)
  1432. }
  1433. // 仓库id
  1434. sqlId.AndInterEx("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID, len(r.WAREHOUSEINFOID) > 0)
  1435. // 日期范围
  1436. if len(r.BeginDate) > 0 {
  1437. if r.BeginDate == r.EndDate {
  1438. sqlId.And("t.tradedate", r.BeginDate)
  1439. } else if r.EndDate > r.BeginDate {
  1440. sqlId.BiggerOrEq("t.tradedate", r.BeginDate)
  1441. sqlId.LessOrEq("t.tradedate", r.EndDate)
  1442. }
  1443. }
  1444. // 出入库类型
  1445. if len(r.FilterType) > 0 {
  1446. // 原INOUTTYPE 1,2,3,4 转为 新表 5,6,7,8
  1447. r.FilterType = strings.ReplaceAll(r.FilterType, "1", "5")
  1448. r.FilterType = strings.ReplaceAll(r.FilterType, "2", "6")
  1449. r.FilterType = strings.ReplaceAll(r.FilterType, "3", "7")
  1450. r.FilterType = strings.ReplaceAll(r.FilterType, "4", "8")
  1451. if len(r.FilterType) == 1 {
  1452. sqlId.AndInterEx("t.OPERATEAPPLYTYPE", r.FilterType, true)
  1453. } else {
  1454. sqlId.Join(fmt.Sprintf(" and t.OPERATEAPPLYTYPE in(%v)", r.FilterType))
  1455. }
  1456. }
  1457. // 出入库状态
  1458. if len(r.FilterStatus) > 0 {
  1459. sqlId.Join(fmt.Sprintf(" and t.APPLYSTATUS in(%v)", r.FilterStatus))
  1460. }
  1461. // 排序
  1462. sqlId.Join(" order by t.APPLYTIME desc")
  1463. return sqlId.String()
  1464. }
  1465. // GetDataEx 查询库存申请(记录)
  1466. func (r *Ermcp3AreaStockApply) GetDataEx() (interface{}, error) {
  1467. sData := make([]Ermcp3AreaStockApply, 0)
  1468. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1469. if err == nil {
  1470. for i := range sData {
  1471. sData[i].calc()
  1472. }
  1473. }
  1474. return sData, err
  1475. }
  1476. // Ermcp3AreaStockApplySum 合同出入库数量汇总
  1477. type Ermcp3AreaStockApplySum struct {
  1478. INOUTTYPE int32 `json:"inouttype" xorm:"'INOUTTYPE'"` // 出入库类型 - 5:采购入库 6:销售出库 7:生产入库 8:生产出库
  1479. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类id
  1480. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 品牌id
  1481. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1482. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1483. TOTALQTY float64 `json:"totalqty" xorm:"'TOTALQTY'"` // 总数量
  1484. SPOTCONTRACTID string `json:"spotcontractid" xorm:"'relatedid'"` // 关联现货合同ID
  1485. DETAILJSON string `json:"-" xorm:"'DETAILJSON'"` // 出入库数量
  1486. Key string `json:"-"` // 汇总主键
  1487. }
  1488. // Calc
  1489. func (r *Ermcp3AreaStockApplySum) Calc() {
  1490. if len(r.DETAILJSON) > 0 {
  1491. // 解析qty字段
  1492. req := struct {
  1493. QTY float64 `json:"InOutQty"` // 数量
  1494. }{}
  1495. if json.Unmarshal([]byte(r.DETAILJSON), &req) == nil {
  1496. r.TOTALQTY = req.QTY
  1497. }
  1498. }
  1499. r.Key = fmt.Sprintf("%v_%v_%v", r.INOUTTYPE, r.WRSTANDARDID, r.SPOTGOODSBRANDID)
  1500. }
  1501. func (r *Ermcp3AreaStockApplySum) buildSql() string {
  1502. var sqlId utils.SQLVal = "select t.detailjson," +
  1503. " t.operateapplytype INOUTTYPE," +
  1504. " t.wrstandardid," +
  1505. " t.relatedid," +
  1506. " t.spotgoodsbrandid," +
  1507. " gb.dgfactoryitemvalue brandname," +
  1508. " w.wrstandardcode," +
  1509. " w.wrstandardname" +
  1510. " from ermcp_contractoperateapply t" +
  1511. " left join dgfactoryitem gb" +
  1512. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1513. " left join wrstandard w" +
  1514. " on t.wrstandardid = w.wrstandardid" +
  1515. " where t.operateapplytype in (5, 6)"
  1516. sqlId.And("t.relatedid", r.SPOTCONTRACTID)
  1517. return sqlId.String()
  1518. }
  1519. // GetDataEx 获取合同出入库汇总信息
  1520. func (r *Ermcp3AreaStockApplySum) GetDataEx() (interface{}, error) {
  1521. sData := make([]Ermcp3AreaStockApplySum, 0)
  1522. if err := db.GetEngine().SQL(r.buildSql()).Find(&sData); err == nil {
  1523. mSum := map[string]Ermcp3AreaStockApplySum{}
  1524. for i := range sData {
  1525. sData[i].Calc()
  1526. // 按品牌+品类+出入库类型 汇总
  1527. if d, ok := mSum[sData[i].Key]; ok {
  1528. d.TOTALQTY += sData[i].TOTALQTY
  1529. mSum[sData[i].Key] = d
  1530. } else {
  1531. mSum[sData[i].Key] = sData[i]
  1532. }
  1533. }
  1534. if len(mSum) > 0 {
  1535. sData = make([]Ermcp3AreaStockApplySum, 0)
  1536. for _, v := range mSum {
  1537. sData = append(sData, v)
  1538. }
  1539. }
  1540. }
  1541. return sData, nil
  1542. }
  1543. // Ermcp3AreaStock 机构库存表
  1544. type Ermcp3AreaStock struct {
  1545. TODAYBUYINQTY float64 `json:"todaybuyinqty" xorm:"'TODAYBUYINQTY'"` // 今日采购入库量
  1546. TODAYPRODUCEINQTY float64 `json:"todayproduceinqty" xorm:"'TODAYPRODUCEINQTY'"` // 今日生产入库量
  1547. TODAYSELLOUTQTY float64 `json:"todayselloutqty" xorm:"'TODAYSELLOUTQTY'"` // 今日销售出库量
  1548. TODAYPRODUCEOUTQTY float64 `json:"todayproduceoutqty" xorm:"'TODAYPRODUCEOUTQTY'"` // 今日生产出库量
  1549. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  1550. USERID int64 `json:"userid" xorm:"'USERID'" form:"userid" binding:"required"` // 机构ID
  1551. WRSTANDARDID string `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类ID
  1552. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID
  1553. WAREHOUSEINFOID string `json:"warehouseinfoid" xorm:"'WAREHOUSEINFOID'"` // 仓库ID
  1554. ORISTOCK float64 `json:"oristock" xorm:"'ORISTOCK'"` // 期初库存量(昨日量)
  1555. CURSTOCK float64 `json:"curstock" xorm:"'CURSTOCK'"` // 期末库存量(今日量)
  1556. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1557. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1558. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 单位id
  1559. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1560. WAREHOUSENAME string `json:"warehousename" xorm:"'WAREHOUSENAME'"` // 仓库名称
  1561. WAREHOUSECODE string `json:"warehousecode" xorm:"'WAREHOUSECODE'"` // 仓库代码
  1562. WAREHOUSETYPE int32 `json:"warehousetype" xorm:"'WAREHOUSETYPE'"` // 仓库类型 - 1 厂库 2 自有库 3 合作库
  1563. USERNAME string `json:"username"` // 机构名称
  1564. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  1565. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种id
  1566. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  1567. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  1568. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODSUNITID'"` // 现货商品单位id
  1569. }
  1570. func (r *Ermcp3AreaStock) calc() {
  1571. r.USERNAME = mtpcache.GetUserNameByUserId(r.USERID)
  1572. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  1573. }
  1574. func (r *Ermcp3AreaStock) buildSql() string {
  1575. var sqlId utils.SQLVal = "SELECT t.TODAYBUYINQTY," +
  1576. " t.TODAYPRODUCEINQTY," +
  1577. " t.TODAYSELLOUTQTY," +
  1578. " t.TODAYPRODUCEOUTQTY," +
  1579. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1580. " t.USERID," +
  1581. " t.WRSTANDARDID," +
  1582. " t.SPOTGOODSBRANDID," +
  1583. " t.WAREHOUSEINFOID," +
  1584. " t.DELIVERYGOODSID," +
  1585. " t.ORISTOCK," +
  1586. " t.CURSTOCK," +
  1587. " w.wrstandardname," +
  1588. " w.wrstandardcode," +
  1589. " w.unitid," +
  1590. " gb.dgfactoryitemvalue brandname," +
  1591. " h.warehousename," +
  1592. " h.warehousecode," +
  1593. " h.warehousetype," +
  1594. " g.deliverygoodsid," +
  1595. " g.deliverygoodscode," +
  1596. " g.deliverygoodsname," +
  1597. " g.goodsunitid" +
  1598. " FROM ERMCP_AREASTOCK t" +
  1599. " LEFT JOIN WRSTANDARD w" +
  1600. " on t.wrstandardid = w.wrstandardid" +
  1601. " LEFT JOIN dgfactoryitem gb" +
  1602. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1603. " LEFT JOIN deliverygoods g" +
  1604. " on t.deliverygoodsid = g.deliverygoodsid" +
  1605. " LEFT JOIN WAREHOUSEINFO h" +
  1606. " on t.warehouseinfoid = h.autoid" +
  1607. " WHERE 1 = 1"
  1608. sqlId.And("t.USERID", r.USERID)
  1609. sqlId.Join(" order by t.WRSTANDARDID, t.UPDATETIME desc")
  1610. return sqlId.String()
  1611. }
  1612. // GetDataEx 获取机构库存信息
  1613. func (r *Ermcp3AreaStock) GetDataEx() (interface{}, error) {
  1614. sData := make([]Ermcp3AreaStock, 0)
  1615. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1616. if err == nil {
  1617. for i := range sData {
  1618. sData[i].calc()
  1619. }
  1620. }
  1621. return sData, err
  1622. }
  1623. // Ermcp3SpotGoodsPrice 现货市价
  1624. type Ermcp3SpotGoodsPrice struct {
  1625. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货品类ID(通用则为0)
  1626. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID(通用则为0, 不为0则须先有品类ID)
  1627. CURRENCYID int32 `json:"currencyid" xorm:"'CURRENCYID'"` // 报价货币ID
  1628. SPOTGOODSPRICE float64 `json:"spotgoodsprice" xorm:"'SPOTGOODSPRICE'"` // 现货价格
  1629. TRADEDATE string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd)
  1630. OPERATESRC int32 `json:"operatesrc" xorm:"'OPERATESRC'"` // 最后操作来源 - 1:管理端 2:终端
  1631. OPERATEID int64 `json:"operateid" xorm:"'OPERATEID'"` // 最后操作人
  1632. OPERATETIME string `json:"operatetime" xorm:"'OPERATETIME'"` // 最后操作时间
  1633. ISVALID int32 `json:"isvalid" xorm:"'ISVALID'"` // 是否有效 - 0:无效 1:有效
  1634. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构id
  1635. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1636. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1637. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 现货商品单位id
  1638. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1639. GMUNITID int32 `json:"gmunitid" xorm:"'GMUNITID'"` // 品类单位id
  1640. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货商品id
  1641. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货商品代码
  1642. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货商品名称
  1643. OPERATORNAME string `json:"operatorname"` // 操作人名称
  1644. ENUMDICNAME string `json:"enumdicname"` // 现货商品单位名称
  1645. GBENUMDICNAME string `json:"gbenumdicname"` // 品类单位名称
  1646. CURRENCYNAME string `json:"currencyname"` // 币种名称
  1647. }
  1648. func (r *Ermcp3SpotGoodsPrice) calc() {
  1649. if r.OPERATESRC == 1 {
  1650. r.OPERATORNAME = mtpcache.GetSystemmangerLoginCode(r.OPERATEID)
  1651. } else {
  1652. r.OPERATORNAME = mtpcache.GetLoginCodeByLoginId(r.OPERATEID)
  1653. }
  1654. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  1655. r.GBENUMDICNAME = mtpcache.GetEnumDicitemName(r.GMUNITID)
  1656. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  1657. }
  1658. func (r *Ermcp3SpotGoodsPrice) buildSql() string {
  1659. var sqlId utils.SQLVal = "SELECT t.WRSTANDARDID," +
  1660. " t.SPOTGOODSBRANDID," +
  1661. " t.CURRENCYID," +
  1662. " t.SPOTGOODSPRICE," +
  1663. " t.TRADEDATE," +
  1664. " t.OPERATESRC," +
  1665. " t.OPERATEID," +
  1666. " to_char(t.OPERATETIME, 'yyyy-mm-dd hh24:mi:ss') OPERATETIME," +
  1667. " t.ISVALID," +
  1668. " w.areauserid," +
  1669. " w.wrstandardcode," +
  1670. " w.wrstandardname," +
  1671. " w.unitid gmunitid," +
  1672. " gb.dgfactoryitemvalue brandname," +
  1673. " g.deliverygoodsid," +
  1674. " g.deliverygoodscode," +
  1675. " g.deliverygoodsname," +
  1676. " g.goodsunitid unitid" +
  1677. " FROM ERMCP_SPOTGOODSPRICE t" +
  1678. " left join wrstandard w" +
  1679. " on t.wrstandardid = w.wrstandardid" +
  1680. " left join dgfactoryitem gb" +
  1681. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1682. " left join deliverygoods g" +
  1683. " on t.deliverygoodsid = g.deliverygoodsid" +
  1684. " WHERE 1 = 1"
  1685. sqlId.And("w.areauserid", r.AREAUSERID)
  1686. return sqlId.String()
  1687. }
  1688. // GetDataEx 获取现货市价
  1689. func (r *Ermcp3SpotGoodsPrice) GetDataEx() (interface{}, error) {
  1690. sData := make([]Ermcp3SpotGoodsPrice, 0)
  1691. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1692. if err == nil {
  1693. for i := range sData {
  1694. sData[i].calc()
  1695. }
  1696. }
  1697. return sData, err
  1698. }
  1699. // Ermcp3SpotGoodsPriceLog 现货市价操作日志
  1700. type Ermcp3SpotGoodsPriceLog struct {
  1701. LOGID int64 `json:"logid" xorm:"'LOGID'"` // 日志id
  1702. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货品类ID(通用则为0)
  1703. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID(通用则为0, 不为0则须先有品类ID)
  1704. CURRENCYID int64 `json:"currencyid" xorm:"'CURRENCYID'"` // 报价货币ID
  1705. SPOTGOODSPRICE float64 `json:"spotgoodsprice" xorm:"'SPOTGOODSPRICE'"` // 现货价格
  1706. TRADEDATE string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd)
  1707. OPERATESRC int32 `json:"operatesrc" xorm:"'OPERATESRC'"` // 最后操作来源 - 1:管理端 2:终端
  1708. OPERATEID int64 `json:"operateid" xorm:"'OPERATEID'"` // 最后操作人
  1709. OPERATETIME string `json:"operatetime" xorm:"'OPERATETIME'"` // 最后操作时间
  1710. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构id
  1711. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1712. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1713. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 现货商品单位id
  1714. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1715. GMUNITID int32 `json:"gmunitid" xorm:"'GMUNITID'"` // 品类单位id
  1716. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货商品id
  1717. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货商品代码
  1718. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货商品名称
  1719. OPERATORNAME string `json:"operatorname"` // 操作人名称
  1720. ENUMDICNAME string `json:"enumdicname"` // 现货商品单位名称
  1721. GBENUMDICNAME string `json:"gbenumdicname"` // 品类单位名称
  1722. }
  1723. func (r *Ermcp3SpotGoodsPriceLog) calc() {
  1724. if r.OPERATESRC == 1 {
  1725. r.OPERATORNAME = mtpcache.GetSystemmangerLoginCode(r.OPERATEID)
  1726. } else {
  1727. r.OPERATORNAME = mtpcache.GetLoginCodeByLoginId(r.OPERATEID)
  1728. }
  1729. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  1730. r.GBENUMDICNAME = mtpcache.GetEnumDicitemName(r.GMUNITID)
  1731. }
  1732. func (r *Ermcp3SpotGoodsPriceLog) buildSql() string {
  1733. var sqlId utils.SQLVal = "SELECT t.WRSTANDARDID," +
  1734. " t.SPOTGOODSBRANDID," +
  1735. " t.CURRENCYID," +
  1736. " t.SPOTGOODSPRICE," +
  1737. " t.TRADEDATE," +
  1738. " t.OPERATESRC," +
  1739. " t.OPERATEID," +
  1740. " to_char(t.OPERATETIME, 'yyyy-mm-dd hh24:mi:ss') OPERATETIME," +
  1741. " t.LOGID," +
  1742. " w.areauserid," +
  1743. " w.wrstandardcode," +
  1744. " w.wrstandardname," +
  1745. " w.unitid gmunitid," +
  1746. " gb.dgfactoryitemvalue brandname," +
  1747. " g.goodsunitid unitid ," +
  1748. " g.deliverygoodsid," +
  1749. " g.deliverygoodscode," +
  1750. " g.deliverygoodsname" +
  1751. " FROM ERMCP_SPOTGOODSPRICELOG t" +
  1752. " left join wrstandard w" +
  1753. " on t.wrstandardid = w.wrstandardid" +
  1754. " left join dgfactoryitem gb" +
  1755. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1756. " left join deliverygoods g" +
  1757. " on t.deliverygoodsid = g.deliverygoodsid" +
  1758. " WHERE 1 = 1"
  1759. sqlId.And("w.areauserid", r.AREAUSERID)
  1760. sqlId.And("t.deliverygoodsid", r.DELIVERYGOODSID)
  1761. sqlId.AndEx("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID, r.SPOTGOODSBRANDID > 0)
  1762. sqlId.AndEx("t.wrstandardid", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  1763. return sqlId.String()
  1764. }
  1765. // GetDataEx 获取现货市价信息日志
  1766. func (r *Ermcp3SpotGoodsPriceLog) GetDataEx() (interface{}, error) {
  1767. sData := make([]Ermcp3SpotGoodsPriceLog, 0)
  1768. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1769. if err == nil {
  1770. for i := range sData {
  1771. sData[i].calc()
  1772. }
  1773. }
  1774. return sData, err
  1775. }
  1776. // Ermcp3HedgePlan 套保计划表结构
  1777. type Ermcp3HedgePlan struct {
  1778. Areauserid int64 `json:"areauserid" xorm:"'Areauserid'"` // 用户ID
  1779. Hedgeplanid string `json:"hedgeplanid" xorm:"'Hedgeplanid'"` // 套保计划ID(601+Unix秒时间戳(10位)+xxxxxx)
  1780. Hedgeplanno string `json:"hedgeplanno" xorm:"'Hedgeplanno'"` // 套保计划编号(名称)
  1781. Contracttype int `json:"contracttype" xorm:"'Contracttype'"` // 计划类型 - 1:采购 -1:销售
  1782. Deliverygoodsid int `json:"deliverygoodsid" xorm:"'Deliverygoodsid'"` // 现货品种ID
  1783. Deliverygoodsname string `json:"deliverygoodsname" xorm:"'Deliverygoodsname'"` // 现货品种名称
  1784. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  1785. Producttype int `json:"producttype" xorm:"'Producttype'"` // 产品类型 - 1:标准仓单 2:等标 3:非标
  1786. Spotgoodsdesc string `json:"spotgoodsdesc" xorm:"'Spotgoodsdesc'"` // 商品型号
  1787. Planqty float64 `json:"planqty" xorm:"'Planqty'"` // 计划数量
  1788. Convertfactor float64 `json:"convertfactor" xorm:"'Convertfactor'"` // 标仓系数
  1789. Plantime string `json:"plantime" xorm:"'Plantime'"` // 计划时间
  1790. Hedgeplanstatus int `json:"hedgeplanstatus" xorm:"'Hedgeplanstatus'"` // 套保计划状态 - 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
  1791. Remark string `json:"remark" xorm:"'Remark'"` // 备注
  1792. CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  1793. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  1794. AUDITTIME string `json:"audittime" xorm:"'AUDITTIME'"` // 审核时间
  1795. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 单位id(现货商品)
  1796. ENUMDICNAME string `json:"enumdicname"` // 单位名称(现货商品)
  1797. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类id
  1798. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1799. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1800. ACCOUNTID int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 资金账号
  1801. TAACCOUNTNAME string `json:"taaccountname" xorm:"'TAACCOUNTNAME'"` // 资金账号名称
  1802. TRADEUSERID int64 `json:"tradeuserid" xorm:"'TRADEUSERID'"` // 交易用户id
  1803. CURRENCYID int32 `json:"currencyid" xorm:"'CURRENCYID'"` // 币种id
  1804. WRUNITID int32 `json:"wrunitid" xorm:"'WRUNITID'"` // 品类单位id
  1805. AUDITID int64 `json:"auditid" xorm:"'auditid'"` // 审核id
  1806. AUDITSRC int32 `json:"auditsrc" xorm:"'AUDITSRC'"` // 审核来源 1-管理端 2-终端
  1807. AUDITREMARK string `json:"auditremark" xorm:"'auditremark'"` // 审核备注
  1808. AUDITNAME string `json:"auditname" xorm:"'AUDITNAME'"` // 审核人名称
  1809. VATRATE float64 `json:"vatrate" xorm:"'VATRATE'"` // 增值税率
  1810. WRUNITIDNAME string `json:"wrunitidame"` // 品类单位名称
  1811. TRADEUSERNAME string `json:"tradeusername"` // 交易用户名称
  1812. CURRENCYNAME string `json:"currencyname"` // 币种名称
  1813. UserType int32 `json:"-"` // 过滤条件, 用户类型
  1814. }
  1815. func (r *Ermcp3HedgePlan) calc() {
  1816. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  1817. r.TRADEUSERNAME = mtpcache.GetUserNameByUserId(r.TRADEUSERID)
  1818. r.CURRENCYNAME = mtpcache.GetCurrencyName(r.CURRENCYID)
  1819. if r.WRUNITID > 0 {
  1820. r.WRUNITIDNAME = mtpcache.GetEnumDicitemName(r.WRUNITID)
  1821. }
  1822. if r.AUDITSRC == 1 {
  1823. if r.AUDITNAME == "" && r.AUDITID > 0 {
  1824. r.AUDITNAME = mtpcache.GetSystemmangerLoginCode(r.AUDITID)
  1825. }
  1826. }
  1827. }
  1828. func (r *Ermcp3HedgePlan) buildSql(status string) string {
  1829. str := "select to_char(t.Hedgeplanid) Hedgeplanid," +
  1830. " t.Hedgeplanno," +
  1831. " t.Contracttype," +
  1832. " t.Deliverygoodsid," +
  1833. " g.Deliverygoodsname," +
  1834. " g.deliverygoodscode," +
  1835. " g.goodsunitid unitid," +
  1836. " t.Producttype," +
  1837. " t.Spotgoodsdesc," +
  1838. " t.Planqty," +
  1839. " t.Convertfactor," +
  1840. " to_char(t.Plantime, 'yyyy-mm-dd hh24:mi:ss') Plantime," +
  1841. " to_char(t.createtime, 'yyyy-mm-dd hh24:mi:ss') createtime," +
  1842. " to_char(t.updatetime, 'yyyy-mm-dd hh24:mi:ss') updatetime," +
  1843. " to_char(t.audittime, 'yyyy-mm-dd hh24:mi:ss') audittime," +
  1844. " t.Hedgeplanstatus," +
  1845. " t.Remark," +
  1846. " t.wrstandardid," +
  1847. " w.wrstandardcode," +
  1848. " w.wrstandardname," +
  1849. " w.unitid wrunitid," +
  1850. " w.vatrate," +
  1851. " t.accountid," +
  1852. " t.tradeuserid," +
  1853. " t.currencyid," +
  1854. " t.auditid," +
  1855. " t.auditsrc," +
  1856. " t.auditremark," +
  1857. " ta.accountname taaccountname," +
  1858. " l.accountname auditname" +
  1859. " from ermcp_hedgeplan t" +
  1860. " left join deliverygoods g" +
  1861. " on t.deliverygoodsid = g.deliverygoodsid" +
  1862. " left join wrstandard w" +
  1863. " on t.wrstandardid = w.wrstandardid" +
  1864. " left join taaccount ta" +
  1865. " on t.accountid = ta.accountid" +
  1866. " left join loginaccount l on t.auditid=l.loginid" +
  1867. " where t.hedgeplanstatus in (%v)"
  1868. if r.UserType == 0 {
  1869. // 终端没传值, 则自己去找
  1870. userId := mtpcache.GetAreaUserId(r.Areauserid, 0)
  1871. if userId == r.Areauserid {
  1872. r.UserType = 2
  1873. } else {
  1874. r.UserType = 7
  1875. }
  1876. }
  1877. if r.UserType == 2 {
  1878. str += " and t.areauserid = %v"
  1879. } else if r.UserType == 7 {
  1880. // 企业成员用tradeuserid匹配
  1881. str += " and t.tradeuserid = %v"
  1882. } else {
  1883. str += " and t.areauserid = %v"
  1884. }
  1885. if status == "2" {
  1886. // 执行中状态,按审核时间倒序
  1887. str += " order by t.audittime desc"
  1888. } else {
  1889. str += " order by t.updatetime desc"
  1890. }
  1891. return fmt.Sprintf(str, status, r.Areauserid)
  1892. }
  1893. // GetData 从数据库中查询套保计划数据
  1894. func (r *Ermcp3HedgePlan) GetData(status string) ([]Ermcp3HedgePlan, error) {
  1895. e := db.GetEngine()
  1896. sData := make([]Ermcp3HedgePlan, 0)
  1897. if err := e.SQL(r.buildSql(status)).Find(&sData); err != nil {
  1898. logger.GetLogger().Errorf("query hedgeplan:%v", err)
  1899. return sData, err
  1900. }
  1901. for i := range sData {
  1902. sData[i].calc()
  1903. }
  1904. return sData, nil
  1905. }
  1906. // Ermcp3GoodsGroup 商品组
  1907. type Ermcp3GoodsGroup struct {
  1908. GOODSGROUPID int32 `json:"goodsgroupid" xorm:"'GOODSGROUPID'"` // 商品组ID(自增ID)
  1909. GOODSGROUPNAME string `json:"goodsgroupname" xorm:"'GOODSGROUPNAME'"` // 商品组名称
  1910. GOODSGROUPSTATUS int32 `json:"goodsgroupstatus" xorm:"'GOODSGROUPSTATUS'"` // 商品组状态 - 1:正常 2:注销
  1911. MARKETID int32 `json:"marketid" xorm:"'MARKETID'"` // 所属市场ID
  1912. CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  1913. CREATORID int32 `json:"creatorid" xorm:"'CREATORID'"` // 创建人
  1914. MODIFYTIME string `json:"modifytime" xorm:"'MODIFYTIME'"` // 修改时间
  1915. MODIFIERID int32 `json:"modifierid" xorm:"'MODIFIERID'"` // 修改人
  1916. QUOTESOURCEGROUPID int64 `json:"quotesourcegroupid" xorm:"'QUOTESOURCEGROUPID'"` // 所属行情源分组ID[参考行情市场用\通道交易]
  1917. OUTERGROUPCODE string `json:"outergroupcode" xorm:"'OUTERGROUPCODE'"` // 外部商品组代码[通道交易]
  1918. CANSHORT int32 `json:"canshort" xorm:"'CANSHORT'"` // 是否允许做空[通道交易] - 0:不能做空 1:可以做空
  1919. EXEXCHANGEID int64 `json:"exexchangeid" xorm:"'EXEXCHANGEID'"` // 外部交易所ID[通道交易]
  1920. CURRENCYID int32 `json:"currencyid" xorm:"'CURRENCYID'"` // 报价货币ID
  1921. GOODUNITID int32 `json:"goodunitid" xorm:"'GOODUNITID'"` // 报价单位ID
  1922. AGREEUNIT float32 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约单位
  1923. DECIMALPLACE int32 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 报价小数位
  1924. QUOTEMINUNIT int32 `json:"quoteminunit" xorm:"'QUOTEMINUNIT'"` // 行情最小变动单位 [整数,报价小数位一起使用]
  1925. INNERDEALMODE int32 `json:"innerdealmode" xorm:"'INNERDEALMODE'"` // 内部成交方式[通道交易] - 1:净头寸 2:开平 3:平今
  1926. OUTERDEALMODE int32 `json:"outerdealmode" xorm:"'OUTERDEALMODE'"` // 外部成交方式[通道交易]- 1:净头寸 2:开平 3:平今
  1927. SYNCGOODSQTY int32 `json:"syncgoodsqty" xorm:"'SYNCGOODSQTY'"` // 同步合约数[通道交易-投资管理用] - 0表示不限
  1928. CLOSEPRICEMODE int32 `json:"closepricemode" xorm:"'CLOSEPRICEMODE'"` // 强平价格方式 - 1:市价 2:最新价 3:涨跌停(未实现)
  1929. GROUPCATEGROYID int32 `json:"groupcategroyid" xorm:"'GROUPCATEGROYID'"` // 品种分类ID
  1930. EXEXCHANGECODE string `json:"exexchangecode" xorm:"'EXEXCHANGECODE'"` // 交易所代码
  1931. EXEXCHANGENAME string `json:"exexchangename" xorm:"'EXEXCHANGENAME'"` // 交易所名称
  1932. EXCHANGEFULLNAME string `json:"exchangefullname" xorm:"'EXCHANGEFULLNAME'"` // 交易所全称
  1933. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  1934. AreaUserId int64 `json:"areauserid" form:"areauserid" binding:"required"` // 所属机构id(套保品是有areauserid的,当要排除已配置商品组时需要areauserid过滤)
  1935. ExcludeCfg int32 `json:"excludecfg" form:"excludecfg"` // 排除套保品已配置期货分类下的商品组(但显示已配置商品组) 1-排除
  1936. }
  1937. func (r *Ermcp3GoodsGroup) calc() {
  1938. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.GOODUNITID)
  1939. }
  1940. func (r *Ermcp3GoodsGroup) buildSql() string {
  1941. var sqlId utils.SQLVal = "SELECT t.GOODSGROUPID," +
  1942. " t.GOODSGROUPNAME," +
  1943. " t.GOODSGROUPSTATUS," +
  1944. " t.MARKETID," +
  1945. " to_char(t.CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME," +
  1946. " t.CREATORID," +
  1947. " to_char(t.MODIFYTIME, 'yyyy-mm-dd hh24:mi:ss') MODIFYTIME," +
  1948. " t.MODIFIERID," +
  1949. " t.QUOTESOURCEGROUPID," +
  1950. " t.OUTERGROUPCODE," +
  1951. " t.CANSHORT," +
  1952. " t.EXEXCHANGEID," +
  1953. " t.CURRENCYID," +
  1954. " t.GOODUNITID," +
  1955. " t.AGREEUNIT," +
  1956. " t.DECIMALPLACE," +
  1957. " t.QUOTEMINUNIT," +
  1958. " t.INNERDEALMODE," +
  1959. " t.OUTERDEALMODE," +
  1960. " t.SYNCGOODSQTY," +
  1961. " t.CLOSEPRICEMODE," +
  1962. " t.GROUPCATEGROYID," +
  1963. " e.exexchangecode," +
  1964. " e.exexchangename," +
  1965. " e.exchangefullname" +
  1966. " FROM GOODSGROUP t" +
  1967. " LEFT JOIN market m" +
  1968. " on t.marketid = m.marketid" +
  1969. " LEFT JOIN externalexchange e on t.exexchangeid=e.autoid" +
  1970. " WHERE 1 = 1" +
  1971. " and m.trademode = 15"
  1972. if r.ExcludeCfg == 1 {
  1973. str := "select t.goodsgroupid" +
  1974. " from ERMS_MiddleGoods t" +
  1975. " where t.isvalid=1 and t.areauserid = %v" +
  1976. " union all " +
  1977. "select goodsgroupid" +
  1978. " from goodsgroup t" +
  1979. " where t.groupcategroyid not in" +
  1980. " (select distinct g.groupcategroyid" +
  1981. " from erms_middlegoods t" +
  1982. " left join goodsgroup g" +
  1983. " on t.goodsgroupid = g.goodsgroupid" +
  1984. " where t.isvalid=1 and t.areauserid = %v)"
  1985. str = fmt.Sprintf(str, r.AreaUserId, r.AreaUserId)
  1986. sqlId.Join(fmt.Sprintf(" and t.goodsgroupid in(%v)", str))
  1987. }
  1988. return sqlId.String()
  1989. }
  1990. // GetData
  1991. func (r *Ermcp3GoodsGroup) GetData() ([]Ermcp3GoodsGroup, error) {
  1992. sData := make([]Ermcp3GoodsGroup, 0)
  1993. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1994. for i := range sData {
  1995. sData[i].calc()
  1996. }
  1997. return sData, err
  1998. }
  1999. // GetDataEx 商品组信息
  2000. func (r *Ermcp3GoodsGroup) GetDataEx() (interface{}, error) {
  2001. return r.GetData()
  2002. }
  2003. // Ermcp3RootUserAccount 顶级用户
  2004. type Ermcp3RootUserAccount struct {
  2005. ROOTUSERID int64 `json:"rootuserid" xorm:"ROOTUSERID"` // 根用户ID
  2006. CANRECOMMEND int32 `json:"canrecommend" xorm:"CANRECOMMEND"` // 是否可推荐 - 0:不可 1;可
  2007. USERID int64 `json:"userid" xorm:"USERID"` // 用户ID
  2008. USERTYPE int32 `json:"usertype" xorm:"USERTYPE"` // 账户类型 - 1:交易所 2:机构 3:会员子机构 4:经纪人 5:投资者 6:客户 7:企业成员(云平台)
  2009. MODIFYSTATUS int32 `json:"modifystatus" xorm:"MODIFYSTATUS"` // 变更状态 1 未变更 2 变更中 3 变更待审核 4 变更待复核(投资者)
  2010. ACCOUNTNAME string `json:"accountname" xorm:"ACCOUNTNAME"` // 账户名称(机构名称)
  2011. PARENTUSERID int64 `json:"parentuserid" xorm:"PARENTUSERID"` // 所属机构ID
  2012. BROKER int64 `json:"broker" xorm:"BROKER"` // 所属经纪人ID
  2013. SUBAREALEVELPATH string `json:"subarealevelpath" xorm:"SUBAREALEVELPATH"` // 子机构层级路径(逗号分隔,首尾加逗号)
  2014. REFEREEUSERID int64 `json:"refereeuserid" xorm:"REFEREEUSERID"` // 推荐人ID
  2015. MEMBERUSERID int64 `json:"memberuserid" xorm:"MEMBERUSERID"` // 所属会员ID
  2016. ACCOUNTSTATUS int32 `json:"accountstatus" xorm:"ACCOUNTSTATUS"` // 账户状态 - 1:待激活 2:待审核 3:待复审 4:正常 5:审核拒绝 6:停用(注销) 7:注销(删除)
  2017. RECKONACCOUNTID int64 `json:"reckonaccountid" xorm:"RECKONACCOUNTID"` // 默认结算资金账号ID(机构分润使用) 作废
  2018. CREATETIME string `json:"createtime" xorm:"CREATETIME"` // 创建时间
  2019. CREATORID int64 `json:"creatorid" xorm:"CREATORID"` // 创建人
  2020. AUDITTIME string `json:"audittime" xorm:"AUDITTIME"` // 审核时间
  2021. AUDITUSERID int64 `json:"audituserid" xorm:"AUDITUSERID"` // 审核人
  2022. AUDITREMARK string `json:"auditremark" xorm:"AUDITREMARK"` // 审核备注
  2023. CANCELTIME string `json:"canceltime" xorm:"CANCELTIME"` // 销户时间
  2024. CANCELUSERID int64 `json:"canceluserid" xorm:"CANCELUSERID"` // 销户人
  2025. MODIFYTIME string `json:"modifytime" xorm:"MODIFYTIME"` // 修改时间
  2026. MODIFIERID int64 `json:"modifierid" xorm:"MODIFIERID"` // 修改人
  2027. MODIFYREMARK string `json:"modifyremark" xorm:"MODIFYREMARK"` // 变更备注
  2028. MAXINVESTORNUM int64 `json:"maxinvestornum" xorm:"MAXINVESTORNUM"` // 最大用户数(经纪会员下投资者个数)
  2029. HASAUTH int32 `json:"hasauth" xorm:"HASAUTH"` // 是否已实名认证 - 0:未认证 1:已认证 2:已提交(待审核) 3:已拒绝
  2030. REFERCOUNT int32 `json:"refercount" xorm:"REFERCOUNT"` // 推荐总人数
  2031. REFERNUM string `json:"refernum" xorm:"REFERNUM"` // 推荐码
  2032. ISANONYMOUS int32 `json:"isanonymous" xorm:"ISANONYMOUS"` // 是否匿名下单 - 0:否 1:是
  2033. PARENTTOPUSER string `json:"parenttopuser" xorm:"PARENTTOPUSER"` // 上级顶级机构 [092=0,1时,默认为1, 092=2时若自已为顶级,则填入自己,自己不为顶级,填入ParentUserID的"ParentTopUser"]
  2034. SUBACCOUNTLEVEL int32 `json:"subaccountlevel" xorm:"SUBACCOUNTLEVEL"` // 子账户层数
  2035. APPLYSRC int32 `json:"applysrc" xorm:"APPLYSRC"` // 申请来源 - 1:管理端 2:终端
  2036. AUDITSRC int32 `json:"auditsrc" xorm:"AUDITSRC"` // 审核来源 - 1:管理端 2:终端
  2037. }
  2038. func (r *Ermcp3RootUserAccount) calc() {
  2039. }
  2040. func (r *Ermcp3RootUserAccount) buildSql() string {
  2041. var sqlId utils.SQLVal = "SELECT t.ROOTUSERID," +
  2042. " t.CANRECOMMEND," +
  2043. " t.USERID," +
  2044. " t.USERTYPE," +
  2045. " t.MODIFYSTATUS," +
  2046. " t.ACCOUNTNAME," +
  2047. " t.PARENTUSERID," +
  2048. " t.BROKER," +
  2049. " t.SUBAREALEVELPATH," +
  2050. " t.REFEREEUSERID," +
  2051. " t.MEMBERUSERID," +
  2052. " t.ACCOUNTSTATUS," +
  2053. " t.RECKONACCOUNTID," +
  2054. " to_char(t.CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME," +
  2055. " t.CREATORID," +
  2056. " to_char(t.AUDITTIME, 'yyyy-mm-dd hh24:mi:ss') AUDITTIME," +
  2057. " t.AUDITUSERID," +
  2058. " t.AUDITREMARK," +
  2059. " to_char(t.CANCELTIME, 'yyyy-mm-dd hh24:mi:ss') CANCELTIME," +
  2060. " t.CANCELUSERID," +
  2061. " to_char(t.MODIFYTIME, 'yyyy-mm-dd hh24:mi:ss') MODIFYTIME," +
  2062. " t.MODIFIERID," +
  2063. " t.MODIFYREMARK," +
  2064. " t.MAXINVESTORNUM," +
  2065. " t.HASAUTH," +
  2066. " t.REFERCOUNT," +
  2067. " t.REFERNUM," +
  2068. " t.ISANONYMOUS," +
  2069. " t.PARENTTOPUSER," +
  2070. " t.SUBACCOUNTLEVEL," +
  2071. " t.APPLYSRC," +
  2072. " t.AUDITSRC" +
  2073. " FROM USERACCOUNT t" +
  2074. " WHERE 1 = 1 and t.userid in(select rootuserid from useraccount where userid=%v)"
  2075. sqlId.FormatParam(r.USERID)
  2076. return sqlId.String()
  2077. }
  2078. // GetDataEx 获取顶级用户
  2079. func (r *Ermcp3RootUserAccount) GetDataEx() (interface{}, error) {
  2080. sData := make([]Ermcp3RootUserAccount, 0)
  2081. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2082. for i := range sData {
  2083. sData[i].calc()
  2084. }
  2085. return sData, err
  2086. }
  2087. // Ermcp3PaAreaSubject 交易主体
  2088. type Ermcp3PaAreaSubject struct {
  2089. SUBJECTID int64 `json:"subjectid" xorm:"SUBJECTID" form:"subjectid"` // 主体ID(SEQ_ERMCP_PA_AREASUBJECT)
  2090. SUBJECTTYPE int32 `json:"subjecttype" xorm:"SUBJECTTYPE"` // 主体类型 - 1:交易主体 2:业务部门
  2091. SUBJECTNAME string `json:"subjectname" xorm:"SUBJECTNAME"` // 主体名称
  2092. PARENTSUBJECTID string `json:"parentsubjectid" xorm:"PARENTSUBJECTID" form:"parentsubjectid"` // 上级主体ID
  2093. AREAUSERID int64 `json:"areauserid" xorm:"AREAUSERID" form:"userid"` // 所属机构
  2094. CREATETIME string `json:"createtime" xorm:"CREATETIME"` // 创建时间
  2095. CREATORSRC int32 `json:"creatorsrc" xorm:"CREATORSRC"` // 创建人来源 - 1:管理端 2:终端 3:交易
  2096. CREATORID string `json:"creatorid" xorm:"CREATORID" form:"creatorid"` // 创建人ID
  2097. MODIFIERSRC int32 `json:"modifiersrc" xorm:"MODIFIERSRC"` // 修改人来源 - 1:管理端 2:终端
  2098. MODIFIERID string `json:"modifierid" xorm:"MODIFIERID" form:"modifierid"` // 修改人
  2099. MODIFYTIME string `json:"modifytime" xorm:"MODIFYTIME"` // 修改时间
  2100. SUBJECTSTATUS int32 `json:"subjectstatus" xorm:"SUBJECTSTATUS" form:"subjectstatus"` // 主体状态 - 1:正常 2: 停用
  2101. CANMODIFY int32 `json:"canmodify" xorm:"CANMODIFY"` // 是否可修改 - 0:否 1:是
  2102. REMARK string `json:"remark" xorm:"REMARK"` // 备注
  2103. }
  2104. func (r *Ermcp3PaAreaSubject) calc() {
  2105. }
  2106. func (r *Ermcp3PaAreaSubject) buildSql() string {
  2107. var sqlId utils.SQLVal = `
  2108. SELECT t.SUBJECTID,
  2109. t.SUBJECTTYPE,
  2110. t.SUBJECTNAME,
  2111. t.PARENTSUBJECTID,
  2112. t.AREAUSERID,
  2113. to_char(t.CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME,
  2114. t.CREATORSRC,
  2115. t.CREATORID,
  2116. t.MODIFIERSRC,
  2117. t.MODIFIERID,
  2118. to_char(t.MODIFYTIME, 'yyyy-mm-dd hh24:mi:ss') MODIFYTIME,
  2119. t.SUBJECTSTATUS,
  2120. t.CANMODIFY,
  2121. t.REMARK
  2122. FROM ERMCP_PA_AREASUBJECT t
  2123. WHERE 1 = 1
  2124. `
  2125. areaUserId := mtpcache.GetAreaUserId(r.AREAUSERID, 0)
  2126. sqlId.And("t.AREAUSERID", areaUserId)
  2127. sqlId.AndEx("t.SUBJECTID", r.SUBJECTID, r.SUBJECTID > 0)
  2128. sqlId.AndEx("t.SUBJECTTYPE", r.SUBJECTTYPE, r.SUBJECTTYPE > 0)
  2129. sqlId.AndEx("t.SUBJECTSTATUS", r.SUBJECTSTATUS, r.SUBJECTSTATUS > 0)
  2130. return sqlId.String()
  2131. }
  2132. // GetDataEx 获取交易主体
  2133. func (r *Ermcp3PaAreaSubject) GetDataEx() (interface{}, error) {
  2134. sData := make([]Ermcp3PaAreaSubject, 0)
  2135. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2136. for i := range sData {
  2137. sData[i].calc()
  2138. }
  2139. return sData, err
  2140. }
  2141. // Ermcp3PaAreaAuditCfg 审核设置
  2142. type Ermcp3PaAreaAuditCfg struct {
  2143. AREAUSERID int64 `json:"areauserid" xorm:"AREAUSERID" form:"userid"` // 用户ID
  2144. CUSTOMERAUDITFLAG int32 `json:"customerauditflag" xorm:"CUSTOMERAUDITFLAG"` // 客户资料是否需要审核 - 0:不需审核 1:需要审核
  2145. SPOTCONTRACTAUDITFLAG int32 `json:"spotcontractauditflag" xorm:"SPOTCONTRACTAUDITFLAG"` // 现货合同是否需要审核 - 0:不需审核 1:需要审核
  2146. POINTPRICEAUDITFLAG int32 `json:"pointpriceauditflag" xorm:"POINTPRICEAUDITFLAG"` // 点价是否需要审核 - 0:不需审核 1:需要审核
  2147. DELIVERYAUDITFLAG int32 `json:"deliveryauditflag" xorm:"DELIVERYAUDITFLAG"` // 交收是否需要审核 - 0:不需审核 1:需要审核
  2148. MONEYAUDITFLAG int32 `json:"moneyauditflag" xorm:"MONEYAUDITFLAG"` // 款项是否需要审核 - 0:不需审核 1:需要审核
  2149. INVOICEAUDITFLAG int32 `json:"invoiceauditflag" xorm:"INVOICEAUDITFLAG"` // 发票是否需要审核 - 0:不需审核 1:需要审核
  2150. BUYINAUDITFLAG int32 `json:"buyinauditflag" xorm:"BUYINAUDITFLAG"` // 采购入库是否需要审核 - 0:不需审核 1:需要审核
  2151. SELLOUTAUDITFLAG int32 `json:"selloutauditflag" xorm:"SELLOUTAUDITFLAG"` // 销售出库是否需要审核 - 0:不需审核 1:需要审核
  2152. PRODUCEINAUDITFLAG int32 `json:"produceinauditflag" xorm:"PRODUCEINAUDITFLAG"` // 生产入库是否需要审核 - 0:不需审核 1:需要审核
  2153. PRODUCEOUTAUDITFLAG int32 `json:"produceoutauditflag" xorm:"PRODUCEOUTAUDITFLAG"` // 生产出库是否需要审核 - 0:不需审核 1:需要审核
  2154. OPERATORSRC int32 `json:"operatorsrc" xorm:"OPERATORSRC"` // 操作来源 - 1:管理端 2:终端
  2155. OPERATORID int64 `json:"operatorid" xorm:"OPERATORID" form:"operatorid"` // 操作人
  2156. UPDATETIME string `json:"updatetime" xorm:"UPDATETIME"` // 更新时间
  2157. }
  2158. func (r *Ermcp3PaAreaAuditCfg) calc() {
  2159. }
  2160. func (r *Ermcp3PaAreaAuditCfg) buildSql() string {
  2161. var sqlId utils.SQLVal = `
  2162. SELECT t.AREAUSERID,
  2163. t.CUSTOMERAUDITFLAG,
  2164. t.SPOTCONTRACTAUDITFLAG,
  2165. t.POINTPRICEAUDITFLAG,
  2166. t.DELIVERYAUDITFLAG,
  2167. t.MONEYAUDITFLAG,
  2168. t.INVOICEAUDITFLAG,
  2169. t.BUYINAUDITFLAG,
  2170. t.SELLOUTAUDITFLAG,
  2171. t.PRODUCEINAUDITFLAG,
  2172. t.PRODUCEOUTAUDITFLAG,
  2173. t.OPERATORSRC,
  2174. t.OPERATORID,
  2175. to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME
  2176. FROM ERMCP_PA_AREAAUDITCONFIG t
  2177. WHERE 1 = 1
  2178. `
  2179. areaUserId := mtpcache.GetAreaUserId(r.AREAUSERID, 0)
  2180. sqlId.And("t.AREAUSERID", areaUserId)
  2181. return sqlId.String()
  2182. }
  2183. // GetDataEx 获取审核设置
  2184. func (r *Ermcp3PaAreaAuditCfg) GetDataEx() (interface{}, error) {
  2185. sData := make([]Ermcp3PaAreaAuditCfg, 0)
  2186. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2187. for i := range sData {
  2188. sData[i].calc()
  2189. }
  2190. return sData, err
  2191. }
  2192. // XHDeliveryGoods 商品信息-现货品种
  2193. type XHDeliveryGoods struct {
  2194. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'" form:"areauserid"` // 所属机构
  2195. DELIVERYGOODSID int64 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'" form:"deliverygoodsid"` // 现货品种ID(SEQ_DELIVERYGOODS)
  2196. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  2197. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  2198. ISVALID int32 `json:"isvalid" xorm:"'ISVALID'"` // 是否有效 - 0:无效 1:有效
  2199. DGSTATUS int32 `json:"dgstatus" xorm:"'DGSTATUS'"` // 品种状态 - 作废 - 0:未激活 1:正常
  2200. REMARK string `json:"remark" xorm:"'REMARK'"` // 备注
  2201. UNITID int64 `json:"unitid" xorm:"'UNITID'"` // 单位id(目前看,数据库中是空值)
  2202. WDLST []XHWrstandard `json:"wdlst"` // 品类(现货商品)列表
  2203. BDLST []XHBRAND `json:"bdlst"` // 品牌列表
  2204. }
  2205. func (r *XHDeliveryGoods) calc() {
  2206. if r.WDLST == nil {
  2207. r.WDLST = make([]XHWrstandard, 0)
  2208. r.BDLST = make([]XHBRAND, 0)
  2209. }
  2210. }
  2211. func (r *XHDeliveryGoods) buildSql() string {
  2212. var sqlId utils.SQLVal = `
  2213. SELECT t.AREAUSERID,
  2214. t.DELIVERYGOODSID,
  2215. t.DELIVERYGOODSCODE,
  2216. t.DELIVERYGOODSNAME,
  2217. t.ISVALID,
  2218. t.DGSTATUS,
  2219. t.GOODSUNITID UNITID,
  2220. t.REMARK
  2221. FROM DELIVERYGOODS t
  2222. WHERE 1 = 1
  2223. `
  2224. sqlId.AndEx("t.AREAUSERID", r.AREAUSERID, r.AREAUSERID > 0)
  2225. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  2226. return sqlId.String()
  2227. }
  2228. // GetDataEx 获取商品信息-现货品种
  2229. func (r *XHDeliveryGoods) GetDataEx() (interface{}, error) {
  2230. sData := make([]XHDeliveryGoods, 0)
  2231. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2232. for i := range sData {
  2233. sData[i].calc()
  2234. }
  2235. if len(sData) > 0 {
  2236. // 获取品类列表
  2237. m1 := XHWrstandard{AREAUSERID: r.AREAUSERID, DELIVERYGOODSID: r.DELIVERYGOODSID}
  2238. if d1, err := m1.GetDataEx(); err == nil {
  2239. lstWd := d1.([]XHWrstandard)
  2240. for i := range sData {
  2241. sData[i].addWdlst(lstWd)
  2242. }
  2243. }
  2244. // 获取品牌列表
  2245. m2 := XHBRAND{DELIVERYGOODSID: r.DELIVERYGOODSID, AREAUSERID: r.AREAUSERID}
  2246. if d2, err := m2.GetDataEx(); err == nil {
  2247. lstBrand := d2.([]XHBRAND)
  2248. for i := range sData {
  2249. sData[i].addBrandlst(lstBrand)
  2250. }
  2251. }
  2252. }
  2253. return sData, err
  2254. }
  2255. // 增加品类列表
  2256. func (r *XHDeliveryGoods) addWdlst(lstWd []XHWrstandard) {
  2257. for i := range lstWd {
  2258. if lstWd[i].DELIVERYGOODSID == r.DELIVERYGOODSID {
  2259. r.WDLST = append(r.WDLST, lstWd[i])
  2260. }
  2261. }
  2262. }
  2263. // 增加品牌列表
  2264. func (r *XHDeliveryGoods) addBrandlst(lstBrand []XHBRAND) {
  2265. for i := range lstBrand {
  2266. if lstBrand[i].DELIVERYGOODSID == r.DELIVERYGOODSID {
  2267. r.BDLST = append(r.BDLST, lstBrand[i])
  2268. }
  2269. }
  2270. }
  2271. // XHBRAND 现货品种/品牌
  2272. type XHBRAND struct {
  2273. BRANDID int64 `json:"brandid"` // 品牌id
  2274. BRANDNAME string `json:"brandname"` // 品牌名称
  2275. DELIVERYGOODSID int64 `json:"-"` // 品种ID
  2276. AREAUSERID int64 `json:"-"` // 用户id
  2277. }
  2278. // GetDataEx 获取现货品种/品牌
  2279. func (r *XHBRAND) GetDataEx() (interface{}, error) {
  2280. sData := make([]XHBRAND, 0)
  2281. m := Ermcp3Brand{AREAUSERID: r.AREAUSERID, DELIVERYGOODSID: r.DELIVERYGOODSID}
  2282. d, err := m.GetData()
  2283. if err == nil {
  2284. for _, v := range d {
  2285. sData = append(sData, XHBRAND{DELIVERYGOODSID: v.DELIVERYGOODSID, BRANDID: v.BRANDID, BRANDNAME: v.BRANDNAME})
  2286. }
  2287. }
  2288. return sData, err
  2289. }
  2290. // XHWrstandard 现货品种/品类
  2291. type XHWrstandard struct {
  2292. AREAUSERID int64 `json:"-" xorm:"'AREAUSERID'" form:"areauserid"` // 所属机构
  2293. DELIVERYGOODSID int64 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'" form:"deliverygoodsid"` // 品种ID
  2294. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID(SEQ_WRSTANDARD)
  2295. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货商品代码
  2296. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 现货商品名称
  2297. VATRATE float64 `json:"vatrate" xorm:"'VATRATE'"` // 增值税率
  2298. ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  2299. UNITID int64 `json:"unitid" xorm:"'UNITID'"` // 单位id
  2300. MGLST []XHMiddleGoods `json:"mglst"` // 套保品列表
  2301. }
  2302. func (r *XHWrstandard) calc() {
  2303. if r.MGLST == nil {
  2304. r.MGLST = make([]XHMiddleGoods, 0)
  2305. }
  2306. }
  2307. func (r *XHWrstandard) buildSql() string {
  2308. var sqlId utils.SQLVal = `
  2309. select t.areauserid,
  2310. t.deliverygoodsid,
  2311. t.wrstandardid,
  2312. t.wrstandardcode,
  2313. t.wrstandardname,
  2314. t.vatrate,
  2315. t.unitid,
  2316. e.enumdicname
  2317. from wrstandard t
  2318. left join enumdicitem e
  2319. on t.unitid = e.enumitemname
  2320. and e.enumdiccode = 'goodsunit'
  2321. where 1 = 1
  2322. `
  2323. sqlId.AndEx("t.AREAUSERID", r.AREAUSERID, r.AREAUSERID > 0)
  2324. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  2325. return sqlId.String()
  2326. }
  2327. // GetDataEx 获取现货品种/品类
  2328. func (r *XHWrstandard) GetDataEx() (interface{}, error) {
  2329. sData := make([]XHWrstandard, 0)
  2330. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2331. for i := range sData {
  2332. sData[i].calc()
  2333. }
  2334. if len(sData) > 0 {
  2335. // 获取套保品列表
  2336. m := XHMiddleGoods{AREAUSERID: r.AREAUSERID, WRSTANDARDID: r.WRSTANDARDID}
  2337. if d, err := m.GetDataEx(); err == nil {
  2338. lstMidd := d.([]XHMiddleGoods)
  2339. for i := range sData {
  2340. sData[i].addMgLst(lstMidd)
  2341. }
  2342. }
  2343. }
  2344. return sData, err
  2345. }
  2346. func (r *XHWrstandard) addMgLst(midd []XHMiddleGoods) {
  2347. for i := range midd {
  2348. if r.WRSTANDARDID == midd[i].WRSTANDARDID {
  2349. r.MGLST = append(r.MGLST, midd[i])
  2350. }
  2351. }
  2352. }
  2353. // XHMiddleGoods 现货品种/品类/套保品
  2354. type XHMiddleGoods struct {
  2355. AREAUSERID int64 `json:"-" xorm:"'AREAUSERID'" form:"areauserid"` // 所属机构
  2356. WRSTANDARDID int64 `json:"-" xorm:"'WRSTANDARDID'" form:"wrstandardid"` // 现货商品ID(SEQ_WRSTANDARD)
  2357. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 拆算系数
  2358. MIDDLEGOODSID int64 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'"` // 套保品种id
  2359. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保品种代码
  2360. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保品种名称
  2361. ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  2362. UNITID int64 `json:"unitid" xorm:"'UNITID'"` // 单位id
  2363. GOODSGROUPID int64 `json:"-" xorm:"'GOODSGROUPID'"` // 关联期货商品组id
  2364. GPLST []TBGoodsGroup // 期货品种列表
  2365. }
  2366. func (r *XHMiddleGoods) calc() {
  2367. r.GPLST = make([]TBGoodsGroup, 0)
  2368. }
  2369. func (r *XHMiddleGoods) buildSql() string {
  2370. var sqlId utils.SQLVal = `
  2371. select w.wrstandardid,
  2372. c.convertratio,
  2373. m.areauserid,
  2374. m.middlegoodsid,
  2375. m.middlegoodscode,
  2376. m.middlegoodsname,
  2377. m.goodsunitid unitid,
  2378. m.goodsgroupid,
  2379. e.enumdicname
  2380. from wrstandard w
  2381. inner join erms2_wrsconvertdetail c
  2382. on w.wrstandardid = c.wrstandardid
  2383. inner join erms_middlegoods m
  2384. on c.middlegoodsid = m.middlegoodsid
  2385. left join enumdicitem e
  2386. on m.goodsunitid = e.enumitemname
  2387. and e.enumdiccode = 'goodsunit'
  2388. where 1 = 1 and w.isvalid = 1
  2389. `
  2390. sqlId.AndEx("m.areauserid", r.AREAUSERID, r.AREAUSERID > 0)
  2391. sqlId.AndEx("w.WRSTANDARDID", r.WRSTANDARDID, r.WRSTANDARDID > 0)
  2392. return sqlId.String()
  2393. }
  2394. // GetDataEx 获取现货品种/品类/套保品
  2395. func (r *XHMiddleGoods) GetDataEx() (interface{}, error) {
  2396. sData := make([]XHMiddleGoods, 0)
  2397. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2398. for i := range sData {
  2399. sData[i].calc()
  2400. }
  2401. if len(sData) > 0 {
  2402. // 获取关联期货品种
  2403. m1 := TBGoodsGroup{}
  2404. if d1, err := m1.GetDataEx(); err == nil {
  2405. gpLst := d1.([]TBGoodsGroup)
  2406. for i := range sData {
  2407. sData[i].addGpLst(gpLst)
  2408. }
  2409. }
  2410. }
  2411. return sData, err
  2412. }
  2413. func (r *XHMiddleGoods) addGpLst(lst []TBGoodsGroup) {
  2414. for i := range lst {
  2415. if r.GOODSGROUPID == lst[i].DESTGOODSGROUPID {
  2416. r.GPLST = append(r.GPLST, lst[i])
  2417. }
  2418. }
  2419. }
  2420. // TBMiddleGoods 商品信息-套保品种
  2421. type TBMiddleGoods struct {
  2422. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'" form:"areauserid"` // 机构用户ID
  2423. MIDDLEGOODSID int64 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'" form:"middlegoodsid"` // 套保品种ID(SEQ_ERMS_MIDDLEGOODS)
  2424. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'MIDDLEGOODSCODE'"` // 套保品种代码
  2425. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'MIDDLEGOODSNAME'"` // 套保品种名称
  2426. NEEDHEDGERATIO float64 `json:"needhedgeratio" xorm:"'NEEDHEDGERATIO'"` // 套保比率
  2427. NEEDARBITRAGERATIO float64 `json:"needarbitrageratio" xorm:"'NEEDARBITRAGERATIO'"` // 套利比率
  2428. GOODSGROUPID int64 `json:"-" xorm:"'GOODSGROUPID'"` // 关联期货商品组id
  2429. ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  2430. GPLST []TBGoodsGroup // 期货品种列表
  2431. WDLST []TBWrstandard // 商品信息列表(现货品类)
  2432. }
  2433. func (r *TBMiddleGoods) calc() {
  2434. r.GPLST = make([]TBGoodsGroup, 0)
  2435. r.WDLST = make([]TBWrstandard, 0)
  2436. }
  2437. func (r *TBMiddleGoods) buildSql() string {
  2438. var sqlId utils.SQLVal = `
  2439. select t.areauserid,
  2440. t.middlegoodsid,
  2441. t.middlegoodscode,
  2442. t.middlegoodsname,
  2443. t.needhedgeratio,
  2444. t.needarbitrageratio,
  2445. t.goodsgroupid,
  2446. e.enumdicname
  2447. from erms_middlegoods t
  2448. left join enumdicitem e
  2449. on t.goodsunitid = e.enumitemname
  2450. and e.enumdiccode = 'goodsunit'
  2451. where 1 = 1 and t.isvalid=1
  2452. `
  2453. sqlId.AndEx("t.areauserid", r.AREAUSERID, r.AREAUSERID > 0)
  2454. sqlId.AndEx("t.middlegoodsid", r.MIDDLEGOODSID, r.MIDDLEGOODSID > 0)
  2455. return sqlId.String()
  2456. }
  2457. // GetDataEx 获取商品信息-套保品种
  2458. func (r *TBMiddleGoods) GetDataEx() (interface{}, error) {
  2459. sData := make([]TBMiddleGoods, 0)
  2460. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2461. for i := range sData {
  2462. sData[i].calc()
  2463. }
  2464. if len(sData) > 0 {
  2465. // 获取关联期货品种
  2466. m1 := TBGoodsGroup{}
  2467. if d1, err := m1.GetDataEx(); err == nil {
  2468. gpLst := d1.([]TBGoodsGroup)
  2469. for i := range sData {
  2470. sData[i].addGpLst(gpLst)
  2471. }
  2472. }
  2473. // 获取关联现货商品
  2474. m2 := TBWrstandard{}
  2475. if d2, err := m2.GetDataEx(); err == nil {
  2476. wdLst := d2.([]TBWrstandard)
  2477. for i := range sData {
  2478. sData[i].addWdLst(wdLst)
  2479. }
  2480. }
  2481. }
  2482. return sData, err
  2483. }
  2484. func (r *TBMiddleGoods) addGpLst(lst []TBGoodsGroup) {
  2485. for i := range lst {
  2486. if r.GOODSGROUPID == lst[i].DESTGOODSGROUPID {
  2487. r.GPLST = append(r.GPLST, lst[i])
  2488. }
  2489. }
  2490. }
  2491. func (r *TBMiddleGoods) addWdLst(lst []TBWrstandard) {
  2492. for i := range lst {
  2493. if r.MIDDLEGOODSID == lst[i].MIDDLEGOODSID {
  2494. r.WDLST = append(r.WDLST, lst[i])
  2495. }
  2496. }
  2497. }
  2498. // TBGoodsGroup 套保品种/期货品种
  2499. type TBGoodsGroup struct {
  2500. GOODSGROUPID int64 `json:"goodsgroupid" xorm:"'GOODSGROUPID'"` // 商品组ID(自增ID)
  2501. GOODSGROUPNAME string `json:"goodsgroupname" xorm:"'GOODSGROUPNAME'"` // 商品组名称
  2502. GOODUNITID int32 `json:"goodunitid" xorm:"'GOODUNITID'"` // 报价单位ID
  2503. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 折算系数
  2504. DESTGOODSGROUPID int64 `json:"-" xorm:"'DESTGOODSGROUPID'"` // 目标商品组id
  2505. EXEXCHANGECODE string `json:"exexchangecode" xorm:"'EXEXCHANGECODE'"` // 交易所代码
  2506. EXEXCHANGENAME string `json:"exexchangename" xorm:"'EXEXCHANGENAME'"` // 交易所名称
  2507. ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  2508. GDLST []TBGoods // 商品列表
  2509. }
  2510. func (r *TBGoodsGroup) calc() {
  2511. r.GDLST = make([]TBGoods, 0)
  2512. }
  2513. func (r *TBGoodsGroup) buildSql() string {
  2514. var sqlId utils.SQLVal = `
  2515. select t.goodsgroupid,
  2516. t.goodsgroupname,
  2517. t.goodunitid,
  2518. c.convertratio,
  2519. c.destgoodsgroupid,
  2520. ex.exexchangecode,
  2521. ex.exexchangename,
  2522. e.enumdicname
  2523. from goodsgroup t
  2524. inner join ermcp_ggconvertconfig c
  2525. on t.goodsgroupid = c.srcgoodsgroupid
  2526. left join externalexchange ex on t.exexchangeid=ex.autoid
  2527. left join enumdicitem e
  2528. on t.goodunitid = e.enumitemname
  2529. and e.enumdiccode = 'goodsunit'
  2530. where 1 = 1 and t.goodsgroupstatus=1
  2531. `
  2532. return sqlId.String()
  2533. }
  2534. // GetDataEx 获取套保品种/期货品种
  2535. func (r *TBGoodsGroup) GetDataEx() (interface{}, error) {
  2536. sData := make([]TBGoodsGroup, 0)
  2537. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2538. for i := range sData {
  2539. sData[i].calc()
  2540. }
  2541. if len(sData) > 0 {
  2542. // 获取关联商品
  2543. m := TBGoods{}
  2544. if d, err := m.GetDataEx(); err == nil {
  2545. gdLst := d.([]TBGoods)
  2546. for i := range sData {
  2547. sData[i].addGdLst(gdLst)
  2548. }
  2549. }
  2550. }
  2551. return sData, err
  2552. }
  2553. func (r *TBGoodsGroup) addGdLst(lst []TBGoods) {
  2554. for i := range lst {
  2555. if r.GOODSGROUPID == lst[i].GOODSGROUPID {
  2556. r.GDLST = append(r.GDLST, lst[i])
  2557. }
  2558. }
  2559. }
  2560. // TBGoods 套保品种/期货品种/期货合约
  2561. type TBGoods struct {
  2562. GOODSID int64 `json:"goodsid" xorm:"'GOODSID'"` // 商品ID(自增ID SEQ_GOODS)
  2563. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 商品代码(内部)
  2564. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 商品名称
  2565. GOODSGROUPID int64 `json:"-" xorm:"'GOODSGROUPID'"` // 所属商品组ID
  2566. AGREEUNIT float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约乘数
  2567. ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  2568. }
  2569. func (r *TBGoods) calc() {
  2570. }
  2571. func (r *TBGoods) buildSql() string {
  2572. var sqlId utils.SQLVal = `
  2573. select t.goodsid,
  2574. t.goodscode,
  2575. t.goodsname,
  2576. t.goodsgroupid,
  2577. t.agreeunit,
  2578. e.enumdicname
  2579. from goods t
  2580. left join enumdicitem e
  2581. on t.goodunitid = e.enumitemname
  2582. and e.enumdiccode = 'goodsunit'
  2583. where 1 = 1
  2584. and t.goodsstatus not in (4, 5, 6)
  2585. and t.goodsgroupid in
  2586. (select srcgoodsgroupid from ermcp_ggconvertconfig)
  2587. `
  2588. return sqlId.String()
  2589. }
  2590. // GetDataEx 获取套保品种/期货品种/期货合约
  2591. func (r *TBGoods) GetDataEx() (interface{}, error) {
  2592. sData := make([]TBGoods, 0)
  2593. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2594. for i := range sData {
  2595. sData[i].calc()
  2596. }
  2597. return sData, err
  2598. }
  2599. // TBWrstandard 套保品种/商品信息(现货商品)
  2600. type TBWrstandard struct {
  2601. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID(SEQ_WRSTANDARD)
  2602. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货商品代码
  2603. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 现货商品名称
  2604. DELIVERYGOODSID int64 `json:"-" xorm:"'DELIVERYGOODSID'"` // 品种ID
  2605. VATRATE float64 `json:"vatrate" xorm:"'VATRATE'"` // 增值税率
  2606. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 品种名称
  2607. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 折算系数
  2608. MIDDLEGOODSID int64 `json:"-" xorm:"'MIDDLEGOODSID'"` // 品种id
  2609. ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  2610. }
  2611. func (r *TBWrstandard) calc() {
  2612. }
  2613. func (r *TBWrstandard) buildSql() string {
  2614. var sqlId utils.SQLVal = `
  2615. select t.wrstandardid,
  2616. t.wrstandardcode,
  2617. t.wrstandardname,
  2618. t.deliverygoodsid,
  2619. t.vatrate,
  2620. g.deliverygoodsname,
  2621. c.convertratio,
  2622. c.middlegoodsid,
  2623. e.enumdicname
  2624. from wrstandard t
  2625. left join deliverygoods g
  2626. on t.deliverygoodsid = g.deliverygoodsid
  2627. inner join erms2_wrsconvertdetail c
  2628. on t.wrstandardid = c.wrstandardid
  2629. left join enumdicitem e
  2630. on t.unitid = e.enumitemname
  2631. and e.enumdiccode = 'goodsunit'
  2632. where 1 = 1 and t.isvalid=1
  2633. order by c.middlegoodsid
  2634. `
  2635. return sqlId.String()
  2636. }
  2637. // GetDataEx 获取套保品种/商品信息(现货商品)
  2638. func (r *TBWrstandard) GetDataEx() (interface{}, error) {
  2639. sData := make([]TBWrstandard, 0)
  2640. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  2641. for i := range sData {
  2642. sData[i].calc()
  2643. }
  2644. return sData, err
  2645. }