StockOutProduct.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 StockOutProduct 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 *StockOutProduct) TableName() string {
  26. return "stock_out_product" // 数据库名称 // ************** 替换 FormulaList **************
  27. }
  28. type StockOutProductDaoImpl struct {
  29. orm orm.Ormer
  30. }
  31. func NewStockOutProduct(orm orm.Ormer) *StockOutProductDaoImpl {
  32. return &StockOutProductDaoImpl{orm: orm}
  33. }
  34. func init() {
  35. //注册模型
  36. orm.RegisterModel(new(StockOutProduct))
  37. }
  38. type StockOutProduct_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_device_list []string // sn关联列表
  45. // ---------产品信息-----------
  46. T_product_id int // 产品id
  47. T_product_name string // 产品名称
  48. T_product_class_name string // 产品分类名称
  49. T_product_model string // 产品型号
  50. T_product_spec string // 产品规格
  51. T_product_relation_sn int // 是否关联sn 0-否 1-是
  52. T_product_img string // 产品图片
  53. }
  54. func StockOutProductToStockOutProduct_R(t StockOutProduct) (r StockOutProduct_R) {
  55. r.Id = t.Id
  56. r.T_number = t.T_number
  57. r.T_depot_id = t.T_depot_id
  58. r.T_num = t.T_num
  59. r.T_date = t.T_date
  60. //r.T_relation_sn = t.T_relation_sn
  61. r.T_device_list = lib.SplitString(t.T_relation_sn, ",")
  62. r.T_product_id = t.T_product_id
  63. product, _ := Basic.Read_Product_ById(t.T_product_id)
  64. r.T_product_name = product.T_name
  65. r.T_product_class_name = Basic.Read_ProductClass_Get(product.T_class)
  66. r.T_product_model = product.T_model
  67. r.T_product_spec = product.T_spec
  68. r.T_product_relation_sn = product.T_relation_sn
  69. r.T_product_img = product.T_img
  70. return r
  71. }
  72. // 添加
  73. func (dao *StockOutProductDaoImpl) Add_StockOutProduct(r StockOutProduct) (id int64, err error) {
  74. id, err = dao.orm.Insert(&r)
  75. if err != nil {
  76. logs.Error(lib.FuncName(), err)
  77. }
  78. return id, err
  79. }
  80. // 获取 ById
  81. func (dao *StockOutProductDaoImpl) Read_StockOutProduct_List(T_number string) (r []StockOutProduct) {
  82. qs := dao.orm.QueryTable(new(StockOutProduct))
  83. _, err := qs.Filter("T_number", T_number).All(&r)
  84. if err != nil {
  85. logs.Error(lib.FuncName(), err)
  86. }
  87. return
  88. }
  89. // 修改
  90. func (dao *StockOutProductDaoImpl) Update_StockOutProduct(m StockOutProduct, cols ...string) error {
  91. _, err := dao.orm.Update(&m, cols...)
  92. if err != nil {
  93. logs.Error(lib.FuncName(), err)
  94. return err
  95. }
  96. return nil
  97. }
  98. // 根据库存id、产品id、月份获取本月出库总数量
  99. func (dao *StockOutProductDaoImpl) Read_StockOut_Total(T_depot_id, T_product_id int, T_date string) int {
  100. sql := "SELECT SUM(t_num) FROM stock_out_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 + "%'"
  101. var pl_lists orm2.ParamsList
  102. _, err := dao.orm.Raw(sql).ValuesFlat(&pl_lists)
  103. if err != nil {
  104. logs.Error(lib.FuncName(), err)
  105. return 0
  106. }
  107. if pl_lists[0] == nil {
  108. return 0
  109. }
  110. key, _ := strconv.Atoi(pl_lists[0].(string))
  111. return key
  112. }