作者 yangfu

feat: 统计时间过滤

... ... @@ -21,7 +21,7 @@ func NewDeviceDailyRunningRecordDao(transactionContext *pgTransaction.Transactio
}
// 时段产能
func (dao *DeviceDailyRunningRecordDao) TimeSectionRunningRecord(companyId, orgId, workshopId int, lineId int, sectionName string, beginTime time.Time, result interface{}) error {
func (dao *DeviceDailyRunningRecordDao) TimeSectionRunningRecord(companyId, orgId, workshopId int, lineId int, sectionName string, beginTime time.Time, endTime time.Time, result interface{}) error {
tx := dao.transactionContext.PgTx
sql := fmt.Sprintf(`
... ... @@ -39,6 +39,7 @@ WITH ts_product as(
and work_station->>'lineId'='?'
and work_station->>'sectionName'=?
and created_at >?
and created_at <?
) a
group by a.ts
order by ts
... ... @@ -47,20 +48,20 @@ WITH ts_product as(
, ts_product_list as (
select d.ts,ts_product.total from (
select to_char(c.ts::timestamp,'mm-dd') ts from (
select generate_series(now() - interval '6 day',now(),'1 day') ts
select generate_series(to_timestamp(?),to_timestamp(?),'1 day') ts
) c ) d left join ts_product on d.ts = ts_product.ts
)
SELECT ts, coalesce(total,0) total
from ts_product_list
`)
if _, err := tx.Query(result, sql, companyId, orgId, workshopId, lineId, sectionName, beginTime); err != nil {
if _, err := tx.Query(result, sql, companyId, orgId, workshopId, lineId, sectionName, beginTime, endTime, beginTime.Unix(), endTime.Unix()); err != nil {
return err
}
return nil
}
// 车间性能
func (dao *DeviceDailyRunningRecordDao) WorkshopProductionEfficiencyStatistics(companyId, orgId, workshopId int, beginTime time.Time, result interface{}) error {
func (dao *DeviceDailyRunningRecordDao) WorkshopProductionEfficiencyStatistics(companyId, orgId, workshopId int, beginTime time.Time, endTime time.Time, result interface{}) error {
tx := dao.transactionContext.PgTx
sql := fmt.Sprintf(`
... ... @@ -76,10 +77,11 @@ company_id = ?
and org_id = ?
and work_station->>'workshopId'='?'
and created_at>=?
and created_at<?
)
select round(avg(oee),1) oee,round(avg(tu),1) tu,round(avg(pu),1)pu, round(avg(qu),1) qu from device_running
`)
if _, err := tx.Query(result, sql, companyId, orgId, workshopId, beginTime); err != nil {
if _, err := tx.Query(result, sql, companyId, orgId, workshopId, beginTime, endTime); err != nil {
return err
}
return nil
... ...
... ... @@ -100,7 +100,7 @@ from ts_product_list
}
// 二级品比重
func (dao *ProductRecordDao) ProportionOfSecondLevelRecord(companyId, orgId, workshopId int, beginTime time.Time, result interface{}) error {
func (dao *ProductRecordDao) ProportionOfSecondLevelRecord(companyId, orgId, workshopId int, beginTime time.Time, endTime time.Time, result interface{}) error {
tx := dao.transactionContext.PgTx
sql := fmt.Sprintf(`
... ... @@ -116,6 +116,7 @@ select sum(a.weight) item_total,max(sname) sname from (
and product_record_type = 8
and product_record_info->>'approveStatus'='2'
and created_at >=?
and created_at <?
and work_station->>'sectionName' in ('打料','成型','穿串','包装')
) a
group by a.workStationId
... ... @@ -128,7 +129,7 @@ select sname,round(item_total/(select sum(item_total) from item_product)*100, 0)
select a.sname, coalesce(b.rate,0) rate from (
select unnest(ARRAY ['打料','成型','穿串','包装']) sname
) a left join item_product_rate b on a.sname=b.sname`)
if _, err := tx.Query(result, sql, companyId, orgId, workshopId, beginTime); err != nil {
if _, err := tx.Query(result, sql, companyId, orgId, workshopId, beginTime, endTime); err != nil {
return err
}
return nil
... ...
... ... @@ -3,6 +3,7 @@ package domainService
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/linmadan/egglib-go/utils/xtime"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/dao"
... ... @@ -122,9 +123,10 @@ func (ptr *PGCommonStatisticsService) HourProductiveStatistics(queryOptions map[
}
type HourProductiveStatisticsRequest struct {
CompanyId int `json:"companyId" valid:"Required"`
OrgId int `json:"orgId" valid:"Required"`
WorkshopId int `json:"workshopId" valid:"Required"`
CompanyId int `json:"companyId" valid:"Required"`
OrgId int `json:"orgId" valid:"Required"`
WorkshopId int `json:"workshopId" valid:"Required"`
Date time.Time `json:"date"`
}
func NewXYData(xData []string, values interface{}) interface{} {
... ... @@ -151,7 +153,10 @@ func (ptr *PGCommonStatisticsService) DailyProductiveStatistics(queryOptions map
Ts string `json:"ts"`
Total float64 `json:"total"`
}
var date = time.Now()
if !xtime.IsZero(request.Date) {
date = request.Date
}
workshop, err := workshopRepository.FindOne(map[string]interface{}{"workshopId": request.WorkshopId})
if err != nil || workshop == nil {
return nil, nil
... ... @@ -163,7 +168,7 @@ func (ptr *PGCommonStatisticsService) DailyProductiveStatistics(queryOptions map
for _, v := range workshop.GetProductLines(domain.NotDeleted) {
var result = make([]*record, 0)
if err := productRecordDao.TimeSectionRunningRecord(request.CompanyId, request.OrgId, request.WorkshopId, v.LineId, SectionNameCCJ, time.Now().Add(-time.Hour*24*7), &result); err != nil {
if err := productRecordDao.TimeSectionRunningRecord(request.CompanyId, request.OrgId, request.WorkshopId, v.LineId, SectionNameCCJ, date.Add(-time.Hour*24*7), date, &result); err != nil {
log.Logger.Error(err.Error())
continue
}
... ... @@ -207,20 +212,24 @@ func (ptr *PGCommonStatisticsService) ProportionOfSecondLevelStatistics(queryOpt
return nil, nil
}
productRecordDao, _ := dao.NewProductRecordDao(ptr.transactionContext)
var date = time.Now()
if !xtime.IsZero(request.Date) {
date = request.Date
}
var input = []struct {
name string
t time.Time
name string
begin time.Time
end time.Time
}{
{"today", utils.GetZeroTime(time.Now())},
{"current_week", utils.GetCurrentWeekFirstDay(time.Now())},
{"current_month", utils.GetCurrentMonthFirstDay(time.Now())},
{"today", utils.GetZeroTime(date), utils.GetZeroTime(date.AddDate(0, 0, 1))},
{"current_week", utils.GetCurrentWeekFirstDay(date), utils.GetCurrentWeekFirstDay(date.AddDate(0, 0, 7))},
{"current_month", utils.GetCurrentMonthFirstDay(date), utils.GetCurrentMonthFirstDay(date.AddDate(0, 1, 0))},
}
var response = make(map[string]interface{})
for _, v := range input {
var result = make([]*record, 0)
if err := productRecordDao.ProportionOfSecondLevelRecord(request.CompanyId, request.OrgId, request.WorkshopId, v.t, &result); err != nil {
if err := productRecordDao.ProportionOfSecondLevelRecord(request.CompanyId, request.OrgId, request.WorkshopId, v.begin, v.end, &result); err != nil {
log.Logger.Error(err.Error())
return nil, err
}
... ... @@ -252,20 +261,24 @@ func (ptr *PGCommonStatisticsService) WorkshopProductionEfficiencyStatistics(que
return nil, nil
}
productRecordDao, _ := dao.NewDeviceDailyRunningRecordDao(ptr.transactionContext)
var date = time.Now()
if !xtime.IsZero(request.Date) {
date = request.Date
}
var input = []struct {
name string
t time.Time
name string
begin time.Time
end time.Time
}{
{"today", utils.GetZeroTime(time.Now())},
{"current_week", utils.GetCurrentWeekFirstDay(time.Now())},
{"current_month", utils.GetCurrentMonthFirstDay(time.Now())},
{"today", utils.GetZeroTime(date), utils.GetZeroTime(date.AddDate(0, 0, 1))},
{"current_week", utils.GetCurrentWeekFirstDay(date), utils.GetCurrentWeekFirstDay(date.AddDate(0, 0, 7))},
{"current_month", utils.GetCurrentMonthFirstDay(date), utils.GetCurrentMonthFirstDay(date.AddDate(0, 1, 0))},
}
var response = make(map[string]interface{})
for _, v := range input {
var result = record{}
if err := productRecordDao.WorkshopProductionEfficiencyStatistics(request.CompanyId, request.OrgId, request.WorkshopId, v.t, &result); err != nil {
if err := productRecordDao.WorkshopProductionEfficiencyStatistics(request.CompanyId, request.OrgId, request.WorkshopId, v.begin, v.end, &result); err != nil {
log.Logger.Error(err.Error())
return nil, err
}
... ...