CREATE trigger [dbo].[T_Update_Member_Finance] ON [dbo].AgencyDrawing 
for Update
as
begin
	declare @status int
	declare @newStatus int
	declare @drawFinance decimal(18,2)
	declare @memberID int
	select @status=Status,@drawFinance=DrawFinance,@memberID=MemberID from deleted
	select @newStatus=Status from inserted
	if(@status=0 and @newStatus=1)
		Update Member set Finance = Finance - @drawFinance Where MemberID = @memberID
end

GO

CREATE trigger [dbo].[T_Delete_Agent] ON [dbo].Agent
for Delete
as
begin
	declare @agentID int
	select @agentID=AgentID from deleted
	Delete Agent Where ParentAgent = @agentID
end

GO

CREATE trigger [dbo].[T_Update_Agent_Status] ON [dbo].Agent
for Update
as
begin
	declare @status int
	declare @newStatus int
	declare @agentID int
	select @status=Status,@agentID=AgentID from deleted
	select @newStatus=Status from inserted
	if(@status=1 and @newStatus=0)
		Update Agent set Status = 0 Where ParentAgent = @agentID
end

GO

CREATE trigger [dbo].[T_Update_Product] ON [dbo].Brand
for delete
as
begin	
	declare @brandID int
	select @brandID=BrandID from deleted
	UPDATE Product SET Brand = '0' WHERE Brand = @brandID
end

GO

CREATE trigger [dbo].[T_Update_CatProduct_CatBrand] ON [dbo].Category
instead of delete
as
begin	
	declare @categoryID int
	select @categoryID=CategoryID from deleted
	--这个是关联商品
	update Product set Status = -1 Where ProductID in (select ProductID from CatProduct where CategoryID = @categoryID and ProductID in (select ProductID from CatProduct group by ProductID having count(*)=1))
	delete from CatProduct Where CategoryID = @categoryID
	--这个是关联品牌
	delete from Brand Where BrandID in (select BrandID from CatBrand where CategoryID = @categoryID and BrandID in (select BrandID from CatBrand group by BrandID having count(*)=1))
	delete from CatBrand Where CategoryID = @categoryID
	delete from Category Where CategoryID = @categoryID
end

GO

CREATE trigger [dbo].[NewProductDelete] ON [dbo].NewProduct 
for delete
as
begin
	declare @type int
	select @type=type from deleted
	if(@type=1)
	delete from NewBid
	where NewProductID in
	(select NewProductID from deleted)
	if(@type=2)
	delete from NewBuy
	where NewProductID in
	(select NewProductID from deleted)
end

GO

CREATE trigger [dbo].[T_Delete_CatProduct] ON [dbo].Product 
for update
as
begin
	declare @productID int
	declare @status int
	select @productID=ProductID,@status=Status from inserted
	if(@status=-1)
	delete from CatProduct Where ProductID = @productID
end

GO

CREATE trigger [dbo].[T_Update_Product_Storage] ON [dbo].Storage 
for insert
as
begin
	declare @amount int
	declare @productNo varchar(50)
	declare @Warehousing int
	select @amount=Amount from inserted
	select @productNo=ProductNo from inserted
	select @Warehousing=Warehousing from inserted
	if(@Warehousing = 1)
	update Product set Storage = Storage + @amount Where ProductNo = @productNo
end

GO

CREATE trigger [dbo].[T_Update_Product_StorageCheck] ON [dbo].StorageCheck
for insert
as
begin
	declare @amount int
	declare @productNo varchar(50)
	declare @Warehousing int
	select @amount=Amount from inserted
	select @productNo=ProductNo from inserted
	select @Warehousing=Warehousing from inserted
	update Product set Storage = Storage + (@Warehousing * @amount) Where ProductNo = @productNo
end