作者 yangfu

refactor: 产能统计代码优化

  1 +delete FROM "dictionarys" where dictionary_id in (3812266576427090944);
  2 +
  3 +INSERT INTO "public"."dictionarys"("dictionary_id", "dict_code", "dict_name", "describe", "dict_items") VALUES (3972873532463383552, 'ACM-001', '天联共创-生产制造-物料属性', '', '[{"sort": 1, "isShow": 1, "itemCode": "1", "itemLabel": "", "itemValue": "外购"}, {"sort": 2, "isShow": 1, "itemCode": "2", "itemLabel": "", "itemValue": "自制"}, {"sort": 3, "isShow": 1, "itemCode": "3", "itemLabel": "", "itemValue": "委外"}]');
  4 +INSERT INTO "public"."dictionarys"("dictionary_id", "dict_code", "dict_name", "describe", "dict_items") VALUES (3972875981165167616, 'ACM-002', '天联共创-生产制造-物料类别', '', '[{"sort": 1, "isShow": 1, "itemCode": "1", "itemLabel": "", "itemValue": "原材料"}, {"sort": 2, "isShow": 1, "itemCode": "2", "itemLabel": "", "itemValue": "半成品"}, {"sort": 3, "isShow": 1, "itemCode": "3", "itemLabel": "", "itemValue": "成品"}]');
  5 +INSERT INTO "public"."dictionarys"("dictionary_id", "dict_code", "dict_name", "describe", "dict_items") VALUES (3972882719129994240, 'ACM-003', '天联共创-生产制造-单位', '', '[{"sort": 1, "isShow": 1, "itemCode": "1", "itemLabel": "", "itemValue": "千克"}, {"sort": 2, "isShow": 1, "itemCode": "2", "itemLabel": "", "itemValue": "个"}, {"sort": 3, "isShow": 1, "itemCode": "3", "itemLabel": "", "itemValue": "包"}, {"sort": 4, "isShow": 1, "itemCode": "4", "itemLabel": "", "itemValue": "箱"}, {"sort": 5, "isShow": 1, "itemCode": "5", "itemLabel": "", "itemValue": "片"}, {"sort": 6, "isShow": 1, "itemCode": "6", "itemLabel": "", "itemValue": "套"}, {"sort": 7, "isShow": 1, "itemCode": "7", "itemLabel": "", "itemValue": "份"}, {"sort": 8, "isShow": 1, "itemCode": "8", "itemLabel": "", "itemValue": "台"}, {"sort": 9, "isShow": 1, "itemCode": "9", "itemLabel": "", "itemValue": "米"}, {"sort": 10, "isShow": 1, "itemCode": "10", "itemLabel": "", "itemValue": "升"}, {"sort": 11, "isShow": 1, "itemCode": "11", "itemLabel": "", "itemValue": "辆"}, {"sort": 12, "isShow": 1, "itemCode": "12", "itemLabel": "", "itemValue": "秒"}, {"sort": 13, "isShow": 1, "itemCode": "13", "itemLabel": "", "itemValue": "栋"}]');
  6 +INSERT INTO "public"."dictionarys"("dictionary_id", "dict_code", "dict_name", "describe", "dict_items") VALUES (3978995238135399424, 'ACM-004', '天联共创-生产制造-单位换算-物料类别', '', '[{"sort": 1, "isShow": 1, "itemCode": "1", "itemLabel": "", "itemValue": "南瓜饼串"}, {"sort": 2, "isShow": 1, "itemCode": "2", "itemLabel": "", "itemValue": "包装袋(空)"}, {"sort": 3, "isShow": 1, "itemCode": "3", "itemLabel": "", "itemValue": "包装袋(件数)"}, {"sort": 4, "isShow": 1, "itemCode": "4", "itemLabel": "", "itemValue": "装箱(件数)"}]');
  7 +INSERT INTO "public"."dictionarys"("dictionary_id", "dict_code", "dict_name", "describe", "dict_items") VALUES (3812266576427090944, 'XTZD-003', '生产制造-单位', '', '[{"sort": 1, "isShow": 1, "itemCode": "1", "itemLabel": "", "itemValue": "个"}, {"sort": 2, "isShow": 1, "itemCode": "2", "itemLabel": "", "itemValue": "袋"}, {"sort": 3, "isShow": 1, "itemCode": "3", "itemLabel": "", "itemValue": "kg"}, {"sort": 4, "isShow": 1, "itemCode": "4", "itemLabel": "", "itemValue": "串"}, {"sort": 5, "isShow": 1, "itemCode": "5", "itemLabel": "", "itemValue": "箱"}, {"sort": 6, "isShow": 1, "itemCode": "6", "itemLabel": "", "itemValue": "件"}]');
  1 +package crontab
  2 +
  3 +import (
  4 + "context"
  5 + "fmt"
  6 + "github.com/linmadan/egglib-go/transaction/pg"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
  8 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/syncdata"
  9 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
  10 + "runtime/debug"
  11 +)
  12 +
  13 +// SyncMaterial 定时同步物料
  14 +func SyncMaterial(ctx context.Context) error {
  15 + defer func() {
  16 + if r := recover(); r != nil {
  17 + log.Logger.Error(fmt.Sprintf("%v", r), map[string]interface{}{"task": "定时同步物料", "stack": string(debug.Stack())})
  18 + }
  19 + }()
  20 + transactionContext, err := factory.CreateTransactionContext(nil)
  21 + if err != nil {
  22 + return err
  23 + }
  24 + if err := transactionContext.StartTransaction(); err != nil {
  25 + return err
  26 + }
  27 + defer func() {
  28 + if err != nil {
  29 + log.Logger.Error("【定时同步物料】 失败:" + err.Error())
  30 + }
  31 + transactionContext.RollbackTransaction()
  32 + }()
  33 +
  34 + log.Logger.Debug("【定时同步物料】 启动")
  35 + pullK3CloudService := syncdata.PullDataK3CloudService{}
  36 + if err := pullK3CloudService.SyncDataMaterialGroup(transactionContext.(*pg.TransactionContext)); err != nil {
  37 + log.Logger.Error(err.Error(), map[string]interface{}{"task": "定时同步物料"})
  38 + return nil
  39 + }
  40 + if err = transactionContext.CommitTransaction(); err != nil {
  41 + return err
  42 + }
  43 + return nil
  44 +}
@@ -38,7 +38,7 @@ WITH ts_product as( @@ -38,7 +38,7 @@ WITH ts_product as(
38 and work_station->>'workshopId'='?' 38 and work_station->>'workshopId'='?'
39 and work_station->>'lineId'='?' 39 and work_station->>'lineId'='?'
40 and work_station->>'sectionName'=? 40 and work_station->>'sectionName'=?
41 - and created_at >? 41 + and created_at >=?
42 and created_at <? 42 and created_at <?
43 ) a 43 ) a
44 group by a.ts 44 group by a.ts
@@ -47,14 +47,14 @@ WITH ts_product as( @@ -47,14 +47,14 @@ WITH ts_product as(
47 -- select * from ts_product 47 -- select * from ts_product
48 , ts_product_list as ( 48 , ts_product_list as (
49 select d.ts,ts_product.total from ( 49 select d.ts,ts_product.total from (
50 - select to_char(c.ts::timestamp,'mm-dd') ts from (  
51 - select generate_series(to_timestamp(?),to_timestamp(?),'1 day') ts 50 + select to_char(c.ts::timestamp at time ZONE 'Asia/shanghai','mm-dd') ts from (
  51 + select generate_series(to_timestamp(?,'YYYY-MM-DD HH24:MI:SS'),to_timestamp(?,'YYYY-MM-DD HH24:MI:SS'),'1 day') ts
52 ) c ) d left join ts_product on d.ts = ts_product.ts 52 ) c ) d left join ts_product on d.ts = ts_product.ts
53 ) 53 )
54 SELECT ts, coalesce(total,0) total 54 SELECT ts, coalesce(total,0) total
55 from ts_product_list 55 from ts_product_list
56 `) 56 `)
57 - if _, err := tx.Query(result, sql, companyId, orgId, workshopId, lineId, sectionName, beginTime, endTime, beginTime.Unix(), endTime.Unix()); err != nil { 57 + if _, err := tx.Query(result, sql, companyId, orgId, workshopId, lineId, sectionName, beginTime, endTime, beginTime, endTime); err != nil {
58 return err 58 return err
59 } 59 }
60 return nil 60 return nil
@@ -120,7 +120,7 @@ select * from device_running_oee @@ -120,7 +120,7 @@ select * from device_running_oee
120 } 120 }
121 121
122 // 时段产能 122 // 时段产能
123 -func (dao *DeviceDailyRunningRecordDao) TimeSectionProductRecord(companyId, orgId, workshopId int, lineId int, sectionName string, beginTime time.Time, result interface{}) error { 123 +func (dao *DeviceDailyRunningRecordDao) TimeSectionProductRecord(companyId, orgId, workshopId int, lineId int, sectionName string, beginTime time.Time, endTime time.Time, result interface{}) error {
124 124
125 tx := dao.transactionContext.PgTx 125 tx := dao.transactionContext.PgTx
126 sql := fmt.Sprintf(` 126 sql := fmt.Sprintf(`
@@ -137,7 +137,8 @@ WITH ts_product as( @@ -137,7 +137,8 @@ WITH ts_product as(
137 and work_station->>'lineId'='?' 137 and work_station->>'lineId'='?'
138 and work_station->>'sectionName'=? 138 and work_station->>'sectionName'=?
139 and device_running_record_info->>'deviceType'='CCJ' 139 and device_running_record_info->>'deviceType'='CCJ'
140 - and created_at >? 140 + and created_at >=?
  141 + and created_at < ?
141 ) a 142 ) a
142 group by a.ts 143 group by a.ts
143 order by ts 144 order by ts
@@ -145,19 +146,15 @@ WITH ts_product as( @@ -145,19 +146,15 @@ WITH ts_product as(
145 -- select * from ts_product 146 -- select * from ts_product
146 , ts_product_list as ( 147 , ts_product_list as (
147 select d.ts,ts_product.total from ( 148 select d.ts,ts_product.total from (
148 - select to_char(c.ts::timestamp,'HH24:MI') ts from (  
149 - select generate_series(a.end - interval '5 hour',  
150 - "replace"(to_char(a.end, 'yyyy-mm-dd HH24:') || cast(date_part('minute',a.end) as integer)/30*30+30, ':0', ':00')::timestamp,  
151 - '30 minute') ts from (  
152 - select to_timestamp(to_char(now() at time ZONE 'Asia/shanghai','yyyy-mm-dd HH24'),'yyyy-mm-dd HH24') as end 149 + select "replace"(to_char(created_at at time ZONE 'Asia/shanghai', 'HH24:') || cast(date_part('minute',created_at) as integer)/30*30, ':0', ':00') ts from (
  150 + select generate_series(to_timestamp(?,'YYYY-MM-DD HH24:MI:SS'),to_timestamp(?,'YYYY-MM-DD HH24:MI:SS'),'30 minute') created_at
153 ) a 151 ) a
154 - ) c  
155 ) d left join ts_product on d.ts = ts_product.ts 152 ) d left join ts_product on d.ts = ts_product.ts
156 ) 153 )
157 SELECT ts, coalesce(total,0) total 154 SELECT ts, coalesce(total,0) total
158 from ts_product_list 155 from ts_product_list
159 `) 156 `)
160 - if _, err := tx.Query(result, sql, companyId, orgId, workshopId, lineId, sectionName, beginTime); err != nil { 157 + if _, err := tx.Query(result, sql, companyId, orgId, workshopId, lineId, sectionName, beginTime, endTime, beginTime, endTime.Add(-time.Minute*1)); err != nil {
161 return err 158 return err
162 } 159 }
163 return nil 160 return nil
@@ -94,11 +94,19 @@ func (ptr *PGCommonStatisticsService) HourProductiveStatistics(queryOptions map[ @@ -94,11 +94,19 @@ func (ptr *PGCommonStatisticsService) HourProductiveStatistics(queryOptions map[
94 } 94 }
95 productRecordDao, _ := dao.NewDeviceDailyRunningRecordDao(ptr.transactionContext) 95 productRecordDao, _ := dao.NewDeviceDailyRunningRecordDao(ptr.transactionContext)
96 96
97 - var response = make([]interface{}, 0)  
98 - var tmpXData = make([]string, 0) 97 + var (
  98 + response = make([]interface{}, 0)
  99 + tmpXData = make([]string, 0)
  100 + beginTime = xtime.BeginningOfDay() //time.Now().Add(-time.Hour*5)
  101 + endTime = time.Now()
  102 + )
  103 + if !xtime.IsZero(request.Date.Time()) {
  104 + beginTime = request.Date.Time().AddDate(0, 0, -1)
  105 + endTime = request.Date.Time()
  106 + }
99 for _, v := range workshop.GetProductLines(domain.NotDeleted) { 107 for _, v := range workshop.GetProductLines(domain.NotDeleted) {
100 var result = make([]*record, 0) 108 var result = make([]*record, 0)
101 - if err := productRecordDao.TimeSectionProductRecord(request.CompanyId, request.OrgId, request.WorkshopId, v.LineId, SectionNameCCJ, time.Now().Add(-time.Hour*5), &result); err != nil { 109 + if err := productRecordDao.TimeSectionProductRecord(request.CompanyId, request.OrgId, request.WorkshopId, v.LineId, SectionNameCCJ, beginTime, endTime, &result); err != nil {
102 log.Logger.Error(err.Error()) 110 log.Logger.Error(err.Error())
103 continue 111 continue
104 } 112 }
@@ -141,7 +149,7 @@ func NewXYData(xData []string, values interface{}) interface{} { @@ -141,7 +149,7 @@ func NewXYData(xData []string, values interface{}) interface{} {
141 return values 149 return values
142 } 150 }
143 151
144 -// 时段产能-统计 (传串设备) 152 +// DailyProductiveStatistics 每日产能-统计 (传串设备)
145 func (ptr *PGCommonStatisticsService) DailyProductiveStatistics(queryOptions map[string]interface{}) (interface{}, error) { 153 func (ptr *PGCommonStatisticsService) DailyProductiveStatistics(queryOptions map[string]interface{}) (interface{}, error) {
146 var request = &HourProductiveStatisticsRequest{} 154 var request = &HourProductiveStatisticsRequest{}
147 if err := utils.LoadQueryObject(queryOptions, request); err != nil { 155 if err := utils.LoadQueryObject(queryOptions, request); err != nil {
@@ -153,10 +161,14 @@ func (ptr *PGCommonStatisticsService) DailyProductiveStatistics(queryOptions map @@ -153,10 +161,14 @@ func (ptr *PGCommonStatisticsService) DailyProductiveStatistics(queryOptions map
153 Ts string `json:"ts"` 161 Ts string `json:"ts"`
154 Total float64 `json:"total"` 162 Total float64 `json:"total"`
155 } 163 }
156 - var date = time.Now()  
157 - if !xtime.IsZero(time.Time(request.Date)) {  
158 - date = time.Time(request.Date)  
159 - } 164 + var (
  165 + beingTime = xtime.BeginningOfDay().AddDate(0, 0, -5)
  166 + endTime = xtime.BeginningOfDay().AddDate(0, 0, 1)
  167 + )
  168 + //if !xtime.IsZero(time.Time(request.Date)) {
  169 + // beingTime = request.Date.Time().AddDate(0, 0, -5)
  170 + // endTime = request.Date.Time().AddDate(0, 0, 1)
  171 + //}
160 workshop, err := workshopRepository.FindOne(map[string]interface{}{"workshopId": request.WorkshopId}) 172 workshop, err := workshopRepository.FindOne(map[string]interface{}{"workshopId": request.WorkshopId})
161 if err != nil || workshop == nil { 173 if err != nil || workshop == nil {
162 return nil, nil 174 return nil, nil
@@ -168,7 +180,7 @@ func (ptr *PGCommonStatisticsService) DailyProductiveStatistics(queryOptions map @@ -168,7 +180,7 @@ func (ptr *PGCommonStatisticsService) DailyProductiveStatistics(queryOptions map
168 180
169 for _, v := range workshop.GetProductLines(domain.NotDeleted) { 181 for _, v := range workshop.GetProductLines(domain.NotDeleted) {
170 var result = make([]*record, 0) 182 var result = make([]*record, 0)
171 - if err := productRecordDao.TimeSectionRunningRecord(request.CompanyId, request.OrgId, request.WorkshopId, v.LineId, SectionNameCCJ, date.Add(-time.Hour*24*7), date, &result); err != nil { 183 + if err := productRecordDao.TimeSectionRunningRecord(request.CompanyId, request.OrgId, request.WorkshopId, v.LineId, SectionNameCCJ, beingTime, endTime, &result); err != nil {
172 log.Logger.Error(err.Error()) 184 log.Logger.Error(err.Error())
173 continue 185 continue
174 } 186 }
@@ -214,7 +226,7 @@ func (ptr *PGCommonStatisticsService) ProportionOfSecondLevelStatistics(queryOpt @@ -214,7 +226,7 @@ func (ptr *PGCommonStatisticsService) ProportionOfSecondLevelStatistics(queryOpt
214 productRecordDao, _ := dao.NewProductRecordDao(ptr.transactionContext) 226 productRecordDao, _ := dao.NewProductRecordDao(ptr.transactionContext)
215 var date = time.Now() 227 var date = time.Now()
216 if !xtime.IsZero(time.Time(request.Date)) { 228 if !xtime.IsZero(time.Time(request.Date)) {
217 - date = time.Time(request.Date) 229 + date = request.Date.Time()
218 } 230 }
219 var input = []struct { 231 var input = []struct {
220 name string 232 name string