Promotion.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\api\controller\relations;
  13. use app\api\controller\Controller;
  14. use app\api\model\ApplySeller;
  15. use app\api\model\HireMgs;
  16. use app\api\model\OrderGoods;
  17. use app\api\model\shop\Shops;
  18. use app\api\model\User;
  19. use app\api\model\user\BonusHistory;
  20. use app\api\model\user\CommissionsDetail;
  21. use app\api\service\User as UserService;
  22. use app\common\library\helper;
  23. use app\common\model\PushLog;
  24. use think\facade\Log;
  25. use app\common\model\store\Setting as SettingModel;
  26. use think\facade\Validate;
  27. /**
  28. * 推广中心控制器
  29. * Class Promotion
  30. * @package app\api\controller
  31. */
  32. class Promotion extends Controller
  33. {
  34. private $userId;
  35. public function initialize(){
  36. $this->userId = UserService::getCurrentLoginUserId();
  37. }
  38. /**
  39. * 推广中心
  40. * @return array|\think\response\Json
  41. */
  42. public function centerIndex()
  43. {
  44. //$userId = UserService::getCurrentLoginUserId();
  45. $userInfo = User::with(['avatar'=>function($query){
  46. $query->field('file_id,file_type,storage,domain,file_path');
  47. },'shop'])->where('user_id',$this->userId)
  48. ->field('user_id,mobile,nick_name,avatar_id,role,shop_id,upper_user_id,can_withdraw_money,have_withdrew_money,ktxyj_amount,seller_grade,seller_expire')
  49. ->find();
  50. $shops = $userInfo->shops->toArray();
  51. $userInfo = $userInfo->toArray();
  52. $userInfo['shops'] = $shops;
  53. $upper_user = null;
  54. //推广订单金额
  55. $userInfo['give_out_money'] = OrderGoods::sumGiveOutOrder($this->userId);
  56. //我的收益金
  57. $userInfo['my_profits'] = helper::bcsub($userInfo['ktxyj_amount'],0,2);
  58. $userInfo['my_bonus'] = BonusHistory::getCurrYearBonus($this->userId);
  59. //直推人数
  60. $userInfo['subordinates'] = User::countSubordinates($this->userId,$userInfo['shop']['shop_id'] ?? 0,$userInfo['role']);
  61. $userInfo['sellers'] = $userInfo['binders'] = 0;
  62. if ($userInfo['role'] == User::SHOP_MG){
  63. //门店关联消费者,只有店长可见
  64. $userInfo['binders'] = User::countShopBinder($userInfo['shop_id']);
  65. //店员数量,只有店长可见
  66. $userInfo['sellers'] = User::countShopSellers($userInfo['shop_id']);
  67. }
  68. $ApplySeller = ApplySeller::where('user_id',$this->userId)->find();
  69. $userInfo['apply_state'] = 9;
  70. $userInfo['apply_refuse_reason'] = '';
  71. if($ApplySeller){
  72. $userInfo['apply_state'] = $ApplySeller['state'];
  73. $userInfo['apply_refuse_reason'] = $ApplySeller['refuse_reason'];
  74. }
  75. //推荐官信息
  76. $userInfo['distributor_expire'] = '';
  77. $userInfo['distributor_grade_ratio'] = null;
  78. $userInfo['sub_sales_volume'] = $userInfo['sub_comms_count'] = 0;
  79. if ($userInfo['role'] == 99){
  80. $userInfo['distributor_expire'] = date("Y-m-d H:i:s",$userInfo['seller_expire']);
  81. $userInfo['distributor_grade_ratio'] = (SettingModel::getItem('distributor_grade'))['distributor'][$userInfo['seller_grade'] -1 ];
  82. //下级推荐官销售额
  83. $userInfo['sub_sales_volume'] = CommissionsDetail::sumUserCommSalesVolume($this->userId);
  84. //下级推荐官人数
  85. $userInfo['sub_comms_count'] = User::where('upper_user_id',$this->userId)->where('role',User::COMMISSION_USER)->count();
  86. }
  87. return $this->renderSuccess($userInfo);
  88. }
  89. /**
  90. * 门店老板切换门店
  91. * @return array|\think\response\Json
  92. * @throws \app\common\exception\BaseException
  93. */
  94. public function switchShop(){
  95. $shopId = intval(request()->post('shopId',0));
  96. $userId = UserService::getCurrentLoginUserId();
  97. if (User::switchBindShop($userId,$shopId)){
  98. return $this->renderSuccess();
  99. }
  100. return $this->renderSuccess('系统繁忙');
  101. }
  102. /**
  103. * 我的收益金
  104. * @param $status
  105. * @return array|\think\response\Json
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. */
  110. public function myProfitsList($status){
  111. if ($status!=0 && $status !=1){
  112. return $this->renderError('参数有误');
  113. }
  114. $data = CommissionsDetail::getCommsList($this->userId,$status);
  115. return $this->renderSuccess($data);
  116. }
  117. /**
  118. * 我的收益金 按订单
  119. * @param $status
  120. * @return array|\think\response\Json
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\DbException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. */
  125. public function myProfitsListOrder($status){
  126. if ($status!=0 && $status !=1 && $status!=2){
  127. return $this->renderError('参数有误');
  128. }
  129. $data = CommissionsDetail::getCommsListOrder($this->userId,$status);
  130. return $this->renderSuccess($data);
  131. }
  132. /**
  133. * 我的收益金-退款订单
  134. */
  135. public function refundOrderList() {
  136. $data = CommissionsDetail::getRefundOrderList($this->userId);
  137. return $this->renderSuccess($data);
  138. }
  139. /**
  140. * 直推用户/我的顾客
  141. * @return array|\think\response\Json
  142. */
  143. public function straightUsers(){
  144. $keyword = strval(request()->post('keyword',''));
  145. $data = User::getBindersDetail($this->userId,1,$keyword);
  146. return $this->renderSuccess($data);
  147. }
  148. /**
  149. * 门店关联消费者
  150. * @return array|\think\response\Json
  151. * @throws \think\db\exception\DataNotFoundException
  152. * @throws \think\db\exception\DbException
  153. * @throws \think\db\exception\ModelNotFoundException
  154. */
  155. public function shopRelateUsers(){
  156. $keyword = strval(request()->post('keyword',''));
  157. $shopId = intval(request()->post('shopId',0));
  158. $data = User::getBindersDetail($this->userId,2,$keyword,$shopId);
  159. if (!$data){
  160. return $this->renderSuccess([]);
  161. }
  162. return $this->renderSuccess($data);
  163. }
  164. /**
  165. * 推广订单列表 旧20220507准备废弃
  166. * @return array|\think\response\Json
  167. * @throws \app\common\exception\BaseException
  168. */
  169. public function promotionOrders(){
  170. $dataType = strval(request()->post('dataType',''));//订单状态(all已付款,received已付款待收货,completed已完成,close已失效)
  171. $keyword = strval(request()->post('keyword',''));
  172. $type = intval(request()->post('timeType',0));
  173. $from = strval(request()->post('from',''));
  174. $to = strval(request()->post('to',''));
  175. $shopId = intval(request()->post('shopId',0));
  176. $userId= UserService::getCurrentLoginUserId();
  177. $data = CommissionsDetail::getCommissionOrders($userId,$dataType,$type,$from,$to,$keyword,$shopId);
  178. return $this->renderSuccess($data);
  179. }
  180. /**
  181. * 推广订单列表 新
  182. * @return array|\think\response\Json
  183. * @throws \app\common\exception\BaseException
  184. */
  185. public function promotionOrdersNew(){
  186. $dataType = strval(request()->post('dataType',''));//订单状态(all已付款,received已付款待收货,completed已完成,close已失效)
  187. $keyword = strval(request()->post('keyword',''));
  188. $type = intval(request()->post('timeType',0));
  189. $from = strval(request()->post('from',''));
  190. $to = strval(request()->post('to',''));
  191. $shopId = intval(request()->post('shopId',0));
  192. $isDown = intval(request()->post('isDown',0));
  193. $userId= UserService::getCurrentLoginUserId();
  194. $data = CommissionsDetail::getCommissionOrdersNew($userId,$dataType,$type,$from,$to,$keyword,$shopId,$isDown);
  195. return $this->renderSuccess($data);
  196. }
  197. /**
  198. * 新增员工业绩榜单模块
  199. * @param $month
  200. * @return array|\think\response\Json
  201. * @throws \think\db\exception\DbException
  202. */
  203. public function staffAchievements(){
  204. //$user = User::detail( $this->userId)->toArray();
  205. $shopId = intval($this->request->get('shopId',0));
  206. //$shopId = $user['shop_id'];
  207. $type = request()->get('month',1);//1本周,2本月,3本年
  208. $to = time();
  209. if ($type == 1){
  210. $w = date('w',time()) - 1; //求得当前星期
  211. $from = time() - $w * 60 * 60 * 24;
  212. $from = date("Y-m-d",$from)." 00:00:00";
  213. $from = strtotime($from);
  214. }elseif($type == 2){
  215. $month = date('Y-m',time());
  216. $from = strtotime($month);
  217. }else{
  218. $month = date('Y',time()).'-01-01 '.'00:00:00';
  219. $from = strtotime($month);
  220. }
  221. $aches = User::staffAches($shopId,$from,$to);
  222. return $this->renderSuccess($aches);
  223. }
  224. /**
  225. * 推广中心-我的奖励金
  226. */
  227. public function bonusHistoryList($type = 0) {
  228. $model = new BonusHistory();
  229. $list = $model->getList($type);
  230. // 奖励金结算日 commission_days + 5
  231. $refundSetting = SettingModel::getItem('refund');
  232. $bonus_clearing_day = $refundSetting['commission_days'] + 5;
  233. return $this->renderSuccess(compact('list', 'bonus_clearing_day'));
  234. }
  235. /**
  236. * 推广中心-我的奖励金-门店明细
  237. */
  238. public function shopBonusList() {
  239. $type = intval($this->request->post('type',0));
  240. $year = intval($this->request->post('year',0));
  241. $month = intval($this->request->post('month',0));
  242. $curr_month = false;
  243. if($year == intval(date('Y')) && $month == intval(date('m'))){
  244. $curr_month = true;
  245. }
  246. $model = new BonusHistory();
  247. $list = $model->getShopBonusList($month,$year,$type);
  248. // 奖励金结算日 commission_days + 5
  249. return $this->renderSuccess(compact('list','curr_month'));
  250. }
  251. /**
  252. * 门店详情
  253. * @param $shopId
  254. * @return array|\think\response\Json
  255. */
  256. public function shopDetails($shopId){
  257. $shop = [];
  258. if (!$shopId)return $this->renderSuccess(compact('shop'));
  259. //门店销售额
  260. $from = strtotime(date('Y-m-01 00:00:00'));
  261. $shopM = new Shops();
  262. $shop = $shopM->where('shop_id',$shopId)->field('shop_id,shop_name,province_id,city_id,region_id')->find();
  263. //$shop['rec_sum'] = helper::bcadd($shopM->getRecOrdersAllSum($shopId,$from,time()),0,2);
  264. $shop['rec_sum'] = OrderGoods::sumShopGiveOutOrder($shopId,$from,time());
  265. $shop['binders'] = User::countShopBinder($shopId);
  266. //店员数量
  267. $shop['sellers'] = User::countShopSellers($shopId);
  268. return $this->renderSuccess(compact('shop'));
  269. }
  270. /**
  271. * 店员交接,离职
  272. * @return array
  273. */
  274. public function sellerHandover(){
  275. $user = User::detail( $this->userId)->toArray();
  276. if ($user['role'] != User::SHOP_MG && $user['role'] != User::SHOP_BOSS){
  277. return $this->renderError('无法进行此操作');
  278. }
  279. $shopId = intval($this->request->post('shopId',0));
  280. if (!$shopId)return $this->renderError('门店信息不存在');
  281. $oldSeller = intval($this->request->post('fromUserId',0));
  282. $toSeller = intval($this->request->post('toUserId',0));
  283. if (!$oldSeller || !$toSeller || ($oldSeller==$toSeller)){
  284. return $this->renderError('参数有误');
  285. }
  286. $m = new User();
  287. try {
  288. $count = $m->consumersHandover($oldSeller,$toSeller,$shopId);
  289. $m->updateUserRole($oldSeller);
  290. }catch (\Exception $e){
  291. log_record($e->getMessage(),'error');
  292. return $this->renderError('系统繁忙');
  293. }
  294. $old = User::where('user_id',$oldSeller)->find();
  295. //if(env('APP_DEBUG') == false) { }
  296. PushLog::addSmsMon('【五常大米官方体验店】您原店员身份已失效,为使您的账户中的收益金保持不变,我们为您保留了提现入口,您可以继续提现~', $old['mobile'], 1);
  297. $list = ['count'=>$count];
  298. return $this->renderSuccess($list);
  299. }
  300. /**
  301. * 聘请店长按钮
  302. * @param $shopId
  303. * @return array|\think\response\Json
  304. * @throws \think\db\exception\DataNotFoundException
  305. * @throws \think\db\exception\DbException
  306. * @throws \think\db\exception\ModelNotFoundException
  307. */
  308. public function hireManagerBtn($shopId){
  309. $shopM = new Shops();
  310. $shop = $shopM->where('shop_id',$shopId)->field('shop_id,shop_name,manager_user_id,province_id,city_id,region_id')->find();
  311. $shop->manager = null;
  312. if ($shop->manager_user_id){
  313. $shop->manager = User::find($shop->manager_user_id);
  314. }
  315. return $this->renderSuccess(compact('shop'));
  316. }
  317. /**
  318. * 提交聘请店长
  319. * @param $shopId
  320. * @throws \think\db\exception\DataNotFoundException
  321. * @throws \think\db\exception\DbException
  322. * @throws \think\db\exception\ModelNotFoundException
  323. */
  324. public function hireManager(){
  325. $shopId = intval($this->request->post('shopId',0));
  326. if (!$shopId)return $this->renderError('门店信息不存在');
  327. $shop = Shops::where('shop_id',$shopId)->find();
  328. if (empty($shop)){
  329. return $this->renderError('门店信息不存在');
  330. }
  331. if ($shop['boss_user_id'] != $this->userId){
  332. return $this->renderError('您不是本店老板哦');
  333. }
  334. $data['name'] = strval($this->request->post('name',''));
  335. $data['mobile'] = strval($this->request->post('mobile',''));
  336. $validate = Validate::rule('mobile', 'require|mobile')->rule([
  337. 'name' => 'require|max:25'
  338. ]);
  339. if (!$validate->check($data)) {
  340. return $this->renderError($validate->getError());
  341. }
  342. $user = User::where('mobile','=',$data['mobile'])->find();
  343. $model = new HireMgs();
  344. $data['user_id'] = $this->userId;
  345. $data['shop_id'] = $shopId;
  346. $data['mg_name'] = $data['name'];
  347. $data['mg_mobile'] = $data['mobile'];
  348. if (empty($user)){
  349. $model->save($data);
  350. $res = ['rid'=>$model->id];
  351. return $this->renderSuccess(compact('res'));
  352. }
  353. if ($user['role'] == User::SHOP_SELLER && $user['shop_id'] == $shopId){
  354. //如果是本店店员
  355. //判断是否有下级顾客
  356. $c = User::where('bind_shop_id',$shopId)->where('upper_user_id',$user['user_id'])->count();
  357. if ($c){
  358. return $this->renderError('请先办理店员交接,转移顾客');
  359. }else{
  360. $model->save($data);
  361. $res = ['rid'=>$model->id];
  362. return $this->renderSuccess(compact('res'));
  363. }
  364. }elseif (($user['role'] == User::SHOP_SELLER && $user['shop_id'] != $shopId) || $user['role'] == User::SHOP_BOSS){
  365. return $this->renderError('您邀请的店长已担任其他门店角色');
  366. }elseif ($user['role'] == User::SHOP_MG && $user['shop_id'] == $shopId){
  367. return $this->renderError('您邀请的用户已是店长,请勿重复邀请');
  368. }elseif(($user['role'] == User::SHOP_MG && $user['shop_id'] != $shopId) || ($user['role'] >User::SHOP_SELLER && $user['role'] < User::COMMISSION_USER)){
  369. return $this->renderError('您邀请的店长已担任其他门店角色');
  370. }elseif($user['role'] == User::COMMISSION_USER){
  371. return $this->renderError('您邀请的用户已是分销员,不支持升级店长');
  372. }elseif ($user['role'] == User::NORMAL_USER){
  373. // 如有上级,切断上级
  374. User::where('user_id',$user['user_id'])->update(['upper_user_id'=>0,'bind_shop_id'=>0]);
  375. $model->save($data);
  376. $res = ['rid'=>$model->id];
  377. return $this->renderSuccess(compact('res'));
  378. }else{
  379. return $this->renderError('保存失败');
  380. }
  381. return $this->renderError('保存失败');
  382. }
  383. /**
  384. * 店长离职-解聘店长
  385. * @return array|\think\response\Json
  386. */
  387. public function managerDimission(){
  388. $shopId = intval($this->request->get('shopId',0));
  389. if (!$shopId)return $this->renderError('门店信息不存在');
  390. $shop = Shops::where('shop_id',$shopId)->find();
  391. if (empty($shop)){
  392. return $this->renderError('门店信息不存在');
  393. }
  394. if ($shop['boss_user_id'] != $this->userId){
  395. return $this->renderError('您不是本店老板哦');
  396. }
  397. if (!$shop['manager_user_id']){
  398. return $this->renderError('您还没未本店聘请店长哦');
  399. }
  400. $mg = User::where('user_id',$shop['manager_user_id'])->find();
  401. if (!$mg){
  402. return $this->renderError('店长信息读取异常');
  403. }
  404. /* User::where('user_id',$shop['manager_user_id'])->update(['role'=>User::NORMAL_USER,'shop_id'=>0]);
  405. Shops::where('shop_id',$shopId)->update(['manager_user_id'=>0]);*/
  406. $shopM = new Shops();
  407. $shopM->dissMg($shop['manager_user_id'],$shopId);
  408. PushLog::addSmsMon('【五常大米官方体验店】您原店长身份已失效,为使您的账户中的收益金保持不变,我们为您保留了提现入口,您可以继续提现~',$mg['mobile'],1);
  409. return $this->renderSuccess();
  410. }
  411. /**
  412. * 店长加入团队
  413. * @return array|\think\response\Json
  414. */
  415. public function managerJoinShop(){
  416. $rid = intval($this->request->get('rid',0));
  417. if (!$rid)return $this->renderError('参数错误');
  418. $user = User::detail( $this->userId);
  419. $record = HireMgs::where('id',$rid)->where('status',0)->find();
  420. if (!$record){
  421. return $this->renderError('邀请链接已失效');
  422. }
  423. if ($user['role'] == User::SHOP_MG && $user['shop_id'] == $record['shop_id']){
  424. return $this->renderError('您已是此门店店长');
  425. }
  426. /* if ($user['role'] != User::NORMAL_USER){
  427. return $this->renderError('您不是普通用户或已担任其他门店角色');
  428. }*/
  429. if ($user['mobile'] != $record['mg_mobile']){
  430. return $this->renderError('不合要求');
  431. }
  432. $userM = new User();
  433. if ($userM->mgShopJoin($user['user_id'],$rid,$record['shop_id']) == false){
  434. return $this->renderError('系统繁忙');
  435. }
  436. $shop = Shops::where('shop_id',$record['shop_id'])->field('shop_id,shop_name')->find();
  437. return $this->renderSuccess(['shop_id'=>$shop['shop_id'],'shop_name'=>$shop['shop_name']]);
  438. }
  439. /**
  440. * 员工列表
  441. * @return array|\think\response\Json
  442. * @throws \think\db\exception\DataNotFoundException
  443. * @throws \think\db\exception\DbException
  444. * @throws \think\db\exception\ModelNotFoundException
  445. */
  446. public function staffList(){
  447. $shopId = intval($this->request->get('shopId',0));
  448. if (!$shopId)return $this->renderError('门店信息不存在');
  449. $res = User::where('shop_id',$shopId)->where('role',User::SHOP_SELLER)
  450. ->field('user_id,nick_name,mobile')->select();
  451. return $this->renderSuccess(compact('res'));
  452. }
  453. }