ermcp3.go 64 KB

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