| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- 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()]);
- }
- }
|