ShopGoodsLogic.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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['cate'] = ShopCategoryLogic::getCateById($v['category']);
  54. $v['id'] = $v['goods_id'];
  55. // $v['supplier'] = Db::name('shop_supplier')->where(['id'=>$v['supplier']])->value('name');
  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_remark'] = htmlspecialchars_decode($data['goods_remark']);
  71. $specRelation = ShopGoodsSpecRelation::getArrListByGoodsId($id);
  72. $specAttrId = ShopGoodsSpecType::getPidById($specRelation[0]['spec_id']);
  73. if ($data['is_exist_many_spec'] == 0) {
  74. $data['defaultSku'] = ShopGoodsSpec::getGoodsSpecByGoodId($id);
  75. }
  76. $hot_keywords = ShopHotKeywordsLogic::getCacheList();
  77. $shop_supplier = ShopSupplierLogic::getCacheList();
  78. $shop_goods_type = ShopGoodsTypeLogic::getCacheList();
  79. return [$data, $hot_keywords, $shop_goods_type, $shop_supplier, $specAttrId];
  80. }
  81. }