StockMonth.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package Stock
  2. import (
  3. "ERP_storage/models/Basic"
  4. "git.baozhida.cn/ERP_libs/lib"
  5. orm2 "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. "ERP_storage/logs"
  8. _ "github.com/astaxie/beego/cache/redis"
  9. "github.com/beego/beego/v2/adapter/orm"
  10. _ "github.com/go-sql-driver/mysql"
  11. )
  12. // 库存
  13. type StockMonth struct {
  14. Id int `orm:"column(ID);size(11);auto;pk"`
  15. T_depot_id int `orm:"size(20);null"` // 仓库id
  16. T_product_id int `orm:"size(20);null"` // 产品id
  17. T_product_class int `orm:"size(20);null"` // 产品分类
  18. T_month string `orm:"size(256);null"` // 月份
  19. T_in int `orm:"size(20);null"` // 当月入库
  20. T_out int `orm:"size(20);null"` // 当月出库
  21. T_beginning int `orm:"size(20);null"` // 期初库存
  22. T_ending int `orm:"size(20);null"` // 期末库存
  23. T_project string `orm:"type(text);null"` // 关联项目
  24. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  25. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  26. }
  27. func (t *StockMonth) TableName() string {
  28. return "stock_month" // 数据库名称 // ************** 替换 FormulaList **************
  29. }
  30. type StockMonthDaoImpl struct {
  31. orm orm.Ormer
  32. }
  33. func NewStockMonth(orm orm.Ormer) *StockMonthDaoImpl {
  34. return &StockMonthDaoImpl{orm: orm}
  35. }
  36. func init() {
  37. //注册模型
  38. orm.RegisterModel(new(StockMonth))
  39. }
  40. type StockMonth_R struct {
  41. Id int
  42. T_depot_id int // 仓库id
  43. T_month string // 月份
  44. T_in int // 当月入库
  45. T_out int // 当月出库
  46. T_beginning int // 期初库存
  47. T_ending int // 期末库存
  48. T_project string // 关联项目
  49. // ---------产品信息-----------
  50. T_product_id int
  51. T_product_class int
  52. T_product_name string
  53. T_product_model string
  54. T_product_spec string
  55. }
  56. func StockMonthToStockMonth_R(t StockMonth) (r StockMonth_R) {
  57. r.Id = t.Id
  58. r.T_depot_id = t.T_depot_id
  59. r.T_product_id = t.T_product_id
  60. r.T_month = t.T_month
  61. r.T_in = t.T_in
  62. r.T_out = t.T_out
  63. r.T_beginning = t.T_beginning
  64. r.T_ending = t.T_ending
  65. r.T_project = t.T_project
  66. r.T_product_id = t.T_product_id
  67. r.T_product_class = t.T_product_class
  68. product, _ := Basic.Read_Product_ById(t.T_product_id)
  69. r.T_product_name = product.T_name
  70. r.T_product_model = product.T_model
  71. r.T_product_spec = product.T_spec
  72. return r
  73. }
  74. // 添加
  75. func (dao *StockMonthDaoImpl) Add_StockMonth(r StockMonth) (id int64, err error) {
  76. id, err = dao.orm.Insert(&r)
  77. if err != nil {
  78. logs.Error(lib.FuncName(), err)
  79. }
  80. return id, err
  81. }
  82. // 获取 上月数据
  83. func (dao *StockMonthDaoImpl) Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id int, T_month string) (r []StockMonth) {
  84. qs := dao.orm.QueryTable(new(StockMonth))
  85. var maps []StockMonth
  86. _, err := qs.Limit(1, 0).Filter("T_depot_id", T_depot_id).Filter("T_product_id", T_product_id).
  87. Filter("T_month__lt", T_month).OrderBy("-T_month").All(&maps)
  88. if err != nil {
  89. logs.Error(lib.FuncName(), err)
  90. }
  91. return maps
  92. }
  93. // 获取本月数据
  94. func (dao *StockMonthDaoImpl) Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id int, T_month string) (r StockMonth) {
  95. qs := dao.orm.QueryTable(new(StockMonth))
  96. var maps StockMonth
  97. err := qs.Filter("T_depot_id", T_depot_id).Filter("T_product_id", T_product_id).
  98. Filter("T_month", T_month).OrderBy("-T_month").One(&maps)
  99. if err != nil {
  100. logs.Error(lib.FuncName(), err)
  101. }
  102. return maps
  103. }
  104. // 获取列表
  105. func (dao *StockMonthDaoImpl) Read_StockMonth_List(T_depot_id, T_product_id, T_product_class int, start_date, end_date string) (r_ []StockMonth_R) {
  106. qs := dao.orm.QueryTable(new(StockMonth))
  107. // 过滤
  108. cond := orm.NewCondition()
  109. if T_depot_id > 0 {
  110. cond = cond.And("T_depot_id", T_depot_id)
  111. }
  112. if T_product_id > 0 {
  113. cond = cond.And("T_product_id", T_product_id)
  114. }
  115. if T_product_class > 0 {
  116. cond = cond.And("T_product_class", T_product_class)
  117. }
  118. if len(start_date) > 0 {
  119. cond = cond.And("T_month__gte", start_date)
  120. }
  121. if len(end_date) > 0 {
  122. cond = cond.And("T_month__lte", end_date)
  123. }
  124. // 查询
  125. var r []StockMonth
  126. _, err := qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_product_id").OrderBy("T_month").All(&r)
  127. if err != nil {
  128. logs.Error(lib.FuncName(), err)
  129. return
  130. }
  131. for _, v := range r {
  132. r_ = append(r_, StockMonthToStockMonth_R(v))
  133. }
  134. return r_
  135. }
  136. // 同步库存产品信息
  137. func (dao *StockMonthDaoImpl) Read_StockMonth_ByT_product_id(T_product_id int) (r []StockMonth, err error) {
  138. qs := dao.orm.QueryTable(new(StockMonth))
  139. _, err = qs.Filter("T_product_id", T_product_id).All(&r)
  140. if err != nil {
  141. logs.Error(lib.FuncName(), err)
  142. }
  143. return
  144. }
  145. // 修改
  146. func (dao *StockMonthDaoImpl) Update_StockMonth(m StockMonth, cols ...string) error {
  147. _, err := dao.orm.Update(&m, cols...)
  148. if err != nil {
  149. logs.Error(lib.FuncName(), err)
  150. return err
  151. }
  152. return nil
  153. }