GoodsService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\GoodsModel;
  13. use App\Services\BaseService;
  14. /**
  15. * 商品管理-服务类
  16. * @author laravel开发员
  17. * @since 2020/11/11
  18. * Class GoodsService
  19. * @package App\Services\Common
  20. */
  21. class GoodsService extends BaseService
  22. {
  23. // 静态对象
  24. protected static $instance = null;
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * GoodsService constructor.
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new GoodsModel();
  34. }
  35. /**
  36. * 静态入口
  37. * @return static|null
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = (new static());
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * 列表数据
  48. * @param $params
  49. * @param int $pageSize
  50. * @return array
  51. */
  52. public function getDataList($params, $pageSize = 15)
  53. {
  54. $where = ['a.mark' => 1];
  55. $status = isset($params['status'])? $params['status'] : 0;
  56. if($status>0){
  57. $where['a.status'] = $status;
  58. }
  59. $list = $this->model->from('goods as a')
  60. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  61. ->leftJoin('shop as c', 'c.id', '=', 'a.shop_id')
  62. ->where($where)
  63. ->where(function ($query) use($params){
  64. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  65. if($keyword){
  66. $query->where('a.goods_name','like',"%{$keyword}%")->orWhere('c.name','like',"%{$keyword}%")->orWhere('b.nickname','like',"%{$keyword}%")->orWhere('b.mobile','like',"%{$keyword}%");
  67. }
  68. })
  69. ->select(['a.*','b.nickname','b.mobile as mobile','c.name as shop_name'])
  70. ->orderBy('a.id','desc')
  71. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  72. $list = $list? $list->toArray() :[];
  73. if($list){
  74. foreach($list['data'] as &$item){
  75. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  76. $item['thumb'] = isset($item['thumb']) && $item['thumb']? get_image_url($item['thumb']) : '';
  77. }
  78. }
  79. return [
  80. 'pageSize'=> $pageSize,
  81. 'total'=>isset($list['total'])? $list['total'] : 0,
  82. 'list'=> isset($list['data'])? $list['data'] : []
  83. ];
  84. }
  85. /**
  86. * 添加编辑
  87. * @return array
  88. * @since 2020/11/11
  89. * @author laravel开发员
  90. */
  91. public function edit()
  92. {
  93. // 请求参数
  94. $data = request()->all();
  95. // thumb处理
  96. $thumb = isset($data['thumb'])? trim($data['thumb']) : '';
  97. if ($thumb && strpos($thumb, "temp")) {
  98. $data['thumb'] = save_image($thumb, 'member');
  99. } else if($thumb){
  100. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  101. }
  102. return parent::edit($data); // TODO: Change the autogenerated stub
  103. }
  104. /**
  105. * 验证是否抢购有新的商品
  106. * @param $userId
  107. * @return mixed
  108. */
  109. public function checkNewGoods($userId)
  110. {
  111. return $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])
  112. ->where('create_time','>=', strtotime(date('Y-m-d')))
  113. ->count('id');
  114. }
  115. }