199 lines
4.8 KiB
Go
199 lines
4.8 KiB
Go
package v1
|
|
|
|
import (
|
|
"encoding/json"
|
|
"excel_export/biz/config"
|
|
"excel_export/cmd/cmd"
|
|
"excel_export/impl"
|
|
"excel_export/pkg"
|
|
"excel_export/pkg/e"
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"math"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
func Export(c *gin.Context) {
|
|
var (
|
|
Config *config.Config
|
|
OrderJsonQuery impl.OrderJsonQuery
|
|
)
|
|
|
|
sysName := c.Param("sys")
|
|
jobName := c.Param("job")
|
|
adminId := c.Query("admin_id")
|
|
if err := c.ShouldBindJSON(&OrderJsonQuery); err != nil {
|
|
pkg.Response(c).Error(e.INVALID_PARAMS, "参数错误:"+err.Error())
|
|
return
|
|
}
|
|
path, _ := os.Getwd()
|
|
Config = config.LoadConfig(path + "/config")
|
|
ee := cmd.NewCsv(Config)
|
|
|
|
begin, err := time.Parse(time.DateTime, OrderJsonQuery.BeginTime)
|
|
if err != nil {
|
|
pkg.Response(c).Error(e.INVALID_PARAMS, "begin_time参数错误:"+err.Error())
|
|
return
|
|
}
|
|
end, err := time.Parse(time.DateTime, OrderJsonQuery.EndTime)
|
|
if err != nil {
|
|
pkg.Response(c).Error(e.INVALID_PARAMS, "end_time参数错误:"+err.Error())
|
|
return
|
|
}
|
|
if err != nil {
|
|
pkg.Response(c).Error(e.INVALID_PARAMS, "无效的sql语句:"+err.Error())
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
// 导出任务
|
|
taskIdStr := pkg.GetTaskId()
|
|
pkg.Response(c).Success(taskIdStr)
|
|
go func() {
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
fmt.Printf("error: %v\n", err)
|
|
}
|
|
}()
|
|
if err := ee.ExportMarket(adminId, sysName, jobName, begin, end, OrderJsonQuery.Slice, OrderJsonQuery.Condition, taskIdStr); err != nil {
|
|
pkg.ErrLog("订单导出错误:" + err.Error())
|
|
}
|
|
}()
|
|
}
|
|
|
|
func Download(c *gin.Context) {
|
|
var (
|
|
Config *config.Config
|
|
)
|
|
|
|
sysName := c.Param("sys")
|
|
jobName := c.Param("job")
|
|
taskId := c.Param("task_id")
|
|
adminId := c.Query("admin_id")
|
|
file, err := pkg.MissionLogFile(adminId, sysName, jobName, taskId)
|
|
if err != nil {
|
|
pkg.Response(c).Error(e.MISSION_NOT_FOUND, "未找到相关信息:"+err.Error())
|
|
return
|
|
}
|
|
bytes, err := os.ReadFile(file)
|
|
info := make(map[string]interface{})
|
|
_ = json.Unmarshal(bytes, &info)
|
|
path, _ := os.Getwd()
|
|
Config = config.LoadConfig(path + "/config")
|
|
ee := cmd.NewCsv(Config)
|
|
ee.TaskIdStr = taskId
|
|
ee.JobName = jobName
|
|
ee.SysName = sysName
|
|
ee.Name = info["file_name"].(string)
|
|
c.File(ee.ZipFile())
|
|
}
|
|
|
|
func TaskProcess(c *gin.Context) {
|
|
|
|
sysName := c.Param("sys")
|
|
jobName := c.Param("job")
|
|
taskId := c.Param("task_id")
|
|
adminId := c.Query("admin_id")
|
|
file, err := pkg.MissionLogFile(adminId, sysName, jobName, taskId)
|
|
if err != nil {
|
|
pkg.Response(c).Error(e.MISSION_NOT_FOUND, "未找到相关信息:"+err.Error())
|
|
return
|
|
}
|
|
bytes, err := os.ReadFile(file)
|
|
|
|
if err != nil {
|
|
pkg.Response(c).Error(e.MISSION_NOT_FOUND, "任务进度获取失败:"+err.Error())
|
|
return
|
|
}
|
|
pkg.Response(c).Success(string(bytes))
|
|
}
|
|
|
|
func AllTaskProcess(c *gin.Context) {
|
|
var (
|
|
data []map[string]interface{}
|
|
res config.ResPage
|
|
)
|
|
|
|
sysName := c.Param("sys")
|
|
jobName := c.Param("job")
|
|
page := c.Query("page")
|
|
num := c.Query("num")
|
|
aminId := c.Query("admin_id")
|
|
path, err := pkg.MissionLogPath(aminId, sysName, jobName)
|
|
|
|
if err != nil {
|
|
pkg.Response(c).Error(e.MISSION_NOT_FOUND, "未找到项目文件:"+err.Error())
|
|
return
|
|
}
|
|
entries := pkg.SortFileWithStatus(path)
|
|
count := len(entries)
|
|
pageInt, _ := strconv.ParseInt(page, 10, 64)
|
|
numInt, _ := strconv.ParseInt(num, 10, 64)
|
|
begin := (pageInt - 1) * numInt
|
|
entEnd := begin + numInt
|
|
if count < int(entEnd) {
|
|
entEnd = int64(count)
|
|
}
|
|
entries = entries[begin:entEnd]
|
|
for _, entry := range entries {
|
|
if entry.FileInfo == nil {
|
|
break
|
|
}
|
|
info := make(map[string]interface{})
|
|
if entry.IsDir() {
|
|
continue
|
|
}
|
|
info["task_id"] = entry.Name()
|
|
file := fmt.Sprintf("%s/%s", path, entry.Name())
|
|
bytes, _ := os.ReadFile(file)
|
|
_ = json.Unmarshal(bytes, &info)
|
|
fileOs, _ := os.Stat(file)
|
|
info["update_time"] = fileOs.ModTime().Format(time.DateTime)
|
|
data = append(data, info)
|
|
}
|
|
res = config.ResPage{
|
|
Page: int(pageInt),
|
|
PageSize: int(numInt),
|
|
Total: count,
|
|
Data: data,
|
|
LastPage: int(math.Ceil(float64(count) / float64(numInt))),
|
|
}
|
|
|
|
pkg.Response(c).Success(res)
|
|
}
|
|
|
|
func DelTask(c *gin.Context) {
|
|
sysName := c.Param("sys")
|
|
jobName := c.Param("job")
|
|
taskId := c.Param("task_id")
|
|
adminId := c.Query("admin_id")
|
|
file, err := pkg.MissionPathFile(taskId)
|
|
if err != nil {
|
|
pkg.Response(c).Error(e.MISSION_NOT_FOUND, "未找到任务信息:"+err.Error())
|
|
return
|
|
}
|
|
|
|
err = os.RemoveAll(file)
|
|
if err != nil {
|
|
pkg.Response(c).Error(e.MISSION_NOT_FOUND, "删除失败:"+err.Error())
|
|
return
|
|
}
|
|
|
|
logFile, err := pkg.MissionLogFile(adminId, sysName, jobName, taskId)
|
|
if err != nil {
|
|
pkg.Response(c).Error(e.MISSION_NOT_FOUND, "未找到任务信息:"+err.Error())
|
|
return
|
|
}
|
|
err = os.RemoveAll(logFile)
|
|
if err != nil {
|
|
pkg.Response(c).Error(e.MISSION_NOT_FOUND, "删除失败:"+err.Error())
|
|
return
|
|
}
|
|
pkg.Response(c).Success(taskId)
|
|
}
|