|
@@ -4,6 +4,7 @@ import (
|
|
|
"fmt"
|
|
|
"kpt-pasture/model"
|
|
|
"kpt-pasture/util"
|
|
|
+ "sort"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
@@ -75,7 +76,21 @@ func (e *Entry) ProcessMilkOriginal(pastureId int64) {
|
|
|
zaplog.Error("DeleteRepeatMilkData", zap.Any("pastureId", pastureId), zap.Any("err", err))
|
|
|
return
|
|
|
}
|
|
|
- e.DeleteRepeatMilkData(pastureId, deleteModel, milkClassConfig)
|
|
|
+
|
|
|
+ milkOriginalList := make([]*model.MilkOriginal, 0)
|
|
|
+ if err = e.DB.Model(new(model.MilkOriginal)).
|
|
|
+ Where("pasture_id = ?", pastureId).
|
|
|
+ Where("id BETWEEN ? AND ?", milkClassConfig.OldUpdateMaxId+1, milkClassConfig.CurrentMaxId).
|
|
|
+ Find(&milkOriginalList).Error; err != nil {
|
|
|
+ zaplog.Error("DeleteRepeatMilkData", zap.Any("pastureId", pastureId), zap.Any("err", err))
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ e.DeleteRepeatMilkData(pastureId, deleteModel, milkClassConfig, milkOriginalList)
|
|
|
+ e.MilkHallData(pastureId)
|
|
|
+ e.UpdateRepeatCupSet1(milkOriginalList)
|
|
|
+ e.UpdateMilkOriginCowInfo(milkOriginalList)
|
|
|
+ e.UpdateRepeatCupSet2(milkOriginalList)
|
|
|
}
|
|
|
|
|
|
// UpdateShifts 更新班次
|
|
@@ -184,7 +199,7 @@ func (e *Entry) UpdateMilkDate(pastureId int64, xDBeg int) {
|
|
|
}
|
|
|
|
|
|
// DeleteRepeatMilkData 删除重复数据
|
|
|
-func (e *Entry) DeleteRepeatMilkData(pastureId int64, deleteModel *DeleteMilkOriginal, cfg *MilkClassConfig) {
|
|
|
+func (e *Entry) DeleteRepeatMilkData(pastureId int64, deleteModel *DeleteMilkOriginal, cfg *MilkClassConfig, milkOriginalList []*model.MilkOriginal) {
|
|
|
// 获取最小日期对应的最小wid
|
|
|
var oriWid int64
|
|
|
if err := e.DB.Model(new(model.MilkOriginal)).
|
|
@@ -196,83 +211,117 @@ func (e *Entry) DeleteRepeatMilkData(pastureId int64, deleteModel *DeleteMilkOri
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ for _, v := range milkOriginalList {
|
|
|
+ e.delete1(v, deleteModel.XMind, cfg)
|
|
|
+ e.delete2(v, deleteModel.XMind, cfg)
|
|
|
+ e.delete3(v, deleteModel.XMind, cfg)
|
|
|
+ e.delete4(v, deleteModel.XMind, cfg)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (e *Entry) MilkHallData(pastureId int64) {
|
|
|
+ milkHallList := e.FindMilkHallList(pastureId)
|
|
|
+ if len(milkHallList) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, v := range milkHallList {
|
|
|
+ e.UpdateRecognitionTime(pastureId, v)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// UpdateRecognitionTime 识别时间超过40分钟未套杯牛只,识别改为未识别
|
|
|
+func (e *Entry) UpdateRecognitionTime(pastureId int64, hall *model.MilkHall) {
|
|
|
milkOriginalList := make([]*model.MilkOriginal, 0)
|
|
|
if err := e.DB.Model(new(model.MilkOriginal)).
|
|
|
Where("pasture_id = ?", pastureId).
|
|
|
- Where("id BETWEEN ? AND ?", cfg.OldUpdateMaxId+1, cfg.CurrentMaxId).
|
|
|
+ Where("milk_hall_number = ?", hall.Name).
|
|
|
+ Where("milk_hall_brand = ?", hall.Brand).
|
|
|
+ Where("load = ?", 0).
|
|
|
Find(&milkOriginalList).Error; err != nil {
|
|
|
- zaplog.Error("DeleteRepeatMilkData", zap.Any("pastureId", pastureId), zap.Any("err", err))
|
|
|
+ zaplog.Error("MilkHallData", zap.Any("err", err))
|
|
|
}
|
|
|
|
|
|
for _, v := range milkOriginalList {
|
|
|
- e.Delete1(v, deleteModel.XMind, cfg)
|
|
|
- e.Delete2(v, deleteModel.XMind, cfg)
|
|
|
- e.Delete3(v, deleteModel.XMind, cfg)
|
|
|
- e.Delete4(v, deleteModel.XMind, cfg)
|
|
|
- }
|
|
|
-
|
|
|
- /*// 2. 删除重复记录(除第一条外)
|
|
|
- if err := e.DB.Exec(`
|
|
|
- DELETE FROM milk_original
|
|
|
- WHERE wid IN (
|
|
|
- SELECT m.wid FROM (
|
|
|
- SELECT m1.wid, m1.milk_date, m1.shifts, m1.detacher_address, m1.attach_time, m1.milk_weight, m1.pasture_id
|
|
|
- FROM milk_original m1
|
|
|
- WHERE m1.wid BETWEEN ? AND ?
|
|
|
- AND m1.milk_date >= ?
|
|
|
- AND EXISTS (
|
|
|
- SELECT 1 FROM milk_original m2
|
|
|
- WHERE m2.wid BETWEEN ? AND ?
|
|
|
- AND m2.milk_date = m1.milk_date
|
|
|
- AND m2.shifts = m1.shifts
|
|
|
- AND m2.detacher_address = m1.detacher_address
|
|
|
- AND m2.attach_time = m1.attach_time
|
|
|
- AND m2.milk_weight = m1.milk_weight
|
|
|
- AND m2.pasture_id = m1.pasture_id
|
|
|
- AND m2.wid < m1.wid
|
|
|
- )
|
|
|
- ) m
|
|
|
- )`,
|
|
|
- oriWid, maxWid, minDate, oriWid, maxWid).Error; err != nil {
|
|
|
- zaplog.Error("DeleteRepeatMilkData DeleteDuplicates", zap.Any("err", err))
|
|
|
- }
|
|
|
-
|
|
|
- // 4. 删除各班次开始前无奶量记录
|
|
|
- if err := e.DB.Exec(`
|
|
|
- DELETE FROM milk_original
|
|
|
- WHERE wid IN (
|
|
|
- SELECT m.wid FROM (
|
|
|
- SELECT m1.wid, m1.milk_date, m1.shifts, m1.milk_weight,
|
|
|
- @tot := IF(@tot + m1.milk_weight > 100, 100,
|
|
|
- IF(m1.shifts = @shifts, @tot, 0) + m1.milk_weight) m_tot,
|
|
|
- @shifts := m1.shifts
|
|
|
- FROM milk_original m1, (SELECT @tot := 0, @shifts := 0) aa
|
|
|
- WHERE m1.wid BETWEEN ? AND ?
|
|
|
- AND m1.milk_date >= ?
|
|
|
- AND m1.pasture_id = ?
|
|
|
- ORDER BY m1.milk_date, m1.shifts, STR_TO_DATE(m1.attach_time, '%Y-%m-%d %H:%i:%s')
|
|
|
- ) m
|
|
|
- WHERE m.m_tot = 0
|
|
|
- )`,
|
|
|
- minWid, maxWid, minDate, pastureId).Error; err != nil {
|
|
|
- zaplog.Error("DeleteRepeatMilkData DeleteNoMilkAtShift", zap.Any("err", err))
|
|
|
- }
|
|
|
-
|
|
|
- // 5. 删除时间异常数据
|
|
|
- if err := e.DB.Exec(`
|
|
|
- DELETE FROM milk_original
|
|
|
- WHERE wid BETWEEN ? AND ?
|
|
|
- AND milk_date >= '2020-10-01'
|
|
|
- AND STR_TO_DATE(milk_date_time, '%Y-%m-%d %H:%i:%s') > STR_TO_DATE(detacher_time, '%Y-%m-%d %H:%i:%s')
|
|
|
- AND STR_TO_DATE(attach_time, '%Y-%m-%d %H:%i:%s') > STR_TO_DATE(detacher_time, '%Y-%m-%d %H:%i:%s')
|
|
|
- AND HOUR(STR_TO_DATE(attach_time, '%Y-%m-%d %H:%i:%s')) = 23
|
|
|
- AND pasture_id = ?`,
|
|
|
- minWid, maxWid, pastureId).Error; err != nil {
|
|
|
- zaplog.Error("DeleteRepeatMilkData DeleteAbnormalTime", zap.Any("err", err))
|
|
|
- }*/
|
|
|
+ t1, _ := util.TimeParseLocal(model.LayoutTime, v.AttachTime)
|
|
|
+ t2, _ := util.TimeParseLocal(model.LayoutTime, v.RecognitionTime)
|
|
|
+ diff := t1.Sub(t2)
|
|
|
+ minute := int(diff.Minutes())
|
|
|
+
|
|
|
+ if util.Substr(v.RecognitionTime, -1, 8) != "00:00:00" && minute > 40 {
|
|
|
+ if err := e.DB.Model(new(model.MilkOriginal)).
|
|
|
+ Where("id = ?", v.Id).
|
|
|
+ Updates(map[string]interface{}{
|
|
|
+ "cow_id": 0,
|
|
|
+ "ele_ear_number": "",
|
|
|
+ "recognition_time": fmt.Sprintf("%s 00:00:00", util.Substr(v.RecognitionTime, 0, 10)),
|
|
|
+ }).Error; err != nil {
|
|
|
+ zaplog.Error("MilkHallData", zap.Any("err", err))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// UpdateRepeatCupSet1 更新重复套杯1, 识别时间相同,且不为0为重复套杯
|
|
|
+func (e *Entry) UpdateRepeatCupSet1(milkOriginalList []*model.MilkOriginal) {
|
|
|
+ if len(milkOriginalList) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ milkOriginalMap := make(map[string][]*model.MilkOriginal)
|
|
|
+ for _, v := range milkOriginalList {
|
|
|
+ if strings.HasSuffix(v.RecognitionTime, "00:00:00") {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ key := fmt.Sprintf("%s_%d_%d_%s", v.MilkDate, v.Shifts, v.DetacherAddress, v.RecognitionTime)
|
|
|
+ milkOriginalMap[key] = append(milkOriginalMap[key], v)
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, originalList := range milkOriginalMap {
|
|
|
+ if len(originalList) >= 2 {
|
|
|
+ // 按照Id升序排序(保留第一条)
|
|
|
+ sort.Slice(originalList, func(i, j int) bool {
|
|
|
+ return originalList[i].Id < originalList[j].Id
|
|
|
+ })
|
|
|
+
|
|
|
+ for i, v := range originalList {
|
|
|
+ if i == 0 {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if err := e.DB.Model(new(model.MilkOriginal)).
|
|
|
+ Select("").Where("id = ?", v.Id).
|
|
|
+ Update("nattach", 2).Error; err != nil {
|
|
|
+ zaplog.Error("UpdateRepeatCupSet1", zap.Any("err", err))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (e *Entry) UpdateMilkOriginCowInfo(milkOriginalList []*model.MilkOriginal) {
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+// UpdateRepeatCupSet2 非标准重复套杯
|
|
|
+func (e *Entry) UpdateRepeatCupSet2(milkOriginalList []*model.MilkOriginal) {
|
|
|
+ for _, v := range milkOriginalList {
|
|
|
+ if v.AttachTime == "" || v.InitialTime == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ attchTime, _ := util.TimeParseLocal(model.LayoutTime, v.AttachTime)
|
|
|
+ initialTime, _ := util.TimeParseLocal(model.LayoutTime, v.InitialTime)
|
|
|
+ if util.Substr(v.InitialTime, -1, 5) != "00:00" && v.Nattach == 0 && attchTime.Sub(initialTime).Minutes() <= 1 {
|
|
|
+ if err := e.DB.Model(new(model.MilkOriginal)).
|
|
|
+ Select("nattach").
|
|
|
+ Where("id = ?", v.Id).
|
|
|
+ Update("nattach", 2).Error; err != nil {
|
|
|
+ zaplog.Error("UpdateRepeatCupSet2", zap.Any("err", err))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-func (e *Entry) Delete1(data *model.MilkOriginal, xMinD string, cfg *MilkClassConfig) {
|
|
|
+func (e *Entry) delete1(data *model.MilkOriginal, xMinD string, cfg *MilkClassConfig) {
|
|
|
// 1. 删除attach_time为00:00:00的记录
|
|
|
acctchStr := util.Substr(data.AttachTime, -1, 8)
|
|
|
if data.MilkDate < xMinD || acctchStr != "00:00:00" {
|
|
@@ -303,7 +352,7 @@ func (e *Entry) Delete1(data *model.MilkOriginal, xMinD string, cfg *MilkClassCo
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (e *Entry) Delete2(data *model.MilkOriginal, xMinD string, cfg *MilkClassConfig) {
|
|
|
+func (e *Entry) delete2(data *model.MilkOriginal, xMinD string, cfg *MilkClassConfig) {
|
|
|
// 1. 检查记录是否在时间范围内
|
|
|
if data.MilkDate < xMinD {
|
|
|
return
|
|
@@ -335,7 +384,7 @@ func (e *Entry) Delete2(data *model.MilkOriginal, xMinD string, cfg *MilkClassCo
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (e *Entry) Delete3(data *model.MilkOriginal, xMinD string, cfg *MilkClassConfig) {
|
|
|
+func (e *Entry) delete3(data *model.MilkOriginal, xMinD string, cfg *MilkClassConfig) {
|
|
|
// 1. 检查记录是否在时间范围内
|
|
|
if data.MilkDate < xMinD {
|
|
|
return
|
|
@@ -372,7 +421,7 @@ func (e *Entry) Delete3(data *model.MilkOriginal, xMinD string, cfg *MilkClassCo
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (e *Entry) Delete4(data *model.MilkOriginal, xMinD string, cfg *MilkClassConfig) {
|
|
|
+func (e *Entry) delete4(data *model.MilkOriginal, xMinD string, cfg *MilkClassConfig) {
|
|
|
// 1. 检查记录是否在时间范围内
|
|
|
if data.MilkDate < "2020-10-01" {
|
|
|
return
|