ermcp3.go 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  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. "fmt"
  9. "mtp2_if/db"
  10. "mtp2_if/mtpcache"
  11. "mtp2_if/utils"
  12. )
  13. // ErmcpDeliveryGoods 现货品种(交割品种)
  14. type ErmcpDeliveryGoods struct {
  15. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种ID(SEQ_DELIVERYGOODS)
  16. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  17. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  18. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODSUNITID'"` // 现货品种单位ID
  19. DELIVERYGOODSTYPE int32 `json:"deliverygoodstype" xorm:"'DELIVERYGOODSTYPE'"` // 现货品种类型: 1-整装不拆分 2-散装记录明细 3:整装拆分 4:散装不记录明细
  20. STANDARDQTY int32 `json:"standardqty" xorm:"'STANDARDQTY'"` // 标准数量(库位数量) [标准品特有]
  21. STANDARDQTYRANGE int32 `json:"standardqtyrange" xorm:"'STANDARDQTYRANGE'"` // 标准数量偏差范围 [标准品特有]
  22. AUDITFLAG int32 `json:"auditflag" xorm:"'AUDITFLAG'"` // 交割是否需要审核 - 0:不需要 1:需要审核 默认为0
  23. ISVALID int32 `json:"isvalid" xorm:"'ISVALID'"` // 是否有效 - 0:无效 1:有效
  24. AGREEUNIT int32 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约单位[散货时默认为1, 整装时默认为标准数量]
  25. QTYDECIMALPLACE int32 `json:"qtydecimalplace" xorm:"'QTYDECIMALPLACE'"` // 成交量小数位
  26. CATEGORYID int32 `json:"categoryid" xorm:"'CATEGORYID'"` // 类别ID(SEQ_WRCATEGORY)
  27. DGSTATUS int32 `json:"dgstatus" xorm:"'DGSTATUS'"` // 品种状态 - 作废 - 0:未激活 1:正常
  28. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  29. REMARK string `json:"remark" xorm:"'REMARK'"` // 备注
  30. EnumdicName string `json:"enumdicname"` // 现货品种单位名称
  31. ExcludeCfg int32 `json:"-"` // 排除已配置项 1-排除
  32. }
  33. func (r *ErmcpDeliveryGoods) calc() {
  34. r.EnumdicName = mtpcache.GetEnumDicitemName(r.GOODSUNITID)
  35. }
  36. func (r *ErmcpDeliveryGoods) buildSql() string {
  37. var sqlId utils.SQLVal = "SELECT t.DELIVERYGOODSID," +
  38. " t.DELIVERYGOODSCODE," +
  39. " t.DELIVERYGOODSNAME," +
  40. " t.GOODSUNITID," +
  41. " t.DELIVERYGOODSTYPE," +
  42. " t.STANDARDQTY," +
  43. " t.STANDARDQTYRANGE," +
  44. " t.AUDITFLAG," +
  45. " t.ISVALID," +
  46. " t.ISSPLIT," +
  47. " t.AGREEUNIT," +
  48. " t.QTYDECIMALPLACE," +
  49. " t.CATEGORYID," +
  50. " t.DGSTATUS," +
  51. " t.REMARK," +
  52. " t.AREAUSERID" +
  53. " FROM DELIVERYGOODS t" +
  54. " WHERE 1 = 1"
  55. sqlId.AndEx("t.AREAUSERID", r.AREAUSERID, r.AREAUSERID > 0)
  56. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  57. if r.ExcludeCfg == 1 {
  58. sqlId.Join(" and t.deliverygoodsid not in(select distinct deliverygoodsid from ermcp_bizgroupspotgoods)")
  59. }
  60. return sqlId.String()
  61. }
  62. // GetDataEx 获取现货商品(交割商品)
  63. func (r *ErmcpDeliveryGoods) GetDataEx() (interface{}, error) {
  64. return r.GetData()
  65. }
  66. // GetData
  67. func (r *ErmcpDeliveryGoods) GetData() ([]ErmcpDeliveryGoods, error) {
  68. sData := make([]ErmcpDeliveryGoods, 0)
  69. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  70. for i := range sData {
  71. sData[i].calc()
  72. }
  73. return sData, err
  74. }
  75. // ErmcpDGFactoryItem 品种选择项定义表
  76. type ErmcpDGFactoryItem struct {
  77. DGFACTORYITEMID int64 `json:"dgfactoryitemid" xorm:"'DGFACTORYITEMID'"` // 选择项ID(SEQ_DGFACTORYITEM)
  78. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种ID
  79. DGFACTORYITEMTYPEID int64 `json:"dgfactoryitemtypeid" xorm:"'DGFACTORYITEMTYPEID'"` // 要素项类型
  80. DGFACTORYITEMVALUE string `json:"dgfactoryitemvalue" xorm:"'DGFACTORYITEMVALUE'"` // 要素项值(类型为仓库时填写仓库名称)
  81. WAREHOUSEID int64 `json:"warehouseid" xorm:"'WAREHOUSEID'"` // 仓库ID(类型为仓库时填写)
  82. ISVALID int32 `json:"isvalid" xorm:"'ISVALID'"` // 是否有效 - 0:无效 1:有效
  83. ORDERINDEX int32 `json:"orderindex" xorm:"'ORDERINDEX'"` // 顺序
  84. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 用户id
  85. }
  86. func (r *ErmcpDGFactoryItem) buildSql() string {
  87. var sqlId utils.SQLVal = "SELECT DGFACTORYITEMID," +
  88. " t.DELIVERYGOODSID," +
  89. " to_char(t.DGFACTORYITEMTYPEID) DGFACTORYITEMTYPEID," +
  90. " t.DGFACTORYITEMVALUE," +
  91. " to_char(t.WAREHOUSEID) WAREHOUSEID," +
  92. " t.ISVALID," +
  93. " t.ORDERINDEX," +
  94. " g.areauserid" +
  95. " FROM DGFACTORYITEM t" +
  96. " left join deliverygoods g" +
  97. " on t.deliverygoodsid = g.deliverygoodsid" +
  98. " WHERE t.ISVALID = 1"
  99. sqlId.AndEx("g.areauserid", r.AREAUSERID, r.AREAUSERID > 0)
  100. sqlId.AndEx("t.DGFACTORYITEMID", r.DGFACTORYITEMID, r.DGFACTORYITEMID > 0)
  101. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  102. sqlId.AndEx("t.DGFACTORYITEMTYPEID", r.DGFACTORYITEMTYPEID, r.DGFACTORYITEMTYPEID > 0)
  103. sqlId.AndEx("t.ORDERINDEX", r.ORDERINDEX, r.ORDERINDEX > 0)
  104. return sqlId.String()
  105. }
  106. // GetDataEx 获取品种选择项定义
  107. func (r *ErmcpDGFactoryItem) GetDataEx() (interface{}, error) {
  108. return r.GetData()
  109. }
  110. // GetData
  111. func (r *ErmcpDGFactoryItem) GetData() (interface{}, error) {
  112. sData := make([]ErmcpDGFactoryItem, 0)
  113. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  114. return sData, err
  115. }
  116. // Ermcp3Brand 品牌(从品种选择项定义表中查)
  117. type Ermcp3Brand struct {
  118. BRANDID int64 `json:"brandid"` // 品牌id
  119. BRANDNAME string `json:"brandname"` // 品牌名称
  120. DELIVERYGOODSID int32 `json:"deliverygoodsid"` // 品种ID
  121. AREAUSERID int64 `json:"areauserid"` // 用户id
  122. }
  123. // GetDataEx
  124. func (r *Ermcp3Brand) GetDataEx() (interface{}, error) {
  125. return r.GetData()
  126. }
  127. // GetData 获取品牌数据
  128. func (r *Ermcp3Brand) GetData() ([]Ermcp3Brand, error) {
  129. m := ErmcpDGFactoryItem{
  130. DELIVERYGOODSID: r.DELIVERYGOODSID,
  131. DGFACTORYITEMTYPEID: 2,
  132. ORDERINDEX: 1,
  133. }
  134. sData := make([]Ermcp3Brand, 0)
  135. if d, err := m.GetDataEx(); err == nil {
  136. if dgLst, ok := d.([]ErmcpDGFactoryItem); ok {
  137. for _, val := range dgLst {
  138. sData = append(sData, Ermcp3Brand{BRANDID: val.DGFACTORYITEMID, AREAUSERID: val.AREAUSERID,
  139. BRANDNAME: val.DGFACTORYITEMVALUE, DELIVERYGOODSID: val.DELIVERYGOODSID})
  140. }
  141. }
  142. }
  143. return sData, nil
  144. }
  145. // ErmcpRelatedGoods 现货品种的关联交易商品
  146. type ErmcpRelatedGoods struct {
  147. GoodsId int `json:"goodsid" xorm:"'GoodsId'"` // 商品id
  148. GoodsCode string `json:"goodscode" xorm:"'GoodsCode'"` // 商品代码
  149. GoodsName string `json:"goodsname" xorm:"'GoodsName'"` // 商品名称
  150. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 品种ID
  151. AREAUSERID int64 `json:"-"` // 所属机构
  152. }
  153. func (r *ErmcpRelatedGoods) buildSql() string {
  154. var sqlId utils.SQLVal = "select distinct t.deliverygoodsid, g.goodsid, g.goodscode, g.goodsname" +
  155. " from deliverygoods t" +
  156. " inner join ERMS2_WRSConvertDetail w" +
  157. " on t.deliverygoodsid = w.deliverygoodsid" +
  158. " inner join erms_middlegoods m" +
  159. " on w.middlegoodsid = m.middlegoodsid" +
  160. " inner join ERMCP_GGConvertConfig gc" +
  161. " on m.goodsgroupid = gc.destgoodsgroupid" +
  162. " inner join goods g" +
  163. " on gc.srcgoodsgroupid = g.goodsgroupid" +
  164. " where 1 = 1"
  165. sqlId.AndEx("t.AREAUSERID", r.AREAUSERID, r.AREAUSERID > 0)
  166. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  167. return sqlId.String()
  168. }
  169. // GetDataEx 获取现货关联交易商品
  170. func (r *ErmcpRelatedGoods) GetDataEx() (interface{}, error) {
  171. return r.GetData()
  172. }
  173. // GetData 获取现货关联交易商品
  174. func (r *ErmcpRelatedGoods) GetData() ([]ErmcpRelatedGoods, error) {
  175. sData := make([]ErmcpRelatedGoods, 0)
  176. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  177. return sData, err
  178. }
  179. // ErmcpDeliveryGoodsDetail 现货商品详情
  180. type ErmcpDeliveryGoodsDetail struct {
  181. Data ErmcpDeliveryGoods `json:"data"` // 现货商品基本信息(交割品种)
  182. GmList []ErmcpWrstandard `json:"gmlist"` // 品类列表(仓单标准)
  183. GbList []Ermcp3Brand `json:"gblist"` // 品牌列表
  184. WrsList []WRSConverTDetail `json:"wrslist"` // 套保信息列表
  185. GoodsList []ErmcpRelatedGoods `json:"goodslist"` // 关联交易商品列表
  186. QryTradeGoods int32 `json:"-"` // 是否查询关联交易商品 1-查询
  187. }
  188. func (r *ErmcpDeliveryGoodsDetail) addGmList(lst []ErmcpWrstandard) {
  189. for i := range lst {
  190. if lst[i].DELIVERYGOODSID == r.Data.DELIVERYGOODSID {
  191. r.GmList = append(r.GmList, lst[i])
  192. }
  193. }
  194. }
  195. func (r *ErmcpDeliveryGoodsDetail) addGbList(lst []Ermcp3Brand) {
  196. for i := range lst {
  197. if lst[i].DELIVERYGOODSID == r.Data.DELIVERYGOODSID {
  198. r.GbList = append(r.GbList, lst[i])
  199. }
  200. }
  201. }
  202. func (r *ErmcpDeliveryGoodsDetail) addWrsList(lst []WRSConverTDetail) {
  203. for i := range lst {
  204. if lst[i].DELIVERYGOODSID == r.Data.DELIVERYGOODSID {
  205. r.WrsList = append(r.WrsList, lst[i])
  206. }
  207. }
  208. }
  209. func (r *ErmcpDeliveryGoodsDetail) addGoodsList(lst []ErmcpRelatedGoods) {
  210. for i := range lst {
  211. if lst[i].DELIVERYGOODSID == r.Data.DELIVERYGOODSID {
  212. r.GoodsList = append(r.GoodsList, lst[i])
  213. }
  214. }
  215. }
  216. // GetDataEx
  217. func (r *ErmcpDeliveryGoodsDetail) GetDataEx() (interface{}, error) {
  218. sData := make([]ErmcpDeliveryGoodsDetail, 0)
  219. m := ErmcpDeliveryGoods{AREAUSERID: r.Data.AREAUSERID, DELIVERYGOODSID: r.Data.DELIVERYGOODSID}
  220. data, err := m.GetData()
  221. if err != nil {
  222. return sData, err
  223. }
  224. if data == nil || len(data) == 0 {
  225. return sData, nil
  226. }
  227. // 基本信息
  228. for i := range data {
  229. val := ErmcpDeliveryGoodsDetail{Data: data[i],
  230. GmList: make([]ErmcpWrstandard, 0), GbList: make([]Ermcp3Brand, 0),
  231. WrsList: make([]WRSConverTDetail, 0), GoodsList: make([]ErmcpRelatedGoods, 0),
  232. }
  233. sData = append(sData, val)
  234. }
  235. //品类/品牌/套保信息
  236. gm := ErmcpWrstandard{AREAUSERID: int(r.Data.AREAUSERID), DELIVERYGOODSID: r.Data.DELIVERYGOODSID, ISVALID: 1}
  237. gb := Ermcp3Brand{DELIVERYGOODSID: r.Data.DELIVERYGOODSID}
  238. WrsC := WRSConverTDetail{DELIVERYGOODSID: r.Data.DELIVERYGOODSID}
  239. gmLst, _ := gm.GetData()
  240. gbLst, _ := gb.GetData()
  241. wrsLst, _ := WrsC.GetData()
  242. var goodLst []ErmcpRelatedGoods
  243. if r.QryTradeGoods == 1 {
  244. goodsM := ErmcpRelatedGoods{AREAUSERID: r.Data.AREAUSERID, DELIVERYGOODSID: r.Data.DELIVERYGOODSID}
  245. goodLst, _ = goodsM.GetData()
  246. }
  247. for i := range sData {
  248. sData[i].addGmList(gmLst)
  249. sData[i].addGbList(gbLst)
  250. sData[i].addWrsList(wrsLst)
  251. sData[i].addGoodsList(goodLst)
  252. }
  253. return sData, nil
  254. }
  255. // Ermcp3Contract 现货合同
  256. type Ermcp3Contract struct {
  257. SPOTCONTRACTID string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` // 现货合同ID(602+Unix秒时间戳(10位)+xxxxxx)
  258. CONTRACTNO string `json:"contractno" xorm:"'CONTRACTNO'"` // 现货合同编号
  259. CONTRACTTYPE int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 现货合同类型 - 1:采购 -1:销售
  260. USERID int64 `json:"userid" xorm:"'USERID'"` // 所属机构ID
  261. BUYUSERID int64 `json:"buyuserid" xorm:"'BUYUSERID'"` // 采购方ID
  262. BUYUSERName string `json:"buyusername" xorm:"'BUYUSERName'"` // 采购方名称
  263. SELLUSERID int64 `json:"selluserid" xorm:"'SELLUSERID'"` // 销售方ID
  264. SELLUSERNAME string `json:"sellusername" xorm:"'SELLUSERNAME'"` // 销售方名称
  265. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种ID
  266. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'wrstandardid'"` // 品类ID
  267. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  268. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  269. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  270. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  271. PRODUCTTYPE int32 `json:"producttype" xorm:"'PRODUCTTYPE'"` // 产品类型 - 1:标准仓单 2:等标 3:非标
  272. CONVERTFACTOR float64 `json:"convertfactor" xorm:"'CONVERTFACTOR'"` // 标仓系数(品类)
  273. SPOTGOODSDESC string `json:"spotgoodsdesc" xorm:"'SPOTGOODSDESC'"` // 商品型号
  274. PRICETYPE int32 `json:"pricetype" xorm:"'PRICETYPE'"` // 定价类型 - 1:一口价 2:点价 3:暂定价
  275. QTY float64 `json:"qty" xorm:"'QTY'"` // 数量
  276. PRICE float64 `json:"price" xorm:"'PRICE'"` // 价格\暂定价 [1:一口价、3:暂定价]
  277. AMOUNT float64 `json:"amount" xorm:"'AMOUNT'"` // 金额 [1:一口价、3:暂定价]
  278. DELIVERYSTARTDATE string `json:"deliverystartdate" xorm:"'DELIVERYSTARTDATE'"` // 交收期(开始)
  279. DELIVERYENDDATE string `json:"deliveryenddate" xorm:"'DELIVERYENDDATE'"` // 交收期(结束)
  280. GOODSID int32 `json:"goodsid" xorm:"'GOODSID'"` // 点价合约ID - 0:为现货,其它为期货商品合约ID [2:点价 3:暂定价]
  281. GOODSCODE string `json:"goodscode" xorm:"'GOODSCODE'"` // 点价合约代码
  282. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 点价商品名称
  283. PRICEMOVE float64 `json:"pricemove" xorm:"'PRICEMOVE'"` // 升贴水 [2:点价 3:暂定价]
  284. STARTDATE string `json:"startdate" xorm:"'STARTDATE'"` // 点价开始日期 [2:点价 3:暂定价]
  285. ENDDATE string `json:"enddate" xorm:"'ENDDATE'"` // 点价结束日期 [2:点价 3:暂定价]
  286. MARGIN float64 `json:"margin" xorm:"'MARGIN'"` // 当前保证金
  287. CONTRACTMARGIN float64 `json:"contractmargin" xorm:"'CONTRACTMARGIN'"` // 合同保证金
  288. EnumdicName string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  289. CONTRACCTSTATUS int32 `json:"contracctstatus" xorm:"'CONTRACTSTATUS'"` // 合同状态 - 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
  290. Remark string `json:"remark" xorm:"'Remark'"` // 备注
  291. AUDITREMARK string `json:"auditremark" xorm:"'AUDITREMARK'"` // 审核意见
  292. AUDITTIME string `json:"audittime" xorm:"'AUDITTIME'"` // 审核时间
  293. CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  294. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  295. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID(DGFactoryItem表的ID)
  296. BRANDNAME string `json:"brandname" xorm:"'brandname'"` // 品牌名称
  297. ATTACHMENT string `json:"attachment" xorm:"'ATTACHMENT'"` // 附件
  298. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 单位id(取品类上的单位id)
  299. TRADEUSERID int64 `json:"tradeuserid" xorm:"'TRADEUSERID'"` // 交易员id
  300. SALEUSERID int64 `json:"saleuserid" xorm:"'SALEUSERID'"` // 销售员id
  301. MERUSERID int64 `json:"meruserid" xorm:"'MERUSERID'"` // 业务员id
  302. ACCOUNTID string `json:"accountid" xorm:"'ACCOUNTID'"` // 期货账户id
  303. BIZTYPE int32 `json:"biztype" xorm:"'BIZTYPE'"` // 业务类型 1-套保 2-套利
  304. SELLNICKNAME string `json:"sellnickname"` // 采购方昵称
  305. BUYNICKNAME string `json:"buynickname"` // 销售方昵称
  306. TRADEUSERLOGINCODE string `json:"tradeuserlogincode"` // 交易员登录代码
  307. SALEUSERLOGINCODE string `json:"saleuserlogincode"` // 销售员登录代码
  308. MERUSERLOGINCODE string `json:"meruserlogincode"` // 业务员登录代码
  309. // 筛选条件
  310. QryType int32 `json:"-"` // 查询类型 1-未提交 2-待审核 3-履约中 4-已完成
  311. UserType int32 `json:"-"` // 用户类型 2-管理员 7-企业成员
  312. OwnUserId int64 `json:"-"` // 登录用户id
  313. }
  314. func (r *Ermcp3Contract) getQryTypeStatus() string {
  315. var status string
  316. switch r.QryType {
  317. case 1: // 未提交
  318. status = "0,4,6"
  319. case 2: // 待审核
  320. status = "1"
  321. case 3: // 履约中
  322. status = "2"
  323. case 4: // 已完成
  324. status = "3,5"
  325. }
  326. return status
  327. }
  328. func (r *Ermcp3Contract) calc() {
  329. r.BUYUSERName, r.BUYNICKNAME = mtpcache.GetUserNameAndNickName(r.BUYUSERID)
  330. r.SELLUSERNAME, r.SELLNICKNAME = mtpcache.GetUserNameAndNickName(r.SELLUSERID)
  331. r.EnumdicName = mtpcache.GetEnumDicitemName(r.UNITID)
  332. r.TRADEUSERLOGINCODE = mtpcache.GetLoginCodeByUserId(r.TRADEUSERID)
  333. r.SALEUSERLOGINCODE = mtpcache.GetLoginCodeByUserId(r.SALEUSERID)
  334. r.MERUSERLOGINCODE = mtpcache.GetLoginCodeByUserId(r.MERUSERID)
  335. }
  336. func (r *Ermcp3Contract) buildSql() string {
  337. var sqlId utils.SQLVal = "SELECT to_char(t.SPOTCONTRACTID) SPOTCONTRACTID," +
  338. " t.CONTRACTNO," +
  339. " t.CONTRACTTYPE," +
  340. " t.USERID," +
  341. " t.BUYUSERID," +
  342. " t.SELLUSERID," +
  343. " t.DELIVERYGOODSID," +
  344. " t.wrstandardid," +
  345. " t.PRODUCTTYPE," +
  346. " w.CONVERTFACTOR," +
  347. " t.SPOTGOODSDESC," +
  348. " t.PRICETYPE," +
  349. " t.QTY," +
  350. " t.PRICE," +
  351. " t.AMOUNT," +
  352. " to_char(t.DELIVERYSTARTDATE, 'yyyy-mm-dd hh24:mi:ss') DELIVERYSTARTDATE," +
  353. " to_char(t.DELIVERYENDDATE, 'yyyy-mm-dd hh24:mi:ss') DELIVERYENDDATE," +
  354. " t.GOODSID," +
  355. " t.PRICEMOVE," +
  356. " to_char(t.STARTDATE, 'yyyy-mm-dd hh24:mi:ss') STARTDATE," +
  357. " to_char(t.ENDDATE, 'yyyy-mm-dd hh24:mi:ss') ENDDATE," +
  358. " t.MARGIN," +
  359. " t.contractmargin," +
  360. " t.remark," +
  361. " t.auditremark," +
  362. " to_char(t.contractattachment) attachment," +
  363. " t.contractstatus," +
  364. " t.SPOTGOODSBRANDID," +
  365. " to_char(t.audittime, 'yyyy-mm-dd hh24:mi:ss') audittime," +
  366. " to_char(t.createtime, 'yyyy-mm-dd hh24:mi:ss') createtime," +
  367. " to_char(t.updatetime, 'yyyy-mm-dd hh24:mi:ss') updatetime," +
  368. " g.deliverygoodscode," +
  369. " g.deliverygoodsname," +
  370. " g2.goodscode," +
  371. " g2.goodsname," +
  372. " gb.dgfactoryitemvalue brandname," +
  373. " w.wrstandardname," +
  374. " w.wrstandardcode," +
  375. " w.unitid," +
  376. " t.tradeuserid," +
  377. " t.saleuserid," +
  378. " t.meruserid," +
  379. " to_char(t.accountid) accountid," +
  380. " t.biztype" +
  381. " FROM ERMCP_SPOTCONTRACT t" +
  382. " left join deliverygoods g" +
  383. " on t.deliverygoodsid = g.deliverygoodsid" +
  384. " left join goods g2" +
  385. " on t.goodsid = g2.goodsid" +
  386. " left join DGFACTORYITEM gb" +
  387. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  388. " left join wrstandard w" +
  389. " on t.wrstandardid = w.wrstandardid" +
  390. " where 1 = 1"
  391. sqlId.AndEx("t.userid", r.USERID, r.USERID > 0)
  392. sqlId.AndEx("t.SPOTCONTRACTID", r.SPOTCONTRACTID, len(r.SPOTCONTRACTID) > 0)
  393. if r.UserType == 7 {
  394. // 企业成员
  395. sqlId.Join(fmt.Sprintf(" and %v in(t.saleuserid, t.tradeuserid, t.meruserid)", r.OwnUserId))
  396. }
  397. status := r.getQryTypeStatus()
  398. if len(status) > 0 {
  399. sqlId.Join(fmt.Sprintf(" and t.contractstatus in (%v)", status))
  400. }
  401. return sqlId.String()
  402. }
  403. // GetDataEx
  404. func (r *Ermcp3Contract) GetDataEx() (interface{}, error) {
  405. sData := make([]Ermcp3Contract, 0)
  406. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  407. for i := range sData {
  408. sData[i].calc()
  409. }
  410. return sData, err
  411. }
  412. // Ermcp3SellBuyContract 采购/销售合同
  413. type Ermcp3SellBuyContract struct {
  414. UserID int64 `json:"userid" xorm:"'UserID'"` // 机构ID
  415. SpotContractId string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` // 合同ID
  416. AccountName string `json:"accountname" xorm:"'ACCOUNTNAME'"` // 账户名称
  417. DeliveryGoodsId int `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货商品ID
  418. DeliveryGoodsCode string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  419. DeliveryGoodsName string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  420. SpotGoodsdesc string `json:"spotgoodsdesc" xorm:"'SPOTGOODSDESC'"` // 商品型号(商品规格)
  421. GoodsId int `json:"goodsid" xorm:"'GOODSID'"` // 点价商品ID
  422. GOODSNAME string `json:"goodsname" xorm:"'GOODSNAME'"` // 点价商品名称
  423. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'"` // 点价商品代码
  424. Pricemove float64 `json:"pricemove" xorm:"'PRICEMOVE'"` // 升贴水
  425. Qty float64 `json:"qty" xorm:"'QTY'"` // 合同量
  426. UnpricedQty float64 `json:"unpricedqty" xorm:"'UNPRICEDQTY'"` // 未定价量
  427. PricedQty float64 `json:"pricedqty" xorm:"'PRICEDQTY'"` // 已定价量
  428. UnsureQty float64 `json:"unsureqty" xorm:"'UNSUREQTY'"` // 未确定量
  429. PayAmount float64 `json:"payamount" xorm:"'PAYAMOUNT'"` // 已收付额(收款或付款)
  430. UnpayAmount float64 `json:"unpayamount"` // 应收付款额(应支付或应收款)
  431. PrePayAmount float64 `json:"prepayamount"` // 预收付额
  432. InvoiceAmount float64 `json:"invoiceamount" xorm:"'INVOICEAMOUNT'"` // 已开票额
  433. DaikaiAmount float64 `json:"daikaiamount" xorm:"'DAIKAIAMOUNT'"` // 应收(开)票额
  434. PreInvoiceAmount float64 `json:"preinvoiceamount"` // 预收(开)票额
  435. StartDate string `json:"startdate" xorm:"'STARTDATE'"` // 点价开始日
  436. EndDate string `json:"enddate" xorm:"'ENDDATE'"` // 点价结束日
  437. DeliveryStartDate string `json:"deliverystartdate" xorm:"'DELIVERYSTARTDATE'"` // 交割开始日
  438. DeliveryendDate string `json:"deliveryenddate" xorm:"'DELIVERYENDDATE'"` // 交割结束日
  439. Convertfactor float64 `json:"convertfactor" xorm:"'CONVERTFACTOR'"` // 标仓系数(品类)
  440. EnumdicName string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 单位名称
  441. Contracctstatus uint `json:"contracctstatus" xorm:"'CONTRACTSTATUS'"` // 合同状态- 0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
  442. PriceType int `json:"pricetype" xorm:"'PRICETYPE'"` // 定价类型 - 1:一口价 2:点价 3:暂定价
  443. ProductType int `json:"producttype" xorm:"'PRODUCTTYPE'"` // 产品类型 - 1:标准仓单 2:等标 3:非标
  444. Contracttype int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 合同类型 1-采购, -1-销售
  445. Pricedamount float64 `json:"pricedamount" xorm:"'PRICEDAMOUNT'"` // 已定价额
  446. PricedAvg float64 `json:"pricedavg" xorm:"'PRICEDAVG'"` // 已点均价
  447. Margin float64 `json:"margin" xorm:"'MARGIN'"` // 保证金
  448. CONTRACTMARGIN float64 `json:"contractmargin" xorm:"'CONTRACTMARGIN'"` // 合同保证金
  449. Remark string `json:"remark" xorm:"'Remark'"` // 备注
  450. ATTACHMENT string `json:"attachment" xorm:"'ATTACHMENT'"` // 附件
  451. ReckonRealQty float64 `json:"reckonrealqty" xorm:"'ReckonRealQty'"` // 已确定量
  452. ReckonOtherAmount float64 `json:"reckonotheramount" xorm:"'ReckonOtherAmount'"` // 其它费用
  453. ReckonAdjustAmount float64 `json:"reckonadjustamount" xorm:"'ReckonAdjustAmount'"` // 调整金额
  454. Price float64 `json:"price" xorm:"'Price'"` // 价格
  455. LoanAmount float64 `json:"loanamount" xorm:"'LoanAmount'"` // 贷款总额=已定价额+调整金额
  456. Contractno string `json:"contractno" xorm:"'Contractno'"` // 合同编号
  457. TotalAmount float64 `json:"totalamount" xorm:"'-'"` // 合计总额
  458. ReckonedAmount float64 `json:"reckonedamount" xorm:"'ReckonedAmount'"` // 实际已收付额(已确定额,已收付总额-已退款总额)
  459. BUYUSERID int64 `json:"-" xorm:"'BUYUSERID'"` // 采购方ID
  460. SELLUSERID int64 `json:"-" xorm:"'SELLUSERID'"` // 销售方ID
  461. AUDITTIME string `json:"audittime" xorm:"'AUDITTIME'"` // 审核时间
  462. CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  463. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID(DGFactoryItem表的ID)
  464. BRANDNAME string `json:"brandname" xorm:"'brandname'"` // 品牌名称
  465. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类ID
  466. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  467. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  468. // 筛选条件
  469. QryType int32 `json:"-"` // 查询类型 1-未提交 2-待审核 3-履约中 4-已完成
  470. UserType int32 `json:"-"` // 用户类型 2-管理员 7-企业成员
  471. OwnUserId int64 `json:"-"` // 登录用户id
  472. }
  473. func (r *Ermcp3SellBuyContract) calc() {
  474. r.TotalAmount = r.LoanAmount + r.ReckonOtherAmount
  475. r.UnpayAmount = r.TotalAmount - r.ReckonedAmount
  476. r.PrePayAmount = r.ReckonedAmount - r.TotalAmount
  477. r.PreInvoiceAmount = r.InvoiceAmount - r.TotalAmount
  478. if r.Contracttype == 1 {
  479. //采购合同, 取销售方名称
  480. r.AccountName = mtpcache.GetUserNameByUserId(r.SELLUSERID)
  481. } else {
  482. //销售合同, 取采购方名称
  483. r.AccountName = mtpcache.GetUserNameByUserId(r.BUYUSERID)
  484. }
  485. }
  486. func (r *Ermcp3SellBuyContract) buildSql() string {
  487. var sqlId utils.SQLVal = "select t.contractno," +
  488. " to_char(t.SpotContractId) SpotContractId," +
  489. " t.deliverygoodsid," +
  490. " t.spotgoodsdesc," +
  491. " t.goodsid," +
  492. " t.pricemove," +
  493. " t.userid," +
  494. " t.BUYUSERID," +
  495. " t.SELLUSERID," +
  496. " t.qty," +
  497. " t.qty - t.pricedqty unpricedqty," +
  498. " t.pricedqty," +
  499. " (case" +
  500. " when t.pricetype = 3 then" +
  501. " t.qty - t.RECKONREALQTY" +
  502. " else" +
  503. " t.pricedqty - t.ReckonRealQty" +
  504. " end) unsureqty," +
  505. " t.reckonedamount PayAmount," +
  506. " t.invoiceamount," +
  507. " (t.pricedamount + t.RECKONADJUSTAMOUNT + t.RECKONOTHERAMOUNT -" +
  508. " t.invoiceamount) as daikaiAmount," +
  509. " to_char(t.startdate, 'yyyy-mm-dd hh24:mi:ss') startdate," +
  510. " to_char(t.enddate, 'yyyy-mm-dd hh24:mi:ss') enddate," +
  511. " to_char(t.deliverystartdate, 'yyyy-mm-dd hh24:mi:ss') deliverystartdate," +
  512. " to_char(t.deliveryenddate, 'yyyy-mm-dd hh24:mi:ss') deliveryenddate," +
  513. " w.convertfactor," +
  514. " t.contractstatus," +
  515. " t.pricetype," +
  516. " t.producttype," +
  517. " t.contracttype," +
  518. " t.remark," +
  519. " to_char(t.contractattachment) attachment," +
  520. " t.margin," +
  521. " t.contractmargin," +
  522. " t.SPOTGOODSBRANDID," +
  523. " g.deliverygoodscode," +
  524. " g.deliverygoodsname," +
  525. " wr.wrstandardid," +
  526. " wr.wrstandardname," +
  527. " wr.wrstandardcode," +
  528. " g2.goodscode," +
  529. " g2.goodsname," +
  530. " e.enumdicname," +
  531. " t.contracttype," +
  532. " t.pricedamount," +
  533. " case" +
  534. " when t.pricedqty > 0 then" +
  535. " (t.pricedamount) / t.pricedqty" +
  536. " else" +
  537. " 0" +
  538. " end pricedAvg," +
  539. " t.ReckonRealQty," +
  540. " t.ReckonOtherAmount," +
  541. " t.ReckonAdjustAmount," +
  542. " t.Price," +
  543. " t.ReckonedAmount," +
  544. " to_char(t.audittime, 'yyyy-mm-dd hh24:mi:ss') audittime," +
  545. " to_char(t.createtime, 'yyyy-mm-dd hh24:mi:ss') createtime," +
  546. " t.pricedamount + t.ReckonAdjustAmount as LoanAmount," +
  547. " gb.dgfactoryitemvalue BRANDNAME" +
  548. " from ermcp_spotcontract t" +
  549. " left join deliverygoods g" +
  550. " on t.deliverygoodsid = g.deliverygoodsid" +
  551. " left join goods g2" +
  552. " on t.goodsid = g2.goodsid" +
  553. " left join wrstandard wr" +
  554. " on t.wrstandardid = wr.wrstandardid" +
  555. " left join DGFactoryItem gb" +
  556. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  557. " left join enumdicitem e" +
  558. " on wr.unitid = e.enumitemname" +
  559. " and e.enumdiccode = 'goodsunit'" +
  560. " where 1 = 1"
  561. sqlId.AndEx("t.userid", r.UserID, r.UserID > 0)
  562. sqlId.AndEx("t.contracttype", r.Contracttype, r.Contracttype != 0)
  563. sqlId.AndEx("t.SpotContractId", r.SpotContractId, len(r.SpotContractId) > 0)
  564. if r.UserType == 7 {
  565. // 企业成员
  566. sqlId.Join(fmt.Sprintf(" and %v in(t.saleuserid, t.tradeuserid, t.meruserid)", r.OwnUserId))
  567. }
  568. var tmp = Ermcp3Contract{QryType: r.QryType}
  569. status := tmp.getQryTypeStatus()
  570. if len(status) > 0 {
  571. sqlId.Join(fmt.Sprintf(" and t.contractstatus in (%v)", status))
  572. }
  573. return sqlId.String()
  574. }
  575. // GetDataEx
  576. func (r *Ermcp3SellBuyContract) GetDataEx() (interface{}, error) {
  577. sData := make([]Ermcp3SellBuyContract, 0)
  578. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  579. for i := range sData {
  580. sData[i].calc()
  581. }
  582. return sData, err
  583. }
  584. // Ermcp3ExposureDetail 敞口现货明细
  585. type Ermcp3ExposureDetail struct {
  586. Createtime string `json:"createtime" xorm:"'createtime'"` // 时间
  587. Areauserid int64 `json:"areauserid" xorm:"'areauserid'"` // 机构ID
  588. Logtype int32 `json:"logtype" xorm:"'logtype'"` // 类型 - 1:套保计划 2:现货合同
  589. Contracttype int32 `json:"contracttype" xorm:"'contracttype'"` // 现货合同类型 - 1:采购 -1:销售
  590. Qty float64 `json:"qty" xorm:"'qty'"` // 数量
  591. RelateNo string `json:"relateNo" xorm:"'relateNo'"` // 现货合同/套保计划编号
  592. Middlegoodsname string `json:"middlegoodsname" xorm:"'middlegoodsname'"` // 套保商品名称
  593. Middlegoodscode string `json:"middlegoodscode" xorm:"'middlegoodscode'"` // 套保商品代码
  594. MiddlegoodsId int32 `json:"middlegoodsId" xorm:"'middlegoodsId'"` // 套保商品id
  595. Unitid int32 `json:"-" xorm:"'unitid'"` // 现货商品单位ID
  596. Enumdicname string `json:"enumdicname" xorm:"'enumdicname'"` // 现货商品单位名称
  597. ChangeQty float64 `json:"changeQty" xorm:"'changeQty'"` // 套保变动量
  598. Convertfactor float64 `json:"convertfactor" xorm:"'convertfactor'"` // 标仓系数
  599. Convertratio float64 `json:"convertratio" xorm:"'convertratio'"` // 套保系数
  600. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种id
  601. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  602. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  603. }
  604. func (r *Ermcp3ExposureDetail) calc() {
  605. // 销售合同转换为负数
  606. if r.Logtype == 2 && r.Contracttype == -1 {
  607. r.ChangeQty *= -1
  608. r.Qty *= -1
  609. }
  610. // 采购计划 转换为负数
  611. if r.Logtype == 1 && r.Contracttype == 1 {
  612. r.ChangeQty *= -1
  613. r.Qty *= -1
  614. }
  615. }
  616. func (r *Ermcp3ExposureDetail) buildSql() string {
  617. var sqlId utils.SQLVal = "with tmp as" +
  618. " (select 2 as LogType," +
  619. " s.spotcontractid as relatedid," +
  620. " s.contractno as relateNo," +
  621. " s.qty" +
  622. " from ermcp_spotcontract s" +
  623. " union all" +
  624. " select 1," +
  625. " t.hedgeplanid as relateid," +
  626. " t.hedgeplanno as relateNo," +
  627. " t.planqty as qty" +
  628. " from ermcp_hedgeplan t)" +
  629. "select to_char(t.createtime, 'yyyy-mm-dd hh24:mi:ss') createtime," +
  630. " t.middlegoodsid," +
  631. " t.areauserid," +
  632. " t.logtype," +
  633. " t.contracttype," +
  634. " t.qty / t.convertfactor / t.convertratio as qty," +
  635. " t.convertfactor," +
  636. " t.convertratio," +
  637. " t.qty changeQty," +
  638. " tmp.relateNo," +
  639. " m.middlegoodsname," +
  640. " m.middlegoodscode," +
  641. " g.deliverygoodsid," +
  642. " g.deliverygoodscode," +
  643. " g.deliverygoodsname," +
  644. " g.goodsunitid unitid," +
  645. " e.enumdicname" +
  646. " from ermcp_spotexposurelog t" +
  647. " left join erms_middlegoods m" +
  648. " on t.middlegoodsid = m.middlegoodsid" +
  649. " left join deliverygoods g" +
  650. " on t.deliverygoodsid = g.deliverygoodsid" +
  651. " left join enumdicitem e" +
  652. " on g.goodsunitid = e.enumitemname" +
  653. " and e.enumdiccode = 'goodsunit'" +
  654. " left join tmp" +
  655. " on t.logtype = tmp.LogType" +
  656. " and t.relatedid = tmp.relatedid" +
  657. " where 1=1 and t.tradedate=to_char(sysdate, 'yyyymmdd')"
  658. sqlId.And("t.areauserid", r.Areauserid)
  659. sqlId.And("t.middlegoodsid", r.MiddlegoodsId)
  660. return sqlId.String()
  661. }
  662. // GetDataEx
  663. func (r *Ermcp3ExposureDetail) GetDataEx() (interface{}, error) {
  664. e := db.GetEngine()
  665. s := e.SQL(r.buildSql())
  666. sData := make([]Ermcp3ExposureDetail, 0)
  667. if err := s.Find(&sData); err != nil {
  668. return nil, err
  669. }
  670. for i := range sData {
  671. sData[i].calc()
  672. }
  673. return sData, nil
  674. }
  675. // Ermcp3AreaSpot 敞口现货头寸
  676. type Ermcp3AreaSpot struct {
  677. AREAUSERID int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  678. ORIBUYPLANQTY float64 `json:"-" xorm:"'ORIBUYPLANQTY'"` // 期初采购计划数量
  679. ORIBUYPRICEDQTY float64 `json:"-" xorm:"'ORIBUYPRICEDQTY'"` // 期初采购合同已定价数量
  680. ORISELLPLANQTY float64 `json:"-" xorm:"'ORISELLPLANQTY'"` // 期初销售计划数量
  681. ORISELLPRICEDQTY float64 `json:"-" xorm:"'ORISELLPRICEDQTY'"` // 期初销售合同已定价数量
  682. BUYPLANQTY float64 `json:"-" xorm:"'BUYPLANQTY'"` // 采购计划数量
  683. BUYPRICEDQTY float64 `json:"-" xorm:"'BUYPRICEDQTY'"` // 采购合同已定价数量
  684. SELLPLANQTY float64 `json:"-" xorm:"'SELLPLANQTY'"` // 销售计划数量
  685. SELLPRICEDQTY float64 `json:"-" xorm:"'SELLPRICEDQTY'"` // 销售合同已定价数量
  686. TOTALSPOTQTY float64 `json:"totalspotqty" xorm:"'TOTALSPOTQTY'"` // 当前数量(现货头寸总量) = (销售计划数量 - 销售已定价数量) - (采购计划数量 - 采购已定价数量)
  687. OriToalSpotQty float64 `json:"oritoalspotqty" xorm:"'OriToalSpotQty'"` // 昨日数量
  688. IncreaseQty float64 `json:"increaseqty" xorm:"'IncreaseQty'"` // 增加数量=销售计划数量+采购已定价数量
  689. DecreaseQty float64 `json:"decreaseqty" xorm:"'DecreaseQty'"` // 减少数量=-(销售已定价数量+采购计划数量)
  690. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  691. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种id
  692. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  693. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  694. }
  695. func (r *Ermcp3AreaSpot) calc() {
  696. /*
  697. 增加数量=今日销售计划数量+今日采购已定价数量
  698. 减少数量=-(今日销售已定价数量+今日采购计划数量)
  699. 其中,
  700. 今日销售计划数量=期末销售计划数量-期初销售计划数量
  701. 今日采购已定价数量=期末采购已定价数量-期初采购已定价数量
  702. 今日销售已定价数量=期末销售已定价数量-期初销售已定价数量
  703. 今日采购计划数量=期末采购计划数量-期初采购计划数量
  704. */
  705. r.IncreaseQty = r.SELLPLANQTY - r.ORISELLPLANQTY + r.BUYPRICEDQTY - r.ORIBUYPRICEDQTY
  706. r.DecreaseQty = (r.SELLPRICEDQTY - r.ORISELLPRICEDQTY + r.BUYPLANQTY - r.ORIBUYPLANQTY) * -1
  707. r.OriToalSpotQty = (r.ORISELLPLANQTY - r.ORISELLPRICEDQTY) - (r.ORIBUYPLANQTY - r.ORIBUYPRICEDQTY)
  708. }
  709. func (r *Ermcp3AreaSpot) buildSql() string {
  710. var sqlId utils.SQLVal = "select t.AREAUSERID," +
  711. " t.ORIBUYPLANQTY," +
  712. " t.ORIBUYPRICEDQTY," +
  713. " t.ORISELLPLANQTY," +
  714. " t.ORISELLPRICEDQTY," +
  715. " t.BUYPLANQTY," +
  716. " t.BUYPRICEDQTY," +
  717. " t.SELLPLANQTY," +
  718. " t.SELLPRICEDQTY," +
  719. " t.TOTALSPOTQTY," +
  720. " g.deliverygoodsid," +
  721. " g.deliverygoodscode," +
  722. " g.deliverygoodsname," +
  723. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME" +
  724. " from ermcp_areaspot t" +
  725. " left join deliverygoods g" +
  726. " on t.deliverygoodsid = g.deliverygoodsid" +
  727. " where 1 = 1"
  728. sqlId.And("t.areauserid", r.AREAUSERID)
  729. return sqlId.String()
  730. }
  731. // GetDataEx
  732. func (r *Ermcp3AreaSpot) GetDataEx() (interface{}, error) {
  733. e := db.GetEngine()
  734. s := e.SQL(r.buildSql())
  735. sData := make([]Ermcp3AreaSpot, 0)
  736. if err := s.Find(&sData); err != nil {
  737. return nil, err
  738. }
  739. for i := range sData {
  740. sData[i].calc()
  741. }
  742. return sData, nil
  743. }
  744. // Ermcp3AreaSpotDetail 敞口现货头寸明细
  745. type Ermcp3AreaSpotDetail struct {
  746. Relatedid string `json:"relatedid" xorm:"'relatedid'"` // 套保计划ID/现货合同ID
  747. Relatedno string `json:"relatedno" xorm:"'relatedno'"` // 编号
  748. LogType int32 `json:"logtype" xorm:"'logType'"` // 记录类型 1-套保 2-现货合同
  749. Contracttype int32 `json:"contracttype" xorm:"'contracttype'"` // 合同类型 1-采购 -1-销售
  750. Qty float64 `json:"qty" xorm:"'qty'"` // 数量
  751. Strtime string `json:"strtime" xorm:"'strtime'"` // 时间
  752. Enumdicname string `json:"enumdicname"` // 现货商品单位名称
  753. Recordname string `json:"recordname"` // 类型名称
  754. CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  755. Unitid int32 `json:"-" xorm:"'UNITID'"` // 单位ID
  756. UserId int `json:"-"` // 所属用户ID
  757. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种id
  758. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  759. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  760. }
  761. func (r *Ermcp3AreaSpotDetail) calc() {
  762. var logTypeName, contractTypeName string
  763. if r.LogType == 1 {
  764. logTypeName = "计划"
  765. } else {
  766. logTypeName = "合同"
  767. }
  768. if r.Contracttype == 1 {
  769. contractTypeName = "采购"
  770. } else {
  771. contractTypeName = "销售"
  772. }
  773. // 销售合同 数量转为负数
  774. if r.LogType == 2 && r.Contracttype == -1 {
  775. if r.Qty > 0 {
  776. r.Qty = r.Qty * -1
  777. }
  778. }
  779. // 采购计划 数量转为负数
  780. if r.LogType == 1 && r.Contracttype == 1 {
  781. if r.Qty > 0 {
  782. r.Qty = r.Qty * -1
  783. }
  784. }
  785. r.Recordname = contractTypeName + logTypeName
  786. r.Enumdicname = mtpcache.GetEnumDicitemName(r.Unitid)
  787. }
  788. func (r *Ermcp3AreaSpotDetail) buildSql() string {
  789. var sqlId utils.SQLVal = "with tmp as" +
  790. " (select to_char(t.hedgeplanid) relatedid," +
  791. " t.hedgeplanno relatedno," +
  792. " 1 as logType," +
  793. " t.contracttype" +
  794. " from ermcp_hedgeplan t" +
  795. " where t.areauserid = %v" +
  796. " and t.deliverygoodsid = %v" +
  797. " union all" +
  798. " select to_char(t.spotcontractid)," +
  799. " t.contractno," +
  800. " 2 as logType," +
  801. " t.contracttype" +
  802. " from ermcp_spotcontract t" +
  803. " where t.userid = %v" +
  804. " and t.deliverygoodsid = %v)" +
  805. "select t.relatedid," +
  806. " tmp.relatedno," +
  807. " t.LogType," +
  808. " tmp.contracttype," +
  809. " t.RealQty qty," +
  810. " to_char(t.createtime, 'yyyy-mm-dd hh24:mi:ss') createtime," +
  811. " g.deliverygoodsid," +
  812. " g.deliverygoodsname," +
  813. " g.deliverygoodscode," +
  814. " g.goodsunitid unitid" +
  815. " from ermcp_spotlog t" +
  816. " inner join tmp" +
  817. " on t.LogType = tmp.logType" +
  818. " and t.relatedid = tmp.relatedid" +
  819. " and t.areauserid = %v" +
  820. " and t.deliverygoodsid = %v" +
  821. " left join deliverygoods g" +
  822. " on t.deliverygoodsid = g.deliverygoodsid" +
  823. " where t.tradedate = to_char(sysdate, 'yyyymmdd')"
  824. sqlId.FormatParam(r.UserId, r.DELIVERYGOODSID, r.UserId, r.DELIVERYGOODSID, r.UserId, r.DELIVERYGOODSID)
  825. return sqlId.String()
  826. }
  827. // GetDataEx
  828. func (r *Ermcp3AreaSpotDetail) GetDataEx() (interface{}, error) {
  829. e := db.GetEngine()
  830. s := e.SQL(r.buildSql())
  831. sData := make([]Ermcp3AreaSpotDetail, 0)
  832. if err := s.Find(&sData); err != nil {
  833. return nil, err
  834. }
  835. for i := range sData {
  836. sData[i].calc()
  837. }
  838. return sData, nil
  839. }
  840. // Ermcp3Wrstandard 现货品类
  841. type Ermcp3Wrstandard struct {
  842. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类ID(SEQ_WRSTANDARD)
  843. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  844. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种ID
  845. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 品类单位ID
  846. MINIVALUE int32 `json:"minivalue" xorm:"'MINIVALUE'"` // 最小变动值
  847. MINIVALUEDP int32 `json:"minivaluedp" xorm:"'MINIVALUEDP'"` // 最小变动值小数位
  848. REALMINIVALUE int32 `json:"realminivalue" xorm:"'REALMINIVALUE'"` // 实际最小变动值
  849. REALMINIVALUEDP int32 `json:"realminivaluedp" xorm:"'REALMINIVALUEDP'"` // 实际最小变动值小数位
  850. CREATORID int64 `json:"creatorid" xorm:"'CREATORID'"` // 创建人
  851. CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  852. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  853. AREAUSERID int `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  854. EnumdicName string `json:"enumdicname"` // 品类单位名称
  855. ISVALID int32 `json:"isvalid" xorm:"'ISVALID'"` // 是否有效 0-无效(停用) 1-有效(正常)
  856. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  857. REMARK string `json:"remark" xorm:"'REMARK'"` // 备注
  858. }
  859. func (r *Ermcp3Wrstandard) calc() {
  860. r.EnumdicName = mtpcache.GetEnumDicitemName(r.UNITID)
  861. }
  862. func (r *Ermcp3Wrstandard) buildSql() string {
  863. str := "select WRSTANDARDID," +
  864. " WRSTANDARDCODE," +
  865. " DELIVERYGOODSID," +
  866. " UNITID," +
  867. " MINIVALUE," +
  868. " MINIVALUEDP," +
  869. " REALMINIVALUE," +
  870. " REALMINIVALUEDP," +
  871. " CREATORID," +
  872. " to_char(CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME," +
  873. " WRSTANDARDNAME," +
  874. " ISVALID," +
  875. " to_char(UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  876. " REMARK," +
  877. " AREAUSERID" +
  878. " from wrstandard t" +
  879. " where t.AREAUSERID=%v"
  880. sqlId := fmt.Sprintf(str, r.AREAUSERID)
  881. if r.DELIVERYGOODSID > 0 {
  882. sqlId = sqlId + fmt.Sprintf(" and DELIVERYGOODSID=%v", r.DELIVERYGOODSID)
  883. }
  884. sqlId += " order by createtime desc"
  885. return sqlId
  886. }
  887. // GetDataEx
  888. func (r *Ermcp3Wrstandard) GetDataEx() (interface{}, error) {
  889. e := db.GetEngine()
  890. s := e.SQL(r.buildSql())
  891. sData := make([]Ermcp3Wrstandard, 0)
  892. if err := s.Find(&sData); err != nil {
  893. return nil, err
  894. }
  895. for i := range sData {
  896. sData[i].calc()
  897. }
  898. return sData, nil
  899. }
  900. // Ermcp3AreaStockApply 出入库申请
  901. type Ermcp3AreaStockApply struct {
  902. INOUTAPPLYID string `json:"inoutapplyid" xorm:"'INOUTAPPLYID'"` // 申请ID(607+Unix秒时间戳(10位)+xxxxxx)
  903. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构ID
  904. INOUTTYPE int32 `json:"inouttype" xorm:"'INOUTTYPE'"` // 出入库类型 - 1:采购入库 2:销售出库 3:生产入库 4:生产出库
  905. WRSTANDARDID string `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类ID
  906. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID
  907. SPOTCONTRACTID string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` // 关联现货合同ID
  908. WAREHOUSEINFOID string `json:"warehouseinfoid" xorm:"'WAREHOUSEINFOID'"` // 现货仓库ID
  909. QTY float64 `json:"qty" xorm:"'QTY'"` // 数量
  910. APPLYSTATUS int32 `json:"applystatus" xorm:"'APPLYSTATUS'"` // 申请状态 - 1:待审核 2:审核通过 3:审核拒绝 4:处理失败 5:已撤回
  911. APPLYSRC int32 `json:"applysrc" xorm:"'APPLYSRC'"` // 申请来源 - 1:管理端 2:终端
  912. APPLYID int64 `json:"applyid" xorm:"'APPLYID'"` // 申请人
  913. APPLYREMARK string `json:"applyremark" xorm:"'APPLYREMARK'"` // 申请备注
  914. APPLYTIME string `json:"applytime" xorm:"'APPLYTIME'"` // 申请时间
  915. AUDITSRC int32 `json:"auditsrc" xorm:"'AUDITSRC'"` // 审核来源 - 1:管理端 2:终端
  916. AUDITID int64 `json:"auditid" xorm:"'AUDITID'"` // 审核人
  917. AUDITTIME string `json:"audittime" xorm:"'AUDITTIME'"` // 审核时间
  918. AUDITREMARK string `json:"auditremark" xorm:"'AUDITREMARK'"` // 审核备注
  919. AUDITTRADEDATE string `json:"audittradedate" xorm:"'AUDITTRADEDATE'"` // 审核交易日(yyyyMMdd)
  920. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  921. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  922. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 单位id
  923. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  924. WAREHOUSENAME string `json:"warehousename" xorm:"'WAREHOUSENAME'"` // 仓库名称
  925. WAREHOUSECODE string `json:"warehousecode" xorm:"'WAREHOUSECODE'"` // 仓库代码
  926. WAREHOUSETYPE int32 `json:"warehousetype" xorm:"'WAREHOUSETYPE'"` // 仓库类型 - 1 厂库 2 自有库 3 合作库
  927. CONTRACTNO string `json:"contractno" xorm:"'CONTRACTNO'"` // 合同编号
  928. CONTRACTTYPE int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 现货合同类型 - 1:采购 -1:销售
  929. BUYUSERID int64 `json:"buyuserid" xorm:"'BUYUSERID'"` // 采购方userid
  930. SELLUSERID int64 `json:"selluserid" xorm:"'SELLUSERID'"` // 销售方userid
  931. PRICETYPE int32 `json:"pricetype" xorm:"'PRICETYPE'"` // 定价类型 - 1:一口价 2:点价 3:暂定价
  932. CONTRACTQTY float64 `json:"contractqty" xorm:"'CONTRACTQTY'"` // 合同量
  933. BUYUSERNAME string `json:"buyusername"` // 采购方名称
  934. SELLUSERNAME string `json:"sellusername"` // 销售方名称
  935. APPLYNAME string `json:"applyname"` // 申请人名称
  936. AUDITNAME string `json:"auditname"` // 审核人名称
  937. ENUMDICNAME string `json:"enumdicname"` // 现货商品单位名称
  938. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种id
  939. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  940. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  941. FilterStatus string `json:"-"` // 查询条件, 申请状态, 逗号隔开
  942. FilterType string `json:"-"` // 查询条件, 出入库类型, 逗号隔开
  943. BeginDate string `json:"-"` // 开始日期
  944. EndDate string `json:"-"` // 结束日期
  945. }
  946. func (r *Ermcp3AreaStockApply) calc() {
  947. // 采购方名称
  948. r.BUYUSERNAME = mtpcache.GetUserNameByUserId(r.BUYUSERID)
  949. // 销售方名称
  950. r.SELLUSERNAME = mtpcache.GetUserNameByUserId(r.SELLUSERID)
  951. // 申请人名称
  952. if r.APPLYSRC == 1 {
  953. r.APPLYNAME = mtpcache.GetSystemmangerLoginCode(r.APPLYID)
  954. } else {
  955. r.APPLYNAME = mtpcache.GetLoginCodeByLoginId(r.APPLYID)
  956. }
  957. // 审核人名称
  958. if r.AUDITSRC == 1 {
  959. r.AUDITNAME = mtpcache.GetSystemmangerLoginCode(r.AUDITID)
  960. } else {
  961. r.AUDITNAME = mtpcache.GetLoginCodeByLoginId(r.AUDITID)
  962. }
  963. // 单位名称
  964. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  965. }
  966. func (r *Ermcp3AreaStockApply) buildSql() string {
  967. var sqlId utils.SQLVal = "SELECT to_char(t.INOUTAPPLYID) INOUTAPPLYID," +
  968. " t.USERID," +
  969. " t.INOUTTYPE," +
  970. " t.WRSTANDARDID," +
  971. " t.SPOTGOODSBRANDID," +
  972. " t.DELIVERYGOODSID," +
  973. " to_char(t.SPOTCONTRACTID) SPOTCONTRACTID," +
  974. " to_char(t.WAREHOUSEINFOID) WAREHOUSEINFOID," +
  975. " t.QTY," +
  976. " t.APPLYSTATUS," +
  977. " t.APPLYSRC," +
  978. " t.APPLYID," +
  979. " t.APPLYREMARK," +
  980. " to_char(t.APPLYTIME, 'yyyy-mm-dd hh24:mi:ss') APPLYTIME," +
  981. " t.AUDITSRC," +
  982. " t.AUDITID," +
  983. " to_char(t.AUDITTIME, 'yyyy-mm-dd hh24:mi:ss') AUDITTIME," +
  984. " t.AUDITREMARK," +
  985. " t.AUDITTRADEDATE," +
  986. " w.wrstandardname," +
  987. " w.wrstandardcode," +
  988. " g.goodsunitid unitid," +
  989. " gb.dgfactoryitemvalue brandname," +
  990. " h.warehousename," +
  991. " h.warehousecode," +
  992. " h.warehousetype," +
  993. " s.contractno," +
  994. " s.contracttype," +
  995. " s.buyuserid," +
  996. " s.selluserid," +
  997. " s.pricetype," +
  998. " s.qty CONTRACTQTY," +
  999. " g.deliverygoodscode," +
  1000. " g.deliverygoodsname" +
  1001. " FROM ERMCP_AREAINOUTSTOCKAPPLY t" +
  1002. " left join wrstandard w" +
  1003. " on t.wrstandardid = w.wrstandardid" +
  1004. " left join dgfactoryitem gb" +
  1005. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1006. " left join deliverygoods g" +
  1007. " on t.deliverygoodsid = g.deliverygoodsid" +
  1008. " left join warehouseinfo h" +
  1009. " on t.warehouseinfoid = h.autoid" +
  1010. " left join ermcp_spotcontract s" +
  1011. " on t.spotcontractid = s.spotcontractid" +
  1012. " WHERE 1 = 1"
  1013. // 用户id(必要条件)
  1014. sqlId.And("t.USERID", r.USERID)
  1015. // 合同id
  1016. if len(r.SPOTCONTRACTID) > 0 {
  1017. sqlId.And("t.SPOTCONTRACTID", r.SPOTCONTRACTID)
  1018. }
  1019. // 现货品种id
  1020. if r.DELIVERYGOODSID > 0 {
  1021. sqlId.And("t.DELIVERYGOODSID", r.DELIVERYGOODSID)
  1022. }
  1023. // 品类id
  1024. if len(r.WRSTANDARDID) > 0 {
  1025. sqlId.And("t.WRSTANDARDID", r.WRSTANDARDID)
  1026. }
  1027. // 品牌id
  1028. if r.SPOTGOODSBRANDID > 0 {
  1029. sqlId.And("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID)
  1030. }
  1031. // 仓库id
  1032. if len(r.WAREHOUSEINFOID) > 0 {
  1033. sqlId.And("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID)
  1034. }
  1035. // 日期范围
  1036. if len(r.BeginDate) > 0 {
  1037. if r.BeginDate == r.EndDate {
  1038. sqlId.And("t.audittradedate", r.BeginDate)
  1039. } else if r.EndDate > r.BeginDate {
  1040. sqlId.BiggerOrEq("t.audittradedate", r.BeginDate)
  1041. sqlId.LessOrEq("t.audittradedate", r.EndDate)
  1042. }
  1043. }
  1044. // 出入库类型
  1045. if len(r.FilterType) > 0 {
  1046. if len(r.FilterType) == 1 {
  1047. sqlId.And("t.INOUTTYPE", r.FilterType)
  1048. } else {
  1049. sqlId.Join(fmt.Sprintf(" and t.INOUTTYPE in(%v)", r.FilterType))
  1050. }
  1051. }
  1052. // 出入库状态
  1053. if len(r.FilterStatus) > 0 {
  1054. sqlId.Join(fmt.Sprintf(" and t.APPLYSTATUS in(%v)", r.FilterStatus))
  1055. }
  1056. // 排序
  1057. sqlId.Join(" order by t.APPLYTIME desc")
  1058. return sqlId.String()
  1059. }
  1060. // GetDataEx 查询库存申请(记录)
  1061. func (r *Ermcp3AreaStockApply) GetDataEx() (interface{}, error) {
  1062. sData := make([]Ermcp3AreaStockApply, 0)
  1063. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1064. if err == nil {
  1065. for i := range sData {
  1066. sData[i].calc()
  1067. }
  1068. }
  1069. return sData, err
  1070. }
  1071. // Ermcp3AreaStockApplySum 合同出入库数量汇总
  1072. type Ermcp3AreaStockApplySum struct {
  1073. INOUTTYPE int32 `json:"inouttype" xorm:"'INOUTTYPE'"` // 出入库类型 - 1:采购入库 2:销售出库 3:生产入库 4:生产出库
  1074. WRSTANDARDID int32 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类id
  1075. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 品牌id
  1076. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1077. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1078. TOTALQTY float64 `json:"totalqty" xorm:"'TOTALQTY'"` // 总数量
  1079. SPOTCONTRACTID string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` // 关联现货合同ID
  1080. }
  1081. func (r *Ermcp3AreaStockApplySum) buildSql() string {
  1082. sqlId := "select a.*, gb.dgfactoryitemvalue brandname, w.wrstandardname" +
  1083. " from (SELECT t.INOUTTYPE," +
  1084. " t.wrstandardid," +
  1085. " t.SPOTGOODSBRANDID," +
  1086. " t.SPOTCONTRACTID," +
  1087. " sum(t.QTY) TOTALQTY" +
  1088. " FROM ERMCP_AREAINOUTSTOCKAPPLY t" +
  1089. " WHERE t.applystatus = 2" +
  1090. " and t.spotcontractid = %v" +
  1091. " group by t.inouttype," +
  1092. " t.spotgoodsbrandid," +
  1093. " t.wrstandardid," +
  1094. " t.spotcontractid) a" +
  1095. " left join dgfactoryitem gb" +
  1096. " on a.spotgoodsbrandid = gb.dgfactoryitemid" +
  1097. " left join wrstandard w" +
  1098. " on a.wrstandardid = w.wrstandardid"
  1099. sqlId = fmt.Sprintf(sqlId, r.SPOTCONTRACTID)
  1100. // 不关联合同id
  1101. sqlId2 := "select a.*, gb.dgfactoryitemvalue brandname, w.wrstandardname" +
  1102. " from (SELECT t.INOUTTYPE," +
  1103. " t.wrstandardid," +
  1104. " t.SPOTGOODSBRANDID," +
  1105. " t.SPOTCONTRACTID," +
  1106. " sum(t.QTY) TOTALQTY" +
  1107. " FROM ERMCP_AREAINOUTSTOCKAPPLY t" +
  1108. " WHERE t.applystatus = 2" +
  1109. " group by t.inouttype," +
  1110. " t.spotgoodsbrandid," +
  1111. " t.wrstandardid," +
  1112. " t.spotcontractid) a" +
  1113. " left join dgfactoryitem gb" +
  1114. " on a.spotgoodsbrandid = gb.dgfactoryitemid" +
  1115. " left join wrstandard w" +
  1116. " on a.wrstandardid = w.wrstandardid"
  1117. if len(r.SPOTCONTRACTID) == 0 {
  1118. return sqlId2
  1119. }
  1120. return sqlId
  1121. }
  1122. // GetDataEx 获取合同出入库汇总信息
  1123. func (r *Ermcp3AreaStockApplySum) GetDataEx() (interface{}, error) {
  1124. sData := make([]Ermcp3AreaStockApplySum, 0)
  1125. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1126. return sData, err
  1127. }
  1128. // Ermcp3AreaStock 机构库存表
  1129. type Ermcp3AreaStock struct {
  1130. TODAYBUYINQTY float64 `json:"todaybuyinqty" xorm:"'TODAYBUYINQTY'"` // 今日采购入库量
  1131. TODAYPRODUCEINQTY float64 `json:"todayproduceinqty" xorm:"'TODAYPRODUCEINQTY'"` // 今日生产入库量
  1132. TODAYSELLOUTQTY float64 `json:"todayselloutqty" xorm:"'TODAYSELLOUTQTY'"` // 今日销售出库量
  1133. TODAYPRODUCEOUTQTY float64 `json:"todayproduceoutqty" xorm:"'TODAYPRODUCEOUTQTY'"` // 今日生产出库量
  1134. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  1135. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构ID
  1136. WRSTANDARDID string `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类ID
  1137. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID
  1138. WAREHOUSEINFOID string `json:"warehouseinfoid" xorm:"'WAREHOUSEINFOID'"` // 仓库ID
  1139. ORISTOCK float64 `json:"oristock" xorm:"'ORISTOCK'"` // 期初库存量(昨日量)
  1140. CURSTOCK float64 `json:"curstock" xorm:"'CURSTOCK'"` // 期末库存量(今日量)
  1141. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1142. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1143. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 单位id
  1144. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1145. WAREHOUSENAME string `json:"warehousename" xorm:"'WAREHOUSENAME'"` // 仓库名称
  1146. WAREHOUSECODE string `json:"warehousecode" xorm:"'WAREHOUSECODE'"` // 仓库代码
  1147. WAREHOUSETYPE int32 `json:"warehousetype" xorm:"'WAREHOUSETYPE'"` // 仓库类型 - 1 厂库 2 自有库 3 合作库
  1148. USERNAME string `json:"username"` // 机构名称
  1149. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  1150. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种id
  1151. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  1152. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  1153. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODSUNITID'"` // 现货商品单位id
  1154. }
  1155. func (r *Ermcp3AreaStock) calc() {
  1156. r.USERNAME = mtpcache.GetUserNameByUserId(r.USERID)
  1157. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  1158. }
  1159. func (r *Ermcp3AreaStock) buildSql() string {
  1160. var sqlId utils.SQLVal = "SELECT t.TODAYBUYINQTY," +
  1161. " t.TODAYPRODUCEINQTY," +
  1162. " t.TODAYSELLOUTQTY," +
  1163. " t.TODAYPRODUCEOUTQTY," +
  1164. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1165. " t.USERID," +
  1166. " t.WRSTANDARDID," +
  1167. " t.SPOTGOODSBRANDID," +
  1168. " t.WAREHOUSEINFOID," +
  1169. " t.DELIVERYGOODSID," +
  1170. " t.ORISTOCK," +
  1171. " t.CURSTOCK," +
  1172. " w.wrstandardname," +
  1173. " w.wrstandardcode," +
  1174. " w.unitid," +
  1175. " gb.dgfactoryitemvalue brandname," +
  1176. " h.warehousename," +
  1177. " h.warehousecode," +
  1178. " h.warehousetype," +
  1179. " g.deliverygoodsid," +
  1180. " g.deliverygoodscode," +
  1181. " g.deliverygoodsname," +
  1182. " g.goodsunitid" +
  1183. " FROM ERMCP_AREASTOCK t" +
  1184. " LEFT JOIN WRSTANDARD w" +
  1185. " on t.wrstandardid = w.wrstandardid" +
  1186. " LEFT JOIN dgfactoryitem gb" +
  1187. " on t.spotgoodsbrandid = gb.dgfactoryitemvalue" +
  1188. " LEFT JOIN deliverygoods g" +
  1189. " on t.deliverygoodsid = g.deliverygoodsid" +
  1190. " LEFT JOIN WAREHOUSEINFO h" +
  1191. " on t.warehouseinfoid = h.autoid" +
  1192. " WHERE 1 = 1"
  1193. sqlId.And("t.USERID", r.USERID)
  1194. sqlId.Join(" order by t.WRSTANDARDID, t.UPDATETIME desc")
  1195. return sqlId.String()
  1196. }
  1197. // GetDataEx 获取机构库存信息
  1198. func (r *Ermcp3AreaStock) GetDataEx() (interface{}, error) {
  1199. sData := make([]Ermcp3AreaStock, 0)
  1200. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1201. if err == nil {
  1202. for i := range sData {
  1203. sData[i].calc()
  1204. }
  1205. }
  1206. return sData, err
  1207. }
  1208. // Ermcp3AreaStockReport 库存报表
  1209. type Ermcp3AreaStockReport struct {
  1210. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构ID
  1211. WRSTANDARDID string `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 品类ID
  1212. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID
  1213. WAREHOUSEINFOID string `json:"warehouseinfoid" xorm:"'WAREHOUSEINFOID'"` // 仓库ID
  1214. ORISTOCK float64 `json:"oristock" xorm:"'ORISTOCK'"` // 期初库存量
  1215. CURSTOCK float64 `json:"curstock" xorm:"'CURSTOCK'"` // 期末库存量
  1216. TODAYBUYINQTY float64 `json:"todaybuyinqty" xorm:"'TODAYBUYINQTY'"` // 今日采购入库量
  1217. TODAYPRODUCEINQTY float64 `json:"todayproduceinqty" xorm:"'TODAYPRODUCEINQTY'"` // 今日生产入库量
  1218. TODAYSELLOUTQTY float64 `json:"todayselloutqty" xorm:"'TODAYSELLOUTQTY'"` // 今日销售出库量
  1219. TODAYPRODUCEOUTQTY float64 `json:"todayproduceoutqty" xorm:"'TODAYPRODUCEOUTQTY'"` // 今日生产出库量
  1220. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  1221. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 品类名称
  1222. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 品类代码
  1223. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 品类单位id
  1224. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  1225. WAREHOUSENAME string `json:"warehousename" xorm:"'WAREHOUSENAME'"` // 仓库名称
  1226. WAREHOUSECODE string `json:"warehousecode" xorm:"'WAREHOUSECODE'"` // 仓库代码
  1227. WAREHOUSETYPE int32 `json:"warehousetype" xorm:"'WAREHOUSETYPE'"` // 仓库类型 - 1 厂库 2 自有库 3 合作库
  1228. USERNAME string `json:"username"` // 机构名称
  1229. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  1230. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种id
  1231. DELIVERYGOODSCODE string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 现货品种代码
  1232. DELIVERYGOODSNAME string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 现货品种名称
  1233. GOODSUNITID int32 `json:"goodsunitid" xorm:"'GOODSUNITID'"` // 现货单位id
  1234. ReportType int32 `json:"-"` // 报表类型 1-日报表 2-月报表
  1235. ReportDate string `json:"-"` // 格式 日报表(YYYYMMDD) 月报表(YYYYMM)
  1236. }
  1237. func (r *Ermcp3AreaStockReport) calc() {
  1238. r.USERNAME = mtpcache.GetUserNameByUserId(r.USERID)
  1239. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  1240. }
  1241. func (r *Ermcp3AreaStockReport) buildSql() string {
  1242. var sqlId utils.SQLVal = "SELECT t.TODAYBUYINQTY," +
  1243. " t.TODAYPRODUCEINQTY," +
  1244. " t.TODAYSELLOUTQTY," +
  1245. " t.TODAYPRODUCEOUTQTY," +
  1246. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  1247. " t.USERID," +
  1248. " t.WRSTANDARDID," +
  1249. " t.SPOTGOODSBRANDID," +
  1250. " t.WAREHOUSEINFOID," +
  1251. " t.ORISTOCK," +
  1252. " t.CURSTOCK," +
  1253. " w.wrstandardname," +
  1254. " w.wrstandardcode," +
  1255. " w.unitid," +
  1256. " gb.dgfactoryitemvalue brandname," +
  1257. " h.warehousename," +
  1258. " h.warehousecode," +
  1259. " h.warehousetype," +
  1260. " g.deliverygoodsid," +
  1261. " g.deliverygoodscode," +
  1262. " g.deliverygoodsname," +
  1263. " g.goodsunitid" +
  1264. " FROM %v t" +
  1265. " LEFT JOIN WRSTANDARD w" +
  1266. " on t.wrstandardid = w.wrstandardid" +
  1267. " LEFT JOIN dgfactoryitem gb" +
  1268. " on t.spotgoodsbrandid = gb.dgfactoryitemid" +
  1269. " LEFT JOIN deliverygoods g" +
  1270. " on t.deliverygoodsid = g.deliverygoodsid" +
  1271. " LEFT JOIN WAREHOUSEINFO h" +
  1272. " on t.warehouseinfoid = h.autoid" +
  1273. " WHERE 1 = 1"
  1274. sqlId.And("t.USERID", r.USERID)
  1275. if r.ReportType == 1 {
  1276. // 日报表
  1277. sqlId.FormatParam("RECKON_ERMCP_AREASTOCK")
  1278. sqlId.And("t.reckondate", r.ReportDate)
  1279. } else {
  1280. // 月报表
  1281. sqlId.FormatParam("REPORT_ERMCP_AREASTOCK")
  1282. sqlId.And("t.cycletype", 1)
  1283. sqlId.And("t.cycletime", r.ReportDate)
  1284. }
  1285. // 现货商品id
  1286. sqlId.AndEx("t.DELIVERYGOODSID", r.DELIVERYGOODSID, r.DELIVERYGOODSID > 0)
  1287. // 品类id
  1288. if len(r.WRSTANDARDID) > 0 {
  1289. sqlId.And("t.WRSTANDARDID", r.WRSTANDARDID)
  1290. }
  1291. // 品牌id
  1292. if r.SPOTGOODSBRANDID > 0 {
  1293. sqlId.And("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID)
  1294. }
  1295. // 仓库id
  1296. if len(r.WAREHOUSEINFOID) > 0 {
  1297. sqlId.And("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID)
  1298. }
  1299. return sqlId.String()
  1300. }
  1301. // GetDataEx 查询库存报表
  1302. func (r *Ermcp3AreaStockReport) GetDataEx() (interface{}, error) {
  1303. sData := make([]ErmcpAreaStockReport, 0)
  1304. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  1305. if err == nil {
  1306. for i := range sData {
  1307. sData[i].calc()
  1308. }
  1309. }
  1310. return sData, err
  1311. }