作者 yangfu

refactor:1.代码优化

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