Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. CREATE proc [dbo].[BusinessGetByParams]
  2. @Id int,
  3. @UserId nvarchar(128),
  4. @SearchTerm varchar(50),
  5.  
  6. @NorthEastLong numeric(12,9),
  7. @NorthEastLat numeric(12,9),
  8.  
  9. @SouthWestLong numeric(12,9),
  10. @SouthWestLat numeric(12,9),
  11.  
  12. @PinnedByUser bit,
  13. @PageNumber int = 1,
  14. @PageSize int = 20,
  15. @Categories nvarchar(100),
  16. @WhiskeyId int
  17. as
  18. begin
  19. set @SearchTerm = lower(replace(@SearchTerm, ' ', ''))
  20.  
  21. declare @offset int = (@PageNumber - 1) * @PageSize;
  22. declare @date date = getdate();
  23.  
  24. set @Categories = isnull(@Categories, '');
  25.  
  26. with Categories(CategoryId)
  27. as
  28. (
  29. select item CategoryId from dbo.split(@Categories,'|')
  30. )
  31. select COUNT(*) OVER() TotalRows,
  32. @PageNumber PageNumber,
  33. CEILING((COUNT(*) OVER()) / CAST(@PageSize AS float)) TotalPages,
  34.  
  35. b.[Id]
  36. ,b.ImagePath
  37. ,[SubscriptionId]
  38. ,b.UserId
  39. ,[BusinessName]
  40. ,BusinessDescription
  41. ,BusinessInformation
  42. ,[FullAddress]
  43. ,b.[Phone]
  44. ,b.[CreatedDate]
  45. ,b.[IsActive]
  46. ,b.IsPublished
  47. ,[GeoLat]
  48. ,[GeoLong]
  49. ,AditionalInfoJson
  50. ,aspnetu.Email
  51. ,case when ubp.Id is null then 0 else 1 end IsPinned
  52.  
  53. ,punchCard.PunchCardQty
  54. ,offer.OfferQty
  55. ,timeline.MessageQty
  56. ,bc.CategoryId
  57. ,c.CategoryName Category
  58. ,u.Id ConsumerId
  59. FROM [dbo].[Business] b
  60. join whiskey.Business wb
  61. on wb.BusinessId = b.Id
  62. left join [dbo].[AspNetUsers] aspnetu
  63. on aspnetu.Id = @UserId
  64. left join [dbo].[User] u
  65. on u.IdentityUserId = aspnetu.Id
  66. left join [dbo].[UserBusinessPin] ubp
  67. on ubp.BusinessId = b.Id
  68. and ubp.UserId = u.Id
  69. left join [dbo].BusinessCategory bc
  70. on bc.BusinessId = b.Id
  71. left join [dbo].Category c
  72. on c.Id = bc.CategoryId
  73. outer apply (
  74. select count(*) PunchCardQty
  75. from [dbo].[PunchCard]
  76. where BusinessId = b.Id
  77. and IsActive = 1
  78. and IsPublished = 1
  79. and (
  80. CONVERT(VARCHAR(10), ExpirationDate, 103) >= CONVERT(VARCHAR(10), GETDATE(), 103)
  81. or
  82. ExpirationDate is null
  83. )
  84. ) punchCard
  85.  
  86. outer apply (
  87. select count(*) OfferQty
  88. from [dbo].Offer
  89. where BusinessId = b.Id
  90. and IsActive = 1
  91. and (
  92. @date between AvailableFrom and AvailableTo
  93. )
  94. ) offer
  95.  
  96. outer apply (
  97. select count(*) MessageQty
  98. from [dbo].BusinessMessage
  99. where BusinessId = b.Id
  100. ) timeline
  101.  
  102. where (b.Id = @Id or @Id is null)
  103. --and (
  104. -- replace(
  105. -- lower(
  106. -- isnull(b.BusinessName, '')
  107. -- ),
  108. -- ' ', '')
  109.  
  110. -- like '%' + replace(@SearchTerm, ' ','') + '%'
  111.  
  112. -- or
  113.  
  114. -- @SearchTerm is null
  115. -- )
  116. --and (@Categories = '' or bc.CategoryId in (select CategoryId from Categories))
  117. --and (
  118. -- (ubp.Id is not null and @PinnedByUser = 1)
  119. -- or
  120. -- (@PinnedByUser is null)
  121. -- )
  122. --and (
  123. -- (
  124. -- [GeoLat] <= @NorthEastLat and [GeoLat] >= @SouthWestLat
  125. -- and
  126. -- [GeoLong] <= @NorthEastLong and [GeoLong] >= @SouthWestLong
  127. -- )
  128. -- or
  129. -- (
  130. -- @NorthEastLat = 0 or @SouthWestLat = 0
  131. -- or
  132. -- @NorthEastLong = 0 or @SouthWestLong = 0
  133. -- )
  134. -- )
  135.  
  136. and b.IsActive = 1
  137. and b.IsPublished = 1
  138. and wb.MainId = @WhiskeyId
  139. --and wb.IsMainBusiness = 0
  140. order by b.BusinessName
  141.  
  142. OFFSET @offset rows fetch next @PageSize rows ONLY
  143. end
  144. GO
  145.  
  146. CREATE proc [dbo].[BusinessGetByUserId]
  147. @UserId nvarchar(128)
  148. as
  149. begin
  150. SELECT b.Id
  151. ,null SubscriptionId
  152. --,isnull(s.SubscriptionName, 'Unsubscribed') SubscriptionName
  153. , null SubscriptionName
  154. ,bu.UserId
  155. ,b.ImagePath
  156. ,BusinessName
  157. ,BusinessDescription
  158. ,BusinessInformation
  159. ,FullAddress
  160. ,Phone
  161. ,b.CreatedDate
  162. ,b.IsActive
  163. ,b.IsPublished
  164. ,GeoLat
  165. ,GeoLong
  166. ,AditionalInfoJson
  167. ,anu.Email
  168. from dbo.Business b
  169. join dbo.BusinessUser bu
  170. on bu.BusinessId = b.Id
  171. join dbo.[User] u
  172. on u.Id = bu.UserId
  173. join dbo.aspNetUsers anu
  174. on anu.id = u.identityUserId
  175. --left join dbo.Subscription s
  176. -- on s.Id = b.SubscriptionId
  177. where u.identityUserId = @UserId
  178. end
  179. GO
  180.  
  181. CREATE proc [dbo].[BusinessUpdate]
  182. @BusinessId int,
  183. @UserId nvarchar(128),
  184. @BusinessName nvarchar(150),
  185. @BusinessDescription nvarchar(1000),
  186. @BusinessInformation nvarchar(max),
  187. @FullAddress nvarchar(500),
  188. @Phone nvarchar(15),
  189. @GeoLat decimal(12,9),
  190. @GeoLong decimal(12,9),
  191. @IsPublished bit,
  192. @AditionalInfoJSON nvarchar(max)
  193. as
  194. begin
  195.  
  196. --declare @businessId int
  197.  
  198. --select @businessId = b.Id
  199. --from dbo.Business b
  200. --join dbo.[User] u
  201. -- on u.Id = b.UserId
  202. --where u.IdentityUserId = @UserId
  203. --declare @canBePublished bit = (select cast(case when (@BusinessName is not null
  204. -- and @BusinessDescription is not null
  205. -- and @GeoLat is not null
  206. -- and @GeoLat is not null
  207. -- and ImagePath is not null)
  208. -- then 1 else 0 end as bit)
  209. -- from dbo.Business
  210. -- where Id = @BusinessId)
  211.  
  212. UPDATE [dbo].[Business]
  213. SET [BusinessName] = @BusinessName,
  214. [BusinessDescription] = @BusinessDescription,
  215. [BusinessInformation] = @BusinessInformation,
  216. [FullAddress] = @FullAddress,
  217. [Phone] = @Phone,
  218. [GeoLat] = @GeoLat,
  219. [GeoLong] = @GeoLong,
  220. [IsPublished] = @IsPublished,
  221. [AditionalInfoJson] = @AditionalInfoJSON
  222. WHERE Id = @businessId
  223.  
  224. exec dbo.BusinessGetByUserId @UserId
  225.  
  226. end
  227. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement