1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855 |
- <?php
- namespace JinDouYun\Model\Commission;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Core\StatusCode;
- use JinDouYun\Dao\Commission\DCommissionFlow;
- use JinDouYun\Controller\Common\Logger;
- use JinDouYun\Dao\Commission\DCommissionOrder;
- use JinDouYun\Dao\Customer\DCustomer;
- use JinDouYun\Dao\Goods\DGoods;
- use JinDouYun\Dao\Order\DOrderGoods;
- use JinDouYun\Dao\Order\DOrderIndex;
- use JinDouYun\Dao\Commission\DCommissionBusinessman;
- use JinDouYun\Dao\Commission\DCommissionGoods;
- use JinDouYun\Dao\Commission\DCommissionGoodsStatistics;
- use JinDouYun\Dao\Order\DOrder;
- use JinDouYun\Dao\Commission\DCommissionOrderIndex;
- /**
- * Description:分销计算
- * Class MCommissionCalculate
- * @package JinDouYun\Model\Commission
- */
- class MCommissionCalculate
- {
- /**
- * @var array 插入分销商品记录表的数据
- */
- private $insert = [];
- /**
- * @var DCommissionOrder
- */
- private $objDCommissionOrder;
- /**
- * @var integer
- */
- private $onlineUserId;
- /**
- * @var integer
- */
- private $onlineEnterpriseId;
- /**
- * @var DCustomer
- */
- private $objDCustomer;
- /**
- * @var MCommissionSetting
- */
- private $objMCommissionSetting;
- /**
- * @var DOrderIndex
- */
- private $objDOrderIndex;
- /**
- * @var integer
- */
- private $cutTable = 200000;
- /**
- * @var DOrderGoods
- */
- private $objDOrderGoods;
- /**
- * @var DGoods
- */
- private $objDGoods;
- /**
- * @var DCommissionGoods
- */
- private $objDCommissionGoods;
- /**
- * @var int 4:优惠前计算 5:实际支付金额
- */
- private $mode;
- /**
- * @var array 级别对应标示映射
- */
- private static $levelMap = [
- 1 => 'oneRate',
- 2 => 'twoRate',
- 3 => 'threeRate',
- 'oneRate' => 1,
- 'twoRate' => 2,
- 'threeRate' => 3
- ];
- /**
- * @var DCommissionBusinessman
- */
- private $objDCommissionBusinessman;
- /**
- * @var MCommissionBusinessman
- */
- private $objMCommissionBusinessman;
- /**
- * @var int 平台设置分销层级
- */
- private $level;
- /**
- * @var MCommissionGrade
- */
- private $objMCommissionGrade;
- /**
- * @var array 需要检测是否达到升级条件的userCenterId
- */
- private $needUserCenterIds = [];
- /**
- * @var array 所有等级条件别名
- */
- private $allAliasCondition = [];
- /**
- * @var DCommissionGoodsStatistics
- */
- private $objDCommissionGoodsStatistics;
- /**
- * @var DOrder
- */
- private $objDOrder;
- /**
- * @var DCommissionOrderIndex
- */
- private $objDCommissionOrderIndex;
- /**
- * @var DCommissionFlow
- */
- private $objDCommissionFlow;
- /**
- * @var array 分销设置
- */
- private $setting;
- /**
- * @var array 当前订单信息
- */
- private $orderData;
- /**
- * @var array 当前订单商品
- */
- private $orderGoods;
- /**
- * @var int 是否开启自购
- */
- private $isRetSelf;
- /**
- * MCommissionCalculate constructor.
- * @param $onlineEnterpriseId
- * @param $onlineUserId
- * @throws \Exception
- */
- public function __construct($onlineEnterpriseId, $onlineUserId)
- {
- $this->onlineUserId = $onlineUserId;
- $this->onlineEnterpriseId = $onlineEnterpriseId;
- $this->objDCommissionOrder = new DCommissionOrder();
- $this->objDCommissionOrder->setTable('qianniao_commission_order_' . $this->onlineEnterpriseId . '_' . date('Y') . '_' . ceil(date('m') / 3));
- $this->objDCustomer = new DCustomer();
- $this->objDCustomer->setTable('qianniao_customer_' . $this->onlineEnterpriseId);
- $this->objMCommissionSetting = new MCommissionSetting($this->onlineEnterpriseId, $this->onlineUserId);
- $this->objDOrderIndex = new DOrderIndex();
- $this->objDOrderIndex->setTable('qianniao_order_index_' . $this->onlineEnterpriseId);
- $this->objDOrderGoods = new DOrderGoods();
- $this->objDOrder = new DOrder();
- $this->objDGoods = new DGoods();
- $this->objDGoods->setTable('qianniao_goods_' . $this->onlineEnterpriseId);
- $this->objDCommissionGoods = new DCommissionGoods();
- $this->objDCommissionGoods->setTable('qianniao_commission_goods_' . $this->onlineEnterpriseId);
- $this->objDCommissionBusinessman = new DCommissionBusinessman();
- $this->objDCommissionBusinessman->setTable('qianniao_commission_businessman_' . $this->onlineEnterpriseId);
- $this->objMCommissionBusinessman = new MCommissionBusinessman($this->onlineEnterpriseId, $this->onlineUserId);
- $this->objMCommissionGrade = new MCommissionGrade($this->onlineEnterpriseId, $this->onlineUserId);
- $this->objDCommissionGoodsStatistics = new DCommissionGoodsStatistics();
- $this->objDCommissionGoodsStatistics->setTable('qianniao_commission_goods_statistics_'.$this->onlineEnterpriseId);
- $this->objDCommissionOrderIndex = new DCommissionOrderIndex();
- $this->objDCommissionOrderIndex->setTable('qianniao_commission_order_index_'.$this->onlineEnterpriseId);
- $this->objDCommissionFlow = new DCommissionFlow();
- $this->objDCommissionFlow->setTable('qianniao_commission_flow_'.$this->onlineEnterpriseId);
- $this->needUserCenterIds = [];
- }
- /**
- * 增加分销商品的销量以及分销金额
- * @throws \Exception
- */
- public function __destruct()
- {
- //todo
- //在这里判断分销商是否达到升级条件,进行升级
- // TODO: Implement __destruct() method.
- if (!empty($this->insert)) self::incrCommissionGoods($this->insert);//增加销量
- if (!empty($this->orderData)){
- $result = $this->objMCommissionBusinessman->outSetStatistics(
- $this->orderData['userCenterId'],
- [
- 'skuIds' => array_column($this->orderGoods,'skuId'),
- 'statistics'=>[
- 'auditOrderTotal' => 1,
- 'auditTotalMoney' => $this->orderData['payAmount']
- ]
- ]
- );
- if (!$result->isSuccess()){
- Logger::logs(E_USER_ERROR,'分销商',__CLASS__,__LINE__,$result->getData());
- }
- }
- //if (!empty($this->needUserCenterIds)) self::upgrade();//升级
- }
- /**
- * Doc: (des="创建分销")
- * User: XMing
- * Date: 2020/7/23
- * Time: 10:46 上午
- * @param int $customerId
- * @param int $orderId
- * @return ResultWrapper
- * @throws \Exception
- */
- public function createCommission(int $customerId, int $orderId)
- {
- // 查询买家是否有上级,和买家是否是分销商
- $customer = $this->objDCustomer->get(['id' => $customerId], 'isCommission,commissionPath');
- if ($customer === false) {
- return ResultWrapper::fail($this->objDCustomer->error(), ErrorCode::$dberror);
- }
- if (empty($customer)) {
- return ResultWrapper::fail('未查询到客户', ErrorCode::$paramError);
- }
- // 获取订单索引数据
- $orderIndex = $this->objDOrderIndex->get(['id' => $orderId], 'userCenterId,no');
- if ($orderIndex === false) {
- return ResultWrapper::fail($this->objDOrderIndex->error(), ErrorCode::$dberror);
- }
- if (empty($orderIndex)) {
- return ResultWrapper::fail('未查询到指定订单', ErrorCode::$paramError);
- }
- // 切表
- $fix = ceil($orderIndex['userCenterId'] / $this->cutTable);
- $this->objDOrderGoods->setTable('qianniao_order_goods_' . $this->onlineEnterpriseId . '_' . $fix);
- $this->objDOrder->setTable('qianniao_order_'.$this->onlineEnterpriseId.'_'.$fix);
- // 获取订单数据
- $orderData = $this->objDOrder->get(['id'=>$orderId],'id,payAmount,userCenterId,customerId');
- if ($orderData === false){
- return ResultWrapper::fail($this->objDOrder->error(),ErrorCode::$dberror);
- }
- if (empty($orderData)) {
- return ResultWrapper::fail('订单数据为空', ErrorCode::$paramError);
- }
- $this->orderData = $orderData;
- // 查询订单商品数据
- $orderGoods = $this->objDOrderGoods->select(['orderId' => $orderId], 'goodsId,skuId,totalMoney,buyNum,vipDiscount,preferential,goodsName,shopName,unitName,specGroup,goodsImages');
- if ($orderGoods === false) {
- return ResultWrapper::fail($this->objDOrderGoods->error(), ErrorCode::$dberror);
- }
- if (empty($orderGoods)) {
- return ResultWrapper::fail('订单商品数据为空', ErrorCode::$paramError);
- }
- $this->orderGoods = $orderGoods;
- // 判断当前客户是否是分销商
- $isCommission = $customer['isCommission'];//是否是分销商 5->分销商 4->不是分销商
- $retPath = $customer['commissionPath'];//分销商关系
- if ($isCommission == StatusCode::$delete && empty($retPath)) {
- return ResultWrapper::success('不符合分销条件');
- }
- // 查询分销设置数据
- $settingResult = $this->objMCommissionSetting->getSetting(true);
- if (!$settingResult->isSuccess()) {
- return ResultWrapper::fail($settingResult->getData(), $settingResult->getErrorCode());
- }
- $setting = $settingResult->getData();
- if (!isset($setting['mode'])) {
- return ResultWrapper::fail('分销佣金计算方式未设置', ErrorCode::$paramError);
- }
- $this->setting = $setting;
- $isRetSelf = $setting['self']; // 自动返佣
- $this->mode = $setting['mode']; // 佣金计算方式
- $this->level = $setting['level'];//分销等级 三级分销
- $this->isRetSelf = $isRetSelf;
- //组装订单商品数据数据
- $commonData = [
- 'orderId' => $orderId,
- 'orderNo' => $orderIndex['no'],
- 'userCenterId' => $orderIndex['userCenterId'],
- 'customerId' => $customerId
- ];
- foreach ($orderGoods as &$item) {
- $item = array_merge($item, $commonData);
- }
- $selfOrderInsert = [];
- // 如果当前客户是分销商且开启自购返佣
- if ($isCommission == StatusCode::$standard && $isRetSelf == StatusCode::$standard) {
- empty($this->needUserCenterIds) && $this->needUserCenterIds = [];
- $this->needUserCenterIds = array_merge($this->needUserCenterIds, [$orderIndex['userCenterId']]);
- $selfRetCommission = self::selfRetCommission($orderIndex['userCenterId'], $customerId, $orderGoods);
- if (!$selfRetCommission->isSuccess()) {
- return ResultWrapper::fail($selfRetCommission->getData(), $selfRetCommission->getErrorCode());
- }
- $selfOrderInsert = $selfRetCommission->getData();
- }
- // 给上级返佣
- $retOrderInsert = [];
- if (!empty($retPath) && $this->level != 0) {
- // 所有上级的usercenterid集合
- $retPathArr = array_values(json_decode($retPath, true));
- empty($this->needUserCenterIds) && $this->needUserCenterIds = [];
- $this->needUserCenterIds = array_merge($this->needUserCenterIds, $retPathArr);
- $iterRetCommissionResult = self::iterRetCommission($retPathArr, $orderGoods);
- if (!$iterRetCommissionResult->isSuccess()) {
- return ResultWrapper::fail($iterRetCommissionResult->getData(), $iterRetCommissionResult->getErrorCode());
- }
- $retOrderInsert = $iterRetCommissionResult->getData();
- }
- $insert = array_merge($selfOrderInsert, $retOrderInsert);
- if (empty($insert)) {
- return ResultWrapper::success('没有需要记录的数据');
- }
- $updateOrderData = self::updateOrderData($selfOrderInsert,$retOrderInsert);//订单佣金拓展字段
- $commissionOrderIndex = self::commissionIndexData($selfOrderInsert,$retOrderInsert,$orderData['payAmount']);//分销订单索引数据
- $updateCommissionResult = self::updateCommission($insert);//计算分销商的变动数据
- if (!$updateCommissionResult->isSuccess()) {
- return ResultWrapper::fail($updateCommissionResult->getData(), $updateCommissionResult->getErrorCode());
- }
- $updateCommission = $updateCommissionResult->getData();
- $statisticsDataResult = self::saveBusinessmanStatistics($commissionOrderIndex,$orderData['payAmount']);//计算要回写的数据
- if (!$statisticsDataResult->isSuccess()){
- return ResultWrapper::fail($statisticsDataResult->getData(),$statisticsDataResult->getErrorCode());
- }
- $statisticsData = $statisticsDataResult->getData();//要回写的数据
- /***************************************写记录start*************************************************************/
- $this->objDCommissionOrder->beginTransaction();
- foreach ($updateCommission as $key => $value) {
- $update = $this->objDCommissionBusinessman->update([
- 'waitMoney' => $value['waitMoney'],
- 'updateTime' => $value['updateTime']
- ], ['userCenterId' => $value['userCenterId']]);
- if ($update === false) {
- $this->objDCommissionOrder->rollBack();
- return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
- }
- }
- $insert = self::insertSort($insert);
- $result = $this->objDCommissionOrder->insert($insert, true);
- if ($result === false) {
- $this->objDCommissionOrder->rollBack();
- return ResultWrapper::fail($this->objDCommissionOrder->error(), ErrorCode::$dberror);
- }
- //回写订单索引表
- $updateIndex = $this->objDOrderIndex->update(
- [
- 'isCommission'=> StatusCode::$standard,
- 'commissionData' => json_encode($updateOrderData),
- 'updateTime' => time()
- ],
- ['id' => $orderId]
- );
- if ($updateIndex === false){
- $this->objDCommissionOrder->rollBack();
- return ResultWrapper::fail($this->objDOrderIndex->error(),ErrorCode::$dberror);
- }
- //写订单表
- $updateOrder = $this->objDOrder->update([
- 'isCommission'=> StatusCode::$standard,
- 'commissionData' => json_encode($updateOrderData),
- 'updateTime' => time()
- ],['id' => $orderId]);
- if ($updateOrder === false){
- $this->objDCommissionOrder->rollBack();
- return ResultWrapper::fail($this->objDOrder->error(),ErrorCode::$dberror);
- }
- //分销订单索引数据
- $commissionOrderIndex = self::insertSort($commissionOrderIndex);
- $commissionOrderIndex = $this->objDCommissionOrderIndex->insert($commissionOrderIndex,true);
- if ($commissionOrderIndex === false){
- $this->objDCommissionOrder->rollBack();
- return ResultWrapper::fail($this->objDCommissionOrderIndex->error(),ErrorCode::$dberror);
- }
- //回写数据
- foreach ($statisticsData as $userCenterId => $row){
- $outSetStatisticsResult = $this->objMCommissionBusinessman->outSetStatistics($userCenterId,$row);
- if (!$outSetStatisticsResult->isSuccess()){
- $this->objDCommissionOrder->rollBack();
- }
- }
- $this->objDCommissionOrder->commit();
- $this->insert = $insert;
- return ResultWrapper::success("操作成功");
- }
- /**
- * Doc: (des="分销订单主索引表")
- * User: XMing
- * Date: 2020/7/30
- * Time: 11:38 上午
- * @param array $selfOrderInsert
- * @param array $retOrderInsert
- * @param $payAmount
- * @return array
- */
- private function commissionIndexData(array $selfOrderInsert,array $retOrderInsert,$payAmount)
- {
- $commissionIndexMap = [];
- //自购返佣
- if (!empty($selfOrderInsert)){
- //将数据按照订单级别合并
- $retMoney = array_sum(array_column($selfOrderInsert,'retMoney'));
- $commissionIndexMap[] = [
- 'userCenterId' => $selfOrderInsert[0]['userCenterId'],
- 'customerId' => $selfOrderInsert[0]['customerId'],
- 'orderId' => $selfOrderInsert[0]['orderId'],
- 'orderNo' => $selfOrderInsert[0]['orderNo'],
- 'payAmount' => $payAmount,
- 'retUserCenterId' => $selfOrderInsert[0]['retUserCenterId'],
- 'retCustomerId' => $selfOrderInsert[0]['retCustomerId'],
- 'retMoney' => $retMoney,
- 'retLevel' => $selfOrderInsert[0]['retLevel'],
- 'retGrade' => $selfOrderInsert[0]['retGrade']
- ];
- }
- //三级返佣
- if (!empty($retOrderInsert)){
- $oneRetMoney = 0;
- $twoRetMoney = 0;
- $threeRetMoney = 0;
- foreach ($retOrderInsert as $item){
- $common[$item['retLevel']] = [
- 'userCenterId' => $item['userCenterId'],
- 'customerId' => $item['customerId'],
- 'orderId' => $item['orderId'],
- 'orderNo' => $item['orderNo'],
- 'payAmount' => $payAmount,
- 'retUserCenterId' => $item['retUserCenterId'],
- 'retCustomerId' => $item['retCustomerId'],
- 'retLevel' => $item['retLevel'],
- 'retGrade' => $item['retGrade']
- ];
- switch ($item['retLevel']){
- case self::$levelMap['oneRate']:
- $oneRetMoney = bcadd($oneRetMoney,$item['retMoney'],2);
- break;
- case self::$levelMap['twoRate']:
- $twoRetMoney = bcadd($twoRetMoney,$item['retMoney'],2);
- break;
- case self::$levelMap['threeRate']:
- $threeRetMoney = bcadd($threeRetMoney,$item['retMoney'],2);
- break;
- }
- }
- //一级
- if ($oneRetMoney != 0){
- $common[self::$levelMap['oneRate']]['retMoney'] = $oneRetMoney;
- $commissionIndexMap[] = $common[self::$levelMap['oneRate']];
- }
- //二级
- if($twoRetMoney != 0){
- $common[self::$levelMap['twoRate']]['retMoney'] = $twoRetMoney;
- $commissionIndexMap[] = $common[self::$levelMap['twoRate']];
- }
- //三级
- if ($threeRetMoney != 0){
- $common[self::$levelMap['threeRate']]['retMoney'] = $threeRetMoney;
- $commissionIndexMap[] = $common[self::$levelMap['threeRate']];
- }
- }
- Logger::logs(E_USER_ERROR,'分销订单索引数据',__CLASS__,__LINE__,$commissionIndexMap);
- return $commissionIndexMap;
- }
- /**
- * Doc: (des="对插入的二维数组进行排序")
- * User: XMing
- * Date: 2020/7/30
- * Time: 2:42 下午
- * @param array $data
- * @return array
- */
- private function insertSort(array $data)
- {
- foreach ($data as &$value){
- ksort($value);
- }
- return $data;
- }
- /**
- * Doc: (des="回写订单的分销状态,记录订单分销数据json")
- * User: XMing
- * Date: 2020/7/29
- * Time: 6:57 下午
- * @param array $selfOrderInsert
- * @param array $retOrderInsert
- * @example
- * isCommission 是否是分销订单 5是 4否
- * @return array
- *
- */
- private function updateOrderData(array $selfOrderInsert,array $retOrderInsert)
- {
- $commissionMap=[];
- //自购返佣
- if (!empty($selfOrderInsert)){
- $retMoney = array_sum(array_column($selfOrderInsert,'retMoney'));
- $commissionMap['self'] = [
- 'retLevel' => 1,
- 'retMoney' => $retMoney,
- 'retCustomerId' => $selfOrderInsert[0]['customerId'],
- 'retUserCenterId' => $selfOrderInsert[0]['userCenterId'],
- ];
- }
- //三级返佣
- if(!empty($retOrderInsert)){
- $oneRetMoney = 0;
- $twoRetMoney = 0;
- $threeRetMoney = 0;
- foreach ($retOrderInsert as $item){
- switch ($item['retLevel']){
- case self::$levelMap['oneRate']:
- $oneRetMoney = bcadd($oneRetMoney,$item['retMoney'],2);
- //一级返佣
- $commissionMap['oneRet'] = [
- 'retLevel' => self::$levelMap['oneRate'],
- 'retMoney' => $oneRetMoney,
- 'retCustomerId' => $item['retCustomerId'],
- 'retUserCenterId' => $item['retUserCenterId']
- ];
- break;
- case self::$levelMap['twoRate']:
- //二级
- $twoRetMoney = bcadd($twoRetMoney,$item['retMoney'],2);
- $commissionMap['twoRet'] = [
- 'retLevel' => self::$levelMap['twoRate'],
- 'retMoney' => $twoRetMoney,
- 'retCustomerId' => $item['retCustomerId'],
- 'retUserCenterId' => $item['retUserCenterId']
- ];
- break;
- case self::$levelMap['threeRate']:
- //三级
- $threeRetMoney = bcadd($threeRetMoney,$item['retMoney'],2);
- $commissionMap['threeRet'] = [
- 'retLevel' => self::$levelMap['threeRate'],
- 'retMoney' => $threeRetMoney,
- 'retCustomerId' => $item['retCustomerId'],
- 'retUserCenterId' => $item['retUserCenterId']
- ];
- break;
- }
- }
- }
- return $commissionMap;
- }
- /**
- * Doc: (des="计算分销商的更新数据")
- * User: XMing
- * Date: 2020/7/24
- * Time: 10:21 上午
- * @param array $data
- * @return ResultWrapper
- */
- private function updateCommission(array $data)
- {
- //查询分销商记录
- $allRetUserCenterIds = [];
- $changeMap = [];
- foreach ($data as $item) {
- $allRetUserCenterIds[] = $item['retUserCenterId'];
- $changeMap[$item['retUserCenterId']][] = $item['retMoney'];
- }
- $businessmanResult = self::getCommissionBusinessman($allRetUserCenterIds);
- if (!$businessmanResult->isSuccess()){
- return ResultWrapper::fail($businessmanResult->getData(),$businessmanResult->getErrorCode());
- }
- $businessman = $businessmanResult->getData();
- $update = [];
- foreach ($businessman as $row) {
- $oldWaitMoney = $row['waitMoney'];//原金额
- //获取当前用户变动金额
- $changeMoney = array_sum($changeMap[$row['userCenterId']]);//变动金额
- $newWaitMoney = bcadd($oldWaitMoney, $changeMoney, 2);//变动后金额
- $update[] = [
- 'userCenterId' => $row['userCenterId'],
- 'waitMoney' => $newWaitMoney,
- 'updateTime' => time(),//变动时间
- ];
- }
- if (!empty($update)){
- Logger::logs(E_USER_ERROR,'分销商更新数据',__CLASS__,__LINE__,$update);
- }
- return ResultWrapper::success($update);
- }
- /**
- * Doc: (des="获取分销商的信息集合")
- * User: XMing
- * Date: 2020/7/31
- * Time: 11:30 上午
- * @param array $allRetUserCenterIds
- * @param string $fields
- * @return ResultWrapper
- *
- */
- private function getCommissionBusinessman(array $allRetUserCenterIds,string $fields='id,userCenterId,balance,totalMoney,waitMoney')
- {
- $businessman = $this->objDCommissionBusinessman->select(['userCenterId' => $allRetUserCenterIds], $fields);
- if ($businessman === false) {
- return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($businessman);
- }
- /**
- * Doc: (des="分销数据的回写")
- * User: XMing
- * Date: 2020/7/28
- * Time: 3:23 下午
- * @param array $data 分销返佣的数据
- * @param float $payAmount
- * @example
- * @return ResultWrapper
- */
- private function saveBusinessmanStatistics(array $data,float $payAmount)
- {
- //demo
- /**$params = [
- 'balance' => 100.00, //余额
- 'waitMoney' => 100.00, //待入账金额
- 'withdraw' => 100.00, //已提现金额
- 'statistics' => [
- 'orderMoney' => 100.00, //分销订单总额
- 'orderNum' => 100, //分销订单总数
- 'oneOrderMoney' => 100.00, //一级分销订单总额
- 'oneOrderNum' => 100, //一级分销订单总数
- 'selfOrderMoney' => 100.00, //自购订单总额
- 'selfOrderNum' => 100, //自购订单总数
- 'sub' => 100, //下线人数
- 'subBusinessman' => 100, //下线分销商人数
- 'oneSub' => 100, //一级下线人数
- 'oneSubBusinessman' => 100, //一级下线分销商
- 'totalMoney' => 100.00, //累计佣金
- ],
- ];*/
- $allRetUserCenterIds = [];
- $changeMap = [];
- foreach ($data as $item) {
- $allRetUserCenterIds[] = $item['retUserCenterId'];
- $changeMap[$item['retUserCenterId']][] = $item['retMoney'];
- }
- $businessmanResult = self::getCommissionBusinessman($allRetUserCenterIds);
- if (!$businessmanResult->isSuccess()){
- return ResultWrapper::fail($businessmanResult->getData(),$businessmanResult->getErrorCode());
- }
- $businessman = $businessmanResult->getData();
- $businessmanMap = [];
- foreach ($businessman as $value){
- $businessmanMap[$value['userCenterId']] = $value;
- }
- $arrayMap = [];
- foreach ($data as $key => $item){
- if (!isset($businessmanMap[$item['retUserCenterId']])){
- return ResultWrapper::fail('分销商数据映射失败',ErrorCode::$paramError);
- }
- $oldTotalMoney = $businessmanMap[$item['retUserCenterId']]['totalMoney'];
- $arrayMap[$item['retUserCenterId']] = [
- 'totalMoney' => bcadd($oldTotalMoney,$item['retMoney']),
- 'statistics' => [
- 'orderMoney' => $payAmount,
- 'orderNum' => 1,
- ],
- ];//公共数据
- switch ($key){
- case 'self':
- //自购
- $arrayMap[$item['retUserCenterId']]['statistics']['selfOrderMoney'] = $payAmount;
- $arrayMap[$item['retUserCenterId']]['statistics']['selfOrderNum'] = 1;
- break;
- case 'oneRet':
- //一级
- $arrayMap[$item['retUserCenterId']]['statistics']['oneOrderMoney'] = $payAmount;
- $arrayMap[$item['retUserCenterId']]['statistics']['oneOrderNum'] = 1;
- break;
- case 'twoRet':
- //二级
- break;
- case 'threeRet':
- //三级
- break;
- }
- }
- if (!empty($arrayMap)){
- Logger::logs(E_USER_ERROR,'回写数据',__CLASS__,__LINE__,$arrayMap);
- }
- return ResultWrapper::success($arrayMap);
- }
- /**
- * 上级返佣方法
- * @param array $retPathArr 所有上级的usercenterIds
- * @param array $orderGoodsData 订单商品数据
- * @return ResultWrapper
- */
- private function iterRetCommission(array $retPathArr, array $orderGoodsData)
- {
- $retGoodsResult = self::getGoodsRule($orderGoodsData);
- if (!$retGoodsResult->isSuccess()) {
- return ResultWrapper::fail($retGoodsResult->getData(), $retGoodsResult->getErrorCode());
- }
- $retGoods = $retGoodsResult->getData();
- $defaultGoods = $retGoods['defaultGoods']; // 默认返佣规则商品
- $defineGoods = $retGoods['defineGoods']; // 自定义佣金规则商品
- // 获取分销商等级和等级对应的佣金规则
- $gradeResult = self::getGradeRuleBySelect($retPathArr);
- if (!$gradeResult->isSuccess()) {
- return ResultWrapper::fail($gradeResult->getData(), $gradeResult->getErrorCode());
- }
- $gradeRule = $gradeResult->getData();//分销商等级佣金比例规则
- //$retLevel 当前被返佣分销商所处级别1-3
- //$retUserCenterId 当前被返佣分销商的用户id
- $defaultInsert = [];
- $defineInsert = [];
- foreach ($retPathArr as $index => $retUserCenterId) {
- // 如果开启自购返佣,上一级从第二集开始返佣
- if($this->isRetSelf == StatusCode::$standard){
- $retLevel = (int)$index + 2;
- }else{
- $retLevel = (int)$index + 1;
- }
- // 如果当前分销层级大于后台设置的分销层级则不计算佣金
- if ($retLevel > $this->level) {
- continue;
- }
- // 当前路径中的用户id不是分销商,不进行返佣,进入下一级别
- if (!isset($gradeRule[$retUserCenterId])) {
- continue;
- }
- $grade = $gradeRule[$retUserCenterId]['grade'];//被返佣用户等级1-10
- $retCustomerId = $gradeRule[$retUserCenterId]['customerId'];//被返佣客户id
- //1.处理默认分销商品
- if (!empty($defaultGoods)) {
- $rate = $gradeRule[$retUserCenterId][self::$levelMap[$retLevel]];//当前等级当前等级比例
- if (empty($rate)) {
- //比率为0
- continue;
- }
- $rate = bcdiv($rate, 100);
- //生成数据
- $defaultResult = self::createDefaultOrder($defaultGoods, $rate, $grade, $retUserCenterId, $retCustomerId, $retLevel, StatusCode::$delete);
- $defaultInsert = array_merge($defaultInsert, $defaultResult);//合并每次的结果
- }
- //2.处理自定义分销商品
- if (!empty($defineGoods)) {
- $defineResult = self::createDefineOrder($defineGoods, $grade, $retUserCenterId, $retCustomerId, $retLevel, StatusCode::$delete);
- $defineInsert = array_merge($defineInsert, $defineResult);
- }
- }
- $return = array_merge($defaultInsert, $defineInsert);//合并最终数据
- return ResultWrapper::success($return);
- }
- /**
- * @param array $data 订单商品数据
- * @param int $userCenterId 自购分销商用户id
- * @param int $customerId 自购分销商客户id
- * @return ResultWrapper 返回数据为自购返佣记录数据
- */
- private function selfRetCommission(int $userCenterId, int $customerId, array $orderGoodsData)
- {
- $retGoodsResult = self::getGoodsRule($orderGoodsData);
- if (!$retGoodsResult->isSuccess()) {
- return ResultWrapper::fail($retGoodsResult->getData(), $retGoodsResult->getErrorCode());
- }
- $retGoodsResult = $retGoodsResult->getData();
- $defaultGoods = $retGoodsResult['defaultGoods']; // 默认佣金规则的商品
- $defineGoods = $retGoodsResult['defineGoods']; // 单独设置佣金规则的商品
- // 获取分销商等级佣金规则
- $gradeResult = self::getGradeRuleBySelect([$userCenterId]);
- if (!$gradeResult->isSuccess()) {
- return ResultWrapper::fail($gradeResult->getData(), $gradeResult->getErrorCode());
- }
- $gradeRule = $gradeResult->getData();
- $selfGradeRule = isset($gradeRule[$userCenterId]) ? $gradeRule[$userCenterId] : [];//分销商的等级佣金规则
- if (empty($selfGradeRule)) {
- return ResultWrapper::fail('获取分销等级规则为空', ErrorCode::$paramError);
- }
- $grade = $selfGradeRule['grade'];//自购分销商等级0-10
- //默认分销佣金商品
- $defaultInsert = [];
- if (!empty($defaultGoods)) {
- //自购分销商的等级佣金比率
- $rate = $this->isRetSelf == StatusCode::$delete ? 0 : bcdiv($selfGradeRule['oneRate'], 100);//自购使用一级分销商的比率,将比率换算成浮点数
- $defaultInsert = self::createDefaultOrder($defaultGoods, $rate, $grade, $userCenterId, $customerId, 1, StatusCode::$standard);//自购返佣数据
- }
- // 单独设置佣金规则的商品
- $defineInsert = [];
- if (!empty($defineGoods)) {
- $defineInsert = self::createDefineOrder($defineGoods, $grade, $userCenterId, $customerId, 1, StatusCode::$standard);
- }
- $insert = array_merge($defaultInsert, $defineInsert);
- return ResultWrapper::success($insert);
- }
- /**
- * 计算单独设置佣金规则商品
- * @param array $data 单独设置佣金规则的商品数据
- * @param int $grade 被返佣分销商的等级1-10
- * @param int $retUserCenterId 被返佣分销商用户id
- * @param int $retCustomerId 被返佣分销商客户id
- * @param int $retLevel 被返佣分销商的级别1-3
- * @param int $isSelf 是否自购返佣 5是 4否
- *
- * @return array
- */
- private function createDefineOrder(array $data, int $grade, int $retUserCenterId, int $retCustomerId, int $retLevel, int $isSelf)
- {
- $insert = [];
- foreach ($data as $item) {
- $amount = 0;
- switch ($this->mode) {
- case StatusCode::$standard: // 实际支付金额
- $amount = bcsub($item['totalMoney'], bcadd($item['vipDiscount'], $item['preferential']), 2);
- break;
- case StatusCode::$delete: // 商品折扣价
- $amount = bcsub($item['totalMoney'], $item['preferential'],2);
- break;
- }
- $oneRate = isset($item['rule'][$grade][self::$levelMap[$retLevel]]) ? $item['rule'][$grade][self::$levelMap[$retLevel]] : 0;//自购一级佣金比例
- if (empty($oneRate)) {
- continue;
- }
- $retRate = 0; // 佣金计算百分比
- if($item['retType'] == StatusCode::$standard){ // 返佣方式百分比
- $oneRate = bcdiv($oneRate, 100);
- $retMoney = bcmul($amount, $oneRate, 2);
- $retRate = bcmul($oneRate, 100);
- }else{ // 返佣方式固定金额
- $retMoney = bcmul($oneRate, $item['buyNum'], 2);
- }
- //是自购订单但是未开启自购
- if ($isSelf == StatusCode::$standard && $this->isRetSelf == StatusCode::$delete){
- $retMoney = 0;
- }
- $insert[] = [
- 'userCenterId' => $item['userCenterId'],
- 'customerId' => $item['customerId'],
- 'orderId' => $item['orderId'],
- 'orderNo' => $item['orderNo'],
- 'goodsId' => $item['goodsId'],
- 'skuId' => $item['skuId'],
- 'goodsPrice' => $item['totalMoney'],
- 'retUserCenterId' => $retUserCenterId,
- 'retCustomerId' => $retCustomerId,
- 'retMoney' => $retMoney,
- 'retLevel' => $retLevel, //级别1-3
- 'retGrade' => $grade, //等级1-10,
- 'retRate' => $retRate,
- 'buyNum' => $item['buyNum'],
- 'goodsName' => $item['goodsName'],
- 'shopName' => $item['shopName'],
- 'unitName' => $item['unitName'],
- 'specGroup' => $item['specGroup'],
- 'goodsImages' => $item['goodsImages'],
- 'isSelf' => $isSelf
- ];
- }
- return $insert;
- }
- /**
- * Doc: (des="生成默认佣金流水记录数据")
- * User: XMing
- * Date: 2020/7/23
- * Time: 4:45 下午
- * @param array $data 分销商品数据
- * @param string $rate 被返佣分销商所处等级级别比率
- * @param int $grade 被返佣分销商的等级1-10
- * @param int $retUserCenterId 被返佣分销商用户id
- * @param int $retCustomerId 被返佣分销商客户id
- * @param int $retLevel 被返佣人所处级别1-3
- * @param int $isSelf 是否自购返佣 5是 4否
- * @return array
- */
- private function createDefaultOrder(array $data, string $rate, int $grade, int $retUserCenterId, int $retCustomerId, int $retLevel, int $isSelf)
- {
- $insert = [];
- foreach ($data as $item) {
- $amount = 0;
- switch ($this->mode) {
- case StatusCode::$standard:
- //实际支付金额
- $amount = bcsub($item['totalMoney'], bcadd($item['vipDiscount'], $item['preferential']), 2);
- break;
- case StatusCode::$delete:
- $amount = bcsub($item['totalMoney'], $item['preferential'],2);
- break;
- }
- $retMoney = bcmul($amount, $rate, 2);
- $insert[] = [
- 'userCenterId' => $item['userCenterId'],
- 'customerId' => $item['customerId'],
- 'orderId' => $item['orderId'],
- 'orderNo' => $item['orderNo'],
- 'goodsId' => $item['goodsId'],
- 'skuId' => $item['skuId'],
- 'goodsPrice' => $item['totalMoney'],
- 'retUserCenterId' => $retUserCenterId,
- 'retCustomerId' => $retCustomerId,
- 'retMoney' => $retMoney,
- 'retLevel' => $retLevel,//级别1-3
- 'retGrade' => $grade,//等级1-10,
- 'retRate' => bcmul($rate, 100),
- 'buyNum' => $item['buyNum'],
- 'goodsName' => $item['goodsName'],//
- 'shopName' => $item['shopName'],//
- 'unitName' => $item['unitName'],//
- 'specGroup' => $item['specGroup'],//
- 'goodsImages' => $item['goodsImages'],//
- 'isSelf' => $isSelf
- ];
- }
- return $insert;
- }
- /**
- * 批量获取指定用户的分销商等级
- * @param array $params 分销商的userCenterIds
- * @return ResultWrapper
- */
- private function getGradeRuleBySelect(array $params)
- {
- $commissionBusinessmanResult = $this->objMCommissionBusinessman->getBusinessmanGrade($params);
- if (!$commissionBusinessmanResult->isSuccess()) {
- return ResultWrapper::fail($commissionBusinessmanResult->getData(), $commissionBusinessmanResult->getErrorCode());
- }
- return ResultWrapper::success($commissionBusinessmanResult->getData());
- }
- /**
- * 处理单独设置佣金的商品
- * @param array $orderGoods 订单商品
- * @return ResultWrapper
- */
- private function getGoodsRule(array $orderGoods)
- {
- // 提取所有订单商品id并映射
- $goodsIds = [];
- $orderGoodsMap = [];
- foreach ($orderGoods as $goods) {
- $goodsIds[] = $goods['goodsId'];
- $orderGoodsMap[$goods['goodsId']] = $goods;
- }
- // 获取商品是否参与分销是否单独有设置比率
- $goodsData = $this->objDGoods->select(['id' => $goodsIds], 'id,isJoinCommission,isDefine,retType');
- if ($goodsData === false) {
- return ResultWrapper::fail($this->objDGoods->error(), ErrorCode::$dberror);
- }
- if (empty($goodsData)) {
- return ResultWrapper::fail('获取商品分销设置失败', ErrorCode::$paramError);
- }
- // 过滤不参加分销的,并且筛选自定义佣金比例商品
- $defineGoods = []; // 自定义佣金商品集合
- $deleteGoods = []; // 不参与分销的商品集合
- $defaultGoods = []; // 默认佣金商品集合
- foreach ($goodsData as $key => $datum) {
- $orderGoodsMap[$datum['id']]['retType'] = $datum['retType'];
- // 未参与分销商品
- if ($datum['isJoinCommission'] == StatusCode::$delete) {
- $deleteGoods[] = array_merge($deleteGoods, $orderGoodsMap[$datum['id']]);
- unset($orderGoodsMap[$datum['id']]);
- continue;
- }
- // 自定义佣金商品
- if ($datum['isDefine'] == StatusCode::$standard) {
- $defineGoods[] = array_merge($defineGoods, $orderGoodsMap[$datum['id']]);
- unset($orderGoodsMap[$datum['id']]);
- continue;
- }
- //剩下的都是默认的佣金配置
- $defaultGoods[] = array_merge($defaultGoods, $orderGoodsMap[$datum['id']]);
- }
- // 自定义佣金商品不为空
- if (!empty($defineGoods)) {
- $skuIds = [];
- foreach ($defineGoods as $item) {
- $skuIds[] = $item['skuId'];
- }
- // 查询单独设置佣金的商品设置内容
- $commissionGoods = $this->objDCommissionGoods->select(['skuId' => $skuIds, 'deleteStatus' => StatusCode::$standard], 'commissionRule,goodsId,skuId');
- if ($commissionGoods === false) {
- return ResultWrapper::fail($this->objDCommissionGoods->error(), ErrorCode::$dberror);
- }
- if (empty($commissionGoods)) {
- return ResultWrapper::fail('获取自定义商品佣金比例为空', ErrorCode::$paramError);
- }
- $commissionGoodsMap = [];
- foreach ($commissionGoods as $item) {
- $commissionGoodsMap[$item['skuId']] = empty($item['commissionRule']) ? [] : json_decode($item['commissionRule'], true);
- }
- foreach ($defineGoods as $key => $item) {
- $defineGoods[$key]['rule'] = isset($commissionGoodsMap[$item['skuId']]) ? $commissionGoodsMap[$item['skuId']] : [];
- }
- }
- $return = [
- 'defaultGoods' => $defaultGoods,
- 'defineGoods' => $defineGoods
- ];
- return ResultWrapper::success($return);
- }
- /**
- * Doc: (des="检测是否达到升级的条件")
- * User: XMing
- * Date: 2020/7/25
- * Time: 9:49 上午
- */
- public function upgrade()
- {
- //test-demo
- /*$this->needUserCenterIds = [
- 168, 169, 140
- ];*/
- //获取分销商的等级
- //需要检测是否升级的用户id
- if (empty($this->needUserCenterIds)) {
- return ResultWrapper::success('没有需要升级的分销商');
- }
- //获取所有等级
- $gradeConditionResult = $this->objMCommissionGrade->getAllGradeCondition(['type' => StatusCode::$delete]);
- if (!$gradeConditionResult->isSuccess()){
- return ResultWrapper::fail($gradeConditionResult->getData(),$gradeConditionResult->getErrorCode());
- }
- $gradeCondition = $gradeConditionResult->getData();
- if (empty($gradeCondition)){
- return ResultWrapper::fail('等级条件未配置',ErrorCode::$paramError);
- }
- $this->allAliasCondition = array_column($gradeCondition,'key');
- //获取这批用户的当前等级
- $commissionBusinessmanResult = $this->objMCommissionBusinessman->getBusinessmanGrade($this->needUserCenterIds);
- if (!$commissionBusinessmanResult->isSuccess()) {
- return ResultWrapper::fail($commissionBusinessmanResult->getData(), $commissionBusinessmanResult->getErrorCode());
- }
- $commissionBusinessman = $commissionBusinessmanResult->getData();
- //查询等级升级条件
- $gradeUpgradeConditionAllResult = $this->objMCommissionGrade->getAllGrade();
- if (!$gradeUpgradeConditionAllResult->isSuccess()) {
- return ResultWrapper::fail($gradeUpgradeConditionAllResult->getData(), $gradeUpgradeConditionAllResult->getErrorCode());
- }
- $gradeUpgradeConditionAll = $gradeUpgradeConditionAllResult->getData();
- $gradeUpgradeConditionMap = [];//等级对应升级条件
- foreach ($gradeUpgradeConditionAll as $item) {
- $gradeUpgradeConditionMap[$item['grade']] = $item;
- }
- $allUserCenterIds = [];
- foreach ($commissionBusinessman as $userCenterId => $value) {
- $nextGrade = (int)$value['grade'] + 1;
- $commissionBusinessman[$userCenterId]['nextCondition'] = [];
- if (!isset($gradeUpgradeConditionMap[$nextGrade])) {
- unset($commissionBusinessman[$userCenterId]);
- continue;
- }
- $commissionBusinessman[$userCenterId]['nextCondition'] = $gradeUpgradeConditionMap[$nextGrade]['upgradeConditionInfo'];
- $commissionBusinessman[$userCenterId]['nextGrade'] = $nextGrade;
- $commissionBusinessman[$userCenterId]['upgradeMode'] = $gradeUpgradeConditionMap[$nextGrade]['upgradeMode'];//是否需要满足所有条件 5是 4否
- $commissionBusinessman[$userCenterId]['nextGradeId'] = $gradeUpgradeConditionMap[$nextGrade]['id'];//是否需要满足所有条件 5是 4否
- $allUserCenterIds[] = $userCenterId;
- }
- if (empty($commissionBusinessman)) {
- return ResultWrapper::success('没有需要升级的分销商');
- }
- //获取分销商的统计数据
- $statisticsResult = self::statisticsData($allUserCenterIds);
- if (!$statisticsResult->isSuccess()) {
- return ResultWrapper::fail($statisticsResult->getData(), $statisticsResult->getErrorCode());
- }
- $statisticsData = $statisticsResult->getData();
- if (empty($statisticsData)){
- return ResultWrapper::fail('获取分销商统计数据为空',ErrorCode::$paramError);
- }
- $upgradeData = [];//需要升级的分销商数据
- foreach ($commissionBusinessman as $userCenterId => $item){
- //获取分销商统计数据
- if (!isset($statisticsData[$userCenterId])){
- continue;
- }
- $commissionData = $statisticsData[$userCenterId];
- $upgradeBool = self::checkCommissionConditionGrad($item['nextCondition'],$commissionData,$item['upgradeMode']);
- if ($upgradeBool == false){
- //不满足条件
- continue;
- }
- //升级
- $upgradeData[] = [
- 'id' => $item['businessmanId'],
- 'userCenterId' => $item['userCenterId'],
- 'grade' => $item['nextGrade'],
- 'gradeId' => $item['nextGradeId'],
- 'updateTime' => time()
- ];
- }
- //操作数据库
- if(empty($upgradeData)){
- return ResultWrapper::success('没有需要升级的分销商');
- }
- $this->objDCommissionBusinessman->beginTransaction();
- foreach ($upgradeData as $datum){
- $update = $this->objDCommissionBusinessman->update([
- 'grade' => $datum['grade'],
- 'gradeId' => $datum['gradeId'],
- 'updateTime' => $datum['updateTime']
- ],[ 'userCenterId' => $datum['userCenterId']]);
- if ($update === false){
- $this->objDCommissionBusinessman->rollBack();
- return ResultWrapper::fail($this->objDCommissionBusinessman->error(),ErrorCode::$dberror);
- }
- }
- $this->objDCommissionBusinessman->commit();
- Logger::logs(E_USER_ERROR,'升级的数据',__CLASS__,__LINE__,$upgradeData);
- return ResultWrapper::success('操作成功');
- }
- /**
- * Doc: (des="判断分销商是否满足对应条件")
- * User: XMing
- * Date: 2020/7/25
- * Time: 3:37 下午
- * @param array $nextCondition 升到下一级所需要的条件
- * @param array $commissionData 分销商的统计数据
- * @param int $upgradeMode 是否需要满足所有条件 4:否 5:是
- * @return boolean
- * 升级 => true
- */
- private function checkCommissionConditionGrad(array $nextCondition,array $commissionData,int $upgradeMode)
- {
- $num = count($nextCondition);
- $meet = 0;//满足条件的数目
- foreach ($nextCondition as $item){
- $data = isset($commissionData[$item['key']]) ? $commissionData[$item['key']] : 0;
- if ($data >= $item['value']){
- //满足条件
- if ($upgradeMode == StatusCode::$delete){
- //满足任意条件即可
- return true;
- }
- $meet = $meet + 1;
- }
- }
- //不符合升级条件
- if ($meet == 0){
- return false;
- }
- //满足 4任意条件 / 5全部条件
- switch ($upgradeMode){
- case StatusCode::$delete:
- //满足一个条件即可
- if ($meet > 0) return true;
- break;
- case StatusCode::$standard:
- if ($meet == $num) return true;
- break;
- }
- return false;
- }
- /**
- * Doc: (des="统计分销商的数据")
- * User: XMing
- * Date: 2020/7/25
- * Time: 3:30 下午
- *
- * orderMoney 分销订单总额
- * orderNum 分销订单总数
- * oneOrderMoney 一级分销订单总额
- * oneOrderNum 一级分销订单总数
- * selfOrderMoney 自购订单总额
- * selfOrderNum 自购订单总数
- * sub 下线人数
- * subBusinessman 下线分销商人数
- * oneSub 一级下线人数
- * oneSubBusinessman 一级下线分销商人数
- * withdraw 已提现金额
- * buyCard 购买会员卡
- *
- * @param array $userCenterIds
- * @return ResultWrapper
- */
- private function statisticsData(array $userCenterIds)
- {
- //demo
- /**$map = [
- 168 => [
- 'orderMoney' => 100,
- 'orderNum' => 1,
- 'oneOrderMoney' => 100,
- 'oneOrderNum' => 1,
- 'selfOrderMoney' => 100,
- 'selfOrderNum' => 1,
- 'sub' => 10,
- 'subBusinessman' => 1,
- 'oneSub' => 10,
- 'oneSubBusinessman' => 1,
- 'withdraw' => 100,
- 'buyCard' => true
- ]
- ];*/
- $businessmanStatisticsResult = $this->objMCommissionBusinessman->getBusinessmanStatistics($userCenterIds);
- if (!$businessmanStatisticsResult->isSuccess()){
- return ResultWrapper::fail($businessmanStatisticsResult->getData(),$businessmanStatisticsResult->getErrorCode());
- }
- //统计分销商的订单总辆
- return ResultWrapper::success($businessmanStatisticsResult->getData());
- }
- /**
- * Doc: (des="增加分销商品的销量佣金")
- * User: XMing
- * Date: 2020/7/28
- * Time: 9:15 上午
- * @param array $data
- * @return ResultWrapper
- */
- private function incrCommissionGoods(array $data)
- {
- $allSkuIds = [];
- foreach ($data as $value){
- $allSkuIds[] = $value['skuId'];
- }
- //获取这批数据记录
- $oldCommissionGoodsStatistics = $this->objDCommissionGoodsStatistics->select(['skuId'=>$allSkuIds]);
- if ($oldCommissionGoodsStatistics === false){
- return ResultWrapper::fail($this->objDCommissionGoodsStatistics->error(),ErrorCode::$dberror);
- }
- //给对应的商品增加销量以及商品佣金
- $commissionGoodsMap = [];
- foreach ($oldCommissionGoodsStatistics as $item){
- $commissionGoodsMap[$item['skuId']] = $item;
- }
- $replace = [];
- foreach ($data as $datum){
- $oldSalesNum = isset($commissionGoodsMap[$datum['skuId']]['salesNum']) ? $commissionGoodsMap[$datum['skuId']]['salesNum'] : 0;
- $oldCommission = isset($commissionGoodsMap[$datum['skuId']]['commission']) ? $commissionGoodsMap[$datum['skuId']]['commission'] : 0;
- $replace[] = [
- 'goodsId' => $datum['goodsId'],
- 'skuId' => $datum['skuId'],
- 'salesNum' => bcadd($oldSalesNum,$datum['buyNum']),
- 'commission' => bcadd($oldCommission,$datum['retMoney']),
- 'createTime' => time()
- ];
- }
- if (empty($replace)){
- return ResultWrapper::success('没有数据需要更新');
- }
- $replace = $this->objDCommissionGoodsStatistics->replace($replace,true);
- if ($replace === false){
- return ResultWrapper::fail($this->objDCommissionGoodsStatistics->error(),ErrorCode::$dberror);
- }
- return ResultWrapper::success('操作成功');
- }
- public function updateBalanceNew(int $orderId){
-
- }
- /**
- * Doc: (des="订单完成将等待入账转移到余额")
- * User: XMing
- * Date: 2020/7/30
- * Time: 5:15 下午
- * @param int $orderId
- * @return ResultWrapper
- */
- public function updateBalance(int $orderId)
- {
- $commissionDataResult = self::getOrderIndexData($orderId);
- if (!$commissionDataResult->isSuccess()){
- return ResultWrapper::fail($commissionDataResult->getData(),$commissionDataResult->getErrorCode());
- }
- $commissionData = $commissionDataResult->getData();
- $selfCommissionData = isset($commissionData['self']) ? $commissionData['self'] : [];
- $oneCommissionData = isset($commissionData['oneRet']) ? $commissionData['oneRet'] : [];
- $twoCommissionData = isset($commissionData['twoRet']) ? $commissionData['twoRet'] : [];
- $threeCommissionData = isset($commissionData['threeRet']) ? $commissionData['threeRet'] : [];
- $allUserCenterIds = array_column($commissionData,'retUserCenterId');
- //获取分销商的余额
- $businessman = $this->objDCommissionBusinessman->select(['userCenterId' => $allUserCenterIds], 'id,userCenterId,customerId,balance,waitMoney');
- if ($businessman === false) {
- return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
- }
- if(empty($businessman)){
- return ResultWrapper::fail('获取分销商数据为空',ErrorCode::$paramError);
- }
- $businessmanMap = [];
- foreach ($businessman as $value){
- $businessmanMap[$value['userCenterId']] = $value;
- }
- $updateDataResult = self::buildUpdateData($selfCommissionData,$oneCommissionData,$twoCommissionData,$threeCommissionData,$businessmanMap);
- if (!$updateDataResult->isSuccess()){
- return ResultWrapper::fail($updateDataResult->getData(),$updateDataResult->getErrorCode());
- }
- $updateData = $updateDataResult->getData();
- if (empty($updateData)){
- return ResultWrapper::fail('组装分销商更新数据失败',ErrorCode::$paramError);
- }
- $this->objDCommissionBusinessman->beginTransaction();
- foreach ($updateData as $value){
- //分销商账户表
- $update = $this->objDCommissionBusinessman->update(
- [
- 'balance' => $value['balance'],
- 'waitMoney' => $value['waitMoney'],
- 'updateTime' => time(),
- ],
- [
- 'userCenterId'=> $value['userCenterId']
- ]
- );
- if ($update === false){
- $this->objDCommissionBusinessman->rollBack();
- return ResultWrapper::fail($this->objDCommissionBusinessman->error(),ErrorCode::$dberror);
- }
- //资金变动记录表
- $logResult = self::changeLog($value, $value['changeMoney'],$value['balance'], StatusCode::$standard, '分佣');
- if (!$logResult->isSuccess()){
- $this->objDCommissionBusinessman->rollBack();
- return ResultWrapper::fail($logResult->getData(),$logResult->getErrorCode());
- }
- }
- //更新这批分销订单
- $updateCommissionOrderIndex = $this->objDCommissionOrderIndex->update(
- [
- 'isAccount' => StatusCode::$standard,
- 'updateTime'=>time()
- ],
- [
- 'orderId' => $orderId
- ]
- );
- if ($updateCommissionOrderIndex === false){
- $this->objDCommissionBusinessman->rollBack();
- }
- $this->objDCommissionBusinessman->commit();
- return ResultWrapper::success('更新分销商佣金成功');
- }
- /**
- * Doc: (des="构建分销商更新数据")
- * User: XMing
- * Date: 2020/7/30
- * Time: 6:09 下午
- * @param array $selfCommissionData
- * @param array $oneCommissionData
- * @param array $twoCommissionData
- * @param array $threeCommissionData
- * @param array $businessmanMap
- * @return ResultWrapper
- */
- private function buildUpdateData(array $selfCommissionData,array $oneCommissionData,array $twoCommissionData,array $threeCommissionData,array $businessmanMap)
- {
- $updateMap = [];
- if (!empty($selfCommissionData)){
- //自购返佣
- //原等待入账
- if (!isset($businessmanMap[$selfCommissionData['retUserCenterId']])){
- return ResultWrapper::fail('获取分销商数据失败',ErrorCode::$paramError);
- }
- $oldWaitMoney = $businessmanMap[$selfCommissionData['retUserCenterId']]['waitMoney'];
- $oldBalance = $businessmanMap[$selfCommissionData['retUserCenterId']]['balance'];
- $changeMoney = $selfCommissionData['retMoney'];
- $updateMap[] = [
- 'userCenterId' => $selfCommissionData['retUserCenterId'],
- 'oldBalance' => $oldBalance,//原账户金额
- 'changeMoney' => $changeMoney,//变动金额
- 'balance' => bcadd($oldBalance,$changeMoney,2),//变动后金额
- 'waitMoney' => bcsub($oldWaitMoney,$changeMoney,2),
- 'customerId' => $businessmanMap[$selfCommissionData['retUserCenterId']]['customerId'],
- 'businessmanId' => $businessmanMap[$selfCommissionData['retUserCenterId']]['id'],
- 'updateTime' => time(),
- ];
- }
- if (!empty($oneCommissionData)){
- //一级返佣
- if (!isset($businessmanMap[$oneCommissionData['retUserCenterId']])){
- return ResultWrapper::fail('获取分销商数据失败',ErrorCode::$paramError);
- }
- $oldWaitMoney = $businessmanMap[$oneCommissionData['retUserCenterId']]['waitMoney'];
- $oldBalance = $businessmanMap[$oneCommissionData['retUserCenterId']]['balance'];
- $changeMoney = $oneCommissionData['retMoney'];
- $updateMap[] = [
- 'userCenterId' => $oneCommissionData['retUserCenterId'],
- 'oldBalance' => $oldBalance,//原账户金额
- 'changeMoney' => $changeMoney,//变动金额
- 'balance' => bcadd($oldBalance,$changeMoney,2),
- 'waitMoney' => bcsub($oldWaitMoney,$changeMoney,2),
- 'customerId' => $businessmanMap[$oneCommissionData['retUserCenterId']]['customerId'],
- 'businessmanId' => $businessmanMap[$oneCommissionData['retUserCenterId']]['id'],
- 'updateTime' => time(),
- ];
- }
- if (!empty($twoCommissionData)){
- //二级返佣
- if (!isset($businessmanMap[$twoCommissionData['retUserCenterId']])){
- return ResultWrapper::fail('获取分销商数据失败',ErrorCode::$paramError);
- }
- $oldWaitMoney = $businessmanMap[$twoCommissionData['retUserCenterId']]['waitMoney'];
- $oldBalance = $businessmanMap[$twoCommissionData['retUserCenterId']]['balance'];
- $changeMoney = $twoCommissionData['retMoney'];
- $updateMap[] = [
- 'userCenterId' => $twoCommissionData['retUserCenterId'],
- 'oldBalance' => $oldBalance,//原账户金额
- 'changeMoney' => $changeMoney,//变动金额
- 'balance' => bcadd($oldBalance,$changeMoney,2),
- 'waitMoney' => bcsub($oldWaitMoney,$changeMoney,2),
- 'customerId' => $businessmanMap[$twoCommissionData['retUserCenterId']]['customerId'],
- 'businessmanId' => $businessmanMap[$twoCommissionData['retUserCenterId']]['id'],
- 'updateTime' => time(),
- ];
- }
- if (!empty($threeCommissionData)){
- //三级
- if (!isset($businessmanMap[$threeCommissionData['retUserCenterId']])){
- return ResultWrapper::fail('获取分销商数据失败',ErrorCode::$paramError);
- }
- $oldWaitMoney = $businessmanMap[$threeCommissionData['retUserCenterId']]['waitMoney'];
- $oldBalance = $businessmanMap[$threeCommissionData['retUserCenterId']]['balance'];
- $changeMoney = $threeCommissionData['retMoney'];
- $updateMap[] = [
- 'userCenterId' => $threeCommissionData['retUserCenterId'],
- 'oldBalance' => $oldBalance,//原账户金额
- 'changeMoney' => $changeMoney,//变动金额
- 'balance' => bcadd($oldBalance,$changeMoney,2),
- 'waitMoney' => bcsub($oldWaitMoney,$changeMoney,2),
- 'customerId' => $businessmanMap[$threeCommissionData['retUserCenterId']]['customerId'],
- 'businessmanId' => $businessmanMap[$threeCommissionData['retUserCenterId']]['id'],
- 'updateTime' => time(),
- ];
- }
- if (!empty($updateMap)){
- Logger::logs(E_USER_ERROR,'分销商更新数据',__CLASS__,__LINE__,$updateMap);
- }
- return ResultWrapper::success($updateMap);
- }
- /**
- * Doc: (des="根据订单id获取订单的分销信息")
- * User: XMing
- * Date: 2020/7/31
- * Time: 5:15 下午
- * @param int $orderId
- * @param string $fields
- * @return ResultWrapper
- */
- private function getOrderIndexData(int $orderId,string $fields = 'id,commissionData,isCommission')
- {
- $orderIndex = $this->objDOrderIndex->get(['id'=>$orderId],$fields);
- if ($orderIndex === false){
- return ResultWrapper::fail($this->objDOrder->error(),ErrorCode::$dberror);
- }
- if (empty($orderIndex)){
- return ResultWrapper::fail('获取分销订单数据为空',ErrorCode::$paramError);
- }
- if ($orderIndex['isCommission'] != StatusCode::$standard){
- return ResultWrapper::fail('不是分销订单',ErrorCode::$paramError);
- }
- $commissionData = json_decode($orderIndex['commissionData'],true);
- if (empty($commissionData)){
- return ResultWrapper::fail('返佣数据为空',ErrorCode::$paramError);
- }
- return ResultWrapper::success($commissionData);
- }
- /**
- * Doc: (des="分销订单取消,扣除返佣")
- * User: XMing
- * Date: 2020/7/31
- * Time: 3:51 下午
- * @param int $orderId
- * @example
- * @return ResultWrapper
- *
- */
- public function cancelSubCommission(int $orderId)
- {
- $commissionDataResult = self::getOrderIndexData($orderId);
- if (!$commissionDataResult->isSuccess()){
- return ResultWrapper::fail($commissionDataResult->getData(),$commissionDataResult->getErrorCode());
- }
- $commissionData = $commissionDataResult->getData();
- $selfCommissionData = isset($commissionData['self']) ? $commissionData['self'] : [];
- $oneCommissionData = isset($commissionData['oneRet']) ? $commissionData['oneRet'] : [];
- $twoCommissionData = isset($commissionData['twoRet']) ? $commissionData['twoRet'] : [];
- $threeCommissionData = isset($commissionData['threeRet']) ? $commissionData['threeRet'] : [];
- $allUserCenterIds = array_column($commissionData,'retUserCenterId');
- //获取分销商的余额
- $businessman = $this->objDCommissionBusinessman->select(['userCenterId' => $allUserCenterIds], 'id,userCenterId,balance,totalMoney,waitMoney');
- if ($businessman === false) {
- return ResultWrapper::fail($this->objDCommissionBusinessman->error(), ErrorCode::$dberror);
- }
- if(empty($businessman)){
- return ResultWrapper::fail('获取分销商数据为空',ErrorCode::$paramError);
- }
- $businessmanMap = [];
- foreach ($businessman as $value){
- $businessmanMap[$value['userCenterId']] = $value;
- }
- $updateDataResult = self::buildCancelData($selfCommissionData,$oneCommissionData,$twoCommissionData,$threeCommissionData,$businessmanMap);
- if (!$updateDataResult->isSuccess()){
- return ResultWrapper::fail($updateDataResult->getData(),$updateDataResult->getErrorCode());
- }
- $updateData = $updateDataResult->getData();
- if (empty($updateData)){
- return ResultWrapper::fail('组装分销商更新数据失败',ErrorCode::$paramError);
- }
- $this->objDCommissionBusinessman->beginTransaction();
- foreach ($updateData as $value){
- $update = $this->objDCommissionBusinessman->update(
- [
- 'waitMoney' => $value['waitMoney'],
- 'totalMoney' => $value['totalMoney'],
- 'updateTime' => time(),
- ],
- [
- 'userCenterId'=> $value['userCenterId']
- ]
- );
- if ($update === false){
- $this->objDCommissionBusinessman->rollBack();
- return ResultWrapper::fail($this->objDCommissionBusinessman->error(),ErrorCode::$dberror);
- }
- }
- //更新这批分销订单取消
- $updateCommissionOrderIndex = $this->objDCommissionOrderIndex->update(
- [
- 'isAccount' => StatusCode::$auditStatus['auditNotPass'],//3已取消
- 'updateTime'=>time()
- ],
- [
- 'orderId' => $orderId
- ]
- );
- if ($updateCommissionOrderIndex === false){
- $this->objDCommissionBusinessman->rollBack();
- }
- $this->objDCommissionBusinessman->commit();
- return ResultWrapper::success('更新分销商佣金成功');
- }
- /**
- * Doc: (des="取消分销订单,分销商更新数据")
- * User: XMing
- * Date: 2020/7/31
- * Time: 5:50 下午
- * @param array $selfCommissionData
- * @param array $oneCommissionData
- * @param array $twoCommissionData
- * @param array $threeCommissionData
- * @param array $businessmanMap
- * @return ResultWrapper
- */
- private function buildCancelData(array $selfCommissionData,array $oneCommissionData,array $twoCommissionData,array $threeCommissionData,array $businessmanMap)
- {
- $updateMap = [];
- if (!empty($selfCommissionData)){
- if (!isset($businessmanMap[$selfCommissionData['retUserCenterId']])){
- return ResultWrapper::fail('获取分销商账户失败',ErrorCode::$dberror);
- }
- $oldWaitMoney = $businessmanMap[$selfCommissionData['retUserCenterId']]['waitMoney'];
- $oldTotalMoney = $businessmanMap[$selfCommissionData['retUserCenterId']]['totalMoney'];
- //获取当前分销商的等待入账金额
- $changeMoney = $selfCommissionData['retMoney'];
- $updateMap[] = [
- 'userCenterId' => $selfCommissionData['retUserCenterId'],
- 'waitMoney' => bcsub($oldWaitMoney,$changeMoney,2),
- 'totalMoney' => bcsub($oldTotalMoney,$changeMoney,2),
- 'updateTime' => time(),
- ];
- }
- if (!empty($oneCommissionData)){
- if (!isset($businessmanMap[$oneCommissionData['retUserCenterId']])){
- return ResultWrapper::fail('获取分销商账户失败',ErrorCode::$dberror);
- }
- $oldWaitMoney = $businessmanMap[$oneCommissionData['retUserCenterId']]['waitMoney'];
- $oldTotalMoney = $businessmanMap[$oneCommissionData['retUserCenterId']]['totalMoney'];
- //获取当前分销商的等待入账金额
- $changeMoney = $oneCommissionData['retMoney'];
- $updateMap[] = [
- 'userCenterId' => $oneCommissionData['retUserCenterId'],
- 'waitMoney' => bcsub($oldWaitMoney,$changeMoney,2),
- 'totalMoney' => bcsub($oldTotalMoney,$changeMoney,2),
- 'updateTime' => time(),
- ];
- }
- if (!empty($twoCommissionData)){
- if (!isset($businessmanMap[$twoCommissionData['retUserCenterId']])){
- return ResultWrapper::fail('获取分销商账户失败',ErrorCode::$dberror);
- }
- $oldWaitMoney = $businessmanMap[$twoCommissionData['retUserCenterId']]['waitMoney'];
- $oldTotalMoney = $businessmanMap[$twoCommissionData['retUserCenterId']]['totalMoney'];
- //获取当前分销商的等待入账金额
- $changeMoney = $twoCommissionData['retMoney'];
- $updateMap[] = [
- 'userCenterId' => $twoCommissionData['retUserCenterId'],
- 'waitMoney' => bcsub($oldWaitMoney,$changeMoney,2),
- 'totalMoney' => bcsub($oldTotalMoney,$changeMoney,2),
- 'updateTime' => time(),
- ];
- }
- if (!empty($threeCommissionData)){
- if (!isset($businessmanMap[$threeCommissionData['retUserCenterId']])){
- return ResultWrapper::fail('获取分销商账户失败',ErrorCode::$dberror);
- }
- $oldWaitMoney = $businessmanMap[$threeCommissionData['retUserCenterId']]['waitMoney'];
- $oldTotalMoney = $businessmanMap[$threeCommissionData['retUserCenterId']]['totalMoney'];
- //获取当前分销商的等待入账金额
- $changeMoney = $threeCommissionData['retMoney'];
- $updateMap[] = [
- 'userCenterId' => $threeCommissionData['retUserCenterId'],
- 'waitMoney' => bcsub($oldWaitMoney,$changeMoney,2),
- 'totalMoney' => bcsub($oldTotalMoney,$changeMoney,2),
- 'updateTime' => time(),
- ];
- }
- if (!empty($updateMap)){
- Logger::logs(E_USER_ERROR,'分销订单取消,更新分销商数据',__CLASS__,__LINE__,$updateMap);
- }
- return ResultWrapper::success($updateMap);
- }
- /**
- * Doc: (des="提现状态变更,操作分销商数据")
- * User: XMing
- * Date: 2020/7/31
- * Time: 4:06 下午
- * @param array $withdraw
- * @param int $status
- * @return ResultWrapper
- * @example
- * balance 余额
- * waitAuditWithdraw 提现待审核金额
- * auditWithdraw 提现待入账金额
- * withdraw 已提现金额
- */
- public function buildWithdraw(array $withdraw,int $status)
- {
- $businessman = $this->objDCommissionBusinessman->get(['id'=>$withdraw['businessmanId']],'id,userCenterId,balance,waitAuditWithdraw,auditWithdraw,withdraw');
- if ($businessman === false){
- return ResultWrapper::fail($this->objDCommissionBusinessman->error(),ErrorCode::$dberror);
- }
- if (empty($businessman)){
- return ResultWrapper::fail('获取分销商账户信息失败',ErrorCode::$paramError);
- }
- $oldBalance = $businessman['balance'];//可提现
- $oldWaitAuditWithdraw = $businessman['waitAuditWithdraw'];//提现等待审核
- $oldAuditWithdraw = $businessman['auditWithdraw'];//提现待入账金额
- $oldWithdraw = $businessman['withdraw'];//已提现金额
- $changeMoney = $withdraw['money'];//本次变动金额
- $updateMap = [];//需要更新的数据
- switch ($status){
- case StatusCode::$auditStatus['auditing']:
- //申请提现操作
- if ($oldBalance < $withdraw['money']){
- return ResultWrapper::fail('可提现金额不足',ErrorCode::$paramError);
- }
- $updateMap = [
- 'id' => $businessman['id'],
- 'updateTime' => time(),
- 'balance' => bcsub($oldBalance,$changeMoney,2),
- 'waitAuditWithdraw' => bcadd($oldWaitAuditWithdraw,$changeMoney,2),
- ];
- break;
- case StatusCode::$auditStatus['auditPass']:
- //打款成功
- if (bcsub($oldAuditWithdraw,$changeMoney,2) < 0){
- return ResultWrapper::fail('待入账金额不足',ErrorCode::$paramError);
- }
- $updateMap = [
- 'id' => $businessman['id'],
- 'updateTime' => time(),
- 'auditWithdraw' => bcsub($oldAuditWithdraw,$changeMoney,2),
- 'withdraw' => bcadd($oldWithdraw,$changeMoney,2),
- ];
- break;
- case StatusCode::$auditStatus['auditNotPass']:
- //提现审核拒绝,回到余额
- if (bcsub($oldWaitAuditWithdraw,$changeMoney,2) < 0){
- return ResultWrapper::fail('待审核金额不足',ErrorCode::$paramError);
- }
- $updateMap = [
- 'id' => $businessman['id'],
- 'updateTime' => time(),
- 'balance' => bcadd($oldBalance,$changeMoney,2),
- 'waitAuditWithdraw' => bcsub($oldWaitAuditWithdraw,$changeMoney,2),
- ];
- break;
- case StatusCode::$auditStatus['auditIng']:
- //等待打款,审核成功
- if (bcsub($oldWaitAuditWithdraw,$changeMoney,2) < 0){
- return ResultWrapper::fail('待审核金额不足',ErrorCode::$paramError);
- }
- $updateMap = [
- 'id' => $businessman['id'],
- 'updateTime' => time(),
- 'waitAuditWithdraw' => bcsub($oldWaitAuditWithdraw,$changeMoney,2),
- 'auditWithdraw' => bcadd($oldAuditWithdraw,$changeMoney,2),
- ];
- break;
- default:
- return ResultWrapper::fail('状态值有误',ErrorCode::$paramError);
- break;
- }
- if (!empty($updateMap)){
- Logger::logs(E_USER_ERROR,'提现变动数据',__CLASS__,__LINE__,$updateMap);
- }
- return ResultWrapper::success($updateMap);
- }
- /**
- * Doc: (des="佣金变动记录 balance")
- * User: XMing
- * Date: 2020/8/3
- * Time: 2:32 下午
- * @param array $data
- * @param float $changeAmount 变动金额
- * @param float $balance 变动后金额
- * @param int $type +/-
- * @param string $des 描述
- * @return ResultWrapper
- */
- private function changeLog(array $data,float $changeAmount,float $balance,int $type,string $des="")
- {
- $insertRes = $this->objDCommissionFlow->insert([
- "userCenterId" => $data['userCenterId'] ,//int(10) DEFAULT NULL,
- "customerId" => $data['customerId'],//int(10) DEFAULT NULL COMMENT '客户id',
- "businessmanId" => $data['businessmanId'] ,//int(10) DEFAULT NULL COMMENT '分销商id',
- "amount" => $changeAmount,//decimal(8,2) DEFAULT NULL COMMENT '金额',
- "changeAmount" => $balance ,//decimal(8,2) DEFAULT NULL COMMENT '变动后金额',
- "type"=>$type ,//tinyint(1) DEFAULT NULL COMMENT '状态 4:减 5:加',
- "des" => $des,
- "createTime" => time(),
- ]);
- if ($insertRes === false){
- return ResultWrapper::fail($this->objDCommissionFlow->error(),ErrorCode::$dberror);
- }
- return ResultWrapper::success('操作成功');
- }
- }
|