GoodsCategoryService.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace bizHd\goods\services;
  3. use bizHd\base\services\BaseService;
  4. use bizHd\goods\classes\GoodsCategoryClass;
  5. use bizHd\goods\classes\GoodsClass;
  6. class GoodsCategoryService extends BaseService
  7. {
  8. public static $baseFile = '\bizHd\goods\classes\GoodsCategoryClass';
  9. //获取商品所属的分类id ssh 201917
  10. public static function getGoodsHasCategoryIds($goodsId)
  11. {
  12. $categoryIdList = GoodsCategoryClass::getAllByCondition(['gId' => $goodsId], null, 'cId', 'cId');
  13. if (empty($categoryIdList)) {
  14. return [];
  15. }
  16. $ids = array_keys($categoryIdList);
  17. return $ids;
  18. }
  19. //所有商品
  20. public static function getGoodsAll($where)
  21. {
  22. $list = GoodsCategoryClass::getAllList('*', $where, 'inTurn DESC');
  23. $ids = array_column($list, 'gId');
  24. $list = GoodsService::getAllByCondition(['id' => ['in', $ids]], null, '*');
  25. $data = GoodsClass::groupGoodsBaseInfo($list);
  26. return $data;
  27. }
  28. //获取商家下所有启用的商品分类
  29. public static function getEnableGoodsCategory($mainId)
  30. {
  31. $cat = CategoryService::getEnableList($mainId);
  32. $where = ['mainId' => $mainId, 'cId' => ['in', array_column($cat, 'id')]];
  33. $data = self::getAllList('cId as classId,gId', $where);
  34. return $data;
  35. }
  36. //获取常用分类的 goods id lqh 2021.04.09
  37. public static function getOftenIds($mainId)
  38. {
  39. $data = GoodsClass::getLimitList("id", ['mainId' => $mainId], 10, 'actualSold desc');
  40. if ($data) {
  41. $data = array_column($data, 'id');
  42. }
  43. return $data;
  44. }
  45. public static function assembleData($classIds, $goodsCateData, $goodsData)
  46. {
  47. $classMapItem = [];
  48. if ($goodsData) {
  49. foreach ($goodsCateData as $v) {
  50. $classMapItem[$v['classId']][] = $v['gId'];
  51. }
  52. }
  53. $classData = [];
  54. foreach ($classIds as $v) {
  55. $classId = $v['id'];
  56. if ($classId > 0) {
  57. $tmp = [];
  58. $tmp['classId'] = $classId;
  59. $tmp['className'] = $v['name'];
  60. $items = $classMapItem[$classId] ?? [];
  61. if ($items) {
  62. if ($goodsData) {
  63. foreach ($goodsData as $v) {
  64. if (in_array($v['id'], $items)) {
  65. $v['classId'] = $classId; //前端要求增加classId 2021.1.31 lqh
  66. $tmp['child'][] = $v;
  67. }
  68. }
  69. } else {
  70. $tmp['child'] = [];
  71. }
  72. } else {
  73. $tmp['child'] = [];
  74. }
  75. //去除空分类
  76. if (empty($tmp['child'])) {
  77. continue;
  78. }
  79. $classData[] = $tmp;
  80. }
  81. }
  82. return $classData;
  83. }
  84. }