package Stock import ( "ERP_storage/models/Account" "ERP_storage/models/Basic" "fmt" "gogs.baozhida.cn/zoie/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" ) // 入库 type StockIn struct { Id int `orm:"column(ID);size(11);auto;pk"` T_number string `orm:"size(256);null"` // 入库单号 T_type int `orm:"size(256);;default(1)"` // 入库类型 1-入库 2-退库 T_depot_id int `orm:"size(20);null"` // 仓库id T_date string `orm:"size(256);null"` // 业务日期 T_submit string `orm:"size(256);null"` // 经办人 T_remark string `orm:"type(text);null"` // 备注 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 *StockIn) TableName() string { return "stock_in" // 数据库名称 // ************** 替换 FormulaList ************** } type StockInDaoImpl struct { orm orm.Ormer } func NewStockIn(orm orm.Ormer) *StockInDaoImpl { return &StockInDaoImpl{orm: orm} } func init() { //注册模型 orm.RegisterModel(new(StockIn)) } type StockIn_R struct { Id int T_number string T_type int T_depot_id int T_depot_name string T_date string T_submit string T_submit_name string T_remark string } type StockIn_Detail struct { Id int T_number string T_type int T_depot_id int T_depot_name string T_date string T_submit string T_submit_name string T_remark string T_Product []StockInProduct_R } func StockInToStockIn_R(t StockIn) (r StockIn_R) { r.Id = t.Id r.T_number = t.T_number r.T_type = t.T_type r.T_depot_id = t.T_depot_id r.T_depot_name = Basic.Read_Depot_Get(t.T_depot_id) r.T_date = t.T_date r.T_submit = t.T_submit r.T_submit_name = Account.Read_User_T_name_Get(t.T_submit) r.T_remark = t.T_remark return r } func StockInToStockIn_Detail(t StockIn, productList []StockInProduct_R) (r StockIn_Detail) { r.Id = t.Id r.T_number = t.T_number r.T_type = t.T_type r.T_depot_id = t.T_depot_id r.T_depot_name = Basic.Read_Depot_Get(t.T_depot_id) r.T_date = t.T_date r.T_submit = t.T_submit r.T_submit_name = Account.Read_User_T_name_Get(t.T_submit) r.T_remark = t.T_remark r.T_Product = productList return r } // 添加 func (dao *StockInDaoImpl) Add_StockIn(r StockIn) (id int64, err error) { id, err = dao.orm.Insert(&r) if err != nil { logs.Error(lib.FuncName(), err) } return id, err } // 获取 ById func (dao *StockInDaoImpl) Read_StockIn_ByT_number(T_number string) (r StockIn, err error) { qs := dao.orm.QueryTable(new(StockIn)) err = qs.Filter("T_number", T_number).One(&r) if err != nil { logs.Error(lib.FuncName(), err) } return } // 修改 func (dao *StockInDaoImpl) Update_StockIn(m StockIn, cols ...string) error { _, err := dao.orm.Update(&m, cols...) if err != nil { logs.Error(lib.FuncName(), err) return err } return nil } // 删除 func (dao *StockInDaoImpl) Delete_StockIn(m StockIn) error { _, err := dao.orm.Delete(&m) if err != nil { logs.Error(lib.FuncName(), err) return err } return nil } // 获取列表 func (dao *StockInDaoImpl) Read_StockIn_List(T_depot_id int, T_start_date, T_end_date string, page, page_z int) (r_ []StockIn_R, cnt int64) { qs := dao.orm.QueryTable(new(StockIn)) var offset int64 if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } // 过滤 cond := orm.NewCondition() if T_depot_id > 0 { cond = cond.And("T_depot_id", T_depot_id) } if len(T_start_date) > 0 { cond = cond.And("T_date__gte", T_start_date) } if len(T_end_date) > 0 { cond = cond.And("T_date__lte", T_end_date) } // 查询 var r []StockIn _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&r) if err != nil { logs.Error(lib.FuncName(), err) return } cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count() if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range r { r_ = append(r_, StockInToStockIn_R(v)) } return r_, cnt } // 通过入库编号查询入库产品列表 func (dao *StockInDaoImpl) Read_StockInProduct_List(T_name, T_start_date, T_end_date string, T_depot_id int, T_product_name, T_product_model string, page int, page_z int) ( r_ []StockInProductList, cnt int64) { var offset int if page <= 1 { offset = 0 } else { offset = (page - 1) * page_z } var maps_z []orm2.ParamsList var r []StockInProductList sqlWhere := "" if len(T_name) > 0 { sqlWhere += " AND (si.t_number like \"%" + T_name + "%\" or si.t_remark like \"%" + T_name + "%\")" } if T_depot_id > 0 { sqlWhere += fmt.Sprintf(" AND si.t_depot_id = %d", T_depot_id) } if len(T_start_date) > 0 { sqlWhere += " AND si.t_date >= \"" + T_start_date + "\"" } if len(T_end_date) > 0 { sqlWhere += " AND si.t_date <= \"" + T_end_date + "\"" } if len(T_product_name) > 0 { sqlWhere += " AND p.t_name like \"%" + T_product_name + "%\"" } if len(T_product_model) > 0 { sqlWhere += " AND p.t_model like \"%" + T_product_model + "%\"" } // 获取总条数 sql := "SELECT COUNT(product.ID) FROM stock_in_product product " + "JOIN product p ON product.t_product_id = p.ID " + "LEFT JOIN stock_in si ON si.t_number=product.t_number WHERE 1=1 " sql = sql + sqlWhere fmt.Println(sql) _, err := dao.orm.Raw(sql).ValuesList(&maps_z) if err != nil { return r, 0 } if len(maps_z) == 0 { return r, 0 } sql = "SELECT si.t_number as t_number,si.t_depot_id as t_depot_id,si.t_date as t_date,product.*,si.* FROM stock_in_product product " + "JOIN product p ON product.t_product_id = p.ID " + "LEFT JOIN stock_in si ON si.t_number=product.t_number WHERE 1=1" sql = sql + sqlWhere sql += " ORDER BY si.t_date DESC" if page_z != 9999 { sql += " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z) } fmt.Println(sql) _, err = dao.orm.Raw(sql).QueryRows(&r) for _, v := range r { r_ = append(r_, StockInProductListToStockInProductList(v)) } count, _ := strconv.Atoi(maps_z[0][0].(string)) return r_, int64(count) }