123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- package Basic
- import (
- "ERP_storage/conf"
- "ERP_storage/logs"
- "encoding/json"
- "errors"
- "fmt"
- "git.baozhida.cn/ERP_libs/lib"
- "github.com/astaxie/beego/cache"
- _ "github.com/astaxie/beego/cache/redis"
- "github.com/beego/beego/v2/adapter/orm"
- orm2 "github.com/beego/beego/v2/client/orm"
- _ "github.com/go-sql-driver/mysql"
- "strconv"
- "time"
- )
- // 仓库
- type Product struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_name string `orm:"size(256);null"` // 名称
- T_img string `orm:"size(256);null"` // 图片
- T_class int `orm:"size(20);default(0)"` // 产品分类 T_class=0 冷链验证
- T_model string `orm:"size(20);default(0)"` // 产品型号
- T_spec string `orm:"size(20);default(0)"` // 产品规格
- T_relation_sn int `orm:"size(2);default(0)"` // 关联sn 0-否 1-是
- T_State int `orm:"size(2);default(1)"` // 0 删除(伪删除) 1 正常
- T_remark string `orm:"type(text);null"` // 备注
- T_price float32 `orm:"digits(12);decimals(2)"` // 单价
- T_sort int `orm:"size(20);default(0)"` // 排序
- 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 *Product) TableName() string {
- return "product" // 数据库名称 // ************** 替换 FormulaList **************
- }
- var redisCache_Product cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(Product))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_Product", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- fmt.Println(config)
- var err error
- redisCache_Product, err = cache.NewCache("redis", config)
- if err != nil || redisCache_Product == nil {
- errMsg := "failed to init redis"
- logs.Error(errMsg, err)
- panic(errMsg)
- }
- }
- // ---------------- Redis -------------------
- func Redis_Product_Set(r Product) (err error) {
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- err = redisCache_Product.Put(strconv.Itoa(r.Id), str, 24*time.Hour)
- if err != nil {
- logs.Error("set key:", r.Id, ",value:", str, err)
- }
- return
- }
- func Redis_Product_Get(Id int) (r Product, err error) {
- key := strconv.Itoa(Id)
- if redisCache_Product.IsExist(key) {
- //println("找到key:",key)
- v := redisCache_Product.Get(key)
- err = json.Unmarshal(v.([]byte), &r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return Product{}, err
- }
- return r, nil
- }
- return Product{}, errors.New("未找到")
- }
- func Redis_Product_DelK(Id int) (err error) {
- key := strconv.Itoa(Id)
- err = redisCache_Product.Delete(key)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- return
- }
- type Product_R struct {
- Id int
- T_name string // 名称
- T_class int // 产品分类
- T_class_name string // 产品分类名称
- T_model string // 产品型号
- T_spec string // 产品规格
- T_price float32 // 单价
- T_relation_sn int // 关联sn 0-否 1-是
- T_img string // 图片
- T_remark string // 备注
- T_sort int // 排序
- }
- func ProductToProduct_R(t Product) (r Product_R) {
- r.Id = t.Id
- r.T_name = t.T_name
- r.T_name = t.T_name
- r.T_class = t.T_class
- r.T_class_name = Read_ProductClass_Get(t.T_class)
- r.T_model = t.T_model
- r.T_spec = t.T_spec
- r.T_price = t.T_price
- r.T_relation_sn = t.T_relation_sn
- r.T_img = t.T_img
- r.T_remark = t.T_remark
- r.T_sort = t.T_sort
- return r
- }
- // 添加
- func Add_Product(r Product) (id int64, err error) {
- o := orm.NewOrm()
- id, err = o.Insert(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return id, err
- }
- Redis_Product_Set(r)
- return id, err
- }
- // 获取 ById
- func Read_Product_ById(Id int) (r Product, err error) {
- if r, err = Redis_Product_Get(Id); err == nil {
- return
- }
- o := orm.NewOrm()
- qs := o.QueryTable(new(Product))
- err = qs.Filter("Id", Id).One(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- Redis_Product_Set(r)
- return
- }
- func Read_Product_ByT_name(T_name, T_model, T_spec string) (r Product, err error) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(Product))
- err = qs.Filter("T_name", T_name).Filter("T_model", T_model).Filter("T_spec", T_spec).Filter("T_class__gt", 0).Filter("T_State", 1).One(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- return
- }
- // 修改
- func Update_Product(m Product, cols ...string) error {
- o := orm.NewOrm()
- _, err := o.Update(&m, cols...)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- Redis_Product_Set(m)
- return nil
- }
- // 删除
- func Delete_Product(v Product) error {
- o := orm.NewOrm()
- v.T_State = 0
- _, err := o.Update(&v, "T_State")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return err
- }
- // 获取列表
- func Read_Product_List(T_name, T_model string, T_class, T_VerifyContract, page, page_z int) (r_ []Product_R, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Product))
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- // 过滤
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1)
- if T_VerifyContract == 1 {
- cond1 = cond1.And("T_class__lte", 0)
- } else {
- cond1 = cond1.And("T_class__gt", 0)
- }
- if T_class > 0 {
- cond1 = cond1.And("T_class", T_class)
- }
- if len(T_model) > 0 {
- cond1 = cond1.And("T_model__icontains", T_model)
- }
- if len(T_name) > 0 {
- cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_model__icontains", T_name))
- }
- // 查询
- var r []Product
- _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort").All(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- for _, v := range r {
- r_ = append(r_, ProductToProduct_R(v))
- }
- return r_, cnt
- }
- // 获取产品型号
- func Read_ProductModel_List(T_name string) (lists orm2.ParamsList) {
- o := orm.NewOrm()
- var pl_lists orm2.ParamsList
- _, err := o.Raw("SELECT t_model FROM product WHERE t_class > 0 AND t_name = '" + T_name + "' LIMIT 0,1000").ValuesFlat(&pl_lists)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- return pl_lists
- }
- // 获取产品名称
- func Read_ProductName_List(T_name string, T_class int) (lists orm2.ParamsList) {
- o := orm.NewOrm()
- var pl_lists orm2.ParamsList
- sql := "SELECT DISTINCT t_name FROM product WHERE t_class > 0 AND t_name like '%" + T_name + "%'"
- if T_class > 0 {
- sql += " AND T_class = " + strconv.Itoa(T_class)
- }
- sql += " LIMIT 0,1000"
- _, err := o.Raw(sql).ValuesFlat(&pl_lists)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- return pl_lists
- }
|