CalcServeBonusCalc.php 61 KB

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