GoodsService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\GoodsModel;
  13. use App\Models\StoreModel;
  14. use App\Models\GoodsSkuModel;
  15. use App\Services\BaseService;
  16. use App\Services\RedisService;
  17. use Illuminate\Support\Facades\DB;
  18. /**
  19. * 商品管理-服务类
  20. * @author laravel开发员
  21. * @since 2020/11/11
  22. * @package App\Services\Common
  23. */
  24. class GoodsService extends BaseService
  25. {
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * AdService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new GoodsModel();
  35. }
  36. /**
  37. * 列表(商品审核列表)
  38. * @param $params
  39. * @param int $pageSize
  40. * @return array
  41. */
  42. public function getDataList($params, $pageSize = 15)
  43. {
  44. $query = $this->getQuery($params);
  45. $list = $query
  46. ->with(['store', 'category'])
  47. ->where(function ($query) use ($params) {
  48. if (isset($params['store_id']) && $params['store_id'] > 0) {
  49. $query->where('store_id', $params['store_id']);
  50. }
  51. })
  52. ->orderBy('create_time', 'desc')
  53. ->orderBy('id', 'desc')
  54. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  55. $list = $list ? $list->toArray() : [];
  56. if ($list && isset($list['data'])) {
  57. foreach ($list['data'] as &$item) {
  58. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  59. $item['status_text'] = $this->getStatusText($item['status'] ?? 1);
  60. // 商家名称
  61. if (isset($item['store']) && !empty($item['store'])) {
  62. $item['store_name'] = $item['store']['name'] ?? '平台商品';
  63. } else {
  64. $item['store_name'] = $item['store_id'] > 0 ? '未知商家' : '平台商品';
  65. }
  66. // 分类名称
  67. if (isset($item['category']) && !empty($item['category'])) {
  68. $item['category_name'] = $item['category']['name'] ?? '未分类';
  69. } else {
  70. $item['category_name'] = '未分类';
  71. }
  72. // 商品图片
  73. if (isset($item['thumb'])) {
  74. $item['thumb'] = get_image_url($item['thumb']);
  75. }
  76. // 计算总库存(单规格直接取stock,多规格需要计算skus的stock总和)
  77. if (isset($item['sku_type']) && $item['sku_type'] == 2) {
  78. // 多规格,需要查询skus表
  79. $skuModel = new GoodsSkuModel();
  80. $totalStock = $skuModel->where(['goods_id' => $item['id'], 'mark' => 1, 'status' => 1])
  81. ->sum('stock');
  82. $item['total_stock'] = $totalStock ?? 0;
  83. } else {
  84. // 单规格
  85. $item['total_stock'] = $item['stock'] ?? 0;
  86. }
  87. }
  88. }
  89. return [
  90. 'pageSize' => $pageSize,
  91. 'total' => isset($list['total']) ? $list['total'] : 0,
  92. 'list' => isset($list['data']) ? $list['data'] : []
  93. ];
  94. }
  95. /**
  96. * 获取状态文本
  97. * @param int $status
  98. * @return string
  99. */
  100. private function getStatusText($status)
  101. {
  102. $statusMap = [
  103. 1 => '已发布',
  104. 2 => '待发布',
  105. 3 => '待审核',
  106. 4 => '审核失败'
  107. ];
  108. return isset($statusMap[$status]) ? $statusMap[$status] : '未知';
  109. }
  110. /**
  111. * 查询
  112. * @param $params
  113. * @return \Illuminate\Database\Eloquent\Builder
  114. */
  115. public function getQuery($params)
  116. {
  117. $where = ['mark' => 1];
  118. // 审核状态筛选(1-发布,2-待发布,3-待审核,4-审核失败)
  119. $status = isset($params['status']) ? intval($params['status']) : 0;
  120. if ($status > 0) {
  121. $where['status'] = $status;
  122. }
  123. // 多商户支持:如果当前登录用户关联了stores表,则只查询该商家的商品
  124. if (isset($params['store_id']) && $params['store_id'] > 0) {
  125. $where['store_id'] = $params['store_id'];
  126. }
  127. // 分类筛选
  128. $categoryId = isset($params['category_id']) ? intval($params['category_id']) : 0;
  129. if ($categoryId > 0) {
  130. $where['category_id'] = $categoryId;
  131. }
  132. $model = $this->model->where($where);
  133. // 搜索条件
  134. $model->where(function ($query) use ($params) {
  135. // 商品名称搜索
  136. $goodsName = isset($params['goods_name']) ? trim($params['goods_name']) : '';
  137. if ($goodsName) {
  138. $query->where('goods_name', 'like', "%{$goodsName}%");
  139. }
  140. // 商家名称搜索(通过关联stores表)
  141. $storeName = isset($params['store_name']) ? trim($params['store_name']) : '';
  142. if ($storeName) {
  143. $query->whereHas('store', function ($q) use ($storeName) {
  144. $q->where('name', 'like', "%{$storeName}%");
  145. });
  146. }
  147. });
  148. return $model;
  149. }
  150. /**
  151. * 添加或编辑
  152. * @return array
  153. * @since 2020/11/11
  154. * @author laravel开发员
  155. */
  156. public function edit()
  157. {
  158. $data = request()->all();
  159. // 图片处理
  160. if (isset($data['thumb'])) {
  161. $data['thumb'] = get_image_path($data['thumb']);
  162. }
  163. // 相册处理
  164. if (isset($data['albums']) && is_array($data['albums'])) {
  165. $data['albums'] = json_encode($data['albums']);
  166. }
  167. // 验证商品名称
  168. if (empty($data['goods_name'])) {
  169. return message('请填写商品名称', false);
  170. }
  171. // 验证分类
  172. if (!isset($data['category_id']) || $data['category_id'] <= 0) {
  173. return message('请选择商品分类', false);
  174. }
  175. // 多商户支持:如果当前登录用户关联了stores表,自动设置store_id
  176. if (isset($data['store_id']) && $data['store_id'] > 0) {
  177. $data['store_id'] = $data['store_id'];
  178. } else {
  179. $data['store_id'] = 0;
  180. }
  181. // 设置默认值
  182. if (!isset($data['sku_type'])) {
  183. $data['sku_type'] = 1; // 默认单规格
  184. }
  185. if (!isset($data['status'])) {
  186. $data['status'] = 3; // 默认待审核
  187. }
  188. if (!isset($data['sort'])) {
  189. $data['sort'] = 0;
  190. }
  191. if (!isset($data['price'])) {
  192. $data['price'] = 0.00;
  193. }
  194. if (!isset($data['stock'])) {
  195. $data['stock'] = 0;
  196. }
  197. if (!isset($data['weight'])) {
  198. $data['weight'] = 0.00;
  199. }
  200. // 保存商品
  201. $result = parent::edit($data);
  202. // 如果是多规格商品,需要处理规格数据
  203. if ($result && isset($result['code']) && $result['code'] == 0 && isset($result['data']['id'])) {
  204. $goodsId = $result['data']['id'];
  205. // 如果是多规格商品,处理规格数据
  206. if (isset($data['sku_type']) && $data['sku_type'] == 2) {
  207. if (isset($data['skus']) && is_array($data['skus']) && !empty($data['skus'])) {
  208. $this->saveGoodsSkus($goodsId, $data['skus']);
  209. }
  210. }
  211. }
  212. return $result;
  213. }
  214. /**
  215. * 保存商品规格
  216. * @param int $goodsId
  217. * @param array $skus
  218. * @return bool
  219. */
  220. protected function saveGoodsSkus($goodsId, $skus)
  221. {
  222. // 先删除旧的规格(软删除)
  223. GoodsSkuModel::where('goods_id', $goodsId)->update(['mark' => 0, 'update_time' => time()]);
  224. $now = time();
  225. $skuModel = new GoodsSkuModel();
  226. foreach ($skus as $sku) {
  227. if (isset($sku['id']) && $sku['id'] > 0) {
  228. // 更新
  229. $skuModel->where('id', $sku['id'])->update([
  230. 'goods_id' => $goodsId,
  231. 'sku_name' => $sku['sku_name'] ?? '',
  232. 'price' => $sku['price'] ?? 0.00,
  233. 'stock' => $sku['stock'] ?? 0,
  234. 'sort' => $sku['sort'] ?? 0,
  235. 'status' => $sku['status'] ?? 1,
  236. 'update_time' => $now,
  237. 'mark' => 1
  238. ]);
  239. } else {
  240. // 新增
  241. $skuModel->insert([
  242. 'goods_id' => $goodsId,
  243. 'sku_name' => $sku['sku_name'] ?? '',
  244. 'price' => $sku['price'] ?? 0.00,
  245. 'stock' => $sku['stock'] ?? 0,
  246. 'sort' => $sku['sort'] ?? 0,
  247. 'status' => $sku['status'] ?? 1,
  248. 'create_time' => $now,
  249. 'update_time' => $now,
  250. 'mark' => 1
  251. ]);
  252. }
  253. }
  254. return true;
  255. }
  256. /**
  257. * 审核商品
  258. * @param int $userId 操作人ID
  259. * @param array $params 审核参数
  260. * @return bool
  261. */
  262. public function confirm($userId, $params)
  263. {
  264. $id = isset($params['id']) ? intval($params['id']) : 0;
  265. $status = isset($params['status']) ? intval($params['status']) : 0;
  266. $remark = isset($params['remark']) ? trim($params['remark']) : '';
  267. if (!$id) {
  268. $this->error = '商品ID不能为空';
  269. return false;
  270. }
  271. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  272. if (!$info) {
  273. $this->error = '商品信息不存在';
  274. return false;
  275. }
  276. // 多商户支持:检查权限
  277. if (isset($params['store_id']) && $params['store_id'] > 0) {
  278. $storeId = $params['store_id'];
  279. } else {
  280. $storeId = 0;
  281. }
  282. if ($storeId > 0 && $info->store_id != $storeId) {
  283. $this->error = '无权操作此商品';
  284. return false;
  285. }
  286. // 审核通过
  287. if ($status == 1) {
  288. $updateData = [
  289. 'status' => 1, // 1-发布
  290. 'confirm_remark' => $remark ?: '审核通过',
  291. 'update_time' => time()
  292. ];
  293. $this->model->where('id', $id)->update($updateData);
  294. $this->error = '审核通过成功';
  295. return true;
  296. }
  297. // 审核驳回
  298. elseif ($status == 4) {
  299. if (empty($remark)) {
  300. $this->error = '驳回理由不能为空';
  301. return false;
  302. }
  303. $this->model->where('id', $id)->update([
  304. 'status' => 4, // 4-审核失败
  305. 'confirm_remark' => $remark,
  306. 'update_time' => time()
  307. ]);
  308. $this->error = '审核驳回成功';
  309. return true;
  310. } else {
  311. $this->error = '审核状态参数错误';
  312. return false;
  313. }
  314. }
  315. /**
  316. * 获取商品详情(重写父类方法)
  317. * @return array
  318. */
  319. public function info()
  320. {
  321. // 记录ID
  322. $id = request()->input("id", 0);
  323. $info = [];
  324. if ($id) {
  325. // 获取商品基本信息(使用关联查询)
  326. $goods = $this->model->with(['store', 'category', 'skus'])
  327. ->where(['id' => $id, 'mark' => 1])
  328. ->first();
  329. if ($goods) {
  330. // 多商户支持:检查权限
  331. $storeId = isset($info['store_id']) ? intval($info['store_id']) : 0;
  332. if ($storeId > 0 && $goods->store_id != $storeId) {
  333. return message('无权查看此商品', false);
  334. }
  335. $info = $goods->toArray();
  336. // 处理图片
  337. if (isset($info['thumb'])) {
  338. $info['thumb'] = get_image_url($info['thumb']);
  339. }
  340. // 处理相册
  341. if (isset($info['albums']) && !empty($info['albums'])) {
  342. $albums = json_decode($info['albums'], true);
  343. if (is_array($albums)) {
  344. foreach ($albums as &$album) {
  345. $album = get_image_url($album);
  346. }
  347. $info['albums'] = $albums;
  348. }
  349. }
  350. // 处理时间
  351. if (isset($info['create_time'])) {
  352. $info['create_time'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  353. }
  354. // 获取分类信息
  355. if (isset($info['category']) && !empty($info['category'])) {
  356. $info['category_name'] = $info['category']['name'] ?? '未分类';
  357. } else {
  358. $info['category_name'] = '未分类';
  359. }
  360. // 获取商家信息
  361. if (isset($info['store']) && !empty($info['store'])) {
  362. $info['store_name'] = $info['store']['name'] ?? '平台商品';
  363. } else {
  364. $info['store_name'] = $info['store_id'] > 0 ? '未知商家' : '平台商品';
  365. }
  366. // 添加状态文本
  367. $info['status_text'] = $this->getStatusText($info['status'] ?? 1);
  368. }
  369. }
  370. return message(MESSAGE_OK, true, $info);
  371. }
  372. }