GoodsService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. use App\Services\RedisService;
  15. use wxkxklmyt\Scws;
  16. /**
  17. * 商品管理-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * @package App\Services\Common
  21. */
  22. class GoodsService extends BaseService
  23. {
  24. /**
  25. * 构造函数
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. * AdService constructor.
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new GoodsModel();
  33. }
  34. /**
  35. * 列表
  36. * @param $params
  37. * @param int $pageSize
  38. * @return array
  39. */
  40. public function getDataList($params, $pageSize = 15)
  41. {
  42. $query = $this->getQuery($params);
  43. $list = $query->select(['a.*'])
  44. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  45. $list = $list? $list->toArray() :[];
  46. if($list){
  47. foreach($list['data'] as &$item){
  48. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  49. $item['discount_start_time'] = $item['discount_start_time']? date('Y-m-d H:i:s', $item['discount_start_time']):'';
  50. $item['discount_end_time'] = $item['discount_end_time']? date('Y-m-d H:i:s', $item['discount_end_time']):'';
  51. }
  52. }
  53. return [
  54. 'pageSize'=> $pageSize,
  55. 'total'=>isset($list['total'])? $list['total'] : 0,
  56. 'list'=> isset($list['data'])? $list['data'] : []
  57. ];
  58. }
  59. /**
  60. * 查询
  61. * @param $params
  62. * @return \Illuminate\Database\Eloquent\Builder
  63. */
  64. public function getQuery($params)
  65. {
  66. $where = ['a.status'=>1,'a.mark' => 1];
  67. $status = isset($params['status'])? $params['status'] : 1;
  68. if($status>0){
  69. $where['a.status'] = $status;
  70. }else{
  71. unset($where['a.status']);
  72. }
  73. $model = $this->model->with(['category'])->from('goods as a')
  74. ->leftJoin('goods_categorys as b','b.cate_id','=','a.cate_id')
  75. ->where($where)
  76. ->where(function($query) use($params){
  77. // 分类
  78. $cateId = isset($params['cate_id'])? $params['cate_id'] : 0;
  79. if($cateId){
  80. $query->where(function($query) use($cateId){
  81. $prefix = env('DB_PREFIX','lev_');
  82. $query->where('a.cate_id', $cateId)
  83. ->orWhereRaw("FIND_IN_SET({$cateId},{$prefix}b.pids)");
  84. });
  85. }
  86. // 品牌
  87. $brandId = isset($params['brand_id'])? $params['brand_id'] : 0;
  88. if($brandId>0){
  89. $query->where('a.brand_id', $brandId);
  90. }
  91. $type = isset($params['type'])? $params['type'] : 0;
  92. if($type==2){
  93. $query->where('a.is_discount', 1)->where(function($query){
  94. $query->where('a.discount_start_time', '<=', time())
  95. ->where('a.discount_end_time', '>=', time())
  96. ->orWhere('a.discount_end_time', '<=', 0);
  97. });
  98. }
  99. })
  100. ->where(function ($query) use($params){
  101. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  102. if($keyword){
  103. $scws = new Scws();
  104. $kws = $scws->scws($keyword,3,false);
  105. if(count($kws) >1){
  106. $query->where(function ($query) use ($kws) {
  107. foreach ($kws as $kw) {
  108. $kw = trim($kw);
  109. $query->where('a.goods_name', 'like', "%{$kw}%")
  110. ->where('a.keywords', 'like', "%{$kw}%");
  111. }
  112. });
  113. }else{
  114. $query->where(function ($query) use ($keyword){
  115. $query->where('a.goods_name','like',"%{$keyword}%")
  116. ->orWhere('a.goods_pinyin','like',"%{$keyword}%")
  117. ->orWhere('a.keywords', 'like', "%{$keyword}%");
  118. });
  119. }
  120. }
  121. });
  122. return $model;
  123. }
  124. /**
  125. * 导入
  126. */
  127. public function import()
  128. {
  129. }
  130. /**
  131. * 添加或编辑
  132. * @return array
  133. * @since 2020/11/11
  134. * @author laravel开发员
  135. */
  136. public function edit()
  137. {
  138. $data = request()->all();
  139. // 图片处理
  140. if(isset($data['thumb'])){
  141. $data['thumb'] = get_image_path($data['thumb']);
  142. }
  143. // 折扣时间
  144. if (isset($data['discount_start_time'])) {
  145. $data['discount_start_time'] = strtotime($data['discount_start_time']);
  146. }
  147. if (isset($data['discount_end_time'])) {
  148. $data['discount_end_time'] = strtotime($data['discount_end_time']);
  149. }
  150. return parent::edit($data); // TODO: Change the autogenerated stub
  151. }
  152. }