USE [master]
GO
/****** Object:  Database [PHP_Trend]    Script Date: 2023/1/7 下午 10:47:51 ******/
CREATE DATABASE [PHP_Trend]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'PHP_Trend', FILENAME = N'D:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA\PHP_Trend.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
 LOG ON 
( NAME = N'PHP_Trend_log', FILENAME = N'D:\Program Files\Microsoft SQL Server\MSSQL14.SQLEXPRESS\MSSQL\DATA\PHP_Trend_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )
GO
ALTER DATABASE [PHP_Trend] SET COMPATIBILITY_LEVEL = 140
GO
ALTER DATABASE [PHP_Trend] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [PHP_Trend] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [PHP_Trend] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [PHP_Trend] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [PHP_Trend] SET ARITHABORT OFF 
GO
ALTER DATABASE [PHP_Trend] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [PHP_Trend] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [PHP_Trend] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [PHP_Trend] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [PHP_Trend] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [PHP_Trend] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [PHP_Trend] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [PHP_Trend] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [PHP_Trend] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [PHP_Trend] SET  DISABLE_BROKER 
GO
ALTER DATABASE [PHP_Trend] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [PHP_Trend] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [PHP_Trend] SET TRUSTWORTHY OFF 
GO
ALTER DATABASE [PHP_Trend] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO
ALTER DATABASE [PHP_Trend] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [PHP_Trend] SET READ_COMMITTED_SNAPSHOT OFF 
GO
ALTER DATABASE [PHP_Trend] SET HONOR_BROKER_PRIORITY OFF 
GO
ALTER DATABASE [PHP_Trend] SET RECOVERY SIMPLE 
GO
ALTER DATABASE [PHP_Trend] SET  MULTI_USER 
GO
ALTER DATABASE [PHP_Trend] SET PAGE_VERIFY CHECKSUM  
GO
ALTER DATABASE [PHP_Trend] SET DB_CHAINING OFF 
GO
ALTER DATABASE [PHP_Trend] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) 
GO
ALTER DATABASE [PHP_Trend] SET TARGET_RECOVERY_TIME = 60 SECONDS 
GO
ALTER DATABASE [PHP_Trend] SET DELAYED_DURABILITY = DISABLED 
GO
ALTER DATABASE [PHP_Trend] SET QUERY_STORE = OFF
GO
USE [PHP_Trend]
GO
/****** Object:  Table [dbo].[users]    Script Date: 2023/1/7 下午 10:47:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[roles](
	[role_id] [varchar](50) NOT NULL,
	[role_name] [nvarchar](200) NOT NULL,
	[enabled] [bit] NOT NULL,
 CONSTRAINT [PK_roles] PRIMARY KEY CLUSTERED 
(
	[role_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[roles_users]    Script Date: 2023/1/7 下午 10:47:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[roles_users](
	[role_id] [varchar](50) NOT NULL,
	[user_id] [varchar](50) NOT NULL,
 CONSTRAINT [PK_roles_users] PRIMARY KEY CLUSTERED 
(
	[role_id] ASC,
	[user_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  View [dbo].[v_roles_users]    Script Date: 2023/1/7 下午 10:47:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Station_Spec](
	[STN] [int] NULL,
	[Name] [nvarchar](255) NULL,
	[City] [nvarchar](255) NULL,
	[City_Name] [nvarchar](255) NULL,
	[Section_Name] [nvarchar](255) NULL,
	[Section] [nvarchar](255) NULL,
	[Area_Name] [nvarchar](255) NULL,
	[Area] [nvarchar](255) NULL,
	[Capacity] [float] NULL,
	[Inverter] [nvarchar](255) NULL,
	[Device_Def] [nvarchar](255) NULL,
	[Weather_Code] [nvarchar](255) NULL,
	[Model] [nvarchar](255) NULL,
	[Module_No] [nvarchar](255) NULL,
	[Title_PNG] [nvarchar](255) NULL,
	[Tag_PNG] [nvarchar](255) NULL,
	[Area_Main_PNG] [nvarchar](255) NULL,
	[NO] [nvarchar](255) NULL,
	[Customer_Code] [float] NULL,
	[Order_Number] [float] NULL,
	[PPA_contracts] [nvarchar](255) NULL,
	[Unit_Price] [float] NULL,
	[Address] [nvarchar](255) NULL,
	[Record_Date] [float] NULL,
	[Price_Rate] [float] NULL,
	[Name1] [nvarchar](128) NULL,
	[Note] [nvarchar](max) NULL,
	[Insert_day] [date] NULL,
	[STN_2] [nvarchar](100) NULL,
	[lng] [varchar](50) NULL,
	[lat] [varchar](50) NULL,
	[Online] [int] NULL,
	[BrokenLine] [int] NULL,
	[BrokenTime] [datetime] NULL,
	[BrokenInv] [varchar](500) NULL,
	[ACMeter] [bit] NULL,
	[ACMeterCount] [int] NULL,
	[Solar_Version] [varchar](50) NULL,
	[Sales_Rep] [varchar](50) NULL,
	[Special_Station] [varchar](50) NULL,
	[SimCard] [nvarchar](50) NULL,
	[Remark] [nvarchar](250) NULL,
	[RecalculateBy] [tinyint] NOT NULL,
	[IsV2] [bit] NOT NULL,
	[LatestCmbTime] [datetime] NULL,
	[INV_PR_Limit] [float] NULL,
	[MPPT_RA_Limit] [float] NULL,
	[INV_PR_Limit_Days] [int] NULL,
	[MPPT_RA_Limit_Days] [int] NULL,
	[AI_Solar_Panel_Clear_Days] [int] NULL,
	[INV_PR_Device_Limit] [float] NULL,
	[AttentionItems] [nvarchar](max) NULL,
	[STN_Order] [int] NOT NULL,
	[Al_INV] [bit] NOT NULL,
	[Al_SunshineTemp] [bit] NOT NULL,
	[Al_Protected] [bit] NOT NULL,
	[Al_Factory] [bit] NOT NULL,
	[Al_DAB] [bit] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
/****** Object:  Table [dbo].[roles_station_spec]    Script Date: 2023/1/7 下午 10:47:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[roles_station_spec](
	[role_id] [varchar](50) NOT NULL,
	[STN] [int] NOT NULL,
 CONSTRAINT [PK_roles_station_spec] PRIMARY KEY CLUSTERED 
(
	[role_id] ASC,
	[STN] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  View [dbo].[v_roles_stations]    Script Date: 2023/1/7 下午 10:47:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_roles_stations]
AS
SELECT          dbo.roles.*, dbo.Station_Spec.*
FROM              dbo.roles INNER JOIN
                            dbo.roles_station_spec ON dbo.roles.role_id = dbo.roles_station_spec.role_id INNER JOIN
                            dbo.Station_Spec ON dbo.roles_station_spec.STN = dbo.Station_Spec.STN
WHERE          (dbo.Station_Spec.Online = 1) AND (dbo.roles.enabled = 1)
UNION
SELECT          dbo.roles.*, dbo.Station_Spec.*
FROM              dbo.roles CROSS JOIN
                            dbo.Station_Spec
WHERE          (dbo.Station_Spec.Online = 1) AND (dbo.roles.enabled = 1) AND (role_id in('admin','all_stn'))
GO
/****** Object:  Table [dbo].[roles_menu]    Script Date: 2023/1/7 下午 10:47:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[roles_menu](
	[role_id] [varchar](50) NOT NULL,
	[menu_id] [varchar](50) NOT NULL,
 CONSTRAINT [PK_roles_menu] PRIMARY KEY CLUSTERED 
(
	[role_id] ASC,
	[menu_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[menu]    Script Date: 2023/1/7 下午 10:47:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[menu](
	[menu_id] [varchar](30) NOT NULL,
	[menu_name] [nvarchar](50) NOT NULL,
	[enabled] [bit] NOT NULL,
 CONSTRAINT [PK_menu] PRIMARY KEY CLUSTERED 
(
	[menu_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  View [dbo].[v_roles_menu]    Script Date: 2023/1/7 下午 10:47:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_roles_menu]
AS
SELECT          dbo.roles.role_id, dbo.roles.role_name, dbo.menu.menu_id, dbo.menu.menu_name
FROM              dbo.roles INNER JOIN
                            dbo.roles_menu ON dbo.roles.role_id = dbo.roles_menu.role_id INNER JOIN
                            dbo.menu ON dbo.roles_menu.menu_id = dbo.menu.menu_id
WHERE          (dbo.menu.enabled = 1)
UNION
SELECT          dbo.roles.role_id, dbo.roles.role_name, dbo.menu.menu_id, dbo.menu.menu_name
FROM              dbo.roles CROSS JOIN
                            dbo.menu
WHERE          (dbo.menu.enabled = 1) AND (role_id IN ('admin', 'all_menu'))
GO
/****** Object:  View [dbo].[v_roles_users_menu]    Script Date: 2023/1/7 下午 10:47:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_roles_users_menu]
AS
SELECT          dbo.v_roles_menu.menu_id, dbo.v_roles_users.*
FROM              dbo.v_roles_menu INNER JOIN
                            dbo.v_roles_users ON dbo.v_roles_menu.role_id = dbo.v_roles_users.role_id
GO
/****** Object:  View [dbo].[v_roles_users_stations]    Script Date: 2023/1/7 下午 10:47:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_roles_users_stations]
AS
SELECT          dbo.v_roles_stations.*, dbo.v_roles_users.user_id, dbo.v_roles_users.user_name
FROM              dbo.v_roles_stations INNER JOIN
                            dbo.v_roles_users ON dbo.v_roles_stations.role_id = dbo.v_roles_users.role_id
GO
INSERT [dbo].[menu] ([menu_id], [menu_name], [enabled]) VALUES (N'b', N'b', 1)
INSERT [dbo].[menu] ([menu_id], [menu_name], [enabled]) VALUES (N'index', N'INDEX', 1)
INSERT [dbo].[menu] ([menu_id], [menu_name], [enabled]) VALUES (N'test', N'test', 1)
GO
INSERT [dbo].[roles] ([role_id], [role_name], [enabled]) VALUES (N'admin', N'Admin', 1)
INSERT [dbo].[roles] ([role_id], [role_name], [enabled]) VALUES (N'all_menu', N'All Menu', 1)
INSERT [dbo].[roles] ([role_id], [role_name], [enabled]) VALUES (N'all_stn', N'All Station', 1)
INSERT [dbo].[roles] ([role_id], [role_name], [enabled]) VALUES (N'user', N'User', 1)
GO
INSERT [dbo].[roles_station_spec] ([role_id], [STN]) VALUES (N'user', 1)
GO
INSERT [dbo].[roles_users] ([role_id], [user_id]) VALUES (N'admin', N'sunware')
INSERT [dbo].[roles_users] ([role_id], [user_id]) VALUES (N'all_menu', N'all')
INSERT [dbo].[roles_users] ([role_id], [user_id]) VALUES (N'all_stn', N'all')
GO
INSERT [dbo].[Station_Spec] ([STN], [Name], [City], [City_Name], [Section_Name], [Section], [Area_Name], [Area], [Capacity], [Inverter], [Device_Def], [Weather_Code], [Model], [Module_No], [Title_PNG], [Tag_PNG], [Area_Main_PNG], [NO], [Customer_Code], [Order_Number], [PPA_contracts], [Unit_Price], [Address], [Record_Date], [Price_Rate], [Name1], [Note], [Insert_day], [STN_2], [lng], [lat], [Online], [BrokenLine], [BrokenTime], [BrokenInv], [ACMeter], [ACMeterCount], [Solar_Version], [Sales_Rep], [Special_Station], [SimCard], [Remark], [RecalculateBy], [IsV2], [LatestCmbTime], [INV_PR_Limit], [MPPT_RA_Limit], [INV_PR_Limit_Days], [MPPT_RA_Limit_Days], [AI_Solar_Panel_Clear_Days], [INV_PR_Device_Limit], [AttentionItems], [STN_Order], [Al_INV], [Al_SunshineTemp], [Al_Protected], [Al_Factory], [Al_DAB]) VALUES (1, N'TEST1', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, 1, NULL, 0.8, 0.8, 0, 0, 0, 1, NULL, 0, 0, 0, 0, 0, 0)
INSERT [dbo].[Station_Spec] ([STN], [Name], [City], [City_Name], [Section_Name], [Section], [Area_Name], [Area], [Capacity], [Inverter], [Device_Def], [Weather_Code], [Model], [Module_No], [Title_PNG], [Tag_PNG], [Area_Main_PNG], [NO], [Customer_Code], [Order_Number], [PPA_contracts], [Unit_Price], [Address], [Record_Date], [Price_Rate], [Name1], [Note], [Insert_day], [STN_2], [lng], [lat], [Online], [BrokenLine], [BrokenTime], [BrokenInv], [ACMeter], [ACMeterCount], [Solar_Version], [Sales_Rep], [Special_Station], [SimCard], [Remark], [RecalculateBy], [IsV2], [LatestCmbTime], [INV_PR_Limit], [MPPT_RA_Limit], [INV_PR_Limit_Days], [MPPT_RA_Limit_Days], [AI_Solar_Panel_Clear_Days], [INV_PR_Device_Limit], [AttentionItems], [STN_Order], [Al_INV], [Al_SunshineTemp], [Al_Protected], [Al_Factory], [Al_DAB]) VALUES (2, N'TEST2', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1, 1, NULL, 0.8, 0.8, 0, 0, 0, 1, NULL, 0, 0, 0, 0, 0, 0)
GO
ALTER TABLE [dbo].[menu] ADD  CONSTRAINT [DF_menu_enabled]  DEFAULT ((1)) FOR [enabled]
GO
ALTER TABLE [dbo].[roles] ADD  CONSTRAINT [DF_roles_enabled]  DEFAULT ((1)) FOR [enabled]
GO
ALTER TABLE [dbo].[Station_Spec] ADD  CONSTRAINT [DF_Station_Spec_RecalculateBy]  DEFAULT ((1)) FOR [RecalculateBy]
GO
ALTER TABLE [dbo].[Station_Spec] ADD  CONSTRAINT [DF_Station_Spec_IsV2]  DEFAULT ((1)) FOR [IsV2]
GO
ALTER TABLE [dbo].[Station_Spec] ADD  CONSTRAINT [DF_Station_Spec_INV_PR_Limit]  DEFAULT ((0.8)) FOR [INV_PR_Limit]
GO
ALTER TABLE [dbo].[Station_Spec] ADD  CONSTRAINT [DF_Station_Spec_MPPT_RA_Limit]  DEFAULT ((0.8)) FOR [MPPT_RA_Limit]
GO
ALTER TABLE [dbo].[Station_Spec] ADD  CONSTRAINT [DF_Station_Spec_INV_PR_Limit_Days]  DEFAULT ((0)) FOR [INV_PR_Limit_Days]
GO
ALTER TABLE [dbo].[Station_Spec] ADD  CONSTRAINT [DF_Station_Spec_MPPT_RA_Limit_Days]  DEFAULT ((0)) FOR [MPPT_RA_Limit_Days]
GO
ALTER TABLE [dbo].[Station_Spec] ADD  CONSTRAINT [DF_Station_Spec_AI_Solar_Panel_Clear_Days]  DEFAULT ((0)) FOR [AI_Solar_Panel_Clear_Days]
GO
ALTER TABLE [dbo].[Station_Spec] ADD  CONSTRAINT [DF_Station_Spec_AI_Solar_Panel_Clear_Device_Limit]  DEFAULT ((1)) FOR [INV_PR_Device_Limit]
GO
ALTER TABLE [dbo].[Station_Spec] ADD  CONSTRAINT [DF_Station_Spec_STN_Order]  DEFAULT ((0)) FOR [STN_Order]
GO
ALTER TABLE [dbo].[Station_Spec] ADD  CONSTRAINT [DF_Station_Spec_Al_INV]  DEFAULT ((0)) FOR [Al_INV]
GO
ALTER TABLE [dbo].[Station_Spec] ADD  CONSTRAINT [DF_Station_Spec_Al_SunshineTemp]  DEFAULT ((0)) FOR [Al_SunshineTemp]
GO
ALTER TABLE [dbo].[Station_Spec] ADD  CONSTRAINT [DF_Station_Spec_Al_Protected]  DEFAULT ((0)) FOR [Al_Protected]
GO
ALTER TABLE [dbo].[Station_Spec] ADD  CONSTRAINT [DF_Station_Spec_Al_Factory]  DEFAULT ((0)) FOR [Al_Factory]
GO
ALTER TABLE [dbo].[Station_Spec] ADD  CONSTRAINT [DF_Station_Spec_Al_Dab]  DEFAULT ((0)) FOR [Al_DAB]
GO
USE [master]
GO
ALTER DATABASE [PHP_Trend] SET  READ_WRITE 
GO
