ScoreGoodsService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Models\ScoreGoodsModel;
  14. use App\Services\BaseService;
  15. /**
  16. * 积分商品管理-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * Class ScoreGoodsService
  20. * @package App\Services\Common
  21. */
  22. class ScoreGoodsService extends BaseService
  23. {
  24. // 静态对象
  25. protected static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. * ScoreGoodsService constructor.
  31. */
  32. public function __construct()
  33. {
  34. $this->model = new ScoreGoodsModel();
  35. }
  36. /**
  37. * 静态入口
  38. * @return static|null
  39. */
  40. public static function make()
  41. {
  42. if (!self::$instance) {
  43. self::$instance = (new static());
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * 列表数据
  49. * @param $params
  50. * @param int $pageSize
  51. * @return array
  52. */
  53. public function getDataList($params, $pageSize = 15)
  54. {
  55. $where = ['a.mark' => 1];
  56. $status = isset($params['status'])? $params['status'] : 0;
  57. if($status>0){
  58. $where['a.status'] = $status;
  59. }
  60. $list = $this->model->from('score_goods as a')
  61. ->leftJoin('score_goods_cate as c', 'c.id', '=', 'a.cate_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}%");
  67. }
  68. })
  69. ->select(['a.*','c.name as cate_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. if(!isset($data['code']) || empty($data['code'])){
  103. $data['code'] = isset($data['id'])&&$data['id']? 'G'.$data['id'] :'G'.(ScoreGoodsModel::max('id')+1);
  104. }
  105. return parent::edit($data); // TODO: Change the autogenerated stub
  106. }
  107. }