You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Service\v3\Implementations;
  3. use App\Constants\v3\Goods as GoodsConstants;
  4. use App\Constants\v3\Store as StoreConstants;
  5. use App\Model\v3\Market;
  6. use App\Model\v3\Store;
  7. use App\Model\v3\Goods;
  8. use App\Service\v3\Interfaces\StoreServiceInterface;
  9. use Hyperf\Utils\ApplicationContext;
  10. class StoreService implements StoreServiceInterface
  11. {
  12. public function do()
  13. {
  14. // TODO: Implement do() method.
  15. }
  16. public function check($storeId)
  17. {
  18. $store = Store::query()->where([
  19. ['id','=',$storeId],
  20. ['is_rest','=',0],
  21. ['status','=',2]
  22. ])
  23. ->select('time1','time2','time3','time4')
  24. ->first();
  25. if(!empty($store)){
  26. $nowTime = time();
  27. $startTime = strtotime(($store->time1));
  28. if(!empty($store->time3) && !empty($store->time4)){
  29. $time4 = strtotime(($store->time4));
  30. $endTime = $time4;
  31. }else{
  32. $time2 = strtotime(($store->time2));
  33. $endTime = $time2;
  34. }
  35. if($nowTime > $startTime && $nowTime < $endTime){
  36. return true;
  37. }
  38. }
  39. return false;
  40. }
  41. public function undo()
  42. {
  43. // TODO: Implement undo() method.
  44. }
  45. public function detail($storeId)
  46. {
  47. return Store::query()
  48. ->with('market')
  49. ->where('id',$storeId)
  50. ->first();
  51. }
  52. public function getList($marketId,$ids = [], $page=1, $pagesize=10)
  53. {
  54. $market = Market::query()->withoutGlobalScope('normal')->find($marketId);
  55. $builder = Store::query()->where('market_id',$marketId);
  56. if(!is_null($ids)){
  57. $builder->whereNotIn('id',$ids);
  58. }
  59. $paginate = $builder->orderBy('is_rest', 'asc')->paginate($pagesize);
  60. $stores = $paginate->toArray();
  61. $market->stores = $stores['data'];
  62. $newIds = array_values(array_column($stores['data'], 'id'));
  63. if(!is_null($ids)){
  64. $newIds = array_merge($newIds,$ids);
  65. }
  66. return ['has_more_pages' => $paginate->hasMorePages(), 'market' => $market,'ids' => $newIds];
  67. }
  68. public function getListByMarketId($marketId, $page=1, $pagesize=10)
  69. {
  70. $storeTable = ApplicationContext::getContainer()->get(Store::class)->getTable();
  71. $goodsTable = ApplicationContext::getContainer()->get(Goods::class)->getTable();
  72. $builder = Store::query()
  73. ->select(''.$storeTable.'.*')
  74. ->join($goodsTable,''.$storeTable.'.id', '=', ''.$goodsTable.'.store_id')
  75. ->where([''.$goodsTable.'.on_sale' => GoodsConstants::ON_SALE_YES])
  76. ->where([''.$goodsTable.'.market_id' => $marketId])
  77. ->where(function ($query) use ($goodsTable) {
  78. $query->where(''.$goodsTable.'.inventory', '>', 0)->orWhere(''.$goodsTable.'.is_infinite', '=', 1);
  79. })
  80. ->whereRaw(''.$goodsTable.'.deleted_at IS NULL')
  81. ->where([''.$storeTable.'.market_id' => $marketId])
  82. ->orderBy($storeTable.'.is_rest','asc');;
  83. $paginate = $builder->groupBy(''.$storeTable.'.id')->orderByDesc($storeTable.'.sales')->paginate($pagesize);
  84. $stores = $paginate->map(function ($item, $key) {
  85. $item->goods = Goods::query()
  86. ->where('store_id',$item->id)
  87. ->where(function ($query){
  88. $query->where('inventory', '>', 0)->orWhere('is_infinite', '=', 1);
  89. })
  90. ->limit(5)->get();
  91. return $item;
  92. })->all();
  93. return ['has_more_pages' => $paginate->hasMorePages(), 'stores' => $stores];
  94. }
  95. public function updateIsRest($storeId)
  96. {
  97. $store = Store::query()->withoutGlobalScope('normal')->find($storeId);
  98. $isRest = $store->is_rest + 1;
  99. $store->is_rest = $isRest % 2;
  100. return $store->save();
  101. }
  102. }