// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\api\controller\relations; use app\api\controller\Controller; use app\api\model\ApplySeller; use app\api\model\Order; use app\api\model\OrderGoods; use app\api\model\Setting as SettingModel; use app\api\model\shop\Shops; use app\api\model\ShopsDailySalesSt; use app\api\model\ShopsMonthlyBonusSt; use app\api\model\StaffMobile; use app\api\model\StoreDailySalesSt; use app\api\model\User; use app\api\model\user\BonusHistory; use app\common\model\GoodsSalesRank; use app\common\model\user\CommissionsDetail as CommissionDetailModel; use app\common\service\commission\GiveOutCommission; use app\common\service\commission\RecordWaitCommission; use app\console\service\DailyBonus as DailyBonusService; use app\console\service\MonthlyBonusClear; use app\store\model\ChannelSaleVolumeTj; use app\store\model\user\CommerCountTj; use think\facade\Queue; /** * 页面控制器 * Class Index * @package app\api\controller */ class Join extends Controller { /** * 记录员工手机号 * @return array|\think\response\Json * @throws \app\common\exception\BaseException */ public function recordStaffMobile(){ $mobile = strval(request()->post('mobile','')); $shopId = intval(request()->post('shopId',0)); if (!$shopId)return $this->renderError('参数错误'); $mobiles = explode(',',$mobile); /* try { $flag = $this->validate(['mobile'=>$mobile],['mobile'=>'mobile']); } catch (\think\exception\ValidateException $e) { $msg = $e->getError(); Log::error('flag::'.$e->getError()); return $this->renderSuccess($msg); }*/ $msg = '成功'; if (!$mobiles || !count($mobiles)){ return $this->renderSuccess($msg); } $errMsg = ''; foreach ($mobiles as $mob){ $flag = StaffMobile::recordMobile($mob,$shopId); if ($flag == false){ $errMsg .= $mob.'添加失败'; } } if ($errMsg){ $msg = $errMsg; //$msg = '请检查店员手机号是否正确'; //return $this->renderError($msg); } return $this->renderSuccess($msg); } /** * 加入店员 * @return array|\think\response\Json */ public function addNewStaff() { $managerId = intval(request()->post('managerId',0)); $shop_name = User::getShopInfo($managerId); //$mobile = strval(request()->post('mobile','')); $msg = '成功'; $f = User::updateShopId($managerId); if ($f == false){ $msg = '请核对手机号码'; return $this->renderError($msg); } return $this->renderSuccess(['shop_name'=>$shop_name]);//compact('data') } /** * 申请成为推荐官 * @return array|\think\response\Json * @throws \app\common\exception\BaseException */ public function applyBeSeller(){ $model = new ApplySeller(); if (!$model->addNew($this->request->post())){ return $this->renderError($model->getError()); } return $this->renderSuccess('申请成功'); } /** * 获取推荐官的等级说明 * @author: zjwhust * @Time: 2021/12/11 11:03 */ public function getDistributorList(){ $distributor_grade = SettingModel::getItem('distributor_grade');//分销员推广佣金配置 return $this->renderSuccess($distributor_grade); } /** * 测试用,订单待结算佣金加入队列接口,异常情况的订单也可用 * @return array|\think\response\Json */ public function testRecordWaitCommission(){ $orderId = intval($this->request->post('orderId',0)); if (!$orderId){ return $this->renderError('orderId不对'); } $jobHandlerClassName = 'app\job\recordWaitCommissionsDetail'; $jobQueueName = "orderWaitCommissionsQueue"; //数组数据 $orderData = [ 'orderId' => $orderId,//uniqid() ]; $isPushed = Queue::push($jobHandlerClassName, $orderData ,$jobQueueName); //$isPushed = Queue::later(5, $jobHandlerClassName, $orderData, $jobQueueName); if( $isPushed !== false ){ $msg = date('Y-m-d H:i:s') . " 队列添加成功"; }else{ $msg = '队列添加失败'; } return $this->renderSuccess($msg); } //测试用,写待分佣记录 public function testWaitCommission($orderId){ $orderId = intval($orderId); if($orderId < 1){ return $this->renderError('参数有误'); } $o = new RecordWaitCommission(); $flag = $o->waitGiveOutCommission($orderId); $msg = $flag?'success':'failed'; return $this->renderSuccess($msg); } //测试用,收货后分佣结算 public function testConfirmGoodsCommission($orderId) { $orderId = intval($orderId); if($orderId < 1){ return $this->renderError('参数有误'); } $con = new GiveOutCommission(); $con->testConfirmOrderCommission($orderId); return $this->renderSuccess('成功'); } /** * 每日计算奖励金接口 * @return array|\think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function dailyBonusClear(){ set_time_limit(120); $service = new DailyBonusService; //v1.3.6版本奖励金区分门店和角色 $service->calBonusMoney36($this->storeId); return $this->renderSuccess('成功'); } /** * 每月20日结算奖励金接口 * @return array|\think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function monthlyBonusClear(){ $debug = boolval($this->request->get('debug',false)); $service = new MonthlyBonusClear(); if ($debug == true){ //$service->clearBonusMoneyTest($this->storeId); $service->clearBonusMoney36Test($this->storeId); }else{ //$service->clearBonusMoney($this->storeId); $service->clearBonusMoney36($this->storeId); } return $this->renderSuccess('成功'); } /** * 测试用-清除当月的奖励金结算状态 * @return array|\think\response\Json */ public function monthlyBonusClearFlush(){ if (env('APP_DEBUG') == true){ $yearMonth = intval(date('Ym')); BonusHistory::where('year_month',$yearMonth)->update(['clearing_status'=>0]); } return $this->renderSuccess('成功清除本月奖励金结算状态'); } /** * 每日统计门店销售数据 * @return array|\think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function dailyShopSalesStatistics(){ $flag = $this->request->get('flag',0);//1:统计今天的,供测试使用 $shops = Shops::where('is_delete',0)->field('shop_id,province_id,city_id,region_id')->select(); $m = new ShopsDailySalesSt(); //$orderM = new OrderGoods(); $orderM = new Order(); $to = strtotime(date("Y-m-d"),time()); if($flag == 1 && env('APP_DEBUG')){ $from = $to; $dayMid = $to + 43200; $to = $to + 86399; }else{ $from = $to - 86400; $dayMid = $to - 43200; $to -= 1; } $now = time(); $datas = []; foreach ($shops as $shop){ $temp = []; $tj = $orderM->dailySalesTj($shop['shop_id'],$from,$to); $temp['total_price'] = $tj['total_price']; $temp['total_goods_price'] = $tj['total_goods_price']; $temp['total_pay_price'] = $tj['total_pay_price']; $temp['coupon_money'] = $tj['coupon_money']; $temp['order_count'] = $tj['order_count']; $temp['express_price'] = $tj['express_price']; $temp['boss_comm_money'] = $tj['boss_comm_money']; $temp['mg_comm_money'] = $tj['mg_comm_money']; $temp['seller_comm_money'] = $tj['seller_comm_money']; $temp['shop_id'] = $shop['shop_id']; $temp['st_time'] = $dayMid; $temp['create_time'] = $now; $temp['update_time'] = $now; $datas[] = $temp; } //批量插入,当门店过多时,需优化成分批插入 $m->where('st_time',$dayMid)->delete(); $m->insertAll($datas); return $this->renderSuccess(); } /** * 每月21号统计门店月奖励金 * @return array|\think\response\Json */ public function monthlyShopBonusStatistics(){ $flag = $this->request->get('flag',0);//1:统计本月的,供测试使用 $shops = Shops::where('is_delete',0)->field('shop_id,province_id,city_id,region_id,boss_user_id,manager_user_id')->select(); $m = new ShopsMonthlyBonusSt(); if($flag == 1 && env('APP_DEBUG')){ $from = strtotime(date('Y-m-01 00:00:00',time()));//本月开始时间 $to = strtotime(date('Y-m-01 00:00:00',strtotime("+1 month",time()))) - 1; $year = date('Y'); $month = intval(date('m')); }else{ $to = strtotime(date('Y-m-01 00:00:00',time())) - 1; $from = strtotime(date('Y-m-01',$to)); $year = date("Y", strtotime("-1 month")); $month = intval(date("m", strtotime("-1 month"))); } $bonusM = new BonusHistory(); $datas = []; $stYearMonth = date('Ym',$from); $now = time(); foreach ($shops as $shop){ $data = $bonusM->getRoleBonus($shop,$from,$to,$year,$month); $data['shop_id'] = $shop['shop_id']; $data['st_year'] = $year; $data['st_month'] = $month; $data['st_year_month'] = $stYearMonth; $data['create_time'] = $now; $data['update_time'] = $now; $datas[] = $data; } $m->where('st_year_month',$stYearMonth)->delete(); $m->insertAll($datas); return $this->renderSuccess(); } /** * 每日统计商城销售数据 * @return array|\think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function dailyStoreSalesStatistics(){ $flag = $this->request->get('flag',0);//1:统计今天的,供测试使用 $m = new StoreDailySalesSt(); $orderM = new Order(); $to = strtotime(date("Y-m-d"),time()); if($flag == 1 && env('APP_DEBUG')){ $from = $to; $to = $to + 86399; }else{ $from = $to - 86400; $to -= 1; } $dayMid = date('Ymd',$to); $tj = $orderM->dailyStoreSaleVolumeSt($from,$to); $m->addOne($tj,$dayMid); //商品销量排行昨日统计数据添加 (new GoodsSalesRank)->addGoodsSalesRankDaily(); return $this->renderSuccess(); } /** * 测试-统计渠道销售额 * @return array|\think\response\Json */ public function testGenChannelSaleVolume(){ $now = time() + 86400; //推荐官销售数据统计 $csvTj = new ChannelSaleVolumeTj(); $csvTj->day($now); $csvTj->week($now); $csvTj->month($now); $commerTj = new CommerCountTj(); $commerTj->day($now); $commerTj->week($now); $commerTj->month($now); return $this->renderSuccess(); } }