123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- package crontab
- import (
- "kpt-pasture/model"
- "kpt-pasture/util"
- "gitee.com/xuyiping_admin/pkg/logger/zaplog"
- "go.uber.org/zap"
- )
- // UpdateMilkLoad2 (UPDATE milkweight m SET m.nattach=1 WHERE m.wid BETWEEN xdminwid AND xdmaxwid AND m.milkdate=xcurdate AND m.nattach=0 AND m.station=xvarName;)
- func (e *Entry) UpdateMilkLoad2(pastureId int64, milkClassConfig *MilkClassConfig, hall *model.MilkHall) {
- milkOriginalList := make([]*model.MilkOriginal, 0)
- if err := e.DB.Model(new(model.MilkOriginal)).
- Where("pasture_id = ?", pastureId).
- Where("nattach = ?", 0).
- Where("milk_hall_number = ?", hall.Name).
- Where("id BETWEEN ? AND ?", milkClassConfig.OldUpdateMaxId+1, milkClassConfig.CurrentMaxId).
- Find(&milkOriginalList).Error; err != nil {
- return
- }
- for _, v := range milkOriginalList {
- if err := e.DB.Model(new(model.MilkOriginal)).
- Where("id = ?", v.Id).
- Update("nattach", 1).Error; err != nil {
- zaplog.Error("UpdateMilkLoad2", zap.Any("err", err))
- return
- }
- }
- }
- // UpdateMilkLoad3 更新重复套杯牛只圈数
- func (e *Entry) UpdateMilkLoad3(pastureId int64, milkClassConfig *MilkClassConfig, hall *model.MilkHall) {
- milkOriginalList := make([]*model.MilkOriginal, 0)
- if err := e.DB.Model(new(model.MilkOriginal)).
- Where("pasture_id = ?", pastureId).
- Where("milk_hall_number = ?", hall.Name).
- Where("id BETWEEN ? AND ?", milkClassConfig.OldUpdateMaxId+1, milkClassConfig.CurrentMaxId).
- Order("shifts,detached_address,id").
- Find(&milkOriginalList).Error; err != nil {
- zaplog.Error("UpdateMilkLoad3", zap.Any("err", err))
- return
- }
- // 如果没有数据,直接返回
- if len(milkOriginalList) == 0 {
- return
- }
- // 初始化变量,模拟SQL中的变量
- //var currentShifts int32 = 6
- var currentAddress int64 = 0
- var currentLoad int32 = 1
- // 创建需要更新的记录映射
- updateMap := make(map[int64]int32)
- // 遍历排序后的记录,计算正确的load值
- for _, record := range milkOriginalList {
- // 如果nattach=1或者detached_address变化,则更新currentLoad
- if record.Nattach == 1 || currentAddress != record.DetachedAddress {
- currentLoad = record.Load
- }
- // 更新当前变量值
- //currentShifts = record.Shifts
- currentAddress = record.DetachedAddress
- // 如果nattach=2,则记录需要更新的load值
- if record.Nattach == 2 {
- updateMap[record.Id] = currentLoad
- }
- }
- // 更新数据库中的load字段
- for id, load := range updateMap {
- if err := e.DB.Model(new(model.MilkOriginal)).
- Where("id = ?", id).
- Update("load", load).Error; err != nil {
- zaplog.Error("UpdateMilkLoad3", zap.Any("err", err), zap.Any("id", id), zap.Any("load", load))
- return
- }
- }
- }
- // UpdateMilkCowIdResetZero 假定2.5分钟内套杯为重复套杯,3分钟外为其它牛只,清除错误牛号
- func (e *Entry) UpdateMilkCowIdResetZero(pastureId int64, milkClassConfig *MilkClassConfig, hall *model.MilkHall) {
- // 1. 首先查询重复套杯的牛只记录
- // 查询条件:同一牧场、同一奶厅、同一班次、同一脱杯地址、同一牛号有多条记录
- duplicateCowRecords := make([]*model.BaseRecords, 0)
- if err := e.DB.Model(new(model.MilkOriginal)).
- Select("milk_date, shifts, detached_address, cow_id, COUNT(0) as count").
- Where("pasture_id = ?", pastureId).
- Where("milk_hall_number = ?", hall.Name).
- Where("id BETWEEN ? AND ?", milkClassConfig.OldUpdateMaxId+1, milkClassConfig.CurrentMaxId).
- Where("cow_id > ?", 0).
- Group("milk_date, shifts, detached_address, cow_id").
- Having("count > ?", 1).
- Find(&duplicateCowRecords).Error; err != nil {
- zaplog.Error("UpdateMilkCowIdResetZero", zap.Any("err", err))
- return
- }
- // 如果没有重复套杯的记录,直接返回
- if len(duplicateCowRecords) == 0 {
- return
- }
- // 2. 查询需要处理的详细记录
- baseRecords := make([]*model.BaseRecords, 0)
- for _, record := range duplicateCowRecords {
- var records []*model.BaseRecords
- if err := e.DB.Model(new(model.MilkOriginal)).
- Select("id, attach_adjust_time, detached_address, shifts, ear_number, milk_date, recognition_time").
- Where("pasture_id = ?", pastureId).
- Where("milk_hall_number = ?", hall.Name).
- Where("milk_date = ?", record.MilkDate).
- Where("shifts = ?", record.Shifts).
- Where("detached_address = ?", record.DetachedAddress).
- Where("cow_id = ?", record.CowId).
- Where("id BETWEEN ? AND ?", milkClassConfig.OldUpdateMaxId+1, milkClassConfig.CurrentMaxId).
- Order("shifts, detached_address, cow_id, id").
- Find(&records).Error; err != nil {
- zaplog.Error("UpdateMilkCowIdResetZero", zap.Any("err", err))
- continue
- }
- baseRecords = append(baseRecords, records...)
- }
- // 如果没有详细记录,直接返回
- if len(baseRecords) == 0 {
- return
- }
- // 3. 初始化变量,模拟SQL中的变量
- var currentCowId int64 = 999999
- //var currentLoad int32 = 0
- var currentAddress int32 = 999
- var currentShifts int32 = 6
- var currentDetachTime string = "2001-01-01 01:01:01"
- // 4. 创建需要更新的记录映射
- updateMap := make(map[int64]bool)
- // 5. 遍历排序后的记录,计算需要清除牛号的记录
- for _, record := range baseRecords {
- // 解析时间
- attachAdjustTime, err := util.TimeParseLocal(model.LayoutTime, record.AttachAdjust)
- if err != nil {
- continue
- }
- detachTime, err := util.TimeParseLocal(model.LayoutTime, currentDetachTime)
- if err != nil {
- continue
- }
- // 计算时间差(秒)
- timeDiff := int64(attachAdjustTime.Sub(detachTime).Seconds())
- // 判断是否需要清除牛号
- // 条件:同一牛号、同一脱杯地址、同一班次,且时间差小于180秒(3分钟)
- if record.CowId == currentCowId && record.DetachedAddress == currentAddress && record.Shifts == currentShifts && timeDiff < 180 {
- // 这是重复套杯,不需要清除牛号
- } else if record.CowId == currentCowId {
- // 这是同一牛号但不同脱杯地址或班次,需要清除牛号
- updateMap[record.Id] = true
- }
- // 更新当前变量值
- currentCowId = record.CowId
- currentAddress = record.DetachedAddress
- currentShifts = record.Shifts
- currentDetachTime = record.AttachAdjust
- }
- // 6. 更新数据库中的记录,清除牛号
- for id := range updateMap {
- if err := e.DB.Model(new(model.MilkOriginal)).
- Where("id = ?", id).
- Updates(map[string]interface{}{
- "cow_id": 0,
- "ear_number": "",
- "pen_id": 0,
- "pen_name": "",
- }).Error; err != nil {
- zaplog.Error("UpdateMilkCowIdResetZero", zap.Any("err", err), zap.Any("id", id))
- return
- }
- }
- }
|