package controllers

import (
	"LAPP_LF_MOM_BACKEND/utils"
	"LAPP_LF_MOM_BACKEND/web/middleware/jwts"
	"LAPP_LF_MOM_BACKEND/web/models"
	"LAPP_LF_MOM_BACKEND/web/supports"
	"encoding/json"
	"github.com/kataras/iris/v12"
	"log"
	"time"
)

func UpdatePmWoAttrlst(ctx iris.Context) {

	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	logs := new(models.LeitServerLog)
	logs.File = "/controllers/PmWoattrlst_controller.go"
	logs.Level = "info"
	logs.Function = "UpdatePmWoAttrlst"
	logs.Message = "更新维护工单服务属性"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()

	data := new(models.PmWoAttrlst)
	if err := ctx.ReadJSON(data); err != nil {
		supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
		return
	}
	original, err := data.SelectOne()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到原有信息:"+err.Error(), nil)
		return
	}
	data.Bitmap = original.Bitmap
	data.Lastuser = user.Userid
	data.Operatetime = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	data.Lastmodif = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	err = data.Update()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "更新失败:"+err.Error(), nil)
		return
	}

	supports.Ok(ctx, "修改成功", "")
}

func UploadImgPmWoAttrlst(ctx iris.Context) {
	user, ok := jwts.ParseToken(ctx)
	utils.TrimStruct(user, *user)
	if !ok {
		supports.Error(ctx, iris.StatusBadRequest, supports.ParseParamsFailur, nil)
		return
	}
	//日志
	logs := new(models.LeitServerLog)
	logs.File = "/controllers/PmWoattrlst_controller.go"
	logs.Level = "info"
	logs.Function = "UpdatePmWoAttrlst"
	logs.Message = "更新维护工单服务属性"
	logs.Operator = user.Userid
	logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	logs.InsertRecord()

	//获取通过iris.WithPostMaxMemory获取的最大上传值大小。
	maxSize := ctx.Application().ConfigurationReadOnly().GetPostMaxMemory()
	err := ctx.Request().ParseMultipartForm(maxSize)
	if err != nil {
		ctx.StatusCode(iris.StatusInternalServerError)
		ctx.WriteString(err.Error())
		return
	}
	form := ctx.Request().MultipartForm
	files := form.File["upload[]"]

	data := new(models.PmWoAttrlst)
	info := ctx.FormValue("data")
	err = json.Unmarshal([]byte(info), data)
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "json解析错误:"+err.Error(), nil)
		return
	}
	original, err := data.SelectOne()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "抱歉未找到原有信息:"+err.Error(), nil)
		return
	}
	data.Bitmap = original.Bitmap
	for _, file := range files {
		if !utils.ValueIsEmpty(file.Filename) {
			guid := utils.MakeOrderSn(user.Userid)
			filPath := "public/uploadfile/" + guid + ".jpg"
			savePath := "web/public/uploadfile/" + guid + ".jpg"
			// 上传文件至指定目录
			err := utils.SaveUploadedFile(file, savePath)
			if err != nil {
				log.Printf("上传图片错误:%v", err)
				return
			}
			data.Bitmap = filPath
		}
	}
	data.Lastuser = user.Userid
	data.Operatetime = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	data.Lastmodif = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
	err = data.Update()
	if err != nil {
		supports.Error(ctx, iris.StatusBadRequest, "更新失败", nil)
		return
	}

	supports.Ok(ctx, "修改成功", "")
}
