MCommissionBusinessman.Class.php 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  1. <?php
  2. /**
  3. * 分销商Model
  4. * Created by PhpStorm.
  5. * User: haoren
  6. * Date: 2020/07/22
  7. * Time: 15:00
  8. */
  9. namespace JinDouYun\Model\Commission;
  10. use Exception;
  11. use JinDouYun\Controller\Common\Logger;
  12. use JinDouYun\Dao\Customer\DCustomer;
  13. use JinDouYun\Dao\UserCenter\DUserCenter;
  14. use Mall\Framework\Core\ErrorCode;
  15. use Mall\Framework\Core\StatusCode;
  16. use Mall\Framework\Core\ResultWrapper;
  17. use JinDouYun\Cache\CommissionCache;
  18. use JinDouYun\Dao\Commission\DCommissionBusinessman;
  19. use JinDouYun\Model\Customer\MCustomer;
  20. use JinDouYun\Model\MBaseModel;
  21. use JinDouYun\Model\Commission\MCommissionGrade;
  22. use JinDouYun\Model\Commission\MCommissionSetting;
  23. class MCommissionBusinessman extends MBaseModel
  24. {
  25. private $objDCommissionBusinessman;
  26. private $objMCustomer;
  27. private $objMCommissionGrade;
  28. private $objMCommissionSetting;
  29. private $objDCustomer;
  30. private $objCommissionCache;
  31. private $userCenterId;
  32. private $enterpriseId;
  33. public function __construct($enterpriseId, $userCenterId)
  34. {
  35. $this->enterpriseId = $enterpriseId;
  36. $this->userCenterId = $userCenterId;
  37. $this->objCommissionCache = new CommissionCache($enterpriseId, $userCenterId);
  38. $this->objMCustomer = new MCustomer($enterpriseId, $userCenterId);
  39. $this->objMCommissionGrade = new MCommissionGrade($enterpriseId, $userCenterId);
  40. $this->objMCommissionSetting = new MCommissionSetting($enterpriseId, $userCenterId);
  41. $this->objDCommissionBusinessman = new DCommissionBusinessman();
  42. $this->objDCommissionBusinessman->setTable($this->objDCommissionBusinessman->get_Table().'_'.$enterpriseId);
  43. $this->objDCustomer = new DCustomer();
  44. $this->objDCustomer->setTable('qianniao_customer_'.$enterpriseId);
  45. }
  46. /**
  47. * 新增分销商
  48. * @param $params
  49. * @return ResultWrapper
  50. * @throws Exception
  51. */
  52. public function addBusinessman($params)
  53. {
  54. $params['deleteStatus'] = StatusCode::$standard;
  55. $params['createTime'] = time();
  56. $params['updateTime'] = time();
  57. //判断分销商是否存在
  58. $modelResult = $this->objMCustomer->getCustomerData(['id' => $params['customerId'], 'deleteStatus' => StatusCode::$standard, 'status' => StatusCode::$auditStatus['auditPass']]);
  59. if(!$modelResult->isSuccess()){
  60. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  61. }
  62. $customerData = $modelResult->getData();
  63. unset($modelResult);
  64. if(empty($customerData)){
  65. return ResultWrapper::fail('客户不存在', ErrorCode::$paramError);
  66. }
  67. if($customerData['isCommission'] == StatusCode::$standard){
  68. return ResultWrapper::fail('该客户已经是分销商', ErrorCode::$paramError);
  69. }
  70. !empty($customerData['commissionPath']) && $customerData['commissionPath'] = json_decode($customerData['commissionPath'], true);
  71. $params['userCenterId'] = $customerData['userCenterId'];
  72. //默认等级
  73. $params['grade'] = isset($params['grade']) ? $params['grade'] : 0;
  74. $modelResult = $this->objMCommissionGrade->getInfoGrade(['grade' => $params['grade'], 'deleteStatus' => StatusCode::$standard]);
  75. if(!$modelResult->isSuccess()){
  76. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  77. }
  78. $gradeData = $modelResult->getData();
  79. unset($modelResult);
  80. if(empty($gradeData)){
  81. return ResultWrapper::fail('默认等级查询失败', ErrorCode::$paramError);
  82. }
  83. if($gradeData['enableStatus'] == StatusCode::$delete){
  84. return ResultWrapper::fail('等级已被禁用', ErrorCode::$paramError);
  85. }
  86. $params['gradeId'] = $gradeData['id'];
  87. //查询设置
  88. $modelResult = $this->objMCommissionSetting->getSetting();
  89. if(!$modelResult->isSuccess()){
  90. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  91. }
  92. $setting = $modelResult->getData();
  93. unset($modelResult);
  94. $params['applicationCondition'] = $setting['businessmanCondition'];
  95. $params['applicationComplete'] = StatusCode::$delete;
  96. //判断成为分销商条件
  97. if($setting['businessmanCondition'] == 0 || ($setting['businessmanCondition'] == 1 && $setting['audit'] == StatusCode::$delete)){
  98. //无条件 或者 资料申请 分销商审核关闭
  99. //状态已审核
  100. $params['auditStatus'] = StatusCode::$auditStatus['auditPass'];
  101. $params['auditTime'] = time();
  102. $params['applicationComplete'] = StatusCode::$standard;
  103. //修改客户表字段
  104. $updateCustomerData = [
  105. 'isCommission' => StatusCode::$standard,
  106. ];
  107. $modelResult = $this->objMCustomer->updateCustomer($updateCustomerData, ['id' => $params['customerId']]);
  108. if(!$modelResult->isSuccess()){
  109. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  110. }
  111. unset($modelResult);
  112. //修改分销商数据
  113. //修改客户数据
  114. }
  115. $params['info'] = json_encode($params['info']);
  116. //上级添加统计字段
  117. if($params['auditStatus'] == StatusCode::$auditStatus['auditPass']){
  118. //上1级 下级分销商人数 下一级分销商人数
  119. if(isset($customerData['commissionPath'][StatusCode::$level['one']])){
  120. $modelResult = self::outSetStatistics($customerData['commissionPath'][StatusCode::$level['one']], ['statistics' => ['subBusinessman' => 1, 'oneSubBusinessman' => 1]]);
  121. if(!$modelResult->isSuccess()){
  122. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  123. }
  124. }
  125. //上2级 下级分销商人数
  126. if(isset($customerData['commissionPath'][StatusCode::$level['two']]) && $setting['level'] >= StatusCode::$level['two']){
  127. $modelResult = self::outSetStatistics($customerData['commissionPath'][StatusCode::$level['two']], ['statistics' => ['subBusinessman' => 1]]);
  128. if(!$modelResult->isSuccess()){
  129. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  130. }
  131. }
  132. //上3级 下级分销商人数
  133. if(isset($customerData['commissionPath'][StatusCode::$level['three']]) && $setting['level'] >= StatusCode::$level['three']){
  134. $modelResult = self::outSetStatistics($customerData['commissionPath'][StatusCode::$level['three']], ['statistics' => ['subBusinessman' => 1]]);
  135. if(!$modelResult->isSuccess()){
  136. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  137. }
  138. }
  139. }
  140. //添加分销商
  141. $dbResult = $this->objDCommissionBusinessman->insert($params);
  142. if($dbResult === false){
  143. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  144. }
  145. return ResultWrapper::success($dbResult);
  146. }
  147. /**
  148. * 修改分销商
  149. * @param $update
  150. * @param $where
  151. * @return ResultWrapper
  152. * @throws Exception
  153. */
  154. public function updateBusinessman($update, $where = [])
  155. {
  156. //分销商审核
  157. if (!isset($where['deleteStatus'])){
  158. $where['deleteStatus'] = StatusCode::$standard;
  159. }
  160. $dbResult = $this->objDCommissionBusinessman->get($where);
  161. if($dbResult === false){
  162. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  163. }
  164. $businessman = $dbResult;
  165. unset($dbResult);
  166. if(empty($businessman)){
  167. return ResultWrapper::fail('分销商不存在', ErrorCode::$paramError);
  168. }
  169. if(isset($update['auditStatus']) && $update['auditStatus'] == StatusCode::$auditStatus['auditPass']){
  170. if($businessman['auditStatus'] == StatusCode::$auditStatus['auditPass']){
  171. return ResultWrapper::fail('分销商已审核', ErrorCode::$paramError);
  172. }
  173. $update['auditTime'] = time();
  174. //查询客户
  175. $modelResult = $this->objMCustomer->getCustomerData(['id' => $businessman['customerId'], 'status' => StatusCode::$auditStatus['auditPass'], 'deleteStatus' => StatusCode::$standard]);
  176. if(!$modelResult->isSuccess()){
  177. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  178. }
  179. $customerData = $modelResult->getData();
  180. unset($modelResult);
  181. if(empty($customerData)){
  182. return ResultWrapper::fail('客户不存在', ErrorCode::$paramError);
  183. }
  184. //修改客户表字段
  185. $updateCustomerData = [
  186. 'isCommission' => StatusCode::$standard,
  187. ];
  188. $modelResult = $this->objMCustomer->updateCustomer($updateCustomerData, ['id' => $businessman['customerId']]);
  189. if(!$modelResult->isSuccess()){
  190. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  191. }
  192. unset($modelResult);
  193. //查询设置
  194. $modelResult = $this->objMCommissionSetting->getSetting();
  195. if(!$modelResult->isSuccess()){
  196. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  197. }
  198. $setting = $modelResult->getData();
  199. unset($modelResult);
  200. //上级添加统计字段
  201. if(!empty($customerData['commissionPath'])){
  202. $customerData['commissionPath'] = json_decode($customerData['commissionPath'], true);
  203. //上1级 下级分销商人数 下一级分销商人数
  204. if(isset($customerData['commissionPath'][StatusCode::$level['one']])){
  205. $modelResult = self::outSetStatistics($customerData['commissionPath'][StatusCode::$level['one']], ['subBusinessman' => 1, 'oneSubBusinessman' => 1]);
  206. if(!$modelResult->isSuccess()){
  207. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  208. }
  209. }
  210. //上2级 下级分销商人数
  211. if(isset($customerData['commissionPath'][StatusCode::$level['two']]) && $setting['level'] >= StatusCode::$level['two']){
  212. $modelResult = self::outSetStatistics($customerData['commissionPath'][StatusCode::$level['two']], ['subBusinessman' => 1]);
  213. if(!$modelResult->isSuccess()){
  214. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  215. }
  216. }
  217. //上3级 下级分销商人数
  218. if(isset($customerData['commissionPath'][StatusCode::$level['three']]) && $setting['level'] >= StatusCode::$level['three']){
  219. $modelResult = self::outSetStatistics($customerData['commissionPath'][StatusCode::$level['three']], ['subBusinessman' => 1]);
  220. if(!$modelResult->isSuccess()){
  221. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  222. }
  223. }
  224. }
  225. }
  226. //分销商删除
  227. if(isset($update['deleteStatus']) && $update['deleteStatus'] == StatusCode::$delete){
  228. //修改客户表
  229. $updateCustomerData = [
  230. 'isCommission' => StatusCode::$delete,
  231. ];
  232. $modelResult = $this->objMCustomer->updateCustomer($updateCustomerData, ['id' => $businessman['customerId']]);
  233. if(!$modelResult->isSuccess()){
  234. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  235. }
  236. unset($modelResult);
  237. }
  238. $dbResult = $this->objDCommissionBusinessman->update($update, $where);
  239. if($dbResult === false){
  240. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  241. }
  242. //删除缓存
  243. $this->objCommissionCache->delBusinessmanGrade($businessman['userCenterId']);
  244. return ResultWrapper::success($dbResult);
  245. }
  246. /**
  247. * 分销商列表
  248. * @param $params
  249. * @param $export
  250. * @param $distribution
  251. * @return ResultWrapper
  252. */
  253. public function getAllBusinessman($params = [],$export =0,$distribution=0)
  254. {
  255. $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';
  256. $whereSql = ' where c.status = '.StatusCode::$auditStatus['auditPass'].' and c.deleteStatus = '.StatusCode::$standard.' and b.deleteStatus = '.StatusCode::$standard;
  257. if(isset($params['search'])){
  258. $whereSql .= ' and (u.mobile like "%'.$params['search'].'%" or c.name like "%'.$params['search'].'%") ';
  259. }
  260. if(isset($params['auditStatus'])){
  261. $whereSql .= ' and b.auditStatus = '.$params['auditStatus'];
  262. }
  263. if(isset($params['grade'])){
  264. $whereSql .= ' and b.grade = '.$params['grade'];
  265. }
  266. if(isset($params['createStar']) && isset($params['createEnd'])){
  267. $whereSql .= ' and c.createTime between '.$params['createStar'].' and '.$params['createEnd'];
  268. }
  269. if(isset($params['applicationStar']) && isset($params['applicationEnd'])){
  270. $whereSql .= ' and b.createTime between '.$params['applicationStar'].' and '.$params['applicationEnd'];
  271. }
  272. if(isset($params['auditStar']) && isset($params['auditEnd'])){
  273. $whereSql .= ' and b.auditTime between '.$params['auditStar'].' and '.$params['auditEnd'];
  274. }
  275. $orderSql = ' order by b.createTime desc ';
  276. if($export){
  277. $limitSql = '';
  278. }else{
  279. $limitSql = ' limit '.$params['offset'].','.$params['limit'];
  280. }
  281. $selectSql = $sql.$whereSql.$orderSql.$limitSql;
  282. $dbResult = $this->objDCommissionBusinessman->query($selectSql);
  283. if($dbResult === false){
  284. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  285. }
  286. $returnData = $dbResult;
  287. unset($dbResult);
  288. if(empty($returnData)){
  289. $return = [
  290. 'data' => $returnData,
  291. 'total' => 0,
  292. ];
  293. return ResultWrapper::success($return);
  294. }
  295. //总数
  296. $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';
  297. $selectSql = $sql.$whereSql;
  298. $dbResult = $this->objDCommissionBusinessman->query($selectSql);
  299. if($dbResult === false){
  300. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  301. }
  302. $dbResult = array_shift($dbResult);
  303. $total = $dbResult['count'];
  304. unset($dbResult);
  305. //格式化
  306. $modelResult = self::formatBusinessman($returnData);
  307. if(!$modelResult->isSuccess()){
  308. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  309. }
  310. $returnData = $modelResult->getData();
  311. unset($modelResult);
  312. $return = [
  313. 'data' => $returnData,
  314. 'total' => ($total) ? intval($total) : 0,
  315. ];
  316. if($export){
  317. self::exportBusinessman($return['data'],$distribution);
  318. exit;
  319. }
  320. return ResultWrapper::success($return);
  321. }
  322. /**
  323. * 分销商审核导出方法
  324. * @param $businessmanData
  325. * @param $distribution
  326. */
  327. public function exportBusinessman($businessmanData,$distribution)
  328. {
  329. //导出到本地
  330. header("Content-type:application/vnd.ms-excel");
  331. header("Content-Disposition:filename=分销商审核.csv");
  332. header('Cache-Control: max-age=0');
  333. $fp = fopen('php://output', 'a');
  334. if($distribution){
  335. $head = ['姓名', '手机号','等级', '累计佣金', '提现佣金','成为分销商时间','下线总数', '下线分销商总数']; //定义标题
  336. }else{
  337. $head = ['姓名', '手机号','等级', '累计消费次数', '累计消费金额','注册时间','申请状态', '申请时间']; //定义标题
  338. }
  339. foreach ($head as $i => $v) {
  340. $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8'); //将中文标题转换编码,否则乱码
  341. }
  342. fputcsv($fp, $head);
  343. $limit = 10000;
  344. $num = 0;//计数器
  345. foreach ($businessmanData as $value) {//循环数据
  346. $auditOrderTotal = '0';
  347. $auditTotalMoney = '0';
  348. $subBusinessman ='0';
  349. $sub ='0';
  350. if(!empty($value['statistics'])){
  351. $statistics = $value['statistics'];
  352. $auditOrderTotal = $statistics['auditOrderTotal'];
  353. $auditTotalMoney = $statistics['auditTotalMoney'];
  354. $subBusinessman = $statistics['subBusinessman'];
  355. $sub = $statistics['sub'];
  356. }
  357. $num++;
  358. if ($num == $limit) {
  359. ob_flush();//释放内存
  360. flush();
  361. }
  362. if($distribution){
  363. $rows['name'] = isset($value['name']) ? $value['name'] : null; // 姓名
  364. $rows['mobile'] = isset($value['mobile']) ? $value['mobile'] : null; // 手机号
  365. $rows['gradeName'] = isset($value['gradeName']) ? $value['gradeName'] : null; // 等级
  366. $rows['totalMoney'] = isset($value['totalMoney']) ? $value['totalMoney'] : null; // 累计佣金
  367. $rows['waitMoney'] = isset($value['waitMoney']) ? $value['waitMoney'] : null; // 提现佣金
  368. $rows['distribution'] = isset($value['createTime']) ? date("Y-m-d H:i:s",$value['createTime']) : null; // 成为分销商时间
  369. $rows['sub'] = $sub; // 下线总数
  370. $rows['subBusinessman'] = $subBusinessman; // 下线分销商总数
  371. }else{
  372. $rows['name'] = isset($value['name']) ? $value['name'] : null; // 姓名
  373. $rows['mobile'] = isset($value['mobile']) ? $value['mobile'] : null; // 手机号
  374. $rows['gradeName'] = isset($value['gradeName']) ? $value['gradeName'] : null; // 等级
  375. $rows['auditOrderTotal'] = $auditOrderTotal; // 累计消费次数
  376. $rows['auditTotalMoney'] = $auditTotalMoney; // 累计消费金额
  377. $rows['loginTime'] = isset($value['createTime']) ? date("Y-m-d H:i:s",$value['createTime']) : null; // 注册时间
  378. $rows['applicationComplete'] = isset($value['applicationComplete'])&& $value['applicationComplete']== 4? '未满足' : '满足'; // 申请状态
  379. $rows['createTime'] = isset($value['createTime']) ? date("Y-m-d H:i:s",$value['createTime']) : null; // 申请时间
  380. }
  381. foreach ($rows as $kk => $vv) {
  382. $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码
  383. }
  384. fputcsv($fp, $rs);
  385. $rows = [];
  386. }
  387. }
  388. /**
  389. * 分销商详情
  390. * @param $params
  391. * @return ResultWrapper
  392. */
  393. public function getInfoBusinessman($params)
  394. {
  395. // 查询分销商信息
  396. $params['deleteStatus'] = StatusCode::$standard;
  397. $dbResult = $this->objDCommissionBusinessman->get($params);
  398. if($dbResult === false){
  399. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  400. }
  401. // 格式化等级
  402. $modelResult = self::formatBusinessman($dbResult);
  403. if(!$modelResult->isSuccess()){
  404. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  405. }
  406. $return = $modelResult->getData();
  407. unset($modelResult);
  408. return ResultWrapper::success($return);
  409. }
  410. /**
  411. * 格式化分销商数据
  412. * @param $params 分销商信息数据
  413. * @return ResultWrapper
  414. */
  415. public function formatBusinessman($params)
  416. {
  417. if(empty($params)){
  418. return ResultWrapper::success($params);
  419. }
  420. $format = isset($params['id']) ? [$params] : $params;
  421. $userCenterIds = [];
  422. $selectCustomer = [];
  423. $selectUserCenter = [];
  424. $subTotalUserCenterIds = [];
  425. foreach($format as $value){
  426. if(isset($value['userCenterId'])){
  427. $userCenterIds[] = $value['userCenterId'];
  428. if(!isset($value['mobile']) || empty($value['mobile'])){
  429. $selectUserCenter[] = $value['userCenterId'];
  430. }
  431. }
  432. if(isset($value['customerId']) && (!isset($value['name']) || empty($value['name']))){
  433. $selectCustomer[] = $value['customerId'];
  434. }
  435. //判断是否有统计下级人数参数字段
  436. if(!empty($value['statistics'])){
  437. $statistics = json_decode($value['statistics'], true);
  438. if(isset($statistics['sub'])){
  439. $subTotalUserCenterIds[] = $value['userCenterId'];
  440. }
  441. }
  442. }
  443. //查询设置
  444. $modelResult = $this->objMCommissionSetting->getSetting();
  445. if(!$modelResult->isSuccess()){
  446. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  447. }
  448. $setting = $modelResult->getData();
  449. unset($modelResult);
  450. //查询等级
  451. $modelResult = self::getBusinessmanGrade($userCenterIds);
  452. if(!$modelResult->isSuccess()){
  453. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  454. }
  455. $businessmanGrade = $modelResult->getData();
  456. unset($modelResult);
  457. //查询userCenter手机号
  458. $userData = [];
  459. if(!empty($selectUserCenter)){
  460. $objDUserCenter = new DUserCenter();
  461. $dbResult = $objDUserCenter->select(['id' => $userCenterIds], 'id,mobile');
  462. if($dbResult === false){
  463. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  464. }
  465. foreach($dbResult as $value){
  466. $userData[$value['id']] = $value['mobile'];
  467. }
  468. unset($dbResult);
  469. }
  470. //查询客户数据获取name
  471. $customer = [];
  472. if(!empty($selectCustomer)){
  473. $modelResult = $this->objMCustomer->getCustomerData(['id' => $selectCustomer, 'deleteStatus' => StatusCode::$standard, 'status' => StatusCode::$auditStatus['auditPass']], 'id,name,avatar', false);
  474. if(!$modelResult->isSuccess()){
  475. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  476. }
  477. $customerData = $modelResult->getData();
  478. unset($modelResult);
  479. foreach($customerData as $value){
  480. $customer[$value['id']] = [
  481. 'name' => $value['name'],
  482. 'avatar' => $value['avatar'],
  483. ];
  484. }
  485. unset($customerData);
  486. }
  487. //查询所有分销商下级统计人数
  488. $subStatistics = [];
  489. if(!empty($subTotalUserCenterIds)){
  490. $modelResult = self::getBusinessmanSubLevelNum($subTotalUserCenterIds);
  491. if(!$modelResult->isSuccess()){
  492. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  493. }
  494. $subStatistics = $modelResult->getData();
  495. unset($modelResult);
  496. }
  497. //查询所有条件映射
  498. $modelResult = $this->objMCommissionGrade->getAllGradeCondition();
  499. if(!$modelResult->isSuccess()){
  500. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  501. }
  502. $condition = $modelResult->getData();
  503. unset($modelResult);
  504. if(empty($condition)){
  505. return ResultWrapper::fail('条件查询错误', ErrorCode::$paramError);
  506. // $condition = [];//影子:分销商等级升级条件表
  507. }
  508. $conditionData = [];
  509. foreach($condition as $value){
  510. $conditionData[$value['key']] = 0;
  511. }
  512. foreach($format as &$value){
  513. $value['gradeName'] = '';
  514. $value['oneRate'] = 0.00;
  515. $value['twoRate'] = 0.00;
  516. $value['threeRate'] = 0.00;
  517. if(isset($businessmanGrade[$value['userCenterId']])){
  518. $value['gradeName'] = $businessmanGrade[$value['userCenterId']]['gradeName'];
  519. $value['oneRate'] = $businessmanGrade[$value['userCenterId']]['oneRate'];
  520. $value['twoRate'] = $businessmanGrade[$value['userCenterId']]['twoRate'];
  521. $value['threeRate'] = $businessmanGrade[$value['userCenterId']]['threeRate'];
  522. }
  523. if(isset($value['info']) && !empty($value['info'])){
  524. $value['info'] = json_decode($value['info'], true);
  525. }
  526. //格式化手机号
  527. if(!empty($selectUserCenter)){
  528. $value['mobile'] = '';
  529. if(isset($userData[$value['userCenterId']])){
  530. $value['mobile'] = $userData[$value['userCenterId']];
  531. }
  532. }
  533. //格式化name
  534. if(!empty($selectCustomer)){
  535. $value['name'] = '';
  536. if(isset($customer[$value['customerId']])){
  537. $value['name'] = $customer[$value['customerId']]['name'];
  538. $value['avatar'] = $customer[$value['customerId']]['avatar'];
  539. }
  540. }
  541. //格式化统计数据
  542. if(!empty($value['statistics'])){
  543. $value['statistics'] = json_decode($value['statistics'], true);
  544. foreach($conditionData as $key => $v){
  545. if($key == 'sub' || $key == 'subBusinessman'){
  546. $value['statistics'][$key] = $subStatistics[$value['userCenterId']][$key][$setting['level']];
  547. }else{
  548. if(!isset($value['statistics'][$key])) {
  549. $value['statistics'][$key] = 0;
  550. }
  551. }
  552. }
  553. }else{
  554. $value['statistics'] = $conditionData;
  555. }
  556. }
  557. $format = isset($params['id']) ? array_shift($format) : $format;
  558. return ResultWrapper::success($format);
  559. }
  560. /**
  561. * 查询分销商等级佣金比例
  562. * @param $userCenterIds
  563. * @return ResultWrapper
  564. */
  565. public function getBusinessmanGrade($userCenterIds)
  566. {
  567. $this->objCommissionCache->delBusinessmanGrade(true);
  568. if(empty($userCenterIds)) return ResultWrapper::success($userCenterIds);
  569. $returnData = [];
  570. $emptyData = [];
  571. foreach($userCenterIds as $userCenterId){
  572. //查询缓存
  573. $cacheData = $this->objCommissionCache->getBusinessmanGrade($userCenterId);
  574. if(!empty($cacheData)){
  575. $returnData[$userCenterId] = $cacheData;
  576. }else{
  577. $emptyData[] = $userCenterId;
  578. }
  579. }
  580. if(empty($emptyData)){
  581. return ResultWrapper::success($returnData);
  582. }
  583. // 查询分销商数据
  584. $dbResult = $this->objDCommissionBusinessman->select(['userCenterId' => $emptyData, 'deleteStatus' => StatusCode::$standard]);
  585. if($dbResult === false){
  586. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  587. }
  588. $businessman = $dbResult;
  589. unset($dbResult);
  590. if(empty($businessman)){
  591. return ResultWrapper::success($returnData);
  592. }
  593. // 查询分销商等级数据
  594. $where['grade'] = [];
  595. foreach($businessman as $value){
  596. !in_array($value['grade'], $where['grade']) && $where['grade'][] = $value['grade'];
  597. }
  598. $modelResult = $this->objMCommissionGrade->getAllGrade($where);
  599. if(!$modelResult->isSuccess()){
  600. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  601. }
  602. $grade = $modelResult->getData();
  603. unset($modelResult);
  604. if(empty($grade)){
  605. return ResultWrapper::success($returnData);
  606. }
  607. $gradeData = [];
  608. foreach($grade as $value){
  609. $gradeData[$value['grade']] = $value;
  610. }
  611. foreach($businessman as $value){
  612. $cacheArray = [
  613. 'businessmanId' => $value['id'],
  614. 'userCenterId' => $value['userCenterId'],
  615. 'customerId' => $value['customerId'],
  616. 'grade' => $value['grade'],
  617. 'gradeId' => $value['gradeId'],
  618. 'gradeName' => $gradeData[$value['grade']]['name'],
  619. 'oneRate' => $gradeData[$value['grade']]['oneRate'],
  620. 'twoRate' => $gradeData[$value['grade']]['twoRate'],
  621. 'threeRate' => $gradeData[$value['grade']]['threeRate']
  622. ];
  623. $cacheResult = $this->objCommissionCache->saveBusinessmanGrade($value['userCenterId'], $cacheArray);
  624. if($cacheResult === false){
  625. return ResultWrapper::fail('分销商等级数据缓存添加失败', ErrorCode::$paramError);
  626. }
  627. $returnData[$value['userCenterId']] = $cacheArray;
  628. }
  629. foreach($userCenterIds as $value){
  630. if(!isset($returnData[$value])){
  631. $returnData[$value] = [];
  632. }
  633. }
  634. return ResultWrapper::success($returnData);
  635. }
  636. /**
  637. * 保存分销商统计数据
  638. * @param $params
  639. * @return ResultWrapper
  640. */
  641. public function saveBusinessmanStatistics($params)
  642. {
  643. $paramsTest = [
  644. 1 => [//userCenterId
  645. 'orderTotal' => 1000, // 条件别名 => 条件值
  646. 'sonOrder' => 100,
  647. 'commissionMoneyTotal' => 10000.00
  648. ],
  649. 2 => [//userCenterId
  650. 'orderTotal' => 2000, // 条件别名 => 条件值
  651. 'sonOrder' => 200,
  652. 'commissionMoneyTotal' => 20000.00
  653. ]
  654. ];
  655. if(empty($params)){
  656. return ResultWrapper::fail('分销商统计数据参数为空', ErrorCode::$paramError);
  657. }
  658. foreach($params as $userCenterId => $value){
  659. $statisticsData = $this->objCommissionCache->getBusinessmanStatistics($userCenterId);
  660. if(empty($statisticsData)){
  661. $dbResult = $this->objDCommissionBusinessman->get(['userCenterId' => $userCenterId, 'deleteStatus' => StatusCode::$standard, 'auditStatus' => StatusCode::$auditStatus['auditPass']]);
  662. if($dbResult === false){
  663. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  664. }
  665. if(empty($dbResult)){
  666. return ResultWrapper::fail($userCenterId.'分销商数据查询失败', ErrorCode::$paramError);
  667. }
  668. if(!empty($dbResult['statistics'])){
  669. $statisticsData = json_decode($dbResult['statistics'], true);
  670. }
  671. }
  672. foreach($value as $key => $v){
  673. $statisticsData[$key] = isset($statisticsData[$key]) ? bcadd($statisticsData[$key], $v) : $v;
  674. }
  675. $cacheResult = $this->objCommissionCache->saveBusinessmanStatistics($userCenterId, $statisticsData);
  676. if(!$cacheResult){
  677. return ResultWrapper::fail('缓存保存失败', ErrorCode::$paramError);
  678. }
  679. }
  680. return ResultWrapper::success(true);
  681. }
  682. /**
  683. * 获取分销商统计数据
  684. * @param $userCenterIds
  685. * @return ResultWrapper
  686. */
  687. public function getBusinessmanStatistics($userCenterIds)
  688. {
  689. if(empty($userCenterIds)){
  690. return ResultWrapper::success($userCenterIds);
  691. }
  692. $returnData = [];
  693. $emptyArray = [];
  694. foreach($userCenterIds as $userCenterId){
  695. $cacheResult = $this->objCommissionCache->getBusinessmanStatistics($userCenterId);
  696. if($cacheResult){
  697. $returnData[$userCenterId] = $cacheResult;
  698. }else{
  699. $emptyArray[] = $userCenterId;
  700. }
  701. }
  702. if(empty($emptyArray)){
  703. return ResultWrapper::success($returnData);
  704. }
  705. $dbResult = $this->objDCommissionBusinessman->select(['userCenterId' => $emptyArray, 'deleteStatus' => StatusCode::$standard, 'auditStatus' => StatusCode::$auditStatus['auditPass']]);
  706. if($dbResult === false){
  707. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  708. }
  709. if(empty($dbResult)){
  710. return ResultWrapper::success($returnData);
  711. }
  712. $formatData = [];
  713. foreach($dbResult as $value){
  714. if(isset($value['statistics']) && !empty($value['statistics'])){
  715. $formatData[$value['userCenterId']] = json_decode($value['statistics'], true);
  716. }
  717. }
  718. foreach($emptyArray as $userCenterId){
  719. if(isset($formatData[$userCenterId])){
  720. $returnData[$userCenterId] = $formatData[$userCenterId];
  721. $cacheResult = $this->objCommissionCache->saveBusinessmanStatistics($userCenterId ,$formatData[$userCenterId]);
  722. if(!$cacheResult){
  723. return ResultWrapper::fail('保存缓存失败', ErrorCode::$paramError);
  724. }
  725. }
  726. }
  727. return ResultWrapper::success($returnData);
  728. }
  729. /**
  730. * 创建上下级关系
  731. * @param $params
  732. * @return ResultWrapper
  733. * @throws Exception
  734. */
  735. public function relationshipBusinessman($params)
  736. {
  737. if(empty($params)){
  738. return ResultWrapper::fail('创建上下级关系参数为空', ErrorCode::$paramError);
  739. }
  740. if(!isset($params['businessmanId']) || empty($params['businessmanId'])){
  741. return ResultWrapper::fail('创建上下级关系businessmanId参数为空', ErrorCode::$paramError);
  742. }
  743. // 获取分销基础设置数据
  744. $objMCommissionSetting = new MCommissionSetting($this->enterpriseId, $this->userCenterId);
  745. $modelResult = $objMCommissionSetting->getSetting();
  746. if(!$modelResult->isSuccess()){
  747. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  748. }
  749. $setting = $modelResult->getData();
  750. unset($modelResult);
  751. if(empty($setting)){
  752. return ResultWrapper::fail('分销设置查询失败', ErrorCode::$paramError);
  753. }
  754. // 判断分销等级是否开启
  755. if($setting['level'] == StatusCode::$level['off']){
  756. return ResultWrapper::fail('分销已关闭', ErrorCode::$paramError);
  757. }
  758. // 判断是否满足成为下线条件
  759. if($params['source'] != $setting['subConditions']){
  760. return ResultWrapper::success(true);
  761. }
  762. // 获取客户信息
  763. $modelResult = $this->objMCustomer->getCustomerData(['id' => $params['businessmanId'], 'deleteStatus' => StatusCode::$standard]);
  764. if(!$modelResult->isSuccess()){
  765. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  766. }
  767. $parent = $modelResult->getData();
  768. unset($modelResult);
  769. if(empty($parent)){
  770. return ResultWrapper::fail('您的上级分销商客户信息查询为空', ErrorCode::$notShowError);
  771. }
  772. // 判断是否为同一个人
  773. if($parent['userCenterId'] == $this->userCenterId){
  774. return ResultWrapper::fail('不能添加自己为上级', ErrorCode::$notShowError);
  775. }
  776. // 判断上级是否是分销商
  777. if($parent['isCommission'] != StatusCode::$standard){
  778. return ResultWrapper::fail('您的上级不是分销商', ErrorCode::$notShowError);
  779. }
  780. // 上级的分销层级关系
  781. $parentCommissionPath = [];
  782. if(!empty($parent['commissionPath'])){
  783. $parentCommissionPath = json_decode($parent['commissionPath'], true);
  784. }
  785. // 判断下级是否已经有上级
  786. $modelResult = $this->objMCustomer->getCustomerData(['userCenterId' => $this->userCenterId, 'deleteStatus' => StatusCode::$standard]);
  787. if(!$modelResult->isSuccess()){
  788. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  789. }
  790. $customer = $modelResult->getData();
  791. unset($modelResult);
  792. if(empty($customer)){
  793. return ResultWrapper::fail('当前客户信息查询为空', ErrorCode::$notShowError);
  794. }
  795. if(!empty($customer['commissionPath'])){
  796. return ResultWrapper::fail('您已经有上级分销商', ErrorCode::$notShowError);
  797. }
  798. //添加上下级关系
  799. $one = StatusCode::$level['one'];
  800. $two = StatusCode::$level['two'];
  801. $three = StatusCode::$level['three'];
  802. $commissionPath[$one] = $parent['userCenterId'];
  803. //增加1级 下线人数 一级下线人数
  804. $modelResult = self::outSetStatistics($commissionPath[$one], ['statistics' => ['sub' => 1, 'oneSub' => 1]]);
  805. if(!$modelResult->isSuccess()){
  806. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  807. }
  808. if(!empty($parentCommissionPath)){
  809. if(isset($parentCommissionPath[$one])){
  810. $commissionPath[$two] = $parentCommissionPath[$one];
  811. if($setting['level'] >= StatusCode::$level['two']) {
  812. //增加2级 下线人数
  813. $modelResult = self::outSetStatistics($commissionPath[$two], ['statistics' => ['sub' => 1]]);
  814. if(!$modelResult->isSuccess()){
  815. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  816. }
  817. }
  818. }
  819. if(isset($parentCommissionPath[$two])){
  820. $commissionPath[$three] = $parentCommissionPath[$two];
  821. if($setting['level'] >= StatusCode::$level['three']){
  822. //增加3级 下线人数
  823. $modelResult = self::outSetStatistics($commissionPath[$three], ['statistics' => ['sub' => 1]]);
  824. if(!$modelResult->isSuccess()){
  825. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  826. }
  827. }
  828. }
  829. }
  830. $update = [
  831. 'commissionPath' => json_encode($commissionPath),
  832. 'updateTime' => time()
  833. ];
  834. $modelResult = $this->objMCustomer->updateCustomer($update, ['userCenterId' => $this->userCenterId]);
  835. if(!$modelResult->isSuccess()){
  836. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  837. }
  838. $return = $modelResult->getData();
  839. unset($modelResult);
  840. return ResultWrapper::success($return);
  841. }
  842. /**
  843. * 回写统计数据
  844. * @param $userCenterId
  845. * @param $params
  846. * @return ResultWrapper
  847. * @throws Exception
  848. */
  849. public function outSetStatistics($userCenterId, $params)
  850. {
  851. /*$params = [
  852. 'balance' => 100.00, //余额
  853. 'waitMoney' => 100.00, //待入账金额
  854. 'withdraw' => 100.00, //已提现金额
  855. 'totalMoney' => 100.00, //累计佣金
  856. 'skuIds' => [1,2,3,4],//订单购买商品skuId
  857. 'statistics' => [
  858. 'orderMoney' => 100.00, //分销订单总额
  859. 'orderNum' => 100, //分销订单总数
  860. 'oneOrderMoney' => 100.00, //一级分销订单总额
  861. 'oneOrderNum' => 100, //一级分销订单总数
  862. 'selfOrderMoney' => 100.00, //自购订单总额
  863. 'selfOrderNum' => 100, //自购订单总数
  864. 'sub' => 100, //下线人数
  865. 'subBusinessman' => 100, //下线分销商人数
  866. 'oneSub' => 100, //一级下线人数
  867. 'oneSubBusinessman' => 100, //一级下线分销商
  868. 'auditOrderTotal' => 100, //审核订单总数
  869. 'auditTotalMoney' => 100, //审核订单总额
  870. ],
  871. ];*/
  872. //statistics
  873. $paramsStatistics = $params['statistics'];
  874. // 查询分销商等级升级条件
  875. $modelResult = $this->objMCommissionGrade->getAllGradeCondition();
  876. if(!$modelResult->isSuccess()){
  877. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  878. }
  879. $conditionData = $modelResult->getData();
  880. unset($modelResult);
  881. if(empty($conditionData)){
  882. return ResultWrapper::fail('升级条件查询失败', ErrorCode::$dberror);
  883. }
  884. //拿出所有条件
  885. $condition = [];
  886. foreach($conditionData as $value){
  887. $condition[$value['key']] = $value;
  888. }
  889. $dbResult = $this->objDCommissionBusinessman->get(['userCenterId' => $userCenterId, 'deleteStatus' => StatusCode::$standard]);
  890. if($dbResult === false){
  891. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  892. }
  893. if(empty($dbResult)){
  894. return ResultWrapper::fail('分销商查询失败', ErrorCode::$paramError);
  895. }
  896. $businessman = $dbResult;
  897. unset($dbResult);
  898. $statistics = json_decode($businessman['statistics'], true);
  899. // 查询所有分销商等级
  900. $modelResult = $this->objMCommissionGrade->getAllGrade();
  901. if(!$modelResult->isSuccess()){
  902. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  903. }
  904. $gradeData = $modelResult->getData();
  905. unset($modelResult);
  906. if(empty($gradeData)){
  907. return ResultWrapper::fail('分销商等级查询失败', ErrorCode::$dberror);
  908. }
  909. // 查询分销商设置数据
  910. $modelResult = $this->objMCommissionSetting->getSetting();
  911. if(!$modelResult->isSuccess()){
  912. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  913. }
  914. $setting = $modelResult->getData();
  915. unset($modelResult);
  916. //映射条件
  917. $keepArray = ['orderNum', 'oneOrderNum', 'selfOrderNum', 'sub', 'subBusinessman', 'oneSub', 'oneSubBusinessman', 'auditOrderTotal'];
  918. foreach($condition as $key => $value){
  919. if(isset($paramsStatistics[$key])){
  920. $keep = 2;
  921. if(in_array($key,$keepArray)){
  922. $keep = 0;
  923. }
  924. if($businessman['auditStatus'] == StatusCode::$auditStatus['auditing']){
  925. //未审核分销商 只累加审核字段数据
  926. if($value['type'] == StatusCode::$standard){
  927. if(isset($statistics[$key])){
  928. $statistics[$key] = bcadd($statistics[$key], $paramsStatistics[$key], $keep);
  929. }else{
  930. $statistics[$key] = $paramsStatistics[$key];
  931. }
  932. }
  933. }else{
  934. //已审核分销商 累加所有字段
  935. if(isset($statistics[$key])){
  936. //判断是否是sub,subBusinessman统计字段
  937. if($key == 'sub' || $key == 'subBusinessman'){
  938. //查询下级人数 累加
  939. $this->objCommissionCache->delBusinessmanSub($userCenterId);
  940. $modelResult = self::getBusinessmanSubLevelNum([$userCenterId]);
  941. if(!$modelResult->isSuccess()){
  942. return ResultWrapper::fail($modelResult->getData(),$modelResult->getErrorCode());
  943. }
  944. $statisticsResult = $modelResult->getData();
  945. unset($modelResult);
  946. $statistics[$key] = bcadd($statisticsResult[$userCenterId][$key][$setting['level']], $paramsStatistics[$key], $keep);
  947. //删除缓存
  948. $this->objCommissionCache->delBusinessmanSub($userCenterId);
  949. }else{
  950. $statistics[$key] = bcadd($statistics[$key], $paramsStatistics[$key], $keep);
  951. }
  952. }else{
  953. $statistics[$key] = $paramsStatistics[$key];
  954. }
  955. }
  956. }
  957. }
  958. $suffice = 0;
  959. $upgrade = 0;
  960. $grade = $businessman['grade'];
  961. //有条件 判断条件
  962. foreach($gradeData as $key => $value){
  963. if($value['grade'] > $grade){
  964. if(!empty($value['upgradeConditionInfo'])){
  965. $suffice = 0;
  966. foreach($value['upgradeConditionInfo'] as $info){
  967. //判断传参有没有条件值 $statistics统计数据 $info升级条件
  968. if(isset($statistics[$info['key']]) && $info['value'] > 0){
  969. if($statistics[$info['key']] >= $info['value']){
  970. $suffice++;
  971. }
  972. }
  973. }
  974. //判断全部满足还是满足一个
  975. if($value['upgradeMode'] == StatusCode::$standard){
  976. $count = count($value['upgradeConditionInfo']);
  977. //满足全部
  978. if($count == $suffice){
  979. $upgrade = true;
  980. $grade = $value['grade'];
  981. }
  982. }else{
  983. //满足一个
  984. if($suffice > 0){
  985. $upgrade = true;
  986. $grade = $value['grade'];
  987. }
  988. }
  989. }else{
  990. //无条件 直接升级
  991. $upgrade = true;
  992. $grade = $value['grade'];
  993. }
  994. }
  995. }
  996. $buyGoodsGrade = 0;
  997. $auditStatus = false;
  998. $applicationComplete = StatusCode::$delete;
  999. //如果分销商未审核 并且条件也不满足
  1000. if($businessman['auditStatus'] == StatusCode::$auditStatus['auditing'] && $businessman['applicationComplete'] == StatusCode::$delete){
  1001. //判断添加分销商是基础设置 成为分销商条件
  1002. switch ($businessman['applicationCondition']){
  1003. case 2:
  1004. //消费次数
  1005. if(isset($statistics['auditOrderTotal']) && !empty($statistics['auditOrderTotal'])){
  1006. //判断是否大于等于设置数量
  1007. if($statistics['auditOrderTotal'] >= $setting['buyNum']){
  1008. $auditStatus = StatusCode::$auditStatus['auditPass'];
  1009. $applicationComplete = StatusCode::$standard;
  1010. }
  1011. }
  1012. break;
  1013. case 3:
  1014. //消费金额
  1015. if(isset($statistics['auditTotalMoney']) && !empty($statistics['auditTotalMoney'])){
  1016. //判断是否大于等于设置数量
  1017. if($statistics['auditTotalMoney'] >= $setting['buyAmount']){
  1018. $auditStatus = StatusCode::$auditStatus['auditPass'];
  1019. $applicationComplete = StatusCode::$standard;
  1020. }
  1021. }
  1022. break;
  1023. case 4:
  1024. //购买指定商品 成为分销商 或者 直接升级
  1025. if(!empty($params['skuIds'])){
  1026. //对比购买商品id 和 设置的商品id有没有重复
  1027. foreach($setting['buyGoods'] as $key => $value){
  1028. if(!empty($value['goodsData'])){
  1029. foreach($value['goodsData'] as $v){
  1030. if(in_array($v['skuId'], $params['skuIds'])){
  1031. if($key > $buyGoodsGrade){
  1032. $upgrade = true;
  1033. $buyGoodsGrade = (int) $key;
  1034. }
  1035. }
  1036. }
  1037. }
  1038. }
  1039. //如果有修改 审核状态 满足条件状态
  1040. $auditStatus = StatusCode::$auditStatus['auditPass'];
  1041. $applicationComplete = StatusCode::$standard;
  1042. }
  1043. break;
  1044. default:
  1045. break;
  1046. }
  1047. }
  1048. //保存
  1049. $update = [
  1050. 'statistics' => json_encode($statistics),
  1051. 'updateTime' => time()
  1052. ];
  1053. if($upgrade && $grade != $businessman['grade']){
  1054. if($buyGoodsGrade) $grade = $buyGoodsGrade;
  1055. $modelResult = $this->objMCommissionGrade->getInfoGrade(['grade' => $grade]);
  1056. if(!$modelResult->isSuccess()){
  1057. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1058. }
  1059. $topGrade = $modelResult->getData();
  1060. unset($modelResult);
  1061. if(!empty($topGrade)){
  1062. $update['grade'] = $grade;
  1063. $update['gradeId'] = $topGrade['id'];
  1064. }
  1065. }
  1066. isset($params['balance']) && $update['balance'] = $params['balance'];
  1067. isset($params['waitMoney']) && $update['waitMoney'] = $params['waitMoney'];
  1068. isset($params['withdraw']) && $update['withdraw'] = $params['withdraw'];
  1069. isset($params['totalMoney']) && $update['totalMoney'] = $params['totalMoney'];
  1070. $applicationComplete == StatusCode::$standard && $update['applicationComplete'] = $applicationComplete;
  1071. //判断修改审核状态 设置中审核分销商是否关闭
  1072. if($auditStatus && $setting['audit'] == StatusCode::$delete){
  1073. //如果关闭 直接修改审核状态 为 已审核
  1074. $update['auditStatus'] = $auditStatus;
  1075. $update['auditTime'] = time();
  1076. }
  1077. $modelResult = self::updateBusinessman($update, ['id' => $businessman['id']]);
  1078. if(!$modelResult->isSuccess()){
  1079. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1080. }
  1081. return ResultWrapper::success($modelResult->getData());
  1082. }
  1083. /**
  1084. * 获取下级
  1085. * @param $params
  1086. * @return ResultWrapper
  1087. * @throws Exception
  1088. */
  1089. public function getAllSub($params)
  1090. {
  1091. $limit = $params['limit'];
  1092. unset($params['limit']);
  1093. $offset = $params['offset'];
  1094. unset($params['offset']);
  1095. //查询分销商数据
  1096. $dbResult = $this->objDCommissionBusinessman->get(['id' => $params['id'], 'deleteStatus' => StatusCode::$standard, 'auditStatus' => StatusCode::$auditStatus['auditPass']]);
  1097. if($dbResult === false){
  1098. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  1099. }
  1100. $businessman = $dbResult;
  1101. unset($dbResult);
  1102. if(empty($businessman)){
  1103. return ResultWrapper::fail('分销商不存在', ErrorCode::$paramError);
  1104. }
  1105. $statistics = json_decode($businessman['statistics'], true);
  1106. $businessman['statistics'] = $statistics;
  1107. //查询客户数据
  1108. $modelResult = $this->objMCustomer->getCustomerData(['id' => $businessman['customerId'], 'deleteStatus' => StatusCode::$standard, 'status' => StatusCode::$auditStatus['auditPass']]);
  1109. if(!$modelResult->isSuccess()){
  1110. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1111. }
  1112. $customer = $modelResult->getData();
  1113. unset($modelResult);
  1114. if(empty($customer)){
  1115. return ResultWrapper::fail('客户数据查询失败', ErrorCode::$dberror);
  1116. }
  1117. //查询统计数据
  1118. $modelResult = self::getBusinessmanSubLevelNum([$businessman['userCenterId']]);
  1119. if(!$modelResult->isSuccess()){
  1120. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1121. }
  1122. $statisticsNum = $modelResult->getData();
  1123. unset($modelResult);
  1124. //查询设置
  1125. $modelResult = $this->objMCommissionSetting->getSetting();
  1126. if(!$modelResult->isSuccess()){
  1127. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1128. }
  1129. $setting = $modelResult->getData();
  1130. unset($modelResult);
  1131. //查询下级
  1132. $field = ' * ';
  1133. $sql = 'select '.$field.' from qianniao_customer_'.$this->enterpriseId;
  1134. $whereSql = " where commissionPath->'$.\"".$params['level']."\"' = ".$businessman["userCenterId"];
  1135. $whereSql .= ' and deleteStatus = '.StatusCode::$standard;
  1136. $orderSql = ' order by id desc ';
  1137. $limitSql = ' limit '.$offset.','.$limit;
  1138. $sql = $sql.$whereSql.$orderSql.$limitSql;
  1139. $modelResult = $this->objMCustomer->getCustomerDataByQuery($sql);
  1140. if(!$modelResult->isSuccess()){
  1141. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1142. }
  1143. $customerArray = $modelResult->getData();
  1144. unset($modelResult);
  1145. $totalData = [];
  1146. if(!empty($customerArray)){
  1147. //提取所有userCenterId
  1148. $userCenterIds = array_column($customerArray, 'userCenterId');
  1149. //查询分销订单数 分销订单总金额
  1150. $objMCommissionOrder = new MCommissionOrder($this->enterpriseId, $this->userCenterId);
  1151. $modelResult = $objMCommissionOrder->getCommissionOrderData($userCenterIds);
  1152. if(!$modelResult->isSuccess()){
  1153. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1154. }
  1155. $totalData = $modelResult->getData();
  1156. unset($modelResult);
  1157. }
  1158. $returnData = [];
  1159. foreach($customerArray as $value){
  1160. $data = [
  1161. 'userCenterId' =>$value['userCenterId'],
  1162. 'avatar' => $value['avatar'],
  1163. 'name' => $value['name'],
  1164. 'createTime' => $value['createTime'],
  1165. 'isCommission' => $value['isCommission'],
  1166. 'totalMoney' => 0.00,
  1167. 'orderTotal' => 0,
  1168. ];
  1169. if(isset($totalData[$value['userCenterId']])){
  1170. $data['totalMoney'] = isset($totalData[$value['userCenterId']]['totalOrderAmount']) ? $totalData[$value['userCenterId']]['totalOrderAmount'] : 0.00;
  1171. $data['orderTotal'] = isset($totalData[$value['userCenterId']]['total']) ? $totalData[$value['userCenterId']]['total'] : 0;
  1172. }
  1173. $returnData[] = $data;
  1174. }
  1175. $countSql = 'select count(*) as count from qianniao_customer_'.$this->enterpriseId;
  1176. $countSql .= $whereSql;
  1177. $modelResult = $this->objMCustomer->getCustomerDataByQuery($countSql);
  1178. if(!$modelResult->isSuccess()){
  1179. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1180. }
  1181. $countResult = $modelResult->getData();
  1182. unset($modelResult);
  1183. $count = $countResult ? array_shift($countResult) : 0;
  1184. //该等级分销商数
  1185. $businessmanSql = 'select count(*) as `count` from qianniao_customer_'.$this->enterpriseId;
  1186. $businessmanSql .= $whereSql.' and isCommission = '.StatusCode::$standard;
  1187. $modelResult = $this->objMCustomer->getCustomerDataByQuery($businessmanSql);
  1188. if(!$modelResult->isSuccess()){
  1189. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1190. }
  1191. $topCountResult = $modelResult->getData();
  1192. unset($modelResult);
  1193. $topCount = $topCountResult ? array_shift($topCountResult) : 0;
  1194. $return = [
  1195. 'data' => $returnData,
  1196. 'total' => isset($count['count']) ? $count['count'] : 0,
  1197. 'subTotal' => isset($statisticsNum[$businessman['userCenterId']]['sub'][$setting['level']]) ? (int)$statisticsNum[$businessman['userCenterId']]['sub'][$setting['level']] : 0,
  1198. 'businessmanTotal' => isset($statisticsNum[$businessman['userCenterId']]['subBusinessman'][$setting['level']]) ? $statisticsNum[$businessman['userCenterId']]['subBusinessman'][$setting['level']] : 0,
  1199. 'subBusinessmanTotal' => isset($topCount['count']) ? $topCount['count'] : 0,
  1200. ];
  1201. return ResultWrapper::success($return);
  1202. }
  1203. /**
  1204. * 首页数据统计
  1205. */
  1206. public function statisticsCommissionOne()
  1207. {
  1208. //提现待审核
  1209. $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'];
  1210. $dbResult = $this->objDCommissionBusinessman->query($sql);
  1211. if($dbResult === false){
  1212. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  1213. }
  1214. $dbResult = array_shift($dbResult);
  1215. //提现待审核
  1216. $totalData['withdrawNoAudit'] = $dbResult['waitAuditWithdraw'];
  1217. //提现待打款
  1218. $totalData['withdrawNoPay'] = $dbResult['auditWithdraw'];
  1219. //总金额 累计佣金
  1220. $totalData['totalMoney'] = $dbResult['totalMoney'];
  1221. //已提现 提现佣金
  1222. $totalData['withdraw'] = $dbResult['withdraw'];
  1223. unset($dbResult);
  1224. //分销商
  1225. //待审核
  1226. $dbResult = $this->objDCommissionBusinessman->count(['auditStatus' => StatusCode::$auditStatus['auditing'], 'deleteStatus' => StatusCode::$standard]);
  1227. if($dbResult === false){
  1228. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  1229. }
  1230. $totalData['auditBusinessman'] = $dbResult;
  1231. unset($dbResult);
  1232. //分销商
  1233. $dbResult = $this->objDCommissionBusinessman->count(['auditStatus' => StatusCode::$auditStatus['auditPass'], 'deleteStatus' => StatusCode::$standard]);
  1234. if($dbResult === false){
  1235. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  1236. }
  1237. $totalData['businessman'] = $dbResult;
  1238. unset($dbResult);
  1239. //会员数
  1240. $sql = 'select count(*) as total from qianniao_customer_'.$this->enterpriseId.' where status = '.StatusCode::$auditStatus['auditPass']. ' and deleteStatus = '.StatusCode::$standard;
  1241. $modelResult = $this->objMCustomer->getCustomerDataByQuery($sql);
  1242. if(!$modelResult->isSuccess()){
  1243. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1244. }
  1245. $dbResult = $modelResult->getData();
  1246. $dbResult = array_shift($dbResult);
  1247. $totalData['customer'] = $dbResult['total'];
  1248. unset($dbResult);
  1249. unset($modelResult);
  1250. //每个等级分销商人数
  1251. $sql = 'select grade,count(*) as num from '.$this->objDCommissionBusinessman->get_Table().' where deleteStatus = '.StatusCode::$standard.' and auditStatus = '.StatusCode::$auditStatus['auditPass'].' group by grade';
  1252. $dbResult = $this->objDCommissionBusinessman->query($sql);
  1253. if($dbResult === false){
  1254. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  1255. }
  1256. $businessman = $dbResult;
  1257. $formatBusinessman = [];
  1258. foreach($businessman as $value){
  1259. $formatBusinessman[$value['grade']] = $value['num'];
  1260. }
  1261. //查询所有等级
  1262. $modelResult = $this->objMCommissionGrade->getAllGrade();
  1263. if(!$modelResult->isSuccess()){
  1264. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1265. }
  1266. $grade = $modelResult->getData();
  1267. unset($modelResult);
  1268. $totalData['gradeBusinessman'] = [];
  1269. foreach($grade as $value){
  1270. $totalData['gradeBusinessman'][$value['grade']]['gradeName'] = $value['name'];
  1271. $totalData['gradeBusinessman'][$value['grade']]['grade'] = $value['grade'];
  1272. $totalData['gradeBusinessman'][$value['grade']]['businessmanNum'] = 0;
  1273. if(isset($formatBusinessman[$value['grade']])){
  1274. $totalData['gradeBusinessman'][$value['grade']]['businessmanNum'] = $formatBusinessman[$value['grade']];
  1275. }
  1276. }
  1277. return ResultWrapper::success($totalData);
  1278. }
  1279. /**
  1280. * 首页数据统计(新增分销商数)
  1281. */
  1282. public function statisticsCommissionTwo()
  1283. {
  1284. $day = 6;
  1285. $return = [];
  1286. for($i = $day; $i >= 0; $i--){
  1287. $data = ['createData' => date("Y-m-d",strtotime("-".$i." day")), 'count' => 0];
  1288. $data['createTime'] = strtotime($data['createData']);
  1289. $return[] = $data;
  1290. }
  1291. //查询每天分销商人数
  1292. $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';
  1293. $dbResult = $this->objDCommissionBusinessman->query($sql);
  1294. if($dbResult === false){
  1295. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  1296. }
  1297. if(!empty($dbResult)){
  1298. $format = [];
  1299. foreach($dbResult as $value){
  1300. $format[strtotime($value['createDate'])] = $value['count'];
  1301. }
  1302. foreach($return as &$value){
  1303. if(isset($format[$value['createTime']])){
  1304. $value['count'] = $format[$value['createTime']];
  1305. }
  1306. $value['createData'] = date("m-d", $value['createTime']);
  1307. unset($value['createTime']);
  1308. }
  1309. unset($value);
  1310. }
  1311. return ResultWrapper::success($return);
  1312. }
  1313. /**
  1314. * 首页统计(TOP)
  1315. */
  1316. public function statisticsCommissionThree()
  1317. {
  1318. //下级会员top10
  1319. $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';
  1320. $dbResult = $this->objDCommissionBusinessman->query($sql);
  1321. if($dbResult === false){
  1322. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  1323. }
  1324. if(!empty($dbResult)){
  1325. $modelResult = self::formatBusinessman($dbResult);
  1326. if(!$modelResult->isSuccess()){
  1327. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1328. }
  1329. $dbResult = $modelResult->getData();
  1330. unset($modelResult);
  1331. }
  1332. $subTop = [];
  1333. foreach ($dbResult as $value){
  1334. $subTop[] = [
  1335. 'avatar' => $value['avatar'],
  1336. 'name' => $value['name'],
  1337. 'sub' => (int) json_decode($value['sub'], true)
  1338. ];
  1339. }
  1340. $return['subTop'] = $subTop;
  1341. unset($dbResult);
  1342. //累计佣金top10
  1343. $sql = 'select * from '.$this->objDCommissionBusinessman->get_Table().' where deleteStatus = '.StatusCode::$standard.' and auditStatus = '.StatusCode::$auditStatus['auditPass'].' order by totalMoney desc limit 0,10';
  1344. $dbResult = $this->objDCommissionBusinessman->query($sql);
  1345. if($dbResult === false){
  1346. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  1347. }
  1348. if(!empty($dbResult)){
  1349. $modelResult = self::formatBusinessman($dbResult);
  1350. if(!$modelResult->isSuccess()){
  1351. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1352. }
  1353. $dbResult = $modelResult->getData();
  1354. unset($modelResult);
  1355. }
  1356. $moneyTopTop = [];
  1357. foreach ($dbResult as $value){
  1358. $moneyTopTop[] = [
  1359. 'avatar' => $value['avatar'],
  1360. 'name' => $value['name'],
  1361. 'money' => $value['totalMoney']
  1362. ];
  1363. }
  1364. $return['moneyTop'] = $moneyTopTop;
  1365. return ResultWrapper::success($return);
  1366. }
  1367. /**
  1368. * 根据手机号, 微信昵称 查询分销商
  1369. * @param $searchParam
  1370. * @return ResultWrapper
  1371. */
  1372. public function searchBusinessman($searchParam)
  1373. {
  1374. $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;
  1375. $dbResult = $this->objDCommissionBusinessman->query($sql);
  1376. if($dbResult === false){
  1377. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  1378. }
  1379. return ResultWrapper::success($dbResult);
  1380. }
  1381. /**
  1382. * 等级说明
  1383. */
  1384. public function getBusinessmanGradeInfo()
  1385. {
  1386. $modelResult = $this->objMCommissionGrade->getAllGrade();
  1387. if(!$modelResult->isSuccess()){
  1388. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1389. }
  1390. $gradeData = $modelResult->getData();
  1391. unset($modelResult);
  1392. $modelResult = self::getInfoBusinessman(['userCenterId' => $this->userCenterId]);
  1393. if(!$modelResult->isSuccess()){
  1394. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1395. }
  1396. $businessmanData = $modelResult->getData();
  1397. unset($modelResult);
  1398. foreach($gradeData as &$value){
  1399. if($value['upgradeMode'] == StatusCode::$standard){
  1400. //全部满足
  1401. if($businessmanData['grade'] == $value['grade']){
  1402. foreach($value['upgradeConditionInfo'] as &$v){
  1403. if(isset($businessmanData['statistics'][$v['key']])){
  1404. if($businessmanData['statistics'][$v['key']] >= $v['value']){
  1405. $v['status'] = true;
  1406. }
  1407. }else{
  1408. $v['status'] = false;
  1409. }
  1410. }
  1411. }elseif($businessmanData['grade'] > $value['grade']){
  1412. foreach($value['upgradeConditionInfo'] as &$v){
  1413. $v['status'] = true;
  1414. }
  1415. }else{
  1416. foreach($value['upgradeConditionInfo'] as &$v){
  1417. $v['status'] = false;
  1418. }
  1419. }
  1420. }else{
  1421. //满足一个
  1422. if($businessmanData['grade'] > $value['grade']){
  1423. foreach($value['upgradeConditionInfo'] as &$v){
  1424. $v['status'] = true;
  1425. }
  1426. }else{
  1427. foreach($value['upgradeConditionInfo'] as &$v){
  1428. $v['status'] = false;
  1429. }
  1430. }
  1431. }
  1432. }
  1433. return ResultWrapper::success($gradeData);
  1434. }
  1435. /**
  1436. *
  1437. * 查询某个分销商下级人数
  1438. * @param $userCenterIds
  1439. */
  1440. public function getBusinessmanSubLevelNum($userCenterIds)
  1441. {
  1442. //查询缓存
  1443. if(empty($userCenterIds)){
  1444. return ResultWrapper::fail('userCenterIds参数为空', ErrorCode::$paramError);
  1445. }
  1446. $emptyUserCenterId = [];
  1447. $statistics = [];
  1448. foreach($userCenterIds as $value){
  1449. $cacheResult = $this->objCommissionCache->getBusinessmanSub($value);
  1450. $cacheResult = [];
  1451. if(!empty($cacheResult)){
  1452. $statistics[$value] = $cacheResult;
  1453. }else{
  1454. $emptyUserCenterId[] = $value;
  1455. }
  1456. }
  1457. if(empty($emptyUserCenterId)){
  1458. return ResultWrapper::success($statistics);
  1459. }
  1460. //缓存不存在 查询数据库
  1461. foreach($emptyUserCenterId as $value){
  1462. //查询下级
  1463. $subNum = 0;
  1464. $subBusinessmanNum = 0;
  1465. for($i = 1; $i <= 3; $i++){
  1466. //分销商
  1467. $sql = 'select count(*) as count from qianniao_customer_'.$this->enterpriseId.' where isCommission = '.StatusCode::$standard.' and commissionPath->\'$."'.$i.'"\' = '.$value;
  1468. $dbResult = $this->objDCommissionBusinessman->query($sql);
  1469. if($dbResult === false){
  1470. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  1471. }
  1472. $statistics[$value]['subBusinessman'][$i] = $subBusinessmanNum + array_shift($dbResult)['count'];
  1473. $subBusinessmanNum = $statistics[$value]['subBusinessman'][$i];
  1474. //用户
  1475. $sql = 'select count(*) as count from qianniao_customer_'.$this->enterpriseId.' where commissionPath->\'$."'.$i.'"\' = '.$value;
  1476. $dbResult = $this->objDCommissionBusinessman->query($sql);
  1477. if($dbResult === false){
  1478. return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
  1479. }
  1480. $statistics[$value]['sub'][$i] = $subNum + array_shift($dbResult)['count'];
  1481. $subNum = $statistics[$value]['sub'][$i];
  1482. }
  1483. //保存缓存
  1484. $cacheResult = $this->objCommissionCache->saveBusinessmanSub($value, $statistics[$value]);
  1485. if($cacheResult === false){
  1486. return ResultWrapper::fail('缓存失败', ErrorCode::$paramError);
  1487. }
  1488. }
  1489. //返回数据
  1490. return ResultWrapper::success($statistics);
  1491. }
  1492. /**
  1493. * 修改直系上级关系
  1494. * @throws Exception
  1495. * 客户id,上级id
  1496. */
  1497. public function editRelationship($params)
  1498. {
  1499. $id = $params['id'];
  1500. $superiorId = $params['superiorId'];
  1501. // 查询当前客户的上级分销商数据
  1502. $dbResult = $this->objDCustomer->get(['userCenterId'=>$id]);
  1503. if($dbResult === false){
  1504. return ResultWrapper::fail($this->objDCustomer->error(), ErrorCode::$dberror);
  1505. }
  1506. if(empty($dbResult['commissionPath'])){
  1507. return ResultWrapper::fail('当前用户没有上级分销商',ErrorCode::$notAllowAccess);
  1508. }
  1509. $distributionDate = json_decode($dbResult['commissionPath'],true);
  1510. $tmpDate = [];
  1511. // 查询分销基础设置数据
  1512. $modelResult = $this->objMCommissionSetting->getSetting();
  1513. if(!$modelResult->isSuccess()){
  1514. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1515. }
  1516. $setting = $modelResult->getData();
  1517. if(empty($setting)){
  1518. return ResultWrapper::fail('获取分销商基础信息错误',ErrorCode::$notAllowAccess);
  1519. }
  1520. $level = $setting['level'];
  1521. // 判断当前要需改的上级分销商是否已经存在上集中
  1522. foreach ($distributionDate as $key => $value){
  1523. if($value == $superiorId){
  1524. return ResultWrapper::fail('要新绑定的分销商已经是该用户的上级',ErrorCode::$notAllowAccess);
  1525. }
  1526. }
  1527. // 查询替换分销商的上级信息
  1528. $dbResult1 = $this->objDCustomer->get(['userCenterId'=>$superiorId]);
  1529. if($dbResult1 === false){
  1530. return ResultWrapper::fail($this->objDCustomer->error(), ErrorCode::$dberror);
  1531. }
  1532. if(empty($dbResult1)){
  1533. return ResultWrapper::fail('当前上级分销商不存在',ErrorCode::$notAllowAccess);
  1534. }
  1535. if(!empty($dbResult1['commissionPath'])){
  1536. $distributionDate1 = json_decode($dbResult1['commissionPath'],true);
  1537. }
  1538. if($level == 1){
  1539. $tmpDate = [
  1540. "1"=>$superiorId,
  1541. ];
  1542. //更新人数
  1543. $modelResult = self::outSetStatistics($superiorId, ['statistics' => ['sub' => 1, 'oneSub' => 1]]);
  1544. if(!$modelResult->isSuccess()){
  1545. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1546. }
  1547. }
  1548. if($level == 2){
  1549. $tmpDate = [
  1550. "1"=>$superiorId,
  1551. ];
  1552. if(isset($distributionDate1['1']) && !empty($distributionDate1['1']) ){
  1553. array_push($tmpDate,$distributionDate1['1']);
  1554. }
  1555. //更新人数
  1556. $modelResult = self::outSetStatistics($superiorId, ['statistics' => ['sub'=>1]]);
  1557. if(!$modelResult->isSuccess()){
  1558. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1559. }
  1560. }
  1561. if($level == 3){
  1562. $tmpDate = [
  1563. "1"=>$superiorId,
  1564. ];
  1565. if(isset($distributionDate1['1']) && !empty($distributionDate1['1']) ){
  1566. array_push($tmpDate,$distributionDate1['1']);
  1567. }
  1568. if(isset($distributionDate1['2']) && !empty($distributionDate1['2']) ){
  1569. array_push($tmpDate,$distributionDate1['2']);
  1570. }
  1571. //更新人数
  1572. $modelResult = self::outSetStatistics($superiorId, ['statistics' => ['sub'=>1]]);
  1573. if(!$modelResult->isSuccess()){
  1574. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  1575. }
  1576. }
  1577. //更新分销商等级关系
  1578. $updateResult = $this->objDCustomer->update(['commissionPath'=>json_encode($tmpDate)],['userCenterId'=>$id]);
  1579. if($updateResult === false){
  1580. return ResultWrapper::fail($this->objDCustomer->error(), ErrorCode::$dberror);
  1581. }
  1582. return ResultWrapper::success($updateResult);
  1583. }
  1584. }