123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- 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 StockInProduct 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 保存时都会对时间自动更新
- }
- // 入库列表
- type StockInProductList struct {
- Id int
- T_number string // StockIn - 入库单号
- T_depot_id int // StockIn - 仓库id
- T_depot_name string // StockIn - 仓库名称
- T_date string // StockIn - 业务日期
- T_submit string // StockIn - 经办人
- T_submit_name string // StockIn - 经办人名称
- T_remark string // StockIn - 备注
- 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 (t *StockInProduct) TableName() string {
- return "stock_in_product" // 数据库名称 // ************** 替换 FormulaList **************
- }
- type StockInProductDaoImpl struct {
- orm orm.Ormer
- }
- func NewStockInProduct(orm orm.Ormer) *StockInProductDaoImpl {
- return &StockInProductDaoImpl{orm: orm}
- }
- func init() {
- //注册模型
- orm.RegisterModel(new(StockInProduct))
- }
- type StockInProduct_R struct {
- Id int
- T_number string // 入库单号
- T_depot_id int // 仓库id
- T_num int // 入库数量
- T_date string // 日期
- //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 StockInProductToStockInProduct_R(t StockInProduct) (r StockInProduct_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
- }
- func StockInProductListToStockInProductList(t StockInProductList) (r StockInProductList) {
- 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_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_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 *StockInProductDaoImpl) Add_StockInProduct(r StockInProduct) (id int64, err error) {
- id, err = dao.orm.Insert(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return id, err
- }
- // 通过入库编号查询入库产品列表
- func (dao *StockInProductDaoImpl) Read_StockInProduct_List_ByT_number(T_number string) (r []StockInProduct) {
- qs := dao.orm.QueryTable(new(StockInProduct))
- _, err := qs.Filter("T_number", T_number).All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return
- }
- // 修改
- func (dao *StockInProductDaoImpl) Update_StockInProduct(r StockInProduct) error {
- qs := dao.orm.QueryTable(new(StockInProduct))
- var product StockInProduct
- 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 *StockInProductDaoImpl) Update_StockInProduct_T_date(T_number, T_date string) error {
- qs := dao.orm.QueryTable(new(StockInProduct))
- var list []StockInProduct
- _, 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 *StockInProductDaoImpl) Delete_StockInProduct(T_number string, T_depot_id, T_product_id int) error {
- qs := dao.orm.QueryTable(new(StockInProduct))
- _, 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 *StockInProductDaoImpl) Read_StockIn_Total(T_depot_id, T_product_id int, T_date string) int {
- 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 + "%'"
- 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
- }
|