CalcServeBonusCalc.php 77 KB

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