enterpriseId = $enterpriseId; $this->userCenterId = $userCenterId; $this->objCommissionCache = new CommissionCache($enterpriseId, $userCenterId); $this->objMCustomer = new MCustomer($enterpriseId, $userCenterId); $this->objMCommissionGrade = new MCommissionGrade($enterpriseId, $userCenterId); $this->objMCommissionSetting = new MCommissionSetting($enterpriseId, $userCenterId); $this->objDCommissionBusinessman = new DCommissionBusinessman(); $this->objDCommissionBusinessman->setTable($this->objDCommissionBusinessman->get_Table().'_'.$enterpriseId); $this->objDCustomer = new DCustomer(); $this->objDCustomer->setTable('qianniao_customer_'.$enterpriseId); } /** * 新增分销商 * @param $params * @return ResultWrapper * @throws Exception */ public function addBusinessman($params) { $params['deleteStatus'] = StatusCode::$standard; $params['createTime'] = time(); $params['updateTime'] = time(); //判断分销商是否存在 $modelResult = $this->objMCustomer->getCustomerData(['id' => $params['customerId'], 'deleteStatus' => StatusCode::$standard, 'status' => StatusCode::$auditStatus['auditPass']]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $customerData = $modelResult->getData(); unset($modelResult); if(empty($customerData)){ return ResultWrapper::fail('客户不存在', ErrorCode::$paramError); } if($customerData['isCommission'] == StatusCode::$standard){ return ResultWrapper::fail('该客户已经是分销商', ErrorCode::$paramError); } !empty($customerData['commissionPath']) && $customerData['commissionPath'] = json_decode($customerData['commissionPath'], true); $params['userCenterId'] = $customerData['userCenterId']; //默认等级 $params['grade'] = isset($params['grade']) ? $params['grade'] : 0; $modelResult = $this->objMCommissionGrade->getInfoGrade(['grade' => $params['grade'], 'deleteStatus' => StatusCode::$standard]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $gradeData = $modelResult->getData(); unset($modelResult); if(empty($gradeData)){ return ResultWrapper::fail('默认等级查询失败', ErrorCode::$paramError); } if($gradeData['enableStatus'] == StatusCode::$delete){ return ResultWrapper::fail('等级已被禁用', ErrorCode::$paramError); } $params['gradeId'] = $gradeData['id']; //查询设置 $modelResult = $this->objMCommissionSetting->getSetting(); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $setting = $modelResult->getData(); unset($modelResult); $params['applicationCondition'] = $setting['businessmanCondition']; $params['applicationComplete'] = StatusCode::$delete; //判断成为分销商条件 if($setting['businessmanCondition'] == 0 || ($setting['businessmanCondition'] == 1 && $setting['audit'] == StatusCode::$delete)){ //无条件 或者 资料申请 分销商审核关闭 //状态已审核 $params['auditStatus'] = StatusCode::$auditStatus['auditPass']; $params['auditTime'] = time(); $params['applicationComplete'] = StatusCode::$standard; //修改客户表字段 $updateCustomerData = [ 'isCommission' => StatusCode::$standard, ]; $modelResult = $this->objMCustomer->updateCustomer($updateCustomerData, ['id' => $params['customerId']]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } unset($modelResult); //修改分销商数据 //修改客户数据 } $params['info'] = json_encode($params['info']); //上级添加统计字段 if($params['auditStatus'] == StatusCode::$auditStatus['auditPass']){ //上1级 下级分销商人数 下一级分销商人数 if(isset($customerData['commissionPath'][StatusCode::$level['one']])){ $modelResult = self::outSetStatistics($customerData['commissionPath'][StatusCode::$level['one']], ['statistics' => ['subBusinessman' => 1, 'oneSubBusinessman' => 1]]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } } //上2级 下级分销商人数 if(isset($customerData['commissionPath'][StatusCode::$level['two']]) && $setting['level'] >= StatusCode::$level['two']){ $modelResult = self::outSetStatistics($customerData['commissionPath'][StatusCode::$level['two']], ['statistics' => ['subBusinessman' => 1]]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } } //上3级 下级分销商人数 if(isset($customerData['commissionPath'][StatusCode::$level['three']]) && $setting['level'] >= StatusCode::$level['three']){ $modelResult = self::outSetStatistics($customerData['commissionPath'][StatusCode::$level['three']], ['statistics' => ['subBusinessman' => 1]]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } } } //添加分销商 $dbResult = $this->objDCommissionBusinessman->insert($params); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } /** * 修改分销商 * @param $update * @param $where * @return ResultWrapper * @throws Exception */ public function updateBusinessman($update, $where = []) { //分销商审核 if (!isset($where['deleteStatus'])){ $where['deleteStatus'] = StatusCode::$standard; } $dbResult = $this->objDCommissionBusinessman->get($where); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } $businessman = $dbResult; unset($dbResult); if(empty($businessman)){ return ResultWrapper::fail('分销商不存在', ErrorCode::$paramError); } if(isset($update['auditStatus']) && $update['auditStatus'] == StatusCode::$auditStatus['auditPass']){ if($businessman['auditStatus'] == StatusCode::$auditStatus['auditPass']){ return ResultWrapper::fail('分销商已审核', ErrorCode::$paramError); } $update['auditTime'] = time(); //查询客户 $modelResult = $this->objMCustomer->getCustomerData(['id' => $businessman['customerId'], 'status' => StatusCode::$auditStatus['auditPass'], 'deleteStatus' => StatusCode::$standard]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $customerData = $modelResult->getData(); unset($modelResult); if(empty($customerData)){ return ResultWrapper::fail('客户不存在', ErrorCode::$paramError); } //修改客户表字段 $updateCustomerData = [ 'isCommission' => StatusCode::$standard, ]; $modelResult = $this->objMCustomer->updateCustomer($updateCustomerData, ['id' => $businessman['customerId']]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } unset($modelResult); //查询设置 $modelResult = $this->objMCommissionSetting->getSetting(); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $setting = $modelResult->getData(); unset($modelResult); //上级添加统计字段 if(!empty($customerData['commissionPath'])){ $customerData['commissionPath'] = json_decode($customerData['commissionPath'], true); //上1级 下级分销商人数 下一级分销商人数 if(isset($customerData['commissionPath'][StatusCode::$level['one']])){ $modelResult = self::outSetStatistics($customerData['commissionPath'][StatusCode::$level['one']], ['subBusinessman' => 1, 'oneSubBusinessman' => 1]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } } //上2级 下级分销商人数 if(isset($customerData['commissionPath'][StatusCode::$level['two']]) && $setting['level'] >= StatusCode::$level['two']){ $modelResult = self::outSetStatistics($customerData['commissionPath'][StatusCode::$level['two']], ['subBusinessman' => 1]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } } //上3级 下级分销商人数 if(isset($customerData['commissionPath'][StatusCode::$level['three']]) && $setting['level'] >= StatusCode::$level['three']){ $modelResult = self::outSetStatistics($customerData['commissionPath'][StatusCode::$level['three']], ['subBusinessman' => 1]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } } } } //分销商删除 if(isset($update['deleteStatus']) && $update['deleteStatus'] == StatusCode::$delete){ //修改客户表 $updateCustomerData = [ 'isCommission' => StatusCode::$delete, ]; $modelResult = $this->objMCustomer->updateCustomer($updateCustomerData, ['id' => $businessman['customerId']]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } unset($modelResult); } $dbResult = $this->objDCommissionBusinessman->update($update, $where); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } //删除缓存 $this->objCommissionCache->delBusinessmanGrade($businessman['userCenterId']); return ResultWrapper::success($dbResult); } /** * 分销商列表 * @param $params * @param $export * @param $distribution * @return ResultWrapper */ public function getAllBusinessman($params = [],$export =0,$distribution=0) { $sql = 'select b.*,u.mobile,c.name,c.avatar,c.createTime as customerCreateTime from qianniao_user_center u left join qianniao_customer_'.$this->enterpriseId.' c on c.userCenterId = u.id left join '.$this->objDCommissionBusinessman->get_Table().' b on b.userCenterId = u.id'; $whereSql = ' where c.status = '.StatusCode::$auditStatus['auditPass'].' and c.deleteStatus = '.StatusCode::$standard.' and b.deleteStatus = '.StatusCode::$standard; if(isset($params['search'])){ $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") '; } if(isset($params['auditStatus'])){ $whereSql .= ' and b.auditStatus = '.$params['auditStatus']; } if(isset($params['grade'])){ $whereSql .= ' and b.grade = '.$params['grade']; } if(isset($params['createStar']) && isset($params['createEnd'])){ $whereSql .= ' and c.createTime between '.$params['createStar'].' and '.$params['createEnd']; } if(isset($params['applicationStar']) && isset($params['applicationEnd'])){ $whereSql .= ' and b.createTime between '.$params['applicationStar'].' and '.$params['applicationEnd']; } if(isset($params['auditStar']) && isset($params['auditEnd'])){ $whereSql .= ' and b.auditTime between '.$params['auditStar'].' and '.$params['auditEnd']; } $orderSql = ' order by b.createTime desc '; if($export){ $limitSql = ''; }else{ $limitSql = ' limit '.$params['offset'].','.$params['limit']; } $selectSql = $sql.$whereSql.$orderSql.$limitSql; $dbResult = $this->objDCommissionBusinessman->query($selectSql); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } $returnData = $dbResult; unset($dbResult); if(empty($returnData)){ $return = [ 'data' => $returnData, 'total' => 0, ]; return ResultWrapper::success($return); } //总数 $sql = 'select count(*) as `count` from qianniao_user_center u left join qianniao_customer_'.$this->enterpriseId.' c on c.userCenterId = u.id left join '.$this->objDCommissionBusinessman->get_Table().' b on b.userCenterId = u.id'; $selectSql = $sql.$whereSql; $dbResult = $this->objDCommissionBusinessman->query($selectSql); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } $dbResult = array_shift($dbResult); $total = $dbResult['count']; unset($dbResult); //格式化 $modelResult = self::formatBusinessman($returnData); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $returnData = $modelResult->getData(); unset($modelResult); $return = [ 'data' => $returnData, 'total' => ($total) ? intval($total) : 0, ]; if($export){ self::exportBusinessman($return['data'],$distribution); exit; } return ResultWrapper::success($return); } /** * 分销商审核导出方法 * @param $businessmanData * @param $distribution */ public function exportBusinessman($businessmanData,$distribution) { //导出到本地 header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:filename=分销商审核.csv"); header('Cache-Control: max-age=0'); $fp = fopen('php://output', 'a'); if($distribution){ $head = ['姓名', '手机号','等级', '累计佣金', '提现佣金','成为分销商时间','下线总数', '下线分销商总数']; //定义标题 }else{ $head = ['姓名', '手机号','等级', '累计消费次数', '累计消费金额','注册时间','申请状态', '申请时间']; //定义标题 } foreach ($head as $i => $v) { $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码 } fputcsv($fp, $head); $limit = 10000; $num = 0;//计数器 foreach ($businessmanData as $value) {//循环数据 $auditOrderTotal = '0'; $auditTotalMoney = '0'; $subBusinessman ='0'; $sub ='0'; if(!empty($value['statistics'])){ $statistics = $value['statistics']; $auditOrderTotal = $statistics['auditOrderTotal']; $auditTotalMoney = $statistics['auditTotalMoney']; $subBusinessman = $statistics['subBusinessman']; $sub = $statistics['sub']; } $num++; if ($num == $limit) { ob_flush();//释放内存 flush(); } if($distribution){ $rows['name'] = isset($value['name']) ? $value['name'] : null; // 姓名 $rows['mobile'] = isset($value['mobile']) ? $value['mobile'] : null; // 手机号 $rows['gradeName'] = isset($value['gradeName']) ? $value['gradeName'] : null; // 等级 $rows['totalMoney'] = isset($value['totalMoney']) ? $value['totalMoney'] : null; // 累计佣金 $rows['waitMoney'] = isset($value['waitMoney']) ? $value['waitMoney'] : null; // 提现佣金 $rows['distribution'] = isset($value['createTime']) ? date("Y-m-d H:i:s",$value['createTime']) : null; // 成为分销商时间 $rows['sub'] = $sub; // 下线总数 $rows['subBusinessman'] = $subBusinessman; // 下线分销商总数 }else{ $rows['name'] = isset($value['name']) ? $value['name'] : null; // 姓名 $rows['mobile'] = isset($value['mobile']) ? $value['mobile'] : null; // 手机号 $rows['gradeName'] = isset($value['gradeName']) ? $value['gradeName'] : null; // 等级 $rows['auditOrderTotal'] = $auditOrderTotal; // 累计消费次数 $rows['auditTotalMoney'] = $auditTotalMoney; // 累计消费金额 $rows['loginTime'] = isset($value['createTime']) ? date("Y-m-d H:i:s",$value['createTime']) : null; // 注册时间 $rows['applicationComplete'] = isset($value['applicationComplete'])&& $value['applicationComplete']== 4? '未满足' : '满足'; // 申请状态 $rows['createTime'] = isset($value['createTime']) ? date("Y-m-d H:i:s",$value['createTime']) : null; // 申请时间 } foreach ($rows as $kk => $vv) { $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码 } fputcsv($fp, $rs); $rows = []; } } /** * 分销商详情 * @param $params * @return ResultWrapper */ public function getInfoBusinessman($params) { // 查询分销商信息 $params['deleteStatus'] = StatusCode::$standard; $dbResult = $this->objDCommissionBusinessman->get($params); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } // 格式化等级 $modelResult = self::formatBusinessman($dbResult); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $return = $modelResult->getData(); unset($modelResult); return ResultWrapper::success($return); } /** * 格式化分销商数据 * @param $params 分销商信息数据 * @return ResultWrapper */ public function formatBusinessman($params) { if(empty($params)){ return ResultWrapper::success($params); } $format = isset($params['id']) ? [$params] : $params; $userCenterIds = []; $selectCustomer = []; $selectUserCenter = []; $subTotalUserCenterIds = []; foreach($format as $value){ if(isset($value['userCenterId'])){ $userCenterIds[] = $value['userCenterId']; if(!isset($value['mobile']) || empty($value['mobile'])){ $selectUserCenter[] = $value['userCenterId']; } } if(isset($value['customerId']) && (!isset($value['name']) || empty($value['name']))){ $selectCustomer[] = $value['customerId']; } //判断是否有统计下级人数参数字段 if(!empty($value['statistics'])){ $statistics = json_decode($value['statistics'], true); if(isset($statistics['sub'])){ $subTotalUserCenterIds[] = $value['userCenterId']; } } } //查询设置 $modelResult = $this->objMCommissionSetting->getSetting(); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $setting = $modelResult->getData(); unset($modelResult); //查询等级 $modelResult = self::getBusinessmanGrade($userCenterIds); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $businessmanGrade = $modelResult->getData(); unset($modelResult); //查询userCenter手机号 $userData = []; if(!empty($selectUserCenter)){ $objDUserCenter = new DUserCenter(); $dbResult = $objDUserCenter->select(['id' => $userCenterIds], 'id,mobile'); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } foreach($dbResult as $value){ $userData[$value['id']] = $value['mobile']; } unset($dbResult); } //查询客户数据获取name $customer = []; if(!empty($selectCustomer)){ $modelResult = $this->objMCustomer->getCustomerData(['id' => $selectCustomer, 'deleteStatus' => StatusCode::$standard, 'status' => StatusCode::$auditStatus['auditPass']], 'id,name,avatar', false); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $customerData = $modelResult->getData(); unset($modelResult); foreach($customerData as $value){ $customer[$value['id']] = [ 'name' => $value['name'], 'avatar' => $value['avatar'], ]; } unset($customerData); } //查询所有分销商下级统计人数 $subStatistics = []; if(!empty($subTotalUserCenterIds)){ $modelResult = self::getBusinessmanSubLevelNum($subTotalUserCenterIds); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $subStatistics = $modelResult->getData(); unset($modelResult); } //查询所有条件映射 $modelResult = $this->objMCommissionGrade->getAllGradeCondition(); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $condition = $modelResult->getData(); unset($modelResult); if(empty($condition)){ return ResultWrapper::fail('条件查询错误', ErrorCode::$paramError); // $condition = [];//影子:分销商等级升级条件表 } $conditionData = []; foreach($condition as $value){ $conditionData[$value['key']] = 0; } foreach($format as &$value){ $value['gradeName'] = ''; $value['oneRate'] = 0.00; $value['twoRate'] = 0.00; $value['threeRate'] = 0.00; if(isset($businessmanGrade[$value['userCenterId']])){ $value['gradeName'] = $businessmanGrade[$value['userCenterId']]['gradeName']; $value['oneRate'] = $businessmanGrade[$value['userCenterId']]['oneRate']; $value['twoRate'] = $businessmanGrade[$value['userCenterId']]['twoRate']; $value['threeRate'] = $businessmanGrade[$value['userCenterId']]['threeRate']; } if(isset($value['info']) && !empty($value['info'])){ $value['info'] = json_decode($value['info'], true); } //格式化手机号 if(!empty($selectUserCenter)){ $value['mobile'] = ''; if(isset($userData[$value['userCenterId']])){ $value['mobile'] = $userData[$value['userCenterId']]; } } //格式化name if(!empty($selectCustomer)){ $value['name'] = ''; if(isset($customer[$value['customerId']])){ $value['name'] = $customer[$value['customerId']]['name']; $value['avatar'] = $customer[$value['customerId']]['avatar']; } } //格式化统计数据 if(!empty($value['statistics'])){ $value['statistics'] = json_decode($value['statistics'], true); foreach($conditionData as $key => $v){ if($key == 'sub' || $key == 'subBusinessman'){ $value['statistics'][$key] = $subStatistics[$value['userCenterId']][$key][$setting['level']]; }else{ if(!isset($value['statistics'][$key])) { $value['statistics'][$key] = 0; } } } }else{ $value['statistics'] = $conditionData; } } $format = isset($params['id']) ? array_shift($format) : $format; return ResultWrapper::success($format); } /** * 查询分销商等级佣金比例 * @param $userCenterIds * @return ResultWrapper */ public function getBusinessmanGrade($userCenterIds) { $this->objCommissionCache->delBusinessmanGrade(true); if(empty($userCenterIds)) return ResultWrapper::success($userCenterIds); $returnData = []; $emptyData = []; foreach($userCenterIds as $userCenterId){ //查询缓存 $cacheData = $this->objCommissionCache->getBusinessmanGrade($userCenterId); if(!empty($cacheData)){ $returnData[$userCenterId] = $cacheData; }else{ $emptyData[] = $userCenterId; } } if(empty($emptyData)){ return ResultWrapper::success($returnData); } // 查询分销商数据 $dbResult = $this->objDCommissionBusinessman->select(['userCenterId' => $emptyData, 'deleteStatus' => StatusCode::$standard]); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } $businessman = $dbResult; unset($dbResult); if(empty($businessman)){ return ResultWrapper::success($returnData); } // 查询分销商等级数据 $where['grade'] = []; foreach($businessman as $value){ !in_array($value['grade'], $where['grade']) && $where['grade'][] = $value['grade']; } $modelResult = $this->objMCommissionGrade->getAllGrade($where); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $grade = $modelResult->getData(); unset($modelResult); if(empty($grade)){ return ResultWrapper::success($returnData); } $gradeData = []; foreach($grade as $value){ $gradeData[$value['grade']] = $value; } foreach($businessman as $value){ $cacheArray = [ 'businessmanId' => $value['id'], 'userCenterId' => $value['userCenterId'], 'customerId' => $value['customerId'], 'grade' => $value['grade'], 'gradeId' => $value['gradeId'], 'gradeName' => $gradeData[$value['grade']]['name'], 'oneRate' => $gradeData[$value['grade']]['oneRate'], 'twoRate' => $gradeData[$value['grade']]['twoRate'], 'threeRate' => $gradeData[$value['grade']]['threeRate'] ]; $cacheResult = $this->objCommissionCache->saveBusinessmanGrade($value['userCenterId'], $cacheArray); if($cacheResult === false){ return ResultWrapper::fail('分销商等级数据缓存添加失败', ErrorCode::$paramError); } $returnData[$value['userCenterId']] = $cacheArray; } foreach($userCenterIds as $value){ if(!isset($returnData[$value])){ $returnData[$value] = []; } } return ResultWrapper::success($returnData); } /** * 保存分销商统计数据 * @param $params * @return ResultWrapper */ public function saveBusinessmanStatistics($params) { $paramsTest = [ 1 => [//userCenterId 'orderTotal' => 1000, // 条件别名 => 条件值 'sonOrder' => 100, 'commissionMoneyTotal' => 10000.00 ], 2 => [//userCenterId 'orderTotal' => 2000, // 条件别名 => 条件值 'sonOrder' => 200, 'commissionMoneyTotal' => 20000.00 ] ]; if(empty($params)){ return ResultWrapper::fail('分销商统计数据参数为空', ErrorCode::$paramError); } foreach($params as $userCenterId => $value){ $statisticsData = $this->objCommissionCache->getBusinessmanStatistics($userCenterId); if(empty($statisticsData)){ $dbResult = $this->objDCommissionBusinessman->get(['userCenterId' => $userCenterId, 'deleteStatus' => StatusCode::$standard, 'auditStatus' => StatusCode::$auditStatus['auditPass']]); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } if(empty($dbResult)){ return ResultWrapper::fail($userCenterId.'分销商数据查询失败', ErrorCode::$paramError); } if(!empty($dbResult['statistics'])){ $statisticsData = json_decode($dbResult['statistics'], true); } } foreach($value as $key => $v){ $statisticsData[$key] = isset($statisticsData[$key]) ? bcadd($statisticsData[$key], $v) : $v; } $cacheResult = $this->objCommissionCache->saveBusinessmanStatistics($userCenterId, $statisticsData); if(!$cacheResult){ return ResultWrapper::fail('缓存保存失败', ErrorCode::$paramError); } } return ResultWrapper::success(true); } /** * 获取分销商统计数据 * @param $userCenterIds * @return ResultWrapper */ public function getBusinessmanStatistics($userCenterIds) { if(empty($userCenterIds)){ return ResultWrapper::success($userCenterIds); } $returnData = []; $emptyArray = []; foreach($userCenterIds as $userCenterId){ $cacheResult = $this->objCommissionCache->getBusinessmanStatistics($userCenterId); if($cacheResult){ $returnData[$userCenterId] = $cacheResult; }else{ $emptyArray[] = $userCenterId; } } if(empty($emptyArray)){ return ResultWrapper::success($returnData); } $dbResult = $this->objDCommissionBusinessman->select(['userCenterId' => $emptyArray, 'deleteStatus' => StatusCode::$standard, 'auditStatus' => StatusCode::$auditStatus['auditPass']]); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } if(empty($dbResult)){ return ResultWrapper::success($returnData); } $formatData = []; foreach($dbResult as $value){ if(isset($value['statistics']) && !empty($value['statistics'])){ $formatData[$value['userCenterId']] = json_decode($value['statistics'], true); } } foreach($emptyArray as $userCenterId){ if(isset($formatData[$userCenterId])){ $returnData[$userCenterId] = $formatData[$userCenterId]; $cacheResult = $this->objCommissionCache->saveBusinessmanStatistics($userCenterId ,$formatData[$userCenterId]); if(!$cacheResult){ return ResultWrapper::fail('保存缓存失败', ErrorCode::$paramError); } } } return ResultWrapper::success($returnData); } /** * 创建上下级关系 * @param $params * @return ResultWrapper * @throws Exception */ public function relationshipBusinessman($params) { if(empty($params)){ return ResultWrapper::fail('创建上下级关系参数为空', ErrorCode::$paramError); } if(!isset($params['businessmanId']) || empty($params['businessmanId'])){ return ResultWrapper::fail('创建上下级关系businessmanId参数为空', ErrorCode::$paramError); } // 获取分销基础设置数据 $objMCommissionSetting = new MCommissionSetting($this->enterpriseId, $this->userCenterId); $modelResult = $objMCommissionSetting->getSetting(); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $setting = $modelResult->getData(); unset($modelResult); if(empty($setting)){ return ResultWrapper::fail('分销设置查询失败', ErrorCode::$paramError); } // 判断分销等级是否开启 if($setting['level'] == StatusCode::$level['off']){ return ResultWrapper::fail('分销已关闭', ErrorCode::$paramError); } // 判断是否满足成为下线条件 if($params['source'] != $setting['subConditions']){ return ResultWrapper::success(true); } // 获取客户信息 $modelResult = $this->objMCustomer->getCustomerData(['id' => $params['businessmanId'], 'deleteStatus' => StatusCode::$standard]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $parent = $modelResult->getData(); unset($modelResult); if(empty($parent)){ return ResultWrapper::fail('您的上级分销商客户信息查询为空', ErrorCode::$notShowError); } // 判断是否为同一个人 if($parent['userCenterId'] == $this->userCenterId){ return ResultWrapper::fail('不能添加自己为上级', ErrorCode::$notShowError); } // 判断上级是否是分销商 if($parent['isCommission'] != StatusCode::$standard){ return ResultWrapper::fail('您的上级不是分销商', ErrorCode::$notShowError); } // 上级的分销层级关系 $parentCommissionPath = []; if(!empty($parent['commissionPath'])){ $parentCommissionPath = json_decode($parent['commissionPath'], true); } // 判断下级是否已经有上级 $modelResult = $this->objMCustomer->getCustomerData(['userCenterId' => $this->userCenterId, 'deleteStatus' => StatusCode::$standard]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $customer = $modelResult->getData(); unset($modelResult); if(empty($customer)){ return ResultWrapper::fail('当前客户信息查询为空', ErrorCode::$notShowError); } if(!empty($customer['commissionPath'])){ return ResultWrapper::fail('您已经有上级分销商', ErrorCode::$notShowError); } //添加上下级关系 $one = StatusCode::$level['one']; $two = StatusCode::$level['two']; $three = StatusCode::$level['three']; $commissionPath[$one] = $parent['userCenterId']; //增加1级 下线人数 一级下线人数 $modelResult = self::outSetStatistics($commissionPath[$one], ['statistics' => ['sub' => 1, 'oneSub' => 1]]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } if(!empty($parentCommissionPath)){ if(isset($parentCommissionPath[$one])){ $commissionPath[$two] = $parentCommissionPath[$one]; if($setting['level'] >= StatusCode::$level['two']) { //增加2级 下线人数 $modelResult = self::outSetStatistics($commissionPath[$two], ['statistics' => ['sub' => 1]]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } } } if(isset($parentCommissionPath[$two])){ $commissionPath[$three] = $parentCommissionPath[$two]; if($setting['level'] >= StatusCode::$level['three']){ //增加3级 下线人数 $modelResult = self::outSetStatistics($commissionPath[$three], ['statistics' => ['sub' => 1]]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } } } } $update = [ 'commissionPath' => json_encode($commissionPath), 'updateTime' => time() ]; $modelResult = $this->objMCustomer->updateCustomer($update, ['userCenterId' => $this->userCenterId]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $return = $modelResult->getData(); unset($modelResult); return ResultWrapper::success($return); } /** * 回写统计数据 * @param $userCenterId * @param $params * @return ResultWrapper * @throws Exception */ public function outSetStatistics($userCenterId, $params) { /*$params = [ 'balance' => 100.00, //余额 'waitMoney' => 100.00, //待入账金额 'withdraw' => 100.00, //已提现金额 'totalMoney' => 100.00, //累计佣金 'skuIds' => [1,2,3,4],//订单购买商品skuId 'statistics' => [ 'orderMoney' => 100.00, //分销订单总额 'orderNum' => 100, //分销订单总数 'oneOrderMoney' => 100.00, //一级分销订单总额 'oneOrderNum' => 100, //一级分销订单总数 'selfOrderMoney' => 100.00, //自购订单总额 'selfOrderNum' => 100, //自购订单总数 'sub' => 100, //下线人数 'subBusinessman' => 100, //下线分销商人数 'oneSub' => 100, //一级下线人数 'oneSubBusinessman' => 100, //一级下线分销商 'auditOrderTotal' => 100, //审核订单总数 'auditTotalMoney' => 100, //审核订单总额 ], ];*/ //statistics $paramsStatistics = $params['statistics']; // 查询分销商等级升级条件 $modelResult = $this->objMCommissionGrade->getAllGradeCondition(); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $conditionData = $modelResult->getData(); unset($modelResult); if(empty($conditionData)){ return ResultWrapper::fail('升级条件查询失败', ErrorCode::$dberror); } //拿出所有条件 $condition = []; foreach($conditionData as $value){ $condition[$value['key']] = $value; } $dbResult = $this->objDCommissionBusinessman->get(['userCenterId' => $userCenterId, 'deleteStatus' => StatusCode::$standard]); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } if(empty($dbResult)){ return ResultWrapper::fail('分销商查询失败', ErrorCode::$paramError); } $businessman = $dbResult; unset($dbResult); $statistics = json_decode($businessman['statistics'], true); // 查询所有分销商等级 $modelResult = $this->objMCommissionGrade->getAllGrade(); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $gradeData = $modelResult->getData(); unset($modelResult); if(empty($gradeData)){ return ResultWrapper::fail('分销商等级查询失败', ErrorCode::$dberror); } // 查询分销商设置数据 $modelResult = $this->objMCommissionSetting->getSetting(); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $setting = $modelResult->getData(); unset($modelResult); //映射条件 $keepArray = ['orderNum', 'oneOrderNum', 'selfOrderNum', 'sub', 'subBusinessman', 'oneSub', 'oneSubBusinessman', 'auditOrderTotal']; foreach($condition as $key => $value){ if(isset($paramsStatistics[$key])){ $keep = 2; if(in_array($key,$keepArray)){ $keep = 0; } if($businessman['auditStatus'] == StatusCode::$auditStatus['auditing']){ //未审核分销商 只累加审核字段数据 if($value['type'] == StatusCode::$standard){ if(isset($statistics[$key])){ $statistics[$key] = bcadd($statistics[$key], $paramsStatistics[$key], $keep); }else{ $statistics[$key] = $paramsStatistics[$key]; } } }else{ //已审核分销商 累加所有字段 if(isset($statistics[$key])){ //判断是否是sub,subBusinessman统计字段 if($key == 'sub' || $key == 'subBusinessman'){ //查询下级人数 累加 $this->objCommissionCache->delBusinessmanSub($userCenterId); $modelResult = self::getBusinessmanSubLevelNum([$userCenterId]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(),$modelResult->getErrorCode()); } $statisticsResult = $modelResult->getData(); unset($modelResult); $statistics[$key] = bcadd($statisticsResult[$userCenterId][$key][$setting['level']], $paramsStatistics[$key], $keep); //删除缓存 $this->objCommissionCache->delBusinessmanSub($userCenterId); }else{ $statistics[$key] = bcadd($statistics[$key], $paramsStatistics[$key], $keep); } }else{ $statistics[$key] = $paramsStatistics[$key]; } } } } $suffice = 0; $upgrade = 0; $grade = $businessman['grade']; //有条件 判断条件 foreach($gradeData as $key => $value){ if($value['grade'] > $grade){ if(!empty($value['upgradeConditionInfo'])){ $suffice = 0; foreach($value['upgradeConditionInfo'] as $info){ //判断传参有没有条件值 $statistics统计数据 $info升级条件 if(isset($statistics[$info['key']]) && $info['value'] > 0){ if($statistics[$info['key']] >= $info['value']){ $suffice++; } } } //判断全部满足还是满足一个 if($value['upgradeMode'] == StatusCode::$standard){ $count = count($value['upgradeConditionInfo']); //满足全部 if($count == $suffice){ $upgrade = true; $grade = $value['grade']; } }else{ //满足一个 if($suffice > 0){ $upgrade = true; $grade = $value['grade']; } } }else{ //无条件 直接升级 $upgrade = true; $grade = $value['grade']; } } } $buyGoodsGrade = 0; $auditStatus = false; $applicationComplete = StatusCode::$delete; //如果分销商未审核 并且条件也不满足 if($businessman['auditStatus'] == StatusCode::$auditStatus['auditing'] && $businessman['applicationComplete'] == StatusCode::$delete){ //判断添加分销商是基础设置 成为分销商条件 switch ($businessman['applicationCondition']){ case 2: //消费次数 if(isset($statistics['auditOrderTotal']) && !empty($statistics['auditOrderTotal'])){ //判断是否大于等于设置数量 if($statistics['auditOrderTotal'] >= $setting['buyNum']){ $auditStatus = StatusCode::$auditStatus['auditPass']; $applicationComplete = StatusCode::$standard; } } break; case 3: //消费金额 if(isset($statistics['auditTotalMoney']) && !empty($statistics['auditTotalMoney'])){ //判断是否大于等于设置数量 if($statistics['auditTotalMoney'] >= $setting['buyAmount']){ $auditStatus = StatusCode::$auditStatus['auditPass']; $applicationComplete = StatusCode::$standard; } } break; case 4: //购买指定商品 成为分销商 或者 直接升级 if(!empty($params['skuIds'])){ //对比购买商品id 和 设置的商品id有没有重复 foreach($setting['buyGoods'] as $key => $value){ if(!empty($value['goodsData'])){ foreach($value['goodsData'] as $v){ if(in_array($v['skuId'], $params['skuIds'])){ if($key > $buyGoodsGrade){ $upgrade = true; $buyGoodsGrade = (int) $key; } } } } } //如果有修改 审核状态 满足条件状态 $auditStatus = StatusCode::$auditStatus['auditPass']; $applicationComplete = StatusCode::$standard; } break; default: break; } } //保存 $update = [ 'statistics' => json_encode($statistics), 'updateTime' => time() ]; if($upgrade && $grade != $businessman['grade']){ if($buyGoodsGrade) $grade = $buyGoodsGrade; $modelResult = $this->objMCommissionGrade->getInfoGrade(['grade' => $grade]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $topGrade = $modelResult->getData(); unset($modelResult); if(!empty($topGrade)){ $update['grade'] = $grade; $update['gradeId'] = $topGrade['id']; } } isset($params['balance']) && $update['balance'] = $params['balance']; isset($params['waitMoney']) && $update['waitMoney'] = $params['waitMoney']; isset($params['withdraw']) && $update['withdraw'] = $params['withdraw']; isset($params['totalMoney']) && $update['totalMoney'] = $params['totalMoney']; $applicationComplete == StatusCode::$standard && $update['applicationComplete'] = $applicationComplete; //判断修改审核状态 设置中审核分销商是否关闭 if($auditStatus && $setting['audit'] == StatusCode::$delete){ //如果关闭 直接修改审核状态 为 已审核 $update['auditStatus'] = $auditStatus; $update['auditTime'] = time(); } $modelResult = self::updateBusinessman($update, ['id' => $businessman['id']]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } return ResultWrapper::success($modelResult->getData()); } /** * 获取下级 * @param $params * @return ResultWrapper * @throws Exception */ public function getAllSub($params) { $limit = $params['limit']; unset($params['limit']); $offset = $params['offset']; unset($params['offset']); //查询分销商数据 $dbResult = $this->objDCommissionBusinessman->get(['id' => $params['id'], 'deleteStatus' => StatusCode::$standard, 'auditStatus' => StatusCode::$auditStatus['auditPass']]); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } $businessman = $dbResult; unset($dbResult); if(empty($businessman)){ return ResultWrapper::fail('分销商不存在', ErrorCode::$paramError); } $statistics = json_decode($businessman['statistics'], true); $businessman['statistics'] = $statistics; //查询客户数据 $modelResult = $this->objMCustomer->getCustomerData(['id' => $businessman['customerId'], 'deleteStatus' => StatusCode::$standard, 'status' => StatusCode::$auditStatus['auditPass']]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $customer = $modelResult->getData(); unset($modelResult); if(empty($customer)){ return ResultWrapper::fail('客户数据查询失败', ErrorCode::$dberror); } //查询统计数据 $modelResult = self::getBusinessmanSubLevelNum([$businessman['userCenterId']]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $statisticsNum = $modelResult->getData(); unset($modelResult); //查询设置 $modelResult = $this->objMCommissionSetting->getSetting(); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $setting = $modelResult->getData(); unset($modelResult); //查询下级 $field = ' * '; $sql = 'select '.$field.' from qianniao_customer_'.$this->enterpriseId; $whereSql = " where commissionPath->'$.\"".$params['level']."\"' = ".$businessman["userCenterId"]; $whereSql .= ' and deleteStatus = '.StatusCode::$standard; $orderSql = ' order by id desc '; $limitSql = ' limit '.$offset.','.$limit; $sql = $sql.$whereSql.$orderSql.$limitSql; $modelResult = $this->objMCustomer->getCustomerDataByQuery($sql); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $customerArray = $modelResult->getData(); unset($modelResult); $totalData = []; if(!empty($customerArray)){ //提取所有userCenterId $userCenterIds = array_column($customerArray, 'userCenterId'); //查询分销订单数 分销订单总金额 $objMCommissionOrder = new MCommissionOrder($this->enterpriseId, $this->userCenterId); $modelResult = $objMCommissionOrder->getCommissionOrderData($userCenterIds); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $totalData = $modelResult->getData(); unset($modelResult); } $returnData = []; foreach($customerArray as $value){ $data = [ 'userCenterId' =>$value['userCenterId'], 'avatar' => $value['avatar'], 'name' => $value['name'], 'createTime' => $value['createTime'], 'isCommission' => $value['isCommission'], 'totalMoney' => 0.00, 'orderTotal' => 0, ]; if(isset($totalData[$value['userCenterId']])){ $data['totalMoney'] = isset($totalData[$value['userCenterId']]['totalOrderAmount']) ? $totalData[$value['userCenterId']]['totalOrderAmount'] : 0.00; $data['orderTotal'] = isset($totalData[$value['userCenterId']]['total']) ? $totalData[$value['userCenterId']]['total'] : 0; } $returnData[] = $data; } $countSql = 'select count(*) as count from qianniao_customer_'.$this->enterpriseId; $countSql .= $whereSql; $modelResult = $this->objMCustomer->getCustomerDataByQuery($countSql); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $countResult = $modelResult->getData(); unset($modelResult); $count = $countResult ? array_shift($countResult) : 0; //该等级分销商数 $businessmanSql = 'select count(*) as `count` from qianniao_customer_'.$this->enterpriseId; $businessmanSql .= $whereSql.' and isCommission = '.StatusCode::$standard; $modelResult = $this->objMCustomer->getCustomerDataByQuery($businessmanSql); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $topCountResult = $modelResult->getData(); unset($modelResult); $topCount = $topCountResult ? array_shift($topCountResult) : 0; $return = [ 'data' => $returnData, 'total' => isset($count['count']) ? $count['count'] : 0, 'subTotal' => isset($statisticsNum[$businessman['userCenterId']]['sub'][$setting['level']]) ? (int)$statisticsNum[$businessman['userCenterId']]['sub'][$setting['level']] : 0, 'businessmanTotal' => isset($statisticsNum[$businessman['userCenterId']]['subBusinessman'][$setting['level']]) ? $statisticsNum[$businessman['userCenterId']]['subBusinessman'][$setting['level']] : 0, 'subBusinessmanTotal' => isset($topCount['count']) ? $topCount['count'] : 0, ]; return ResultWrapper::success($return); } /** * 首页数据统计 */ public function statisticsCommissionOne() { //提现待审核 $sql = 'select sum(auditWithdraw) as auditWithdraw,sum(waitAuditWithdraw) as waitAuditWithdraw,sum(balance) as balance,sum(waitMoney) as waitMoney,sum(withdraw) as withdraw,sum(balance) + sum(waitMoney) + sum(withdraw) as totalMoney from '.$this->objDCommissionBusinessman->get_Table().' where deleteStatus = '.StatusCode::$standard.' and auditStatus = '.StatusCode::$auditStatus['auditPass']; $dbResult = $this->objDCommissionBusinessman->query($sql); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } $dbResult = array_shift($dbResult); //提现待审核 $totalData['withdrawNoAudit'] = $dbResult['waitAuditWithdraw']; //提现待打款 $totalData['withdrawNoPay'] = $dbResult['auditWithdraw']; //总金额 累计佣金 $totalData['totalMoney'] = $dbResult['totalMoney']; //已提现 提现佣金 $totalData['withdraw'] = $dbResult['withdraw']; unset($dbResult); //分销商 //待审核 $dbResult = $this->objDCommissionBusinessman->count(['auditStatus' => StatusCode::$auditStatus['auditing'], 'deleteStatus' => StatusCode::$standard]); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } $totalData['auditBusinessman'] = $dbResult; unset($dbResult); //分销商 $dbResult = $this->objDCommissionBusinessman->count(['auditStatus' => StatusCode::$auditStatus['auditPass'], 'deleteStatus' => StatusCode::$standard]); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } $totalData['businessman'] = $dbResult; unset($dbResult); //会员数 $sql = 'select count(*) as total from qianniao_customer_'.$this->enterpriseId.' where status = '.StatusCode::$auditStatus['auditPass']. ' and deleteStatus = '.StatusCode::$standard; $modelResult = $this->objMCustomer->getCustomerDataByQuery($sql); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $dbResult = $modelResult->getData(); $dbResult = array_shift($dbResult); $totalData['customer'] = $dbResult['total']; unset($dbResult); unset($modelResult); //每个等级分销商人数 $sql = 'select grade,count(*) as num from '.$this->objDCommissionBusinessman->get_Table().' where deleteStatus = '.StatusCode::$standard.' and auditStatus = '.StatusCode::$auditStatus['auditPass'].' group by grade'; $dbResult = $this->objDCommissionBusinessman->query($sql); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } $businessman = $dbResult; $formatBusinessman = []; foreach($businessman as $value){ $formatBusinessman[$value['grade']] = $value['num']; } //查询所有等级 $modelResult = $this->objMCommissionGrade->getAllGrade(); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $grade = $modelResult->getData(); unset($modelResult); $totalData['gradeBusinessman'] = []; foreach($grade as $value){ $totalData['gradeBusinessman'][$value['grade']]['gradeName'] = $value['name']; $totalData['gradeBusinessman'][$value['grade']]['grade'] = $value['grade']; $totalData['gradeBusinessman'][$value['grade']]['businessmanNum'] = 0; if(isset($formatBusinessman[$value['grade']])){ $totalData['gradeBusinessman'][$value['grade']]['businessmanNum'] = $formatBusinessman[$value['grade']]; } } return ResultWrapper::success($totalData); } /** * 首页数据统计(新增分销商数) */ public function statisticsCommissionTwo() { $day = 6; $return = []; for($i = $day; $i >= 0; $i--){ $data = ['createData' => date("Y-m-d",strtotime("-".$i." day")), 'count' => 0]; $data['createTime'] = strtotime($data['createData']); $return[] = $data; } //查询每天分销商人数 $sql = 'select FROM_UNIXTIME(createTime, \'%Y-%m-%d\') as createDate,count(*) as count from '.$this->objDCommissionBusinessman->get_Table().' where deleteStatus = '.StatusCode::$standard.' and auditStatus = '.StatusCode::$auditStatus['auditPass'].' group by createDate'; $dbResult = $this->objDCommissionBusinessman->query($sql); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } if(!empty($dbResult)){ $format = []; foreach($dbResult as $value){ $format[strtotime($value['createDate'])] = $value['count']; } foreach($return as &$value){ if(isset($format[$value['createTime']])){ $value['count'] = $format[$value['createTime']]; } $value['createData'] = date("m-d", $value['createTime']); unset($value['createTime']); } unset($value); } return ResultWrapper::success($return); } /** * 首页统计(TOP) */ public function statisticsCommissionThree() { //下级会员top10 $sql = 'select *,JSON_EXTRACT(statistics, "$.sub") as sub from '.$this->objDCommissionBusinessman->get_Table().' where deleteStatus = '.StatusCode::$standard.' and auditStatus = '.StatusCode::$auditStatus['auditPass'].' order by sub desc limit 0,10'; $dbResult = $this->objDCommissionBusinessman->query($sql); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } if(!empty($dbResult)){ $modelResult = self::formatBusinessman($dbResult); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $dbResult = $modelResult->getData(); unset($modelResult); } $subTop = []; foreach ($dbResult as $value){ $subTop[] = [ 'avatar' => $value['avatar'], 'name' => $value['name'], 'sub' => (int) json_decode($value['sub'], true) ]; } $return['subTop'] = $subTop; unset($dbResult); //累计佣金top10 $sql = 'select * from '.$this->objDCommissionBusinessman->get_Table().' where deleteStatus = '.StatusCode::$standard.' and auditStatus = '.StatusCode::$auditStatus['auditPass'].' order by totalMoney desc limit 0,10'; $dbResult = $this->objDCommissionBusinessman->query($sql); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } if(!empty($dbResult)){ $modelResult = self::formatBusinessman($dbResult); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $dbResult = $modelResult->getData(); unset($modelResult); } $moneyTopTop = []; foreach ($dbResult as $value){ $moneyTopTop[] = [ 'avatar' => $value['avatar'], 'name' => $value['name'], 'money' => $value['totalMoney'] ]; } $return['moneyTop'] = $moneyTopTop; return ResultWrapper::success($return); } /** * 根据手机号, 微信昵称 查询分销商 * @param $searchParam * @return ResultWrapper */ public function searchBusinessman($searchParam) { $sql = 'select * from qianniao_userCenter u left join qianniao_customer_'.$this->enterpriseId.' c on c.userCenterId = u.id where u.mobile like "%'.$searchParam.'%" or c.name like "%'.$searchParam.'%" and c.status = '.StatusCode::$auditStatus['auditPass'].' and c.deleteStatus = '.StatusCode::$standard; $dbResult = $this->objDCommissionBusinessman->query($sql); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } /** * 等级说明 */ public function getBusinessmanGradeInfo() { $modelResult = $this->objMCommissionGrade->getAllGrade(); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $gradeData = $modelResult->getData(); unset($modelResult); $modelResult = self::getInfoBusinessman(['userCenterId' => $this->userCenterId]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $businessmanData = $modelResult->getData(); unset($modelResult); foreach($gradeData as &$value){ if($value['upgradeMode'] == StatusCode::$standard){ //全部满足 if($businessmanData['grade'] == $value['grade']){ foreach($value['upgradeConditionInfo'] as &$v){ if(isset($businessmanData['statistics'][$v['key']])){ if($businessmanData['statistics'][$v['key']] >= $v['value']){ $v['status'] = true; } }else{ $v['status'] = false; } } }elseif($businessmanData['grade'] > $value['grade']){ foreach($value['upgradeConditionInfo'] as &$v){ $v['status'] = true; } }else{ foreach($value['upgradeConditionInfo'] as &$v){ $v['status'] = false; } } }else{ //满足一个 if($businessmanData['grade'] > $value['grade']){ foreach($value['upgradeConditionInfo'] as &$v){ $v['status'] = true; } }else{ foreach($value['upgradeConditionInfo'] as &$v){ $v['status'] = false; } } } } return ResultWrapper::success($gradeData); } /** * * 查询某个分销商下级人数 * @param $userCenterIds */ public function getBusinessmanSubLevelNum($userCenterIds) { //查询缓存 if(empty($userCenterIds)){ return ResultWrapper::fail('userCenterIds参数为空', ErrorCode::$paramError); } $emptyUserCenterId = []; $statistics = []; foreach($userCenterIds as $value){ $cacheResult = $this->objCommissionCache->getBusinessmanSub($value); $cacheResult = []; if(!empty($cacheResult)){ $statistics[$value] = $cacheResult; }else{ $emptyUserCenterId[] = $value; } } if(empty($emptyUserCenterId)){ return ResultWrapper::success($statistics); } //缓存不存在 查询数据库 foreach($emptyUserCenterId as $value){ //查询下级 $subNum = 0; $subBusinessmanNum = 0; for($i = 1; $i <= 3; $i++){ //分销商 $sql = 'select count(*) as count from qianniao_customer_'.$this->enterpriseId.' where isCommission = '.StatusCode::$standard.' and commissionPath->\'$."'.$i.'"\' = '.$value; $dbResult = $this->objDCommissionBusinessman->query($sql); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } $statistics[$value]['subBusinessman'][$i] = $subBusinessmanNum + array_shift($dbResult)['count']; $subBusinessmanNum = $statistics[$value]['subBusinessman'][$i]; //用户 $sql = 'select count(*) as count from qianniao_customer_'.$this->enterpriseId.' where commissionPath->\'$."'.$i.'"\' = '.$value; $dbResult = $this->objDCommissionBusinessman->query($sql); if($dbResult === false){ return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror); } $statistics[$value]['sub'][$i] = $subNum + array_shift($dbResult)['count']; $subNum = $statistics[$value]['sub'][$i]; } //保存缓存 $cacheResult = $this->objCommissionCache->saveBusinessmanSub($value, $statistics[$value]); if($cacheResult === false){ return ResultWrapper::fail('缓存失败', ErrorCode::$paramError); } } //返回数据 return ResultWrapper::success($statistics); } /** * 修改直系上级关系 * @throws Exception * 客户id,上级id */ public function editRelationship($params) { $id = $params['id']; $superiorId = $params['superiorId']; // 查询当前客户的上级分销商数据 $dbResult = $this->objDCustomer->get(['userCenterId'=>$id]); if($dbResult === false){ return ResultWrapper::fail($this->objDCustomer->error(), ErrorCode::$dberror); } if(empty($dbResult['commissionPath'])){ return ResultWrapper::fail('当前用户没有上级分销商',ErrorCode::$notAllowAccess); } $distributionDate = json_decode($dbResult['commissionPath'],true); $tmpDate = []; // 查询分销基础设置数据 $modelResult = $this->objMCommissionSetting->getSetting(); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $setting = $modelResult->getData(); if(empty($setting)){ return ResultWrapper::fail('获取分销商基础信息错误',ErrorCode::$notAllowAccess); } $level = $setting['level']; // 判断当前要需改的上级分销商是否已经存在上集中 foreach ($distributionDate as $key => $value){ if($value == $superiorId){ return ResultWrapper::fail('要新绑定的分销商已经是该用户的上级',ErrorCode::$notAllowAccess); } } // 查询替换分销商的上级信息 $dbResult1 = $this->objDCustomer->get(['userCenterId'=>$superiorId]); if($dbResult1 === false){ return ResultWrapper::fail($this->objDCustomer->error(), ErrorCode::$dberror); } if(empty($dbResult1)){ return ResultWrapper::fail('当前上级分销商不存在',ErrorCode::$notAllowAccess); } if(!empty($dbResult1['commissionPath'])){ $distributionDate1 = json_decode($dbResult1['commissionPath'],true); } if($level == 1){ $tmpDate = [ "1"=>$superiorId, ]; //更新人数 $modelResult = self::outSetStatistics($superiorId, ['statistics' => ['sub' => 1, 'oneSub' => 1]]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } } if($level == 2){ $tmpDate = [ "1"=>$superiorId, ]; if(isset($distributionDate1['1']) && !empty($distributionDate1['1']) ){ array_push($tmpDate,$distributionDate1['1']); } //更新人数 $modelResult = self::outSetStatistics($superiorId, ['statistics' => ['sub'=>1]]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } } if($level == 3){ $tmpDate = [ "1"=>$superiorId, ]; if(isset($distributionDate1['1']) && !empty($distributionDate1['1']) ){ array_push($tmpDate,$distributionDate1['1']); } if(isset($distributionDate1['2']) && !empty($distributionDate1['2']) ){ array_push($tmpDate,$distributionDate1['2']); } //更新人数 $modelResult = self::outSetStatistics($superiorId, ['statistics' => ['sub'=>1]]); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } } //更新分销商等级关系 $updateResult = $this->objDCustomer->update(['commissionPath'=>json_encode($tmpDate)],['userCenterId'=>$id]); if($updateResult === false){ return ResultWrapper::fail($this->objDCustomer->error(), ErrorCode::$dberror); } return ResultWrapper::success($updateResult); } }