CouponService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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\CouponModel;
  13. use App\Services\BaseService;
  14. /**
  15. * 优惠券管理-服务类
  16. * @author laravel开发员
  17. * @since 2020/11/11
  18. * Class CouponService
  19. * @package App\Services\Common
  20. */
  21. class CouponService extends BaseService
  22. {
  23. // 静态对象
  24. protected static $instance = null;
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. */
  30. public function __construct()
  31. {
  32. $this->model = new CouponModel();
  33. }
  34. /**
  35. * 静态入口
  36. */
  37. public static function make()
  38. {
  39. if (!self::$instance) {
  40. self::$instance = new static();
  41. }
  42. return self::$instance;
  43. }
  44. /**
  45. * 列表
  46. * @param $params
  47. * @param int $pageSize
  48. * @return array
  49. */
  50. public function getDataList($params, $pageSize = 15)
  51. {
  52. $where = ['a.mark' => 1];
  53. $list = $this->model->from('coupons as a')
  54. ->where($where)
  55. ->where(function ($query) use($params){
  56. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  57. if($keyword){
  58. $query->where('a.name','like',"%{$keyword}%");
  59. }
  60. })
  61. ->where(function ($query) use($params){
  62. $status = isset($params['status'])? $params['status'] : 0;
  63. if($status>0 && is_array($status)){
  64. $query->whereIn('a.status', $status);
  65. }else if($status){
  66. $query->where('a.status', $status);
  67. }
  68. $type = isset($params['type'])? $params['type'] : 0;
  69. if($type>0){
  70. $query->where('a.type', $type);
  71. }
  72. $couponType = isset($params['coupon_type'])? $params['coupon_type'] : 0;
  73. if($couponType>0){
  74. $query->where('a.coupon_type', $couponType);
  75. }
  76. })
  77. ->select(['a.*'])
  78. ->orderBy('a.create_time','desc')
  79. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  80. $list = $list? $list->toArray() :[];
  81. if($list){
  82. foreach($list['data'] as &$item){
  83. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  84. $item['start_time'] = $item['start_time']? datetime($item['start_time'],'Y-m-d H.i.s') : '';
  85. $item['end_time'] = $item['end_time']? datetime($item['end_time'],'Y-m-d H.i.s') : '';
  86. $item['icon'] = $item['icon']? get_image_url($item['icon']) : '';
  87. if($item['end_time'] && $item['end_time'] < date('Y-m-d H.i.s')){
  88. $item['status'] = 2;
  89. }
  90. }
  91. }
  92. return [
  93. 'pageSize'=> $pageSize,
  94. 'total'=>isset($list['total'])? $list['total'] : 0,
  95. 'list'=> isset($list['data'])? $list['data'] : []
  96. ];
  97. }
  98. /**
  99. * 编辑
  100. * @return array
  101. */
  102. public function edit()
  103. {
  104. $data = request()->post();
  105. $data['icon'] = isset($data['icon']) && $data['icon']? get_image_path($data['icon']) : '';
  106. $data['expire_day'] = isset($data['expire_day']) && $data['expire_day']>0? intval($data['expire_day']) : 7;
  107. $data['start_time'] = isset($data['start_time']) && $data['start_time']? strtotime($data['start_time']) : time();
  108. $data['end_time'] = intval($data['start_time'] + $data['expire_day']*86400);
  109. return parent::edit($data);
  110. }
  111. }