StockInProduct.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. "strconv"
  7. "time"
  8. "ERP_storage/logs"
  9. _ "github.com/astaxie/beego/cache/redis"
  10. "github.com/beego/beego/v2/adapter/orm"
  11. _ "github.com/go-sql-driver/mysql"
  12. )
  13. // 入库产品mingxi
  14. type StockInProduct struct {
  15. Id int `orm:"column(ID);size(11);auto;pk"`
  16. T_number string `orm:"size(256);null"` // 入库单号
  17. T_depot_id int `orm:"size(20);null"` // 仓库id
  18. T_product_id int `orm:"size(20);null"` // 产品id
  19. T_num int `orm:"size(20);null"` // 入库数量
  20. T_date string `orm:"size(256);null"` // 业务日期
  21. T_relation_sn string `orm:"type(text);null"` // 关联sn,20220101,20230202,
  22. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  23. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  24. }
  25. func (t *StockInProduct) TableName() string {
  26. return "stock_in_product" // 数据库名称 // ************** 替换 FormulaList **************
  27. }
  28. type StockInProductDaoImpl struct {
  29. orm orm.Ormer
  30. }
  31. func NewStockInProduct(orm orm.Ormer) *StockInProductDaoImpl {
  32. return &StockInProductDaoImpl{orm: orm}
  33. }
  34. func init() {
  35. //注册模型
  36. orm.RegisterModel(new(StockInProduct))
  37. }
  38. type StockInProduct_R struct {
  39. Id int
  40. T_number string // 入库单号
  41. T_depot_id int // 仓库id
  42. T_num int // 入库数量
  43. T_date string // 日期
  44. //T_relation_sn string // 关联sn,20220101,20230202,
  45. T_device_list []string // sn关联列表
  46. // ---------产品信息-----------
  47. T_product_id int
  48. T_product_name string
  49. T_product_class_name string
  50. T_product_model string
  51. T_product_spec string
  52. T_product_relation_sn int
  53. T_product_img string
  54. }
  55. func StockInProductToStockInProduct_R(t StockInProduct) (r StockInProduct_R) {
  56. r.Id = t.Id
  57. r.T_number = t.T_number
  58. r.T_depot_id = t.T_depot_id
  59. r.T_num = t.T_num
  60. r.T_date = t.T_date
  61. //r.T_relation_sn = t.T_relation_sn
  62. r.T_device_list = lib.SplitString(t.T_relation_sn, ",")
  63. r.T_product_id = t.T_product_id
  64. product, _ := Basic.Read_Product_ById(t.T_product_id)
  65. r.T_product_name = product.T_name
  66. r.T_product_class_name = Basic.Read_ProductClass_Get(product.T_class)
  67. r.T_product_model = product.T_model
  68. r.T_product_spec = product.T_spec
  69. r.T_product_relation_sn = product.T_relation_sn
  70. r.T_product_img = product.T_img
  71. return r
  72. }
  73. // 添加
  74. func (dao *StockInProductDaoImpl) Add_StockInProduct(r StockInProduct) (id int64, err error) {
  75. id, err = dao.orm.Insert(&r)
  76. if err != nil {
  77. logs.Error(lib.FuncName(), err)
  78. }
  79. return id, err
  80. }
  81. // 获取 ById
  82. func (dao *StockInProductDaoImpl) Read_StockInProduct_List(T_number string) (r []StockInProduct) {
  83. qs := dao.orm.QueryTable(new(StockInProduct))
  84. _, err := qs.Filter("T_number", T_number).All(&r)
  85. if err != nil {
  86. logs.Error(lib.FuncName(), err)
  87. }
  88. return
  89. }
  90. // 修改
  91. func (dao *StockInProductDaoImpl) Update_StockInProduct(m StockInProduct, cols ...string) error {
  92. _, err := dao.orm.Update(&m, cols...)
  93. if err != nil {
  94. logs.Error(lib.FuncName(), err)
  95. return err
  96. }
  97. return nil
  98. }
  99. // 根据库存id、产品id、月份获取本月出库总数量
  100. func (dao *StockInProductDaoImpl) Read_StockIn_Total(T_depot_id, T_product_id int, T_date string) int {
  101. sql := "SELECT SUM(t_num) FROM stock_in_product WHERE t_depot_id = " + strconv.Itoa(T_depot_id) + " AND t_product_id = " + strconv.Itoa(T_product_id) + " AND t_date like '" + T_date + "%'"
  102. var pl_lists orm2.ParamsList
  103. _, err := dao.orm.Raw(sql).ValuesFlat(&pl_lists)
  104. if err != nil {
  105. logs.Error(lib.FuncName(), err)
  106. return 0
  107. }
  108. if pl_lists[0] == nil {
  109. return 0
  110. }
  111. key, _ := strconv.Atoi(pl_lists[0].(string))
  112. return key
  113. }