StoreBranchProductAttrValueServices.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\product\branch;
  12. use app\dao\product\sku\StoreProductAttrValueDao;
  13. use app\services\BaseServices;
  14. use app\services\product\sku\StoreProductAttrValueServices;
  15. use app\services\product\product\StoreProductStockRecordServices;
  16. use app\webscoket\SocketPush;
  17. use crmeb\exceptions\AdminException;
  18. use crmeb\traits\ServicesTrait;
  19. /**
  20. * Class StoreBranchProductAttrValueServices
  21. * @package app\services\product\branch
  22. * @mixin StoreProductAttrValueDao
  23. */
  24. class StoreBranchProductAttrValueServices extends BaseServices
  25. {
  26. use ServicesTrait;
  27. /**
  28. * StoreBranchProductAttrValueServices constructor.
  29. * @param StoreProductAttrValueDao $dao
  30. */
  31. public function __construct(StoreProductAttrValueDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * @param string $unique
  37. * @param int $storeId
  38. * @return int|mixed
  39. */
  40. public function uniqueByStock(string $unique, int $storeId)
  41. {
  42. if (!$unique) return 0;
  43. return $this->dao->uniqueByStock($unique, $storeId);
  44. }
  45. /**
  46. * 更新
  47. * @param int $id
  48. * @param array $data
  49. * @param int $store_id
  50. */
  51. public function updataAll(int $id, array $data, int $store_id)
  52. {
  53. /** @var StoreBranchProductServices $productServices */
  54. $productServices = app()->make(StoreBranchProductServices::class);
  55. $where = [];
  56. $where['product_id'] = $id;
  57. $where['store_id'] = $store_id;
  58. $where['type'] = 0;
  59. $this->transaction(function () use ($id, $store_id, $where, $data, $productServices) {
  60. $attrArr = [];
  61. $stock = 0;
  62. $this->dao->delete($where);
  63. foreach ($data['attrs'] as $key => $item) {
  64. $attrArr[$key]['product_id'] = $id;
  65. $attrArr[$key]['store_id'] = $store_id;
  66. $attrArr[$key]['unique'] = $item['unique'] ?? '';
  67. $attrArr[$key]['stock'] = intval($item['stock']) ?? 0;
  68. $attrArr[$key]['bar_code'] = $item['bar_code'] ?? 0;
  69. $attrArr[$key]['type'] = 0;
  70. $stock += (int)($item['stock'] ?? 0);
  71. }
  72. $res1 = $this->dao->saveAll($attrArr);
  73. $productServices->saveStoreProduct($id, $store_id, $stock, $data);
  74. $unique = array_column($data['attrs'], 'unique');
  75. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  76. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  77. $storeProductAttrValueServices->updateSumStock($unique);
  78. //记录入出库
  79. /** @var StoreProductStockRecordServices $storeProductStockRecordServces */
  80. $storeProductStockRecordServces = app()->make(StoreProductStockRecordServices::class);
  81. $storeProductStockRecordServces->saveRecord($id, $attrArr, 0, $store_id);
  82. if (!$res1) {
  83. throw new AdminException('添加失败!');
  84. }
  85. });
  86. }
  87. /**
  88. * 获取某个门店商品下的库存
  89. * @param int $storeId
  90. * @param int $productId
  91. * @return array
  92. */
  93. public function getProductAttrUnique(int $storeId, int $productId)
  94. {
  95. return $this->dao->getColumn(['store_id' => $storeId, 'product_id' => $productId], 'stock', 'unique');
  96. }
  97. /**
  98. * 获取某个sku下的商品库存总和
  99. * @param string $unique
  100. * @return array
  101. */
  102. public function getProductAttrValueStockSum(array $unique)
  103. {
  104. return $this->dao->getColumn([['unique', 'in', $unique]], 'sum(stock) as sum_stock', 'unique');
  105. }
  106. /**
  107. * 获取门店商品规格信息
  108. * @param int $id
  109. * @param int $type
  110. * @return mixed
  111. * @throws \think\db\exception\DataNotFoundException
  112. * @throws \think\db\exception\DbException
  113. * @throws \think\db\exception\ModelNotFoundException
  114. */
  115. public function getStoreProductAttr(int $id, int $type = 0)
  116. {
  117. return $this->dao->getProductAttrValue(['product_id' => $id, 'type' => $type]);
  118. }
  119. }