作者 yangfu

feat: 统计时间过滤

@@ -21,7 +21,7 @@ func NewDeviceDailyRunningRecordDao(transactionContext *pgTransaction.Transactio @@ -21,7 +21,7 @@ func NewDeviceDailyRunningRecordDao(transactionContext *pgTransaction.Transactio
21 } 21 }
22 22
23 // 时段产能 23 // 时段产能
24 -func (dao *DeviceDailyRunningRecordDao) TimeSectionRunningRecord(companyId, orgId, workshopId int, lineId int, sectionName string, beginTime time.Time, result interface{}) error { 24 +func (dao *DeviceDailyRunningRecordDao) TimeSectionRunningRecord(companyId, orgId, workshopId int, lineId int, sectionName string, beginTime time.Time, endTime time.Time, result interface{}) error {
25 25
26 tx := dao.transactionContext.PgTx 26 tx := dao.transactionContext.PgTx
27 sql := fmt.Sprintf(` 27 sql := fmt.Sprintf(`
@@ -39,6 +39,7 @@ WITH ts_product as( @@ -39,6 +39,7 @@ WITH ts_product as(
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 ) a 43 ) a
43 group by a.ts 44 group by a.ts
44 order by ts 45 order by ts
@@ -47,20 +48,20 @@ WITH ts_product as( @@ -47,20 +48,20 @@ WITH ts_product as(
47 , ts_product_list as ( 48 , ts_product_list as (
48 select d.ts,ts_product.total from ( 49 select d.ts,ts_product.total from (
49 select to_char(c.ts::timestamp,'mm-dd') ts from ( 50 select to_char(c.ts::timestamp,'mm-dd') ts from (
50 - select generate_series(now() - interval '6 day',now(),'1 day') ts 51 + select generate_series(to_timestamp(?),to_timestamp(?),'1 day') ts
51 ) 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
52 ) 53 )
53 SELECT ts, coalesce(total,0) total 54 SELECT ts, coalesce(total,0) total
54 from ts_product_list 55 from ts_product_list
55 `) 56 `)
56 - if _, err := tx.Query(result, sql, companyId, orgId, workshopId, lineId, sectionName, beginTime); err != nil { 57 + if _, err := tx.Query(result, sql, companyId, orgId, workshopId, lineId, sectionName, beginTime, endTime, beginTime.Unix(), endTime.Unix()); err != nil {
57 return err 58 return err
58 } 59 }
59 return nil 60 return nil
60 } 61 }
61 62
62 // 车间性能 63 // 车间性能
63 -func (dao *DeviceDailyRunningRecordDao) WorkshopProductionEfficiencyStatistics(companyId, orgId, workshopId int, beginTime time.Time, result interface{}) error { 64 +func (dao *DeviceDailyRunningRecordDao) WorkshopProductionEfficiencyStatistics(companyId, orgId, workshopId int, beginTime time.Time, endTime time.Time, result interface{}) error {
64 65
65 tx := dao.transactionContext.PgTx 66 tx := dao.transactionContext.PgTx
66 sql := fmt.Sprintf(` 67 sql := fmt.Sprintf(`
@@ -76,10 +77,11 @@ company_id = ? @@ -76,10 +77,11 @@ company_id = ?
76 and org_id = ? 77 and org_id = ?
77 and work_station->>'workshopId'='?' 78 and work_station->>'workshopId'='?'
78 and created_at>=? 79 and created_at>=?
  80 +and created_at<?
79 ) 81 )
80 select round(avg(oee),1) oee,round(avg(tu),1) tu,round(avg(pu),1)pu, round(avg(qu),1) qu from device_running 82 select round(avg(oee),1) oee,round(avg(tu),1) tu,round(avg(pu),1)pu, round(avg(qu),1) qu from device_running
81 `) 83 `)
82 - if _, err := tx.Query(result, sql, companyId, orgId, workshopId, beginTime); err != nil { 84 + if _, err := tx.Query(result, sql, companyId, orgId, workshopId, beginTime, endTime); err != nil {
83 return err 85 return err
84 } 86 }
85 return nil 87 return nil
@@ -100,7 +100,7 @@ from ts_product_list @@ -100,7 +100,7 @@ from ts_product_list
100 } 100 }
101 101
102 // 二级品比重 102 // 二级品比重
103 -func (dao *ProductRecordDao) ProportionOfSecondLevelRecord(companyId, orgId, workshopId int, beginTime time.Time, result interface{}) error { 103 +func (dao *ProductRecordDao) ProportionOfSecondLevelRecord(companyId, orgId, workshopId int, beginTime time.Time, endTime time.Time, result interface{}) error {
104 104
105 tx := dao.transactionContext.PgTx 105 tx := dao.transactionContext.PgTx
106 sql := fmt.Sprintf(` 106 sql := fmt.Sprintf(`
@@ -116,6 +116,7 @@ select sum(a.weight) item_total,max(sname) sname from ( @@ -116,6 +116,7 @@ select sum(a.weight) item_total,max(sname) sname from (
116 and product_record_type = 8 116 and product_record_type = 8
117 and product_record_info->>'approveStatus'='2' 117 and product_record_info->>'approveStatus'='2'
118 and created_at >=? 118 and created_at >=?
  119 + and created_at <?
119 and work_station->>'sectionName' in ('打料','成型','穿串','包装') 120 and work_station->>'sectionName' in ('打料','成型','穿串','包装')
120 ) a 121 ) a
121 group by a.workStationId 122 group by a.workStationId
@@ -128,7 +129,7 @@ select sname,round(item_total/(select sum(item_total) from item_product)*100, 0) @@ -128,7 +129,7 @@ select sname,round(item_total/(select sum(item_total) from item_product)*100, 0)
128 select a.sname, coalesce(b.rate,0) rate from ( 129 select a.sname, coalesce(b.rate,0) rate from (
129 select unnest(ARRAY ['打料','成型','穿串','包装']) sname 130 select unnest(ARRAY ['打料','成型','穿串','包装']) sname
130 ) a left join item_product_rate b on a.sname=b.sname`) 131 ) a left join item_product_rate b on a.sname=b.sname`)
131 - if _, err := tx.Query(result, sql, companyId, orgId, workshopId, beginTime); err != nil { 132 + if _, err := tx.Query(result, sql, companyId, orgId, workshopId, beginTime, endTime); err != nil {
132 return err 133 return err
133 } 134 }
134 return nil 135 return nil
@@ -3,6 +3,7 @@ package domainService @@ -3,6 +3,7 @@ package domainService
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 pgTransaction "github.com/linmadan/egglib-go/transaction/pg" 5 pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  6 + "github.com/linmadan/egglib-go/utils/xtime"
6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" 7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" 8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/dao" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/dao"
@@ -125,6 +126,7 @@ type HourProductiveStatisticsRequest struct { @@ -125,6 +126,7 @@ type HourProductiveStatisticsRequest struct {
125 CompanyId int `json:"companyId" valid:"Required"` 126 CompanyId int `json:"companyId" valid:"Required"`
126 OrgId int `json:"orgId" valid:"Required"` 127 OrgId int `json:"orgId" valid:"Required"`
127 WorkshopId int `json:"workshopId" valid:"Required"` 128 WorkshopId int `json:"workshopId" valid:"Required"`
  129 + Date time.Time `json:"date"`
128 } 130 }
129 131
130 func NewXYData(xData []string, values interface{}) interface{} { 132 func NewXYData(xData []string, values interface{}) interface{} {
@@ -151,7 +153,10 @@ func (ptr *PGCommonStatisticsService) DailyProductiveStatistics(queryOptions map @@ -151,7 +153,10 @@ func (ptr *PGCommonStatisticsService) DailyProductiveStatistics(queryOptions map
151 Ts string `json:"ts"` 153 Ts string `json:"ts"`
152 Total float64 `json:"total"` 154 Total float64 `json:"total"`
153 } 155 }
154 - 156 + var date = time.Now()
  157 + if !xtime.IsZero(request.Date) {
  158 + date = request.Date
  159 + }
155 workshop, err := workshopRepository.FindOne(map[string]interface{}{"workshopId": request.WorkshopId}) 160 workshop, err := workshopRepository.FindOne(map[string]interface{}{"workshopId": request.WorkshopId})
156 if err != nil || workshop == nil { 161 if err != nil || workshop == nil {
157 return nil, nil 162 return nil, nil
@@ -163,7 +168,7 @@ func (ptr *PGCommonStatisticsService) DailyProductiveStatistics(queryOptions map @@ -163,7 +168,7 @@ func (ptr *PGCommonStatisticsService) DailyProductiveStatistics(queryOptions map
163 168
164 for _, v := range workshop.GetProductLines(domain.NotDeleted) { 169 for _, v := range workshop.GetProductLines(domain.NotDeleted) {
165 var result = make([]*record, 0) 170 var result = make([]*record, 0)
166 - if err := productRecordDao.TimeSectionRunningRecord(request.CompanyId, request.OrgId, request.WorkshopId, v.LineId, SectionNameCCJ, time.Now().Add(-time.Hour*24*7), &result); err != nil { 171 + if err := productRecordDao.TimeSectionRunningRecord(request.CompanyId, request.OrgId, request.WorkshopId, v.LineId, SectionNameCCJ, date.Add(-time.Hour*24*7), date, &result); err != nil {
167 log.Logger.Error(err.Error()) 172 log.Logger.Error(err.Error())
168 continue 173 continue
169 } 174 }
@@ -207,20 +212,24 @@ func (ptr *PGCommonStatisticsService) ProportionOfSecondLevelStatistics(queryOpt @@ -207,20 +212,24 @@ func (ptr *PGCommonStatisticsService) ProportionOfSecondLevelStatistics(queryOpt
207 return nil, nil 212 return nil, nil
208 } 213 }
209 productRecordDao, _ := dao.NewProductRecordDao(ptr.transactionContext) 214 productRecordDao, _ := dao.NewProductRecordDao(ptr.transactionContext)
210 - 215 + var date = time.Now()
  216 + if !xtime.IsZero(request.Date) {
  217 + date = request.Date
  218 + }
211 var input = []struct { 219 var input = []struct {
212 name string 220 name string
213 - t time.Time 221 + begin time.Time
  222 + end time.Time
214 }{ 223 }{
215 - {"today", utils.GetZeroTime(time.Now())},  
216 - {"current_week", utils.GetCurrentWeekFirstDay(time.Now())},  
217 - {"current_month", utils.GetCurrentMonthFirstDay(time.Now())}, 224 + {"today", utils.GetZeroTime(date), utils.GetZeroTime(date.AddDate(0, 0, 1))},
  225 + {"current_week", utils.GetCurrentWeekFirstDay(date), utils.GetCurrentWeekFirstDay(date.AddDate(0, 0, 7))},
  226 + {"current_month", utils.GetCurrentMonthFirstDay(date), utils.GetCurrentMonthFirstDay(date.AddDate(0, 1, 0))},
218 } 227 }
219 228
220 var response = make(map[string]interface{}) 229 var response = make(map[string]interface{})
221 for _, v := range input { 230 for _, v := range input {
222 var result = make([]*record, 0) 231 var result = make([]*record, 0)
223 - if err := productRecordDao.ProportionOfSecondLevelRecord(request.CompanyId, request.OrgId, request.WorkshopId, v.t, &result); err != nil { 232 + if err := productRecordDao.ProportionOfSecondLevelRecord(request.CompanyId, request.OrgId, request.WorkshopId, v.begin, v.end, &result); err != nil {
224 log.Logger.Error(err.Error()) 233 log.Logger.Error(err.Error())
225 return nil, err 234 return nil, err
226 } 235 }
@@ -252,20 +261,24 @@ func (ptr *PGCommonStatisticsService) WorkshopProductionEfficiencyStatistics(que @@ -252,20 +261,24 @@ func (ptr *PGCommonStatisticsService) WorkshopProductionEfficiencyStatistics(que
252 return nil, nil 261 return nil, nil
253 } 262 }
254 productRecordDao, _ := dao.NewDeviceDailyRunningRecordDao(ptr.transactionContext) 263 productRecordDao, _ := dao.NewDeviceDailyRunningRecordDao(ptr.transactionContext)
255 - 264 + var date = time.Now()
  265 + if !xtime.IsZero(request.Date) {
  266 + date = request.Date
  267 + }
256 var input = []struct { 268 var input = []struct {
257 name string 269 name string
258 - t time.Time 270 + begin time.Time
  271 + end time.Time
259 }{ 272 }{
260 - {"today", utils.GetZeroTime(time.Now())},  
261 - {"current_week", utils.GetCurrentWeekFirstDay(time.Now())},  
262 - {"current_month", utils.GetCurrentMonthFirstDay(time.Now())}, 273 + {"today", utils.GetZeroTime(date), utils.GetZeroTime(date.AddDate(0, 0, 1))},
  274 + {"current_week", utils.GetCurrentWeekFirstDay(date), utils.GetCurrentWeekFirstDay(date.AddDate(0, 0, 7))},
  275 + {"current_month", utils.GetCurrentMonthFirstDay(date), utils.GetCurrentMonthFirstDay(date.AddDate(0, 1, 0))},
263 } 276 }
264 277
265 var response = make(map[string]interface{}) 278 var response = make(map[string]interface{})
266 for _, v := range input { 279 for _, v := range input {
267 var result = record{} 280 var result = record{}
268 - if err := productRecordDao.WorkshopProductionEfficiencyStatistics(request.CompanyId, request.OrgId, request.WorkshopId, v.t, &result); err != nil { 281 + if err := productRecordDao.WorkshopProductionEfficiencyStatistics(request.CompanyId, request.OrgId, request.WorkshopId, v.begin, v.end, &result); err != nil {
269 log.Logger.Error(err.Error()) 282 log.Logger.Error(err.Error())
270 return nil, err 283 return nil, err
271 } 284 }