ermcpAreaStock.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**
  2. * @Author: zou.yingbin
  3. * @Create : 2021/3/10 9:13
  4. * @Modify : 2021/3/10 9:13
  5. */
  6. package models
  7. import (
  8. "fmt"
  9. "mtp2_if/db"
  10. "mtp2_if/mtpcache"
  11. "mtp2_if/utils"
  12. )
  13. // ErmcpAreaStockApply 出入库申请
  14. type ErmcpAreaStockApply struct {
  15. INOUTAPPLYID string `json:"inoutapplyid" xorm:"'INOUTAPPLYID'"` // 申请ID(607+Unix秒时间戳(10位)+xxxxxx)
  16. USERID string `json:"userid" xorm:"'USERID'"` // 机构ID
  17. INOUTTYPE int32 `json:"inouttype" xorm:"'INOUTTYPE'"` // 出入库类型 - 1:采购入库 2:销售出库 3:生产入库 4:生产出库
  18. WRSTANDARDID string `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID
  19. SPOTGOODSMODELID int32 `json:"spotgoodsmodelid" xorm:"'SPOTGOODSMODELID'"` // 现货型号ID
  20. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID
  21. DELIVERYGOODSID int64 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种ID
  22. SPOTCONTRACTID string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` // 关联现货合同ID
  23. WAREHOUSEINFOID string `json:"warehouseinfoid" xorm:"'WAREHOUSEINFOID'"` // 现货仓库ID
  24. QTY float64 `json:"qty" xorm:"'QTY'"` // 数量
  25. APPLYSTATUS int32 `json:"applystatus" xorm:"'APPLYSTATUS'"` // 申请状态 - 1:待审核 2:审核通过 3:审核拒绝 4:处理失败 5:已撤回
  26. APPLYSRC int32 `json:"applysrc" xorm:"'APPLYSRC'"` // 申请来源 - 1:管理端 2:终端
  27. APPLYID int64 `json:"applyid" xorm:"'APPLYID'"` // 申请人
  28. APPLYREMARK string `json:"applyremark" xorm:"'APPLYREMARK'"` // 申请备注
  29. APPLYTIME string `json:"applytime" xorm:"'APPLYTIME'"` // 申请时间
  30. AUDITSRC int32 `json:"auditsrc" xorm:"'AUDITSRC'"` // 审核来源 - 1:管理端 2:终端
  31. AUDITID int64 `json:"auditid" xorm:"'AUDITID'"` // 审核人
  32. AUDITTIME string `json:"audittime" xorm:"'AUDITTIME'"` // 审核时间
  33. AUDITREMARK string `json:"auditremark" xorm:"'AUDITREMARK'"` // 审核备注
  34. AUDITTRADEDATE string `json:"audittradedate" xorm:"'AUDITTRADEDATE'"` // 审核交易日(yyyyMMdd)
  35. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 现货商品名称
  36. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货商品代码
  37. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 单位id
  38. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  39. MODELNAME string `json:"modelname" xorm:"'MODELNAME'"` // 型号名称
  40. WAREHOUSENAME string `json:"warehousename" xorm:"'WAREHOUSENAME'"` // 仓库名称
  41. WAREHOUSECODE string `json:"warehousecode" xorm:"'WAREHOUSECODE'"` // 仓库代码
  42. WAREHOUSETYPE int32 `json:"warehousetype" xorm:"'WAREHOUSETYPE'"` // 仓库类型 - 1 厂库 2 自有库 3 合作库
  43. CONTRACTNO string `json:"contractno" xorm:"'CONTRACTNO'"` // 合同编号
  44. CONTRACTTYPE int32 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 现货合同类型 - 1:采购 -1:销售
  45. BUYUSERID int64 `json:"buyuserid" xorm:"'BUYUSERID'"` // 采购方userid
  46. SELLUSERID int64 `json:"selluserid" xorm:"'SELLUSERID'"` // 销售方userid
  47. PRICETYPE int32 `json:"pricetype" xorm:"'PRICETYPE'"` // 定价类型 - 1:一口价 2:点价 3:暂定价
  48. CONTRACTQTY float64 `json:"contractqty" xorm:"'CONTRACTQTY'"` // 合同量
  49. BUYUSERNAME string `json:"buyusername"` // 采购方名称
  50. SELLUSERNAME string `json:"sellusername"` // 销售方名称
  51. APPLYNAME string `json:"applyname"` // 申请人名称
  52. AUDITNAME string `json:"auditname"` // 审核人名称
  53. ENUMDICNAME string `json:"enumdicname"` // 现货商品单位名称
  54. FilterStatus string `json:"-"` // 查询条件, 申请状态, 逗号隔开
  55. FilterType string `json:"-"` // 查询条件, 出入库类型, 逗号隔开
  56. }
  57. func (r *ErmcpAreaStockApply) calc() {
  58. // 采购方名称
  59. r.BUYUSERNAME = mtpcache.GetUserNameByUserId(r.BUYUSERID)
  60. // 销售方名称
  61. r.SELLUSERNAME = mtpcache.GetUserNameByUserId(r.SELLUSERID)
  62. // 申请人名称
  63. if r.APPLYSRC == 1 {
  64. r.APPLYNAME = mtpcache.GetSystemmangerLoginCode(r.APPLYID)
  65. } else {
  66. r.APPLYNAME = mtpcache.GetLoginCodeByLoginId(r.APPLYID)
  67. }
  68. // 审核人名称
  69. if r.AUDITSRC == 1 {
  70. r.AUDITNAME = mtpcache.GetSystemmangerLoginCode(r.AUDITID)
  71. } else {
  72. r.AUDITNAME = mtpcache.GetLoginCodeByLoginId(r.AUDITID)
  73. }
  74. // 单位名称
  75. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  76. }
  77. func (r *ErmcpAreaStockApply) buildSql() string {
  78. var sqlId utils.SQLVal = "SELECT to_char(t.INOUTAPPLYID) INOUTAPPLYID," +
  79. " t.USERID," +
  80. " t.INOUTTYPE," +
  81. " t.WRSTANDARDID," +
  82. " t.SPOTGOODSMODELID," +
  83. " t.SPOTGOODSBRANDID," +
  84. " t.DELIVERYGOODSID," +
  85. " to_char(t.SPOTCONTRACTID) SPOTCONTRACTID," +
  86. " to_char(t.WAREHOUSEINFOID) WAREHOUSEINFOID," +
  87. " t.QTY," +
  88. " t.APPLYSTATUS," +
  89. " t.APPLYSRC," +
  90. " t.APPLYID," +
  91. " t.APPLYREMARK," +
  92. " to_char(t.APPLYTIME, 'yyyy-mm-dd hh24:mi:ss') APPLYTIME," +
  93. " t.AUDITSRC," +
  94. " t.AUDITID," +
  95. " to_char(t.AUDITTIME, 'yyyy-mm-dd hh24:mi:ss') AUDITTIME," +
  96. " t.AUDITREMARK," +
  97. " t.AUDITTRADEDATE," +
  98. " w.wrstandardname," +
  99. " w.wrstandardcode," +
  100. " w.unitid," +
  101. " gb.brandname," +
  102. " gm.modelname," +
  103. " h.warehousename," +
  104. " h.warehousecode," +
  105. " h.warehousetype," +
  106. " s.contractno," +
  107. " s.contracttype," +
  108. " s.buyuserid," +
  109. " s.selluserid," +
  110. " s.pricetype," +
  111. " s.qty CONTRACTQTY" +
  112. " FROM ERMCP_AREAINOUTSTOCKAPPLY t" +
  113. " left join wrstandard w" +
  114. " on t.wrstandardid = w.wrstandardid" +
  115. " left join spotgoodsbrand gb" +
  116. " on t.spotgoodsbrandid = gb.brandid" +
  117. " left join spotgoodsmodel gm" +
  118. " on t.spotgoodsmodelid = gm.modelid" +
  119. " left join warehouseinfo h" +
  120. " on t.warehouseinfoid = h.autoid" +
  121. " left join ermcp_spotcontract s" +
  122. " on t.spotcontractid = s.spotcontractid" +
  123. " WHERE 1 = 1"
  124. // 用户id(必要条件)
  125. sqlId.And("t.USERID", r.USERID)
  126. // 出入库类型
  127. if r.INOUTTYPE > 0 {
  128. sqlId.And("t.INOUTTYPE", r.INOUTTYPE)
  129. }
  130. // 合同id
  131. if len(r.SPOTCONTRACTID) > 0 {
  132. sqlId.And("t.SPOTCONTRACTID", r.SPOTCONTRACTID)
  133. }
  134. // 现货商品id
  135. if len(r.WRSTANDARDID) > 0 {
  136. sqlId.And("t.WRSTANDARDID", r.WRSTANDARDID)
  137. }
  138. // 品牌id
  139. if r.SPOTGOODSBRANDID > 0 {
  140. sqlId.And("t.SPOTGOODSBRANDID", r.SPOTGOODSBRANDID)
  141. }
  142. // 型号id
  143. if r.SPOTGOODSMODELID > 0 {
  144. sqlId.And("t.SPOTGOODSMODELID", r.SPOTGOODSMODELID)
  145. }
  146. // 仓库id
  147. if len(r.WAREHOUSEINFOID) > 0 {
  148. sqlId.And("t.WAREHOUSEINFOID", r.WAREHOUSEINFOID)
  149. }
  150. // 出入库状态
  151. if len(r.FilterStatus) > 0 {
  152. sqlId.Join(fmt.Sprintf(" and t.APPLYSTATUS in(%v)", r.FilterStatus))
  153. }
  154. // 排序
  155. sqlId.Join(" order by t.APPLYTIME desc")
  156. return sqlId.String()
  157. }
  158. // 查询库存申请(记录)
  159. func (r *ErmcpAreaStockApply) GetDataEx() (interface{}, error) {
  160. sData := make([]ErmcpAreaStockApply, 0)
  161. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  162. if err == nil {
  163. for i := range sData {
  164. sData[i].calc()
  165. }
  166. }
  167. return sData, err
  168. }
  169. // ErmcpAreaStockApplySum 合同出入库数量汇总
  170. type ErmcpAreaStockApplySum struct {
  171. INOUTTYPE int32 `json:"inouttype" xorm:"'INOUTTYPE'"` // 出入库类型 - 1:采购入库 2:销售出库 3:生产入库 4:生产出库
  172. SPOTGOODSMODELID int32 `json:"spotgoodsmodelid" xorm:"'SPOTGOODSMODELID'"` // 型号id
  173. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 品牌id
  174. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  175. MODELNAME string `json:"modelname" xorm:"'MODELNAME'"` // 型号名称
  176. TOTALQTY float64 `json:"totalqty" xorm:"'TOTALQTY'"` // 总数量
  177. SPOTCONTRACTID string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` // 关联现货合同ID
  178. }
  179. func (r *ErmcpAreaStockApplySum) buildSql() string {
  180. sqlId := "select a.*, gb.brandname, gm.modelname" +
  181. " from (SELECT t.INOUTTYPE," +
  182. " t.SPOTGOODSMODELID," +
  183. " t.SPOTGOODSBRANDID," +
  184. " sum(t.QTY) TOTALQTY" +
  185. " FROM ERMCP_AREAINOUTSTOCKAPPLY t" +
  186. " WHERE t.applystatus= 2 and t.inouttype = %v and t.spotcontractid = %v" +
  187. " group by t.inouttype, t.spotgoodsbrandid, t.spotgoodsmodelid) a" +
  188. " left join spotgoodsbrand gb" +
  189. " on a.spotgoodsbrandid = gb.brandid" +
  190. " left join spotgoodsmodel gm" +
  191. " on a.spotgoodsmodelid = gm.modelid"
  192. sqlId = fmt.Sprintf(sqlId, r.INOUTTYPE, r.SPOTCONTRACTID)
  193. return sqlId
  194. }
  195. // GetDataEx 获取合同出入库汇总信息
  196. func (r *ErmcpAreaStockApplySum) GetDataEx() (interface{}, error) {
  197. sData := make([]ErmcpAreaStockApplySum, 0)
  198. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  199. return sData, err
  200. }
  201. // ErmcpAreaStock 机构库存表
  202. type ErmcpAreaStock struct {
  203. TODAYBUYINQTY float64 `json:"todaybuyinqty" xorm:"'TODAYBUYINQTY'"` // 今日采购入库量
  204. TODAYPRODUCEINQTY float64 `json:"todayproduceinqty" xorm:"'TODAYPRODUCEINQTY'"` // 今日生产入库量
  205. TODAYSELLOUTQTY float64 `json:"todayselloutqty" xorm:"'TODAYSELLOUTQTY'"` // 今日销售出库量
  206. TODAYPRODUCEOUTQTY float64 `json:"todayproduceoutqty" xorm:"'TODAYPRODUCEOUTQTY'"` // 今日生产出库量
  207. UPDATETIME string `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  208. USERID int64 `json:"userid" xorm:"'USERID'"` // 机构ID
  209. WRSTANDARDID string `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 现货商品ID
  210. SPOTGOODSMODELID int32 `json:"spotgoodsmodelid" xorm:"'SPOTGOODSMODELID'"` // 现货型号ID
  211. SPOTGOODSBRANDID int32 `json:"spotgoodsbrandid" xorm:"'SPOTGOODSBRANDID'"` // 现货品牌ID
  212. WAREHOUSEINFOID string `json:"warehouseinfoid" xorm:"'WAREHOUSEINFOID'"` // 仓库ID
  213. DELIVERYGOODSID int64 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种ID
  214. ORISTOCK float64 `json:"oristock" xorm:"'ORISTOCK'"` // 期初库存量(昨日量)
  215. CURSTOCK float64 `json:"curstock" xorm:"'CURSTOCK'"` // 期末库存量(今日量)
  216. WRSTANDARDNAME string `json:"wrstandardname" xorm:"'WRSTANDARDNAME'"` // 现货商品名称
  217. WRSTANDARDCODE string `json:"wrstandardcode" xorm:"'WRSTANDARDCODE'"` // 现货商品代码
  218. UNITID int32 `json:"unitid" xorm:"'UNITID'"` // 单位id
  219. BRANDNAME string `json:"brandname" xorm:"'BRANDNAME'"` // 品牌名称
  220. MODELNAME string `json:"modelname" xorm:"'MODELNAME'"` // 型号名称
  221. WAREHOUSENAME string `json:"warehousename" xorm:"'WAREHOUSENAME'"` // 仓库名称
  222. WAREHOUSECODE string `json:"warehousecode" xorm:"'WAREHOUSECODE'"` // 仓库代码
  223. WAREHOUSETYPE int32 `json:"warehousetype" xorm:"'WAREHOUSETYPE'"` // 仓库类型 - 1 厂库 2 自有库 3 合作库
  224. USERNAME string `json:"username"` // 机构名称
  225. ENUMDICNAME string `json:"enumdicname"` // 单位名称
  226. }
  227. func (r *ErmcpAreaStock) calc() {
  228. r.USERNAME = mtpcache.GetUserNameByUserId(r.USERID)
  229. r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
  230. }
  231. func (r *ErmcpAreaStock) buildSql() string {
  232. var sqlId utils.SQLVal = "SELECT t.TODAYBUYINQTY," +
  233. " t.TODAYPRODUCEINQTY," +
  234. " t.TODAYSELLOUTQTY," +
  235. " t.TODAYPRODUCEOUTQTY," +
  236. " to_char(t.UPDATETIME, 'yyyy-mm-dd hh24:mi:ss') UPDATETIME," +
  237. " t.USERID," +
  238. " t.WRSTANDARDID," +
  239. " t.SPOTGOODSMODELID," +
  240. " t.SPOTGOODSBRANDID," +
  241. " t.WAREHOUSEINFOID," +
  242. " t.DELIVERYGOODSID," +
  243. " t.ORISTOCK," +
  244. " t.CURSTOCK," +
  245. " w.wrstandardname," +
  246. " w.wrstandardcode," +
  247. " w.unitid," +
  248. " gb.brandname," +
  249. " gm.modelname," +
  250. " h.warehousename," +
  251. " h.warehousecode," +
  252. " h.warehousetype" +
  253. " FROM ERMCP_AREASTOCK t" +
  254. " LEFT JOIN WRSTANDARD w" +
  255. " on t.wrstandardid = w.wrstandardid" +
  256. " LEFT JOIN SPOTGOODSBRAND gb" +
  257. " on t.spotgoodsbrandid = gb.brandid" +
  258. " LEFT JOIN SPOTGOODSMODEL gm" +
  259. " on t.spotgoodsmodelid = gm.modelid" +
  260. " LEFT JOIN WAREHOUSEINFO h" +
  261. " on t.warehouseinfoid = h.autoid" +
  262. " WHERE 1 = 1"
  263. sqlId.And("t.USERID", r.USERID)
  264. sqlId.Join(" order by t.WRSTANDARDID, t.UPDATETIME desc")
  265. return sqlId.String()
  266. }
  267. // GetDataEx 获取机构库存信息
  268. func (r *ErmcpAreaStock) GetDataEx() (interface{}, error) {
  269. sData := make([]ErmcpAreaStock, 0)
  270. err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
  271. if err == nil {
  272. for i := range sData {
  273. sData[i].calc()
  274. }
  275. }
  276. return sData, err
  277. }