AfterSalesCategory.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. package AfterSales
  2. import (
  3. "Cold_Api/conf"
  4. "Cold_Api/controllers/lib"
  5. "encoding/json"
  6. "fmt"
  7. "strconv"
  8. "time"
  9. "github.com/astaxie/beego/cache"
  10. _ "github.com/astaxie/beego/cache/redis"
  11. "github.com/beego/beego/v2/adapter/orm"
  12. orm2 "github.com/beego/beego/v2/client/orm"
  13. "github.com/beego/beego/v2/core/logs"
  14. _ "github.com/go-sql-driver/mysql"
  15. )
  16. // 售后服务分类表
  17. type AfterSalesCategory struct {
  18. Id int `orm:"column(ID);size(11);auto;pk"`
  19. T_name string `orm:"size(256);null"` // 分类名称
  20. T_sort int `orm:"size(11);default(0)"` // 排序
  21. T_State int `orm:"size(2);default(1)"` // 0 删除 1 正常
  22. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  23. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新
  24. }
  25. // 返回结构体
  26. type AfterSalesCategory_R struct {
  27. Id int `json:"id"`
  28. T_name string `json:"t_name"` // 分类名称
  29. T_sort int `json:"t_sort"` // 排序
  30. T_State int `json:"t_state"` // 状态
  31. CreateTime time.Time `json:"create_time"` // 创建时间
  32. UpdateTime time.Time `json:"update_time"` // 更新时间
  33. }
  34. // 简化返回结构体
  35. type AfterSalesCategory_Simple struct {
  36. Id int `json:"id"`
  37. T_name string `json:"t_name"` // 分类名称
  38. T_sort int `json:"t_sort"` // 排序
  39. }
  40. // 分类统计返回结构体
  41. type AfterSalesCategory_Count_R struct {
  42. Id int `json:"id"`
  43. T_name string `json:"t_name"` // 分类名称
  44. T_sort int `json:"t_sort"` // 排序
  45. Count int64 `json:"count"` // 该分类下的售后服务数量
  46. }
  47. func (t *AfterSalesCategory) TableName() string {
  48. return "after_sales_category"
  49. }
  50. var redisCache_AfterSalesCategory cache.Cache
  51. func init() {
  52. //注册模型
  53. orm.RegisterModel(new(AfterSalesCategory))
  54. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  55. "redis_AfterSalesCategory", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  56. fmt.Println(config)
  57. var err error
  58. redisCache_AfterSalesCategory, err = cache.NewCache("redis", config)
  59. if err != nil || redisCache_AfterSalesCategory == nil {
  60. errMsg := "failed to init redis"
  61. logs.Error(errMsg, err)
  62. panic(errMsg)
  63. }
  64. }
  65. // ---------------- Redis 缓存方法 -------------------
  66. func Redis_AfterSalesCategory_Set(r AfterSalesCategory) (err error) {
  67. key := strconv.Itoa(r.Id)
  68. //json序列化
  69. str, err := json.Marshal(r)
  70. if err != nil {
  71. logs.Error(lib.FuncName(), err)
  72. return
  73. }
  74. err = redisCache_AfterSalesCategory.Put(key, str, 2*time.Hour)
  75. if err != nil {
  76. logs.Error("set key:", key, ",value:", str, err)
  77. }
  78. return
  79. }
  80. func Redis_AfterSalesCategory_Get(key string) (AfterSalesCategory, bool) {
  81. if redisCache_AfterSalesCategory.IsExist(key) {
  82. v := redisCache_AfterSalesCategory.Get(key)
  83. var r AfterSalesCategory
  84. err := json.Unmarshal(v.([]byte), &r)
  85. if err != nil {
  86. logs.Error(lib.FuncName(), err)
  87. return AfterSalesCategory{}, false
  88. }
  89. return r, true
  90. }
  91. return AfterSalesCategory{}, false
  92. }
  93. func Redis_AfterSalesCategory_DelK(key string) (err error) {
  94. err = redisCache_AfterSalesCategory.Delete(key)
  95. if err != nil {
  96. logs.Error(lib.FuncName(), err)
  97. return
  98. }
  99. return
  100. }
  101. // ---------------- 转换方法 -------------------
  102. func AfterSalesCategoryToAfterSalesCategory_R(t AfterSalesCategory) (r AfterSalesCategory_R) {
  103. r.Id = t.Id
  104. r.T_name = t.T_name
  105. r.T_sort = t.T_sort
  106. r.T_State = t.T_State
  107. r.CreateTime = t.CreateTime
  108. r.UpdateTime = t.UpdateTime
  109. return r
  110. }
  111. func AfterSalesCategoryToAfterSalesCategory_Simple(t AfterSalesCategory) (r AfterSalesCategory_Simple) {
  112. r.Id = t.Id
  113. r.T_name = t.T_name
  114. r.T_sort = t.T_sort
  115. return r
  116. }
  117. // 分类转换为统计结构体
  118. func AfterSalesCategoryToAfterSalesCategory_Count_R(t AfterSalesCategory, count int64) (r AfterSalesCategory_Count_R) {
  119. r.Id = t.Id
  120. r.T_name = t.T_name
  121. r.T_sort = t.T_sort
  122. r.Count = count
  123. return r
  124. }
  125. // ---------------- CRUD 方法 -------------------
  126. // 添加售后服务分类
  127. func Add_AfterSalesCategory(m AfterSalesCategory) (id int64, err error) {
  128. o := orm.NewOrm()
  129. id, err = o.Insert(&m)
  130. if err != nil {
  131. logs.Error(lib.FuncName(), err)
  132. return
  133. }
  134. m.Id = int(id)
  135. Redis_AfterSalesCategory_Set(m)
  136. return
  137. }
  138. // 根据ID获取售后服务分类
  139. func Read_AfterSalesCategory_ById(id int) (r AfterSalesCategory, err error) {
  140. key := strconv.Itoa(id)
  141. if r, is := Redis_AfterSalesCategory_Get(key); is {
  142. return r, nil
  143. }
  144. o := orm.NewOrm()
  145. r = AfterSalesCategory{Id: id, T_State: 1}
  146. err = o.Read(&r, "Id", "T_State")
  147. if err != nil {
  148. logs.Error(lib.FuncName(), err)
  149. return r, err
  150. }
  151. Redis_AfterSalesCategory_Set(r)
  152. return r, err
  153. }
  154. // 修改售后服务分类
  155. func Update_AfterSalesCategory(r AfterSalesCategory, cols ...string) bool {
  156. o := orm.NewOrm()
  157. num, err := o.Update(&r, cols...)
  158. if err != nil {
  159. logs.Error(lib.FuncName(), err)
  160. return false
  161. }
  162. logs.Info("Number of records updated in database:", num)
  163. Redis_AfterSalesCategory_Set(r)
  164. return true
  165. }
  166. // 删除售后服务分类(软删除)
  167. func Delete_AfterSalesCategory_ById(id int) bool {
  168. o := orm.NewOrm()
  169. v := AfterSalesCategory{Id: id}
  170. if err := o.Read(&v); err == nil {
  171. var num int64
  172. v.T_State = 0
  173. num, err = o.Update(&v, "T_State")
  174. if err != nil {
  175. logs.Error(lib.FuncName(), err)
  176. return false
  177. }
  178. logs.Info("Number of records updated in database:", num)
  179. key := strconv.Itoa(v.Id)
  180. Redis_AfterSalesCategory_DelK(key)
  181. return true
  182. }
  183. return false
  184. }
  185. // 获取售后服务分类列表
  186. func Read_AfterSalesCategory_List(T_name string, page int, page_z int) (r []AfterSalesCategory_R, cnt int64) {
  187. o := orm.NewOrm()
  188. var map_r []AfterSalesCategory
  189. qs := o.QueryTable(new(AfterSalesCategory))
  190. var offset int64
  191. if page <= 1 {
  192. offset = 0
  193. } else {
  194. offset = int64((page - 1) * page_z)
  195. }
  196. cond := orm.NewCondition()
  197. cond1 := cond.And("T_State", 1)
  198. if len(T_name) > 0 {
  199. cond1 = cond1.And("T_name__icontains", T_name)
  200. }
  201. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-T_sort", "-Id").All(&map_r)
  202. if err != nil {
  203. logs.Error(lib.FuncName(), err)
  204. return
  205. }
  206. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  207. if err != nil {
  208. logs.Error(lib.FuncName(), err)
  209. return
  210. }
  211. for _, v := range map_r {
  212. r = append(r, AfterSalesCategoryToAfterSalesCategory_R(v))
  213. }
  214. return r, cnt
  215. }
  216. // 获取所有售后服务分类(简化版本,用于下拉选择等)
  217. func Read_AfterSalesCategory_All() (r []AfterSalesCategory_Simple) {
  218. o := orm.NewOrm()
  219. var map_r []AfterSalesCategory
  220. qs := o.QueryTable(new(AfterSalesCategory))
  221. cond := orm.NewCondition()
  222. cond1 := cond.And("T_State", 1)
  223. _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "-Id").All(&map_r)
  224. if err != nil {
  225. logs.Error(lib.FuncName(), err)
  226. return
  227. }
  228. for _, v := range map_r {
  229. r = append(r, AfterSalesCategoryToAfterSalesCategory_Simple(v))
  230. }
  231. return r
  232. }
  233. // 根据分类获取售后服务数量
  234. func Read_AfterSales_Count_ByCategoryId(T_category_id int) int64 {
  235. o := orm.NewOrm()
  236. qs := o.QueryTable(new(AfterSales))
  237. cond := orm.NewCondition()
  238. cond1 := cond.And("T_State", 1).And("T_category", T_category_id)
  239. cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count()
  240. if err != nil {
  241. logs.Error(lib.FuncName(), err)
  242. return 0
  243. }
  244. return cnt
  245. }
  246. // 检查分类名称是否存在(用于验证重复)
  247. func Check_AfterSalesCategory_Name_Exists(T_name string, excludeId int) bool {
  248. o := orm.NewOrm()
  249. qs := o.QueryTable(new(AfterSalesCategory))
  250. cond := orm.NewCondition()
  251. cond1 := cond.And("T_State", 1).And("T_name", T_name)
  252. if excludeId > 0 {
  253. cond1 = cond1.AndNot("Id", excludeId)
  254. }
  255. cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count()
  256. if err != nil {
  257. logs.Error(lib.FuncName(), err)
  258. return false
  259. }
  260. return cnt > 0
  261. }
  262. // 初始化默认分类数据(可在系统启动时调用)
  263. func Init_Default_Categories() error {
  264. defaultCategories := []AfterSalesCategory{
  265. {T_name: "硬件介绍", T_sort: 1},
  266. {T_name: "软件介绍", T_sort: 2},
  267. {T_name: "安装指导", T_sort: 3},
  268. {T_name: "操作指导", T_sort: 4},
  269. {T_name: "异常处理方案", T_sort: 5},
  270. {T_name: "文献查阅", T_sort: 6},
  271. {T_name: "技术交流", T_sort: 7},
  272. {T_name: "售后联系", T_sort: 8},
  273. }
  274. for _, category := range defaultCategories {
  275. // 检查是否已存在
  276. if !Check_AfterSalesCategory_Name_Exists(category.T_name, 0) {
  277. _, err := Add_AfterSalesCategory(category)
  278. if err != nil {
  279. logs.Error("Init default category failed:", category.T_name, err)
  280. return err
  281. }
  282. }
  283. }
  284. return nil
  285. }