You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
175 lines
4.6 KiB
175 lines
4.6 KiB
|
4 months ago
|
package taskmanagement
|
||
|
|
|
||
|
|
import (
|
||
|
|
"appPlatform/api/version1/customerform"
|
||
|
|
"appPlatform/models/customerForm"
|
||
|
|
"appPlatform/models/modelAppPlatform"
|
||
|
|
"appPlatform/models/modelshr"
|
||
|
|
"appPlatform/overall"
|
||
|
|
"appPlatform/overall/publicmethod"
|
||
|
|
"encoding/csv"
|
||
|
|
"fmt"
|
||
|
|
"mime/multipart"
|
||
|
|
"net/http"
|
||
|
|
"regexp"
|
||
|
|
"strconv"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (a *ApiMethod) ImportFormListFile(ctx *gin.Context) {
|
||
|
|
formId := ctx.Request.FormValue("formId")
|
||
|
|
pFileHeader, err := ctx.FormFile("file")
|
||
|
|
if err != nil {
|
||
|
|
ctx.JSON(http.StatusBadRequest, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
file, err := pFileHeader.Open()
|
||
|
|
if err != nil {
|
||
|
|
ctx.JSON(http.StatusBadRequest, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
defer file.Close()
|
||
|
|
|
||
|
|
//获取form的view,以便可以获取具体的table名
|
||
|
|
var formCont modelAppPlatform.CustomerFormView
|
||
|
|
err = formCont.GetCont(map[string]interface{}{"`id`": formId})
|
||
|
|
if err != nil || formCont.TableKey == "" {
|
||
|
|
ctx.JSON(http.StatusBadGateway, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
//直接读取数据库获取表的详细结构
|
||
|
|
masterFieldAry, mastErr := customerform.ReadDatabaseForm(formCont.TableKey)
|
||
|
|
if mastErr != nil {
|
||
|
|
ctx.JSON(http.StatusBadGateway, mastErr)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
isCSV := strings.HasSuffix(pFileHeader.Filename, ".csv")
|
||
|
|
var (
|
||
|
|
title []string
|
||
|
|
data [][]interface{}
|
||
|
|
)
|
||
|
|
if isCSV {
|
||
|
|
title, data, err = handleCSVDataFile(file, masterFieldAry)
|
||
|
|
} else {
|
||
|
|
//处理excel
|
||
|
|
//title, data, err = handleCSVDataFile(file)
|
||
|
|
}
|
||
|
|
|
||
|
|
//构建task记录,每条导入记录绑定一个task
|
||
|
|
context, _ := ctx.Get(overall.MyContJwt)
|
||
|
|
var userCont modelshr.ManCont
|
||
|
|
userCont.GetLoginCont(context) //当前操作人
|
||
|
|
cureeTime := time.Now().Unix() //写入时间
|
||
|
|
|
||
|
|
valueMap := make(map[string]interface{})
|
||
|
|
valueMap["creater_time"] = cureeTime
|
||
|
|
valueMap["creater"] = userCont.Key
|
||
|
|
valueMap["createrOrg"] = userCont.AdminOrg
|
||
|
|
valueMap["edit_time"] = cureeTime
|
||
|
|
i, l := 0, len(title)
|
||
|
|
for _, v := range data {
|
||
|
|
if len(v) != l {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
_uuid := publicmethod.GetUUid(1) //统一识别符
|
||
|
|
valueMap["masters_key"] = _uuid
|
||
|
|
for ; i < l; i++ {
|
||
|
|
//准备数据
|
||
|
|
valueMap[title[i]] = v[i]
|
||
|
|
}
|
||
|
|
//重置循环
|
||
|
|
i = 0
|
||
|
|
delete(valueMap, "@id")
|
||
|
|
//开始插入
|
||
|
|
if err = overall.CONSTANT_DB_CustomerForm.Table(formCont.TableKey).Create(valueMap).Error; err != nil {
|
||
|
|
break
|
||
|
|
}
|
||
|
|
|
||
|
|
var taskCont customerForm.TaskRecord
|
||
|
|
taskCont.MastersKey = _uuid
|
||
|
|
taskCont.Title = fmt.Sprintf("%v-%v(%v)-%v", formCont.Name, userCont.Name, userCont.Number, publicmethod.UnixTimeToDay(cureeTime, 14)) //标题"`
|
||
|
|
taskCont.AppKey = 0
|
||
|
|
taskCont.TableKey = 0
|
||
|
|
taskCont.Creater = userCont.Key //创建人"`
|
||
|
|
taskCont.CreaterTime = cureeTime //创建时间"`
|
||
|
|
taskCont.EditTime = cureeTime //编辑时间"`
|
||
|
|
taskCont.Types = formCont.FlowIsOpen //类型(1:流程表单;2:普通表单)"`
|
||
|
|
taskCont.VersionId = formCont.Id //来源于哪个表单"`
|
||
|
|
taskCont.Status = 2
|
||
|
|
taskCont.CreaterOrg = userCont.AdminOrg
|
||
|
|
taskCont.MastesForm = formCont.MastesForm
|
||
|
|
taskCont.MastesFormJson = formCont.MastesFormJson
|
||
|
|
if err = overall.CONSTANT_DB_CustomerForm.Create(&taskCont).Error; err != nil {
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if err != nil {
|
||
|
|
ctx.JSON(http.StatusBadGateway, err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
ctx.JSON(http.StatusOK, nil)
|
||
|
|
}
|
||
|
|
|
||
|
|
func handleCSVDataFile(file multipart.File, fields []customerform.Result) (title []string, result [][]interface{}, err error) {
|
||
|
|
f := csv.NewReader(file)
|
||
|
|
records, _ := f.ReadAll()
|
||
|
|
title = records[0]
|
||
|
|
reg := regexp.MustCompile(`(\w+)`)
|
||
|
|
|
||
|
|
reflects := []string{}
|
||
|
|
//根据title,获取对应列的具体存储类型
|
||
|
|
for k, v := range title {
|
||
|
|
//(shen1gao1)标题 => shen1gao1
|
||
|
|
v = reg.FindString(v)
|
||
|
|
title[k] = v
|
||
|
|
for _, f := range fields {
|
||
|
|
if v == f.Field {
|
||
|
|
if strings.Contains(f.Type, "varchar") {
|
||
|
|
reflects = append(reflects, "string")
|
||
|
|
} else if strings.Contains(f.Type, "int") {
|
||
|
|
reflects = append(reflects, "int")
|
||
|
|
} else if strings.Contains(f.Type, "double") || strings.Contains(f.Type, "float") {
|
||
|
|
reflects = append(reflects, "number")
|
||
|
|
} else {
|
||
|
|
reflects = append(reflects, "string")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
max := len(records)
|
||
|
|
result = make([][]interface{}, len(title)-1) //丢掉标题行
|
||
|
|
for i := 1; i < max; i++ {
|
||
|
|
arr := []interface{}{}
|
||
|
|
for k, v := range records[i] { //csv的第i行数据
|
||
|
|
//根据列数,先转换类型,然后在插入到对应的数组中
|
||
|
|
switch reflects[k] {
|
||
|
|
case "int":
|
||
|
|
vi, err0 := strconv.Atoi(v)
|
||
|
|
if err0 != nil {
|
||
|
|
err = err0
|
||
|
|
return
|
||
|
|
}
|
||
|
|
arr = append(arr, vi)
|
||
|
|
case "number":
|
||
|
|
vf, err0 := strconv.ParseFloat(v, 64)
|
||
|
|
if err0 != nil {
|
||
|
|
err = err0
|
||
|
|
return
|
||
|
|
}
|
||
|
|
arr = append(arr, vf)
|
||
|
|
default:
|
||
|
|
arr = append(arr, v)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
result[i-1] = arr
|
||
|
|
}
|
||
|
|
|
||
|
|
return
|
||
|
|
}
|