CalcServeBonusCalc.php 69 KB

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