1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729 |
- <?php
- /**
- * 分销商Model
- * Created by PhpStorm.
- * User: haoren
- * Date: 2020/07/22
- * Time: 15:00
- */
- namespace JinDouYun\Model\Commission;
- use Exception;
- use JinDouYun\Controller\Common\Logger;
- use JinDouYun\Dao\Customer\DCustomer;
- use JinDouYun\Dao\UserCenter\DUserCenter;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Cache\CommissionCache;
- use JinDouYun\Dao\Commission\DCommissionBusinessman;
- use JinDouYun\Model\Customer\MCustomer;
- use JinDouYun\Model\MBaseModel;
- use JinDouYun\Model\Commission\MCommissionGrade;
- use JinDouYun\Model\Commission\MCommissionSetting;
- class MCommissionBusinessman extends MBaseModel
- {
- private $objDCommissionBusinessman;
- private $objMCustomer;
- private $objMCommissionGrade;
- private $objMCommissionSetting;
- private $objDCustomer;
- private $objCommissionCache;
- private $userCenterId;
- private $enterpriseId;
- public function __construct($enterpriseId, $userCenterId)
- {
- $this->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);
- }
- }
|