ShopService.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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['trade_total'] = TradeService::make()->getShopTradeTotal($item['id'],'real_price',2);
  116. $item['service_total'] = TradeService::make()->getShopTradeTotal($item['id'],'service_fee',2);
  117. $item['fee_total'] = TradeService::make()->getShopTradeTotal($item['id'],'fee',2);
  118. $item['bonus_total'] = TradeService::make()->getShopTradeTotal($item['id'],'bonus',2);
  119. }
  120. }
  121. }
  122. return [
  123. 'pageSize'=> $pageSize,
  124. 'total'=>isset($list['total'])? $list['total'] : 0,
  125. 'list'=> isset($list['data'])? $list['data'] : []
  126. ];
  127. }
  128. /**
  129. * 选项
  130. * @return array
  131. */
  132. public function options()
  133. {
  134. // 获取参数
  135. $param = request()->all();
  136. // 用户ID
  137. $keyword = getter($param, "keyword");
  138. $parentId = getter($param, "parent_id");
  139. $id = getter($param, "id");
  140. $datas = $this->model->where(function($query) use($parentId){
  141. if($parentId){
  142. $query->where(['id'=> $parentId,'mark'=>1]);
  143. }else{
  144. $query->where(['status'=> 1,'mark'=>1]);
  145. }
  146. })
  147. ->where(function($query) use($id){
  148. if($id){
  149. $query->whereNotIn('id', [$id]);
  150. }
  151. })
  152. ->where(function($query) use($keyword){
  153. if($keyword){
  154. $query->where('name','like',"%{$keyword}%")->orWhere('code','like',"%{$keyword}%");
  155. }
  156. })
  157. ->select(['id','name','code','status'])
  158. ->get();
  159. return $datas? $datas->toArray() : [];
  160. }
  161. /**
  162. * 推荐人列表
  163. * @return array
  164. */
  165. public function parents()
  166. {
  167. // 获取参数
  168. $param = request()->all();
  169. // 用户ID
  170. $keyword = getter($param, "keyword");
  171. $parentId = getter($param, "parent_id");
  172. $id = getter($param, "id");
  173. $datas = $this->model->where(function($query) use($parentId){
  174. if($parentId){
  175. $query->where(['id'=> $parentId,'mark'=>1]);
  176. }else{
  177. $query->where(['status'=> 1,'mark'=>1]);
  178. }
  179. })
  180. ->where(function($query) use($id){
  181. if($id){
  182. $query->whereNotIn('id', [$id]);
  183. }
  184. })
  185. ->where(function($query) use($keyword){
  186. if($keyword){
  187. $query->where('name','like',"%{$keyword}%")->orWhere('code','like',"%{$keyword}%");
  188. }
  189. })
  190. ->select(['id','name','code','status'])
  191. ->get();
  192. return $datas? $datas->toArray() : [];
  193. }
  194. /**
  195. * 添加会编辑店铺
  196. * @return array
  197. * @since 2020/11/11
  198. * @author laravel开发员
  199. */
  200. public function edit()
  201. {
  202. // 请求参数
  203. $data = request()->all();
  204. // logo处理
  205. $logo = isset($data['logo'])? trim($data['logo']) : '';
  206. if ($logo && strpos($logo, "temp")) {
  207. $data['logo'] = save_image($logo, 'member');
  208. } else if($logo){
  209. $data['logo'] = str_replace(IMG_URL, "", $data['logo']);
  210. }
  211. $data['parent_id'] = isset($data['parent_id'])? intval($data['parent_id']):0;
  212. $id = isset($data['id'])? $data['id'] : 0;
  213. $code = isset($data['code'])? $data['code'] : '';
  214. if($this->model->where(['code'=> $code,'mark'=>1])->whereNotIn('id', [$id])->value('id')){
  215. return message(2023,false);
  216. }
  217. return parent::edit($data); // TODO: Change the autogenerated stub
  218. }
  219. /**
  220. * 设置抢拍时间
  221. * @param $userId
  222. * @param $shopId
  223. * @return mixed
  224. */
  225. public function setTime($userId, $shopId, $params)
  226. {
  227. $shopInfo = $this->model->where(['id'=> $shopId,'mark'=>1])->first();
  228. $shopUid = isset($shopInfo['user_id'])? $shopInfo['user_id'] : 0;
  229. if(empty($shopInfo) || $userId != $shopUid){
  230. $this->error = 2020;
  231. return false;
  232. }
  233. $startTime = isset($params['start_time'])? $params['start_time'] : '';
  234. $endTime = isset($params['end_time'])? $params['end_time'] : '';
  235. if($endTime < $startTime){
  236. $this->error = 2021;
  237. return false;
  238. }
  239. RedisService::keyDel('caches:shop:'.$shopId);
  240. return $this->model->where(['id'=> $shopId])->update(['start_time'=> $startTime,'end_time'=> $endTime,'update_time'=>time()]);
  241. }
  242. }