CalcServeBonusCalc.php 61 KB

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