CalcServeBonusCalc.php 73 KB

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