StoreManageServices.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 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\admin;
  12. use app\services\activity\coupon\StoreCouponIssueServices;
  13. use app\services\activity\coupon\StoreCouponUserServices;
  14. use app\services\BaseServices;
  15. use app\services\order\StoreOrderCreateServices;
  16. use app\services\order\StoreOrderRefundServices;
  17. use app\services\order\StoreOrderServices;
  18. use app\services\product\product\StoreCategoryServices;
  19. use app\services\product\product\StoreProductLabelServices;
  20. use app\services\product\product\StoreProductServices;
  21. use app\services\product\sku\StoreProductAttrValueServices;
  22. use app\services\system\SystemUserLevelServices;
  23. use app\services\user\UserLabelRelationServices;
  24. use app\services\user\UserBillServices;
  25. use app\services\user\UserGroupServices;
  26. use app\services\user\UserLabelCateServices;
  27. use app\services\user\UserMoneyServices;
  28. use app\services\user\UserRechargeServices;
  29. use app\services\user\UserServices;
  30. use app\services\user\UserVisitServices;
  31. use crmeb\exceptions\ApiException;
  32. class StoreManageServices extends BaseServices
  33. {
  34. /**
  35. * 商家统计
  36. * @return array
  37. * @author wuhaotian
  38. * @email 442384644@qq.com
  39. * @date 2025/11/13
  40. */
  41. public function statistics()
  42. {
  43. $storeOrderServices = app()->make(StoreOrderServices::class);
  44. $userVisitServices = app()->make(UserVisitServices::class);
  45. $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
  46. $storeProductServices = app()->make(StoreProductServices::class);
  47. // 今日订单金额,去除用户取消,删除的订单,并且是已支付的订单
  48. $todayOrderPrice = $storeOrderServices->sum([
  49. ['add_time', '>', strtotime(date('Y-m-d'))],
  50. ['is_del', '=', 0],
  51. ['is_cancel', '=', 0],
  52. ['paid', '=', 1],
  53. ['pid', '>=', 0],
  54. ], 'pay_price', false);
  55. // 今日订单总数,去除用户取消,删除的订单
  56. $todayOrderCount = $storeOrderServices->count([
  57. ['add_time', '>', strtotime(date('Y-m-d'))],
  58. ['is_del', '=', 0],
  59. ['is_cancel', '=', 0],
  60. ['pid', '>=', 0],
  61. ]);
  62. // 今日支付人数,去除用户取消,删除的订单,并且是已支付的订单,去重
  63. $todayOrderUserCount = $storeOrderServices->getDistinctCount([
  64. ['add_time', '>', strtotime(date('Y-m-d'))],
  65. ['is_del', '=', 0],
  66. ['is_cancel', '=', 0],
  67. ['paid', '=', 1],
  68. ['pid', '<=', 0],
  69. ], 'uid', false);
  70. // 今日浏览量
  71. $todayVisitCount = $userVisitServices->count([['add_time', '>', strtotime(date('Y-m-d'))]]);
  72. // 待发货的订单数量,全部时间
  73. $unDeliveryOrderCount = $storeOrderServices->count(['status' => 1, 'shipping_type' => 1, 'pid' => 0]);
  74. // 今日退款申请数量,全部时间
  75. $refundingCount = $storeOrderRefundServices->count(['is_cancel' => 0, 'refund_type' => [1, 2, 4, 5]]);
  76. // 已售罄的商品数量
  77. $outOfStock = $storeProductServices->getCount(['type' => 4]);
  78. // 警戒库存商品数量
  79. $policeForce = $storeProductServices->getCount(['type' => 5, 'store_stock' => sys_config('store_stock') > 0 ? sys_config('store_stock') : 2]);
  80. return compact('todayOrderCount', 'todayOrderPrice', 'todayOrderUserCount', 'todayVisitCount', 'unDeliveryOrderCount', 'refundingCount', 'outOfStock', 'policeForce');
  81. }
  82. /**
  83. * 商品列表
  84. * @param $where
  85. * @return array
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\DbException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. * @author wuhaotian
  90. * @email 442384644@qq.com
  91. * @date 2025/11/13
  92. */
  93. public function product($where)
  94. {
  95. $storeProductServices = app()->make(StoreProductServices::class);
  96. $where['is_del'] = 0;
  97. return $storeProductServices->getList($where);
  98. }
  99. /**
  100. * 商品上下架
  101. * @param $id
  102. * @param $isShow
  103. * @return bool
  104. * @author wuhaotian
  105. * @email 442384644@qq.com
  106. * @date 2025/11/13
  107. */
  108. public function productShow($id, $isShow)
  109. {
  110. $storeProductServices = app()->make(StoreProductServices::class);
  111. $del = $storeProductServices->value(['id' => $id], 'is_del');
  112. if ($del == 1) throw new ApiException('商品已删除');
  113. $storeProductServices->setShow([$id], $isShow);
  114. return true;
  115. }
  116. /**
  117. * 商品标签列表
  118. * @return array
  119. * @author wuhaotian
  120. * @email 442384644@qq.com
  121. * @date 2025/11/13
  122. */
  123. public function productLabel()
  124. {
  125. $storeProductLabelServices = app()->make(StoreProductLabelServices::class);
  126. return $storeProductLabelServices->labelUseList();
  127. }
  128. /**
  129. * 商品标签保存
  130. * @param $ids
  131. * @param $label_list
  132. * @return bool
  133. * @throws \Exception
  134. * @author wuhaotian
  135. * @email 442384644@qq.com
  136. * @date 2025/11/13
  137. */
  138. public function saveProductLabel($ids, $label_list)
  139. {
  140. $data['ids'] = $ids;
  141. $data['label_list'] = $label_list;
  142. $data['type'] = 9;
  143. $storeProductServices = app()->make(StoreProductServices::class);
  144. $storeProductServices->batchSetting($data);
  145. return true;
  146. }
  147. /**
  148. * 商品分类列表
  149. * @return array
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\DbException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. * @author wuhaotian
  154. * @email 442384644@qq.com
  155. * @date 2025/11/13
  156. */
  157. public function productCate()
  158. {
  159. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  160. return $storeCategoryServices->cascaderList(1, 1);
  161. }
  162. /**
  163. * 商品分类保存
  164. * @param $ids
  165. * @param $cate_id
  166. * @return bool
  167. * @throws \Exception
  168. * @author wuhaotian
  169. * @email 442384644@qq.com
  170. * @date 2025/11/13
  171. */
  172. public function saveProductCate($ids, $cate_id)
  173. {
  174. $data['ids'] = $ids;
  175. $data['cate_id'] = $cate_id;
  176. $data['type'] = 1;
  177. $storeProductServices = app()->make(StoreProductServices::class);
  178. $storeProductServices->batchSetting($data);
  179. return true;
  180. }
  181. /**
  182. * 商品属性
  183. * @param $id
  184. * @return array
  185. * @author wuhaotian
  186. * @email 442384644@qq.com
  187. * @date 2025/11/13
  188. */
  189. public function productAttr($id)
  190. {
  191. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  192. return $storeProductAttrValueServices->selectList(['product_id' => $id, 'type' => 0])->toArray();
  193. }
  194. /**
  195. * 商品属性保存
  196. * @param $id
  197. * @param $attr_value
  198. * @return bool|\think\Response
  199. * @author wuhaotian
  200. * @email 442384644@qq.com
  201. * @date 2025/11/13
  202. */
  203. public function saveProductAttr($id, $attr_value)
  204. {
  205. $storeProductServices = app()->make(StoreProductServices::class);
  206. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  207. if (!$id) {
  208. return app('json')->fail('请选择商品');
  209. }
  210. if (!$attr_value) {
  211. return app('json')->fail('请填写属性值');
  212. }
  213. //判断规格的属性值是否存在
  214. $requiredKeys = ['unique', 'price', 'stock', 'cost', 'ot_price'];
  215. foreach ($attr_value as $attr) {
  216. $missingKeys = array_diff($requiredKeys, array_keys($attr));
  217. if (!empty($missingKeys)) {
  218. throw new ApiException('请重新修改规格库存');
  219. }
  220. }
  221. $product_stock = $product_price = $product_ot_price = $product_cost = 0;
  222. $attrs = $storeProductAttrValueServices->selectList(['product_id' => $id, 'type' => 0])->toArray();
  223. $attr_value = array_combine(array_column($attr_value, 'unique'), $attr_value);
  224. foreach ($attrs as $item) {
  225. $attr = $attr_value[$item['unique']] ?? [];
  226. if ($attr) {
  227. $storeProductAttrValueServices->update($item['id'], [
  228. 'price' => $attr['price'],
  229. 'stock' => $attr['stock'],
  230. 'cost' => $attr['cost'],
  231. 'ot_price' => $attr['ot_price']
  232. ]);
  233. }
  234. $product_array = $attr ?: $item;
  235. // 计算商品库存
  236. $product_stock = bcadd((string)$product_stock, (string)$product_array['stock'], 0);
  237. // 更新商品价格
  238. $product_price = max($product_price, $product_array['price']);
  239. // 更新商品原价
  240. $product_ot_price = max($product_ot_price, $product_array['ot_price']);
  241. // 更新商品成本
  242. $product_cost = max($product_cost, $product_array['cost']);
  243. }
  244. // 修改商品库存等信息
  245. $storeProductServices->update($id, [
  246. 'stock' => $product_stock,
  247. 'price' => $product_price,
  248. 'ot_price' => $product_ot_price,
  249. 'cost' => $product_cost,
  250. ]);
  251. return true;
  252. }
  253. /**
  254. * 商品创建
  255. * @param $data
  256. * @return bool
  257. * @author wuhaotian
  258. * @email 442384644@qq.com
  259. * @date 2025/12/9
  260. */
  261. public function createProduct($data)
  262. {
  263. $data['attr']['brokerage'] = 0;
  264. $data['attr']['brokerage_two'] = 0;
  265. $data['attr']['vip_price'] = 0;
  266. $data['attr']['virtual_list'] = [];
  267. $data['attr']['coupon_id'] = 0;
  268. $saveData = [
  269. 'virtual_type' => 0,
  270. 'cate_id' => $data['cate_id'],
  271. 'store_name' => $data['store_name'],
  272. 'keyword' => '',
  273. 'unit_name' => $data['unit_name'],
  274. 'store_info' => '',
  275. 'slider_image' => $data['slider_image'],
  276. 'video_open' => 0,
  277. 'video_link' => '',
  278. 'spec_type' => 0,
  279. 'items' => [],
  280. 'attrs' => [$data['attr']],
  281. 'description' => $data['content'],
  282. 'description_images' => [],
  283. 'logistics' => $data['logistics'],
  284. 'freight' => $data['freight'],
  285. 'postage' => $data['postage'],
  286. 'temp_id' => $data['temp_id'],
  287. 'give_integral' => 0,
  288. 'presale' => 0,
  289. 'presale_time' => 0,
  290. 'presale_day' => 1,
  291. 'vip_product' => 0,
  292. 'vip_product_type' => 0,
  293. 'is_sub' => [],
  294. 'recommend' => [],
  295. 'activity' => ['默认', '秒杀', '砍价', '拼团'],
  296. 'recommend_list' => [],
  297. 'coupon_ids' => [],
  298. 'label_id' => [],
  299. 'command_word' => '',
  300. 'is_show' => 0,
  301. 'ficti' => 0,
  302. 'sort' => 0,
  303. 'recommend_image' => '',
  304. 'sales' => 0,
  305. 'custom_form' => [],
  306. 'type' => 0,
  307. 'is_copy' => 0,
  308. 'is_limit' => 0,
  309. 'limit_type' => 0,
  310. 'limit_num' => 0,
  311. 'min_qty' => 1,
  312. 'params_list' => [],
  313. 'label_list' => [],
  314. 'protection_list' => [],
  315. 'is_gift' => 0,
  316. 'gift_price' => 0,
  317. ];
  318. $storeProductServices = app()->make(StoreProductServices::class);
  319. $storeProductServices->save(0, $saveData);
  320. return true;
  321. }
  322. /**
  323. * 用户列表
  324. * @param $where
  325. * @return array
  326. * @author wuhaotian
  327. * @email 442384644@qq.com
  328. * @date 2025/11/17
  329. */
  330. public function user($where)
  331. {
  332. $userServices = app()->make(UserServices::class);
  333. return $userServices->index($where);
  334. }
  335. /**
  336. * 用户信息详情
  337. * @param $uid
  338. * @return mixed
  339. * @throws \think\db\exception\DataNotFoundException
  340. * @throws \think\db\exception\DbException
  341. * @throws \think\db\exception\ModelNotFoundException
  342. * @author wuhaotian
  343. * @email 442384644@qq.com
  344. * @date 2025/11/17
  345. */
  346. public function userInfo($uid)
  347. {
  348. $userServices = app()->make(UserServices::class);
  349. $userInfo = $userServices->get($uid);
  350. if (!$userInfo) throw new ApiException('用户不存在');
  351. $userInfo = $userInfo->toArray();
  352. $userInfo['avatar'] = set_file_url($userInfo['avatar']);
  353. $userInfo['birthday'] = $userInfo['birthday'] != 0 ? date('Y-m-d', $userInfo['birthday']) : '';
  354. // 优惠券数量
  355. $userInfo['coupon_num'] = app()->make(StoreCouponUserServices::class)->getUserValidCouponCount((int)$uid);
  356. // 用户标签
  357. $label_list = app()->make(UserLabelRelationServices::class)->getUserLabelList([$uid]);
  358. $label_id = [];
  359. $userInfo['label_list'] = '';
  360. if ($label_list) {
  361. $userInfo['label_list'] = implode(',', array_column($label_list, 'label_name'));
  362. foreach ($label_list as $item) {
  363. $label_id[] = [
  364. 'id' => $item['label_id'],
  365. 'label_name' => $item['label_name']
  366. ];
  367. }
  368. }
  369. $userInfo['label_id'] = $label_id;
  370. // 用户订单金额及数量
  371. $orderServices = app()->make(StoreOrderServices::class);
  372. $userInfo['order_total_price'] = $orderServices->sum(['uid' => $uid, 'paid' => 1, 'refund_status' => 0], 'pay_price');
  373. $userInfo['order_total_count'] = $orderServices->count(['uid' => $uid, 'paid' => 1, 'refund_status' => 0]);
  374. // 会员
  375. $userInfo['isMember'] = $userInfo['is_money_level'] > 0 ? 1 : 0;
  376. if ($userInfo['is_ever_level'] == 1) {
  377. $userInfo['svip_overdue_time'] = $userInfo['svip_over_day'] = '永久';
  378. } else {
  379. if ($userInfo['is_money_level'] > 0 && $userInfo['overdue_time'] > 0) {
  380. $userInfo['svip_over_day'] = ceil(($userInfo['overdue_time'] - time()) / 86400);
  381. $userInfo['svip_overdue_time'] = date('Y-m-d', $userInfo['overdue_time']);
  382. }
  383. }
  384. return $userInfo;
  385. }
  386. /**
  387. * 用户分组
  388. * @return array
  389. * @throws \think\db\exception\DataNotFoundException
  390. * @throws \think\db\exception\DbException
  391. * @throws \think\db\exception\ModelNotFoundException
  392. * @author wuhaotian
  393. * @email 442384644@qq.com
  394. * @date 2025/11/17
  395. */
  396. public function userGroup()
  397. {
  398. $userGroupServices = app()->make(UserGroupServices::class);
  399. return $userGroupServices->getGroupList();
  400. }
  401. /**
  402. * 用户等级
  403. * @return array
  404. * @throws \think\db\exception\DataNotFoundException
  405. * @throws \think\db\exception\DbException
  406. * @throws \think\db\exception\ModelNotFoundException
  407. * @author wuhaotian
  408. * @email 442384644@qq.com
  409. * @date 2025/11/17
  410. */
  411. public function userLevel()
  412. {
  413. $systemUserLevelServices = app()->make(SystemUserLevelServices::class);
  414. return $systemUserLevelServices->getLevelList([], 'id,name,icon,image');
  415. }
  416. /**
  417. * 用户标签
  418. * @param $uid
  419. * @return array
  420. * @throws \think\db\exception\DataNotFoundException
  421. * @throws \think\db\exception\DbException
  422. * @throws \think\db\exception\ModelNotFoundException
  423. * @author wuhaotian
  424. * @email 442384644@qq.com
  425. * @date 2025/11/17
  426. */
  427. public function userLabel($uid)
  428. {
  429. $userLabelCateServices = app()->make(UserLabelCateServices::class);
  430. return $userLabelCateServices->getUserLabel($uid);
  431. }
  432. /**
  433. * 用户优惠券
  434. * @param $where
  435. * @return mixed
  436. * @author wuhaotian
  437. * @email 442384644@qq.com
  438. * @date 2025/11/17
  439. */
  440. public function userCoupon($where)
  441. {
  442. if ($where['uid'] == 0) {
  443. $page = $where['page'] ?? 1;
  444. $limit = $where['limit'] ?? 10;
  445. unset($where['page'], $where['limit'], $where['uid']);
  446. $where['receive_types'] = 3;
  447. $where['is_del'] = 0;
  448. $where['status'] = 1;
  449. return app()->make(StoreCouponIssueServices::class)->getList($where, $page, $limit);
  450. } else {
  451. return app()->make(StoreCouponUserServices::class)->getUserCouponList($where['uid'])['list'];
  452. }
  453. }
  454. /**
  455. * 用户数据修改
  456. * @param $uid
  457. * @param $data
  458. * @return bool
  459. * @throws \think\Exception
  460. * @throws \think\db\exception\DataNotFoundException
  461. * @throws \think\db\exception\DbException
  462. * @throws \think\db\exception\ModelNotFoundException
  463. * @author wuhaotian
  464. * @email 442384644@qq.com
  465. * @date 2025/11/17
  466. */
  467. public function userUpdate($uid, $data)
  468. {
  469. $userServices = app()->make(UserServices::class);
  470. $userInfo = $userServices->getUserInfo($uid);
  471. switch ($data['type']) {
  472. case 0: // 余额
  473. /** @var UserMoneyServices $userMoneyServices */
  474. $userMoneyServices = app()->make(UserMoneyServices::class);
  475. if ($data['status'] == 1) { //增加
  476. $edit['now_money'] = bcadd($userInfo['now_money'], $data['number'], 2);
  477. $userMoneyServices->income('system_add', $uid, $data['number'], $edit['now_money'], 0, '移动端商家管理增加余额');
  478. //增加充值记录
  479. $recharge_data = [
  480. 'order_id' => app()->make(StoreOrderCreateServices::class)->getNewOrderId('cz'),
  481. 'uid' => $uid,
  482. 'price' => $data['number'],
  483. 'recharge_type' => 'system',
  484. 'paid' => 1,
  485. 'add_time' => time(),
  486. 'give_price' => 0,
  487. 'channel_type' => 'system',
  488. 'pay_time' => time(),
  489. ];
  490. app()->make(UserRechargeServices::class)->save($recharge_data);
  491. } else { //减少
  492. if ($userInfo['now_money'] > $data['number']) {
  493. $edit['now_money'] = bcsub($userInfo['now_money'], $data['number'], 2);
  494. } else {
  495. $edit['now_money'] = 0;
  496. $data['number'] = $userInfo['now_money'];
  497. }
  498. $userMoneyServices->income('system_sub', $uid, $data['number'], $edit['now_money'], 0, '移动端商家管理减少余额');
  499. }
  500. $userServices->update($uid, $edit);
  501. break;
  502. case 1: // 积分
  503. /** @var UserBillServices $userBill */
  504. $userBill = app()->make(UserBillServices::class);
  505. $integral_data = ['link_id' => 0, 'number' => $data['number']];
  506. if ($data['status'] == 1) { //增加
  507. $edit['integral'] = bcadd($userInfo['integral'], $data['number'], 2);
  508. $integral_data['balance'] = $edit['integral'];
  509. $integral_data['title'] = '系统增加积分';
  510. $integral_data['mark'] = '系统增加了' . floatval($data['number']) . '积分';
  511. $userBill->incomeIntegral($uid, 'system_add', $integral_data);
  512. } else { //减少
  513. $edit['integral'] = bcsub($userInfo['integral'], $data['number'], 2);
  514. $integral_data['balance'] = $edit['integral'];
  515. $integral_data['title'] = '系统减少积分';
  516. $integral_data['mark'] = '系统扣除了' . floatval($data['number']) . '积分';
  517. $userBill->expendIntegral($uid, 'system_sub', $integral_data);
  518. }
  519. $userServices->update($uid, $edit);
  520. break;
  521. case 2: // 等级
  522. $userServices->saveGiveLevel((int)$uid, (int)$data['level']);
  523. break;
  524. case 3: // 付费会员
  525. $userServices->saveGiveLevelTime((int)$uid, (int)$data['days']);
  526. break;
  527. case 4: // 优惠券
  528. /** @var StoreCouponIssueServices $issueService */
  529. $issueService = app()->make(StoreCouponIssueServices::class);
  530. $coupon = $issueService->get($data['coupon_id']);
  531. if (!$coupon) {
  532. throw new ApiException('优惠券不存在');
  533. } else {
  534. $coupon = $coupon->toArray();
  535. }
  536. $issueService->setCoupon($coupon, [$uid]);
  537. break;
  538. case 5: // 分组
  539. $userServices->saveSetGroup([$uid], $data['group_id']);
  540. break;
  541. case 6: // 用户标签
  542. $userServices->saveSetLabel([$uid], $data['label_id'], 0);
  543. break;
  544. }
  545. return true;
  546. }
  547. }