// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\ShopModel; use App\Services\BaseService; use App\Services\ConfigService; use App\Services\RedisService; /** * 店铺管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class ShopService * @package App\Services\Common */ class ShopService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * ShopService constructor. */ public function __construct() { $this->model = new ShopModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 获取资料详情 * @param $where * @param array $field */ public function getInfo($where, array $field = []) { $cacheKey = "caches:shop:".(!is_array($where)? $where : md5(json_encode($where))); $info = RedisService::get($cacheKey); if($info){ return $info; } $field = $field ? $field : ['id', 'name', 'user_id', 'logo', 'code', 'theme_id', 'start_time', 'end_time', 'status']; if (is_array($where)) { $info = $this->model->where($where)->select($field)->first(); } else { $info = $this->model->where(['id' => (int)$where])->select($field)->first(); } $info = $info ? $info->toArray() : []; if($info){ $info['logo'] = $info['logo'] ? get_image_url($info['logo']) : ''; RedisService::set($cacheKey, $info, rand(5,10)); } return $info; } /** * @param $params * @param int $pageSize * @return array */ public function getDataList($params, $pageSize = 15) { $where = ['a.mark' => 1]; $status = isset($params['status'])? $params['status'] : 0; $type = isset($params['type'])? $params['type'] : 0; $parentId = isset($params['parent_id'])? $params['parent_id'] : 0; if($parentId>0){ $where['a.parent_id'] = $parentId; } if($status>0){ $where['a.status'] = $status; } $list = $this->model->from('shop as a') ->leftJoin('member as b', 'a.user_id', '=', 'b.id') ->leftJoin('shop as c', 'c.id', '=', 'a.parent_id') ->where($where) ->where(function ($query) use($params){ $keyword = isset($params['keyword'])? $params['keyword'] : ''; $type = isset($params['type'])? $params['type'] : 0; if($keyword){ if($type == 1){ $query->where('b.nickname','like',"%{$keyword}%")->orWhere('a.code','like',"%{$keyword}%")->orWhere('b.mobile','like',"%{$keyword}%"); }else{ $query->where('a.name','like',"%{$keyword}%")->orWhere('a.code','like',"%{$keyword}%")->orWhere('b.mobile','like',"%{$keyword}%"); } } }) ->select(['a.*','b.username as shop_username','b.mobile as shop_mobile','b.nickname','c.name as parent_name','c.code as parent_code']) ->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['logo'] = isset($item['logo']) && $item['logo']? get_image_url($item['logo']) : ''; $item['parent_id'] = $item['parent_id']? $item['parent_id'] : ''; if($type == 1){ $item['bonus_total'] = AccountService::make()->getShopAccountTotal($item['id'],2,2); // $item['trade_count'] = TradeService::make()->getShopTradeCount($item['id'], 3); $item['trade_total'] = TradeService::make()->getShopTradeTotal($item['id'], 3); } } } return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } /** * 选项 * @return array */ public function options() { // 获取参数 $param = request()->all(); // 用户ID $keyword = getter($param, "keyword"); $parentId = getter($param, "parent_id"); $id = getter($param, "id"); $datas = $this->model->where(function($query) use($parentId){ if($parentId){ $query->where(['id'=> $parentId,'mark'=>1]); }else{ $query->where(['status'=> 1,'mark'=>1]); } }) ->where(function($query) use($id){ if($id){ $query->whereNotIn('id', [$id]); } }) ->where(function($query) use($keyword){ if($keyword){ $query->where('name','like',"%{$keyword}%")->orWhere('code','like',"%{$keyword}%"); } }) ->select(['id','name','code','status']) ->get(); return $datas? $datas->toArray() : []; } /** * 推荐人列表 * @return array */ public function parents() { // 获取参数 $param = request()->all(); // 用户ID $keyword = getter($param, "keyword"); $parentId = getter($param, "parent_id"); $id = getter($param, "id"); $datas = $this->model->where(function($query) use($parentId){ if($parentId){ $query->where(['id'=> $parentId,'mark'=>1]); }else{ $query->where(['status'=> 1,'mark'=>1]); } }) ->where(function($query) use($id){ if($id){ $query->whereNotIn('id', [$id]); } }) ->where(function($query) use($keyword){ if($keyword){ $query->where('name','like',"%{$keyword}%")->orWhere('code','like',"%{$keyword}%"); } }) ->select(['id','name','code','status']) ->get(); return $datas? $datas->toArray() : []; } /** * 添加会编辑店铺 * @return array * @since 2020/11/11 * @author laravel开发员 */ public function edit() { // 请求参数 $data = request()->all(); // logo处理 $logo = isset($data['logo'])? trim($data['logo']) : ''; if ($logo && strpos($logo, "temp")) { $data['logo'] = save_image($logo, 'member'); } else if($logo){ $data['logo'] = str_replace(IMG_URL, "", $data['logo']); } $data['parent_id'] = intval($data['parent_id']); return parent::edit($data); // TODO: Change the autogenerated stub } /** * 设置抢拍时间 * @param $userId * @param $shopId * @return mixed */ public function setTime($userId, $shopId, $params) { $shopInfo = $this->model->where(['id'=> $shopId,'mark'=>1])->first(); $shopUid = isset($shopInfo['user_id'])? $shopInfo['user_id'] : 0; if(empty($shopInfo) || $userId != $shopUid){ $this->error = 2020; return false; } $startTime = isset($params['start_time'])? $params['start_time'] : ''; $endTime = isset($params['end_time'])? $params['end_time'] : ''; if($endTime < $startTime){ $this->error = 2021; return false; } RedisService::keyDel('caches:shop:'.$shopId); return $this->model->where(['id'=> $shopId])->update(['start_time'=> $startTime,'end_time'=> $endTime,'update_time'=>time()]); } }