CalcServeBonusCalc.php 60 KB

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