GoodsService.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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\ActionLogModel;
  14. use App\Services\BaseService;
  15. use App\Services\RedisService;
  16. use wxkxklmyt\Scws;
  17. /**
  18. * 商品管理-服务类
  19. * @author laravel开发员
  20. * @since 2020/11/11
  21. * @package App\Services\Common
  22. */
  23. class GoodsService extends BaseService
  24. {
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * AdService constructor.
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new GoodsModel();
  34. }
  35. /**
  36. * 列表
  37. * @param $params
  38. * @param int $pageSize
  39. * @return array
  40. */
  41. public function getDataList($params, $pageSize = 15)
  42. {
  43. $query = $this->getQuery($params);
  44. $list = $query->select(['a.*'])
  45. ->orderBy('a.picker_status','asc')
  46. ->orderBy('a.create_time','desc')
  47. ->orderBy('a.id','desc')
  48. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  49. $list = $list? $list->toArray() :[];
  50. if($list){
  51. foreach($list['data'] as &$item){
  52. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  53. $item['shipper_area'] = [];
  54. $item['shipper_area'][] = isset($item['shipper_province_id'])? trim($item['shipper_province_id']) : 0;
  55. $item['shipper_area'][] = isset($item['shipper_city_id'])? trim($item['shipper_city_id']) : 0;
  56. $item['shipper_area'][] = isset($item['shipper_district_id'])? trim($item['shipper_district_id']) : 0;
  57. $item['receiver_area'] = [];
  58. $item['receiver_area'][] = isset($item['receiver_province_id'])? trim($item['receiver_province_id']) : 0;
  59. $item['receiver_area'][] = isset($item['receiver_city_id'])? trim($item['receiver_city_id']) : 0;
  60. $item['receiver_area'][] = isset($item['receiver_district_id'])? trim($item['receiver_district_id']) : 0;
  61. }
  62. }
  63. return [
  64. 'pageSize'=> $pageSize,
  65. 'total'=>isset($list['total'])? $list['total'] : 0,
  66. 'list'=> isset($list['data'])? $list['data'] : []
  67. ];
  68. }
  69. /**
  70. * 查询
  71. * @param $params
  72. * @return \Illuminate\Database\Eloquent\Builder
  73. */
  74. public function getQuery($params)
  75. {
  76. $where = ['a.mark' => 1];
  77. $status = isset($params['status'])? $params['status'] : 1;
  78. if($status>0){
  79. $where['a.status'] = $status;
  80. }else{
  81. unset($where['a.status']);
  82. }
  83. $model = $this->model->with(['order'])->from('goods as a')
  84. ->where($where)
  85. ->where(function($query) use($params){
  86. // 接单状态
  87. $pickerStatus = isset($params['picker_status'])? $params['picker_status'] : 0;
  88. if($pickerStatus>0){
  89. $query->where('a.picker_status', $pickerStatus);
  90. }
  91. })
  92. ->where(function ($query) use($params){
  93. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  94. if($keyword){
  95. $scws = new Scws();
  96. $kws = $scws->scws($keyword,3,false);
  97. if(count($kws) >1){
  98. $query->where(function ($query) use ($kws) {
  99. foreach ($kws as $kw) {
  100. $kw = trim($kw);
  101. $query->where('a.goods_name', 'like', "%{$kw}%")
  102. ->where('a.car_type', 'like', "%{$kw}%");
  103. }
  104. });
  105. }else{
  106. $query->where(function ($query) use ($keyword){
  107. $query->where('a.goods_name','like',"%{$keyword}%")
  108. ->orWhere('a.car_type','like',"%{$keyword}%");
  109. });
  110. }
  111. }
  112. // 取货人
  113. $shipper = isset($params['shipper']) ? trim($params['shipper']) : '';
  114. if ($shipper) {
  115. $query->where(function ($query) use ($shipper) {
  116. $query->where('a.shipper_name', 'like', "%{$shipper}%")
  117. ->orWhere('a.shipper_phone', 'like', "%{$shipper}%");
  118. });
  119. }
  120. // 收货人
  121. $receiver = isset($params['receiver']) ? trim($params['receiver']) : '';
  122. if ($receiver) {
  123. $query->where(function ($query) use ($receiver) {
  124. $query->where('a.receiver_name', 'like', "%{$receiver}%")
  125. ->orWhere('a.receiver_phone', 'like', "%{$receiver}%");
  126. });
  127. }
  128. });
  129. return $model;
  130. }
  131. /**
  132. * 添加或编辑
  133. * @return array
  134. * @since 2020/11/11
  135. * @author laravel开发员
  136. */
  137. public function edit()
  138. {
  139. $data = request()->all();
  140. // 图片处理
  141. if(isset($data['thumb'])){
  142. $data['thumb'] = get_image_path($data['thumb']);
  143. }
  144. if(empty($data['goods_name'])){
  145. return message('请填写商品名称',false);
  146. }
  147. if($data['price']<=0){
  148. return message('请填写运费金额',false);
  149. }
  150. if(empty($data['shipper_name'])){
  151. return message('请填写寄货人姓名',false);
  152. }
  153. if(empty($data['shipper_phone'])){
  154. return message('请填写寄货人手机号',false);
  155. }
  156. if(empty($data['shipper_address'])){
  157. return message('请填写寄货详细地址',false);
  158. }
  159. if(empty($data['receiver_name'])){
  160. return message('请填写收货人姓名',false);
  161. }
  162. if(empty($data['receiver_phone'])){
  163. return message('请填写收货人手机号',false);
  164. }
  165. if(empty($data['receiver_address'])){
  166. return message('请填写收货详细地址',false);
  167. }
  168. $shipperArea = isset($data['shipper_area'])? $data['shipper_area'] : [];
  169. if(isset($shipperArea[0])){
  170. $data['shipper_province_id'] = $shipperArea[0];
  171. }
  172. if(isset($shipperArea[1])){
  173. $data['shipper_city_id'] = $shipperArea[1];
  174. }
  175. if(isset($shipperArea[2])){
  176. $data['shipper_district_id'] = $shipperArea[2];
  177. }
  178. $receiverArea = isset($data['receiver_area'])? $data['receiver_area'] : [];
  179. if(isset($receiverArea[0])){
  180. $data['receiver_province_id'] = $receiverArea[0];
  181. }
  182. if(isset($receiverArea[1])){
  183. $data['receiver_city_id'] = $receiverArea[1];
  184. }
  185. if(isset($receiverArea[2])){
  186. $data['receiver_district_id'] = $receiverArea[2];
  187. }
  188. return parent::edit($data);
  189. }
  190. /**
  191. * 商品数量
  192. * @param int $type
  193. * @return array|mixed
  194. */
  195. public function getCounts($type=0)
  196. {
  197. $cacheKey = "caches:goods:count_{$type}";
  198. $data = RedisService::get($cacheKey);
  199. if($data){
  200. return $data;
  201. }
  202. $data = $this->model->where(['mark'=>1])
  203. ->where(function($query) use($type){
  204. if($type== 1){
  205. $query->where(['status'=>1]);
  206. }else if($type == 2){
  207. $query->where(['status'=>1,'picker_status'=>3]);
  208. }
  209. })->count('id');
  210. if($data){
  211. RedisService::set($cacheKey, $data, rand(300, 600));
  212. }
  213. return $data;
  214. }
  215. /**
  216. * 删除七天之前标记软删除的数据
  217. */
  218. public function delete()
  219. {
  220. // 设置日志标题
  221. ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除商品信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
  222. ActionLogModel::record();
  223. $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
  224. return parent::delete();
  225. }
  226. }