作者 yangfu

fix: 设备数据统计修改

  1 +
  2 +/*
  3 +1.设备信息查询函数
  4 + code 设备编码,设备编码为空时返回所有设备信息数据
  5 + cid 公司ID
  6 + oid 组织ID
  7 +*/
  8 + */
  9 +CREATE OR REPLACE FUNCTION "manufacture"."device_running_info"("code" varchar, "cid" int4, "oid" int4)
  10 + RETURNS TABLE("t_device_name" text, "t_device_code" text, "t_status" text, "t_temperature" numeric, "t_temperature1" numeric, "t_device_type" text, "t_total" int4, "t_uptime" numeric) AS $BODY$
  11 +begin
  12 +
  13 + return query
  14 +
  15 + with product_device as (
  16 + select device_code,device_name,device_id,device_type from manufacture.device
  17 + where
  18 + company_id = 1
  19 + and org_id = 1
  20 + and ext #>'{deviceExt,isProductDevice}'='1'
  21 + )
  22 + ,device_status as(
  23 + select c.*,a.device_name,a.device_type,a.device_code device_code
  24 + from (
  25 + select cast(b.device_running_record_info->>'currentStatus' as INTEGER)status,
  26 + cast(b.device_running_record_info->>'temp' as DECIMAL) as t,
  27 + cast(b.device_running_record_info->>'count' as INTEGER) as total,
  28 + cast(b.device_running_record_info->>'upTime' as INTEGER) as uptime,
  29 + cast(b.device_running_record_info->>'temp1'as DECIMAL) as t1,
  30 + extract (min from now()-b.updated_at) span,
  31 + b.device_id
  32 + from
  33 + manufacture.device_daily_running_record b
  34 + where
  35 + company_id = 1
  36 + and org_id = 1
  37 + and b.created_at> (to_date(to_char(now(), 'YYYY-MM-DD'),'YYYY-MM-DD') - interval '8 hours')
  38 + )c right join product_device a on c.device_id = a.device_id
  39 + )
  40 + select cast(device_name as text) device_name,
  41 + cast(device_code as text) device_code,
  42 + cast(
  43 + (case when (status=3 or status=1) and span<10 then '运行中'
  44 + when (status=2 or status=0 )and span<10 then '待机中'
  45 + ELSE '故障中' end) as text
  46 + ) status,
  47 + cast(COALESCE(t,0) as DECIMAL) temperature,
  48 + cast(COALESCE(t1,0) as DECIMAL) temperature1,
  49 + cast(COALESCE(device_type,'') as TEXT) device_type,
  50 + cast(COALESCE(total,0) as INTEGER) total,
  51 + cast(COALESCE(uptime,0) as DECIMAL) uptime
  52 + from device_status
  53 + where device_code = code;
  54 +
  55 +end;
  56 +$BODY$
  57 + LANGUAGE plpgsql VOLATILE
  1 +
  2 +-- 表 product_plan
  3 +-- 唯一索引 company_id,org_id,batch_number
  4 +create unique index idx_product_plan_company_id_org_id_batch_number on manufacture.product_plan using btree (company_id,org_id,batch_number);
  5 +
  6 +
@@ -552,7 +552,7 @@ func (srv *PullDataK3CloudService) SyncDataProductPlan(ptr *pgTransaction.Transa @@ -552,7 +552,7 @@ func (srv *PullDataK3CloudService) SyncDataProductPlan(ptr *pgTransaction.Transa
552 plan.PlanDevoted = &domain.UnitQuantity{ 552 plan.PlanDevoted = &domain.UnitQuantity{
553 Unit: product.ProductSpec.Unit, 553 Unit: product.ProductSpec.Unit,
554 Quantity: v.Qty, 554 Quantity: v.Qty,
555 - Weight: product.ProductWeigh(0), 555 + Weight: product.ProductWeigh(v.Qty),
556 UnitWeight: product.ProductSpec.UnitWeight, 556 UnitWeight: product.ProductSpec.UnitWeight,
557 } 557 }
558 plan.Workshop = workshop.CloneSample() 558 plan.Workshop = workshop.CloneSample()
@@ -330,7 +330,7 @@ func (ptr *PGCommonStatisticsService) DeviceRunningStatistics(queryOptions map[s @@ -330,7 +330,7 @@ func (ptr *PGCommonStatisticsService) DeviceRunningStatistics(queryOptions map[s
330 if r != nil { 330 if r != nil {
331 //log.Logger.Debug(fmt.Sprintf("%v %v",r.UpdatedAt.Local().Hour(),r.UpdatedAt.Local().Minute())) 331 //log.Logger.Debug(fmt.Sprintf("%v %v",r.UpdatedAt.Local().Hour(),r.UpdatedAt.Local().Minute()))
332 m := r.UpdatedAt.Local().Hour()*60 + r.UpdatedAt.Local().Minute() 332 m := r.UpdatedAt.Local().Hour()*60 + r.UpdatedAt.Local().Minute()
333 - if r.UpdatedAt.Before(utils.GetZeroTime(time.Now())) { 333 + if r.UpdatedAt.After(r.ProductDate.Add(time.Hour * 24)) {
334 m = 60 * 24 334 m = 60 * 24
335 } 335 }
336 item["status"] = r.DeviceRunningRecordInfo.HourDeviceStatusDetail(m) 336 item["status"] = r.DeviceRunningRecordInfo.HourDeviceStatusDetail(m)