CalcServeBonusCalc.php 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/8/2
  6. * Time: 上午10:32
  7. */
  8. namespace common\helpers\bonus;
  9. use common\helpers\Cache;
  10. use common\helpers\Date;
  11. use common\helpers\snowflake\SnowFake;
  12. use common\helpers\Tool;
  13. use common\models\CalcBonus;
  14. use common\models\CalcBonusBD;
  15. use common\models\CalcBonusBS;
  16. use common\models\CalcBonusQuarter;
  17. use common\models\CalcBonusGarage;
  18. use common\models\CalcBonusQY;
  19. use common\models\CalcBonusTG;
  20. use common\models\CalcBonusTourism;
  21. use common\models\CalcBonusVilla;
  22. use common\models\PerfMonth;
  23. use common\models\PerfPeriod;
  24. use common\models\Period;
  25. use common\models\StarCrownLevel;
  26. use yii\base\BaseObject;
  27. use yii\base\StaticInstanceTrait;
  28. use yii\db\Query;
  29. class CalcServeBonusCalc extends BaseObject {
  30. use StaticInstanceTrait;
  31. private $_limit = 3000;
  32. private $_handleUserId;
  33. private $_sysConfig = [];
  34. private $_decLevelConfig = [];
  35. private $_empLevelConfig = [];
  36. private $_starCrownLevelConfig = [];
  37. private $_decRoleConfig = [];
  38. private $_errors = [];
  39. private $_periodNum = 0;
  40. private $_periodId;
  41. private $_isCalcMonth = 0;
  42. private $_calcYear;
  43. private $_calcMonth;
  44. private $_calcYearMonth;
  45. private $_calcMonthPeriodNumCount = 0;
  46. //pv
  47. private $_pvRatio;
  48. private $_calcZone = ['openTravel', 'openCar', 'openHouse'];
  49. const LOOP_FINISH = 1;
  50. const LOOP_CONTINUE = 2;
  51. const ORDER_TYPE_TO_FW_COEFFICIENT = [
  52. 'ZC' => 'fwCoefficientFromZc',
  53. 'FX_CASH' => 'fwCoefficientFromFxCash',
  54. 'FX_POINT' => 'fwCoefficientFromFxPoint',
  55. ];
  56. //最小报单pv
  57. const MIN_BD_PV = 980;
  58. public function init() {
  59. parent::init();
  60. }
  61. /**
  62. * 设置期数
  63. * @param int $periodNum
  64. * @return int
  65. */
  66. public function setPeriodNum(int $periodNum) {
  67. return $this->_periodNum = $periodNum;
  68. }
  69. /**
  70. * 获取期数
  71. * @return int
  72. */
  73. public function getPeriodNum() {
  74. return $this->_periodNum;
  75. }
  76. /**
  77. * 加入错误错误
  78. * @param $attr
  79. * @param $error
  80. */
  81. public function addError($attr, $error) {
  82. $this->_errors[$attr][] = $error;
  83. }
  84. /**
  85. * 获取错误信息
  86. * @return array
  87. */
  88. public function getErrors() {
  89. return $this->_errors;
  90. }
  91. /**
  92. * 开始执行结算步骤
  93. * @param $periodNum
  94. * @param null $handleUserId
  95. * @return bool
  96. */
  97. public function calcStep($periodNum, $handleUserId = null) {
  98. try {
  99. $this->_errors = [];
  100. $this->setPeriodNum($periodNum);
  101. $this->_handleUserId = $handleUserId;
  102. $t1 = microtime(true);
  103. // 初始化结算任务
  104. $this->initCalcTask();
  105. $t2 = microtime(true);
  106. // 设置结算状态
  107. $this->setCalcStatus('start');
  108. // 清空所有本期结算用到的缓存
  109. CalcCache::clearCalcBonusCache($this->_periodNum);
  110. $t3 = microtime(true);
  111. // 清空相关表数据
  112. $this->clearCalcTableData();
  113. $t4 = microtime(true);
  114. echo('初始化、清空缓存及相关数据表完成,耗时:' . round($t4 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  115. $this->_updatePercent(10);
  116. // 蓝星奖放到最前面 奖金计算开始
  117. if($this->_sysConfig['openGL']['VALUE']) {
  118. echo('计算蓝星奖开始,' . date('Y-m-d H:i:s', $t4) . PHP_EOL);
  119. // 调用存储过程,计算蓝星管理奖金
  120. $this->calcBsProcedure();
  121. // 将有蓝星管理奖金的用户加入到有奖金缓存用户中
  122. $this->calcBonusBsGL();
  123. // 将有【蓝星业绩奖金】的用户加入到有奖金缓存用户中
  124. // $this->calcBonusBsYJ();
  125. // // 将有【蓝星管理奖金】的用户加入到有奖金缓存用户中
  126. // $this->calcBonusBsGL();
  127. }
  128. $t5 = microtime(true);
  129. echo('计算蓝星奖'.($this->_sysConfig['openGL']['VALUE']?'完成':'关闭').',耗时:' . round($t5 - $t4, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  130. if($this->_sysConfig['openFW']['VALUE']) {
  131. $this->calcBonusBDStepOne();
  132. $this->calcBonusBDStepTwo();
  133. }
  134. $t6 = microtime(true);
  135. echo('计算服务奖'.($this->_sysConfig['openFW']['VALUE']?'完成':'关闭').',耗时:' . round($t6 - $t5, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  136. $this->_updatePercent(15);
  137. // 销售奖/推广奖
  138. if($this->_sysConfig['openTG']['VALUE']) {
  139. $this->calcBonusTG();
  140. }
  141. $t7 = microtime(true);
  142. echo('计算推广奖'.($this->_sysConfig['openTG']['VALUE']?'完成':'关闭').',耗时:' . round($t7 - $t6, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  143. $this->_updatePercent(20);
  144. // 绩效奖/团队奖
  145. if($this->_sysConfig['openQY']['VALUE']) {
  146. $this->calcBonusQY();
  147. }
  148. $t8 = microtime(true);
  149. echo('计算团队奖'.($this->_sysConfig['openQY']['VALUE']?'完成':'关闭').',耗时:' . round($t8 - $t7, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  150. $this->_updatePercent(35);
  151. // $this->calcBonusTourism($this->_sysConfig['openTourism']);
  152. // $t21 = microtime(true);
  153. // echo('计算旅游奖' . ($this->_sysConfig['openTourism']['VALUE'] ? '完成' : '关闭') . ',耗时:' . round($t21 - $t20, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  154. // $this->_updatePercent(68);
  155. $this->calcBonusVilla();
  156. $t22 = microtime(true);
  157. echo('计算房奖' . ($this->_sysConfig['openVilla']['VALUE'] ? '完成' : '关闭').',耗时:' . round($t22 - $t8, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL . PHP_EOL);
  158. $this->_updatePercent(45);
  159. $this->calcBonusGarage();
  160. $t23 = microtime(true);
  161. echo('计算车奖' . ($this->_sysConfig['openGarage']['VALUE'] ? '完成' : '关闭').',耗时:' . round($t23 - $t22, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL . PHP_EOL);
  162. $this->_updatePercent(55);
  163. // 计算季度奖
  164. $this->calcQuarter();
  165. $t24 = microtime(true);
  166. echo('计算季度奖' . ($this->_sysConfig['openQuarter']['VALUE'] ? '开启调用存储过程' : '关闭').',耗时:' . round($t24 - $t23, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL . PHP_EOL);
  167. // 将用户写入缓存
  168. $this->calcQuarterUser();
  169. $this->_updatePercent(65);
  170. $t25 = microtime(true);
  171. echo('计算季度奖' . ($this->_sysConfig['openQuarter']['VALUE'] ? '完成' : '关闭').',耗时:' . round($t25 - $t24, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL . PHP_EOL);
  172. // 奖金写库
  173. $this->loopBonusUsers();
  174. $this->_updatePercent(75);
  175. $t30 = microtime(true);
  176. echo('奖金写库操作完成,耗时:' . round($t30 - $t25, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  177. Period::updateCalcProcess(3, $this->_periodNum);
  178. $this->_updatePercent(100);
  179. $t35 = microtime(true);
  180. echo('结算全部完成,共耗时:' . round($t35 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL . PHP_EOL);
  181. } catch (\Exception $e) {
  182. $this->errorCalcTask();
  183. $this->addError('calc', sprintf('File【%s】, Line【%s】, Msg【%s】', $e->getFile(), $e->getLine(), $e->getMessage()));
  184. return false;
  185. }
  186. return true;
  187. }
  188. /**
  189. * 结算完成
  190. */
  191. public function endCalcTask() {
  192. // 更新结算状态
  193. $this->setCalcStatus('end');
  194. }
  195. /**
  196. * 结算错误
  197. */
  198. public function errorCalcTask() {
  199. // 清空所有本期结算用到的缓存
  200. CalcCache::clearCalcBonusCache($this->_periodNum);
  201. // 更新结算状态
  202. $this->setCalcStatus('fail');
  203. }
  204. /**
  205. * 初始化结算任务
  206. * @throws \yii\db\Exception
  207. */
  208. public function initCalcTask() {
  209. $periodObj = Period::instance();
  210. $periodDataArr = $periodObj->setPeriodNum($this->_periodNum);
  211. if (empty($this->_periodNum)) {
  212. $this->_periodNum = $periodDataArr['PERIOD_NUM'];
  213. }
  214. $this->_sysConfig = Cache::getSystemConfig();
  215. $this->_decLevelConfig = Cache::getDecLevelConfig();
  216. $this->_empLevelConfig = Cache::getEmpLevelConfig();
  217. $this->_starCrownLevelConfig = Cache::getStarCrownLevelConfig();
  218. $this->_decRoleConfig = CalcCache::getDecRoleConfig($this->_periodNum);
  219. $periodNum = $this->_periodNum;
  220. $this->_periodId = $periodDataArr['ID'];
  221. $this->_isCalcMonth = $periodObj->isCalcMonth($periodNum);
  222. $this->_calcYear = $periodObj->getYear($periodNum);
  223. $this->_calcMonth = $periodObj->getMonth($periodNum);
  224. $this->_calcYearMonth = $periodObj->getYearMonth($periodNum);
  225. }
  226. /**
  227. * 设置结算状态
  228. * @param $type
  229. * start|end|fail
  230. */
  231. public function setCalcStatus($type) {
  232. if ($type == 'start') {
  233. Period::updateAll(['IS_CALCING' => 1, 'IS_CALCULATED' => Period::CALCULATE_NONE, 'CALCULATE_STARTED_AT' => Date::nowTime()], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  234. } elseif ($type == 'end') {
  235. Period::updateAll(['IS_CALCING' => 0, 'IS_CALCULATED' => Period::CALCULATE_FINISH, 'CALCULATED_AT' => Date::nowTime()], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  236. } elseif ($type == 'fail') {
  237. Period::updateAll(['IS_CALCING' => 0, 'IS_CALCULATED' => Period::CALCULATE_FAIL, 'CALCULATED_AT' => 0], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  238. }
  239. }
  240. /**
  241. * 清空相关表数据
  242. */
  243. public function clearCalcTableData() {
  244. // 奖金表
  245. CalcBonus::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  246. CalcBonusQY::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  247. CalcBonusBD::pageDeleteAll('PERIOD_NUM='.$this->_periodNum); // 实际上是服务奖流水表
  248. CalcBonusTG::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  249. // 月结时要清空的数据
  250. if ($this->_isCalcMonth) {
  251. CalcBonusTourism::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  252. CalcBonusGarage::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  253. CalcBonusVilla::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  254. }
  255. }
  256. /**
  257. * 推广奖
  258. * @param int $offset
  259. * @return bool
  260. * @throws \yii\db\Exception
  261. */
  262. public function calcBonusTG(int $offset = 0) {
  263. $periodNum = $this->_periodNum;
  264. // 从缓存获取分页有业绩的会员信息
  265. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  266. if ($allData) {
  267. $insertBonusData = [];
  268. foreach ($allData as $userId) {
  269. // 从缓存中获取会员的业绩信息
  270. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  271. if( !$perfData ) continue;
  272. //个人业绩都算推荐奖,包括报单和复消、二次购物
  273. $perfPv = $perfData['PV_PCS_ZC'] ?? 0;
  274. if( $perfPv <= 0 ) continue;
  275. //推广奖使用个人PCS
  276. $recBonus = Tool::formatPrice($perfPv * $this->_sysConfig['recPercent']['VALUE'] / 100);
  277. if ($recBonus <= 0) continue;
  278. // 把对碰后的奖金存入缓存中
  279. $perfUserInfo = CalcCache::getUserInfo($userId, $periodNum);
  280. $bonusUserId = $perfUserInfo['REC_UID'] ?? '';
  281. if( !$bonusUserId ) continue;
  282. // 获取会员的报单级别
  283. $userBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  284. //扣除相应的复消积分和管理费
  285. $deductData = $this->deduct($bonusUserId, $recBonus);
  286. CalcCache::bonus($bonusUserId, $periodNum, 'BONUS_TG', $recBonus, $deductData);
  287. //来源会员信息
  288. $fromUserInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  289. //推广奖流水
  290. $insertBonusData[] = [
  291. 'ID' => SnowFake::instance()->generateId(),
  292. 'USER_ID' => $bonusUserId,
  293. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  294. 'LAST_EMP_LV' => $userBaseInfo['LAST_EMP_LV'],,
  295. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  296. 'FROM_USER_ID' => $userId,
  297. 'LAST_FROM_DEC_LV' => $fromUserInfo['DEC_LV'],
  298. 'LAST_FROM_EMP_LV' => $fromUserInfo['EMP_LV'],
  299. 'LAST_FROM_STATUS' => $fromUserInfo['STATUS'],
  300. 'AMOUNT' => $deductData['surplus'],
  301. 'ORI_BONUS' => $recBonus,
  302. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  303. 'MANAGE_TAX' => $deductData['manageTax'],
  304. 'PERIOD_NUM' => $this->_periodNum,
  305. 'CALC_YEAR' => $this->_calcYear,
  306. 'CALC_MONTH' => $this->_calcYearMonth,
  307. 'CREATED_AT' => Date::nowTime(),
  308. 'LOGS' => json_encode([
  309. 'perfPv' => $perfPv,
  310. 'recPercentConfig' => $this->_sysConfig['recPercent']['VALUE'],
  311. 'recNum' => $userBaseInfo['REC_NUM'],
  312. 'decAmount' => $userBaseInfo['ZC_AMOUNT'],
  313. 'decLevel' => $userBaseInfo['DEC_LV'],
  314. 'bonusTotalLimit' => [
  315. $this->_sysConfig['bonusTotalZeroLimit']['VALUE'],
  316. $this->_sysConfig['bonusTotalOneLimit']['VALUE'],
  317. $this->_sysConfig['bonusTotalTwoLimit']['VALUE'],
  318. ],
  319. ]),
  320. ];
  321. unset($perfData, $perfPv, $perfUserInfo, $recBonus, $bonusUserId, $userBaseInfo, $userId, $deductData, $fromUserInfo);
  322. }
  323. CalcBonusTG::batchInsert($insertBonusData);
  324. unset($allData, $insertBonusData);
  325. return $this->calcBonusTG($offset + $this->_limit);
  326. }
  327. unset($allData);
  328. return true;
  329. }
  330. /**
  331. * 服务奖第一步
  332. * @param int $offset
  333. * @return bool
  334. * @throws \yii\db\Exception
  335. */
  336. public function calcBonusBDStepOne(int $offset = 0) {
  337. echo sprintf("时间:[%s]服务奖第【1】步,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  338. $periodNum = $this->_periodNum;
  339. // 从缓存获取分页有业绩的会员信息
  340. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  341. if ($allData) {
  342. $insertBonusData = [];
  343. foreach ($allData as $userId) {
  344. // 从缓存中获取会员的业绩信息
  345. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  346. if( !$perfData ) continue;
  347. $decRoleBonusFrom = explode(',', $this->_sysConfig['decRoleBonusFrom']['VALUE']);
  348. $validPvPcs = 0;
  349. foreach ($decRoleBonusFrom as $orderType) {
  350. $orderTypeName = sprintf('PV_PCS_%s', $orderType);
  351. $orderTypeValue = $perfData[$orderTypeName] ?? 0;
  352. $coefficientName = self::ORDER_TYPE_TO_FW_COEFFICIENT[$orderType];
  353. $coefficient = $this->_sysConfig[$coefficientName]['VALUE'] ?? 1;
  354. $validPvPcs += $orderTypeValue * $coefficient;
  355. unset($orderType, $orderTypeName, $orderTypeValue, $coefficientName, $coefficient);
  356. }
  357. unset($perfData, $decRoleBonusFrom);
  358. if ( $validPvPcs <= 0 ) continue;
  359. $this->loopRelationParentDo($userId, function ($parent) use($userId, $validPvPcs){
  360. //判断parent的报单中心级别 和 服务奖比例
  361. $bonusUserId = $parent['PARENT_UID'];
  362. //计算级别之后更新过userInfo的缓存,缓存中级别发生了变化
  363. $bonusUserInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  364. $isDec = $bonusUserInfo['IS_DEC'];
  365. if($isDec == 0) return self::LOOP_CONTINUE;
  366. $decRoleId = $bonusUserInfo['DEC_ROLE_ID'];
  367. if( !$decRoleId ) return self::LOOP_CONTINUE;
  368. if( !isset($this->_decRoleConfig[$decRoleId]) ) return self::LOOP_CONTINUE;
  369. $parentDecRoleLevel = $this->_decRoleConfig[$decRoleId];
  370. $parentFwBonusPercent = $parentDecRoleLevel['FW_BONUS_PERCENT'] ?? 0;
  371. $cacheMaxPercent = CalcCache::fwMaxBonusPercent($userId, $this->_periodNum);
  372. $diffPercent = $parentFwBonusPercent - $cacheMaxPercent;
  373. if( $diffPercent <= 0 ) return self::LOOP_CONTINUE;
  374. $fwBonus = $validPvPcs * $diffPercent / 100;
  375. if( $fwBonus <= 0 ) return self::LOOP_CONTINUE;
  376. //给本人添加服务奖比例
  377. CalcCache::fwMaxBonusPercent($userId, $this->_periodNum, $parentFwBonusPercent);
  378. //记录奖金和奖金来源到缓存 并实现在缓存中奖金累加
  379. CalcCache::saveFwBonusList($bonusUserId, $this->_periodNum, $fwBonus, ['fromUid'=>$userId, 'fromPvPcs'=>$validPvPcs]);
  380. CalcCache::addHasFwBonusUsers($bonusUserId, $this->_periodNum);
  381. unset($bonusUserId, $bonusUserInfo, $isDec, $decRoleId, $parentDecRoleLevel, $parentFwBonusPercent, $cacheMaxPercent, $diffPercent, $fwBonus);
  382. });
  383. unset($userId, $validPvPcs);
  384. }
  385. unset($allData, $insertBonusData);
  386. return $this->calcBonusBDStepOne($offset + $this->_limit);
  387. }
  388. unset($allData);
  389. return true;
  390. }
  391. /**
  392. * 服务奖第二步
  393. * @param int $offset
  394. * @return bool
  395. * @throws \yii\db\Exception
  396. */
  397. public function calcBonusBDStepTwo(int $offset = 0) {
  398. echo sprintf("时间:[%s]服务奖第【2】步,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  399. $allData = CalcCache::getHasFwBonusUsers($this->_periodNum, $offset, $this->_limit);
  400. if ($allData) {
  401. $insertBonusData = [];
  402. foreach ($allData as $userId) {
  403. $fwBonusData = CalcCache::getFwBonusList($userId, $this->_periodNum);
  404. if( !$fwBonusData ) continue;
  405. $fwBonus = $fwBonusData['fwBonus'] ?? 0;
  406. if( $fwBonus <=0 ) continue;
  407. //总金额限制
  408. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  409. CalcCache::bonus($userId, $this->_periodNum, 'BONUS_BD', $fwBonus);
  410. $decRoleId = $userBaseInfo['DEC_ROLE_ID'];
  411. $insertBonusData[] = [
  412. 'ID' => SnowFake::instance()->generateId(),
  413. 'USER_ID' => $userId,
  414. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  415. 'LAST_EMP_LV' => $userBaseInfo['LAST_EMP_LV'],
  416. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  417. 'FROM_USER_ID' => $userId,
  418. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  419. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  420. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  421. 'AMOUNT' => $fwBonus,
  422. 'ORI_BONUS' => $fwBonus,
  423. 'RECONSUME_POINTS' => 0,
  424. 'MANAGE_TAX' => 0,
  425. 'PERIOD_NUM' => $this->_periodNum,
  426. 'CALC_YEAR' => $this->_calcYear,
  427. 'CALC_MONTH' => $this->_calcYearMonth,
  428. 'CREATED_AT' => Date::nowTime(),
  429. 'LOGS' => json_encode([
  430. 'decRoleId' => $decRoleId,
  431. ])
  432. ];
  433. unset($userId, $fwBonusData, $userBaseInfo, $decRoleId, $fwBonus);
  434. }
  435. CalcBonusBD::batchInsert($insertBonusData);
  436. unset($insertBonusData, $allData);
  437. $this->calcBonusBDStepTwo($offset + $this->_limit);
  438. }
  439. unset($allData);
  440. return true;
  441. }
  442. /**
  443. * 团队奖
  444. * @param int $offset
  445. * @return bool
  446. * @throws \yii\db\Exception
  447. */
  448. public function calcBonusQY(int $offset = 0) {
  449. echo sprintf("时间:[%s]团队奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  450. $periodNum = $this->_periodNum;
  451. // 从缓存获取分页有业绩的会员信息
  452. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  453. if ($allData) {
  454. $insertBonusData = [];
  455. foreach ($allData as $userId) {
  456. // 从缓存中获取会员的业绩信息
  457. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  458. // 从缓存中获取会员的上期结余业绩信息
  459. $pervSurplusPerf = CalcCache::surplusPerf($userId, $periodNum);
  460. // 本期 + 上期结余
  461. $perfArr = [
  462. 'SURPLUS_1L' => $perfData['PV_1L_TOUCH'] + $pervSurplusPerf['SURPLUS_1L'],
  463. 'SURPLUS_2L' => $perfData['PV_2L_TOUCH'] + $pervSurplusPerf['SURPLUS_2L'],
  464. 'SURPLUS_3L' => $perfData['PV_3L_TOUCH'] + $pervSurplusPerf['SURPLUS_3L'],
  465. 'SURPLUS_4L' => $perfData['PV_4L_TOUCH'] + $pervSurplusPerf['SURPLUS_4L'],
  466. 'SURPLUS_5L' => $perfData['PV_5L_TOUCH'] + $pervSurplusPerf['SURPLUS_5L'],
  467. ];
  468. $oriPerfArr = [
  469. 'perfArr' => $perfArr,
  470. 'touchBonus' => 0,
  471. ];
  472. // 获取会员的报单级别
  473. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  474. $decLevelConfig = $this->_decLevelConfig;
  475. $nowDecLevelConfig = $decLevelConfig[$userBaseInfo['DEC_LV']];
  476. // 对碰
  477. $touchBonusArr = $this->touchPerf($oriPerfArr, $perfArr, $nowDecLevelConfig['QY_PERCENT']/100);
  478. $touchPerfArr = [];
  479. foreach ($touchBonusArr['perfArr'] as $keyR => $perfR) {
  480. $touchPerfArr[$keyR] = $perfR;
  481. }
  482. // 对碰完成后把结余的业绩存入本期业绩缓存中
  483. CalcCache::nowPeriodPerf($userId, $periodNum, $touchPerfArr);
  484. //更新数据库
  485. PerfPeriod::updateAll($touchPerfArr, 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  486. 'USER_ID' => $userId,
  487. 'PERIOD_NUM' => $periodNum,
  488. ]);
  489. if ($touchBonusArr['touchBonus'] <= 0) continue;
  490. $teamBonus = $touchBonusArr['touchBonus'];
  491. $capBonusQy = $teamBonus; // 封顶前的奖金
  492. //判断级别上限,个人奖金封顶限制
  493. $teamBonus = $this->declarationLevelCap($teamBonus, $userId, $userBaseInfo['DEC_LV']);
  494. if( $teamBonus <= 0 ) continue;
  495. // 将封顶前的金额加入用户奖金缓存中,此金额不能发放(总奖金,总实际奖金)
  496. CalcCache::bonus($userId, $periodNum, 'CAPPED_BONUS_QY', $capBonusQy);
  497. //扣除相应的复消积分和管理费
  498. $deductData = $this->deduct($userId, $teamBonus);
  499. // 把对碰后的奖金存入缓存中
  500. CalcCache::bonus($userId, $periodNum, 'BONUS_QY', $teamBonus, $deductData);
  501. // TODO:取小腿值
  502. $payLeg = min([$perfArr['SURPLUS_1L'], $perfArr['SURPLUS_2L']]);
  503. // 计算荣衔星级
  504. $starCrown = StarCrownLevel::getStarCrown($payLeg);
  505. // 星级放入缓存
  506. CalcCache::addUserStarCrown($userId, $periodNum, $starCrown['ID']);
  507. //团队奖流水
  508. $insertBonusData[] = [
  509. 'ID' => SnowFake::instance()->generateId(),
  510. 'USER_ID' => $userId,
  511. 'ORI_CAPPED_BONUS_QY' => $capBonusQy,
  512. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  513. 'LAST_EMP_LV' => $userBaseInfo['LAST_EMP_LV'],
  514. 'LAST_CROWN_LV' => $starCrown['ID'],
  515. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  516. 'AMOUNT' => $deductData['surplus'],
  517. 'ORI_BONUS' => $teamBonus,
  518. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  519. 'MANAGE_TAX' => $deductData['manageTax'],
  520. 'PERIOD_NUM' => $this->_periodNum,
  521. 'CALC_YEAR' => $this->_calcYear,
  522. 'CALC_MONTH' => $this->_calcYearMonth,
  523. 'CREATED_AT' => Date::nowTime(),
  524. 'LOGS' => json_encode([
  525. 'perfArr' => $perfArr,
  526. 'touchPerfArrOri' => $touchBonusArr['perfArr'],
  527. 'touchPerfArr' => $touchPerfArr,
  528. 'nowDecLevelConfig' => $nowDecLevelConfig,
  529. 'decLevel' => $userBaseInfo['DEC_LV'],
  530. ]),
  531. ];
  532. unset($perfData, $pervSurplusPerf, $perfArr, $oriPerfArr, $touchPerfArr, $userBaseInfo, $decLevelConfig, $touchBonusArr, $userId, $nowDecLevelConfig, $teamBonus, $deductData);
  533. }
  534. CalcBonusQY::batchInsert($insertBonusData);
  535. unset($allData, $insertBonusData);
  536. return $this->calcBonusQY($offset + $this->_limit);
  537. }
  538. unset($allData);
  539. return true;
  540. }
  541. /**
  542. * 季度奖计算
  543. *
  544. */
  545. public function calcQuarter() {
  546. if( !$this->_isCalcMonth || !in_array($this->_calcMonth, [3,6,9,12])) {
  547. // echo('不是季结点,进这里,不计算季度奖'. PHP_EOL);
  548. return false;
  549. }
  550. $result = \Yii::$app->db->createCommand("CALL QtrCalc(:periodNum)")
  551. ->bindValue(':periodNum' , $this->_periodNum )
  552. ->execute();
  553. return $result;
  554. }
  555. // 执行蓝星管理奖金的存储过程
  556. public function calcBsProcedure() {
  557. if( !$this->_isCalcMonth ) {
  558. // 不是结算月,则不进行计算
  559. return false;
  560. }
  561. $result = \Yii::$app->db->createCommand("CALL CalcBlue(:periodNum)")
  562. ->bindValue(':periodNum' , $this->_periodNum )
  563. ->execute();
  564. return $result;
  565. }
  566. // 执行旅游奖的计算
  567. public function calcBonusTourism() {
  568. // 月结,如果不是月结点,则直接退出
  569. if (!$this->_isCalcMonth) {
  570. return true;
  571. }
  572. $bonusConfig = $this->_sysConfig['openTourism'];
  573. // 达标条件:聘级、级别、奖项比例
  574. $config = json_decode($bonusConfig['OPTIONS'], true);
  575. // 奖金总比例
  576. $mate = $bonusConfig['VALUE'] / 100;
  577. // 会员级别
  578. $minDecLevel = $config['OPTIONS']['declarationLevel'] ?? [];
  579. // 月度公司总PV
  580. $monthTotalPV = PerfMonth::find()
  581. ->yearMonth($this->_calcYearMonth)
  582. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  583. ->sum('PV_PCS');
  584. // 用于分发的奖金总数
  585. $transferAmount = $monthTotalPV * $mate;
  586. // 基于蓝星奖结果计算符合获奖条件的会员StarDirector
  587. $userStarDirector = CalcBonusBS::find()
  588. ->yearMonth($this->_calcYearMonth)
  589. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  590. ->select('USER_ID,LEVEL_ID,LAST_DEC_LV,LAST_EMP_LV,LAST_STATUS')
  591. ->groupBy('USER_ID')
  592. ->asArray()
  593. ->all();
  594. $userStarDirectorObj = array_column($userStarDirector, NULL, 'USER_ID');
  595. // 基于团队奖/绩效奖结果计算会员的StarCrown
  596. $userStarCrown = CalcBonusQY::find()
  597. ->yearMonth($this->_calcYearMonth)
  598. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  599. ->select('USER_ID,LAST_CROWN_LV')
  600. ->groupBy('USER_ID')
  601. ->asArray()
  602. ->all();
  603. $userStarCrownObj = array_column($userStarCrown, NULL, 'USER_ID');
  604. // 合并用户ID,去重
  605. $bonusUsers = array_unique(array_merge(array_keys($userStarDirectorObj), array_keys($userStarCrownObj)));
  606. // 奖金点数综合
  607. $bonusPointComplex = 0;
  608. $insertBonusData = [];
  609. foreach($bonusUsers as $userId) {
  610. // 计算奖金:取starDirectorPoint和starCrownPoint的大个值
  611. $starDirectorPoint = $this->_empLevelConfig[$userStarDirectorObj[$userId]['LEVEL_ID']]['TOURISM_PERCENT'] ?? 0;
  612. $starCrownPoint = $this->_starCrownLevelConfig[$userStarCrownObj[$userId]['LAST_CROWN_LV']]['TOURISM_PERCENT'] ?? 0;
  613. // 奖金比例:
  614. $bonusPoint = max($starDirectorPoint, $starCrownPoint);
  615. if ($bonusPoint <= 0) {
  616. continue;
  617. }
  618. $insertBonusData[] = [
  619. 'ID' => SnowFake::instance()->generateId(),
  620. 'USER_ID' => $userId,
  621. 'LAST_DEC_LV' => $userStarDirectorObj[$userId]['LAST_DEC_LV'],
  622. 'LAST_EMP_LV' => $userStarDirectorObj[$userId]['LAST_EMP_LV'],
  623. 'LAST_STATUS' => $userStarDirectorObj[$userId]['LAST_STATUS'],
  624. 'LAST_CROWN_LV' => $userStarCrownObj[$userId]['LAST_CROWN_LV'],
  625. 'AMOUNT_STANDARD' => 0,
  626. 'POINT' => $bonusPoint,
  627. 'PERIOD_NUM' => $this->_periodNum,
  628. 'CALC_YEAR' => $this->_calcYear,
  629. 'CALC_MONTH' => $this->_calcYearMonth,
  630. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  631. 'CREATED_AT' => Date::nowTime(),
  632. 'PERF' => $monthTotalPV,
  633. 'TRANSFER_RATE' => $mate,
  634. 'TRANSFER_AMOUNT' => Tool::formatPrice($transferAmount),
  635. 'CAP_AMOUNT' => 0,
  636. 'POINT_COMPLEX' => 0,
  637. ];
  638. $bonusPointComplex += $bonusPoint;
  639. }
  640. // 数据写入总表
  641. if ($insertBonusData) {
  642. foreach ($insertBonusData as &$bonusData) {
  643. // 计算奖金
  644. $amount = Tool::formatPrice($transferAmount * ($bonusData['POINT'] / $bonusPointComplex));
  645. if ($amount <= 0) {
  646. continue;
  647. }
  648. // 会员级别达到要求才会发放奖金
  649. if ($bonusData['LAST_DEC_LV'] == $minDecLevel) {
  650. // 放入缓存
  651. CalcCache::tourismBonus($bonusData['USER_ID'], $this->_periodNum, $amount);
  652. // 加入月奖的会员
  653. CalcCache::addHasMonthBonusUsers($bonusData['USER_ID'], $this->_periodNum);
  654. }
  655. $bonusData['AMOUNT'] = $amount;
  656. $bonusData['POINT_COMPLEX'] = $bonusPointComplex;
  657. }
  658. CalcBonusTourism::batchInsert($insertBonusData);
  659. }
  660. return true;
  661. }
  662. // 执行房奖的计算
  663. public function calcBonusVilla() {
  664. // 月结,如果不是月结点,则直接退出
  665. if (!$this->_isCalcMonth) {
  666. return true;
  667. }
  668. $bonusConfig = $this->_sysConfig['openVilla'];
  669. // 达标条件:聘级、级别、奖项比例
  670. $config = json_decode($bonusConfig['OPTIONS'], true);
  671. // 奖金总比例
  672. $mate = $bonusConfig['VALUE'] / 100;
  673. // 个人奖金封顶
  674. $capBonus = intval($this->_sysConfig['openVillaCap']['VALUE'] ?? 0);
  675. // 会员级别
  676. $minDecLevel = $config['declarationLevel'] ?? [];
  677. // 月度公司总PV
  678. $monthTotalPV = PerfMonth::find()
  679. ->yearMonth($this->_calcYearMonth)
  680. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  681. ->sum('PV_PCS');
  682. // 用于分发的奖金总数
  683. $transferAmount = $monthTotalPV * $mate;
  684. // 基于团队奖/绩效奖结果计算会员的StarCrown.StarCrown基于周期计算,一个月会产生多次,取月周期中的最高星级
  685. $subQuery = CalcBonusQY::find()
  686. ->yearMonth($this->_calcYearMonth)
  687. ->where('CALC_MONTH = :CALC_MONTH AND LAST_CROWN_LV <> :NO_CROWN_LV', [':CALC_MONTH' => $this->_calcYearMonth, ':NO_CROWN_LV' => StarCrownLevel::NO_LEVEL_ID])
  688. ->select('USER_ID,LAST_DEC_LV,LAST_CROWN_LV,LAST_STATUS,LEVEL_NAME,SORT')
  689. ->joinWith(['starCrown' => function($query) {
  690. $query->select(['LEVEL_NAME', 'SORT']);
  691. }])
  692. ->having(1)
  693. ->orderBy('USER_ID ASC, SORT DESC');
  694. $userStarCrownObj = (new Query())->from(['u' => $subQuery])->select('USER_ID,LAST_DEC_LV,LAST_CROWN_LV,LAST_STATUS,LEVEL_NAME,SORT')->groupBy('USER_ID')->indexBy('USER_ID')->all();
  695. // 奖金点数综合
  696. $bonusPointComplex = 0;
  697. $insertBonusData = [];
  698. foreach($userStarCrownObj as $item) {
  699. // 奖金比例
  700. $bonusPoint = $this->_starCrownLevelConfig[$item['LAST_CROWN_LV']]['VILLA_PERCENT'] ?? 0;
  701. if (!$bonusPoint) {
  702. continue;
  703. }
  704. // 会员级别达到要求才会发放奖金
  705. if ($item['LAST_DEC_LV'] != $minDecLevel) {
  706. continue;
  707. }
  708. $userBaseInfo = CalcCache::getUserInfo($item['USER_ID'], $this->_periodNum);
  709. $insertBonusData[] = [
  710. 'ID' => SnowFake::instance()->generateId(),
  711. 'USER_ID' => $item['USER_ID'],
  712. 'LAST_DEC_LV' => $item['LAST_DEC_LV'] ?? '',
  713. 'LAST_EMP_LV' => $userBaseInfo['LAST_EMP_LV'],
  714. 'LAST_STATUS' => $item['LAST_STATUS'] ?? 0,
  715. 'LAST_CROWN_LV' => $item['LAST_CROWN_LV'] ?? '',
  716. 'AMOUNT' => 0,
  717. 'POINT' => $bonusPoint,
  718. 'PERIOD_NUM' => $this->_periodNum,
  719. 'CALC_YEAR' => $this->_calcYear,
  720. 'CALC_MONTH' => $this->_calcYearMonth,
  721. 'CREATED_AT' => Date::nowTime(),
  722. 'PERF' => $monthTotalPV,
  723. 'TRANSFER_RATE' => $mate,
  724. 'TRANSFER_AMOUNT' => Tool::formatPrice($transferAmount),
  725. 'CAP_AMOUNT' => 0,
  726. 'POINT_COMPLEX' => 0,
  727. ];
  728. $bonusPointComplex += $bonusPoint;
  729. }
  730. // 数据写入总表
  731. if ($insertBonusData) {
  732. // 计算个人奖金
  733. foreach ($insertBonusData as &$bonusData) {
  734. // 计算奖金
  735. $amount = Tool::formatPrice($transferAmount * ($bonusData['POINT'] / $bonusPointComplex));
  736. if ($amount <= 0) {
  737. continue;
  738. }
  739. // 封顶前奖金数
  740. $capAmount = $amount;
  741. // 奖金数不能大于封顶值
  742. $amount = ($amount > $capBonus) ? $capBonus : $amount;
  743. $bonusData['AMOUNT'] = $amount;
  744. $bonusData['CAP_AMOUNT'] = $capAmount;
  745. $bonusData['POINT_COMPLEX'] = $bonusPointComplex;
  746. // 放入缓存
  747. CalcCache::villaBonus($bonusData['USER_ID'], $this->_periodNum, $amount);
  748. // 加入月奖的会员
  749. CalcCache::addHasMonthBonusUsers($bonusData['USER_ID'], $this->_periodNum);
  750. }
  751. CalcBonusVilla::batchInsert($insertBonusData);
  752. }
  753. return true;
  754. }
  755. // 执行车奖的计算
  756. public function calcBonusGarage() {
  757. // 月结,如果不是月结点,则直接退出
  758. if (!$this->_isCalcMonth) {
  759. return true;
  760. }
  761. $bonusConfig = $this->_sysConfig['openGarage'];
  762. // 达标条件:聘级、级别、奖项比例
  763. $config = json_decode($bonusConfig['OPTIONS'], true);
  764. // 奖金总比例
  765. $mate = $bonusConfig['VALUE'] / 100;
  766. // 会员级别
  767. $minDecLevel = $config['declarationLevel'] ?? [];
  768. // 个人奖金封顶
  769. $capBonus = intval($this->_sysConfig['openGarageCap']['VALUE'] ?? 0);
  770. // 月度公司总PV
  771. $monthTotalPV = PerfMonth::find()
  772. ->yearMonth($this->_calcYearMonth)
  773. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  774. ->sum('PV_PCS');
  775. // 用于分发的奖金总数
  776. $transferAmount = $monthTotalPV * $mate;
  777. // 基于蓝星奖结果计算符合获奖条件的会员StarDirector
  778. $userStarDirector = CalcBonusBS::find()
  779. ->yearMonth($this->_calcYearMonth)
  780. ->where('CALC_MONTH = :CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  781. ->select('USER_ID,LEVEL_ID,LAST_DEC_LV,LAST_STATUS')
  782. ->groupBy('USER_ID')
  783. ->asArray()
  784. ->all();
  785. $userStarDirectorObj = array_column($userStarDirector, NULL, 'USER_ID');
  786. // 基于团队奖/绩效奖结果计算会员的StarCrown.StarCrown基于周期计算,一个月会产生多次,取月周期中的最高星级
  787. $subQuery = CalcBonusQY::find()
  788. ->yearMonth($this->_calcYearMonth)
  789. ->where('CALC_MONTH = :CALC_MONTH AND LAST_CROWN_LV <> :NO_CROWN_LV', [':CALC_MONTH' => $this->_calcYearMonth, ':NO_CROWN_LV' => StarCrownLevel::NO_LEVEL_ID])
  790. ->select('USER_ID,LAST_DEC_LV,LAST_CROWN_LV,LAST_STATUS,LEVEL_NAME,SORT')
  791. ->joinWith(['starCrown' => function($query) {
  792. $query->select(['LEVEL_NAME', 'SORT']);
  793. }])
  794. ->having(1)
  795. ->orderBy('USER_ID ASC, SORT DESC');
  796. $userStarCrownObj = (new Query())->from(['u' => $subQuery])->select('USER_ID,LAST_DEC_LV,LAST_CROWN_LV,LAST_STATUS,LEVEL_NAME,SORT')->groupBy('USER_ID')->indexBy('USER_ID')->all();
  797. // 合并用户ID,去重
  798. $bonusUsers = array_unique(array_merge(array_keys($userStarDirectorObj), array_keys($userStarCrownObj)));
  799. sort($bonusUsers);
  800. // 奖金点数综合
  801. $bonusPointComplex = 0;
  802. $insertBonusData = [];
  803. foreach($bonusUsers as $userId) {
  804. // 计算奖金:取starDirectorPoint和starCrownPoint的大个值
  805. $starDirectorPoint = !isset($userStarDirectorObj[$userId]['LEVEL_ID']) ? 0 : ($this->_empLevelConfig[$userStarDirectorObj[$userId]['LEVEL_ID']]['GARAGE_PERCENT'] ?? 0);
  806. $starCrownPoint = !isset($userStarCrownObj[$userId]['LAST_CROWN_LV']) ? 0: ($this->_starCrownLevelConfig[$userStarCrownObj[$userId]['LAST_CROWN_LV']]['GARAGE_PERCENT'] ?? 0);
  807. // 奖金比例:
  808. $bonusPoint = max($starDirectorPoint, $starCrownPoint);
  809. if ($bonusPoint <= 0) {
  810. continue;
  811. }
  812. // 会员级别达到要求才会发放奖金
  813. $lastDecLv = $userStarDirectorObj[$userId]['LAST_DEC_LV'] ?? ($userStarCrownObj[$userId]['LAST_DEC_LV'] ?? '');
  814. if ($lastDecLv != $minDecLevel) {
  815. continue;
  816. }
  817. $insertBonusData[] = [
  818. 'ID' => SnowFake::instance()->generateId(),
  819. 'USER_ID' => $userId,
  820. 'LAST_DEC_LV' => $userStarDirectorObj[$userId]['LAST_DEC_LV'] ?? ($userStarCrownObj[$userId]['LAST_DEC_LV'] ?? ''),
  821. 'LAST_EMP_LV' => $userStarDirectorObj[$userId]['LEVEL_ID'] ?? '',
  822. 'LAST_STATUS' => $userStarDirectorObj[$userId]['LAST_STATUS'] ?? ($userStarCrownObj[$userId]['LAST_STATUS'] ?? 1),
  823. 'LAST_CROWN_LV' => $userStarCrownObj[$userId]['LAST_CROWN_LV'] ?? '',
  824. 'AMOUNT' => 0,
  825. 'POINT' => $bonusPoint,
  826. 'PERIOD_NUM' => $this->_periodNum,
  827. 'CALC_YEAR' => $this->_calcYear,
  828. 'CALC_MONTH' => $this->_calcYearMonth,
  829. 'CREATED_AT' => Date::nowTime(),
  830. 'PERF' => $monthTotalPV,
  831. 'TRANSFER_RATE' => $mate,
  832. 'TRANSFER_AMOUNT' => Tool::formatPrice($transferAmount),
  833. 'CAP_AMOUNT' => 0,
  834. 'POINT_COMPLEX' => 0,
  835. ];
  836. $bonusPointComplex += $bonusPoint;
  837. }
  838. // 数据写入总表
  839. if ($insertBonusData) {
  840. foreach ($insertBonusData as &$bonusData) {
  841. // 计算奖金
  842. $amount = Tool::formatPrice($transferAmount * ($bonusData['POINT'] / $bonusPointComplex));
  843. if ($amount <= 0) {
  844. continue;
  845. }
  846. // 封顶前奖金数
  847. $capAmount = $amount;
  848. // 奖金数不能大于封顶值
  849. $amount = ($amount > $capBonus) ? $capBonus : $amount;
  850. $bonusData['AMOUNT'] = $amount;
  851. $bonusData['CAP_AMOUNT'] = $capAmount;
  852. $bonusData['POINT_COMPLEX'] = $bonusPointComplex;
  853. // 放入缓存
  854. CalcCache::garageBonus($bonusData['USER_ID'], $this->_periodNum, $amount);
  855. // 加入月奖的会员
  856. CalcCache::addHasMonthBonusUsers($bonusData['USER_ID'], $this->_periodNum);
  857. }
  858. CalcBonusGarage::batchInsert($insertBonusData);
  859. }
  860. return true;
  861. }
  862. /**
  863. * 季度奖写用户缓存
  864. *
  865. */
  866. public function calcQuarterUser(int $offset = 0) {
  867. if( !$this->_isCalcMonth || !in_array($this->_calcMonth, [3,6,9,12])) {
  868. // 不是结算月,则不进行计算
  869. return false;
  870. }
  871. $allData = CalcBonusQuarter::finduseDbCalc()
  872. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  873. ->groupBy('USER_ID')
  874. ->offset($offset)
  875. ->limit($this->_limit)
  876. ->asArray()
  877. ->all();
  878. if ($allData){
  879. // 达标条件:会员级别:钻卡
  880. $config = json_decode($this->_sysConfig['openQuarter']['OPTIONS'], true);
  881. $minDecLevel = $config['declarationLevel'] ?? [];
  882. foreach ($allData as $user) {
  883. // 扣除相应的复消积分和管理费
  884. $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS']);
  885. $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金
  886. $manageTax = $deductData['manageTax']; // 管理费
  887. $point = $deductData['reConsumePoints'] + $user['RECONSUME_POINTS'];// 复消积分
  888. // 管理奖钻卡发放
  889. if ($user['LAST_DEC_LV'] == $minDecLevel) {
  890. // 把对碰后的奖金存入缓存中
  891. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_QUARTER', $user['ORI_BONUS'], $deductData);
  892. // 加入月奖的会员
  893. CalcCache::addHasMonthBonusUsers($user['USER_ID'], $this->_periodNum);
  894. }
  895. // 更新奖金存储过程的实发金额数据
  896. CalcBonusQuarter::updateAll([
  897. 'RECONSUME_POINTS' => $point,
  898. 'AMOUNT' => $realBonusBs,
  899. 'MANAGE_TAX' => $manageTax],
  900. 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  901. [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
  902. }
  903. return $this->calcQuarterUser($offset + $this->_limit);
  904. }
  905. unset($allData);
  906. return true;
  907. }
  908. /**
  909. * 蓝星管理奖金未拆分
  910. * @param int $offset
  911. * @return bool
  912. * @throws \yii\db\Exception
  913. */
  914. public function calcBonusBsGL(int $offset = 0) {
  915. if( !$this->_isCalcMonth ) {
  916. // 不是结算月,则不进行计算
  917. return false;
  918. }
  919. // 从缓存获取分页有收入的会员信息
  920. $allData = CalcBonusBS::findUseDbCalc()
  921. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  922. ->groupBy('USER_ID')
  923. ->offset($offset)
  924. ->limit($this->_limit)
  925. ->asArray()
  926. ->all();
  927. if ($allData) {
  928. foreach ($allData as $user) {
  929. // 添加到有奖励信息的数据中
  930. CalcCache::addHasBonusUsers($user['USER_ID'], $this->_periodNum);
  931. $fxStatus = $this->_isMonthPerfLimit($user['USER_ID']);
  932. if ($fxStatus) {
  933. // //扣除相应的复消积分和管理费
  934. // $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS']);
  935. // // 把对碰后的奖金存入缓存中
  936. // CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS', $user['ORI_BONUS'], $deductData);
  937. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS', $user['ORI_BONUS']);
  938. // $realBonusBs = $user['ORI_BONUS']; // 扣除管理费和复消积分后的实发蓝星奖金
  939. // $manageTax = $deductData['manageTax']; // 管理费
  940. // $point = $deductData['reConsumePoints'];// 复消积分
  941. // // 更新蓝星奖金存储过程的实发金额数据
  942. // CalcBonusBS::updateAll(['AMOUNT' => $realBonusBs, 'MANAGE_TAX' => $manageTax, 'RECONSUME_POINTS' => $point],
  943. // 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  944. // [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
  945. }
  946. }
  947. return $this->calcBonusBsGL($offset + $this->_limit);
  948. }
  949. unset($allData);
  950. return true;
  951. }
  952. // /**
  953. // * 蓝星管理奖金
  954. // * @param int $offset
  955. // * @return bool
  956. // * @throws \yii\db\Exception
  957. // */
  958. // public function calcBonusBsGL(int $offset = 0) {
  959. // if( !$this->_isCalcMonth ) {
  960. // // 不是结算月,则不进行计算
  961. // return false;
  962. // }
  963. // // 从缓存获取分页有收入的会员信息
  964. // $allData = CalcBonusBS::findUseDbCalc()
  965. // ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  966. // ->groupBy('USER_ID')
  967. // ->offset($offset)
  968. // ->limit($this->_limit)
  969. // ->asArray()
  970. // ->all();
  971. // if ($allData) {
  972. // // 达标条件:会员级别:钻卡
  973. // $config = json_decode($this->_sysConfig['openGL']['OPTIONS'], true);
  974. // $minDecLevel = $config['mntDec'] ?? [];
  975. // foreach ($allData as $user) {
  976. // //扣除相应的复消积分和管理费
  977. // $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS_MNT']);
  978. // $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金
  979. // $manageTax = $deductData['manageTax']; // 管理费
  980. // $point = $deductData['reConsumePoints'] + $user['RECONSUME_POINTS'];// 复消积分
  981. // // 管理奖钻卡发放
  982. // // if (in_array($user['LAST_DEC_LV'], $minDecLevel)) {
  983. // // 把对碰后的奖金存入缓存中
  984. // CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS_MNT', $user['ORI_BONUS_MNT'], $deductData);
  985. // // 加入月奖的会员
  986. // CalcCache::addHasMonthBonusUsers($user['USER_ID'], $this->_periodNum);
  987. // // }
  988. // // 更新蓝星奖金存储过程的实发金额数据
  989. // CalcBonusBS::updateAll([
  990. // 'RECONSUME_POINTS' => $point,
  991. // 'AMOUNT_MNT' => $realBonusBs,
  992. // 'MANAGE_TAX_MNT' => $manageTax],
  993. // 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  994. // [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
  995. // }
  996. // return $this->calcBonusBsGL($offset + $this->_limit);
  997. // }
  998. // unset($allData);
  999. // return true;
  1000. // }
  1001. // /**
  1002. // * 蓝星业绩奖金
  1003. // * @param int $offset
  1004. // * @return bool
  1005. // * @throws \yii\db\Exception
  1006. // */
  1007. // public function calcBonusBsYJ(int $offset = 0) {
  1008. // if( !$this->_isCalcMonth ) {
  1009. // // 不是结算月,则不进行计算
  1010. // return false;
  1011. // }
  1012. // // 从缓存获取分页有收入的会员信息
  1013. // $allData = CalcBonusBS::findUseDbCalc()
  1014. // ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  1015. // ->groupBy('USER_ID')
  1016. // ->offset($offset)
  1017. // ->limit($this->_limit)
  1018. // ->asArray()
  1019. // ->all();
  1020. // if ($allData) {
  1021. // // 达标条件:会员级别:金卡、钻卡
  1022. // $config = json_decode($this->_sysConfig['openGL']['OPTIONS'], true);
  1023. // $minDecLevel = $config['abbrDec'] ?? [];
  1024. // foreach ($allData as $user) {
  1025. // //扣除相应的复消积分和管理费
  1026. // $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS_ABBR']);
  1027. // $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金
  1028. // $manageTax = $deductData['manageTax']; // 管理费
  1029. // $point = $deductData['reConsumePoints'] + $user['RECONSUME_POINTS'];// 复消积分
  1030. // // 业绩奖金卡、钻卡发放
  1031. // // if (in_array($user['LAST_DEC_LV'], $minDecLevel)) {
  1032. // // 把对碰后的奖金存入缓存中
  1033. // CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS_ABBR', $user['ORI_BONUS_ABBR'], $deductData);
  1034. // // 加入月奖的会员
  1035. // CalcCache::addHasMonthBonusUsers($user['USER_ID'], $this->_periodNum);
  1036. // // }
  1037. // // 更新蓝星业绩奖金存储过程的实发金额数据
  1038. // CalcBonusBS::updateAll([
  1039. // 'AMOUNT_ABBR' => $realBonusBs,
  1040. // 'MANAGE_TAX_ABBR' => $manageTax,
  1041. // 'RECONSUME_POINTS' => $point],
  1042. // 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  1043. // [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
  1044. // }
  1045. // return $this->calcBonusBsYJ($offset + $this->_limit);
  1046. // }
  1047. // unset($allData);
  1048. // return true;
  1049. // }
  1050. /**
  1051. * 对碰
  1052. * @param array $oriPerfArr
  1053. * @param array $perfArr
  1054. * @param $percent
  1055. * @param $loopTimes
  1056. * @return array
  1057. */
  1058. public function touchPerf(array $oriPerfArr, array $perfArr, $percent, $loopTimes=1) {
  1059. $resultArr = $oriPerfArr;
  1060. foreach ($perfArr as $keyT => $perfT) {
  1061. if ($perfT <= 0) {
  1062. unset($perfArr[$keyT]);
  1063. }
  1064. }
  1065. if (count($perfArr) >= 2 && $loopTimes < 6) {
  1066. arsort($perfArr, SORT_NUMERIC);
  1067. $onePerf = null;
  1068. $oneKey = null;
  1069. // $touchSurplusAmount = 0;
  1070. $touchAmount = 0;
  1071. // 前两个进行对碰
  1072. foreach ($perfArr as $key => $perf) {
  1073. if ($onePerf === null) {
  1074. $oneKey = $key;
  1075. $onePerf = $perf;
  1076. } else {
  1077. $touchSurplusAmount = $perf - $onePerf;
  1078. if ($touchSurplusAmount > 0) {
  1079. unset($perfArr[$oneKey]);
  1080. $resultArr['perfArr'][$oneKey] = 0;
  1081. $perfArr[$key] = $touchSurplusAmount;
  1082. $touchAmount = $onePerf;
  1083. } elseif ($touchSurplusAmount < 0) {
  1084. unset($perfArr[$key]);
  1085. $resultArr['perfArr'][$key] = 0;
  1086. $perfArr[$oneKey] = abs($touchSurplusAmount);
  1087. $touchAmount = $perf;
  1088. } else {
  1089. unset($perfArr[$oneKey], $perfArr[$key]);
  1090. $resultArr['perfArr'][$oneKey] = 0;
  1091. $resultArr['perfArr'][$key] = 0;
  1092. $touchAmount = $perf;
  1093. }
  1094. break;
  1095. }
  1096. }
  1097. $touchBonus = Tool::formatPrice($touchAmount * $percent);
  1098. $resultArr['touchBonus'] += $touchBonus;
  1099. foreach ($perfArr as $keyR => $perfR) {
  1100. $resultArr['perfArr'][$keyR] = $perfR;
  1101. }
  1102. return $this->touchPerf($resultArr, $perfArr, $percent, $loopTimes+1);
  1103. }
  1104. return $resultArr;
  1105. }
  1106. /**
  1107. * 循环父级并执行回调函数
  1108. * @param $userId
  1109. * @param callable $callbackFunc
  1110. * @param int $offset
  1111. * @return bool
  1112. */
  1113. public function loopNetworkParentDo($userId, callable $callbackFunc, int $offset = 0) {
  1114. $allParents = Cache::getAllNetworkParents($userId);
  1115. $allData = array_slice($allParents, $offset, $this->_limit);
  1116. unset($allParents);
  1117. if ($allData) {
  1118. foreach ($allData as $data) {
  1119. $funcResult = $callbackFunc($data);
  1120. if ($funcResult === self::LOOP_FINISH) {
  1121. return true;
  1122. } elseif ($funcResult === self::LOOP_CONTINUE) {
  1123. continue;
  1124. }
  1125. unset($data, $funcResult);
  1126. }
  1127. unset($allData);
  1128. return $this->loopNetworkParentDo($userId, $callbackFunc, $offset + $this->_limit);
  1129. }
  1130. return true;
  1131. }
  1132. /**
  1133. * 循环推荐网络的父级
  1134. * @param $userId
  1135. * @param callable $callbackFunc
  1136. * @param int $offset
  1137. * @return bool
  1138. */
  1139. public function loopRelationParentDo($userId, callable $callbackFunc, int $offset = 0) {
  1140. $allParents = Cache::getAllRelationParents($userId);
  1141. $allData = array_slice($allParents, $offset, $this->_limit);
  1142. unset($allParents);
  1143. if ($allData) {
  1144. foreach ($allData as $data) {
  1145. $funcResult = $callbackFunc($data);
  1146. if ($funcResult === self::LOOP_FINISH) {
  1147. return true;
  1148. } elseif ($funcResult === self::LOOP_CONTINUE) {
  1149. continue;
  1150. }
  1151. unset($data, $funcResult);
  1152. }
  1153. unset($allData);
  1154. return $this->loopRelationParentDo($userId, $callbackFunc, $offset + $this->_limit);
  1155. }
  1156. return true;
  1157. }
  1158. /**
  1159. * 按级别的收入上限
  1160. * 新的需求调整: 改成不按月进行限制,并且去掉vip奖
  1161. * @param $bonus
  1162. * @param $userId
  1163. * @param $declarationLevel
  1164. * @return float
  1165. */
  1166. public function declarationLevelCap($bonus, $userId, $declarationLevel) {
  1167. $decLevelConfig = $this->_decLevelConfig;
  1168. $nowDecLevelConfig = $decLevelConfig[$declarationLevel];
  1169. unset($decLevelConfig);
  1170. $maxGetBonus = $nowDecLevelConfig['INCOME_CAP'];
  1171. if( $bonus <= $maxGetBonus) {
  1172. return $bonus;
  1173. }else {
  1174. return $maxGetBonus;
  1175. }
  1176. }
  1177. /**
  1178. * 扣除复消积分和管理费
  1179. * @param $userId
  1180. * @param $bonus
  1181. * @return array
  1182. * @throws \yii\db\Exception
  1183. */
  1184. public function deduct($userId, $bonus) {
  1185. //判断是否达到了本月扣除复消的上限
  1186. $cacheData = CalcCache::monthLastPeriodReconsumePoints($userId, $this->_periodNum, $this->_calcYearMonth);
  1187. $bonusCache = CalcCache::bonus($userId, $this->_periodNum);
  1188. $reConsumePointsTotal = $bonusCache['RECONSUME_POINTS'] + $cacheData['RECONSUME_POINTS_SUM'];
  1189. $reConsumePointsCap = $this->_sysConfig['reConsumePointsMonthCap']['VALUE'];
  1190. unset($cacheData, $bonusCache);
  1191. $reConsumePoints = 0;
  1192. if( $reConsumePointsTotal < $reConsumePointsCap ) {
  1193. $reConsumePoints = $bonus * $this->_sysConfig['reConsumePointsPercent']['VALUE'] / 100;
  1194. $reConsumePoints = min($reConsumePoints, $reConsumePointsCap-$reConsumePointsTotal);
  1195. }
  1196. unset($reConsumePointsTotal, $reConsumePointsCap);
  1197. $manageTax = $bonus * $this->_sysConfig['manageTaxPercent']['VALUE'] / 100;
  1198. $surplus = $bonus - $reConsumePoints - $manageTax;
  1199. return [
  1200. 'reConsumePoints' => Tool::formatPrice($reConsumePoints),//复效积分
  1201. 'manageTax' => Tool::formatPrice($manageTax),//管理费
  1202. 'surplus' => Tool::formatPrice($surplus),//真实奖金
  1203. ];
  1204. }
  1205. /**
  1206. * 更新百分比并发送
  1207. * @param $percent
  1208. */
  1209. private function _updatePercent($percent) {
  1210. // 把数据写入数据库中
  1211. Period::updateAll(['CALC_PERCENT' => $percent], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  1212. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin($percent, ['MODEL' => 'PERIOD', 'ID' => $this->_periodId, 'FIELD' => 'CALC_PERCENT']);
  1213. }
  1214. /**
  1215. * 循环奖服务奖金会员,并入库
  1216. * @param int $offset
  1217. * @return bool
  1218. * @throws \yii\db\Exception
  1219. */
  1220. public function loopBonusUsers($offset = 0) {
  1221. echo sprintf("时间:[%s]缓存奖金数据入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1222. // 从缓存列表里面从底层往上倒序获取会员
  1223. $allData = CalcCache::getHasBonusUsers($this->_periodNum, $offset, $this->_limit);
  1224. if($allData){
  1225. $insertDataBonus = [];
  1226. foreach($allData as $userId){
  1227. $tempBonusData = $this->bonusData($userId);
  1228. if(!empty($tempBonusData['result'])){
  1229. $insertDataBonus[] = $tempBonusData['result'];
  1230. }
  1231. unset($userId, $tempBonusData);
  1232. }
  1233. CalcBonus::batchInsert($insertDataBonus);
  1234. unset($insertDataBonus, $allData);
  1235. return $this->loopBonusUsers($offset + $this->_limit);
  1236. }
  1237. return true;
  1238. }
  1239. /**
  1240. * 奖金
  1241. * @param $userId
  1242. * @return array
  1243. * @throws \yii\db\Exception
  1244. */
  1245. public function bonusData($userId) {
  1246. // 从缓存中获取用户的奖金
  1247. $bonus = CalcCache::bonus($userId, $this->_periodNum);
  1248. $baseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1249. $perfData = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
  1250. $tourismBonus = CalcCache::tourismBonus($userId, $this->_periodNum);
  1251. $garageBonus = CalcCache::garageBonus($userId, $this->_periodNum);
  1252. $villaBonus = CalcCache::villaBonus($userId, $this->_periodNum);
  1253. $pervSurplusPerf = CalcCache::surplusPerf($userId, $this->_periodNum);
  1254. // 星级
  1255. $starCrownLv = CalcCache::getUserStarCrown($userId, $this->_periodNum);
  1256. //没有共享和管理奖
  1257. $bonusReal = $bonus['BONUS_BD'] + $bonus['BONUS_TG'] + $bonus['BONUS_XF'] + $bonus['BONUS_YJ'] +
  1258. $bonus['BONUS_QY'] + $bonus['BONUS_YC'] + $bonus['BONUS_YC_EXTRA'] + $bonus['BONUS_VIP'] +
  1259. $bonus['BONUS_BS_MNT'] + $bonus['BONUS_BS_ABBR'] + $bonus['BONUS_QUARTER'];
  1260. $realBonusGx = 0;
  1261. $realBonusGl = 0;
  1262. $realBonusBs = 0; // 蓝星管理奖. BlueStar
  1263. $blueStartOriBonus = 0;
  1264. $blueStartManageTax = 0;
  1265. $exchangePoints = 0; // 蓝星奖管理奖. 产生的兑换积分
  1266. $realBonusBsMnt = 0; // 蓝星管理奖——实发奖金
  1267. $blueStartOriBonusMnt = 0; // 蓝星管理奖——原奖金
  1268. $blueStartManageTaxMnt = 0; // 蓝星管理奖——管理费
  1269. $realBonusBsAbbr = 0; // 蓝星业绩奖——实发奖金
  1270. $blueStartOriBonusAbbr = 0; // 蓝星业绩奖——原奖金
  1271. $blueStartManageTaxAbbr = 0; // 蓝星业绩奖——管理费
  1272. if( $this->_isCalcMonth ) {
  1273. // 个人月消费PV大于配置值,才会计算发放蓝星奖
  1274. $fxPvStatus = $this->_isMonthPerfLimit($userId);
  1275. // BONUS_REAL 字段是发到用户的真实奖金
  1276. if ( $fxPvStatus ) {
  1277. $userBS = CalcBonusBS::find()
  1278. ->where(
  1279. 'PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID',
  1280. [
  1281. ':PERIOD_NUM' => $this->_periodNum,
  1282. ':USER_ID' => $userId
  1283. ]
  1284. )
  1285. ->select('AMOUNT,ORI_BONUS,MANAGE_TAX,LEVEL_ID,PRODUCT_POINT,AMOUNT_MNT,ORI_BONUS_MNT,MANAGE_TAX_MNT,AMOUNT_ABBR,ORI_BONUS_ABBR,MANAGE_TAX_ABBR')
  1286. ->limit(1)
  1287. ->orderBy('CREATED_AT DESC')
  1288. ->asArray()
  1289. ->all();
  1290. $userBS = is_array($userBS) ? reset($userBS) : [];
  1291. $blueStartAmount = isset($userBS['AMOUNT']) && !empty($userBS['AMOUNT']) ? $userBS['AMOUNT'] : 0; // 奖金
  1292. $blueStartOriBonus = isset($userBS['ORI_BONUS']) && !empty($userBS['ORI_BONUS']) ? $userBS['ORI_BONUS'] : 0; // 原奖金
  1293. $blueStartManageTax = isset($userBS['MANAGE_TAX']) && !empty($userBS['MANAGE_TAX']) ? $userBS['MANAGE_TAX'] : 0; // 管理费
  1294. $realBonusBsMnt = $userBS['AMOUNT_MNT'] ?? 0; // 蓝星管理奖. 实发奖金
  1295. $blueStartOriBonusMnt = $userBS['ORI_BONUS_MNT'] ?? 0; // 蓝星管理奖. 原奖金
  1296. $blueStartManageTaxMnt = $userBS['MANAGE_TAX_MNT'] ?? 0; // 蓝星管理奖. 管理费
  1297. $realBonusBsAbbr = $userBS['AMOUNT_ABBR'] ?? 0; // 蓝星业绩奖. 奖金
  1298. $blueStartOriBonusAbbr = $userBS['ORI_BONUS_ABBR'] ?? 0; // 蓝星业绩奖. 原奖金
  1299. $blueStartManageTaxAbbr = $userBS['MANAGE_TAX_ABBR'] ?? 0; // 蓝星业绩奖. 管理费
  1300. $blueStartManageTax += $blueStartManageTaxMnt + $blueStartManageTaxAbbr; // 管理费
  1301. $realBonusBs = $blueStartAmount; // 蓝星奖直接取数据库中算好的值PRODUCT_POINT
  1302. $exchangePoints = isset($userBS['PRODUCT_POINT']) && !empty($userBS['PRODUCT_POINT']) ? $userBS['PRODUCT_POINT'] : 0; // 兑换积分
  1303. }
  1304. }
  1305. if( $this->_isCalcMonth ) { //季度奖
  1306. if(in_array($this->_calcMonth, [3,6,9,12])){ // 季度奖
  1307. }
  1308. }
  1309. $result = [
  1310. 'USER_ID' => $userId,
  1311. 'LAST_USER_NAME' => $baseInfo['USER_NAME'],
  1312. 'LAST_REAL_NAME' => $baseInfo['REAL_NAME'],
  1313. 'LAST_DEC_LV' => $baseInfo['DEC_LV'],
  1314. 'LAST_EMP_LV' => $baseInfo['LAST_EMP_LV'],
  1315. 'LAST_CROWN_LV' => $starCrownLv ?? StarCrownLevel::getDefaultLevelId(),
  1316. 'LAST_STATUS' => $baseInfo['STATUS'],
  1317. 'LAST_REC_USER_NAME' => $baseInfo['REC_USER_NAME'],
  1318. 'LAST_REC_REAL_NAME' => $baseInfo['REC_REAL_NAME'],
  1319. 'LAST_CON_USER_NAME' => $baseInfo['CON_USER_NAME'],
  1320. 'LAST_CON_REAL_NAME' => $baseInfo['CON_REAL_NAME'],
  1321. 'EXCHANGE_POINTS' => $exchangePoints, // 兑换积分
  1322. // 'LAST_LOCATION' => $baseInfo['LOCATION'] ? $baseInfo['LOCATION'] : 1,
  1323. //@todo
  1324. 'LAST_LOCATION' => 1,
  1325. 'BONUS_BD' => $bonus['BONUS_BD'],
  1326. 'BONUS_TG' => $bonus['BONUS_TG'],
  1327. 'BONUS_QY' => $bonus['BONUS_QY'],
  1328. 'RECONSUME_POINTS' => $bonus['RECONSUME_POINTS'],
  1329. 'MANAGE_TAX' => $blueStartManageTax, // 管理费
  1330. 'BONUS_INCOME'=>$bonus['INCOME_TOTAL'],
  1331. 'BONUS_REAL'=> $bonusReal,
  1332. 'BONUS_TOTAL'=>$bonus['BONUS_TOTAL'],
  1333. 'ORI_BONUS_BD' => $bonus['ORI_BONUS_BD'],
  1334. 'ORI_BONUS_TG' => $bonus['ORI_BONUS_TG'],
  1335. 'BONUS_BS' => $realBonusBs, // 新的管理奖金,即蓝星管理奖
  1336. 'ORI_BONUS_BS' => $blueStartOriBonus, // 蓝星管理奖金原奖金,即包含管理费
  1337. 'REAL_BONUS_BS' => $realBonusBs, // 实发蓝星管理奖金
  1338. 'BONUS_BS_MNT' => $realBonusBsMnt, // 蓝星管理奖
  1339. 'ORI_BONUS_BS_MNT' => $blueStartOriBonusMnt, // 蓝星管理奖金原奖金,即包含管理费
  1340. 'REAL_BONUS_BS_MNT' => $realBonusBsMnt, // 实发蓝星管理奖金
  1341. 'MANAGE_TAX_MNT' => $blueStartManageTaxMnt, // 实发蓝星管理——管理费
  1342. 'BONUS_BS_ABBR' => $realBonusBsAbbr, // 蓝星业绩奖
  1343. 'ORI_BONUS_BS_ABBR' => $blueStartOriBonusAbbr, // 蓝星业绩奖金原奖金,即包含管理费
  1344. 'REAL_BONUS_BS_ABBR' => $realBonusBsAbbr, // 实发蓝星业绩奖金
  1345. 'MANAGE_TAX_ABBR' => $blueStartManageTaxAbbr, // 实发蓝星业绩奖——管理费
  1346. 'ORI_BONUS_QY' => $bonus['ORI_BONUS_QY'],
  1347. 'ORI_CAPPED_BONUS_QY' => $bonus['ORI_CAPPED_BONUS_QY'], // 团队奖封顶前的奖金
  1348. 'BONUS_QUARTER' => $bonus['BONUS_QUARTER'],
  1349. 'ORI_BONUS_QUARTER' => $bonus['ORI_BONUS_QUARTER'],
  1350. 'BONUS_TOURISM' => $tourismBonus, // 旅游奖
  1351. 'BONUS_VILLA' => $villaBonus, // 房奖
  1352. 'BONUS_GARAGE' => $garageBonus, // 车奖
  1353. //以下没有用
  1354. 'PV_1L' => $perfData['PV_1L_TOUCH'],//TOUCH为碰业绩
  1355. 'QY_1L' => $perfData['PV_1L_TOUCH'] + $pervSurplusPerf['SURPLUS_1L'],
  1356. 'SURPLUS_1L' => $perfData['SURPLUS_1L'],
  1357. 'PV_2L' => $perfData['PV_2L_TOUCH'],
  1358. 'QY_2L' => $perfData['PV_2L_TOUCH'] + $pervSurplusPerf['SURPLUS_2L'],
  1359. 'SURPLUS_2L' => $perfData['SURPLUS_2L'],
  1360. 'PV_3L' => $perfData['PV_3L_TOUCH'],
  1361. 'QY_3L' => $perfData['PV_3L_TOUCH'] + $pervSurplusPerf['SURPLUS_3L'],
  1362. 'SURPLUS_3L' => $perfData['SURPLUS_3L'],
  1363. 'PV_4L' => $perfData['PV_4L_TOUCH'],
  1364. 'QY_4L' => $perfData['PV_4L_TOUCH'] + $pervSurplusPerf['SURPLUS_4L'],
  1365. 'SURPLUS_4L' => $perfData['SURPLUS_4L'],
  1366. 'PV_5L' => $perfData['PV_5L_TOUCH'],
  1367. 'QY_5L' => $perfData['PV_5L_TOUCH'] + $pervSurplusPerf['SURPLUS_5L'],
  1368. 'SURPLUS_5L' => $perfData['SURPLUS_5L'],
  1369. 'PV_PCS' => $perfData['PV_PCS'],
  1370. 'PV_TOUCH' => Tool::formatPrice($perfData['PV_1L_TOUCH'] + $perfData['PV_2L_TOUCH'] + $perfData['PV_3L_TOUCH'] + $perfData['PV_4L_TOUCH'] + $perfData['PV_5L_TOUCH'] + $perfData['PV_LS_TOUCH']),
  1371. 'PERIOD_NUM' => $this->_periodNum,
  1372. 'CALC_YEAR' => $this->_calcYear,
  1373. 'CALC_MONTH' => $this->_calcYearMonth,
  1374. 'CREATED_AT' => Date::nowTime(),
  1375. ];
  1376. $resend = [];
  1377. unset($bonus, $realBonusGx, $realBonusGl, $bonusReal);
  1378. return ['result'=>$result,'resend'=>$resend];
  1379. }
  1380. // 判断是否满足月最低消费
  1381. public function _isMonthPerfLimit($userId) {
  1382. $userMonthTotal = PerfMonth::find()->where(
  1383. 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH',
  1384. ['USER_ID'=>$userId, 'CALC_MONTH'=>$this->_calcYearMonth]
  1385. )
  1386. ->asArray()
  1387. ->one();
  1388. $fxPvStatus = false;
  1389. if (isset($userMonthTotal['PV_PCS']) && $userMonthTotal['PV_PCS'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE']) {
  1390. $fxPvStatus = true;
  1391. }
  1392. return $fxPvStatus;
  1393. }
  1394. }