StoreProductAttrValueServices.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\sku;
  12. use app\dao\product\sku\StoreProductAttrValueDao;
  13. use app\models\store\StoreProductAttrValue;
  14. use app\services\BaseServices;
  15. use crmeb\exceptions\AdminException;
  16. use app\services\product\product\StoreProductServices;
  17. use crmeb\services\CacheService;
  18. use crmeb\services\workerman\ChannelService;
  19. /**
  20. * Class StoreProductAttrValueService
  21. * @package app\services\product\sku
  22. * @method getProductAttrValue(array $where)
  23. * @method value(array $where, string $field = '') 获取单个键值的数据
  24. * @method decStockIncSales(array $where, int $num, string $stock = 'stock', string $sales = 'sales') 减库存加销量
  25. * @method count(array $where) 获取指定条件下的数量
  26. */
  27. class StoreProductAttrValueServices extends BaseServices
  28. {
  29. /**
  30. * StoreProductAttrValueServices constructor.
  31. * @param StoreProductAttrValueDao $dao
  32. */
  33. public function __construct(StoreProductAttrValueDao $dao)
  34. {
  35. $this->dao = $dao;
  36. }
  37. /**
  38. * 获取单规格规格
  39. * @param array $where
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function getOne(array $where)
  45. {
  46. return $this->dao->getOne($where);
  47. }
  48. /**
  49. * 获取指定条件下的数据以数组返回
  50. * @param array $where
  51. * @param string $field
  52. * @param string $key
  53. * @return array
  54. */
  55. public function getColumn(array $where, string $field = '*', string $key = 'suk')
  56. {
  57. return $this->dao->getColumn($where, $field, $key);
  58. }
  59. /**
  60. * 删除一条数据
  61. * @param int $id
  62. * @param int $type
  63. */
  64. public function del(int $id, int $type)
  65. {
  66. $this->dao->del($id, $type);
  67. }
  68. /**
  69. * 批量保存
  70. * @param array $data
  71. */
  72. public function saveAll(array $data)
  73. {
  74. $res = $this->dao->saveAll($data);
  75. if (!$res) throw new AdminException(100006);
  76. return $res;
  77. }
  78. /**
  79. * 获取sku
  80. * @param array $where
  81. * @return array
  82. */
  83. public function getSkuArray(array $where)
  84. {
  85. return $this->dao->getColumn($where, 'bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,quota,unique', 'suk');
  86. }
  87. /**
  88. * 交易排行榜
  89. * @return array
  90. * @throws \think\db\exception\DataNotFoundException
  91. * @throws \think\db\exception\DbException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. */
  94. public function purchaseRanking()
  95. {
  96. $dlist = $this->dao->attrValue();
  97. /** @var StoreProductServices $proServices */
  98. $proServices = app()->make(StoreProductServices::class);
  99. $slist = $proServices->getProductLimit(['is_del' => 0], $limit = 20, 'id as product_id,store_name,sales * price as val');
  100. $data = array_merge($dlist, $slist);
  101. $last_names = array_column($data, 'val');
  102. array_multisort($last_names, SORT_DESC, $data);
  103. $list = array_splice($data, 0, 20);
  104. return $list;
  105. }
  106. /**获取商品的属性数量
  107. * @param $product_id
  108. * @param $unique
  109. * @param $type
  110. * @return int
  111. */
  112. public function getAttrvalueCount($product_id, $unique, $type)
  113. {
  114. return $this->dao->count(['product_id' => $product_id, 'unique' => $unique, 'type' => $type]);
  115. }
  116. /**
  117. * 获取唯一值下的库存
  118. * @param string $unique
  119. * @return int
  120. */
  121. public function uniqueByStock(string $unique)
  122. {
  123. if (!$unique) return 0;
  124. return $this->dao->uniqueByStock($unique);
  125. }
  126. /**
  127. * 减销量,加库存
  128. * @param $productId
  129. * @param $unique
  130. * @param $num
  131. * @param int $type
  132. * @return mixed
  133. */
  134. public function decProductAttrStock($productId, $unique, $num, $type = 0)
  135. {
  136. $res = $this->dao->decStockIncSales([
  137. 'product_id' => $productId,
  138. 'unique' => $unique,
  139. 'type' => $type
  140. ], $num);
  141. if ($res) {
  142. $this->workSendStock($productId, $unique, $type);
  143. }
  144. return $res;
  145. }
  146. /**
  147. * 减少销量增加库存
  148. * @param $productId
  149. * @param $unique
  150. * @param $num
  151. * @return bool
  152. */
  153. public function incProductAttrStock(int $productId, string $unique, int $num, int $type = 0)
  154. {
  155. return $this->dao->incStockDecSales(['unique' => $unique, 'product_id' => $productId, 'type' => $type], $num);
  156. }
  157. /**
  158. * 库存预警消息提醒
  159. * @param int $productId
  160. * @param string $unique
  161. * @param int $type
  162. */
  163. public function workSendStock(int $productId, string $unique, int $type)
  164. {
  165. $stock = $this->dao->value([
  166. 'product_id' => $productId,
  167. 'unique' => $unique,
  168. 'type' => $type
  169. ], 'stock');
  170. $replenishment_num = sys_config('store_stock') ?? 0;//库存预警界限
  171. if ($replenishment_num >= $stock) {
  172. try {
  173. ChannelService::instance()->send('STORE_STOCK', ['id' => $productId]);
  174. } catch (\Exception $e) {
  175. }
  176. }
  177. }
  178. /**
  179. * 获取秒杀库存
  180. * @param int $productId
  181. * @param string $unique
  182. * @param bool $isNew
  183. * @return array|mixed|\think\Model|null
  184. * @throws \Psr\SimpleCache\InvalidArgumentException
  185. * @throws \think\db\exception\DataNotFoundException
  186. * @throws \think\db\exception\DbException
  187. * @throws \think\db\exception\ModelNotFoundException
  188. */
  189. public function getSeckillAttrStock(int $productId, string $unique, bool $isNew = false)
  190. {
  191. $key = md5('seclkill_attr_stock_' . $productId . '_' . $unique);
  192. $stock = CacheService::get($key);
  193. if (!$stock || $isNew) {
  194. $stock = $this->dao->getOne(['product_id' => $productId, 'unique' => $unique, 'type' => 1], 'suk,quota');
  195. if ($stock) {
  196. CacheService::set($key, $stock, 60);
  197. }
  198. }
  199. return $stock;
  200. }
  201. /**
  202. * @param $product_id
  203. * @param string $suk
  204. * @param string $unique
  205. * @param bool $is_new
  206. * @return int|mixed
  207. * @throws \Psr\SimpleCache\InvalidArgumentException
  208. */
  209. public function getProductAttrStock(int $productId, string $suk = '', string $unique = '', $isNew = false)
  210. {
  211. if (!$suk && !$unique) return 0;
  212. $key = md5('product_attr_stock_' . $productId . '_' . $suk . '_' . $unique);
  213. $stock = CacheService::get($key);
  214. if (!$stock || $isNew) {
  215. $where = ['product_id' => $productId, 'type' => 0];
  216. if ($suk) {
  217. $where['suk'] = $suk;
  218. }
  219. if ($unique) {
  220. $where['unique'] = $unique;
  221. }
  222. $stock = $this->dao->value($where, 'stock');
  223. CacheService::set($key, $stock, 60);
  224. }
  225. return $stock;
  226. }
  227. }