ALTER TABLE "public"."hs_board_manage" ADD COLUMN "sort" int4 DEFAULT 0;

COMMENT ON COLUMN "public"."hs_board_manage"."sort" IS '排序值';

ALTER TABLE "public"."hs_device_extension" ADD COLUMN "model_id" uuid;

COMMENT ON COLUMN "public"."hs_device_extension"."model_id" IS '模型id';

ALTER TABLE "public"."hs_last_expand_extension" ALTER COLUMN "volume_on_history" SET NOT NULL;

COMMENT ON COLUMN "public"."hs_last_expand_extension"."volume_on_history" IS '';

ALTER TABLE "public"."hs_user_extension" ALTER COLUMN "registrate_applet" DROP DEFAULT;

CREATE TABLE "public"."tb_tenant_menu_role" (
  "id" uuid NOT NULL,
  "created_time" int8,
  "created_user" uuid,
  "tenant_id" uuid,
  "updated_time" int8,
  "updated_user" uuid,
  "remark" varchar(255) COLLATE "pg_catalog"."default",
  "tenant_menu_id" uuid,
  "tenant_sys_role_id" uuid,
  "flg" varchar(255) COLLATE "pg_catalog"."default",
  CONSTRAINT "tb_tenant_menu_role_pkey" PRIMARY KEY ("id")
)
;

ALTER TABLE "public"."tb_tenant_menu_role" OWNER TO "postgres";

ALTER TABLE "public"."tcode_base" ALTER COLUMN "created_time" SET DEFAULT 0;

ALTER TABLE "public"."tcode_base" ALTER COLUMN "updated_time" SET DEFAULT 0;

ALTER TABLE "public"."tcode_device" DROP CONSTRAINT "tcode_device_did_mcode_tenant_id_key";

ALTER TABLE "public"."tcode_device" ADD CONSTRAINT "tcode_device_mcode_tenant_id_did_key" UNIQUE ("mcode", "tenant_id", "did");

ALTER TABLE "public"."tcode_group" ADD CONSTRAINT "tcode_group_pkey" PRIMARY KEY ("tid", "code");

CREATE TABLE "public"."tcode_group_electric" (
  "tid" uuid NOT NULL,
  "code" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
  "arr" time(6),
  "dep" time(6),
  "tdays" int4,
  CONSTRAINT "tcode_group_electric_pkey" PRIMARY KEY ("tid", "code")
)
;

ALTER TABLE "public"."tcode_group_electric" OWNER TO "postgres";

COMMENT ON COLUMN "public"."tcode_group_electric"."tid" IS '租户ID';

COMMENT ON COLUMN "public"."tcode_group_electric"."code" IS '峰电peak，谷电valley';

COMMENT ON COLUMN "public"."tcode_group_electric"."arr" IS '开始时间';

COMMENT ON COLUMN "public"."tcode_group_electric"."dep" IS '结束时间';

COMMENT ON COLUMN "public"."tcode_group_electric"."tdays" IS '是否跨日，1为跨日';

ALTER TABLE "public"."tcode_model" ADD COLUMN "credential" varchar(255) COLLATE "pg_catalog"."default";

COMMENT ON COLUMN "public"."tcode_model"."credential" IS '网关令牌';

ALTER TABLE "public"."tcode_model_detail" ADD COLUMN "credential" varchar(255) COLLATE "pg_catalog"."default";

COMMENT ON COLUMN "public"."tcode_model_detail"."credential" IS '网关令牌';

ALTER TABLE "public"."tcode_model_detail_ex" ALTER COLUMN "gid" SET NOT NULL;

ALTER TABLE "public"."tcode_model_detail_ex" ADD CONSTRAINT "tcode_model_detail_ex_pkey" PRIMARY KEY ("tid", "gid", "code", "key");

CREATE TABLE "public"."tcode_model_ex" (
  "tid" uuid NOT NULL,
  "gid" uuid,
  "code" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
  "name" varchar(255) COLLATE "pg_catalog"."default",
  "ctime" timestamp(6) DEFAULT CURRENT_TIMESTAMP,
  "mtime" timestamp(6) DEFAULT CURRENT_TIMESTAMP
)
;

ALTER TABLE "public"."tcode_model_ex" OWNER TO "postgres";

ALTER TABLE "public"."trep_dkey_detail" DROP CONSTRAINT "trep_dkey_detail_pkey";

COMMENT ON COLUMN "public"."trep_dkey_detail"."tid" IS '租户ID';

COMMENT ON COLUMN "public"."trep_dkey_detail"."did" IS '设备ID';

COMMENT ON COLUMN "public"."trep_dkey_detail"."key" IS '属性';

ALTER TABLE "public"."trep_dkey_detail" ALTER COLUMN "bdate" SET NOT NULL;

COMMENT ON COLUMN "public"."trep_dkey_detail"."bdate" IS '营业日期';

COMMENT ON COLUMN "public"."trep_dkey_detail"."val" IS '最新遥测值-废弃';

COMMENT ON COLUMN "public"."trep_dkey_detail"."ctime" IS '创建时间';

COMMENT ON COLUMN "public"."trep_dkey_detail"."mtime" IS '修改时间';

COMMENT ON COLUMN "public"."trep_dkey_detail"."val_arr" IS '遥测值开始';

COMMENT ON COLUMN "public"."trep_dkey_detail"."val_dep" IS '遥测值结束';

COMMENT ON COLUMN "public"."trep_dkey_detail"."peak" IS '峰电-电能属性有效';

COMMENT ON COLUMN "public"."trep_dkey_detail"."valley" IS '谷电-电能属性有效';

ALTER TABLE "public"."trep_dkey_detail" ADD CONSTRAINT "trep_dkey_detail_pkey" PRIMARY KEY ("tid", "did", "key", "bdate");

COMMENT ON COLUMN "public"."trep_hkey_detail"."peak" IS '峰电';

COMMENT ON COLUMN "public"."trep_hkey_detail"."valley" IS '谷电';

ALTER TABLE "public"."trep_hsta_detail" ALTER COLUMN "aid" SET DEFAULT nextval('trep_sta_hdetail_aid_seq'::regclass);

CREATE TABLE "public"."trep_htotal_detail" (
  "id" uuid NOT NULL,
  "tid" uuid NOT NULL,
  "did" uuid NOT NULL,
  "key" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
  "bdate" date DEFAULT date(now()),
  "val_arr" varchar(255) COLLATE "pg_catalog"."default",
  "val_dep" varchar(255) COLLATE "pg_catalog"."default",
  "ctime" timestamp(6) DEFAULT now(),
  "mtime" timestamp(6) DEFAULT now(),
  CONSTRAINT "trep_htotal_detail_pkey" PRIMARY KEY ("id")
)
;

ALTER TABLE "public"."trep_htotal_detail" OWNER TO "postgres";

COMMENT ON COLUMN "public"."trep_htotal_detail"."id" IS '主键ID';

COMMENT ON COLUMN "public"."trep_htotal_detail"."tid" IS '租户id';

COMMENT ON COLUMN "public"."trep_htotal_detail"."did" IS '设备id';

COMMENT ON COLUMN "public"."trep_htotal_detail"."key" IS '属性key';

COMMENT ON COLUMN "public"."trep_htotal_detail"."bdate" IS '营业日期';

COMMENT ON COLUMN "public"."trep_htotal_detail"."val_arr" IS '首次统计属性值';

COMMENT ON COLUMN "public"."trep_htotal_detail"."val_dep" IS '最后统计属性值';

COMMENT ON COLUMN "public"."trep_htotal_detail"."ctime" IS '创建时间';

COMMENT ON COLUMN "public"."trep_htotal_detail"."mtime" IS '修改时间';

CREATE TABLE "public"."trep_total_detail" (
  "tid" uuid NOT NULL,
  "did" uuid NOT NULL,
  "key" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
  "val_arr" varchar(255) COLLATE "pg_catalog"."default",
  "val_dep" varchar(255) COLLATE "pg_catalog"."default",
  "ctime" timestamp(6) DEFAULT now(),
  "mtime" timestamp(6) DEFAULT now(),
  CONSTRAINT "trep_total_detail_pkey" PRIMARY KEY ("tid", "did", "key")
)
;

ALTER TABLE "public"."trep_total_detail" OWNER TO "postgres";

COMMENT ON COLUMN "public"."trep_total_detail"."tid" IS '租户id';

COMMENT ON COLUMN "public"."trep_total_detail"."did" IS '设备id';

COMMENT ON COLUMN "public"."trep_total_detail"."key" IS '属性key';

COMMENT ON COLUMN "public"."trep_total_detail"."val_arr" IS '首次统计属性值';

COMMENT ON COLUMN "public"."trep_total_detail"."val_dep" IS '最后统计属性值';

COMMENT ON COLUMN "public"."trep_total_detail"."ctime" IS '创建时间';

COMMENT ON COLUMN "public"."trep_total_detail"."mtime" IS '修改时间';

CREATE TABLE "public"."trep_yield_detail" (
  "tid" uuid NOT NULL,
  "did" uuid NOT NULL,
  "bdate" date NOT NULL,
  "key" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
  "key_yield" varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
  "val_arr" varchar(255) COLLATE "pg_catalog"."default",
  "val_dep" varchar(255) COLLATE "pg_catalog"."default",
  "val_yield" varchar(255) COLLATE "pg_catalog"."default",
  "ctime" timestamp(6),
  "mtime" timestamp(6),
  CONSTRAINT "trep_yield_detail_pkey" PRIMARY KEY ("tid", "did", "bdate", "key", "key_yield")
)
;

ALTER TABLE "public"."trep_yield_detail" OWNER TO "postgres";

COMMENT ON COLUMN "public"."trep_yield_detail"."tid" IS '租户ID';

COMMENT ON COLUMN "public"."trep_yield_detail"."did" IS '设备ID';

COMMENT ON COLUMN "public"."trep_yield_detail"."bdate" IS '营业日期';

COMMENT ON COLUMN "public"."trep_yield_detail"."key" IS '能耗属性名称key';

COMMENT ON COLUMN "public"."trep_yield_detail"."key_yield" IS '对应产量key';

COMMENT ON COLUMN "public"."trep_yield_detail"."val_arr" IS '开始值';

COMMENT ON COLUMN "public"."trep_yield_detail"."val_dep" IS '能耗结束值';

COMMENT ON COLUMN "public"."trep_yield_detail"."val_yield" IS '产量数据';

COMMENT ON COLUMN "public"."trep_yield_detail"."ctime" IS '开始时间';

COMMENT ON COLUMN "public"."trep_yield_detail"."mtime" IS '结束时间';

DROP FUNCTION "public"."trep_group_detail_function"();

CREATE OR REPLACE FUNCTION "public"."trep_group_detail_function"()
  RETURNS "pg_catalog"."int4" AS $BODY$  
declare  
    total integer;  
BEGIN  
   INSERT into trep_group_detail(tid,did,key,bdate,grpcode,val_arr,val_dep,ctime,mtime)
		SELECT t.tid,t.did, t.key,c.tarr::date as bdate,
		c.code,t.val ,t.val,
		CURRENT_TIMESTAMP ,CURRENT_TIMESTAMP
		from vcode_key_detail t,tcode_model_detail_ex b ,(
			SELECT a.* from
			(
				SELECT CONCAT((CURRENT_DATE)::TEXT,' ',t.arr)::TIMESTAMP as tarr,
				CONCAT((CURRENT_DATE)::TEXT,' ',t.dep)::TIMESTAMP as tdep,
				t.* from tcode_group t
				where t.dep >= t.arr
				union
				SELECT CONCAT((CURRENT_DATE)::TEXT,' ',t.arr)::TIMESTAMP as tarr,
				CONCAT((CURRENT_DATE+1)::TEXT,' ',t.dep)::TIMESTAMP as tdep,
				t.* from tcode_group t
				where t.dep < t.arr
				union
				SELECT CONCAT((CURRENT_DATE-1)::TEXT,' ',t.arr)::TIMESTAMP as tarr,
				CONCAT((CURRENT_DATE)::TEXT,' ',t.dep)::TIMESTAMP as tdep,
				t.* from tcode_group t
				where t.dep < t.arr
			) a
			where CURRENT_TIMESTAMP >= a.tarr and
			CURRENT_TIMESTAMP <= a.tdep
			limit 1
		) c
		where t.tid = b.tid and t.tid = c.tid and b.code = 'all' and t.key = b.key and substr(b.ex_s1,3,1) = '1'
		ON CONFLICT (tid,did,key,bdate,grpcode)
		DO UPDATE SET
		val_dep = EXCLUDED.val_dep,
		mtime = CURRENT_TIMESTAMP;
 
   RETURN total;  
END;  
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

ALTER FUNCTION "public"."trep_group_detail_function"() OWNER TO "postgres";