ScoreGoodsService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. $item['qrcode'] = isset($item['qrcode']) && $item['qrcode']? get_image_url($item['qrcode']) : '';
  78. $albums = isset($item['albums']) && $item['albums']? json_decode($item['albums'], true) : [];
  79. $item['albums'] = get_images_preview($albums);
  80. $thumbs = $item['thumb']? [['url'=>$item['thumb']]]:[];
  81. $item['thumbs'] = array_merge($thumbs, $item['albums']);
  82. }
  83. }
  84. return [
  85. 'pageSize'=> $pageSize,
  86. 'total'=>isset($list['total'])? $list['total'] : 0,
  87. 'list'=> isset($list['data'])? $list['data'] : []
  88. ];
  89. }
  90. /**
  91. * 添加编辑
  92. * @return array
  93. * @since 2020/11/11
  94. * @author laravel开发员
  95. */
  96. public function edit()
  97. {
  98. // 请求参数
  99. $data = request()->all();
  100. // thumb处理
  101. $thumb = isset($data['thumb'])? trim($data['thumb']) : '';
  102. if ($thumb && strpos($thumb, "temp")) {
  103. $data['thumb'] = save_image($thumb, 'images');
  104. } else if($thumb){
  105. $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']);
  106. }
  107. // 收款码
  108. $qrcode = isset($data['qrcode'])? trim($data['qrcode']) : '';
  109. if ($qrcode && strpos($qrcode, "temp")) {
  110. $data['qrcode'] = save_image($qrcode, 'images');
  111. } else if($qrcode){
  112. $data['qrcode'] = str_replace(IMG_URL, "", $data['qrcode']);
  113. }
  114. if(!isset($data['code']) || empty($data['code'])){
  115. $data['code'] = isset($data['id'])&&$data['id']? 'G'.$data['id'] :'G'.(ScoreGoodsModel::max('id')+1);
  116. }
  117. $albums = isset($data['albums'])? $data['albums'] : [];
  118. if($albums){
  119. foreach($albums as &$v){
  120. $v['url'] = get_image_path($v['url']);
  121. }
  122. $data['albums'] = json_encode($albums, 256);
  123. }else{
  124. $data['albums'] = '';
  125. }
  126. return parent::edit($data); // TODO: Change the autogenerated stub
  127. }
  128. }