GoodsCategoryService.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace biz\goods\services;
  3. use biz\base\services\BaseService;
  4. use biz\goods\classes\GoodsCategoryClass;
  5. use biz\goods\classes\GoodsClass;
  6. use Yii;
  7. use linslin\yii2\curl;
  8. class GoodsCategoryService extends BaseService
  9. {
  10. public static $baseFile = '\biz\goods\classes\GoodsCategoryClass';
  11. //查询一个分类下的商品,有分页 shish 2019.12.2
  12. public static function getGoodsList($where)
  13. {
  14. $data = GoodsCategoryClass::getList('*', $where, 'inTurn DESC');
  15. $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
  16. if (empty($list)) {
  17. return $data;
  18. }
  19. $ids = array_column($list, 'gId');
  20. $list = GoodsService::getAllByCondition(['id' => ['in', $ids]], 'inTurn DESC', '*');
  21. $data['list'] = GoodsClass::groupGoodsBaseInfo($list);
  22. return $data;
  23. }
  24. //查询指定分类下的商品,没有分页,首页使用 shish 2019.12.2
  25. public static function getAppointCategoryList($category)
  26. {
  27. $ids = array_column($category, 'id');
  28. //取出分类下的商品id
  29. $list = GoodsCategoryClass::getAllByCondition(['cId' => ['in', $ids]], 'inTurn DESC,addTime DESC', '*');
  30. if (empty($list)) {
  31. foreach ($category as $key => $val) {
  32. $category[$key]['goodsInfoList'] = [];
  33. }
  34. return $category;
  35. }
  36. $goodsList = [];
  37. $categoryGoods = [];
  38. foreach ($list as $val) {
  39. $goodsList[] = $val['gId'];
  40. $cId = $val['cId'];
  41. $categoryGoods[$cId][] = $val['gId'];
  42. }
  43. //查出商品信息
  44. $goodsInfoList = GoodsService::getAllByCondition(['id' => ['in', $goodsList]], 'inTurn DESC', '*', 'id');
  45. $goodsInfoList = GoodsClass::groupGoodsBaseInfo($goodsInfoList);
  46. //每个分类取的商品数
  47. $getNum = [6, 2, 4];
  48. $showRowNum = [2, 1, 2];
  49. foreach ($category as $key => $val) {
  50. $id = $val['id'];
  51. //当前分类下有哪些商品id,并组合商品信息
  52. $categoryGoodsIdList = isset($categoryGoods[$id]) ? $categoryGoods[$id] : [];
  53. $info = [];
  54. if (!empty($categoryGoodsIdList)) {
  55. $currentGetNum = isset($getNum[$key]) ? $getNum[$key] : 2;
  56. $categoryGoodsIdList = array_slice($categoryGoodsIdList, 0, $currentGetNum);
  57. foreach ($categoryGoodsIdList as $currentGoodsId) {
  58. if (isset($goodsInfoList[$currentGoodsId])) {
  59. $info[] = $goodsInfoList[$currentGoodsId];
  60. }
  61. }
  62. }
  63. $category[$key]['goodsInfoList'] = $info;
  64. $category[$key]['showRow'] = isset($showRowNum[$key]) ? $showRowNum[$key] : 2;
  65. }
  66. return $category;
  67. }
  68. //获取商品所属的分类id shish 201917
  69. public static function getGoodsHasCategoryIdList($goodsId)
  70. {
  71. $categoryIdList = GoodsCategoryClass::getAllByCondition(['gId' => $goodsId], null, 'cId', 'cId');
  72. if (empty($categoryIdList)) {
  73. return [];
  74. }
  75. $ids = array_keys($categoryIdList);
  76. return $ids;
  77. }
  78. }