ShopGoodsLogic.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\admin\logic;
  3. use app\admin\model\dao\ShopGoodsSpec;
  4. use app\admin\model\dao\ShopGoodsSpecRelation;
  5. use app\admin\model\dao\ShopGoodsSpecType;
  6. use app\admin\model\dao\ShopGoodsType;
  7. use app\common\model\ShopGoods;
  8. use think\db\Query;
  9. use think\facade\Cache;
  10. use think\facade\Db;
  11. /**
  12. * 商城商品
  13. * Class ShopGoodsLogic
  14. * @package app\admin\logic
  15. */
  16. class ShopGoodsLogic
  17. {
  18. /**
  19. * 数量统计
  20. * @return mixed
  21. * @throws \think\db\exception\DbException
  22. */
  23. public static function getCount()
  24. {
  25. $cacheKey = "caches:goods:counts";
  26. if (!Cache::has($cacheKey)) {
  27. $data = ShopGoods::where(['on_sale' => 1])->count('goods_id');
  28. if ($data) {
  29. Cache::set($cacheKey, $data, rand(3, 5));
  30. }
  31. return $data;
  32. }
  33. return Cache::get($cacheKey);
  34. }
  35. public static function getList($page, $limit, $where)
  36. {
  37. $model = new ShopGoods();
  38. $count = $model
  39. ->withJoin(['supplier', 'cate' => function (Query $query) {
  40. $query->where('cate.id', '>', 0);
  41. }], 'LEFT')
  42. ->where($where)
  43. ->count();
  44. $list = $model
  45. ->withJoin(['supplier', 'cate' => function (Query $query) {
  46. $query->where('cate.id', '>', 0);
  47. }], 'LEFT')
  48. ->where($where)
  49. ->page($page, $limit)
  50. ->order('goods_id desc')
  51. ->select()->toArray();
  52. foreach ($list as $k => &$v) {
  53. $v['id'] = $v['goods_id'];
  54. $v['box_pic'] = isset($v['box_pic'])? getPreviewUrl($v['box_pic']):'';
  55. $v['goods_img'] = $v['goods_img']? getPreviewUrl($v['goods_img']):'';
  56. $v['goods_type'] = ShopGoodsTypeLogic::getNameByCache($v['goods_type']);
  57. }
  58. return [$count, $list];
  59. }
  60. public static function getGoodsOrEmpty($id)
  61. {
  62. }
  63. public static function getGoodsBase($id)
  64. {
  65. $model = new ShopGoods();
  66. $data = $model->where(['goods_id' => $id])->findOrEmpty()->toArray();
  67. // if ($data['goods_img_banner']) {
  68. // $data['goods_img_banner'] = implode('|', json_decode($data['goods_img_banner'], true));
  69. // }
  70. $data['goods_img'] = $data['goods_img']? getPreviewUrl($data['goods_img']):'';
  71. $data['box_pic'] = $data['box_pic']? getPreviewUrl($data['box_pic']):'';
  72. $banners = $data['goods_img_banner']? json_decode($data['goods_img_banner'], true): [];
  73. if($banners){
  74. foreach ($banners as &$v){
  75. $v = getPreviewUrl($v);
  76. }
  77. }
  78. $data['goods_img_banner'] = $banners?implode('|',$banners):'';
  79. $data['goods_remark'] = getContent(htmlspecialchars_decode($data['goods_remark']));
  80. $specRelation = ShopGoodsSpecRelation::getArrListByGoodsId($id);
  81. $specAttrId = isset($specRelation[0])?ShopGoodsSpecType::getPidById($specRelation[0]['spec_id']):0;
  82. if ($data['is_exist_many_spec'] == 0) {
  83. $data['defaultSku'] = ShopGoodsSpec::getGoodsSpecByGoodId($id);
  84. if(empty($data['defaultSku'])){
  85. $data['defaultSku'] = [
  86. 'price'=> 0.00,
  87. 'original_price'=>0.00,
  88. 'cost_price'=>0.00,
  89. 'stock'=> 0,
  90. ];
  91. }
  92. }
  93. $hot_keywords = ShopHotKeywordsLogic::getCacheList();
  94. $shop_supplier = ShopSupplierLogic::getCacheList();
  95. $shop_goods_type = ShopGoodsTypeLogic::getCacheList();
  96. return [$data, $hot_keywords, $shop_goods_type, $shop_supplier, $specAttrId];
  97. }
  98. }