Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlClient;
  4. using System.Dynamic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Transactions;
  8. using App.Common.Helpers.DB;
  9. using App.Common.Helpers.Instance;
  10. using App.Domain.DataRepository.Base;
  11. using App.Domain.DataRepositoryContracts;
  12. using App.Domain.Entities.Common;
  13. using App.Domain.Entities.Products;
  14. using App.Domain.Entities.Products.Dto;
  15. using App.Domain.Entities.Shopify;
  16. using Dapper;
  17. using Signals.Aspects.DI.Attributes;
  18. using Signals.Core.Processing.Results;
  19. using SqlBulkTools;
  20. using SqlBulkTools.Enumeration;
  21. using SQLinq.Dapper;
  22.  
  23. namespace App.Domain.DataRepository.Products
  24. {
  25. [Export(typeof(IProductsRepository))]
  26.  
  27. internal class ProductsRepository : BaseDbRepository<Product>, IProductsRepository
  28. {
  29. /// <summary>
  30. /// Get the products for export to shopify by account retailers
  31. /// </summary>
  32. /// <param name="clientId"></param>
  33. /// <returns></returns>
  34. public ListResult<Product> GetProductsForClient(int clientId,int page, int pageSize)
  35. {
  36. return Using(connection =>
  37. {
  38. var sql = $@"
  39. DECLARE @clientId INT = {clientId}
  40. DECLARE @page INT = {page}
  41. DECLARE @pageSize INT = {pageSize};
  42. WITH
  43. CTEResults AS
  44. (
  45. SELECT
  46. p.*,
  47. c.NAME AS Category,
  48. r.NAME AS RetailerName,
  49. ROW_NUMBER() OVER (ORDER BY p.id asc) AS RowNum
  50. FROM products p
  51. LEFT JOIN categories c ON c.id = p.categoryid
  52. LEFT JOIN retailers r ON r.id = p.retailerid
  53. LEFT JOIN productexternalmap pem ON pem.productid = p.id AND (pem.clientid = @clientId OR pem.clientid IS NULL)
  54. WHERE
  55. p.statusID=1 AND
  56. ((pem.productid IS NULL AND p.retailerid IN(SELECT retailerid FROM retailer_clients rc WHERE clientid=@clientId AND enabled=1
  57. AND EXISTS(SELECT id FROM retailers WHERE id=rc.retailerId AND statusid=1)) AND p.clientId=0 ))
  58. OR (pem.ProductId IS NOT NULL AND p.updated > pem.LastUpdatedOn )
  59.  
  60. )
  61. SELECT
  62. p.*
  63. ,pii.id
  64. ,pii.src
  65. ,pv.[Id]
  66. ,pv.[statusId]
  67. ,pv.position
  68. ,pv.[updated]
  69. ,pv.[type]
  70. ,pv.[value]
  71. ,pv.[sku]
  72. ,pv.[upc]
  73. ,pv.[upcLooked]
  74. ,pv.[price]
  75. ,pv.[compareAtPrice]
  76. ,pv.[stock]
  77. ,pv.[providerId]
  78. ,pv.[options]
  79. ,por.optionType
  80. ,por.optionKey FROM (
  81. SELECT *
  82. FROM CTEResults
  83. WHERE RowNum > ((@Page - 1) * @PageSize) AND RowNum <= (@Page * @PageSize)
  84. ) p
  85. LEFT JOIN product_variants pv ON pv.productid = p.id
  86. LEFT JOIN product_variant_options pvo ON pv.id=pvo.variantId
  87. LEFT JOIN product_options_raw por ON por.id = pvo.rawOptionId
  88. LEFT JOIN product_images pii ON pii.productid = p.id
  89. WHERE (pv.statusid = 1) AND (pii.src is not null)
  90. ";
  91.  
  92. Dictionary<int, Product> lookup = new Dictionary<int, Product>();
  93. connection.Query<Product, ProductImage, ProductVariant, Product>(sql, (product, image, variant) =>
  94. {
  95. Product mapProduct;
  96. if (!lookup.TryGetValue(product.Id, out mapProduct))
  97. {
  98. lookup.Add(product.Id, mapProduct = product);
  99. }
  100.  
  101. mapProduct.Variants.Add(variant);
  102. mapProduct.Images.Add(image);
  103.  
  104. return mapProduct;
  105. },commandTimeout:0);
  106.  
  107. return (lookup.Values.ToList());
  108. });
  109. }
  110.  
  111. /// <summary>
  112. /// We get the products for all clients
  113. /// </summary>
  114. /// <param name="clientId"></param>
  115. /// <param name="page"></param>
  116. /// <param name="pageSize"></param>
  117. /// <returns></returns>
  118. public List<ClientShopifyCredentials> GetClientsRecurringTask()
  119. {
  120.  
  121.  
  122. return Using(connection => connection.Query<ClientShopifyCredentials>(@"
  123. SELECT * FROM ClientShopifyCredentials
  124. ")).ToList();
  125.  
  126.  
  127. }
  128.  
  129. /// <summary>
  130. /// Function to get the mapped products with all columns
  131. /// </summary>
  132. /// <returns></returns>
  133. public List<ProductExternalMapping> GetMappedProducts(int clientId)
  134. {
  135. return Using(connection => connection.Query<ProductExternalMapping>(@"
  136. SELECT
  137. pem.Id
  138. ,pem.ProductId
  139. ,pem.ExternalId
  140. ,pem.CreatedOn
  141. ,pem.LastUpdatedOn
  142. ,pem.AppName
  143. ,pem.Handle
  144. ,pem.ClientId
  145. FROM ProductExternalMap pem
  146. INNER JOIN Products p ON p.id = pem.productid
  147. WHERE
  148. pem.clientid=@clientId
  149. ",
  150. new
  151. {
  152. clientId = clientId
  153. }).ToList());
  154. }
  155.  
  156. /// <summary>
  157. /// Only mapped productid and externalid function
  158. /// </summary>
  159. /// <param name="clientId"></param>
  160. /// <returns></returns>
  161. public List<ProductExternalAndLocalId> GetMappedProductIds(int clientId)
  162. {
  163. return Using(connection => connection.Query<ProductExternalAndLocalId>(@"
  164. SELECT
  165. ProductId,
  166. ExternalId
  167. FROM ProductExternalMap
  168. WHERE
  169. clientid=@clientId
  170. ",
  171. new
  172. {
  173. clientId = clientId
  174. }).ToList());
  175. }
  176.  
  177. /// <summary>
  178. /// Function to get ExternalIds from productexternalmap
  179. /// </summary>
  180. /// <returns></returns>
  181. public List<long> GetExternalIdProducts(int clientId)
  182. {
  183. return Using(connection => connection.Query<long>(@"
  184. SELECT
  185. pem.ExternalId
  186. FROM ProductExternalMap pem
  187. INNER JOIN Products p ON p.id = pem.productid
  188. WHERE
  189. pem.clientid=@clientId
  190. ",
  191. new
  192. {
  193. clientId = clientId
  194. }).ToList());
  195. }
  196.  
  197. public int GetExportedCount(int clientId)
  198. {
  199. return Using(connection =>
  200. {
  201. var sql = $@"
  202. SELECT
  203. Count(pem.ExternalId)
  204. FROM ProductExternalMap pem
  205. INNER JOIN Products p ON p.id = pem.productid
  206. WHERE
  207. pem.clientid=@clientId
  208. ";
  209.  
  210. var reader = connection.QueryMultiple($"{sql};", new { clientid = clientId }, commandTimeout: 0);
  211.  
  212. var totalCount = reader.Read<int>().SingleOrDefault();
  213. return totalCount;
  214. });
  215. }
  216. /// <summary>
  217. /// Create a relation between a products and external system
  218. /// </summary>
  219. /// <param name="mappings"></param>
  220. /// <param name="deleted"></param>
  221. public void MapProductsWithExternalProvider(List<ProductExternalMapping> mappings)
  222. {
  223. if (!mappings.IsNullOrHasZeroElements())
  224. {
  225. ///next we insert or update the other ones that are matched
  226. Using(connection =>
  227. {
  228. var bulk = new BulkOperations();
  229. using (TransactionScope trans = new TransactionScope(TransactionScopeOption.Required, TimeSpan.FromMinutes(10)))
  230. {
  231. bulk.Setup<ProductExternalMapping>()
  232. .ForCollection(mappings)
  233. .WithTable("ProductExternalMap")
  234. .AddAllColumns()
  235. .BulkInsertOrUpdate()
  236. .MatchTargetOn(x => x.ExternalId)
  237. .SetIdentityColumn(x => x.Id, ColumnDirectionType.Input)
  238. .Commit(connection as SqlConnection);
  239.  
  240. trans.Complete();
  241. }
  242.  
  243. });
  244. }
  245.  
  246.  
  247. }
  248.  
  249. /// <summary>
  250. /// Maps single webhook response to database
  251. /// </summary>
  252. /// <param name="map"></param>
  253. public void MapWebhookResponse(ProductExternalMapping map)
  254. {
  255. ///next we insert mapping
  256. Using(connection =>
  257. {
  258. connection.Query(@"INSERT INTO [dbo].[ProductExternalMap]
  259. (
  260. [ProductId]
  261. ,[ExternalId]
  262. ,[CreatedOn]
  263. ,[LastUpdatedOn]
  264. ,[AppName]
  265. ,[ClientId]
  266. )
  267. (
  268. SELECT
  269. @ProductId,
  270. @ExternalId,
  271. @CreatedOn,
  272. @LastUpdatedOn,
  273. @AppName,
  274. @ClientId
  275. )
  276. ",
  277. new
  278. {
  279. ProductId = map.ProductId,
  280. ExternalId = map.ExternalId,
  281. CreatedOn = map.CreatedOn,
  282. LastUpdatedOn = map.LastUpdatedOn,
  283. AppName = map.AppName,
  284. ClientId = map.ClientId
  285.  
  286. });
  287. });
  288.  
  289.  
  290. }
  291. /// <summary>
  292. /// Deleting external products and the mapping
  293. /// </summary>
  294. /// <param name="deleted"></param>
  295. public void DeleteExternalProductAndMapping(List<ProductExternalMapping> deleted)
  296. {
  297. // TODO: implement bulk insert/update/delete
  298. if (!deleted.IsNullOrHasZeroElements())
  299. {
  300. //first we delete the not matched ones
  301. Using(connection =>
  302. {
  303. int pageSize = 1000;
  304. for (int page = 0; ; page++)
  305. {
  306. var ids = deleted.Skip(page * pageSize).Take(pageSize).Select(x => x.ExternalId).ToList();
  307. connection.Execute(@"DELETE FROM ProductExternalMap WHERE ExternalId IN @Ids", new { Ids = ids }, commandTimeout:0);
  308. if (ids.Count < pageSize) break;
  309. }
  310. });
  311.  
  312. }
  313. }
  314.  
  315. /// <summary>
  316. /// We use this to get the clients credentials with his id
  317. /// </summary>
  318. /// <param name="clientId"></param>
  319. /// <returns></returns>
  320. public ClientShopifyCredentials GetCredentials(int clientId)
  321. {
  322.  
  323.  
  324. var cscConvertedtemp = Using(connection => connection.Query<ClientShopifyCredentials>(@"
  325. SELECT
  326. Id
  327. ,ClientId
  328. ,ShopifyStoreURL
  329. ,ShopifyStorePassword
  330. FROM ClientShopifyCredentials
  331. WHERE ClientId = @clientId;
  332. ",
  333. new
  334. {
  335. ClientId = clientId
  336. }).SingleOrDefault());
  337.  
  338. return cscConvertedtemp;
  339.  
  340. }
  341.  
  342. /// <summary>
  343. /// We set the credentials for the shopify access here or we insert new record
  344. /// </summary>
  345. /// <param name="obj"></param>
  346. public void SetCredentials(ClientShopifyCredentials obj)
  347. {
  348.  
  349. Using(connection => connection.Query(@"
  350. IF @ClientId
  351. IN (SELECT DISTINCT clientid FROM dbo.clientshopifycredentials)
  352. BEGIN
  353. UPDATE [dbo].[ClientShopifyCredentials]
  354. SET ShopifyStoreURL = @ShopifyStoreURL, ShopifyStorePassword = @ShopifyStorePassword
  355. WHERE clientId = @ClientId
  356. END
  357. ELSE
  358. INSERT INTO
  359. [dbo].[ClientShopifyCredentials]
  360. (
  361. [ClientId]
  362. ,[ShopifyStoreURL]
  363. ,[ShopifyStorePassword])
  364.  
  365. (
  366. SELECT
  367. @ClientId
  368. ,@ShopifyStoreURL
  369. ,@ShopifyStorePassword
  370. );
  371. ",
  372. new
  373. {
  374. ClientId = obj.ClientId,
  375. ShopifyStoreURL = obj.ShopifyStoreURL,
  376. ShopifyStorePassword = obj.ShopifyStorePassword
  377. }));
  378.  
  379. }
  380.  
  381. /// <summary>
  382. /// We get all the products for grid here
  383. /// </summary>
  384. /// <param name="queryOptions"></param>
  385. /// <param name="clientId"></param>
  386. /// <returns></returns>
  387. public ExportGridToShopify GetAllProductsForGrid(int clientId, AdvancedQueryOptions queryOptions)
  388. {
  389. return Using(connection =>
  390. {
  391. var queryBuilder = new StringBuilder(queryOptions.Filters.Count > 0 ? string.Empty : "1 = 1");
  392. var currentIndex = 0;
  393. var shouldAppendOrOp = true;
  394. dynamic executionData = new ExpandoObject();
  395. foreach (var filter in queryOptions.Filters)
  396. {
  397. if (currentIndex + 1 == queryOptions.Filters.Count) shouldAppendOrOp = false;
  398.  
  399. var query = string.Empty;
  400. switch (filter.Name)
  401. {
  402. case @"Id":
  403. queryBuilder.AppendLine(@" p.Id LIKE @Id ");
  404. executionData.Id = $@"%{filter.Value}%";
  405. break;
  406. case @"Title":
  407. queryBuilder.AppendLine(@" p.Title LIKE @Title ");
  408. executionData.Title = $@"%{filter.Value}%";
  409. break;
  410. case @"Created":
  411. queryBuilder.AppendLine(@" cast(p.Created as Date) = @Created ");
  412. executionData.Created = filter.Value;
  413. break;
  414. case @"Category":
  415. queryBuilder.AppendLine(@" c.Name LIKE @Category ");
  416. executionData.Category = $@"%{filter.Value}%";
  417. break;
  418. case @"RetailerName":
  419. queryBuilder.AppendLine(@" r.Name LIKE @RetailerName ");
  420. executionData.RetailerName = $@"%{filter.Value}%";
  421. break;
  422. case @"Brand":
  423. queryBuilder.AppendLine(@" p.Brand LIKE @Brand ");
  424. executionData.Brand = $@"%{filter.Value}%";
  425. break;
  426. case @"StatusId":
  427. queryBuilder.AppendLine(@" p.StatusId = @StatusId ");
  428. executionData.StatusId = filter.Value;
  429. break;
  430. default:
  431. currentIndex++;
  432. continue;
  433. }
  434.  
  435. queryBuilder.AppendLine(shouldAppendOrOp ? $@"{query} AND " : query);
  436. currentIndex++;
  437. }
  438. var orderByBuilder = new StringBuilder();
  439. if (InstanceHelper.IsNullOrEmpty(queryOptions.OrderBy))
  440. {
  441. queryOptions.OrderBy = @"p.Created";
  442. queryOptions.Order = OrderBy.Desc;
  443. }
  444.  
  445. var orderbyField = queryOptions.OrderBy;
  446. var orderby = queryOptions.Order.ToString();
  447. switch (queryOptions.OrderBy)
  448. {
  449. case @"Id":
  450. orderbyField = @"p.Id";
  451. break;
  452. case @"Title":
  453. orderbyField = @"p.Title";
  454. break;
  455. case @"Created":
  456. orderbyField = @"p.Created";
  457. break;
  458. case @"Category":
  459. orderbyField = @"c.Name";
  460. break;
  461. case @"RetailerName":
  462. orderbyField = @"r.Name";
  463. break;
  464. case @"Brand":
  465. orderbyField = @"p.Brand";
  466. break;
  467. case @"StatusId":
  468. orderbyField = @"p.StatusId";
  469. break;
  470. }
  471.  
  472. var orderbyStatement = $@"{orderbyField} {orderby}";
  473. orderByBuilder.AppendLine(orderbyStatement);
  474.  
  475. var sql =
  476. $@"
  477. ;WITH
  478. CTEResults AS
  479. (
  480. SELECT
  481. p.ID,
  482. p.Created,
  483. p.StatusId,
  484. p.Title,
  485. c.Name AS Category,
  486. r.Name AS RetailerName,
  487. p.Brand,
  488. ROW_NUMBER() OVER (ORDER BY {orderByBuilder},p.id) AS RowNum
  489. FROM products p
  490. LEFT JOIN categories c ON c.id = p.categoryid
  491. LEFT JOIN retailers r ON r.id = p.retailerid
  492. LEFT JOIN productexternalmap pem ON pem.productid = p.id AND (pem.clientid = {clientId} or pem.clientid is null)
  493. WHERE
  494. {queryBuilder}
  495. AND p.statusID=1
  496. AND ((pem.productid is null and p.retailerid in(SELECT retailerid FROM retailer_clients rc WHERE clientid={clientId} AND enabled=1
  497. AND EXISTS(select id from retailers where id=rc.retailerId and statusid=1)) AND p.clientId=0 ))
  498. OR (pem.ProductId IS NOT NULL AND p.updated > pem.LastUpdatedOn)
  499.  
  500.  
  501. )
  502. SELECT
  503. p.ID,
  504. p.Created,
  505. p.StatusId,
  506. (select top 1 src from product_images where p.id=productId) AS Image,
  507. p.Title,
  508. p.Category,
  509. p.RetailerName,
  510. p.Brand
  511.  
  512. FROM (
  513. SELECT *
  514. FROM CTEResults
  515. WHERE RowNum BETWEEN (@Page - 1) * @PageSize AND @Page * @PageSize
  516. ) p
  517. ";
  518.  
  519. var countSql =
  520. $@"
  521. SELECT
  522. count (distinct p.id)
  523. FROM Products P
  524. LEFT JOIN ProductExternalMap pem ON pem.ProductId =p.id AND (pem.clientid = {clientId} or pem.clientid is null)
  525. LEFT JOIN Retailers r on r.id=p.retailerId
  526. LEFT JOIN categories c on c.id=p.categoryId
  527. WHERE
  528. {queryBuilder} AND
  529. (pem.productid IS NOT NULL AND p.updated > pem.LastUpdatedOn )
  530. AND p.statusId=1 AND (p.clientId={clientId}
  531. OR ((p.retailerid in(select retailerid from retailer_clients rc where clientid={clientId} and enabled=1
  532. AND EXISTS(select id from retailers where id=rc.retailerId and statusid=1)) and p.clientId=0 )))
  533.  
  534. ";
  535.  
  536. var createSql =
  537. $@"
  538. SELECT
  539. count (distinct p.id)
  540. FROM Products P
  541. LEFT JOIN ProductExternalMap pem ON pem.ProductId =p.id AND (pem.clientid = {clientId} or pem.clientid is null)
  542. LEFT JOIN Retailers r on r.id=p.retailerId
  543. LEFT JOIN categories c on c.id=p.categoryId
  544. WHERE pem.productid IS NULL AND
  545. p.statusId=1 AND (p.clientId={clientId}
  546. OR ((p.retailerid in(select retailerid from retailer_clients rc where clientid={clientId} and enabled=1
  547. AND EXISTS(select id from retailers where id=rc.retailerId and statusid=1)) and p.clientId=0 )))
  548.  
  549. ";
  550. var updateSql =
  551. $@"
  552. SELECT
  553. count (distinct p.id)
  554. FROM Products P
  555. INNER JOIN ProductExternalMap pem ON pem.ProductId =p.id AND pem.clientid = {clientId}
  556. LEFT JOIN Retailers r on r.id=p.retailerId
  557. LEFT JOIN categories c on c.id=p.categoryId
  558. WHERE
  559. (pem.LastUpdatedOn IS NOT NULL AND p.updated > pem.LastUpdatedOn)
  560. AND p.statusId=1 AND (p.clientId={clientId}
  561. OR ((p.retailerid in(select retailerid from retailer_clients rc where clientid={clientId} and enabled=1
  562. AND EXISTS(select id from retailers where id=rc.retailerId and statusid=1)) and p.clientId=0 )))
  563. ";
  564. var exported =
  565. $@"
  566. SELECT
  567. COUNT(Pem.productid)
  568. FROM ProductExternalMap PEM
  569. WHERE
  570. Pem.ClientId={clientId}
  571. ";
  572.  
  573. var totalProducts =
  574. $@"
  575. SELECT
  576. COUNT(P.Id)
  577. FROM Products P
  578. WHERE
  579. p.statusId=1 AND (p.clientId={clientId}
  580. OR ((p.retailerid in(select retailerid from retailer_clients rc where clientid={clientId} and enabled=1
  581. AND EXISTS(select id from retailers where id=rc.retailerId and statusid=1)) and p.clientId=0 )))
  582.  
  583. ";
  584.  
  585. executionData.PageSize = queryOptions.PageSize;
  586. executionData.Page = queryOptions.Page;
  587.  
  588. var str = connection.GetProfilingSql(sql, (object)executionData);
  589.  
  590. var reader = connection.QueryMultiple($"{sql}; {countSql}; {exported}; {totalProducts}; {updateSql}; {createSql};", (object)executionData,commandTimeout:0);
  591. var values = reader.Read<ProductGrid>().ToList();
  592. var totalCount = reader.Read<int>().SingleOrDefault();
  593. var totalExported = reader.Read<int>().SingleOrDefault();
  594. var totalProductsEver = reader.Read<int>().SingleOrDefault();
  595. var totalUpdate = reader.Read<int>().SingleOrDefault();
  596. var totalCreate = reader.Read<int>().SingleOrDefault();
  597.  
  598. return new ExportGridToShopify(values, totalCount) { ExportCount = totalExported, TotalProducts = totalProductsEver, ForUpdate =totalUpdate, ForCreating = totalCreate};
  599. });
  600. }
  601.  
  602. /// <summary>
  603. /// Getting the deleted products
  604. /// </summary>
  605. /// <param name="clientId"></param>
  606. /// <returns></returns>
  607. public List<ProductExternalMapping> GetProductsWithStatusDeleted(int clientId)
  608. {
  609. return Using(connection => connection.Query<ProductExternalMapping>(@"
  610. ;WITH CTE AS
  611. (
  612. SELECT pem.*
  613. FROM PRODUCTS P
  614. INNER JOIN ProductExternalMap pem ON pem.ProductId = p.id
  615. WHERE
  616. pem.ClientId=@clientid and p.statusId=3 or(EXISTS(select id from retailers r where r.id = p.retailerId and r.statusid=0)
  617. OR EXISTS(select id from retailer_clients rc where rc.retailerId = p.retailerId AND rc.clientid=@clientid and rc.enabled=0))
  618.  
  619. )
  620. SELECT *
  621. FROM CTE
  622. ",
  623. new
  624. {
  625. ClientId = clientId
  626. }).ToList());
  627. }
  628.  
  629. /// <summary>
  630. /// With this we get the categories in a list so we can use them later
  631. /// </summary>
  632. /// <returns></returns>
  633. public List<Category> GetCategories(int clientId)
  634. {
  635. return Using(connection => connection.Query<Category>(@"
  636. SELECT *
  637. FROM Categories c
  638. WHERE
  639. c.id IN (SELECT DISTINCT P.CategoryId FROM Products P
  640. LEFT JOIN product_categories PC ON PC.productId = P.ID
  641. WHERE p.statusID=1 AND
  642. ((p.clientId=@clientId ) or (p.retailerid in(select retailerid from retailer_clients rc where clientid=@clientId and enabled=1
  643. AND EXISTS(select id from retailers where id=rc.retailerId and statusid=1)) and p.clientId=0 )))
  644. ",
  645. new
  646. {
  647. ClientId = clientId
  648. }).ToList());
  649. }
  650.  
  651. /// <summary>
  652. /// Get Categories
  653. /// </summary>
  654. /// <param name="clientId"></param>
  655. /// <returns></returns>
  656. public List<CategoryExternalMapping> GetMappedCategories(int clientId)
  657. {
  658. return Using(connection => connection.Query<CategoryExternalMapping>(@"
  659. ;WITH CTE as (SELECT DISTINCT P.CategoryId FROM Products P
  660. LEFT JOIN product_categories PC ON PC.productId = P.ID
  661. WHERE p.statusID=1 AND
  662. ((p.clientId=@clientId ) or (p.retailerid in(select retailerid from retailer_clients rc where clientid=@clientId and enabled=1
  663. AND EXISTS(select id from retailers where id=rc.retailerId and statusid=1)) and p.clientId=0 )))
  664. SELECT *
  665. FROM CategoryExternalMap cem
  666. INNER JOIN cte c ON c.categoryId=cem.CategoryId AND cem.clientid=@clientId",
  667. new
  668. {
  669. ClientId = clientId
  670. }).ToList());
  671. }
  672.  
  673. /// <summary>
  674. /// Map created categories from shopify
  675. /// </summary>
  676. /// <param name="mapping"></param>
  677. public void MapExternalCategories(CategoryExternalMapping mapping)
  678. {
  679. ///next we insert mapping
  680. Using(connection =>
  681. {
  682. connection.Query(@"INSERT INTO [dbo].[CategoryExternalMap]
  683. (
  684. [CategoryId]
  685. ,[ExternalId]
  686. ,[CreatedOn]
  687. ,[LastUpdatedOn]
  688. ,[AppName]
  689. ,[ClientId]
  690. )
  691. (
  692. SELECT
  693. @CategoryId,
  694. @ExternalId,
  695. @CreatedOn,
  696. @LastUpdatedOn,
  697. @AppName,
  698. @ClientId
  699. )
  700. ",
  701. new
  702. {
  703. CategoryId = mapping.CategoryId,
  704. ExternalId = mapping.ExternalId,
  705. CreatedOn = mapping.CreatedOn,
  706. LastUpdatedOn = mapping.LastUpdatedOn,
  707. AppName = mapping.AppName,
  708. ClientId = mapping.ClientId
  709.  
  710. });
  711. });
  712. }
  713.  
  714. /// <summary>
  715. /// Saving backgorund service status, so we can read later
  716. /// </summary>
  717. /// <param name="status"></param>
  718. /// <param name="clientId"></param>
  719. public void BackgroundServiceStatus(string status, int clientId)
  720. {
  721. ///next we insert mapping
  722. Using(connection =>
  723. {
  724. connection.Query(@"INSERT INTO [dbo].[ShopifyProcessingReports]
  725. (
  726. [ClientId]
  727. ,[StatusMessage]
  728. ,[ProcessTimeStamp]
  729. )
  730. (
  731. SELECT
  732. @ClientId,
  733. @StatusMessage,
  734. GetDate()
  735. )
  736. ",
  737. new
  738. {
  739. ClientId = clientId,
  740. StatusMessage = status
  741. });
  742. });
  743. }
  744. }
  745. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement