CalcServeBonusCalc.php 71 KB

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