package Stock import ( "ERP_storage/models/Account" "ERP_storage/models/Basic" "git.baozhida.cn/ERP_libs/lib" orm2 "github.com/beego/beego/v2/client/orm" "strconv" "time" "ERP_storage/logs" _ "github.com/astaxie/beego/cache/redis" "github.com/beego/beego/v2/adapter/orm" _ "github.com/go-sql-driver/mysql" ) // 入库产品mingxi type StockOutProduct struct { Id int `orm:"column(ID);size(11);auto;pk"` T_number string `orm:"size(256);null"` // 出库单号 T_depot_id int `orm:"size(20);null"` // 仓库id T_product_id int `orm:"size(20);null"` // 产品id T_num int `orm:"size(20);null"` // 入库数量 T_date string `orm:"size(256);null"` // 业务日期 T_relation_sn string `orm:"type(text);null"` // 关联sn,20220101,20230202, CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间 UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新 } func (t *StockOutProduct) TableName() string { return "stock_out_product" // 数据库名称 // ************** 替换 FormulaList ************** } type StockOutProductDaoImpl struct { orm orm.Ormer } func NewStockOutProduct(orm orm.Ormer) *StockOutProductDaoImpl { return &StockOutProductDaoImpl{orm: orm} } func init() { //注册模型 orm.RegisterModel(new(StockOutProduct)) } type StockOutProduct_R struct { Id int T_number string // 入库单号 T_depot_id int // 仓库id T_num int // 入库数量 T_date string // 业务日期 T_device_list []string // sn关联列表 // ---------产品信息----------- T_product_id int // 产品id T_product_name string // 产品名称 T_product_class_name string // 产品分类名称 T_product_model string // 产品型号 T_product_spec string // 产品规格 T_product_relation_sn int // 是否关联sn 0-否 1-是 T_product_img string // 产品图片 } func StockOutProductToStockOutProduct_R(t StockOutProduct) (r StockOutProduct_R) { r.Id = t.Id r.T_number = t.T_number r.T_depot_id = t.T_depot_id r.T_num = t.T_num r.T_date = t.T_date //r.T_relation_sn = t.T_relation_sn r.T_device_list = lib.SplitString(t.T_relation_sn, ",") r.T_product_id = t.T_product_id product, _ := Basic.Read_Product_ById(t.T_product_id) r.T_product_name = product.T_name r.T_product_class_name = Basic.Read_ProductClass_Get(product.T_class) r.T_product_model = product.T_model r.T_product_spec = product.T_spec r.T_product_relation_sn = product.T_relation_sn r.T_product_img = product.T_img return r } type StockOutProductList struct { Id int T_number string // StockOut - 出库单号 T_depot_id int // StockOut - 仓库id T_depot_name string // StockOut - 仓库名称 T_date string // StockOut - 业务日期 T_submit string // StockOut - 经办人 T_remark string // StockOut - 备注 T_project string // StockOut - 项目 T_receive string // StockOut - 领取人 T_receive_name string // 领取人名称 T_num int // 入库数量 T_relation_sn string // 关联sn,20220101,20230202 T_device_list []string // sn关联列表 // ---------产品信息----------- T_product_id int T_product_name string T_product_class_name string T_product_model string T_product_spec string T_product_relation_sn int T_product_img string } func StockOutProductListToStockOutProductList(t StockOutProductList) (r StockOutProductList) { r.Id = t.Id r.T_number = t.T_number r.T_depot_id = t.T_depot_id r.T_depot_name = Basic.Read_Depot_Get(t.T_depot_id) r.T_num = t.T_num r.T_date = t.T_date r.T_receive = t.T_receive r.T_receive_name = Account.Read_User_T_name_Get(t.T_receive) r.T_submit = t.T_submit r.T_remark = t.T_remark r.T_project = t.T_project r.T_num = t.T_num r.T_relation_sn = t.T_relation_sn r.T_device_list = lib.SplitString(t.T_relation_sn, ",") r.T_product_id = t.T_product_id product, _ := Basic.Read_Product_ById(t.T_product_id) r.T_product_name = product.T_name r.T_product_class_name = Basic.Read_ProductClass_Get(product.T_class) r.T_product_model = product.T_model r.T_product_spec = product.T_spec r.T_product_relation_sn = product.T_relation_sn r.T_product_img = product.T_img return r } // 添加 func (dao *StockOutProductDaoImpl) Add_StockOutProduct(r StockOutProduct) (id int64, err error) { id, err = dao.orm.Insert(&r) if err != nil { logs.Error(lib.FuncName(), err) } return id, err } // 获取 ById func (dao *StockOutProductDaoImpl) Read_StockOutProduct_List(T_number string) (r []StockOutProduct) { qs := dao.orm.QueryTable(new(StockOutProduct)) _, err := qs.Filter("T_number", T_number).All(&r) if err != nil { logs.Error(lib.FuncName(), err) } return } // 修改 func (dao *StockOutProductDaoImpl) Update_StockOutProduct(r StockOutProduct) error { qs := dao.orm.QueryTable(new(StockOutProduct)) var product StockOutProduct err := qs.Filter("T_number", r.T_number).Filter("T_product_id", r.T_product_id).One(&product) if err != nil { if err.Error() == orm.ErrNoRows.Error() { _, err = dao.orm.Insert(&product) if err != nil { logs.Error(lib.FuncName(), err) return err } else { return nil } } logs.Error(lib.FuncName(), err) return err } product.T_depot_id = r.T_depot_id product.T_num = r.T_num product.T_relation_sn = r.T_relation_sn _, err = dao.orm.Update(&product, "T_depot_id", "T_num", "T_relation_sn") if err != nil { logs.Error(lib.FuncName(), err) return err } return nil } func (dao *StockOutProductDaoImpl) Update_StockOutProduct_T_date(T_number, T_date string) error { qs := dao.orm.QueryTable(new(StockOutProduct)) var list []StockOutProduct _, err := qs.Filter("T_number", T_number).All(&list) if err != nil { logs.Error(lib.FuncName(), err) return err } for _, product := range list { product.T_date = T_date _, err = dao.orm.Update(&product, "T_date") if err != nil { logs.Error(lib.FuncName(), err) return err } } return nil } // 删除 func (dao *StockOutProductDaoImpl) Delete_StockOutProduct(T_number string, T_depot_id, T_product_id int) error { qs := dao.orm.QueryTable(new(StockOutProduct)) _, err := qs.Filter("T_number", T_number).Filter("T_depot_id", T_depot_id).Filter("T_product_id", T_product_id).Delete() if err != nil { logs.Error(lib.FuncName(), err) return err } return nil } // 根据库存id、产品id、月份获取本月出库总数量 func (dao *StockOutProductDaoImpl) Read_StockOut_Total(T_depot_id, T_product_id int, T_date string) int { 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 + "%'" var pl_lists orm2.ParamsList _, err := dao.orm.Raw(sql).ValuesFlat(&pl_lists) if err != nil { logs.Error(lib.FuncName(), err) return 0 } if pl_lists[0] == nil { return 0 } key, _ := strconv.Atoi(pl_lists[0].(string)) return key }