CalcServeBonusCalc.php 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  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. 'AMOUNT' => $recBonus,
  337. 'ORI_BONUS' => $recBonus,
  338. 'PERIOD_NUM' => $this->_periodNum,
  339. 'CALC_YEAR' => $this->_calcYear,
  340. 'CALC_MONTH' => $this->_calcYearMonth,
  341. 'CREATED_AT' => Date::nowTime(),
  342. 'LOGS' => json_encode([
  343. 'perfPv' => $perfPv,
  344. 'recPercentConfig' => $this->_sysConfig['recPercent']['VALUE'],
  345. 'decLevel' => $userBaseInfo['DEC_LV'],
  346. ]),
  347. ];
  348. unset($perfData, $perfPv, $perfUserInfo, $recBonus, $bonusUserId, $userBaseInfo, $userId, $deductData, $fromUserInfo);
  349. }
  350. CalcBonusTG::batchInsert($insertBonusData);
  351. unset($allData, $insertBonusData);
  352. return $this->calcBonusTG($offset + $this->_limit);
  353. }
  354. unset($allData);
  355. return true;
  356. }
  357. /**
  358. * 服务奖第一步
  359. * @param int $offset
  360. * @return bool
  361. * @throws \yii\db\Exception
  362. */
  363. public function calcBonusBDStepOne(int $offset = 0) {
  364. echo sprintf("时间:[%s]服务奖第【1】步,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  365. $periodNum = $this->_periodNum;
  366. // 从缓存获取分页有业绩的会员信息
  367. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  368. if ($allData) {
  369. $insertBonusData = [];
  370. foreach ($allData as $userId) {
  371. // 从缓存中获取会员的业绩信息
  372. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  373. if( !$perfData ) continue;
  374. $decRoleBonusFrom = explode(',', $this->_sysConfig['decRoleBonusFrom']['VALUE']);
  375. $validPvPcs = 0;
  376. foreach ($decRoleBonusFrom as $orderType) {
  377. $orderTypeName = sprintf('PV_PCS_%s', $orderType);
  378. $orderTypeValue = $perfData[$orderTypeName] ?? 0;
  379. $coefficientName = self::ORDER_TYPE_TO_FW_COEFFICIENT[$orderType];
  380. $coefficient = $this->_sysConfig[$coefficientName]['VALUE'] ?? 1;
  381. $validPvPcs += $orderTypeValue * $coefficient;
  382. unset($orderType, $orderTypeName, $orderTypeValue, $coefficientName, $coefficient);
  383. }
  384. unset($perfData, $decRoleBonusFrom);
  385. if ( $validPvPcs <= 0 ) continue;
  386. $this->loopRelationParentDo($userId, function ($parent) use($userId, $validPvPcs){
  387. try {
  388. //判断parent的报单中心级别 和 服务奖比例
  389. $bonusUserId = $parent['PARENT_UID'];
  390. //计算级别之后更新过userInfo的缓存,缓存中级别发生了变化
  391. $bonusUserInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  392. $isDec = $bonusUserInfo['IS_DEC'];
  393. if($isDec == 0) return self::LOOP_CONTINUE;
  394. $decRoleId = $bonusUserInfo['DEC_ROLE_ID'];
  395. if( !$decRoleId ) return self::LOOP_CONTINUE;
  396. if( !isset($this->_decRoleConfig[$decRoleId]) ) return self::LOOP_CONTINUE;
  397. $parentDecRoleLevel = $this->_decRoleConfig[$decRoleId];
  398. $parentFwBonusPercent = $parentDecRoleLevel['FW_BONUS_PERCENT'] ?? 0;
  399. $cacheMaxPercent = CalcCache::fwMaxBonusPercent($userId, $this->_periodNum);
  400. $diffPercent = $parentFwBonusPercent - $cacheMaxPercent;
  401. if( $diffPercent <= 0 ) return self::LOOP_CONTINUE;
  402. $fwBonus = $validPvPcs * $diffPercent / 100;
  403. if( $fwBonus <= 0 ) return self::LOOP_CONTINUE;
  404. //给本人添加服务奖比例
  405. CalcCache::fwMaxBonusPercent($userId, $this->_periodNum, $parentFwBonusPercent);
  406. //记录奖金和奖金来源到缓存 并实现在缓存中奖金累加
  407. CalcCache::saveFwBonusList($bonusUserId, $this->_periodNum, $fwBonus, ['fromUid'=>$userId, 'fromPvPcs'=>$validPvPcs]);
  408. CalcCache::addHasFwBonusUsers($bonusUserId, $this->_periodNum);
  409. } catch(Exception $e) {
  410. var_dump($e->getMessage(), '------------');
  411. }
  412. unset($bonusUserId, $bonusUserInfo, $isDec, $decRoleId, $parentDecRoleLevel, $parentFwBonusPercent, $cacheMaxPercent, $diffPercent, $fwBonus);
  413. });
  414. unset($userId, $validPvPcs);
  415. }
  416. unset($allData, $insertBonusData);
  417. return $this->calcBonusBDStepOne($offset + $this->_limit);
  418. }
  419. unset($allData);
  420. return true;
  421. }
  422. /**
  423. * 服务奖第二步
  424. * @param int $offset
  425. * @return bool
  426. * @throws \yii\db\Exception
  427. */
  428. public function calcBonusBDStepTwo(int $offset = 0) {
  429. echo sprintf("时间:[%s]服务奖第【2】步,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  430. $allData = CalcCache::getHasFwBonusUsers($this->_periodNum, $offset, $this->_limit);
  431. if ($allData) {
  432. $insertBonusData = [];
  433. foreach ($allData as $userId) {
  434. $fwBonusData = CalcCache::getFwBonusList($userId, $this->_periodNum);
  435. if( !$fwBonusData ) continue;
  436. $fwBonus = $fwBonusData['fwBonus'] ?? 0;
  437. if( $fwBonus <=0 ) continue;
  438. //总金额限制
  439. try {
  440. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  441. CalcCache::bonus($userId, $this->_periodNum, 'BONUS_BD', $fwBonus);
  442. $decRoleId = $userBaseInfo['DEC_ROLE_ID'];
  443. $insertBonusData[] = [
  444. 'ID' => SnowFake::instance()->generateId(),
  445. 'USER_ID' => $userId,
  446. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  447. 'LAST_EMP_LV' => $userBaseInfo['LAST_EMP_LV'],
  448. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  449. 'FROM_USER_ID' => $userId,
  450. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  451. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  452. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  453. 'AMOUNT' => $fwBonus,
  454. 'ORI_BONUS' => $fwBonus,
  455. 'RECONSUME_POINTS' => 0,
  456. 'MANAGE_TAX' => 0,
  457. 'PERIOD_NUM' => $this->_periodNum,
  458. 'CALC_YEAR' => $this->_calcYear,
  459. 'CALC_MONTH' => $this->_calcYearMonth,
  460. 'CREATED_AT' => Date::nowTime(),
  461. 'LOGS' => json_encode([
  462. 'decRoleId' => $decRoleId,
  463. ])
  464. ];
  465. } catch(Exception $e) {
  466. var_dump('---->>>', $e->getMessage());exit;
  467. }
  468. unset($userId, $fwBonusData, $userBaseInfo, $decRoleId, $fwBonus);
  469. }
  470. CalcBonusBD::batchInsert($insertBonusData);
  471. unset($insertBonusData, $allData);
  472. $this->calcBonusBDStepTwo($offset + $this->_limit);
  473. }
  474. unset($allData);
  475. return true;
  476. }
  477. /**
  478. * 团队奖
  479. * @param int $offset
  480. * @return bool
  481. * @throws \yii\db\Exception
  482. */
  483. public function calcBonusQY(int $offset = 0) {
  484. echo sprintf("时间:[%s]团队奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  485. $periodNum = $this->_periodNum;
  486. // 从缓存获取分页有业绩的会员信息
  487. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  488. if ($allData) {
  489. $insertBonusData = [];
  490. foreach ($allData as $userId) {
  491. // 从缓存中获取会员的业绩信息
  492. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  493. // 从缓存中获取会员的上期结余业绩信息
  494. $pervSurplusPerf = CalcCache::surplusPerf($userId, $periodNum);
  495. // 本期 + 上期结余
  496. $perfArr = [
  497. 'SURPLUS_1L' => $perfData['PV_1L_TOUCH'] + $pervSurplusPerf['SURPLUS_1L'],
  498. 'SURPLUS_2L' => $perfData['PV_2L_TOUCH'] + $pervSurplusPerf['SURPLUS_2L'],
  499. 'SURPLUS_3L' => $perfData['PV_3L_TOUCH'] + $pervSurplusPerf['SURPLUS_3L'],
  500. 'SURPLUS_4L' => $perfData['PV_4L_TOUCH'] + $pervSurplusPerf['SURPLUS_4L'],
  501. 'SURPLUS_5L' => $perfData['PV_5L_TOUCH'] + $pervSurplusPerf['SURPLUS_5L'],
  502. ];
  503. $oriPerfArr = [
  504. 'perfArr' => $perfArr,
  505. 'touchBonus' => 0,
  506. ];
  507. // 获取会员的报单级别
  508. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  509. $decLevelConfig = $this->_decLevelConfig;
  510. $nowDecLevelConfig = $decLevelConfig[$userBaseInfo['DEC_LV']];
  511. // 对碰
  512. $touchBonusArr = $this->touchPerf($oriPerfArr, $perfArr, $nowDecLevelConfig['QY_PERCENT']/100);
  513. $touchPerfArr = [];
  514. foreach ($touchBonusArr['perfArr'] as $keyR => $perfR) {
  515. $touchPerfArr[$keyR] = $perfR;
  516. }
  517. // 对碰完成后把结余的业绩存入本期业绩缓存中
  518. CalcCache::nowPeriodPerf($userId, $periodNum, $touchPerfArr);
  519. //更新数据库
  520. PerfPeriod::updateAll($touchPerfArr, 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  521. 'USER_ID' => $userId,
  522. 'PERIOD_NUM' => $periodNum,
  523. ]);
  524. if ($touchBonusArr['touchBonus'] <= 0) continue;
  525. $teamBonus = $touchBonusArr['touchBonus'];
  526. $capBonusQy = $teamBonus; // 封顶前的奖金
  527. //判断级别上限,个人奖金封顶限制
  528. $teamBonus = $this->declarationLevelCap($teamBonus, $userId, $userBaseInfo['DEC_LV']);
  529. if( $teamBonus <= 0 ) continue;
  530. // 将封顶前的金额加入用户奖金缓存中,此金额不能发放(总奖金,总实际奖金)
  531. CalcCache::bonus($userId, $periodNum, 'CAPPED_BONUS_QY', $capBonusQy);
  532. // TODO:取小腿值
  533. $payLeg = min([$perfArr['SURPLUS_1L'], $perfArr['SURPLUS_2L']]);
  534. // 计算荣衔星级
  535. $starCrown = StarCrownLevel::getStarCrown($payLeg);
  536. // 是否活跃
  537. $isActive = $this->_isPerfActive($userId);
  538. $oriBonus = $isActive ? $teamBonus : 0;
  539. $lastCrownLv = $isActive ? $starCrown['ID'] : StarCrownLevel::getDefaultLevelId();
  540. //团队奖流水
  541. $insertBonusData[] = [
  542. 'ID' => SnowFake::instance()->generateId(),
  543. 'USER_ID' => $userId,
  544. 'ORI_CAPPED_BONUS_QY' => $capBonusQy,
  545. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  546. 'LAST_EMP_LV' => $userBaseInfo['LAST_EMP_LV'],
  547. 'LAST_CROWN_LV' => $lastCrownLv,
  548. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  549. 'AMOUNT' => $oriBonus,
  550. 'ORI_BONUS' => $oriBonus,
  551. 'PERIOD_NUM' => $this->_periodNum,
  552. 'CALC_YEAR' => $this->_calcYear,
  553. 'CALC_MONTH' => $this->_calcYearMonth,
  554. 'CREATED_AT' => Date::nowTime(),
  555. 'LOGS' => json_encode([
  556. 'perfArr' => $perfArr,
  557. 'touchPerfArrOri' => $touchBonusArr['perfArr'],
  558. 'touchPerfArr' => $touchPerfArr,
  559. 'nowDecLevelConfig' => $nowDecLevelConfig,
  560. 'decLevel' => $userBaseInfo['DEC_LV'],
  561. ]),
  562. 'IS_ACTIVE' => (int)$isActive,
  563. 'HOPE_CROWN_LV' => $starCrown['ID'],
  564. 'HOPE_BONUS' => $teamBonus,
  565. ];
  566. // 星级放入缓存
  567. CalcCache::addUserStarCrown($userId, $periodNum, $lastCrownLv);
  568. if ($oriBonus > 0) {
  569. // 把对碰后的奖金存入缓存中
  570. // CalcCache::bonus($userId, $periodNum, 'BONUS_QY', $oriBonus, $deductData);
  571. CalcCache::bonus($userId, $periodNum, 'BONUS_QY', $teamBonus);
  572. }
  573. unset($perfData, $pervSurplusPerf, $perfArr, $oriPerfArr, $touchPerfArr, $userBaseInfo, $decLevelConfig, $touchBonusArr, $userId, $nowDecLevelConfig, $teamBonus, $deductData);
  574. }
  575. CalcBonusQY::batchInsert($insertBonusData);
  576. unset($allData, $insertBonusData);
  577. return $this->calcBonusQY($offset + $this->_limit);
  578. }
  579. unset($allData);
  580. return true;
  581. }
  582. /**
  583. * 季度奖计算
  584. */
  585. public function calcQuarter() {
  586. if( !$this->_isCalcMonth || !in_array($this->_calcMonth, [3,6,9,12])) {
  587. // echo('不是季结点,进这里,不计算季度奖'. PHP_EOL);
  588. return false;
  589. }
  590. $result = \Yii::$app->db->createCommand("CALL QtrCalc(:periodNum)")
  591. ->bindValue(':periodNum' , $this->_periodNum )
  592. ->execute();
  593. return $result;
  594. }
  595. // 执行蓝星管理奖金的存储过程
  596. public function calcBsProcedure() {
  597. if( !$this->_isCalcMonth ) {
  598. // 不是结算月,则不进行计算
  599. return false;
  600. }
  601. $result = \Yii::$app->db->createCommand("CALL CalcBlue(:periodNum)")
  602. ->bindValue(':periodNum' , $this->_periodNum )
  603. ->execute();
  604. return $result;
  605. }
  606. // 执行旅游奖的计算
  607. public function calcBonusTourism() {
  608. // 月结,如果不是月结点,则直接退出
  609. if (!$this->_isCalcMonth) {
  610. return true;
  611. }
  612. $bonusConfig = $this->_sysConfig['openTourism'];
  613. // 达标条件:聘级、级别、奖项比例
  614. $config = json_decode($bonusConfig['OPTIONS'], true);
  615. // 奖金总比例
  616. $mate = $bonusConfig['VALUE'] / 100;
  617. // 会员级别
  618. $minDecLevel = $config['OPTIONS']['declarationLevel'] ?? [];
  619. // 月度公司总PV
  620. $monthTotalPV = PerfMonth::find()
  621. ->yearMonth($this->_calcYearMonth)
  622. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  623. ->sum('PV_PCS');
  624. // 用于分发的奖金总数
  625. $transferAmount = $monthTotalPV * $mate;
  626. // 基于蓝星奖结果计算符合获奖条件的会员StarDirector
  627. $userStarDirector = CalcBonusBS::find()
  628. ->yearMonth($this->_calcYearMonth)
  629. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  630. ->select('USER_ID,LEVEL_ID,LAST_DEC_LV,LAST_EMP_LV,LAST_STATUS')
  631. ->groupBy('USER_ID')
  632. ->asArray()
  633. ->all();
  634. $userStarDirectorObj = array_column($userStarDirector, NULL, 'USER_ID');
  635. // 基于团队奖/绩效奖结果计算会员的StarCrown
  636. $userStarCrown = CalcBonusQY::find()
  637. ->yearMonth($this->_calcYearMonth)
  638. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  639. ->select('USER_ID,LAST_CROWN_LV')
  640. ->groupBy('USER_ID')
  641. ->asArray()
  642. ->all();
  643. $userStarCrownObj = array_column($userStarCrown, NULL, 'USER_ID');
  644. // 合并用户ID,去重
  645. $bonusUsers = array_unique(array_merge(array_keys($userStarDirectorObj), array_keys($userStarCrownObj)));
  646. // 奖金点数综合
  647. $bonusPointComplex = 0;
  648. $insertBonusData = [];
  649. foreach($bonusUsers as $userId) {
  650. // 计算奖金:取starDirectorPoint和starCrownPoint的大个值
  651. $starDirectorPoint = $this->_empLevelConfig[$userStarDirectorObj[$userId]['LEVEL_ID']]['TOURISM_PERCENT'] ?? 0;
  652. $starCrownPoint = $this->_starCrownLevelConfig[$userStarCrownObj[$userId]['LAST_CROWN_LV']]['TOURISM_PERCENT'] ?? 0;
  653. // 奖金比例:
  654. $bonusPoint = max($starDirectorPoint, $starCrownPoint);
  655. if ($bonusPoint <= 0) {
  656. continue;
  657. }
  658. $insertBonusData[] = [
  659. 'ID' => SnowFake::instance()->generateId(),
  660. 'USER_ID' => $userId,
  661. 'LAST_DEC_LV' => $userStarDirectorObj[$userId]['LAST_DEC_LV'],
  662. 'LAST_EMP_LV' => $userStarDirectorObj[$userId]['LEVEL_ID'],
  663. 'LAST_STATUS' => $userStarDirectorObj[$userId]['LAST_STATUS'],
  664. 'LAST_CROWN_LV' => $userStarCrownObj[$userId]['LAST_CROWN_LV'],
  665. 'AMOUNT_STANDARD' => 0,
  666. 'POINT' => $bonusPoint,
  667. 'PERIOD_NUM' => $this->_periodNum,
  668. 'CALC_YEAR' => $this->_calcYear,
  669. 'CALC_MONTH' => $this->_calcYearMonth,
  670. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  671. 'CREATED_AT' => Date::nowTime(),
  672. 'PERF' => $monthTotalPV,
  673. 'TRANSFER_RATE' => $mate,
  674. 'TRANSFER_AMOUNT' => Tool::formatPrice($transferAmount),
  675. 'CAP_AMOUNT' => 0,
  676. 'POINT_COMPLEX' => 0,
  677. ];
  678. $bonusPointComplex += $bonusPoint;
  679. }
  680. // 数据写入总表
  681. if ($insertBonusData) {
  682. foreach ($insertBonusData as &$bonusData) {
  683. // 计算奖金
  684. $amount = Tool::formatPrice($transferAmount * ($bonusData['POINT'] / $bonusPointComplex));
  685. if ($amount <= 0) {
  686. continue;
  687. }
  688. // 会员级别达到要求才会发放奖金
  689. if ($bonusData['LAST_DEC_LV'] == $minDecLevel) {
  690. // 放入缓存
  691. CalcCache::tourismBonus($bonusData['USER_ID'], $this->_periodNum, $amount);
  692. }
  693. $bonusData['AMOUNT'] = $amount;
  694. $bonusData['POINT_COMPLEX'] = $bonusPointComplex;
  695. }
  696. CalcBonusTourism::batchInsert($insertBonusData);
  697. }
  698. return true;
  699. }
  700. // 执行房奖的计算
  701. public function calcBonusVilla() {
  702. if (!$this->_isCalcMonth) {
  703. return true;
  704. }
  705. $bonusConfig = $this->_sysConfig['openVilla'];
  706. // 达标条件:聘级、级别、奖项比例
  707. $config = json_decode($bonusConfig['OPTIONS'], true);
  708. // 奖金总比例
  709. $mate = $bonusConfig['VALUE'] / 100;
  710. // 个人奖金封顶
  711. $capBonus = intval($this->_sysConfig['openVillaCap']['VALUE'] ?? 0);
  712. // 会员级别
  713. $minDecLevel = $config['declarationLevel'] ?? [];
  714. // 月度公司总PV
  715. $monthTotalPV = PerfMonth::find()
  716. ->yearMonth($this->_calcYearMonth)
  717. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  718. ->sum('PV_PCS');
  719. // 用于分发的奖金总数
  720. $transferAmount = $monthTotalPV * $mate;
  721. // 基于团队奖/绩效奖结果计算会员的StarCrown.StarCrown基于周期计算,一个月会产生多次,取月周期中的最高星级
  722. $subQuery = CalcBonusQY::find()
  723. ->yearMonth($this->_calcYearMonth)
  724. ->where('CALC_MONTH = :CALC_MONTH AND LAST_CROWN_LV <> :NO_CROWN_LV', [':CALC_MONTH' => $this->_calcYearMonth, ':NO_CROWN_LV' => StarCrownLevel::NO_LEVEL_ID])
  725. ->select('USER_ID,LAST_DEC_LV,LAST_CROWN_LV,LAST_STATUS,LEVEL_NAME,SORT')
  726. ->joinWith(['starCrown' => function($query) {
  727. $query->select(['LEVEL_NAME', 'SORT']);
  728. }])
  729. ->having(1)
  730. ->orderBy('USER_ID ASC, SORT DESC');
  731. $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();
  732. // 奖金点数综合
  733. $bonusPointComplex = 0;
  734. $insertBonusData = [];
  735. foreach($userStarCrownObj as $item) {
  736. // 奖金比例
  737. $bonusPoint = $this->_starCrownLevelConfig[$item['LAST_CROWN_LV']]['VILLA_PERCENT'] ?? 0;
  738. if (!$bonusPoint) {
  739. continue;
  740. }
  741. // 会员级别达到要求才会发放奖金
  742. if ($item['LAST_DEC_LV'] != $minDecLevel) {
  743. continue;
  744. }
  745. $userBaseInfo = CalcCache::getUserInfo($item['USER_ID'], $this->_periodNum);
  746. $insertBonusData[] = [
  747. 'ID' => SnowFake::instance()->generateId(),
  748. 'USER_ID' => $item['USER_ID'],
  749. 'LAST_DEC_LV' => $item['LAST_DEC_LV'] ?? '',
  750. 'LAST_EMP_LV' => $userBaseInfo['LAST_EMP_LV'],
  751. 'LAST_STATUS' => $item['LAST_STATUS'] ?? 0,
  752. 'LAST_CROWN_LV' => $item['LAST_CROWN_LV'] ?? '',
  753. 'AMOUNT' => 0,
  754. 'POINT' => $bonusPoint,
  755. 'PERIOD_NUM' => $this->_periodNum,
  756. 'CALC_YEAR' => $this->_calcYear,
  757. 'CALC_MONTH' => $this->_calcYearMonth,
  758. 'CREATED_AT' => Date::nowTime(),
  759. 'PERF' => $monthTotalPV,
  760. 'TRANSFER_RATE' => $mate,
  761. 'TRANSFER_AMOUNT' => Tool::formatPrice($transferAmount),
  762. 'CAP_AMOUNT' => 0,
  763. 'POINT_COMPLEX' => 0,
  764. ];
  765. $bonusPointComplex += $bonusPoint;
  766. }
  767. // 数据写入总表
  768. if ($insertBonusData) {
  769. // 计算个人奖金
  770. foreach ($insertBonusData as &$bonusData) {
  771. // 计算奖金
  772. $amount = Tool::formatPrice($transferAmount * ($bonusData['POINT'] / $bonusPointComplex));
  773. if ($amount <= 0) {
  774. continue;
  775. }
  776. // 封顶前奖金数
  777. $capAmount = $amount;
  778. // 奖金数不能大于封顶值
  779. $amount = ($amount > $capBonus) ? $capBonus : $amount;
  780. $bonusData['AMOUNT'] = $amount;
  781. $bonusData['CAP_AMOUNT'] = $capAmount;
  782. $bonusData['POINT_COMPLEX'] = $bonusPointComplex;
  783. // 放入缓存
  784. CalcCache::villaBonus($bonusData['USER_ID'], $this->_periodNum, $amount);
  785. }
  786. CalcBonusVilla::batchInsert($insertBonusData);
  787. }
  788. return true;
  789. }
  790. // 执行车奖的计算
  791. public function calcBonusGarage() {
  792. // 月结,如果不是月结点,则直接退出
  793. if (!$this->_isCalcMonth) {
  794. return true;
  795. }
  796. $bonusConfig = $this->_sysConfig['openGarage'];
  797. // 达标条件:聘级、级别、奖项比例
  798. $config = json_decode($bonusConfig['OPTIONS'], true);
  799. // 奖金总比例
  800. $mate = $bonusConfig['VALUE'] / 100;
  801. // 会员级别
  802. $minDecLevel = $config['declarationLevel'] ?? [];
  803. // 个人奖金封顶
  804. $capBonus = intval($this->_sysConfig['openGarageCap']['VALUE'] ?? 0);
  805. // 月度公司总PV
  806. $monthTotalPV = PerfMonth::find()
  807. ->yearMonth($this->_calcYearMonth)
  808. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  809. ->sum('PV_PCS');
  810. // 用于分发的奖金总数
  811. $transferAmount = $monthTotalPV * $mate;
  812. // 基于蓝星奖结果计算符合获奖条件的会员StarDirector
  813. $userStarDirector = CalcBonusBS::find()
  814. ->yearMonth($this->_calcYearMonth)
  815. ->where('CALC_MONTH = :CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  816. ->select('USER_ID,LEVEL_ID,LAST_DEC_LV,LAST_STATUS')
  817. ->groupBy('USER_ID')
  818. ->asArray()
  819. ->all();
  820. $userStarDirectorObj = array_column($userStarDirector, NULL, 'USER_ID');
  821. // 基于团队奖/绩效奖结果计算会员的StarCrown.StarCrown基于周期计算,一个月会产生多次,取月周期中的最高星级
  822. $subQuery = CalcBonusQY::find()
  823. ->yearMonth($this->_calcYearMonth)
  824. ->where('CALC_MONTH = :CALC_MONTH AND LAST_CROWN_LV <> :NO_CROWN_LV', [':CALC_MONTH' => $this->_calcYearMonth, ':NO_CROWN_LV' => StarCrownLevel::NO_LEVEL_ID])
  825. ->select('USER_ID,LAST_DEC_LV,LAST_CROWN_LV,LAST_STATUS,LEVEL_NAME,SORT')
  826. ->joinWith(['starCrown' => function($query) {
  827. $query->select(['LEVEL_NAME', 'SORT']);
  828. }])
  829. ->having(1)
  830. ->orderBy('USER_ID ASC, SORT DESC');
  831. $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();
  832. // 合并用户ID,去重
  833. $bonusUsers = array_unique(array_merge(array_keys($userStarDirectorObj), array_keys($userStarCrownObj)));
  834. sort($bonusUsers);
  835. // 奖金点数综合
  836. $bonusPointComplex = 0;
  837. $insertBonusData = [];
  838. foreach($bonusUsers as $userId) {
  839. // 计算奖金:取starDirectorPoint和starCrownPoint的大个值
  840. $starDirectorPoint = !isset($userStarDirectorObj[$userId]['LEVEL_ID']) ? 0 : ($this->_empLevelConfig[$userStarDirectorObj[$userId]['LEVEL_ID']]['GARAGE_PERCENT'] ?? 0);
  841. $starCrownPoint = !isset($userStarCrownObj[$userId]['LAST_CROWN_LV']) ? 0: ($this->_starCrownLevelConfig[$userStarCrownObj[$userId]['LAST_CROWN_LV']]['GARAGE_PERCENT'] ?? 0);
  842. // 奖金比例:
  843. $bonusPoint = max($starDirectorPoint, $starCrownPoint);
  844. if ($bonusPoint <= 0) {
  845. continue;
  846. }
  847. // 会员级别达到要求才会发放奖金
  848. $lastDecLv = $userStarDirectorObj[$userId]['LAST_DEC_LV'] ?? ($userStarCrownObj[$userId]['LAST_DEC_LV'] ?? '');
  849. if ($lastDecLv != $minDecLevel) {
  850. continue;
  851. }
  852. $insertBonusData[] = [
  853. 'ID' => SnowFake::instance()->generateId(),
  854. 'USER_ID' => $userId,
  855. 'LAST_DEC_LV' => $userStarDirectorObj[$userId]['LAST_DEC_LV'] ?? ($userStarCrownObj[$userId]['LAST_DEC_LV'] ?? ''),
  856. 'LAST_EMP_LV' => $userStarDirectorObj[$userId]['LEVEL_ID'] ?? '',
  857. 'LAST_STATUS' => $userStarDirectorObj[$userId]['LAST_STATUS'] ?? ($userStarCrownObj[$userId]['LAST_STATUS'] ?? 1),
  858. 'LAST_CROWN_LV' => $userStarCrownObj[$userId]['LAST_CROWN_LV'] ?? '',
  859. 'AMOUNT' => 0,
  860. 'POINT' => $bonusPoint,
  861. 'PERIOD_NUM' => $this->_periodNum,
  862. 'CALC_YEAR' => $this->_calcYear,
  863. 'CALC_MONTH' => $this->_calcYearMonth,
  864. 'CREATED_AT' => Date::nowTime(),
  865. 'PERF' => $monthTotalPV,
  866. 'TRANSFER_RATE' => $mate,
  867. 'TRANSFER_AMOUNT' => Tool::formatPrice($transferAmount),
  868. 'CAP_AMOUNT' => 0,
  869. 'POINT_COMPLEX' => 0,
  870. ];
  871. $bonusPointComplex += $bonusPoint;
  872. }
  873. // 数据写入总表
  874. if ($insertBonusData) {
  875. foreach ($insertBonusData as &$bonusData) {
  876. // 计算奖金
  877. $amount = Tool::formatPrice($transferAmount * ($bonusData['POINT'] / $bonusPointComplex));
  878. if ($amount <= 0) {
  879. continue;
  880. }
  881. // 封顶前奖金数
  882. $capAmount = $amount;
  883. // 奖金数不能大于封顶值
  884. $amount = ($amount > $capBonus) ? $capBonus : $amount;
  885. $bonusData['AMOUNT'] = $amount;
  886. $bonusData['CAP_AMOUNT'] = $capAmount;
  887. $bonusData['POINT_COMPLEX'] = $bonusPointComplex;
  888. // 放入缓存
  889. CalcCache::garageBonus($bonusData['USER_ID'], $this->_periodNum, $amount);
  890. }
  891. CalcBonusGarage::batchInsert($insertBonusData);
  892. }
  893. return true;
  894. }
  895. /**
  896. * 季度奖写用户缓存
  897. */
  898. public function calcQuarterUser(int $offset = 0) {
  899. if( !$this->_isCalcMonth || !in_array($this->_calcMonth, [3,6,9,12])) {
  900. // 不是结算月,则不进行计算
  901. return false;
  902. }
  903. try{
  904. $allData = CalcBonusQuarter::finduseDbCalc()
  905. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  906. ->groupBy('USER_ID')
  907. ->offset($offset)
  908. ->limit($this->_limit)
  909. ->asArray()
  910. ->all();
  911. if ($allData){
  912. // 达标条件:会员级别:钻卡
  913. $config = json_decode($this->_sysConfig['openQuarter']['OPTIONS'], true);
  914. $minDecLevel = $config['declarationLevel'] ?? [];
  915. foreach ($allData as $user) {
  916. // 管理奖钻卡发放
  917. if ($user['LAST_DEC_LV'] == $minDecLevel) {
  918. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_QUARTER', $user['ORI_BONUS']);
  919. }
  920. }
  921. return $this->calcQuarterUser($offset + $this->_limit);
  922. }
  923. } catch(Exception $e) {
  924. var_dump($e->getMessage(),'--------------------');
  925. }
  926. unset($allData);
  927. return true;
  928. }
  929. /**
  930. * 蓝星管理奖金未拆分
  931. */
  932. public function calcBonusBsGL(int $offset = 0) {
  933. if( !$this->_isCalcMonth ) {
  934. // 不是结算月,则不进行计算
  935. return false;
  936. }
  937. // 从缓存获取分页有收入的会员信息
  938. $allData = CalcBonusBS::findUseDbCalc()
  939. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  940. ->groupBy('USER_ID')
  941. ->offset($offset)
  942. ->limit($this->_limit)
  943. ->asArray()
  944. ->all();
  945. if ($allData) {
  946. foreach ($allData as $user) {
  947. // 是否活跃会员
  948. $isActive = $this->_isPerfActive($user['USER_ID']);
  949. $oriBonus = $isActive ? $user['ORI_BONUS'] : 0;
  950. $lastEmpLv = $isActive ? $user['LEVEL_ID'] : EmployLevel::getDefaultLevelId();
  951. if ($oriBonus > 0) {
  952. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS', $user['ORI_BONUS']);
  953. }
  954. // 如果不活跃,则不发放奖金,积分,级别
  955. // 更新蓝星奖金存储过程的实发金额数据
  956. CalcBonusBS::updateAll(
  957. [
  958. 'IS_ACTIVE' => (int)$isActive,
  959. 'HOPE_EMP_LV' => $user['LEVEL_ID'],
  960. 'LEVEL_ID' => $lastEmpLv,
  961. ],
  962. 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  963. [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
  964. // 修改用户缓存中记录的用户的最新级别
  965. $userInfo = CalcCache::getUserInfo($user['USER_ID'], $this->_periodNum);
  966. $userInfo['LAST_EMP_LV'] = $lastEmpLv;
  967. CalcCache::setUserInfo($user['USER_ID'], $this->_periodNum, $userInfo);
  968. }
  969. return $this->calcBonusBsGL($offset + $this->_limit);
  970. }
  971. unset($allData);
  972. return true;
  973. }
  974. /**
  975. * 对碰
  976. */
  977. public function touchPerf(array $oriPerfArr, array $perfArr, $percent, $loopTimes=1) {
  978. $resultArr = $oriPerfArr;
  979. foreach ($perfArr as $keyT => $perfT) {
  980. if ($perfT <= 0) {
  981. unset($perfArr[$keyT]);
  982. }
  983. }
  984. if (count($perfArr) >= 2 && $loopTimes < 6) {
  985. arsort($perfArr, SORT_NUMERIC);
  986. $onePerf = null;
  987. $oneKey = null;
  988. // $touchSurplusAmount = 0;
  989. $touchAmount = 0;
  990. // 前两个进行对碰
  991. foreach ($perfArr as $key => $perf) {
  992. if ($onePerf === null) {
  993. $oneKey = $key;
  994. $onePerf = $perf;
  995. } else {
  996. $touchSurplusAmount = $perf - $onePerf;
  997. if ($touchSurplusAmount > 0) {
  998. unset($perfArr[$oneKey]);
  999. $resultArr['perfArr'][$oneKey] = 0;
  1000. $perfArr[$key] = $touchSurplusAmount;
  1001. $touchAmount = $onePerf;
  1002. } elseif ($touchSurplusAmount < 0) {
  1003. unset($perfArr[$key]);
  1004. $resultArr['perfArr'][$key] = 0;
  1005. $perfArr[$oneKey] = abs($touchSurplusAmount);
  1006. $touchAmount = $perf;
  1007. } else {
  1008. unset($perfArr[$oneKey], $perfArr[$key]);
  1009. $resultArr['perfArr'][$oneKey] = 0;
  1010. $resultArr['perfArr'][$key] = 0;
  1011. $touchAmount = $perf;
  1012. }
  1013. break;
  1014. }
  1015. }
  1016. $touchBonus = Tool::formatPrice($touchAmount * $percent);
  1017. $resultArr['touchBonus'] += $touchBonus;
  1018. foreach ($perfArr as $keyR => $perfR) {
  1019. $resultArr['perfArr'][$keyR] = $perfR;
  1020. }
  1021. return $this->touchPerf($resultArr, $perfArr, $percent, $loopTimes+1);
  1022. }
  1023. return $resultArr;
  1024. }
  1025. /**
  1026. * 循环父级并执行回调函数
  1027. * @param $userId
  1028. * @param callable $callbackFunc
  1029. * @param int $offset
  1030. * @return bool
  1031. */
  1032. public function loopNetworkParentDo($userId, callable $callbackFunc, int $offset = 0) {
  1033. $allParents = Cache::getAllNetworkParents($userId);
  1034. $allData = array_slice($allParents, $offset, $this->_limit);
  1035. unset($allParents);
  1036. if ($allData) {
  1037. foreach ($allData as $data) {
  1038. $funcResult = $callbackFunc($data);
  1039. if ($funcResult === self::LOOP_FINISH) {
  1040. return true;
  1041. } elseif ($funcResult === self::LOOP_CONTINUE) {
  1042. continue;
  1043. }
  1044. unset($data, $funcResult);
  1045. }
  1046. unset($allData);
  1047. return $this->loopNetworkParentDo($userId, $callbackFunc, $offset + $this->_limit);
  1048. }
  1049. return true;
  1050. }
  1051. /**
  1052. * 循环推荐网络的父级
  1053. * @param $userId
  1054. * @param callable $callbackFunc
  1055. * @param int $offset
  1056. * @return bool
  1057. */
  1058. public function loopRelationParentDo($userId, callable $callbackFunc, int $offset = 0) {
  1059. $allParents = Cache::getAllRelationParents($userId);
  1060. $allData = array_slice($allParents, $offset, $this->_limit);
  1061. unset($allParents);
  1062. if ($allData) {
  1063. foreach ($allData as $data) {
  1064. $funcResult = $callbackFunc($data);
  1065. if ($funcResult === self::LOOP_FINISH) {
  1066. return true;
  1067. } elseif ($funcResult === self::LOOP_CONTINUE) {
  1068. continue;
  1069. }
  1070. unset($data, $funcResult);
  1071. }
  1072. unset($allData);
  1073. return $this->loopRelationParentDo($userId, $callbackFunc, $offset + $this->_limit);
  1074. }
  1075. return true;
  1076. }
  1077. /**
  1078. * 按级别的收入上限
  1079. * 新的需求调整: 改成不按月进行限制,并且去掉vip奖
  1080. * @param $bonus
  1081. * @param $userId
  1082. * @param $declarationLevel
  1083. * @return float
  1084. */
  1085. public function declarationLevelCap($bonus, $userId, $declarationLevel) {
  1086. $decLevelConfig = $this->_decLevelConfig;
  1087. $nowDecLevelConfig = $decLevelConfig[$declarationLevel];
  1088. unset($decLevelConfig);
  1089. $maxGetBonus = $nowDecLevelConfig['INCOME_CAP'];
  1090. if( $bonus <= $maxGetBonus) {
  1091. return $bonus;
  1092. }else {
  1093. return $maxGetBonus;
  1094. }
  1095. }
  1096. /**
  1097. * 扣除管理费 返回管理费
  1098. */
  1099. public function deductManageTax($bonus) {
  1100. return $bonus * $this->_sysConfig['manageTaxPercent']['VALUE'] / 100;
  1101. }
  1102. // 扣除复消积分 返回应扣除的复消费积分
  1103. public function deductReconsumePonits($userId, $bonus) {
  1104. //判断是否达到了本月扣除复消的上限
  1105. $cacheData = CalcCache::monthLastPeriodReconsumePoints($userId, $this->_periodNum, $this->_calcYearMonth);
  1106. $bonusCache = CalcCache::bonus($userId, $this->_periodNum);
  1107. $lastPeriodPoints = '0.00';
  1108. if (!empty($cacheData)) {
  1109. $lastPeriodPoints = $cacheData['RECONSUME_POINTS_SUM'];
  1110. }
  1111. $reConsumePointsTotal = $bonusCache['RECONSUME_POINTS'] + $lastPeriodPoints;
  1112. $reConsumePointsCap = $this->_sysConfig['reConsumePointsMonthCap']['VALUE'];
  1113. unset($cacheData, $bonusCache, $lastPeriodPoints);
  1114. $reConsumePoints = 0;
  1115. if( $reConsumePointsTotal < $reConsumePointsCap ) {
  1116. $reConsumePoints = $bonus * $this->_sysConfig['reConsumePointsPercent']['VALUE'] / 100;
  1117. $reConsumePoints = min($reConsumePoints, $reConsumePointsCap-$reConsumePointsTotal);
  1118. }
  1119. unset($reConsumePointsTotal, $reConsumePointsCap);
  1120. return $reConsumePoints;
  1121. }
  1122. /**
  1123. * 更新百分比并发送
  1124. * @param $percent
  1125. */
  1126. private function _updatePercent($percent) {
  1127. // 把数据写入数据库中
  1128. Period::updateAll(['CALC_PERCENT' => $percent], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  1129. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin($percent, ['MODEL' => 'PERIOD', 'ID' => $this->_periodId, 'FIELD' => 'CALC_PERCENT']);
  1130. }
  1131. /**
  1132. * 循环奖服务奖金会员,并入库
  1133. */
  1134. public function loopBonusUsers($offset = 0) {
  1135. echo sprintf("时间:[%s]缓存奖金数据入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1136. // 从缓存列表里面从底层往上倒序获取会员
  1137. $allData = CalcCache::getHasBonusUsers($this->_periodNum, $offset, $this->_limit);
  1138. if($allData){
  1139. $insertDataBonus = [];
  1140. foreach($allData as $userId){
  1141. try {
  1142. $tempBonusData = $this->bonusData($userId);
  1143. if(!empty($tempBonusData['result'])){
  1144. $insertDataBonus[] = $tempBonusData['result'];
  1145. }
  1146. }catch(Exception $e) {
  1147. var_dump($e->getMessage(),'ssssssssssssssss',$userId);
  1148. }
  1149. unset($userId, $tempBonusData);
  1150. }
  1151. CalcBonus::batchInsert($insertDataBonus);
  1152. unset($insertDataBonus, $allData);
  1153. return $this->loopBonusUsers($offset + $this->_limit);
  1154. }
  1155. return true;
  1156. }
  1157. /**
  1158. * 奖金入库
  1159. */
  1160. public function bonusData($userId) {
  1161. // 车奖和房奖是积分,不参加到奖金累计中.季度奖季度发放,无最低消费限制
  1162. try{
  1163. $bonus = CalcCache::bonus($userId, $this->_periodNum);
  1164. $baseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1165. $perfData = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
  1166. $tourismBonus = CalcCache::tourismBonus($userId, $this->_periodNum);
  1167. $garageBonus = CalcCache::garageBonus($userId, $this->_periodNum);
  1168. $villaBonus = CalcCache::villaBonus($userId, $this->_periodNum);
  1169. $pervSurplusPerf = CalcCache::surplusPerf($userId, $this->_periodNum);
  1170. $starCrownLv = CalcCache::getUserStarCrown($userId, $this->_periodNum);// 星级
  1171. $bonusReal = '0.000'; // 总实发
  1172. $deductManageTaxItems = Config::DEDUCT_MANAGE_TAX; // 扣除管理费的列表
  1173. $deductReconsumePointsItems = Config::DEDUCT_RECONSUME_POINTS;// 扣除复消积分的列表
  1174. $totalReconsumePointSum = $totalManageSum = '0.000'; // 需要扣除的奖金项,扣除完之后的实发金额
  1175. $deductManageTax = '0.000'; // 扣除的管理费
  1176. $deductReconsumePoints = '0.000'; // 扣除的复消积分
  1177. foreach($deductManageTaxItems as $v) {
  1178. $totalManageSum+= $bonus[$v]; // 需要扣除管理费的累计之和
  1179. }
  1180. foreach($deductReconsumePointsItems as $v) {
  1181. $totalReconsumePointSum+= $bonus[$v]; // 需要扣除复消积分的累计之和
  1182. }
  1183. // 扣除管理费
  1184. if ($totalManageSum > 0) {
  1185. $deductManageTax = $this->deductManageTax($totalManageSum);
  1186. }
  1187. // 扣除复消积分
  1188. if ($totalReconsumePointSum > 0) {
  1189. $deductReconsumePoints = $this->deductReconsumePonits($userId, $totalReconsumePointSum);
  1190. }
  1191. // 总实发=总原奖金-扣除的总管理费-总复消积分
  1192. $bonusReal = $bonus['BONUS_TOTAL'] - $deductManageTax - $deductReconsumePoints;
  1193. // NG系统,不发放兑换积分
  1194. // 因为是最后统一发奖金,每个奖金得实发是和原奖金一样得.只不过要扣除管理费和复消积分.
  1195. // 当然,ng现在不扣除管理费和复消积分.所以都是0
  1196. $result = [
  1197. 'USER_ID' => $userId,
  1198. 'LAST_USER_NAME' => $baseInfo['USER_NAME'],
  1199. 'LAST_REAL_NAME' => $baseInfo['REAL_NAME'],
  1200. 'LAST_DEC_LV' => $baseInfo['DEC_LV'],
  1201. 'LAST_EMP_LV' => $baseInfo['LAST_EMP_LV'],
  1202. 'LAST_CROWN_LV' => $starCrownLv ?? StarCrownLevel::getDefaultLevelId(),
  1203. 'RECONSUME_POINTS' => $deductReconsumePoints,
  1204. 'MANAGE_TAX' => $deductManageTax, // 管理费
  1205. 'BONUS_REAL'=> $bonusReal,
  1206. 'BONUS_TOTAL'=>$bonus['BONUS_TOTAL'],
  1207. 'ORI_BONUS_BD' => $bonus['ORI_BONUS_BD'],
  1208. 'ORI_BONUS_TG' => $bonus['ORI_BONUS_TG'],
  1209. 'ORI_BONUS_BS' => $bonus['ORI_BONUS_BS'], // 蓝星管理奖金原奖金
  1210. 'ORI_BONUS_QY' => $bonus['ORI_BONUS_QY'],
  1211. 'ORI_CAPPED_BONUS_QY' => $bonus['ORI_CAPPED_BONUS_QY'], // 团队奖封顶前的奖金
  1212. 'ORI_BONUS_QUARTER' => $bonus['ORI_BONUS_QUARTER'],
  1213. 'ORI_BONUS_TOURISM' => $tourismBonus, // 旅游奖
  1214. 'ORI_BONUS_VILLA' => $villaBonus, // 房奖
  1215. 'ORI_BONUS_GARAGE' => $garageBonus, // 车奖
  1216. 'PV_1L' => $perfData['PV_1L_TOUCH'],//TOUCH为碰业绩
  1217. 'QY_1L' => $perfData['PV_1L_TOUCH'] + $pervSurplusPerf['SURPLUS_1L'],
  1218. 'SURPLUS_1L' => $perfData['SURPLUS_1L'],
  1219. 'PV_2L' => $perfData['PV_2L_TOUCH'],
  1220. 'QY_2L' => $perfData['PV_2L_TOUCH'] + $pervSurplusPerf['SURPLUS_2L'],
  1221. 'SURPLUS_2L' => $perfData['SURPLUS_2L'],
  1222. 'PV_3L' => $perfData['PV_3L_TOUCH'],
  1223. 'QY_3L' => $perfData['PV_3L_TOUCH'] + $pervSurplusPerf['SURPLUS_3L'],
  1224. 'SURPLUS_3L' => $perfData['SURPLUS_3L'],
  1225. 'PV_4L' => $perfData['PV_4L_TOUCH'],
  1226. 'QY_4L' => $perfData['PV_4L_TOUCH'] + $pervSurplusPerf['SURPLUS_4L'],
  1227. 'SURPLUS_4L' => $perfData['SURPLUS_4L'],
  1228. 'PV_5L' => $perfData['PV_5L_TOUCH'],
  1229. 'QY_5L' => $perfData['PV_5L_TOUCH'] + $pervSurplusPerf['SURPLUS_5L'],
  1230. 'SURPLUS_5L' => $perfData['SURPLUS_5L'],
  1231. 'PV_PCS' => $perfData['PV_PCS'],
  1232. 'PV_TOUCH' => Tool::formatPrice($perfData['PV_1L_TOUCH'] + $perfData['PV_2L_TOUCH'] + $perfData['PV_3L_TOUCH'] + $perfData['PV_4L_TOUCH'] + $perfData['PV_5L_TOUCH']),
  1233. 'PERIOD_NUM' => $this->_periodNum,
  1234. 'CALC_YEAR' => $this->_calcYear,
  1235. 'CALC_MONTH' => $this->_calcYearMonth,
  1236. 'CREATED_AT' => Date::nowTime(),
  1237. ];
  1238. } catch(Exception $e) {
  1239. var_dump($e->getMessage(),'-----------------------------------------------',$userId);
  1240. }
  1241. $resend = [];
  1242. unset($bonus, $bonusReal);
  1243. return ['result'=>$result,'resend'=>$resend];
  1244. }
  1245. // 判断是否满足月最低消费
  1246. public function _isMonthPerfLimit($userId) {
  1247. $userMonthTotal = PerfMonth::find()->where(
  1248. 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH',
  1249. ['USER_ID'=>$userId, 'CALC_MONTH'=>$this->_calcYearMonth]
  1250. )
  1251. ->asArray()
  1252. ->one();
  1253. $fxPvStatus = false;
  1254. if (isset($userMonthTotal['PV_PCS']) && $userMonthTotal['PV_PCS'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE']) {
  1255. $fxPvStatus = true;
  1256. }
  1257. return $fxPvStatus;
  1258. }
  1259. // 判断会员是否活跃
  1260. public function _isPerfActive($userId): bool
  1261. {
  1262. $pv = PerfPeriod::find()->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH AND PERIOD_NUM<=:PERIOD_NUM',
  1263. ['USER_ID'=>$userId, 'CALC_MONTH'=>$this->_calcYearMonth, 'PERIOD_NUM'=>$this->_periodNum])
  1264. ->SUM('PV_PCS');
  1265. return $pv >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE'];
  1266. }
  1267. }