CalcServeBonusCalc.php 62 KB

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