From d6aea9913510936bde157e22a1f7042a0eb33ac3 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期日, 28 四月 2024 10:36:50 +0800 Subject: [PATCH] 时间范围和该工人以往添加记录重复性校验 --- controllers/worker_position_controller.go | 12 +++++++----- utils/timex.go | 18 ++++++++++++++++++ controllers/request/fineness.go | 12 ++++++------ controllers/mentor_controller.go | 2 +- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/controllers/mentor_controller.go b/controllers/mentor_controller.go index 0eaaa56..4daea9b 100644 --- a/controllers/mentor_controller.go +++ b/controllers/mentor_controller.go @@ -116,7 +116,7 @@ util.ResponseFormat(c, code.RequestParamError, "鍙傛暟瑙f瀽澶辫触锛屾暟鎹被鍨嬮敊璇�") return } - mentors, total, err := models.NewMentorSearch().SetPage(params.Page, params.PageSize).SetKeyword(params.Keyword).SetPreload(true).Find() + mentors, total, err := models.NewMentorSearch().SetPage(params.Page, params.PageSize).SetKeyword(params.Keyword).SetPreload(true).SetOrder("id desc").Find() if err != nil { util.ResponseFormat(c, code.RequestParamError, "鏌ヨ澶辫触") return diff --git a/controllers/request/fineness.go b/controllers/request/fineness.go index 2c0cd71..026ff9c 100644 --- a/controllers/request/fineness.go +++ b/controllers/request/fineness.go @@ -5,18 +5,18 @@ ) type AddFinenessRegister struct { - ID uint `json:"id"` //id 娣诲姞鏃朵紶0 + ID uint `json:"id"` //id 娣诲姞鏃朵紶0 Number string `gorm:"type:varchar(255);not null;comment:缂栧彿" json:"number"` //缂栧彿 - FinishDate string `gorm:"type:varchar(255);not null;comment:钀戒笣鏃堕棿" json:"finishDate"` //钀戒笣鏃堕棿 + FinishDate string `gorm:"type:varchar(255);not null;comment:钀戒笣鏃堕棿" json:"finishDate"` //钀戒笣鏃堕棿 Workshop string `gorm:"type:varchar(255);not null;comment:杞﹂棿" json:"name"` //杞﹂棿 WorkshopGroup int `gorm:"type:int(11);not null;default:0;comment:杞︾粍" json:"workshopGroup"` //杞︾粍 Market string `gorm:"type:varchar(255);not null;comment:搴勫彛" json:"market"` //搴勫彛 Spec string `gorm:"type:varchar(255);not null;comment:瑙勬牸" json:"spec"` //瑙勬牸 Circle uint8 `gorm:"not null;comment:鍥炴暟" json:"circle"` //鍥炴暟 - TotalCircle uint8 `gorm:"not null;comment:鎬诲洖鏁�" json:"totalCircle"` //鎬诲洖鏁� - FinenessList []FinenessItem `json:"finenessList"` //绾ゅ害鏁扮粍 - SumFineness decimal.Decimal `gorm:"type:decimal(12,2);not null;comment:鍚堣绾ゅ害" json:"sumFineness"` //鍚堣绾ゅ害 - SumQuantity decimal.Decimal `gorm:"type:decimal(12,2);not null;comment:鍚堣鏁伴噺" json:"sumQuantity"` //鍚堣鏁伴噺 + TotalCircle uint8 `gorm:"not null;comment:鎬诲洖鏁�" json:"totalCircle"` //鎬诲洖鏁� + FinenessList []FinenessItem `json:"finenessList"` //绾ゅ害鏁扮粍 + SumFineness decimal.Decimal `gorm:"type:decimal(12,2);not null;comment:鍚堣绾ゅ害" json:"sumFineness"` //鍚堣绾ゅ害 + SumQuantity decimal.Decimal `gorm:"type:decimal(12,2);not null;comment:鍚堣鏁伴噺" json:"sumQuantity"` //鍚堣鏁伴噺 Position int `gorm:"not null;default:0;comment:杞﹀彿" json:"position"` //鏈�鍚庝竴涓溅鍙� } diff --git a/controllers/worker_position_controller.go b/controllers/worker_position_controller.go index 51015a3..5512fc9 100644 --- a/controllers/worker_position_controller.go +++ b/controllers/worker_position_controller.go @@ -3,12 +3,12 @@ import ( "github.com/gin-gonic/gin" "github.com/spf13/cast" - "gorm.io/gorm" "silkserver/controllers/request" "silkserver/extend/code" "silkserver/extend/util" "silkserver/middleware" "silkserver/models" + "silkserver/utils" ) type WorkerPositionController struct { @@ -31,10 +31,12 @@ } //鏌ヨ鏄惁閲嶅 - _, err = models.NewWorkerPositionSearch().SetWorkerID(params.WorkerId).First() - if err != gorm.ErrRecordNotFound { - util.ResponseFormat(c, code.RequestParamError, "璇峰嬁閲嶅娣诲姞") - return + old, err := models.NewWorkerPositionSearch().SetWorkerID(params.WorkerId).First() + if err == nil { + if utils.IsOverlap(params.StartDate, params.EndDate, old.StartDate, old.EndDate) { + util.ResponseFormat(c, code.RequestParamError, "璇峰嬁閲嶅娣诲姞") + return + } } record := &models.WorkerPosition{ diff --git a/utils/timex.go b/utils/timex.go new file mode 100644 index 0000000..8a6ea02 --- /dev/null +++ b/utils/timex.go @@ -0,0 +1,18 @@ +package utils + +import "time" + +// IsOverlap 鍒ゆ柇涓や釜鏃堕棿娈垫槸鍚︽湁閲嶅彔 +func IsOverlap(start1, end1, start2, end2 string) bool { + layout := "2006-01-02" + startTime1, _ := time.Parse(layout, start1) + endTime1, _ := time.Parse(layout, end1) + startTime2, _ := time.Parse(layout, start2) + endTime2, _ := time.Parse(layout, end2) + + // 濡傛灉涓�涓椂闂存鐨勫紑濮嬫椂闂村湪鍙︿竴涓椂闂存涔嬪悗锛屾垨缁撴潫鏃堕棿鍦ㄥ彟涓�涓椂闂存涔嬪墠锛屽垯鏃犻噸鍙� + if startTime1.After(endTime2) || endTime1.Before(startTime2) { + return false + } + return true +} -- Gitblit v1.8.0