StoreIntegralServices.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. declare (strict_types=1);
  12. namespace app\services\activity\integral;
  13. use app\Request;
  14. use app\services\BaseServices;
  15. use app\dao\activity\integral\StoreIntegralDao;
  16. use app\services\product\product\StoreDescriptionServices;
  17. use app\services\product\product\StoreProductServices;
  18. use app\services\product\product\StoreVisitServices;
  19. use app\services\product\sku\StoreProductAttrResultServices;
  20. use app\services\product\sku\StoreProductAttrServices;
  21. use app\services\product\sku\StoreProductAttrValueServices;
  22. use app\jobs\ProductLogJob;
  23. use crmeb\exceptions\AdminException;
  24. use crmeb\exceptions\ApiException;
  25. use crmeb\services\CacheService;
  26. /**
  27. *
  28. * Class StoreIntegralServices
  29. * @package app\services\activity
  30. * @method getOne(array $where, ?string $field = '*', array $with = []) 根据条件获取一条数据
  31. * @method get(int $id, ?array $field) 获取一条数据
  32. */
  33. class StoreIntegralServices extends BaseServices
  34. {
  35. const THODLCEG = 'ykGUKB';
  36. /**
  37. * StoreIntegralServices constructor.
  38. * @param StoreIntegralDao $dao
  39. */
  40. public function __construct(StoreIntegralDao $dao)
  41. {
  42. $this->dao = $dao;
  43. }
  44. /**
  45. * 获取指定条件下的条数
  46. * @param array $where
  47. */
  48. public function getCount(array $where)
  49. {
  50. $this->dao->count($where);
  51. }
  52. /**
  53. * 积分商品添加
  54. * @param int $id
  55. * @param array $data
  56. */
  57. public function saveData(int $id, array $data)
  58. {
  59. if ($data['num'] < $data['once_num']) {
  60. throw new AdminException(400500);
  61. }
  62. $description = $data['description'];
  63. $detail = $data['attrs'];
  64. $items = $data['items'];
  65. $data['images'] = json_encode($data['images']);
  66. $data['price'] = min(array_column($detail, 'price'));
  67. $data['quota'] = $data['quota_show'] = array_sum(array_column($detail, 'quota'));
  68. $data['stock'] = array_sum(array_column($detail, 'stock'));
  69. unset($data['section_time'], $data['description'], $data['attrs'], $data['items']);
  70. /** @var StoreDescriptionServices $storeDescriptionServices */
  71. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  72. /** @var StoreProductAttrServices $storeProductAttrServices */
  73. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  74. /** @var StoreProductServices $storeProductServices */
  75. $storeProductServices = app()->make(StoreProductServices::class);
  76. if ($data['quota'] > $storeProductServices->value(['id' => $data['product_id']], 'stock')) {
  77. throw new AdminException(400090);
  78. }
  79. $this->transaction(function () use ($id, $data, $description, $detail, $items, $storeDescriptionServices, $storeProductAttrServices, $storeProductServices) {
  80. if ($id) {
  81. $res = $this->dao->update($id, $data);
  82. $storeDescriptionServices->saveDescription((int)$id, $description, 4);
  83. $skuList = $storeProductServices->validateProductAttr($items, $detail, (int)$id, 4);
  84. $storeProductAttrServices->saveProductAttr($skuList, (int)$id, 4);
  85. if (!$res) throw new AdminException(100007);
  86. } else {
  87. if (!$storeProductServices->getOne(['is_show' => 1, 'is_del' => 0, 'id' => $data['product_id']])) {
  88. throw new AdminException(400091);
  89. }
  90. $data['add_time'] = time();
  91. $res = $this->dao->save($data);
  92. $storeDescriptionServices->saveDescription((int)$res->id, $description, 4);
  93. $skuList = $storeProductServices->validateProductAttr($items, $detail, (int)$res->id, 4);
  94. $storeProductAttrServices->saveProductAttr($skuList, (int)$res->id, 4);
  95. if (!$res) throw new AdminException(100022);
  96. }
  97. });
  98. }
  99. /**
  100. * 批量添加商品
  101. * @param array $data
  102. */
  103. public function saveBatchData(array $data)
  104. {
  105. /** @var StoreProductServices $service */
  106. $service = app()->make(StoreProductServices::class);
  107. /** @var StoreDescriptionServices $storeDescriptionServices */
  108. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  109. /** @var StoreProductAttrResultServices $storeProductAttrResultServices */
  110. $storeProductAttrResultServices = app()->make(StoreProductAttrResultServices::class);
  111. if (!$data) {
  112. throw new AdminException(400337);
  113. }
  114. if(!$data['attrs']) throw new AdminException(400337);
  115. $attrs = [];
  116. foreach ($data['attrs'] as $k => $v) {
  117. $attrs[$v['product_id']][] = $v;
  118. }
  119. foreach ($attrs as $k => $v) {
  120. $productInfo = $service->getOne(['id' => $k]);
  121. $productInfo = is_object($productInfo) ? $productInfo->toArray() : [];
  122. if ($productInfo) {
  123. $product = [];
  124. $result = $storeProductAttrResultServices->getResult(['product_id' => $productInfo['id'], 'type' => 0]);
  125. $product['product_id'] = $productInfo['id'];
  126. $product['description'] = $storeDescriptionServices->getDescription(['product_id' => $productInfo['id'], 'type' => 0]);
  127. $product['attrs'] = $v;
  128. $product['items'] = $result['attr'];
  129. $product['is_show'] = $data['is_show'] ?? 0;
  130. $product['title'] = $productInfo['store_name'];
  131. $product['unit_name'] = $productInfo['unit_name'];
  132. $product['image'] = $productInfo['image'];
  133. $product['images'] = $productInfo['slider_image'];
  134. $product['num'] = 0;
  135. $product['is_host'] = 0;
  136. $product['once_num'] = 0;
  137. $product['sort'] = 0;
  138. $this->saveData(0, $product);
  139. }
  140. }
  141. return true;
  142. }
  143. /**
  144. * 积分商品列表
  145. * @param array $where
  146. * @return array
  147. */
  148. public function systemPage(array $where)
  149. {
  150. [$page, $limit] = $this->getPageValue();
  151. $list = $this->dao->getList($where, $page, $limit);
  152. $count = $this->dao->count($where);
  153. return compact('list', 'count');
  154. }
  155. /**
  156. * 获取详情
  157. * @param int $id
  158. * @return array|\think\Model|null
  159. */
  160. public function getInfo(int $id)
  161. {
  162. $info = $this->dao->get($id);
  163. if (!$info) {
  164. throw new AdminException(400533);
  165. }
  166. if ($info->is_del) {
  167. throw new AdminException(400534);
  168. }
  169. $info['price'] = floatval($info['price']);
  170. /** @var StoreDescriptionServices $storeDescriptionServices */
  171. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  172. $info['description'] = $storeDescriptionServices->getDescription(['product_id' => $id, 'type' => 4]);
  173. $info['attrs'] = $this->attrList($id, $info['product_id']);
  174. return $info;
  175. }
  176. /**
  177. * 获取规格
  178. * @param int $id
  179. * @param int $pid
  180. * @return mixed
  181. */
  182. public function attrList(int $id, int $pid)
  183. {
  184. /** @var StoreProductAttrResultServices $storeProductAttrResultServices */
  185. $storeProductAttrResultServices = app()->make(StoreProductAttrResultServices::class);
  186. $combinationResult = $storeProductAttrResultServices->value(['product_id' => $id, 'type' => 4], 'result');
  187. $items = json_decode($combinationResult, true)['attr'];
  188. $productAttr = $this->getAttr($items, $pid, 0);
  189. $combinationAttr = $this->getAttr($items, $id, 4);
  190. foreach ($productAttr as $pk => $pv) {
  191. foreach ($combinationAttr as &$sv) {
  192. if ($pv['detail'] == $sv['detail']) {
  193. $productAttr[$pk] = $sv;
  194. }
  195. }
  196. $productAttr[$pk]['detail'] = json_decode($productAttr[$pk]['detail']);
  197. }
  198. $attrs['items'] = $items;
  199. $attrs['value'] = $productAttr;
  200. foreach ($items as $key => $item) {
  201. $header[] = ['title' => $item['value'], 'key' => 'value' . ($key + 1), 'align' => 'center', 'minWidth' => 80];
  202. }
  203. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 120];
  204. $header[] = ['title' => '兑换积分', 'key' => 'price', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  205. $header[] = ['title' => '库存', 'key' => 'stock', 'align' => 'center', 'minWidth' => 80];
  206. $header[] = ['title' => '兑换次数', 'key' => 'quota', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  207. $header[] = ['title' => '重量(KG)', 'key' => 'weight', 'align' => 'center', 'minWidth' => 80];
  208. $header[] = ['title' => '体积(m³)', 'key' => 'volume', 'align' => 'center', 'minWidth' => 80];
  209. $header[] = ['title' => '商品编号', 'key' => 'bar_code', 'align' => 'center', 'minWidth' => 80];
  210. $attrs['header'] = $header;
  211. return $attrs;
  212. }
  213. /**
  214. * 获得规格
  215. * @param $attr
  216. * @param $id
  217. * @param $type
  218. * @return array
  219. */
  220. public function getAttr($attr, $id, $type)
  221. {
  222. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  223. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  224. list($value, $head) = attr_format($attr);
  225. $valueNew = [];
  226. $count = 0;
  227. foreach ($value as $suk) {
  228. $detail = explode(',', $suk);
  229. $sukValue = $storeProductAttrValueServices->getColumn(['product_id' => $id, 'type' => $type, 'suk' => $suk], 'bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,quota,quota_show', 'suk');
  230. if (count($sukValue)) {
  231. foreach ($detail as $k => $v) {
  232. $valueNew[$count]['value' . ($k + 1)] = $v;
  233. }
  234. $valueNew[$count]['detail'] = json_encode(array_combine($head, $detail));
  235. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  236. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  237. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  238. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  239. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  240. // $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
  241. $valueNew[$count]['quota'] = isset($sukValue[$suk]['quota_show']) && $sukValue[$suk]['quota_show'] ? intval($sukValue[$suk]['quota_show']) : 0;
  242. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  243. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  244. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  245. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  246. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  247. $valueNew[$count]['_checked'] = $type != 0;
  248. $count++;
  249. }
  250. }
  251. return $valueNew;
  252. }
  253. /**
  254. * 积分商品详情
  255. * @param Request $request
  256. * @param int $id
  257. * @return mixed
  258. * @throws \think\db\exception\DataNotFoundException
  259. * @throws \think\db\exception\DbException
  260. * @throws \think\db\exception\ModelNotFoundException
  261. */
  262. public function integralDetail(Request $request, int $id)
  263. {
  264. $storeInfo = $this->dao->getOne(['id' => $id], '*', ['getPrice']);
  265. if (!$storeInfo) {
  266. throw new AdminException(400533);
  267. } else {
  268. $storeInfo = $storeInfo->toArray();
  269. }
  270. $siteUrl = sys_config('site_url');
  271. $storeInfo['image'] = set_file_url($storeInfo['image'], $siteUrl);
  272. $storeInfo['image_base'] = set_file_url($storeInfo['image'], $siteUrl);
  273. $storeInfo['sale_stock'] = 0;
  274. if ($storeInfo['stock'] > 0) $storeInfo['sale_stock'] = 1;
  275. $uid = $request->uid();
  276. /** @var StoreDescriptionServices $storeDescriptionService */
  277. $storeDescriptionService = app()->make(StoreDescriptionServices::class);
  278. $storeInfo['description'] = $storeDescriptionService->getDescription(['product_id' => $id, 'type' => 4]);
  279. $data['storeInfo'] = get_thumb_water($storeInfo, 'big', ['image', 'images']);
  280. $storeInfoNew = get_thumb_water($storeInfo, 'small');
  281. $data['storeInfo']['small_image'] = $storeInfoNew['image'];
  282. /** @var StoreProductAttrServices $storeProductAttrServices */
  283. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  284. list($productAttr, $productValue) = $storeProductAttrServices->getProductAttrDetail($id, $uid, 0, 4, $storeInfo['product_id']);
  285. $data['productAttr'] = $productAttr;
  286. $data['productValue'] = $productValue;
  287. /** @var StoreVisitServices $storeVisit */
  288. $storeVisit = app()->make(StoreVisitServices::class);
  289. $storeVisit->setView($uid, $id, 'combination', $storeInfo['product_id'], 'view');
  290. $data['routine_contact_type'] = sys_config('routine_contact_type', 0);
  291. //浏览记录
  292. ProductLogJob::dispatch(['visit', ['uid' => $uid, 'product_id' => $storeInfo['product_id']]]);
  293. return $data;
  294. }
  295. /**
  296. * 修改销量和库存
  297. * @param $num
  298. * @param $integralId
  299. * @return bool
  300. */
  301. public function decIntegralStock(int $num, int $integralId, string $unique)
  302. {
  303. $product_id = $this->dao->value(['id' => $integralId], 'product_id');
  304. if ($unique) {
  305. /** @var StoreProductAttrValueServices $skuValueServices */
  306. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  307. //减去积分商品的sku库存增加销量
  308. $res = false !== $skuValueServices->decProductAttrStock($integralId, $unique, $num, 4);
  309. //减去积分商品库存
  310. $res = $res && $this->dao->decStockIncSales(['id' => $integralId, 'type' => 4], $num);
  311. //获取拼团的sku
  312. $sku = $skuValueServices->value(['product_id' => $integralId, 'unique' => $unique, 'type' => 4], 'suk');
  313. //减去当前普通商品sku的库存增加销量
  314. $res = $res && $skuValueServices->decStockIncSales(['product_id' => $product_id, 'suk' => $sku, 'type' => 0], $num);
  315. } else {
  316. $res = false !== $this->dao->decStockIncSales(['id' => $integralId, 'type' => 4], $num);
  317. }
  318. /** @var StoreProductServices $services */
  319. $services = app()->make(StoreProductServices::class);
  320. //减去普通商品库存
  321. $res = $res && $services->decProductStock($num, $product_id);
  322. return $res;
  323. }
  324. /**
  325. * 获取一条积分商品
  326. * @param $id
  327. * @return mixed
  328. */
  329. public function getIntegralOne($id)
  330. {
  331. return $this->dao->validProduct($id, '*');
  332. }
  333. /**
  334. * 验证积分商品下单库存限量
  335. * @param int $uid
  336. * @param int $integralId
  337. * @param int $num
  338. * @param string $unique
  339. * @return string
  340. * @throws \think\db\exception\DataNotFoundException
  341. * @throws \think\db\exception\DbException
  342. * @throws \think\db\exception\ModelNotFoundException
  343. */
  344. public function checkoutProductStock(int $uid, int $integralId, int $num = 1, string $unique = '')
  345. {
  346. /** @var StoreProductAttrValueServices $attrValueServices */
  347. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  348. if ($unique == '') {
  349. $unique = $attrValueServices->value(['product_id' => $integralId, 'type' => 4], 'unique');
  350. }
  351. $StoreIntegralInfo = $this->getIntegralOne($integralId);
  352. if (!$StoreIntegralInfo) {
  353. throw new ApiException(400093);
  354. }
  355. /** @var StoreIntegralOrderServices $orderServices */
  356. $orderServices = app()->make(StoreIntegralOrderServices::class);
  357. $userBuyCount = $orderServices->getBuyCount($uid, $integralId);
  358. if ($StoreIntegralInfo['once_num'] < $num && $StoreIntegralInfo['once_num'] != -1) {
  359. throw new ApiException(410313, ['num' => $StoreIntegralInfo['once_num']]);
  360. }
  361. if ($StoreIntegralInfo['num'] < ($userBuyCount + $num) && $StoreIntegralInfo['num'] != -1) {
  362. throw new ApiException(410298, ['num' => $StoreIntegralInfo['num']]);
  363. }
  364. $res = $attrValueServices->getOne(['product_id' => $integralId, 'unique' => $unique, 'type' => 4]);
  365. if ($num > $res['quota']) {
  366. throw new ApiException(410297, ['num' => $num]);
  367. }
  368. $product_stock = $attrValueServices->value(['product_id' => $StoreIntegralInfo['product_id'], 'suk' => $res['suk'], 'type' => 0], 'stock');
  369. if ($product_stock < $num) {
  370. throw new ApiException(410297, ['num' => $num]);
  371. }
  372. return $unique;
  373. }
  374. /**
  375. * 获取推荐积分商品
  376. * @return array
  377. * @throws \think\db\exception\DataNotFoundException
  378. * @throws \think\db\exception\DbException
  379. * @throws \think\db\exception\ModelNotFoundException
  380. */
  381. public function getIntegralList(array $where)
  382. {
  383. [$page, $limit] = $this->getPageValue();
  384. $list = $this->dao->getList($where, $page, $limit, 'id,image,title,price,sales');
  385. return $list;
  386. }
  387. }