| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\GoodsModel;
- use App\Models\ActionLogModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- use wxkxklmyt\Scws;
- /**
- * 商品管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Common
- */
- class GoodsService extends BaseService
- {
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * AdService constructor.
- */
- public function __construct()
- {
- $this->model = new GoodsModel();
- }
- /**
- * 列表
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 15)
- {
- $query = $this->getQuery($params);
- $list = $query->select(['a.*'])
- ->orderBy('a.picker_status','asc')
- ->orderBy('a.create_time','desc')
- ->orderBy('a.id','desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list? $list->toArray() :[];
- if($list){
- foreach($list['data'] as &$item){
- $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
- $item['shipper_area'] = [];
- $item['shipper_area'][] = isset($item['shipper_province_id'])? trim($item['shipper_province_id']) : 0;
- $item['shipper_area'][] = isset($item['shipper_city_id'])? trim($item['shipper_city_id']) : 0;
- $item['shipper_area'][] = isset($item['shipper_district_id'])? trim($item['shipper_district_id']) : 0;
- $item['receiver_area'] = [];
- $item['receiver_area'][] = isset($item['receiver_province_id'])? trim($item['receiver_province_id']) : 0;
- $item['receiver_area'][] = isset($item['receiver_city_id'])? trim($item['receiver_city_id']) : 0;
- $item['receiver_area'][] = isset($item['receiver_district_id'])? trim($item['receiver_district_id']) : 0;
- }
- }
- return [
- 'pageSize'=> $pageSize,
- 'total'=>isset($list['total'])? $list['total'] : 0,
- 'list'=> isset($list['data'])? $list['data'] : []
- ];
- }
- /**
- * 查询
- * @param $params
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function getQuery($params)
- {
- $where = ['a.mark' => 1];
- $status = isset($params['status'])? $params['status'] : 1;
- if($status>0){
- $where['a.status'] = $status;
- }else{
- unset($where['a.status']);
- }
- $model = $this->model->with(['order'])->from('goods as a')
- ->where($where)
- ->where(function($query) use($params){
- // 接单状态
- $pickerStatus = isset($params['picker_status'])? $params['picker_status'] : 0;
- if($pickerStatus>0){
- $query->where('a.picker_status', $pickerStatus);
- }
- })
- ->where(function ($query) use($params){
- $keyword = isset($params['keyword'])? $params['keyword'] : '';
- if($keyword){
- $scws = new Scws();
- $kws = $scws->scws($keyword,3,false);
- if(count($kws) >1){
- $query->where(function ($query) use ($kws) {
- foreach ($kws as $kw) {
- $kw = trim($kw);
- $query->where('a.goods_name', 'like', "%{$kw}%")
- ->where('a.car_type', 'like', "%{$kw}%");
- }
- });
- }else{
- $query->where(function ($query) use ($keyword){
- $query->where('a.goods_name','like',"%{$keyword}%")
- ->orWhere('a.car_type','like',"%{$keyword}%");
- });
- }
- }
- // 取货人
- $shipper = isset($params['shipper']) ? trim($params['shipper']) : '';
- if ($shipper) {
- $query->where(function ($query) use ($shipper) {
- $query->where('a.shipper_name', 'like', "%{$shipper}%")
- ->orWhere('a.shipper_phone', 'like', "%{$shipper}%");
- });
- }
- // 收货人
- $receiver = isset($params['receiver']) ? trim($params['receiver']) : '';
- if ($receiver) {
- $query->where(function ($query) use ($receiver) {
- $query->where('a.receiver_name', 'like', "%{$receiver}%")
- ->orWhere('a.receiver_phone', 'like', "%{$receiver}%");
- });
- }
- });
- return $model;
- }
- /**
- * 添加或编辑
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function edit()
- {
- $data = request()->all();
- // 图片处理
- if(isset($data['thumb'])){
- $data['thumb'] = get_image_path($data['thumb']);
- }
- if(empty($data['goods_name'])){
- return message('请填写商品名称',false);
- }
- if($data['price']<=0){
- return message('请填写运费金额',false);
- }
- if(empty($data['shipper_name'])){
- return message('请填写寄货人姓名',false);
- }
- if(empty($data['shipper_phone'])){
- return message('请填写寄货人手机号',false);
- }
- if(empty($data['shipper_address'])){
- return message('请填写寄货详细地址',false);
- }
- if(empty($data['receiver_name'])){
- return message('请填写收货人姓名',false);
- }
- if(empty($data['receiver_phone'])){
- return message('请填写收货人手机号',false);
- }
- if(empty($data['receiver_address'])){
- return message('请填写收货详细地址',false);
- }
- $shipperArea = isset($data['shipper_area'])? $data['shipper_area'] : [];
- if(isset($shipperArea[0])){
- $data['shipper_province_id'] = $shipperArea[0];
- }
- if(isset($shipperArea[1])){
- $data['shipper_city_id'] = $shipperArea[1];
- }
- if(isset($shipperArea[2])){
- $data['shipper_district_id'] = $shipperArea[2];
- }
- $receiverArea = isset($data['receiver_area'])? $data['receiver_area'] : [];
- if(isset($receiverArea[0])){
- $data['receiver_province_id'] = $receiverArea[0];
- }
- if(isset($receiverArea[1])){
- $data['receiver_city_id'] = $receiverArea[1];
- }
- if(isset($receiverArea[2])){
- $data['receiver_district_id'] = $receiverArea[2];
- }
- return parent::edit($data);
- }
- /**
- * 商品数量
- * @param int $type
- * @return array|mixed
- */
- public function getCounts($type=0)
- {
- $cacheKey = "caches:goods:count_{$type}";
- $data = RedisService::get($cacheKey);
- if($data){
- return $data;
- }
- $data = $this->model->where(['mark'=>1])
- ->where(function($query) use($type){
- if($type== 1){
- $query->where(['status'=>1]);
- }else if($type == 2){
- $query->where(['status'=>1,'picker_status'=>3]);
- }
- })->count('id');
- if($data){
- RedisService::set($cacheKey, $data, rand(300, 600));
- }
- return $data;
- }
- /**
- * 删除七天之前标记软删除的数据
- */
- public function delete()
- {
- // 设置日志标题
- ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除商品信息", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
- ActionLogModel::record();
- $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
- return parent::delete();
- }
- }
|