| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- package AfterSales
- import (
- "Cold_Api/conf"
- "Cold_Api/controllers/lib"
- "encoding/json"
- "fmt"
- "strconv"
- "time"
- "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/beego/beego/v2/core/logs"
- _ "github.com/go-sql-driver/mysql"
- )
- // 售后服务分类表
- type AfterSalesCategory struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_name string `orm:"size(256);null"` // 分类名称
- T_sort int `orm:"size(11);default(0)"` // 排序
- T_State int `orm:"size(2);default(1)"` // 0 删除 1 正常
- 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 AfterSalesCategory_R struct {
- Id int `json:"id"`
- T_name string `json:"t_name"` // 分类名称
- T_sort int `json:"t_sort"` // 排序
- T_State int `json:"t_state"` // 状态
- CreateTime time.Time `json:"create_time"` // 创建时间
- UpdateTime time.Time `json:"update_time"` // 更新时间
- }
- // 简化返回结构体
- type AfterSalesCategory_Simple struct {
- Id int `json:"id"`
- T_name string `json:"t_name"` // 分类名称
- T_sort int `json:"t_sort"` // 排序
- }
- // 分类统计返回结构体
- type AfterSalesCategory_Count_R struct {
- Id int `json:"id"`
- T_name string `json:"t_name"` // 分类名称
- T_sort int `json:"t_sort"` // 排序
- Count int64 `json:"count"` // 该分类下的售后服务数量
- }
- func (t *AfterSalesCategory) TableName() string {
- return "after_sales_category"
- }
- var redisCache_AfterSalesCategory cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(AfterSalesCategory))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_AfterSalesCategory", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- fmt.Println(config)
- var err error
- redisCache_AfterSalesCategory, err = cache.NewCache("redis", config)
- if err != nil || redisCache_AfterSalesCategory == nil {
- errMsg := "failed to init redis"
- logs.Error(errMsg, err)
- panic(errMsg)
- }
- }
- // ---------------- Redis 缓存方法 -------------------
- func Redis_AfterSalesCategory_Set(r AfterSalesCategory) (err error) {
- key := strconv.Itoa(r.Id)
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- err = redisCache_AfterSalesCategory.Put(key, str, 2*time.Hour)
- if err != nil {
- logs.Error("set key:", key, ",value:", str, err)
- }
- return
- }
- func Redis_AfterSalesCategory_Get(key string) (AfterSalesCategory, bool) {
- if redisCache_AfterSalesCategory.IsExist(key) {
- v := redisCache_AfterSalesCategory.Get(key)
- var r AfterSalesCategory
- err := json.Unmarshal(v.([]byte), &r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return AfterSalesCategory{}, false
- }
- return r, true
- }
- return AfterSalesCategory{}, false
- }
- func Redis_AfterSalesCategory_DelK(key string) (err error) {
- err = redisCache_AfterSalesCategory.Delete(key)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- return
- }
- // ---------------- 转换方法 -------------------
- func AfterSalesCategoryToAfterSalesCategory_R(t AfterSalesCategory) (r AfterSalesCategory_R) {
- r.Id = t.Id
- r.T_name = t.T_name
- r.T_sort = t.T_sort
- r.T_State = t.T_State
- r.CreateTime = t.CreateTime
- r.UpdateTime = t.UpdateTime
- return r
- }
- func AfterSalesCategoryToAfterSalesCategory_Simple(t AfterSalesCategory) (r AfterSalesCategory_Simple) {
- r.Id = t.Id
- r.T_name = t.T_name
- r.T_sort = t.T_sort
- return r
- }
- // 分类转换为统计结构体
- func AfterSalesCategoryToAfterSalesCategory_Count_R(t AfterSalesCategory, count int64) (r AfterSalesCategory_Count_R) {
- r.Id = t.Id
- r.T_name = t.T_name
- r.T_sort = t.T_sort
- r.Count = count
- return r
- }
- // ---------------- CRUD 方法 -------------------
- // 添加售后服务分类
- func Add_AfterSalesCategory(m AfterSalesCategory) (id int64, err error) {
- o := orm.NewOrm()
- id, err = o.Insert(&m)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- m.Id = int(id)
- Redis_AfterSalesCategory_Set(m)
- return
- }
- // 根据ID获取售后服务分类
- func Read_AfterSalesCategory_ById(id int) (r AfterSalesCategory, err error) {
- key := strconv.Itoa(id)
- if r, is := Redis_AfterSalesCategory_Get(key); is {
- return r, nil
- }
- o := orm.NewOrm()
- r = AfterSalesCategory{Id: id, T_State: 1}
- err = o.Read(&r, "Id", "T_State")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return r, err
- }
- Redis_AfterSalesCategory_Set(r)
- return r, err
- }
- // 修改售后服务分类
- func Update_AfterSalesCategory(r AfterSalesCategory, cols ...string) bool {
- o := orm.NewOrm()
- num, err := o.Update(&r, cols...)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return false
- }
- logs.Info("Number of records updated in database:", num)
- Redis_AfterSalesCategory_Set(r)
- return true
- }
- // 删除售后服务分类(软删除)
- func Delete_AfterSalesCategory_ById(id int) bool {
- o := orm.NewOrm()
- v := AfterSalesCategory{Id: id}
- if err := o.Read(&v); err == nil {
- var num int64
- v.T_State = 0
- num, err = o.Update(&v, "T_State")
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return false
- }
- logs.Info("Number of records updated in database:", num)
- key := strconv.Itoa(v.Id)
- Redis_AfterSalesCategory_DelK(key)
- return true
- }
- return false
- }
- // 获取售后服务分类列表
- func Read_AfterSalesCategory_List(T_name string, page int, page_z int) (r []AfterSalesCategory_R, cnt int64) {
- o := orm.NewOrm()
- var map_r []AfterSalesCategory
- qs := o.QueryTable(new(AfterSalesCategory))
- 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 len(T_name) > 0 {
- cond1 = cond1.And("T_name__icontains", T_name)
- }
- _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-T_sort", "-Id").All(&map_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 map_r {
- r = append(r, AfterSalesCategoryToAfterSalesCategory_R(v))
- }
- return r, cnt
- }
- // 获取所有售后服务分类(简化版本,用于下拉选择等)
- func Read_AfterSalesCategory_All() (r []AfterSalesCategory_Simple) {
- o := orm.NewOrm()
- var map_r []AfterSalesCategory
- qs := o.QueryTable(new(AfterSalesCategory))
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1)
- _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "-Id").All(&map_r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- for _, v := range map_r {
- r = append(r, AfterSalesCategoryToAfterSalesCategory_Simple(v))
- }
- return r
- }
- // 根据分类获取售后服务数量
- func Read_AfterSales_Count_ByCategoryId(T_category_id int) int64 {
- o := orm.NewOrm()
- qs := o.QueryTable(new(AfterSales))
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1).And("T_category", T_category_id)
- cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return 0
- }
- return cnt
- }
- // 检查分类名称是否存在(用于验证重复)
- func Check_AfterSalesCategory_Name_Exists(T_name string, excludeId int) bool {
- o := orm.NewOrm()
- qs := o.QueryTable(new(AfterSalesCategory))
- cond := orm.NewCondition()
- cond1 := cond.And("T_State", 1).And("T_name", T_name)
- if excludeId > 0 {
- cond1 = cond1.AndNot("Id", excludeId)
- }
- cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return false
- }
- return cnt > 0
- }
- // 初始化默认分类数据(可在系统启动时调用)
- func Init_Default_Categories() error {
- defaultCategories := []AfterSalesCategory{
- {T_name: "硬件介绍", T_sort: 1},
- {T_name: "软件介绍", T_sort: 2},
- {T_name: "安装指导", T_sort: 3},
- {T_name: "操作指导", T_sort: 4},
- {T_name: "异常处理方案", T_sort: 5},
- {T_name: "文献查阅", T_sort: 6},
- {T_name: "技术交流", T_sort: 7},
- {T_name: "售后联系", T_sort: 8},
- }
- for _, category := range defaultCategories {
- // 检查是否已存在
- if !Check_AfterSalesCategory_Name_Exists(category.T_name, 0) {
- _, err := Add_AfterSalesCategory(category)
- if err != nil {
- logs.Error("Init default category failed:", category.T_name, err)
- return err
- }
- }
- }
- return nil
- }
|