|
|
package domainService
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/dao"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/repository"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
|
|
|
"strconv"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
type PGWorkshopDataConsumeService struct {
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
// 消费设备生产数据
|
|
|
func (ptr *PGWorkshopDataConsumeService) Consume(companyId, orgId int, record *domain.DeviceCollection) (interface{}, error) {
|
|
|
var (
|
|
|
deviceRunningData *domain.DeviceRunningData
|
|
|
deviceRunningRecord *domain.DeviceRunningRecord
|
|
|
deviceDailyRecord *domain.DeviceDailyRunningRecord
|
|
|
workStation *domain.WorkStation
|
|
|
device *domain.Device
|
|
|
planId int
|
|
|
err error
|
|
|
plan *domain.ProductPlanDispatchRecord
|
|
|
datetime time.Time
|
|
|
)
|
|
|
var (
|
|
|
deviceRepository, _ = repository.NewDeviceRepository(ptr.transactionContext)
|
|
|
deviceRunningRecordRepository, _ = repository.NewDeviceRunningRecordRepository(ptr.transactionContext)
|
|
|
)
|
|
|
|
|
|
if deviceRunningData, err = ptr.newDeviceRunningData(record); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
// 0.初始化 从缓存捞数据、没取到查询库
|
|
|
deviceDailyRecord, err = redis.GetDeviceDailyRunningRecord(time.Now(), deviceRunningData.DeviceCode)
|
|
|
if err == domain.ErrorNotFound {
|
|
|
err = nil
|
|
|
}
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
if deviceDailyRecord != nil {
|
|
|
workStation = deviceDailyRecord.WorkStation
|
|
|
planId = deviceDailyRecord.DeviceRunningRecordInfo.ProductPlanId
|
|
|
device = &domain.Device{
|
|
|
DeviceId: deviceDailyRecord.DeviceId,
|
|
|
DeviceCode: deviceDailyRecord.DeviceCode,
|
|
|
}
|
|
|
} else {
|
|
|
|
|
|
// 0.1.查询记录对应的工段、批次
|
|
|
// 0.2.保存一天当日记录
|
|
|
if device, err = deviceRepository.FindOne(map[string]interface{}{"companyId": companyId, "orgId": orgId, "deviceCode": deviceRunningData.DeviceCode}); err != nil {
|
|
|
log.Logger.Error(fmt.Sprintf("【设备数据-消费】 未找到设备:%v 数据数据", deviceRunningData.DeviceCode))
|
|
|
return nil, nil
|
|
|
}
|
|
|
workStation = device.WorkStation
|
|
|
// 封箱机、串串机需要定位到批次
|
|
|
if record.DeviceType == domain.DeviceTypeFengXiangJi && record.DeviceType == domain.DeviceTypeChuanChuanJi {
|
|
|
datetime, _ = time.Parse("2006-01-02", deviceRunningData.Date)
|
|
|
if plan, err = ptr.findDeviceProductPlan(companyId, orgId, workStation.WorkStationId, datetime, deviceRunningData.DeviceCode); err != nil {
|
|
|
log.Logger.Error(err.Error())
|
|
|
} else {
|
|
|
planId = plan.PlanDispatchRecordExt.ProductPlanId
|
|
|
}
|
|
|
}
|
|
|
var saveErr error
|
|
|
if deviceDailyRecord, saveErr = ptr.saveDeviceDailyRunningRecord(companyId, orgId, workStation, device, planId, deviceRunningData); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
defer func() {
|
|
|
if saveErr != nil {
|
|
|
redis.RemoveDeviceDailyRunningRecord(time.Now(), deviceRunningData.DeviceCode)
|
|
|
}
|
|
|
}()
|
|
|
}
|
|
|
|
|
|
// 1.保存设备运行记录
|
|
|
deviceRunningRecord, _ = ptr.newDeviceRunningRecord(companyId, orgId, workStation, device, deviceRunningData)
|
|
|
if _, err = deviceRunningRecordRepository.Save(deviceRunningRecord); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
// 2.保存设备生产记录
|
|
|
|
|
|
// 3.更新 设备每日运行记录(汇总) - redis更新 十分钟异步刷库
|
|
|
deviceDailyRecord.DeviceRunningRecordInfo.AddDeviceRunningData(deviceRunningData.CollectionTime, deviceRunningData)
|
|
|
if err = redis.SaveDeviceDailyRunningRecord(deviceDailyRecord); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
return nil, nil
|
|
|
}
|
|
|
|
|
|
func (ptr *PGWorkshopDataConsumeService) newDeviceRunningData(record *domain.DeviceCollection) (*domain.DeviceRunningData, error) {
|
|
|
var err error
|
|
|
var data = &domain.DeviceRunningData{
|
|
|
DeviceCollectionId: record.DeviceCollectionId,
|
|
|
WorkShopName: record.WorkShopName,
|
|
|
CollectionTime: record.CollectionTime,
|
|
|
DeviceCode: record.DeviceSn,
|
|
|
DeviceType: record.DeviceType,
|
|
|
StartupStatus: int(record.StartupStatus),
|
|
|
ComStatus: int(record.ComStatus),
|
|
|
}
|
|
|
var mBytes []byte
|
|
|
if mBytes, err = json.Marshal(record.Values); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
var formatDate = func(y, m, d string) (string, error) {
|
|
|
yd, _ := strconv.Atoi(y)
|
|
|
md, _ := strconv.Atoi(m)
|
|
|
dd, _ := strconv.Atoi(d)
|
|
|
t := time.Date(yd, time.Month(md), dd, 0, 0, 0, 0, time.Local)
|
|
|
return t.Format("2006-01-02"), nil
|
|
|
}
|
|
|
switch record.DeviceType {
|
|
|
//包馅机
|
|
|
case domain.DeviceTypeBaoXianJi:
|
|
|
deviceBaoXianJi := &domain.DeviceBaoXianJi{}
|
|
|
err = json.Unmarshal(mBytes, deviceBaoXianJi)
|
|
|
if err != nil {
|
|
|
break
|
|
|
}
|
|
|
data.Count = int(deviceBaoXianJi.Count)
|
|
|
break
|
|
|
//油炸机
|
|
|
case domain.DeviceTypeYouZhaJi:
|
|
|
deviceYouZhaJi := &domain.DeviceYouZhaJi{}
|
|
|
err = json.Unmarshal(mBytes, deviceYouZhaJi)
|
|
|
if err != nil {
|
|
|
break
|
|
|
}
|
|
|
data.Temp1 = deviceYouZhaJi.FrontTemp
|
|
|
break
|
|
|
//串串机
|
|
|
case domain.DeviceTypeChuanChuanJi:
|
|
|
deviceChuanChuanJi := &domain.DeviceChuanChuanJi{}
|
|
|
err = json.Unmarshal(mBytes, deviceChuanChuanJi)
|
|
|
if err != nil {
|
|
|
break
|
|
|
}
|
|
|
data.Count = int(deviceChuanChuanJi.Count)
|
|
|
data.ProductType = deviceChuanChuanJi.ProductType
|
|
|
if data.Date, err = formatDate(deviceChuanChuanJi.Year, deviceChuanChuanJi.Month, deviceChuanChuanJi.Day); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
break
|
|
|
//速冻线
|
|
|
case domain.DeviceTypeSuDongXian:
|
|
|
deviceSuDongXian := &domain.DeviceSuDongXian{}
|
|
|
err = json.Unmarshal(mBytes, deviceSuDongXian)
|
|
|
if err != nil {
|
|
|
break
|
|
|
}
|
|
|
data.Temp1 = deviceSuDongXian.CurrTemp
|
|
|
break
|
|
|
//封口机
|
|
|
case domain.DeviceTypeFengKouJi:
|
|
|
deviceFengKouJi := &domain.DeviceFengKouJi{}
|
|
|
err = json.Unmarshal(mBytes, deviceFengKouJi)
|
|
|
if err != nil {
|
|
|
break
|
|
|
}
|
|
|
data.Count = int(deviceFengKouJi.Count)
|
|
|
data.ProductType = deviceFengKouJi.ProductType
|
|
|
if data.Date, err = formatDate(deviceFengKouJi.Year, deviceFengKouJi.Month, deviceFengKouJi.Day); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
break
|
|
|
//封箱机
|
|
|
case domain.DeviceTypeFengXiangJi:
|
|
|
deviceFengXiangJi := &domain.DeviceFengXiangJi{}
|
|
|
err = json.Unmarshal(mBytes, deviceFengXiangJi)
|
|
|
if err != nil {
|
|
|
break
|
|
|
}
|
|
|
data.Count = int(deviceFengXiangJi.Count)
|
|
|
data.ProductType = deviceFengXiangJi.ProductType
|
|
|
if data.Date, err = formatDate(deviceFengXiangJi.Year, deviceFengXiangJi.Month, deviceFengXiangJi.Day); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
break
|
|
|
//打浆机
|
|
|
case domain.DeviceTypeDaJiangJi:
|
|
|
default:
|
|
|
}
|
|
|
return data, nil
|
|
|
}
|
|
|
|
|
|
func (ptr *PGWorkshopDataConsumeService) newDeviceRunningRecord(companyId, orgId int, workStation *domain.WorkStation, device *domain.Device, data *domain.DeviceRunningData) (*domain.DeviceRunningRecord, error) {
|
|
|
return &domain.DeviceRunningRecord{
|
|
|
CompanyId: companyId,
|
|
|
OrgId: orgId,
|
|
|
WorkStation: workStation,
|
|
|
DeviceId: device.DeviceId,
|
|
|
DeviceCode: device.DeviceCode,
|
|
|
DeviceRunningRecordInfo: data,
|
|
|
CreatedAt: time.Now(),
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
func (ptr *PGWorkshopDataConsumeService) newProductRecord(companyId int, org *domain.Org, workStation *domain.WorkStation, device *domain.Device, data *domain.DeviceRunningData, planId int) (*domain.ProductRecord, error) {
|
|
|
return &domain.ProductRecord{
|
|
|
CompanyId: companyId,
|
|
|
OrgId: org.OrgId,
|
|
|
WorkStation: workStation,
|
|
|
ProductRecordType: domain.RecordTypeWeigh,
|
|
|
ProductWorker: &domain.User{},
|
|
|
CreatedAt: time.Now(),
|
|
|
UpdatedAt: time.Now(),
|
|
|
ProductRecordInfo: &domain.ProductRecordInfo{
|
|
|
ProductDate: data.CollectionTime.Format("2006-01-02"),
|
|
|
Original: float64(data.Count),
|
|
|
Weigh: float64(data.Count),
|
|
|
WeighBefore: float64(data.Count),
|
|
|
ApproveStatus: domain.AttendanceNotApprove,
|
|
|
ProductPlanId: planId,
|
|
|
},
|
|
|
Ext: domain.NewExt(org.OrgName),
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
func (ptr *PGWorkshopDataConsumeService) saveDeviceDailyRunningRecord(companyId, orgId int, workStation *domain.WorkStation, device *domain.Device, planId int, data *domain.DeviceRunningData) (*domain.DeviceDailyRunningRecord, error) {
|
|
|
var (
|
|
|
record *domain.DeviceDailyRunningRecord
|
|
|
err error
|
|
|
)
|
|
|
deviceDailyRunningRecordRepository, _ := repository.NewDeviceDailyRunningRecordRepository(ptr.transactionContext)
|
|
|
if record, err = deviceDailyRunningRecordRepository.FindOne(map[string]interface{}{
|
|
|
"workStationId": workStation.WorkStationId,
|
|
|
"deviceCode": data.DeviceCode,
|
|
|
"productDate": utils.GetZeroTime(time.Now()),
|
|
|
}); err != nil {
|
|
|
if err != domain.ErrorNotFound {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
if record != nil {
|
|
|
return record, nil
|
|
|
}
|
|
|
recordInfo := domain.NewDeviceRunningRecordInfo()
|
|
|
recordInfo.ProductPlanId = planId
|
|
|
record = &domain.DeviceDailyRunningRecord{
|
|
|
CompanyId: companyId,
|
|
|
OrgId: orgId,
|
|
|
WorkStation: workStation,
|
|
|
DeviceId: device.DeviceId,
|
|
|
DeviceCode: device.DeviceCode,
|
|
|
ProductDate: utils.GetZeroTime(time.Now()),
|
|
|
DeviceRunningRecordInfo: recordInfo,
|
|
|
CreatedAt: time.Now(),
|
|
|
UpdatedAt: time.Now(),
|
|
|
}
|
|
|
if record, err = deviceDailyRunningRecordRepository.Save(record); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
return record, nil
|
|
|
}
|
|
|
|
|
|
// 查找设备的生产计划,如果计划没有上线的话将他上线
|
|
|
func (ptr *PGWorkshopDataConsumeService) findDeviceProductPlan(companyId, orgId int, workStationId string, date time.Time, productCode string) (*domain.ProductPlanDispatchRecord, error) {
|
|
|
planDispatchRecordDao, _ := dao.NewProductPlanDispatchRecord(ptr.transactionContext)
|
|
|
planDispatchRecordRepository, _ := repository.NewProductPlanDispatchRecordRepository(ptr.transactionContext)
|
|
|
var setPlanOnline = false
|
|
|
record, err := planDispatchRecordDao.DeviceProductPlan(companyId, orgId, workStationId, date, productCode, domain.PlanOnline)
|
|
|
if err == domain.ErrorNotFound {
|
|
|
if record, err = planDispatchRecordDao.DeviceProductPlan(companyId, orgId, workStationId, date, productCode, domain.PlanOffline); err != nil {
|
|
|
return nil, err
|
|
|
} else {
|
|
|
setPlanOnline = true
|
|
|
}
|
|
|
}
|
|
|
if setPlanOnline {
|
|
|
record.ChangeStatus(domain.PlanOnline)
|
|
|
if record, err = planDispatchRecordRepository.Save(record); err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
return record, nil
|
|
|
}
|
|
|
|
|
|
func NewPGWorkshopDataConsumeService(transactionContext *pgTransaction.TransactionContext) (*PGWorkshopDataConsumeService, error) {
|
|
|
if transactionContext == nil {
|
|
|
return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
} else {
|
|
|
return &PGWorkshopDataConsumeService{
|
|
|
transactionContext: transactionContext,
|
|
|
}, nil
|
|
|
}
|
|
|
} |
...
|
...
|
|