GoodsService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 22/05/04
  6. * Time: 22:00
  7. */
  8. namespace App\Services\Api;
  9. use App\Models\Goods;
  10. use App\Models\GoodsAttr;
  11. use App\Models\GoodsCat;
  12. use App\Models\GoodsComment;
  13. use App\Models\GoodsLook;
  14. use App\Models\Order;
  15. use App\Models\OrderComment;
  16. use App\Models\User;
  17. use App\Models\UserCollect;
  18. use App\Models\UserMessage;
  19. use App\Models\UserSearchLog;
  20. use Illuminate\Support\Carbon;
  21. use Illuminate\Support\Facades\App;
  22. use Illuminate\Support\Facades\DB;
  23. class GoodsService
  24. {
  25. //获取商品属性 1产地,2所在地,3容量,4桶型,5级别,6批次,7风味,8品牌
  26. public static function getCatList()
  27. {
  28. $list = GoodsAttr::query()
  29. ->whereNull('deleted_at')
  30. ->select('id', 'type', 'name')
  31. ->orderBy('type')
  32. ->get();
  33. $list = $list->isNotEmpty() ? $list->toArray() : [];
  34. $catList = [];
  35. $catList['cat_list'] = GoodsCat::query()->where('status', 1)->whereNull('deleted_at')->select('id', 'name', 'pic_url')->get();
  36. foreach ($list as $key => $val) {
  37. switch ($val['type']) {
  38. case 1: //原产地
  39. $catList['origin_list'][] = $val;
  40. break;
  41. case 2: //所在地
  42. $catList['region_list'][] = $val;
  43. break;
  44. case 3: //容量
  45. $catList['capacity_list'][] = $val;
  46. break;
  47. case 4: //桶型
  48. $catList['barrel_list'][] = $val;
  49. break;
  50. case 5: //级别
  51. $catList['level_list'][] = $val;
  52. break;
  53. case 6: //批次
  54. $catList['batch_list'][] = $val;
  55. break;
  56. case 7: //风味
  57. $catList['flavor_list'][] = $val;
  58. break;
  59. case 8: //品牌
  60. $catList['brand_list'][] = $val;
  61. break;
  62. }
  63. }
  64. return $catList;
  65. }
  66. //添加发布商品
  67. public static function add($request, $userId)
  68. {
  69. $goods = App::make('getGoodsInstance');
  70. $goods->user_id = $userId;
  71. $goods->name = $request->name; //名称
  72. $goods->is_old = $request->is_old; //新旧
  73. $goods->cat_id = $request->cat_id; //类别
  74. $goods->brand_id = $request->brand_id; //品牌
  75. $goods->barrel_id = $request->barrel_id; //桶型
  76. $goods->capacity_id = $request->capacity_id; //容量
  77. $goods->origin_place_id = $request->origin_place_id; //原产地
  78. $goods->region_id = $request->region_id; //所在地
  79. $goods->level_id = $request->level_id; //等级
  80. $goods->batch_id = $request->batch_id; //批次
  81. $goods->flavor_id = $request->flavor_id; //风味
  82. $goods->alcohol_id = $request->alcohol_id; //酒精度
  83. $goods->tag = $request->tag; //标签
  84. $goods->notice = $request->notice; //提示
  85. $goods->introduce = $request->introduce; //介绍
  86. $goods->price = $request->price; //价格
  87. $goods->img = $request->img; //图片
  88. $goods->cover_imgs = $request->cover_imgs; //封面图
  89. $goods->product_year = $request->product_year; //产品年份
  90. $goods->distill_year = $request->distill_year; //蒸馏年份
  91. $goods->bottling_year = $request->bottling_year; //装瓶年份
  92. $goods->pre_seller = $request->pre_seller ?: 0; //上一卖家(新品上架不填,售卖仓库商品必填)
  93. $goods->trace_code = $request->trace_code ?: self::createTraceCode($userId); //溯源码 (新品上架自动生成,售卖仓库商品填商品的)
  94. if (!$goods->save()) {
  95. return false;
  96. }
  97. return true;
  98. }
  99. //生成溯源码
  100. public static function createTraceCode()
  101. {
  102. return md5(date('YmdHis') . rand(1000, 9999));
  103. }
  104. //获取商品列表
  105. public static function getGoodsList($map = [], $page = 1, $limit = null, $ids = [])
  106. {
  107. $limit = $limit ?: 20;
  108. $offset = ($page - 1) * $limit;
  109. $list = Goods::query()
  110. ->when($map, function ($query, $map) {
  111. $query->where($map);
  112. })
  113. ->when($ids, function ($query, $ids) {
  114. $query->whereIn('id', $ids);
  115. })
  116. ->select('id', 'name', 'img', 'integral')
  117. ->offset($offset)
  118. ->limit($limit)
  119. ->get();
  120. if($list->isEmpty()){
  121. return [];
  122. }
  123. $list->makeHidden(['created_at', 'updated_at', 'deleted_at']);
  124. return $list->toArray();
  125. }
  126. //获取详情
  127. public static function getDetail($goodsId)
  128. {
  129. $goods = Goods::query()
  130. ->where('id', $goodsId)
  131. ->whereNull('deleted_at')
  132. ->first();
  133. $goods->makeHidden(['created_at', 'updated_at', 'deleted_at']);
  134. $goods = $goods->toArray();
  135. $goods['cover_imgs'] = json_decode($goods['cover_imgs'], true);
  136. $goods['specific_list'] = json_decode($goods['specific_list'], true);
  137. return $goods;
  138. }
  139. //获取历史购买者信息
  140. public static function getHistoryBuyers($goodsId)
  141. {
  142. $list = Order::query()
  143. ->with('users:id,username,avatar')
  144. ->where('goods_id', $goodsId)
  145. ->whereNull('deleted_at')
  146. ->select('user_id', 'unit_price', 'created_at')
  147. ->get();
  148. foreach ($list as $key => $val) {
  149. $val['date'] = date('Y-m-d', strtotime($val['created_at']));
  150. unset($val['created_at']);
  151. $list[$key] = $val;
  152. }
  153. return $list;
  154. }
  155. //获取历史价格纪录(需要修改)
  156. public static function getHistoryPrices($goodsId)
  157. {
  158. $list = Order::query()->where('goods_id', $goodsId)->whereNull('deleted_at')->select('unit_price', 'created_at')->get();
  159. foreach ($list as $key => $val) {
  160. $val['date'] = date('Y-m-d', strtotime($val['created_at']));
  161. unset($val['created_at']);
  162. $list[$key] = $val;
  163. }
  164. return $list;
  165. }
  166. //获取商品收藏记录
  167. public static function getGoodsCollects($goodsId)
  168. {
  169. $date = Carbon::parse()->subDays(7)->toDateTimeString();
  170. $list = UserCollect::query()
  171. ->with('users:id,username,avatar')
  172. ->where('goods_id', $goodsId)
  173. ->where('status', 1)
  174. ->where('created_at', '>', $date)
  175. ->select('user_id', 'created_at')
  176. ->get();
  177. foreach ($list as $key => $val) {
  178. $val['date'] = date('Y-m-d', strtotime($val['created_at']));
  179. unset($val['created_at']);
  180. $list[$key] = $val;
  181. }
  182. $totalCount = UserCollect::query()->where('goods_id', $goodsId)->where('status', 1)->count('id');
  183. $result = [
  184. 'list' => $list,
  185. 'seven_count' => count($list),
  186. 'total_count' => $totalCount
  187. ];
  188. return $result;
  189. }
  190. //获取用户浏览记录(七日)
  191. public static function getGoodsLooks($goods_id)
  192. {
  193. $date = Carbon::parse()->subDays(7)->toDateTimeString();
  194. $list = GoodsLook::query()
  195. ->where('goods_id', $goods_id)
  196. ->select(DB::raw('DATE(created_at) as date'), DB::raw('COUNT(id) as amount'))
  197. ->groupBy('date')
  198. ->having('date', '>', $date)
  199. ->orderBy('date', 'ASC')
  200. ->get();
  201. $list = $list->isNotEmpty() ? $list->toArray() : [];
  202. $sevenCount = array_sum(array_column($list, 'amount'));
  203. for ($i = 0; $i < 7; $i++) {
  204. $day = Carbon::parse('-' . $i . ' days')->toDateString();
  205. $arr = ['date' => $day, 'amount' => 0];
  206. foreach ($list as $key => $val) {
  207. if ($day == $val['date']) {
  208. $arr['amount'] = $val['amount'];
  209. }
  210. }
  211. $arr['date'] = date('d', strtotime($day));
  212. $res[] = $arr;
  213. }
  214. $result = [
  215. 'list' => $res,
  216. 'seven_count' => $sevenCount
  217. ];
  218. return $result;
  219. }
  220. //商品 收藏/取消
  221. public static function collectGoods($goodsId, $userId)
  222. {
  223. DB::beginTransaction();
  224. try {
  225. $collect = UserCollect::query()->where(['user_id' => $userId, 'goods_id' => $goodsId])->first();
  226. if ($collect) {
  227. $status = $collect->status;
  228. $collect->status = $status ? 0 : 1;
  229. $collect->save();
  230. if ($status) {
  231. self::collectNumDec($goodsId);
  232. UserService::collectNumDec($userId);
  233. } else {
  234. self::collectNumInc($goodsId);
  235. UserService::collectNumInc($userId);
  236. }
  237. } else {
  238. $collect = App::make('getUserCollectInstance');
  239. $collect->user_id = $userId;
  240. $collect->goods_id = $goodsId;
  241. $collect->status = 1;
  242. $collect->save();
  243. self::collectNumInc($goodsId); //商品被收藏数 +1
  244. UserService::collectNumInc($userId); //用户收藏商品数 +1
  245. }
  246. DB::commit();
  247. return true;
  248. } catch (\Exception $exception) {
  249. DB::rollBack();
  250. ErrorMsgServive::write($exception, request()->fullUrl());
  251. return false;
  252. }
  253. }
  254. //收藏数+1
  255. public static function collectNumInc($goodsId)
  256. {
  257. Goods::query()->where('id', $goodsId)->increment('collect_num');
  258. }
  259. //收藏数-1
  260. public static function collectNumDec($goodsId)
  261. {
  262. Goods::query()->where('id', $goodsId)->decrement('collect_num');
  263. }
  264. //增加曝光量
  265. public static function addOpenNum($goodsId)
  266. {
  267. Goods::query()->where('id', $goodsId)->increment('open_num');
  268. }
  269. //增加浏览记录
  270. public static function addGoodsLook($userId, $goodsId)
  271. {
  272. $goodsLook = App::make('getGoodsLookInstance');
  273. $goodsLook->user_id = $userId;
  274. $goodsLook->goods_id = $goodsId;
  275. $goodsLook->save();
  276. }
  277. //商品是否可买
  278. public static function isGoodsCanBuy($goodsId)
  279. {
  280. $info = Goods::query()->where('id', $goodsId)->where('status', 1)->whereNull('deleted_at')->first();
  281. return empty($info) ? false : $info;
  282. }
  283. //通过商品名 模糊搜索商品id
  284. public static function getGoodsIds($keyword)
  285. {
  286. $ids = Goods::query()
  287. ->where('name', 'like', '%' . $keyword . '%')
  288. ->pluck('id');
  289. return $ids->isNotEmpty() ? array_unique($ids->toArray()) : [];
  290. }
  291. //我售卖的商品id
  292. public static function mySellGoodsIds($userId)
  293. {
  294. $ids = Goods::query()
  295. ->where('user_id', $userId)
  296. ->pluck('id');
  297. return $ids->isNotEmpty() ? array_unique($ids->toArray()) : [];
  298. }
  299. //添加评论
  300. public static function addComment($request, $userId)
  301. {
  302. $goods = Goods::find($request->goods_id);
  303. $comment = App::make('getGoodsCommentInstance');
  304. $comment->user_id = $userId;
  305. $comment->goods_id = $request->goods_id;
  306. $comment->goods_user_id = $goods->user_id; //售卖商品的人
  307. $comment->score = $request->score;
  308. $comment->content = $request->content;
  309. $comment->pic_url = $request->pic_url;
  310. if (!$comment->save()) {
  311. return false;
  312. }
  313. //商品被评价进行消息通知
  314. $type = 'comment';
  315. $user = User::find($userId);
  316. $content = $user->username.'评价了您的商品';
  317. MessageService::sendNotice($goods->user_id, $userId, $type, UserMessage::TYPE[$type], $content);
  318. return true;
  319. }
  320. //获取评论列表 $type: 1全部,2买家评论,卖家评论
  321. public static function getCommentList($userId, $type = null, $page = 1)
  322. {
  323. $limit = 20;
  324. $offset = ($page - 1) * $limit;
  325. $list = GoodsComment::query()
  326. ->with('users:id,username,name,avatar')
  327. ->with('goods:id,name,img,price')
  328. ->where('user_id', $userId)
  329. ->where('is_show', 1)
  330. ->offset($offset)
  331. ->limit($limit)
  332. ->orderByDesc('is_top')
  333. ->get();
  334. foreach ($list as $key => $val) {
  335. $format = CommonService::formatTime($val['created_at']);
  336. $val['date_format_1'] = $format['date_format_1'];
  337. $val['date_format_2'] = $format['date_format_2'];
  338. $val['date_format_3'] = $format['date_format_3'];
  339. $list[$key] = $val;
  340. }
  341. return $list;
  342. }
  343. }