FreightSpecService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\FreightPackageModel;
  13. use App\Models\FreightSpecModel;
  14. use App\Services\BaseService;
  15. use App\Services\RedisService;
  16. /**
  17. * 货物包装管理-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * @package App\Services\Common
  21. */
  22. class FreightSpecService extends BaseService
  23. {
  24. // 静态对象
  25. protected static $instance = null;
  26. /**
  27. * 构造函数
  28. * @author laravel开发员
  29. * @since 2020/11/11
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new FreightSpecModel();
  34. }
  35. /**
  36. * 静态入口
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = new static();
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * 列表
  47. * @param $params
  48. * @param int $pageSize
  49. * @return array
  50. */
  51. public function getDataList($params, $pageSize = 15)
  52. {
  53. $where = ['a.mark' => 1];
  54. $list = $this->model->from('freight_spec as a')
  55. ->where($where)
  56. ->where(function ($query) use($params){
  57. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  58. if($keyword){
  59. $query->where('a.name','like',"%{$keyword}%")->orWhere('a.sub_name','like',"%{$keyword}%");
  60. }
  61. })
  62. ->where(function ($query) use($params){
  63. $status = isset($params['status'])? $params['status'] : 0;
  64. if($status>0 && is_array($status)){
  65. $query->whereIn('a.status', $status);
  66. }else if($status){
  67. $query->where('a.status', $status);
  68. }
  69. $categoryId = isset($params['category_id'])? $params['category_id'] : 0;
  70. if($categoryId>0){
  71. $query->where('a.category_id', $categoryId);
  72. }
  73. })
  74. ->select(['a.*'])
  75. ->orderBy('a.create_time','desc')
  76. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  77. $list = $list? $list->toArray() :[];
  78. if($list){
  79. foreach($list['data'] as &$item){
  80. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  81. }
  82. }
  83. return [
  84. 'pageSize'=> $pageSize,
  85. 'total'=>isset($list['total'])? $list['total'] : 0,
  86. 'list'=> isset($list['data'])? $list['data'] : []
  87. ];
  88. }
  89. /**
  90. * 编辑
  91. * @return array
  92. */
  93. public function edit()
  94. {
  95. $data = request()->post();
  96. $data['price'] = $data['price']? str_replace(['|','-',':'],['|','-',':'], $data['price']) : 0;
  97. return parent::edit($data);
  98. }
  99. /**
  100. * 车型选项
  101. * @param int $num
  102. * @return array|mixed
  103. */
  104. public function getOptions($type=0, $num = 20)
  105. {
  106. $cacheKey = "caches:index:freight_package_{$type}";
  107. $datas = RedisService::get($cacheKey);
  108. if($datas){
  109. return $datas;
  110. }
  111. $where = ['status'=>1,'mark'=>1];
  112. $datas = $this->model->where($where)
  113. ->where(function($query) use($type){
  114. if($type){
  115. $query->whereIn('type',[0, $type]);
  116. }
  117. })
  118. ->select(['id','name','type','status'])
  119. ->limit($num)
  120. ->orderBy('sort','desc')
  121. ->orderBy('id','asc')
  122. ->get();
  123. $datas = $datas? $datas->toArray() : [];
  124. if($datas){
  125. RedisService::set($cacheKey, $datas, rand(5, 10));
  126. }
  127. return $datas;
  128. }
  129. }