commit

This commit is contained in:
loser 2022-06-15 10:31:40 +08:00
parent ab116f2380
commit 0acb114bb8
14 changed files with 1720 additions and 0 deletions

55
.drone.yml Normal file
View File

@ -0,0 +1,55 @@
kind: pipeline
type: docker
name: BuildDev
trigger:
event: [push]
branch: [ master ]
steps:
- name: BuildToRegistry
image: plugins/docker
settings:
registry: registry.cn-hangzhou.aliyuncs.com
repo: registry.cn-hangzhou.aliyuncs.com/hyxc_dev/scale_manager
username:
from_secret: registry_username
password:
from_secret: registry_password
use_cache: true
# event为push且分支为master的时候自动打latest为tag则会根据tag自动生成
tag:
- latest
- sha_${DRONE_COMMIT_SHA}
when:
status:
- "success"
---
kind: pipeline
type: docker
name: BuildProd
trigger:
event: [tag]
steps:
- name: BuildToRegistry
image: plugins/docker
# depends_on:
# - CheckCode
settings:
registry: registry.cn-hangzhou.aliyuncs.com
repo: registry.cn-hangzhou.aliyuncs.com/hyxc/scale_manager
username:
from_secret: registry_username
password:
from_secret: registry_password
use_cache: true
# event为push且分支为master的时候自动打latest为tag则会根据tag自动生成
auto_tag: true
when:
status:
- "success"

94
api/upload_excel.go Normal file
View File

@ -0,0 +1,94 @@
package api
import (
"docDemo/client"
"docDemo/model"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/xuri/excelize/v2"
"strings"
)
type scaleApi struct{}
func ScaleApi() *scaleApi {
return &scaleApi{}
}
func (scaleApi) AddScale(ctx *gin.Context) {
var p model.AddQuestion
if ctx.ShouldBind(&p) != nil {
fmt.Println("参数错误")
return
}
open, err := p.File.Open()
if err != nil {
fmt.Println("文件打开失败", err)
return
}
file, err := excelize.OpenReader(open)
defer file.Close()
if err != nil {
return
}
rows, err := file.GetRows("Sheet1")
if err != nil {
return
}
for _, row := range rows[1:3] {
if row[0] == "" {
break
}
var ques []map[string]string
if len(row) > 2 {
//ques = append(ques, map[string]string{"A": row[2]})
//ques = append(ques, map[string]string{"B": row[3]})
//ques = append(ques, map[string]string{"C": row[4]})
//ques = append(ques, map[string]string{"D": row[5]})
for i := range rows {
ques = append(ques, map[string]string{row[i]: row[3]})
}
}
awssJson, err := json.Marshal(ques)
if err != nil {
return
}
var Questions model.Question
Questions.Content = row[1]
Questions.Answer = string(awssJson)
Questions.Analysis = "A"
Questions.Subject = "大学语文"
if strings.Contains(row[0], "单选") || strings.Contains(row[0], "单项") {
Questions.SubjectType = "single"
}
if strings.Contains(row[0], "多选") || strings.Contains(row[0], "多项") {
Questions.SubjectType = "mutil"
}
if strings.Contains(row[0], "判断") {
Questions.SubjectType = "judge"
}
if strings.Contains(row[0], "应用") || strings.Contains(row[0], "简答") {
Questions.SubjectType = "application"
}
err = client.MySQL.Table("sys_question_bank").Create(&Questions).Error
if err != nil {
fmt.Println("导入题库失败:", err)
}
}
}
func (scaleApi) Test(ctx *gin.Context) {
ctx.JSON(200, gin.H{
"message": "drone-Test",
})
return
}

23
client/mysql.go Normal file
View File

@ -0,0 +1,23 @@
package client
import (
"gitee.ltd/lxh/logger"
"gitee.ltd/lxh/logger/log"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var MySQL *gorm.DB
// InitMySQLClient 初始化MySQL客户端
func InitMySQLClient() {
// 创建连接对象
conn, err := gorm.Open(mysql.Open("root:Tw2022!@#@tcp(47.109.17.77:3306)/tuowei?charset=utf8mb4&parseTime=True&loc=Local&timeout=1000ms"), &gorm.Config{Logger: logger.DefaultGormLogger()})
if err != nil {
log.Panicf("初始化MySQL连接失败, 错误信息: %v", err)
} else {
log.Debug("[MySQL] 连接成功")
}
MySQL = conn
}

5
demo.go Normal file
View File

@ -0,0 +1,5 @@
package main
type USer struct {
Name string `json:"name"`
}

68
go.mod Normal file
View File

@ -0,0 +1,68 @@
module docDemo
go 1.18
require (
gitee.ltd/lxh/logger v1.0.14
github.com/gin-gonic/gin v1.8.1
github.com/xuri/excelize/v2 v2.6.0
gorm.io/driver/mysql v1.3.2
gorm.io/gorm v1.23.5
)
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/caarlos0/env/v6 v6.9.2 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.4 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lixh00/loki-client-go v1.0.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.12.2 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.34.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/prometheus/prometheus v1.8.2-0.20201028100903-3245b3267b24 // indirect
github.com/richardlehane/mscfb v1.0.4 // indirect
github.com/richardlehane/msoleps v1.0.1 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/xuri/efp v0.0.0-20220407160117-ad0f7a785be8 // indirect
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/crypto v0.0.0-20220408190544-5352b0902921 // indirect
golang.org/x/net v0.0.0-20220517181318-183a9ca12b87 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220505152158-f39f71e6c8f3 // indirect
google.golang.org/grpc v1.46.2 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

1264
go.sum Normal file

File diff suppressed because it is too large Load Diff

23
main.go Normal file
View File

@ -0,0 +1,23 @@
package main
import "C"
import (
"docDemo/router"
"fmt"
"github.com/gin-gonic/gin"
)
//func init() {
// client.InitMySQLClient()
//}
func main() {
app := gin.Default()
// 初始化路由
router.InitRouter(app.Group("/api"))
// 订阅消息
//go queues.NewQueuesConsumer().SubscribeGetReport()
app.Run(fmt.Sprintf(":8889"))
}

69
map_test.go Normal file
View File

@ -0,0 +1,69 @@
package main
import (
"docDemo/client"
"docDemo/model"
"encoding/json"
"fmt"
"github.com/xuri/excelize/v2"
"strings"
"testing"
)
var AZ = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
func TestMap(t *testing.T) {
file, err := excelize.OpenFile("result.xlsx")
if err != nil {
return
}
rows, err := file.GetRows("Sheet1")
if err != nil {
return
}
for _, row := range rows[1:3] {
if row[0] == "" {
break
}
var ques []map[string]string
for i := range row[2:] {
ques = append(ques, map[string]string{AZ[i]: row[2:][i]})
}
awssJson, err := json.Marshal(ques)
if err != nil {
return
}
var Questions model.Question
Questions.Content = row[1]
Questions.Answer = string(awssJson)
Questions.Analysis = "A"
Questions.CorrectAnswer = "A"
Questions.Subject = "大学语文"
if strings.Contains(row[0], "单选") || strings.Contains(row[0], "单项") {
Questions.SubjectType = "single"
}
if strings.Contains(row[0], "多选") || strings.Contains(row[0], "多项") {
Questions.SubjectType = "mutil"
}
if strings.Contains(row[0], "判断") {
Questions.SubjectType = "judge"
}
if strings.Contains(row[0], "应用") || strings.Contains(row[0], "简答") {
Questions.SubjectType = "application"
}
err = client.MySQL.Table("sys_question_bank").Create(&Questions).Error
if err != nil {
fmt.Println("导入题库失败:", err)
}
}
}

30
model/question.go Normal file
View File

@ -0,0 +1,30 @@
package model
import (
"gorm.io/gorm"
"mime/multipart"
"time"
)
type Question struct {
Id int `json:"id" gorm:"primaryKey;autoIncrement;comment:主键编码" comment:"主键ID"`
KnowledgePointId int `json:"knowledge_point_id" comment:"知识点ID"`
SubjectType string `json:"subject_type" gorm:"type:varchar(10);comment:题目类型 1-大学英语/2-大学英语/3-大学数学/4-大学计算机"`
Content string `json:"content" gorm:"type:text;comment:题目内容"`
Answer string `json:"answer" gorm:"type:text;not null;comment:答案"`
CorrectAnswer string `json:"correct_answer" gorm:"type:text;comment:正确答案"`
Analysis string `json:"analysis" gorm:"type:text;comment:题目解析"`
Score float64 `json:"score"`
Subject string `json:"subject" gorm:"type:varchar(10);comment:科目类型 大学英语/大学数学/大学语文"`
CreatedAt time.Time `json:"createdAt" gorm:"comment:创建时间"`
UpdatedAt time.Time `json:"updatedAt" gorm:"comment:最后更新时间"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index;comment:删除时间"`
}
func (Question) TableName() string {
return "sys_question_bank"
}
type AddQuestion struct {
File *multipart.FileHeader `json:"file" form:"file" binding:"required"`
}

BIN
result.xlsx Normal file

Binary file not shown.

18
router/router.go Normal file
View File

@ -0,0 +1,18 @@
package router
import (
"docDemo/api"
"github.com/gin-gonic/gin"
)
// InitRouter 初始化路由
func InitRouter(app *gin.RouterGroup) {
// 开放接口
openRouter(app.Group("/open"))
}
// 内部开放接口
func openRouter(g *gin.RouterGroup) {
g.POST("/scale/upload", api.ScaleApi().AddScale) // 获取单个量表信息
g.GET("/test", api.ScaleApi().Test)
}

2
test.docx Normal file
View File

@ -0,0 +1,2 @@
如果 a=1av都收敛则级数 c=2cv必收敛

9
utils/conver_utils.go Normal file
View File

@ -0,0 +1,9 @@
package utils
import "strconv"
// StrToInt string转int
func StrToInt(str string) int {
i, _ := strconv.Atoi(str)
return i
}

62
utils/excel_utils.go Normal file
View File

@ -0,0 +1,62 @@
package utils
import (
"docDemo/model"
"errors"
"fmt"
"gitee.ltd/lxh/logger/log"
"github.com/xuri/excelize/v2"
"strings"
)
var AZ = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
type excel struct{}
func ExcelUtils() *excel {
return &excel{}
}
// ParseQuestion 转换Excel为量表问题答案信息
func (e excel) ParseQuestion(ef *excelize.File) (es []model.Question, err error) {
// 获取指定sheet
rows, err := ef.GetRows("question")
if err != nil {
return
}
log.Debugf("读取成功,共%d行%d列数据", len(rows), len(rows[0]))
// 循环行,处理为结构体,不处理第一行,因为是标题
for i, row := range rows[1:] {
// 如果问题出现空行,终止解析
if row[0] == "" {
break
}
// 创建一个问题结构体
var q model.Question
q.QuestionSerial = i + 1
q.Question = row[0]
// 开始解析答案
var answers []model.AnswerEntity
for j, a := range row[1:] {
// 如果答案出现空行,终止解析
if a == "" {
break
}
// 拆开答案,获取答案和分值
if !strings.Contains(a, "|") {
log.Errorf("异常数据: %v", a)
err = errors.New(fmt.Sprintf("模板文件格式错误(%v行%v列),答案和分值必须以|隔开", i+2, j+2))
return
}
answerInfo := strings.Split(a, "|")
answers = append(answers, model.AnswerEntity{
Code: AZ[j],
Answer: answerInfo[0],
Score: StrToInt(answerInfo[1]),
})
}
q.Answers = answers
es = append(es, q)
}
return
}