GoodsService.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. ->orderBy('a.picker_status','asc')
  45. ->orderBy('a.create_time','desc')
  46. ->orderBy('a.id','desc')
  47. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  48. $list = $list? $list->toArray() :[];
  49. if($list){
  50. foreach($list['data'] as &$item){
  51. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  52. $item['shipper_area'] = [];
  53. $item['shipper_area'][] = isset($item['shipper_province_id'])? trim($item['shipper_province_id']) : 0;
  54. $item['shipper_area'][] = isset($item['shipper_city_id'])? trim($item['shipper_city_id']) : 0;
  55. $item['shipper_area'][] = isset($item['shipper_district_id'])? trim($item['shipper_district_id']) : 0;
  56. $item['receiver_area'] = [];
  57. $item['receiver_area'][] = isset($item['receiver_province_id'])? trim($item['receiver_province_id']) : 0;
  58. $item['receiver_area'][] = isset($item['receiver_city_id'])? trim($item['receiver_city_id']) : 0;
  59. $item['receiver_area'][] = isset($item['receiver_district_id'])? trim($item['receiver_district_id']) : 0;
  60. }
  61. }
  62. return [
  63. 'pageSize'=> $pageSize,
  64. 'total'=>isset($list['total'])? $list['total'] : 0,
  65. 'list'=> isset($list['data'])? $list['data'] : []
  66. ];
  67. }
  68. /**
  69. * 查询
  70. * @param $params
  71. * @return \Illuminate\Database\Eloquent\Builder
  72. */
  73. public function getQuery($params)
  74. {
  75. $where = ['a.mark' => 1];
  76. $status = isset($params['status'])? $params['status'] : 1;
  77. if($status>0){
  78. $where['a.status'] = $status;
  79. }else{
  80. unset($where['a.status']);
  81. }
  82. $model = $this->model->with(['order'])->from('goods as a')
  83. ->where($where)
  84. ->where(function($query) use($params){
  85. // 接单状态
  86. $pickerStatus = isset($params['picker_status'])? $params['picker_status'] : 0;
  87. if($pickerStatus>0){
  88. $query->where('a.picker_status', $pickerStatus);
  89. }
  90. })
  91. ->where(function ($query) use($params){
  92. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  93. if($keyword){
  94. $scws = new Scws();
  95. $kws = $scws->scws($keyword,3,false);
  96. if(count($kws) >1){
  97. $query->where(function ($query) use ($kws) {
  98. foreach ($kws as $kw) {
  99. $kw = trim($kw);
  100. $query->where('a.goods_name', 'like', "%{$kw}%")
  101. ->where('a.car_type', 'like', "%{$kw}%");
  102. }
  103. });
  104. }else{
  105. $query->where(function ($query) use ($keyword){
  106. $query->where('a.goods_name','like',"%{$keyword}%")
  107. ->orWhere('a.car_type','like',"%{$keyword}%");
  108. });
  109. }
  110. }
  111. // 取货人
  112. $shipper = isset($params['shipper']) ? trim($params['shipper']) : '';
  113. if ($shipper) {
  114. $query->where(function ($query) use ($shipper) {
  115. $query->where('a.shipper_name', 'like', "%{$shipper}%")
  116. ->orWhere('a.shipper_phone', 'like', "%{$shipper}%");
  117. });
  118. }
  119. // 收货人
  120. $receiver = isset($params['receiver']) ? trim($params['receiver']) : '';
  121. if ($receiver) {
  122. $query->where(function ($query) use ($receiver) {
  123. $query->where('a.receiver_name', 'like', "%{$receiver}%")
  124. ->orWhere('a.receiver_phone', 'like', "%{$receiver}%");
  125. });
  126. }
  127. });
  128. return $model;
  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. if(empty($data['goods_name'])){
  144. return message('请填写商品名称',false);
  145. }
  146. if($data['price']<=0){
  147. return message('请填写运费金额',false);
  148. }
  149. if(empty($data['shipper_name'])){
  150. return message('请填写寄货人姓名',false);
  151. }
  152. if(empty($data['shipper_phone'])){
  153. return message('请填写寄货人手机号',false);
  154. }
  155. if(empty($data['shipper_address'])){
  156. return message('请填写寄货详细地址',false);
  157. }
  158. if(empty($data['receiver_name'])){
  159. return message('请填写收货人姓名',false);
  160. }
  161. if(empty($data['receiver_phone'])){
  162. return message('请填写收货人手机号',false);
  163. }
  164. if(empty($data['receiver_address'])){
  165. return message('请填写收货详细地址',false);
  166. }
  167. $shipperArea = isset($data['shipper_area'])? $data['shipper_area'] : [];
  168. if(isset($shipperArea[0])){
  169. $data['shipper_province_id'] = $shipperArea[0];
  170. }
  171. if(isset($shipperArea[1])){
  172. $data['shipper_city_id'] = $shipperArea[1];
  173. }
  174. if(isset($shipperArea[2])){
  175. $data['shipper_district_id'] = $shipperArea[2];
  176. }
  177. $receiverArea = isset($data['receiver_area'])? $data['receiver_area'] : [];
  178. if(isset($receiverArea[0])){
  179. $data['receiver_province_id'] = $receiverArea[0];
  180. }
  181. if(isset($receiverArea[1])){
  182. $data['receiver_city_id'] = $receiverArea[1];
  183. }
  184. if(isset($receiverArea[2])){
  185. $data['receiver_district_id'] = $receiverArea[2];
  186. }
  187. return parent::edit($data);
  188. }
  189. /**
  190. * 商品数量
  191. * @param int $type
  192. * @return array|mixed
  193. */
  194. public function getCounts($type=0)
  195. {
  196. $cacheKey = "caches:goods:count_{$type}";
  197. $data = RedisService::get($cacheKey);
  198. if($data){
  199. return $data;
  200. }
  201. $data = $this->model->where(['mark'=>1])
  202. ->where(function($query) use($type){
  203. if($type== 1){
  204. $query->where(['status'=>1]);
  205. }else if($type == 2){
  206. $query->where(['status'=>1,'picker_status'=>3]);
  207. }
  208. })->count('id');
  209. if($data){
  210. RedisService::set($cacheKey, $data, rand(300, 600));
  211. }
  212. return $data;
  213. }
  214. }