CalcServeBonusCalc.php 62 KB

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