123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- package Stock
- import (
- "ERP_storage/models/Basic"
- "git.baozhida.cn/ERP_libs/lib"
- orm2 "github.com/beego/beego/v2/client/orm"
- "time"
- "ERP_storage/logs"
- _ "github.com/astaxie/beego/cache/redis"
- "github.com/beego/beego/v2/adapter/orm"
- _ "github.com/go-sql-driver/mysql"
- )
- // 库存
- type StockMonth struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_depot_id int `orm:"size(20);null"` // 仓库id
- T_product_id int `orm:"size(20);null"` // 产品id
- T_product_class int `orm:"size(20);null"` // 产品分类
- T_month string `orm:"size(256);null"` // 月份
- T_in int `orm:"size(20);null"` // 当月入库
- T_out int `orm:"size(20);null"` // 当月出库
- T_beginning int `orm:"size(20);null"` // 期初库存
- T_ending int `orm:"size(20);null"` // 期末库存
- T_project 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 *StockMonth) TableName() string {
- return "stock_month" // 数据库名称 // ************** 替换 FormulaList **************
- }
- type StockMonthDaoImpl struct {
- orm orm.Ormer
- }
- func NewStockMonth(orm orm.Ormer) *StockMonthDaoImpl {
- return &StockMonthDaoImpl{orm: orm}
- }
- func init() {
- //注册模型
- orm.RegisterModel(new(StockMonth))
- }
- type StockMonth_R struct {
- Id int
- T_depot_id int // 仓库id
- T_month string // 月份
- T_in int // 当月入库
- T_out int // 当月出库
- T_beginning int // 期初库存
- T_ending int // 期末库存
- T_project string // 关联项目
- // ---------产品信息-----------
- T_product_id int
- T_product_class int
- T_product_name string
- T_product_model string
- T_product_spec string
- }
- func StockMonthToStockMonth_R(t StockMonth) (r StockMonth_R) {
- r.Id = t.Id
- r.T_depot_id = t.T_depot_id
- r.T_product_id = t.T_product_id
- r.T_month = t.T_month
- r.T_in = t.T_in
- r.T_out = t.T_out
- r.T_beginning = t.T_beginning
- r.T_ending = t.T_ending
- r.T_project = t.T_project
- r.T_product_id = t.T_product_id
- r.T_product_class = t.T_product_class
- product, _ := Basic.Read_Product_ById(t.T_product_id)
- r.T_product_name = product.T_name
- r.T_product_model = product.T_model
- r.T_product_spec = product.T_spec
- return r
- }
- // 添加
- func (dao *StockMonthDaoImpl) Add_StockMonth(r StockMonth) (id int64, err error) {
- id, err = dao.orm.Insert(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return id, err
- }
- // 获取 上月数据
- func (dao *StockMonthDaoImpl) Read_LastMonth_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id int, T_month string) (r []StockMonth) {
- qs := dao.orm.QueryTable(new(StockMonth))
- var maps []StockMonth
- _, err := qs.Limit(1, 0).Filter("T_depot_id", T_depot_id).Filter("T_product_id", T_product_id).
- Filter("T_month__lt", T_month).OrderBy("-T_month").All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return maps
- }
- // 获取本月数据
- func (dao *StockMonthDaoImpl) Read_StockMonth_ByT_depot_id_T_product_id(T_depot_id, T_product_id int, T_month string) (r StockMonth) {
- qs := dao.orm.QueryTable(new(StockMonth))
- var maps StockMonth
- err := qs.Filter("T_depot_id", T_depot_id).Filter("T_product_id", T_product_id).
- Filter("T_month", T_month).OrderBy("-T_month").One(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return maps
- }
- // 获取列表
- func (dao *StockMonthDaoImpl) Read_StockMonth_List(T_depot_id, T_product_id, T_product_class int, start_date, end_date string) (r_ []StockMonth_R) {
- qs := dao.orm.QueryTable(new(StockMonth))
- // 过滤
- cond := orm.NewCondition()
- if T_depot_id > 0 {
- cond = cond.And("T_depot_id", T_depot_id)
- }
- if T_product_id > 0 {
- cond = cond.And("T_product_id", T_product_id)
- }
- if T_product_class > 0 {
- cond = cond.And("T_product_class", T_product_class)
- }
- if len(start_date) > 0 {
- cond = cond.And("T_month__gte", start_date)
- }
- if len(end_date) > 0 {
- cond = cond.And("T_month__lte", end_date)
- }
- // 查询
- var r []StockMonth
- _, err := qs.SetCond((*orm2.Condition)(cond)).OrderBy("T_product_id").OrderBy("T_month").All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- for _, v := range r {
- r_ = append(r_, StockMonthToStockMonth_R(v))
- }
- return r_
- }
- // 同步库存产品信息
- func (dao *StockMonthDaoImpl) Read_StockMonth_ByT_product_id(T_product_id int) (r []StockMonth, err error) {
- qs := dao.orm.QueryTable(new(StockMonth))
- _, err = qs.Filter("T_product_id", T_product_id).All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return
- }
- // 修改
- func (dao *StockMonthDaoImpl) Update_StockMonth(m StockMonth, cols ...string) error {
- _, err := dao.orm.Update(&m, cols...)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- return nil
- }
|