iedc-go/example/main.go
2022-07-24 00:00:36 +08:00

146 lines
4.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
. "code.hoteas.com/golang/hotime"
. "code.hoteas.com/golang/hotime/common"
"fmt"
"github.com/360EntSecGroup-Skylar/excelize"
"golang.org/x/net/websocket"
"os"
"time"
)
func convertToTitle(columnNumber int) string {
var res []byte
for columnNumber > 0 {
a := columnNumber % 26
if a == 0 {
a = 26
}
res = append(res, 'A'+byte(a-1))
columnNumber = (columnNumber - a) / 26
}
// 上面输出的res是反着的前后交换
for i, n := 0, len(res); i < n/2; i++ {
res[i], res[n-1-i] = res[n-1-i], res[i]
}
return string(res)
}
func main() {
appIns := Init("config/config.json")
appIns.Run(Router{"app": Proj{
"user": Ctr{
"test": func(that *Context) {
fmt.Println("dasdasd")
//id := that.Db.Insert("user", Map{"name": "test"})
//ok := that.Db.Update("user", Map{"name": "test1"}, Map{"name": "test"})
ps := that.Db.Select("user", "*")
p := that.Db.Get("user", "*")
//row := that.Db.Delete("user", Map{"id": id})
//that.Display(0, Slice{id, ok, ps, p, row})
that.Display(0, Slice{ps, p})
},
"download": func(that *Context) {
orgId := ObjToInt(that.Req.FormValue("org_id"))
if orgId == 0 {
that.Display(3, "参数错误")
return
}
filePath := "temp/home" + ObjToStr(orgId) + ".xlsx"
f, e := excelize.OpenFile(that.Config.GetString("tpt") + "/" + filePath)
// 设置单元格的值
questions := that.Db.Query("SELECT user.name AS user_name,user.phone,company.name AS company_name,question_company.name,question_company.question_data,question_company.status,question_company.modify_time FROM question_company INNER JOIN `user` ON question_company.`user_id`=`user`.id INNER JOIN `company` ON question_company.`company_id`=`company`.id WHERE question_company.org_id=?", orgId)
if e != nil {
f = excelize.NewFile()
}
// 创建一个工作表
f.NewSheet("企业填报")
f.DeleteSheet("Sheet1")
//cs := append(NewCompanys{}, companys...)
f.SetCellValue("企业填报", convertToTitle(1)+"1", "调查名")
f.SetCellValue("企业填报", convertToTitle(2)+"1", "企业名")
f.SetCellValue("企业填报", convertToTitle(3)+"1", "姓名")
f.SetCellValue("企业填报", convertToTitle(4)+"1", "手机号")
f.SetCellValue("企业填报", convertToTitle(5)+"1", "状态")
f.SetCellValue("企业填报", convertToTitle(6)+"1", "修改时间")
for k, question := range questions {
//企业基础信息
f.SetCellValue("企业填报", convertToTitle(1)+ObjToStr(k+5), question.GetString("name"))
f.SetCellValue("企业填报", convertToTitle(2)+ObjToStr(k+5), question.GetString("company_name"))
f.SetCellValue("企业填报", convertToTitle(3)+ObjToStr(k+5), question.GetString("user_name"))
f.SetCellValue("企业填报", convertToTitle(4)+ObjToStr(k+5), question.GetString("phone"))
f.SetCellValue("企业填报", convertToTitle(5)+ObjToStr(k+5), question.GetString("status"))
f.SetCellValue("企业填报", convertToTitle(6)+ObjToStr(k+5), time.Unix(question.GetCeilInt64("modify_time"), 0).Format("2006-01-02 15:04"))
//企业问题信息基础信息
questionData := question.GetSlice("question_data")
for k1, _ := range questionData {
v1 := questionData.GetMap(k1)
if k == 0 {
f.SetCellValue("企业填报", convertToTitle(k1+7)+"1", v1.GetString("label"))
f.SetCellValue("企业填报", convertToTitle(k1+7)+"2", v1.GetString("unit"))
f.SetCellValue("企业填报", convertToTitle(k1+7)+"3", v1.GetString("remarks"))
f.SetCellValue("企业填报", convertToTitle(k1+7)+"4", v1.GetString("type"))
}
value := v1.GetString("value")
extend := v1.GetSlice("extend")
if extend != nil && len(extend) > 0 {
for k2, _ := range extend {
v2 := extend.GetMap(k2)
value = value + ";" + v2.GetString("value")
}
}
f.SetCellValue("企业填报", convertToTitle(k1+7)+ObjToStr(k+5), value)
}
//break
}
os.MkdirAll(that.Config.GetString("tpt")+"/temp/", os.ModeDir)
// 根据指定路径保存文件
if err := f.SaveAs(that.Config.GetString("tpt") + "/" + filePath); err != nil {
fmt.Println(err)
that.Display(4, "输出异常")
return
}
//}
f.Save()
that.Resp.Header().Set("Location", "/"+filePath)
that.Resp.WriteHeader(307) //关键在这里!
that.Display(0, filePath)
},
"websocket": func(that *Context) {
hdler := websocket.Handler(func(ws *websocket.Conn) {
for true {
msg := make([]byte, 5120)
n, err := ws.Read(msg)
if err != nil {
return
}
fmt.Printf("Receive: %s\n", msg[:n])
send_msg := "[" + string(msg[:n]) + "]"
m, err := ws.Write([]byte(send_msg))
if err != nil {
return
}
fmt.Printf("Send: %s\n", msg[:m])
}
})
hdler.ServeHTTP(that.Resp, that.Req)
},
}}})
}