ermcpWrstandard.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**
  2. * @Author: zou.yingbin
  3. * @Create : 2021/1/13 11:16
  4. * @Modify : 2021/1/13 11:16
  5. */
  6. package models
  7. import (
  8. "fmt"
  9. "mtp2_if/db"
  10. "mtp2_if/logger"
  11. "mtp2_if/utils"
  12. )
  13. // 现货商品表
  14. type ErmcpWrstandard struct {
  15. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID(SEQ_WRSTANDARD)
  16. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货商品代码
  17. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 品种ID
  18. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 单位ID
  19. MINIVALUE int32 `json:"minivalue" xorm:"'MINIVALUE'"` // 最小变动值
  20. MINIVALUEDP int32 `json:"minivaluedp" xorm:"'MINIVALUEDP'"` // 最小变动值小数位
  21. REALMINIVALUE int32 `json:"realminivalue" xorm:"'REALMINIVALUE'"` // 实际最小变动值
  22. REALMINIVALUEDP int32 `json:"realminivaluedp" xorm:"'REALMINIVALUEDP'"` // 实际最小变动值小数位
  23. CREATORID int64 `json:"creatorid" xorm:"'CREATORID'"` // 创建人
  24. CREATETIME string `json:"createtime" xorm:"'CREATETIME'"` // 创建时间
  25. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 现货商品名称
  26. AREAUSERID int `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  27. EnumdicName string `json:"enumdicname"` // 单位名称
  28. ISVALID int32 `json:"isvalid" xorm:"'ISVALID'"` // 是否有效 0-无效(停用) 1-有效(正常)
  29. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  30. REMARK string `json:"remark" xorm:"'REMARK'"` // 备注
  31. }
  32. func (r *ErmcpWrstandard) buildSql() string {
  33. str := "select WRSTANDARDID," +
  34. " WRSTANDARDCODE," +
  35. " DELIVERYGOODSID," +
  36. " UNITID," +
  37. " MINIVALUE," +
  38. " MINIVALUEDP," +
  39. " REALMINIVALUE," +
  40. " REALMINIVALUEDP," +
  41. " CREATORID," +
  42. " to_char(CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME," +
  43. " WRSTANDARDNAME," +
  44. " ISVALID," +
  45. " to_char(UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  46. " REMARK," +
  47. " AREAUSERID" +
  48. " from wrstandard t" +
  49. " where t.AREAUSERID=%v"
  50. sqlId := fmt.Sprintf(str, r.AREAUSERID)
  51. if r.WRSTANDARDID > 0 {
  52. sqlId = sqlId + fmt.Sprintf(" and WRSTANDARDID=%v", r.WRSTANDARDID)
  53. }
  54. if r.ISVALID >= 0 {
  55. sqlId = sqlId + fmt.Sprintf(" and t.ISVALID=%v", r.ISVALID)
  56. }
  57. return sqlId
  58. }
  59. // 查询现货商品
  60. func (r *ErmcpWrstandard) GetData() ([]ErmcpWrstandard, error) {
  61. sData := make([]ErmcpWrstandard, 0)
  62. e := db.GetEngine()
  63. if err := e.SQL(r.buildSql()).Find(&sData); err != nil {
  64. logger.GetLogger().Errorf("query wrstardard fail, %v", err)
  65. return sData, err
  66. }
  67. return sData, nil
  68. }
  69. // 商品型号表
  70. type GoodsTypeModel struct {
  71. MODELID int `json:"modelid" xorm:"'MODELID'"` // 型号ID
  72. MODELNAME string `json:"modelname" xorm:"'MODELNAME'"` // 型号名称
  73. WRSTANDARDID int64 `json:"-" xorm:"'WRSTANDARDID'"` // 现货商品ID
  74. DELIVERYGOODSID int32 `json:"-" xorm:"'DELIVERYGOODSID'"` // 现货品种ID
  75. AREAUSERID int `json:"-" xorm:"'AREAUSERID'"` // 所属机构
  76. ISVALID int32 `json:"-" xorm:"'ISVALID'"` // 是否有效 - 0:无效 1:有效
  77. CREATORSRC int32 `json:"-" xorm:"'CREATORSRC'"` // 创建人来源 - 1:管理端 2:终端
  78. CREATORID int32 `json:"-" xorm:"'CREATORID'"` // 创建人
  79. CREATETIME string `json:"-" xorm:"'CREATETIME'"` // SYSDATE 创建时间
  80. UPDATORSRC int32 `json:"-" xorm:"'UPDATORSRC'"` // 更新人来源 - 1:管理端 2:终端
  81. UPDATORID int32 `json:"-" xorm:"'UPDATORID'"` // 更新人
  82. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  83. }
  84. // 获取商品型号表数据
  85. func (r *GoodsTypeModel) GetData() ([]GoodsTypeModel, error) {
  86. sData := make([]GoodsTypeModel, 0)
  87. if err := db.GetEngine().SQL(r.buildSql()).Find(&sData); err != nil {
  88. logger.GetLogger().Errorf("query goodsTypeModel fail, %v", err)
  89. return nil, err
  90. }
  91. return sData, nil
  92. }
  93. func (r *GoodsTypeModel) buildSql() string {
  94. sqlId := "select MODELID," +
  95. " MODELNAME," +
  96. " WRSTANDARDID," +
  97. " DELIVERYGOODSID," +
  98. " AREAUSERID," +
  99. " ISVALID," +
  100. " CREATORSRC," +
  101. " CREATORID," +
  102. " to_char(CREATETIME, 'yyyy-mm-dd hh24:mi:ss') CREATETIME," +
  103. " UPDATORSRC," +
  104. " UPDATORID," +
  105. " to_char(UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME" +
  106. " from spotgoodsmodel where ISVALID=1"
  107. if r.AREAUSERID > 0 {
  108. sqlId = sqlId + fmt.Sprintf(" and AREAUSERID=%v", r.AREAUSERID)
  109. }
  110. if r.WRSTANDARDID > 0 {
  111. sqlId = sqlId + fmt.Sprintf(" and WRSTANDARDID=%v", r.WRSTANDARDID)
  112. }
  113. return sqlId
  114. }
  115. // 商品品牌
  116. type GoodsBrand struct {
  117. BRANDID int `json:"brandid" xorm:"'bRANDID'"` // 品牌ID
  118. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  119. WRSTANDARDID int64 `json:"-" xorm:"'WRSTANDARDID'"` // 现货商品ID
  120. DELIVERYGOODSID int32 `json:"-" xorm:"'DELIVERYGOODSID'"` // 现货品种ID
  121. AREAUSERID int `json:"-" xorm:"'AREAUSERID'"` // 所属机构
  122. ISVALID int32 `json:"isvalid" xorm:"'ISVALID'"` // 是否有效 - 0:无效 1:有效
  123. }
  124. func (r *GoodsBrand) buildSql() string {
  125. sqlId := "select BRANDID," +
  126. " BRANDNAME," +
  127. " WRSTANDARDID," +
  128. " DELIVERYGOODSID," +
  129. " AREAUSERID," +
  130. " ISVALID" +
  131. " from spotgoodsbrand" +
  132. " where ISVALID = 1"
  133. if r.AREAUSERID > 0 {
  134. sqlId = sqlId + fmt.Sprintf(" and AREAUSERID=%v", r.AREAUSERID)
  135. }
  136. if r.WRSTANDARDID > 0 {
  137. sqlId = sqlId + fmt.Sprintf(" and WRSTANDARDID=%v", r.WRSTANDARDID)
  138. }
  139. return sqlId
  140. }
  141. // 获取品牌数据
  142. func (r *GoodsBrand) GetData() ([]GoodsBrand, error) {
  143. sData := make([]GoodsBrand, 0)
  144. if err := db.GetEngine().SQL(r.buildSql()).Find(&sData); err != nil {
  145. logger.GetLogger().Errorf("query GoodsBrand fail, %v", err)
  146. return nil, err
  147. }
  148. return sData, nil
  149. }
  150. // 现货商品折算配置明细表
  151. type WRSConverTDetail struct {
  152. WRSTANDARDID int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID
  153. DELIVERYGOODSID int32 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种ID
  154. MIDDLEGOODSID int64 `json:"middlegoodsid" xorm:"'MIDDLEGOODSID'"` // 套保品种ID
  155. CONVERTRATIO float64 `json:"convertratio" xorm:"'CONVERTRATIO'"` // 套保系数
  156. MODIFYTIME string `json:"modifytime" xorm:"'MODIFYTIME'"` // 修改时间
  157. MIDDLEGOODSNAME string `json:"middlegoodsname" xorm:"'middlegoodsname'"` // 套保品种名称
  158. MIDDLEGOODSCODE string `json:"middlegoodscode" xorm:"'middlegoodscode'"` // 套保品种代码
  159. }
  160. func (r *WRSConverTDetail) buildSql() string {
  161. sqlId := "select t.WRSTANDARDID," +
  162. " t.DELIVERYGOODSID," +
  163. " t.MIDDLEGOODSID," +
  164. " t.CONVERTRATIO," +
  165. " to_char(t.MODIFYTIME, 'yyyy-mm-dd hh24:mi:ss') MODIFYTIME," +
  166. " m.middlegoodsname," +
  167. " m.middlegoodscode" +
  168. " from Erms2_Wrsconvertdetail t" +
  169. " left join erms_middlegoods m" +
  170. " on t.middlegoodsid = m.middlegoodsid" +
  171. " where 1=1"
  172. if r.WRSTANDARDID > 0 {
  173. sqlId = sqlId + fmt.Sprintf(" and t.wrstandardid=%v", r.WRSTANDARDID)
  174. }
  175. return sqlId
  176. }
  177. // 获取现货商品折算配置明细表数据
  178. func (r *WRSConverTDetail) GetData() ([]WRSConverTDetail, error) {
  179. sData := make([]WRSConverTDetail, 0)
  180. if err := db.GetEngine().SQL(r.buildSql()).Find(&sData); err != nil {
  181. logger.GetLogger().Errorf("query WRSConverTDetail fail, %v", err)
  182. return nil, err
  183. }
  184. return sData, nil
  185. }
  186. // 现货关联的交易商品
  187. type RelatedGoodsEx struct {
  188. GoodsId int `json:"goodsid" xorm:"'GoodsId'"` // 商品id
  189. GoodsCode string `json:"goodscode" xorm:"'GoodsCode'"` // 商品代码
  190. GoodsName string `json:"goodsname" xorm:"'GoodsName'"` // 商品名称
  191. WRSTANDARDID int64 `json:"-" xorm:"'WRSTANDARDID'"` // 现货商品ID
  192. AREAUSERID int `json:"-" xorm:"'AREAUSERID'"` // 所属机构
  193. }
  194. func (r *RelatedGoodsEx) buildSql() string {
  195. var sqlId utils.SQLVal = "select distinct g.goodsid, g.goodscode, g.goodsname" +
  196. " from wrstandard t" +
  197. " inner join ERMS2_WRSConvertDetail w" +
  198. " on t.wrstandardid = w.wrstandardid" +
  199. " inner join erms_middlegoods m" +
  200. " on w.middlegoodsid = m.middlegoodsid" +
  201. " inner join ERMCP_GGConvertConfig gc" +
  202. " on m.goodsgroupid = gc.destgoodsgroupid" +
  203. " inner join goods g" +
  204. " on gc.srcgoodsgroupid = g.goodsgroupid" +
  205. " where 1=1"
  206. sqlId.And("t.areauserid", r.AREAUSERID)
  207. sqlId.And("t.wrstandardid", r.WRSTANDARDID)
  208. sqlId.Join(" order by g.goodsid")
  209. return sqlId.String()
  210. }
  211. func (r *RelatedGoodsEx) GetData() ([]RelatedGoodsEx, error) {
  212. sData := make([]RelatedGoodsEx, 0)
  213. if err := db.GetEngine().SQL(r.buildSql()).Find(&sData); err != nil {
  214. logger.GetLogger().Errorf("query RelatedGoodsEx fail, %v", err)
  215. return nil, err
  216. }
  217. return sData, nil
  218. }
  219. // 现货商品详细
  220. type ErmcpWrstandDetail struct {
  221. Wrd ErmcpWrstandard `json:"wrd"` // 基本信息
  222. GtList []GoodsTypeModel `json:"gtList"` // 型号列表
  223. GbList []GoodsBrand `json:"gbList"` // 品牌列表
  224. WrsList []WRSConverTDetail `json:"wrsList"` // 套保信息列表
  225. GoodsList []RelatedGoodsEx `json:"goodsList"` // 关联交易商品列表
  226. QueryGoods bool `json:"-"`
  227. }
  228. // 查询现货商品详细
  229. func (r *ErmcpWrstandDetail) GetData() (ErmcpWrstandDetail, error) {
  230. // 获取基本信息
  231. m := ErmcpWrstandard{AREAUSERID: r.Wrd.AREAUSERID, WRSTANDARDID: r.Wrd.WRSTANDARDID, ISVALID: -1}
  232. d, err := m.GetData()
  233. if err != nil {
  234. return ErmcpWrstandDetail{}, err
  235. }
  236. if d == nil || len(d) == 0 {
  237. return ErmcpWrstandDetail{}, fmt.Errorf("no record")
  238. }
  239. rsp := ErmcpWrstandDetail{}
  240. rsp.Wrd = d[0]
  241. rsp.GtList = make([]GoodsTypeModel, 0)
  242. rsp.GbList = make([]GoodsBrand, 0)
  243. rsp.WrsList = make([]WRSConverTDetail, 0)
  244. rsp.GoodsList = make([]RelatedGoodsEx, 0)
  245. // 获取型号信息
  246. gt := GoodsTypeModel{AREAUSERID: rsp.Wrd.AREAUSERID, WRSTANDARDID: rsp.Wrd.WRSTANDARDID}
  247. if val, err := gt.GetData(); err == nil {
  248. rsp.GtList = val
  249. }
  250. // 获取品牌信息
  251. gb := GoodsBrand{AREAUSERID: rsp.Wrd.AREAUSERID, WRSTANDARDID: rsp.Wrd.WRSTANDARDID}
  252. if val, err := gb.GetData(); err == nil {
  253. rsp.GbList = val
  254. }
  255. // 获取套保信息
  256. wrs := WRSConverTDetail{WRSTANDARDID: rsp.Wrd.WRSTANDARDID}
  257. if val, err := wrs.GetData(); err == nil {
  258. rsp.WrsList = val
  259. }
  260. // 获取关联交易商品
  261. if r.QueryGoods {
  262. goodsLst := RelatedGoodsEx{AREAUSERID: r.Wrd.AREAUSERID, WRSTANDARDID: r.Wrd.WRSTANDARDID}
  263. if val, err := goodsLst.GetData(); err == nil {
  264. rsp.GoodsList = val
  265. }
  266. }
  267. return rsp, nil
  268. }