ShopService.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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\ShopModel;
  13. use App\Services\BaseService;
  14. use App\Services\ConfigService;
  15. use App\Services\RedisService;
  16. /**
  17. * 店铺管理-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/11
  20. * Class ShopService
  21. * @package App\Services\Common
  22. */
  23. class ShopService extends BaseService
  24. {
  25. // 静态对象
  26. protected static $instance = null;
  27. /**
  28. * 构造函数
  29. * @author laravel开发员
  30. * @since 2020/11/11
  31. * ShopService constructor.
  32. */
  33. public function __construct()
  34. {
  35. $this->model = new ShopModel();
  36. }
  37. /**
  38. * 静态入口
  39. * @return static|null
  40. */
  41. public static function make()
  42. {
  43. if (!self::$instance) {
  44. self::$instance = (new static());
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * 获取资料详情
  50. * @param $where
  51. * @param array $field
  52. */
  53. public function getInfo($where, array $field = [])
  54. {
  55. $cacheKey = "caches:shop:".(!is_array($where)? $where : md5(json_encode($where)));
  56. $info = RedisService::get($cacheKey);
  57. if($info){
  58. return $info;
  59. }
  60. $field = $field ? $field : ['id', 'name', 'user_id', 'logo', 'code', 'theme_id', 'start_time', 'end_time', 'status'];
  61. if (is_array($where)) {
  62. $info = $this->model->where($where)->select($field)->first();
  63. } else {
  64. $info = $this->model->where(['id' => (int)$where])->select($field)->first();
  65. }
  66. $info = $info ? $info->toArray() : [];
  67. if($info){
  68. $info['logo'] = $info['logo'] ? get_image_url($info['logo']) : '';
  69. RedisService::set($cacheKey, $info, rand(5,10));
  70. }
  71. return $info;
  72. }
  73. /**
  74. * @param $params
  75. * @param int $pageSize
  76. * @return array
  77. */
  78. public function getDataList($params, $pageSize = 15)
  79. {
  80. $where = ['a.mark' => 1];
  81. $status = isset($params['status'])? $params['status'] : 0;
  82. $type = isset($params['type'])? $params['type'] : 0;
  83. $parentId = isset($params['parent_id'])? $params['parent_id'] : 0;
  84. if($parentId>0){
  85. $where['a.parent_id'] = $parentId;
  86. }
  87. if($status>0){
  88. $where['a.status'] = $status;
  89. }
  90. $list = $this->model->from('shop as a')
  91. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  92. ->leftJoin('shop as c', 'c.id', '=', 'a.parent_id')
  93. ->where($where)
  94. ->where(function ($query) use($params){
  95. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  96. $type = isset($params['type'])? $params['type'] : 0;
  97. if($keyword){
  98. if($type == 1){
  99. $query->where('b.nickname','like',"%{$keyword}%")->orWhere('a.code','like',"%{$keyword}%")->orWhere('b.mobile','like',"%{$keyword}%");
  100. }else{
  101. $query->where('a.name','like',"%{$keyword}%")->orWhere('a.code','like',"%{$keyword}%")->orWhere('b.mobile','like',"%{$keyword}%");
  102. }
  103. }
  104. })
  105. ->select(['a.*','b.username as shop_username','b.mobile as shop_mobile','b.nickname','c.name as parent_name','c.code as parent_code'])
  106. ->orderBy('a.id','desc')
  107. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  108. $list = $list? $list->toArray() :[];
  109. if($list){
  110. foreach($list['data'] as &$item){
  111. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  112. $item['logo'] = isset($item['logo']) && $item['logo']? get_image_url($item['logo']) : '';
  113. $item['parent_id'] = $item['parent_id']? $item['parent_id'] : '';
  114. if($type == 1){
  115. $item['bonus_total'] = AccountService::make()->getShopAccountTotal($item['id'],2,2);
  116. // $item['trade_count'] = TradeService::make()->getShopTradeCount($item['id'], 3);
  117. $item['trade_total'] = TradeService::make()->getShopTradeTotal($item['id'], 3);
  118. }
  119. }
  120. }
  121. return [
  122. 'pageSize'=> $pageSize,
  123. 'total'=>isset($list['total'])? $list['total'] : 0,
  124. 'list'=> isset($list['data'])? $list['data'] : []
  125. ];
  126. }
  127. /**
  128. * 选项
  129. * @return array
  130. */
  131. public function options()
  132. {
  133. // 获取参数
  134. $param = request()->all();
  135. // 用户ID
  136. $keyword = getter($param, "keyword");
  137. $parentId = getter($param, "parent_id");
  138. $id = getter($param, "id");
  139. $datas = $this->model->where(function($query) use($parentId){
  140. if($parentId){
  141. $query->where(['id'=> $parentId,'mark'=>1]);
  142. }else{
  143. $query->where(['status'=> 1,'mark'=>1]);
  144. }
  145. })
  146. ->where(function($query) use($id){
  147. if($id){
  148. $query->whereNotIn('id', [$id]);
  149. }
  150. })
  151. ->where(function($query) use($keyword){
  152. if($keyword){
  153. $query->where('name','like',"%{$keyword}%")->orWhere('code','like',"%{$keyword}%");
  154. }
  155. })
  156. ->select(['id','name','code','status'])
  157. ->get();
  158. return $datas? $datas->toArray() : [];
  159. }
  160. /**
  161. * 推荐人列表
  162. * @return array
  163. */
  164. public function parents()
  165. {
  166. // 获取参数
  167. $param = request()->all();
  168. // 用户ID
  169. $keyword = getter($param, "keyword");
  170. $parentId = getter($param, "parent_id");
  171. $id = getter($param, "id");
  172. $datas = $this->model->where(function($query) use($parentId){
  173. if($parentId){
  174. $query->where(['id'=> $parentId,'mark'=>1]);
  175. }else{
  176. $query->where(['status'=> 1,'mark'=>1]);
  177. }
  178. })
  179. ->where(function($query) use($id){
  180. if($id){
  181. $query->whereNotIn('id', [$id]);
  182. }
  183. })
  184. ->where(function($query) use($keyword){
  185. if($keyword){
  186. $query->where('name','like',"%{$keyword}%")->orWhere('code','like',"%{$keyword}%");
  187. }
  188. })
  189. ->select(['id','name','code','status'])
  190. ->get();
  191. return $datas? $datas->toArray() : [];
  192. }
  193. /**
  194. * 添加会编辑店铺
  195. * @return array
  196. * @since 2020/11/11
  197. * @author laravel开发员
  198. */
  199. public function edit()
  200. {
  201. // 请求参数
  202. $data = request()->all();
  203. // logo处理
  204. $logo = isset($data['logo'])? trim($data['logo']) : '';
  205. if ($logo && strpos($logo, "temp")) {
  206. $data['logo'] = save_image($logo, 'member');
  207. } else if($logo){
  208. $data['logo'] = str_replace(IMG_URL, "", $data['logo']);
  209. }
  210. $data['parent_id'] = intval($data['parent_id']);
  211. return parent::edit($data); // TODO: Change the autogenerated stub
  212. }
  213. /**
  214. * 设置抢拍时间
  215. * @param $userId
  216. * @param $shopId
  217. * @return mixed
  218. */
  219. public function setTime($userId, $shopId, $params)
  220. {
  221. $shopInfo = $this->model->where(['id'=> $shopId,'mark'=>1])->first();
  222. $shopUid = isset($shopInfo['user_id'])? $shopInfo['user_id'] : 0;
  223. if(empty($shopInfo) || $userId != $shopUid){
  224. $this->error = 2020;
  225. return false;
  226. }
  227. $startTime = isset($params['start_time'])? $params['start_time'] : '';
  228. $endTime = isset($params['end_time'])? $params['end_time'] : '';
  229. if($endTime < $startTime){
  230. $this->error = 2021;
  231. return false;
  232. }
  233. RedisService::keyDel('caches:shop:'.$shopId);
  234. return $this->model->where(['id'=> $shopId])->update(['start_time'=> $startTime,'end_time'=> $endTime,'update_time'=>time()]);
  235. }
  236. }