今天不分享技术,也没学到啥技术…….
今天做了一晚上的阅读理解 
起因是老板让我写 project/  级别的公共库操作
其实这个project/ 级别的操作跟之前 清晨写的 lib 公共库一毛一样
我还以为我需要直接全部把代码搬过来
然后新建一堆数据库进行操作,没想到老板是让我在原来的数据库上进行操作,并且把清晨的代码改进一下–> 从只有system版本 到 有两个版本
然后我就蒙蔽了,他让我写路由,写尼玛,没有丝毫头绪,如果是在同一张表上面修改,就是在scope上面做文章
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 type  LibScore struct  {	model.Base 	Name    string                       `gorm:"comment:指标名称"`   	Tags    datatypes.JSONSlice[string ] `gorm:"comment:指标标签"`  	Scope   string                       `gorm:"comment:'指标等级: 系统级 system, 项目级 project/xxx'"`  	Content []LibScoreContent           `gorm:"foreignkey:LibScoreID;"`  } type  LibScoreContent struct  {	model.Base 	LibScoreID string          `gorm:"size:26;index;comment:所属指标id"`  	StaffId    string          `gorm:"size:19;comment:用户工号"`  	Score      string          `gorm:"comment:指标内容分数/等级"`  	Extra      datatypes.JSON `gorm:"comment:额外信息"`  } 
 
然后让我最蒙蔽的事情来了,他们说引入一个 projectId ,我一直误以为这个变量可能是,LibScore 的 ID 或者是某个成绩表的某个ID
然后我就蒙蔽了半天,准确来说是有半天的实践,反正几个小时吧。
后来才发现 projectId 原来是 随便输入的一个ID,跟这两个结构体都没有关系,真尼玛啊
后来的代码就可以顺理成章的写出来了。。。。。。
果真只是 代码复用而已
不得不说老板太聪明了,能想到这种办法。
代码分享 随便看看吧,我只是当作记录成果用的
System状态下
展现成绩列表 
展现某张表的所有成绩 
展现某张表的某个人的成绩 
 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 func  AppLibInit (e *flamego.Flame)   {	e.Group("/lib" , func ()   { 		e.Group("/score" , func ()   { 			e.Combo("" ). 				Get(web.InjectPaginate(), handler.HandleLibList).                         				Post(binding.JSON(dto.CreateLibScoreRequest{}), handler.HandleLibCreate)  			e.Combo("/{id}" ). 				Get(handler.HandleLibDetail).                                             				Put(binding.JSON(dto.CreateLibScoreRequest{}), handler.HandleLibUpdate).  				Delete(handler.HandleLibDelete)                                           			e.Combo("/{id}/content" ). 				Get(web.InjectPaginate(), 					handler.HandleLibContentList).  				Post(binding.JSON([]dto.CreateLibContentRequest{}), 					handler.HandleLibContentCreate)  			e.Post("/{id}/content/delete" , binding.JSON(dto.LibScoreContentDeleteRequest{}), 				handler.HandleLibContentDeleteBatch)  			e.Combo("/{id}/content/{contentId}" ). 				Get(handler.HandleLibContentDetail).                                              				Put(binding.JSON(dto.CreateLibContentRequest{}), handler.HandleLibContentUpdate)  			 			e.Post("/{id}/content/upload" , handler.HandleLibContentCreateBatch)  		}) 		e.Group("/template" , func ()   { 			e.Post("/{type}/list" , binding.JSON(dto.LibTemplateListRequest{}), web.InjectPaginate(), 				handler.HandleLibTemplateList) 			e.Post("/{type}" , binding.JSON(dto.CreateLibTemplateRequest{}), handler.HandleLibTemplateCreate)  			e.Combo("/{type}/{id}" ). 				Get(handler.HandleLibTemplateDetail).                                                				Put(binding.JSON(dto.CreateLibTemplateRequest{}), handler.HandleLibTemplateUpdate).  				Delete(handler.HandleLibTemplateDelete)                                              			 			 			 		}) 	}, web.Authorization) } 
 
project 状态下
某个project级别下的多张表 
一张表的所有成绩 
某张表的某个人的成绩 
 
所以两张表思路是一样的,但是路由变一下就改变了许多东西,只能说巧妙
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 		e.Group("/{projectId}" , func ()   { 			e.Group("/lib" , func ()   { 				e.Group("/score" , func ()   { 					e.Combo("" ). 						Get(web.InjectPaginate(), libHandler.HandleLibList).                            						Post(binding.JSON(libDto.CreateLibScoreRequest{}), libHandler.HandleLibCreate)  					e.Combo("/{id}" ). 						Get(libHandler.HandleLibDetail).                                                						Put(binding.JSON(libDto.CreateLibScoreRequest{}), libHandler.HandleLibUpdate).  						Delete(libHandler.HandleLibDelete)                                              					e.Combo("/{id}/content" ). 						Get(web.InjectPaginate(), 							libHandler.HandleLibContentList).  						Post(binding.JSON([]libDto.CreateLibContentRequest{}), 							libHandler.HandleLibContentCreate)  					e.Post("/{id}/content/delete" , binding.JSON(libDto.LibScoreContentDeleteRequest{}), 						libHandler.HandleLibContentDeleteBatch)  					e.Combo("/{id}/content/{contentId}" ). 						Get(libHandler.HandleLibContentDetail).                                                 						Put(binding.JSON(libDto.CreateLibContentRequest{}), libHandler.HandleLibContentUpdate)  					 					e.Post("/{id}/content/upload" , libHandler.HandleLibContentCreateBatch)  				}) 				e.Group("/template" , func ()   { 					e.Post("/{type}/list" , binding.JSON(libDto.LibTemplateListRequest{}), web.InjectPaginate(), 						libHandler.HandleLibTemplateList) 					e.Post("/{type}" , binding.JSON(libDto.CreateLibTemplateRequest{}), libHandler.HandleLibTemplateCreate)  					e.Combo("/{type}/{id}" ). 						Get(libHandler.HandleLibTemplateDetail).                                                   						Put(binding.JSON(libDto.CreateLibTemplateRequest{}), libHandler.HandleLibTemplateUpdate).  						Delete(libHandler.HandleLibTemplateDelete)                                                 					 					 					 				}) 			}) 		}) 		 
 
具体逻辑就是,通过 projectId判断是哪个级别的,如果projectId 传入了值就是 project级别的,否则就是system级别的
 
然后具体路由就可以修改了(呈现一二):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 func  HandleLibList (c flamego.Context, r flamego.Render, paginate page.Paginate)   {	var  list []dto.LibScoreListItem 	var  count int64  	projectId := c.Param("projectId" ) 	err := dao.Lib.Model(&model.LibScore{}).Scopes(paginate.Paginate(), 		func (db *gorm.DB)   *gorm.DB { 			if  projectId == ""  { 				return  db.Where("scope = ?" , "system" ) 			} else  { 				return  db.Where("scope = ?" , "project/" +projectId) 			} 		}).Count(&count).Find(&list). 		Offset(-1 ).Limit(-1 ).Error 	if  err != nil  { 		response.ServiceErr(r, err) 		return  	} 	response.HTTPSuccess(r, map [string ]any{ 		"total" : count, 		"list" :  list, 	}) } func  HandleLibCreate (c flamego.Context, r flamego.Render, req dto.CreateLibScoreRequest, errs binding.Errors)   {	if  errs != nil  { 		response.InValidParam(r, errs) 		return  	} 	projectId := c.Param("projectId" ) 	var  scope string  	if  projectId == ""  { 		scope = "system"  	} else  { 		scope = "project/"  + projectId 	} 	err := dao.Lib.WithContext(c.Request().Context()).Model(&model.LibScore{}). 		Create(&model.LibScore{ 			Name:  req.Name, 			Tags:  req.Tags, 			Scope: scope, 		}).Error 	if  err != nil  { 		response.ServiceErr(r, err) 		return  	} 	response.HTTPSuccess(r, nil ) } 
 
总结 阅读理解很重要–需求的阅读
今天还去母校返校演讲了,哈哈哈哈哈哈哈啊哈,给高中生讲电专生活,逃课啥的太有意思了
典型的反面教材。。