作者 yangfu

refactor:1.代码优化

... ... @@ -5,7 +5,7 @@ go 1.16
require (
github.com/ajg/form v1.5.1 // indirect
github.com/beego/beego/v2 v2.0.1
github.com/bwmarrin/snowflake v0.3.0 // indirect
github.com/bwmarrin/snowflake v0.3.0
github.com/eclipse/paho.mqtt.golang v1.3.5
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072 // indirect
github.com/fatih/structs v1.1.0 // indirect
... ...
... ... @@ -20,6 +20,11 @@ import (
)
func main() {
defer func() {
if r := recover(); r != nil {
log.Logger.Error(fmt.Sprintf("%v", r))
}
}()
if constant.ENABLE_KAFKA_LOG {
w, _ := logrus.NewKafkaWriter(constant.KAFKA_HOST, constant.TOPIC_LOG_STASH, false)
log.Logger.AddHook(w)
... ...
... ... @@ -46,6 +46,9 @@ func (crontabService *CrontabService) initTask() {
autoFlushDeviceDailyRunningRecord := task.NewTask("定时刷新设备每日运行记录", "0 */1 * * * *", AutoFlushDeviceDailyRunningRecord)
task.AddTask("autoFlushDeviceDailyRunningRecord", autoFlushDeviceDailyRunningRecord)
autoFlushDeviceDailyRunningRecordOEE := task.NewTask("定时刷新设备每日运行记录-OEE", "0 */10 * * * *", AutoFlushDeviceDailyRunningRecordOEE)
task.AddTask("autoFlushDeviceDailyRunningRecord", autoFlushDeviceDailyRunningRecordOEE)
autoWorkshopPlanCompletionRecord := task.NewTask("定时刷新昨日车间计划完成纪录", "0 5 1-15/3 * * *", AutoWorkshopPlanCompletionRecord)
task.AddTask("autoWorkshopPlanCompletionRecord", autoWorkshopPlanCompletionRecord)
... ...
... ... @@ -3,8 +3,13 @@ package crontab
import (
"context"
"fmt"
"github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
"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/domainService"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
"time"
)
... ... @@ -40,6 +45,9 @@ func AutoFlushDeviceDailyRunningRecordOEE(ctx context.Context) error {
log.Logger.Error(err.Error())
return err
}
total := 24 * 60 * 60
deviceRepository, _, _ := factory.FastPgDevice(transactionContext, 0)
workshopProductRecordDao, _ := dao.NewWorkshopProductRecordDao(transactionContext.(*pg.TransactionContext))
for _, v := range records {
//if v.UpdatedAt.Add(time.Minute * 5).Before(time.Now()) {
... ... @@ -47,26 +55,47 @@ func AutoFlushDeviceDailyRunningRecordOEE(ctx context.Context) error {
// continue
//}
var (
pu float64 = 100
qu float64 = 100
)
// 更新设备效率 OEE = tu * pu * qu
/*
pu 性能利用
设备标准工时,
没有配置设备标准工时的为100
*/
//pu :=100
//if v.DeviceRunningRecordInfo
// 设备数据(标准工时)
// 只计算串串机
if v.DeviceRunningRecordInfo.DeviceType == domain.DeviceTypeChuanChuanJi {
// 设备数据(标准工时)
device, err := deviceRepository.FindOne(map[string]interface{}{"deviceId": v.DeviceId})
if device != nil && err == nil {
if device.Ext.DeviceExt != nil {
pu = utils.Round(float64((v.DeviceRunningRecordInfo.Count*100.0)/(total/device.Ext.DeviceExt.UnitProductionSecTime)), 1)
}
}
// 工段对应二级品数据
v.DeviceRunningRecordInfo.ResetOEE(0, 0)
record, err := workshopProductRecordDao.WorkStationProductRecord(v.CompanyId, v.OrgId, v.WorkStation.WorkStationId, t)
if record != nil && err == nil {
qu = float64(v.DeviceRunningRecordInfo.Count) * domainService.DefaultCCJUnitQuantity
qu = utils.Round((qu-record.SecondLevelWeigh)*100/qu, 1)
}
}
v.DeviceRunningRecordInfo.ResetOEE(pu, qu)
if _, err := deviceDailyRunningRecordRepository.Save(v); err != nil {
log.Logger.Error(err.Error())
continue
} else {
log.Logger.Debug(fmt.Sprintf("【定时刷新设备每日运行记录-OEE】 刷新记录 %v", v))
}
if err := redis.SaveDeviceDailyRunningRecord(v); err != nil {
log.Logger.Error(err.Error())
continue
}
log.Logger.Debug(fmt.Sprintf("【定时刷新设备每日运行记录-OEE】 刷新记录 %v", v))
}
if err = transactionContext.CommitTransaction(); err != nil {
... ...
... ... @@ -46,6 +46,7 @@ func (d *ProductLevelTwoRecord) LoadDto(m *domain.ProductRecord, orgId int) *Pro
d.WeighBefore = m.ProductRecordInfo.WeighBefore
d.WeighAfter = m.ProductRecordInfo.WeighAfter
d.ApproveStatus = m.ProductRecordInfo.ApproveStatus
d.WorkOn = m.ProductRecordInfo.WorkOn
if m.ProductRecordInfo.ApproveAt > 0 {
d.ApproveAt = time.Unix(m.ProductRecordInfo.ApproveAt, 0).Local().Format("2006-01-02 15:04:05")
d.ApproveUser = m.ProductRecordInfo.ApproveUser
... ...
... ... @@ -67,6 +67,8 @@ func (deviceDailyRunningRecord *DeviceDailyRunningRecord) String() string {
// 设备运行记录信息
type DeviceRunningRecordInfo struct {
// 设备类型
DeviceType string `json:"deviceType"`
// 当前状态
// bit0: 运行、停止
// bit1: 正常、故障
... ... @@ -117,6 +119,9 @@ func NewDeviceRunningRecordInfo() *DeviceRunningRecordInfo {
}
}
func (d *DeviceRunningRecordInfo) AddDeviceRunningData(t time.Time, data *DeviceRunningData) {
if len(d.DeviceType) == 0 {
d.DeviceType = data.DeviceType
}
d.CurrentStatus = data.StartupStatus | (1 << data.ComStatus)
d.ResetUpTime()
d.Count += data.Count
... ... @@ -163,7 +168,7 @@ func (d *DeviceRunningRecordInfo) ResetUpTime() float64 {
func (d *DeviceRunningRecordInfo) ResetOEE(pu, qu float64) float64 {
d.PerformanceUtilization = pu
d.QualificationUtilization = qu
d.OEE = (d.TimeUtilization + d.PerformanceUtilization + d.QualificationUtilization) / 3
d.OEE = utils.Round((d.TimeUtilization+d.PerformanceUtilization+d.QualificationUtilization)/3, 1)
return d.OEE
}
... ...
... ... @@ -34,4 +34,6 @@ type ProductRecordInfo struct {
// 生产小组ID
ProductGroupId int `json:"productGroupId,omitempty"`
// 上班班次 1:全天 2:白班 4:中班 8:夜班
WorkOn int `json:"workOn,omitempty"`
}
... ...
... ... @@ -33,6 +33,8 @@ type UserInfo struct {
Email string `json:"email,omitempty"`
UserName string `json:"userName,omitempty"`
Avatar string `json:"avatar,omitempty"`
// 员工类型 1:固定 2:派遣 3.临时
EmployeeType int `json:"employeeType,omitempty"`
}
// Company 公司信息
... ...
... ... @@ -48,6 +48,29 @@ func (dao *WorkshopProductRecordDao) WorkshopProductRecord(companyId, orgId, pla
}
}
// 车间对应工段的生产记录 (生产日期)
func (dao *WorkshopProductRecordDao) WorkStationProductRecord(companyId, orgId int, workStationId string, productTime time.Time) (*domain.WorkshopProductRecord, error) {
tx := dao.transactionContext.PgTx
employeeProductRecordModel := new(models.WorkshopProductRecord)
query := sqlbuilder.BuildQuery(tx.Model(employeeProductRecordModel), map[string]interface{}{})
query.Where("company_id = ?", companyId)
query.Where("org_id = ?", orgId)
query.Where("product_date = ?", productTime.Local().Format("2006-01-02"))
query.Where("work_station ->>'workStationId' = ?", workStationId)
if err := query.First(); err != nil {
if err.Error() == "pg: no rows in result set" {
return nil, domain.ErrorNotFound
} else {
return nil, err
}
}
if employeeProductRecordModel.WorkshopProductRecordId == 0 {
return nil, domain.ErrorNotFound
} else {
return transform.TransformToWorkshopProductRecordDomainModelFromPgModels(employeeProductRecordModel)
}
}
func (dao *WorkshopProductRecordDao) SearchWorkshopProductRecord(queryOptions map[string]interface{}) (int64, []*domain.WorkshopProductRecord, error) {
tx := dao.transactionContext.PgTx
var employeeProductRecordModels []*models.WorkshopProductRecord
... ...
... ... @@ -59,7 +59,7 @@ func (svr *UserService) ToUser(from *models.User) *domain.User {
user := &domain.User{
UserId: from.UserId,
UserName: from.UserInfo.UserName,
EmployeeType: from.EmployeeType,
EmployeeType: from.UserInfo.EmployeeType,
IcCardNumber: from.IcCardNumber,
Avatar: from.UserInfo.Avatar,
Phone: from.UserInfo.Phone,
... ...
... ... @@ -152,7 +152,7 @@ func (ptr *PGCommonStatisticsService) DailyProductiveStatistics(queryOptions map
var values []interface{} = make([]interface{}, 0)
for _, r := range result {
xData = append(xData, r.Ts)
values = append(values, r.Total)
values = append(values, utils.Round(r.Total*DefaultCCJUnitQuantity, 1))
}
if len(tmpXData) == 0 {
tmpXData = xData
... ...
... ... @@ -123,6 +123,7 @@ func (ptr *PGProductRecordService) SubmitProductRecord(productRecordType int, qu
BatchNumber: plan.BatchNumber,
PlanProductName: plan.PlanProductName,
ProductGroupId: request.ProductGroupId,
WorkOn: plan.WorkOn,
},
Ext: domain.NewExt(org.OrgName),
}
... ...