PerfCalc.php 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: liyunlong
  5. * Date: 2019-01-11
  6. * Time: 15:27
  7. */
  8. namespace common\helpers\bonus;
  9. use common\helpers\Cache;
  10. use common\helpers\Date;
  11. use common\helpers\Form;
  12. use common\helpers\snowflake\SnowFake;
  13. use common\helpers\Tool;
  14. use common\helpers\user\Info;
  15. use common\helpers\user\Reconsume;
  16. use common\models\CalcBonus;
  17. use common\models\DeclarationLevel;
  18. use common\models\forms\DeclarationForm;
  19. use common\models\forms\OrderForm;
  20. use common\models\Order;
  21. use common\models\OrderDec;
  22. use common\models\OrderShop;
  23. use common\models\OrderStandard;
  24. use common\models\PerfCompany;
  25. use common\models\PerfMonth;
  26. use common\models\PerfOrder;
  27. use common\models\PerfPeriod;
  28. use common\models\PerfStandard;
  29. use common\models\Period;
  30. use common\models\DecOrder;
  31. use common\models\EmployLevel;
  32. use common\models\PerfActiveUser;
  33. use common\models\StorePerfLog;
  34. use common\models\UserRelation;
  35. use common\models\RemainPv;
  36. use common\models\FlowRemainPv;
  37. use yii\base\Exception;
  38. use yii\base\StaticInstanceTrait;
  39. class PerfCalc {
  40. use StaticInstanceTrait;
  41. private $_limit = 10000;
  42. private $_handleUserId;
  43. private $_companyMonthPerf = 0;
  44. private $_cfTotalPercent = 0;
  45. private $_lxTotalPercent = 0;
  46. private $_sysConfig = [];
  47. private $_decLevelConfig = [];
  48. private $_empLevelConfig = [];
  49. private $_decRoleConfig = [];
  50. private $_errors = [];
  51. private $_periodNum = 0;
  52. private $_periodId;
  53. private $_isCalcMonth = 0;
  54. private $_calcYear;
  55. private $_calcMonth;
  56. private $_calcYearMonth;
  57. private $_lastCalcYear;
  58. private $_lastCalcMonth;
  59. private $_lastCalcYearMonth;
  60. private $_lastPeriodNum;
  61. private $_lastPeriodYear;
  62. private $_lastPeriodMonth;
  63. private $_lastPeriodYearMonth;
  64. //pv
  65. private $_pvRatio;
  66. const LOOP_FINISH = 1;
  67. const LOOP_CONTINUE = 2;
  68. const ORDER_PAY_TYPE_CASH = 'cash';
  69. // const ORDER_PAY_TYPE_POINT = 'point';
  70. /**
  71. * 设置期数
  72. * @param int $periodNum
  73. * @return int
  74. */
  75. public function setPeriodNum(int $periodNum) {
  76. return $this->_periodNum = $periodNum;
  77. }
  78. /**
  79. * 获取期数
  80. * @return int
  81. */
  82. public function getPeriodNum() {
  83. return $this->_periodNum;
  84. }
  85. /**
  86. * 加入错误错误
  87. * @param string $attr
  88. * @param string $error
  89. */
  90. public function addError(string $attr, string $error) {
  91. $this->_errors[$attr][] = $error;
  92. }
  93. /**
  94. * 获取错误信息
  95. * @return array
  96. */
  97. public function getErrors() {
  98. return $this->_errors;
  99. }
  100. /**
  101. * 生成流水号
  102. * @return string
  103. */
  104. private function _generateSn() {
  105. return Date::today('Ymd') . $this->_random(10, 1);
  106. }
  107. /**
  108. * 生成随机数
  109. * @param $length
  110. * @param int $numeric
  111. * @return string
  112. */
  113. private function _random($length, $numeric = 0) {
  114. $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
  115. $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
  116. $hash = '';
  117. $max = strlen($seed) - 1;
  118. for ($i = 0; $i < $length; $i++) {
  119. $hash .= $seed[mt_rand(0, $max)];
  120. }
  121. return $hash;
  122. }
  123. /**
  124. * 处理order表中有remain_pv的订单
  125. * 将结果写入到remainPv相关表中
  126. *
  127. */
  128. private function _calcRemainPv(){
  129. $orders = Order::find()->where('PERIOD_NUM=:PERIOD_NUM AND REMAIN_PV>0',[':PERIOD_NUM'=>$this->_periodNum])->asArray()->all();
  130. foreach($orders as $order){
  131. $oRemainPv = RemainPv::find()->where(['USER_ID' => $order['USER_ID']])->one();
  132. $transactionRemain = \Yii::$app->db->beginTransaction();
  133. try{
  134. $flowRemainPvModel = new FlowRemainPv();
  135. $flowRemainPvModel->ID = $this->_generateSn();
  136. $flowRemainPvModel->USER_ID = $order['USER_ID'];
  137. $flowRemainPvModel->REMAIN_PV_FLOW = $order['REMAIN_PV'];
  138. $flowRemainPvModel->REMAIN_PV_TOTAL = $oRemainPv['REMAIN_PV'] + $order['REMAIN_PV'];
  139. $flowRemainPvModel->PERIOD_NUM = $this->_periodNum;
  140. $flowRemainPvModel->UPDATED_AT = Date::nowTime();
  141. $flowRemainPvModel->ORDER_SN = $order['SN'];
  142. if(!$flowRemainPvModel->save()){
  143. $this->addErrors($flowRemainPvModel->getErrors());
  144. return false;
  145. }
  146. $oRemainPv = RemainPv::find()->where(['USER_ID' => $order['USER_ID']])->one();
  147. if($oRemainPv){
  148. $oRemainPv->updateCounters(['REMAIN_PV'=>$order['REMAIN_PV']]);
  149. }else{
  150. $remainPvModel = new RemainPv();
  151. $remainPvModel->ID = $this->_generateSn();
  152. $remainPvModel->USER_ID = $order['USER_ID'];
  153. $remainPvModel->UPDATED_AT = Date::nowTime();
  154. $remainPvModel->REMAIN_PV = $order['REMAIN_PV'];
  155. $remainPvModel->STATUS = 1;
  156. if(!$remainPvModel->save()){
  157. $this->addErrors($remainPvModel->getErrors());
  158. return false;
  159. }
  160. }
  161. $transactionRemain->commit();
  162. } catch (Exception $e) {
  163. $transactionRemain->rollBack();
  164. $this->addError('add', $e->getMessage());
  165. return null;
  166. }
  167. }
  168. return null;
  169. }
  170. /**
  171. * 生成假订单
  172. *
  173. */
  174. public static function _createFakeOrder($periodNum){
  175. echo('假假假'.$periodNum. PHP_EOL);
  176. $userHaveRemain = RemainPv::findAllAsArray('REMAIN_PV >0');
  177. $currentPeriod = Period::getInfoByPeriodNum($periodNum);
  178. $sysConfig = Cache::getSystemConfig();
  179. $mesureUpCondition = $sysConfig['monthPcsPvFxCondition']['VALUE']; // 月达标条件 NC默认300
  180. if($currentPeriod['IS_MONTH']){
  181. print_r('是月结点'.PHP_EOL);
  182. $periods = Period::getCurrentMonthPeriodByPeriodNum($periodNum);
  183. //先清除本期的假订单
  184. echo('首先,清除上次尝试生成业绩单时所创建的虚假订单'. date('Y-m-d H:i:s', time()) . PHP_EOL);
  185. $delFOrder = Order::deleteAll(['IS_AUTO'=>'1','PERIOD_NUM'=>$periodNum]);
  186. echo('检查有结余PV的用户,如果他当月PV不足'.$mesureUpCondition.',则为其创建假订单'. PHP_EOL);
  187. foreach($userHaveRemain as $uR){
  188. $myPv = Order::find()->where(['PERIOD_NUM'=>$periods, 'USER_ID'=>$uR['USER_ID']])->SUM('PV');
  189. if($myPv < $mesureUpCondition){
  190. //制造虚拟订单
  191. echo('不足'.$mesureUpCondition.'了,生成假订单' . PHP_EOL);
  192. $newOrderForm = new OrderForm();
  193. $newOrderForm->addFakeOrder($uR['USER_ID'],$periodNum);
  194. }
  195. }
  196. }
  197. }
  198. /**
  199. * 计算步骤
  200. * @param $periodNum
  201. * @param null $handleUserId
  202. * @return bool
  203. */
  204. public function calcStep($periodNum, $handleUserId = null) {
  205. try {
  206. $this->_errors = [];
  207. $this->setPeriodNum($periodNum);
  208. $this->_handleUserId = $handleUserId;
  209. $t1 = microtime(true);
  210. // 初始化结算任务
  211. $this->initCalcTask();
  212. // 设置结算状态
  213. $this->setCalcStatus('start');
  214. // 清空所有本期结算用到的缓存
  215. CalcCache::clearAll($this->_periodNum);
  216. // 清空会员推荐和接点关系缓存
  217. CalcCache::clearNetCache();
  218. // 清空相关表数据
  219. $this->clearTableData();
  220. $t2 = microtime(true);
  221. echo(PHP_EOL . $periodNum. '期,生成业绩单,开始' . PHP_EOL);
  222. echo('初始化、清空缓存及相关数据表完成,耗时:' . round($t2 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  223. $this->_updatePercent(10);
  224. // 计算月奖,才需要向缓存中加入按推荐深度的所有用户
  225. echo('向缓存中加入用户开始 ' . date('Y-m-d H:i:s', time()) . PHP_EOL);
  226. echo('若需要,生成假订单' . date('Y-m-d H:i:s', time()) . PHP_EOL);
  227. $this->_createFakeOrder($periodNum);
  228. echo('生成假订单完成,开始缓存用户'. date('Y-m-d H:i:s', time()) . PHP_EOL);
  229. //修改每一期都缓存所有用户
  230. CalcCache::addUsers($this->_periodNum);
  231. $t3 = microtime(true);
  232. echo('向缓存中加入用户完成,耗时:' . round($t3 - $t2, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  233. $this->_updatePercent(20);
  234. // 周结,循环向上级计入业绩并加入业绩单
  235. $this->loopCalcPeriodPerfByDecOrder();
  236. $this->loopCalcPeriodPerfByOrderDec();
  237. $t4 = microtime(true);
  238. echo('计算周业绩表中的数据完成,耗时:' . round($t4 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  239. $this->_updatePercent(40);
  240. // 从会员的复销订单会员计算复销业绩并加入业绩单
  241. $this->loopCalcPerfByFXOrder();
  242. $this->loopCalcPerfByShopFXOrder();
  243. $t5 = microtime(true);
  244. echo('计算复销业绩并写入业绩单完成,耗时:' . round($t5 - $t4, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  245. $this->_updatePercent(60);
  246. //本期业绩入库
  247. $this->loopWriteNowPerf();
  248. $t6 = microtime(true);
  249. echo('本期业绩入库完成,耗时:' . round($t6 - $t5, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  250. $this->_updatePercent(70);
  251. //计算月业绩表中的数据
  252. $this->loopCalcMonthPerfTableData();
  253. $t7 = microtime(true);
  254. echo('计算月业绩表中的数据完成,耗时:' . round($t7 - $t6, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  255. $this->_updatePercent(80);
  256. //每月计算聘级
  257. // modify 聘级字段改成蓝星奖的 奖衔级别(蓝星奖的等级).不在使用荣衔奖金.聘级的等级切换成蓝星奖等级,
  258. // 此等级会影响团队将的封顶.
  259. // $this->loopCalcEmpLevel();
  260. $t8 = microtime(true);
  261. // echo('计算聘级完成,耗时:' . round($t8 - $t7, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  262. //$this->_updatePercent(90);
  263. //本月业绩入库
  264. $this->loopWriteMonthPerf();
  265. $t7 = microtime(true);
  266. echo('本月业绩入库完成,耗时:' . round($t7 - $t6, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  267. $this->_updatePercent(90);
  268. // 店服务奖/月节点
  269. $this->storePerf();
  270. $t8 = microtime(true);
  271. echo('店服务业绩计算,耗时:' . round($t8 - $t7, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  272. $this->_updatePercent(95);
  273. // // //达标业绩
  274. // $this->loopCalcPerfByStandardFXOrder();
  275. // // //达标业绩入库
  276. // $this->loopWriteStandardPerf();
  277. $t9 = microtime(true);
  278. echo('本月业绩入库完成,耗时:' . round($t9 - $t8, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  279. $this->_updatePercent(95);
  280. // 通过插入到perforder中的业绩订单数据,生成此业绩期活跃用户数据
  281. // $this->loopWriteActiveUser();
  282. $this->_updatePercent(100);
  283. $t10 = microtime(true);
  284. echo('业绩结算全部完成,共耗时:' . round($t10 - $t9, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  285. } catch (\Exception $e) {
  286. $this->errorCalcTask();
  287. $this->addError('calc', $e->getMessage());
  288. return false;
  289. }
  290. return true;
  291. }
  292. /**
  293. * 结算完成
  294. * @return bool
  295. */
  296. public function endCalcTask() {
  297. // 更新结算状态
  298. $this->setCalcStatus('end');
  299. //如果自动结算
  300. if(boolval($this->_sysConfig['autoCalcPeriod']['VALUE'])){
  301. $period = Period::instance();
  302. if($period->isLastSent($this->_periodNum)) {
  303. $bonusCalc = BonusCalc::instance();
  304. $asyncResult = $bonusCalc->calcStep($this->_periodNum);
  305. if ($asyncResult) {
  306. $bonusCalc->endCalcTask();
  307. } else {
  308. $bonusCalc->errorCalcTask();
  309. }
  310. return $asyncResult;
  311. }
  312. }
  313. }
  314. /**
  315. * 结算错误
  316. */
  317. public function errorCalcTask() {
  318. // 清空所有本期结算用到的缓存
  319. CalcCache::clearAll($this->_periodNum);
  320. // 更新结算状态
  321. $this->setCalcStatus('fail');
  322. }
  323. /**
  324. * 设置生成业绩单状态
  325. * @param $type
  326. * start|end|fail
  327. */
  328. public function setCalcStatus($type) {
  329. if ($type == 'start') {
  330. Period::updateAll(['IS_PERFING' => 1, 'IS_PERFED' => Period::PERF_NONE, 'PERF_STARTED_AT' => Date::nowTime()], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  331. } elseif ($type == 'end') {
  332. Period::updateAll(['IS_PERFING' => 0, 'IS_PERFED' => Period::PERF_FINISH, 'PERFED_AT' => Date::nowTime()], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  333. } elseif ($type == 'fail') {
  334. Period::updateAll(['IS_PERFING' => 0, 'IS_PERFED' => Period::PERF_FAIL, 'PERFED_AT' => 0], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  335. }
  336. }
  337. /**
  338. * 初始化结算任务
  339. * @throws \yii\db\Exception
  340. */
  341. public function initCalcTask() {
  342. $this->_sysConfig = Cache::getSystemConfig();
  343. $this->_decLevelConfig = Cache::getDecLevelConfig();
  344. $this->_empLevelConfig = Cache::getEmpLevelConfig();
  345. $this->_decRoleConfig = CalcCache::getDecRoleConfig($this->_periodNum);
  346. $periodNum = $this->_periodNum;
  347. // 获取本年月和上年月
  348. $periodObj = Period::instance();
  349. $periodDataArr = $periodObj->setPeriodNum($periodNum);
  350. $this->_periodId = $periodDataArr['ID'];
  351. $this->_isCalcMonth = $periodObj->isCalcMonth($periodNum);
  352. $this->_calcYear = $periodObj->getYear($periodNum);
  353. $this->_calcMonth = $periodObj->getMonth($periodNum);
  354. $this->_calcYearMonth = $periodObj->getYearMonth($periodNum);
  355. $lastYearMonthArr = $periodObj->getLastMonth($periodNum);
  356. $this->_lastCalcYear = $lastYearMonthArr['year'];
  357. $this->_lastCalcMonth = $lastYearMonthArr['month'];
  358. $this->_lastCalcYearMonth = $lastYearMonthArr['yearMonth'];
  359. $this->_lastPeriodNum = $periodNum - 1;
  360. if (Period::isExistsPeriodNum($this->_lastPeriodNum)) {
  361. $this->_lastPeriodYear = $periodObj->getYear($this->_lastPeriodNum);
  362. $this->_lastPeriodMonth = $periodObj->getMonth($this->_lastPeriodNum);
  363. $this->_lastPeriodYearMonth = $periodObj->getYearMonth($this->_lastPeriodNum);
  364. } else {
  365. $this->_lastPeriodYear = 0;
  366. $this->_lastPeriodMonth = 0;
  367. $this->_lastPeriodYearMonth = 0;
  368. }
  369. $this->_pvRatio = $this->_sysConfig['pvRatio']['VALUE'];
  370. }
  371. /**
  372. * 清空相关表数据
  373. */
  374. public function clearTableData() {
  375. // 周业绩
  376. PerfPeriod::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  377. // 业绩单
  378. PerfOrder::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  379. // 删除活跃用户
  380. // PerfActiveUser::pageDeleteAll('PERIOD_NUM='.$this->_periodNum.' AND IS_SENT=0 ');
  381. // 月结时要清空的数据
  382. if ($this->_isCalcMonth) {
  383. // 月业绩表
  384. PerfMonth::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");
  385. // 店服务业绩log表
  386. StorePerfLog::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");
  387. //达标业绩表
  388. PerfStandard::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");
  389. }
  390. }
  391. /**
  392. * 循环判断活跃会员,并入库
  393. * @param int $offset
  394. * @return bool
  395. * @throws \yii\db\Exception
  396. */
  397. public function loopWriteActiveUser($offset = 0) {
  398. echo sprintf("时间:[%s]本期活跃用户PerfOrder表中,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  399. // 从PerfOrder表中获取此周期的用户数据
  400. $data = PerfPeriod::findUseDbCalc()
  401. ->select('USER_ID')
  402. ->where('PERIOD_NUM=:PERIOD_NUM',[
  403. ':PERIOD_NUM' => $this->_periodNum
  404. ])->offset($offset)->limit($this->_limit)->asArray()->all();
  405. if ($data) {
  406. foreach ($data as $upv) {
  407. $userId = $upv['USER_ID'];
  408. // 判断用户每周是否是活跃用户
  409. $allData = PerfOrder::findUseDbCalc()
  410. ->select('USER_ID,PV,DEC_AMOUNT,PERIOD_NUM,CALC_MONTH,P_CALC_MONTH, DEC_SN,ORDER_CREATED_AT')
  411. ->where('PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID',[
  412. ':PERIOD_NUM' => $this->_periodNum,
  413. ':USER_ID' => $userId
  414. ])
  415. ->orderBy('ORDER_CREATED_AT DESC')
  416. ->asArray()
  417. ->all();
  418. if($allData) {
  419. $this->weekMonthOrder($allData);
  420. unset($insertDataPeriodPerf, $allData);
  421. }
  422. unset($allData);
  423. }
  424. unset($data);
  425. return $this->loopWriteActiveUser($offset + $this->_limit);
  426. }
  427. return true;
  428. }
  429. // 活跃判断
  430. public function weekMonthOrder($data) {
  431. // 获取每个订单是周几,把他们分别放到各自的周里面.
  432. // 因为是自然月,所以需要知道是此月的几号,是此月的第几周.然后往前和往后推出时间戳范围内pv的累计之和是多少
  433. $one = 24*60*60; // 一天的时间戳
  434. $time_arr = [];
  435. foreach ($data as $orders) {
  436. $day = intval(date('d', $orders['ORDER_CREATED_AT'])); // 当月第多少号
  437. $weekDay = (date('w', $orders['ORDER_CREATED_AT']) == 0) ? 7 : date('w', $orders['ORDER_CREATED_AT']);// 周几 1-7
  438. $format = date('Y-m-d', $orders['ORDER_CREATED_AT']);
  439. $orderDayStart = strtotime($format.' 0:0:0');
  440. $orderDayEnd = strtotime($format.' 23:59:59');
  441. $startCaluc = $weekDay-1;
  442. $endCaluc = 7-$weekDay;
  443. $calucWeekStart = $orderDayStart - $startCaluc*$one;
  444. $calucWeekEnd = $orderDayEnd + $endCaluc*$one;
  445. if (!isset($time_arr[$calucWeekStart])) {
  446. $time_arr[$calucWeekStart] = array_merge($orders, [
  447. 'temp_start' => $calucWeekStart,
  448. 'temp_end' => $calucWeekEnd,
  449. 'calculate_total_pv' => $orders['PV']
  450. ]);
  451. } else {
  452. $time_arr[$calucWeekStart]['calculate_total_pv'] = $time_arr[$calucWeekStart]['calculate_total_pv'] + $orders['PV'];
  453. }
  454. }
  455. unset($data);
  456. $insert = [];
  457. // 循环判断,当周pv累计是否达到400
  458. foreach ($time_arr as $k => $v) {
  459. if ($v['calculate_total_pv'] >= $this->_sysConfig['activeUserPvCondition']['VALUE']) {
  460. // 如果个人业累计绩达400PV,取得连续四周活跃
  461. // 取出周末再增加三周的时间戳
  462. $insert[] = [
  463. 'ID' => SnowFake::instance()->generateId(),
  464. 'USER_ID' => $v['USER_ID'],
  465. 'PERIOD_NUM' => $v['PERIOD_NUM'],
  466. 'CALC_MONTH' => $v['CALC_MONTH'],
  467. 'P_CALC_MONTH' => $v['P_CALC_MONTH'],
  468. 'SRATR_AT' => $v['temp_start'],
  469. 'END_AT' => $v['temp_end']+(3*7*24*60*60),
  470. 'CREATED_AT' => time(),
  471. 'PV' => $v['calculate_total_pv'],
  472. ];
  473. }
  474. }
  475. PerfActiveUser::batchInsert($insert);
  476. unset($insert);
  477. return true;
  478. }
  479. /**
  480. * 周结,向上级算业绩,并计入业绩单
  481. * @param int $offset
  482. * @return bool
  483. * @throws \yii\db\Exception
  484. */
  485. public function loopCalcPeriodPerfByDecOrder($offset = 0) {
  486. // 循环获取全部报单
  487. $allData = DecOrder::findUseDbCalc()->select('ID,DEC_SN,ORDER_SN,USER_ID,TYPE,TO_USER_ID,IS_ADMIN,DEC_AMOUNT,DEC_PV,PERIOD_NUM,CALC_MONTH,IS_DEL,P_CALC_MONTH,CREATED_AT,DEC_ID')->where("PERIOD_NUM=:PERIOD_NUM AND IS_DEL=0 AND TYPE='ZC'", [':PERIOD_NUM' => $this->_periodNum])->orderBy('CREATED_AT DESC,ID DESC')->offset($offset)->limit($this->_limit)->asArray()->all();
  488. if ($allData) {
  489. $insertPerfOrderData = [];
  490. foreach ($allData as $data) {
  491. // 是否关停等状态不能拿业绩
  492. if (!$this->isHasPerf($data['TO_USER_ID'])) {
  493. continue;
  494. }
  495. //零售单不累计业绩,仅存业绩单
  496. // if($data['TYPE']!='LS') {
  497. // 给自己增加PCS(个人消费)
  498. CalcCache::nowPeriodPerf($data['TO_USER_ID'], $this->_periodNum, [
  499. 'PV_PCS' => $data['DEC_PV'],
  500. 'PV_PCS_ZC' => $data['DEC_PV'],
  501. ]);
  502. // 把该会员加入到能拿到业绩的会员缓存中
  503. CalcCache::addHasPerfUsers($data['TO_USER_ID'], $this->_periodNum);
  504. //加入到报单会员中
  505. $toInfo = CalcCache::getUserInfo($data['TO_USER_ID'], $this->_periodNum);
  506. CalcCache::addHasBDUsers($data['TO_USER_ID'], $this->_periodNum, [
  507. 'TO_USER_ID' => $data['TO_USER_ID'],
  508. 'USER_ID' => $data['USER_ID'],
  509. 'DEC_ID' => $data['DEC_ID'],
  510. //考虑可能会移网的情况
  511. 'REC_USER_ID' => $toInfo['REC_UID'] ?? '',
  512. 'CON_USER_ID' => $toInfo['CON_UID'] ?? '',
  513. 'DEC_AMOUNT' => $data['DEC_AMOUNT'],
  514. 'DEC_PV' => $data['DEC_PV'],
  515. ]);
  516. // 给上追加业绩
  517. $this->loopNetworkParentDo($data['TO_USER_ID'], function ($parent) use (&$data) {
  518. // 给上级会员追加业绩到缓存中
  519. // CalcCache::addUserPerf($parent['PARENT_UID'], $this->_periodNum, [
  520. // 'PV_'.$parent['LOCATION'].'L' => $data['DEC_PV'],
  521. // ]);
  522. // 给上级会员追加本期业绩到缓存中
  523. CalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  524. 'PV_' . $parent['LOCATION'] . 'L' => $data['DEC_PV'],
  525. 'PV_' . $parent['LOCATION'] . 'L_TOUCH' => $data['DEC_PV'],
  526. 'PV_' . $parent['LOCATION'] . 'L_' . $data['TYPE'] => $data['DEC_PV'],
  527. ]);
  528. // 把该会员加入到能拿到业绩的会员缓存中
  529. CalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  530. unset($parent);
  531. });
  532. //给推荐关系累计增加业绩
  533. $this->loopRelationParentDo($data['TO_USER_ID'], function ($parent) use (&$data) {
  534. // 给上级会员追加业绩到缓存中
  535. // CalcCache::addUserPerf($parent['PARENT_UID'], $this->_periodNum, [
  536. // 'PV_'.$parent['LOCATION'].'L' => $data['DEC_PV'],
  537. // ]);
  538. // 给上级会员追加本期业绩到缓存中
  539. CalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  540. 'PV_PSS' => $data['DEC_PV'],
  541. ]);
  542. // 把该会员加入到能拿到业绩的会员缓存中
  543. CalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  544. unset($parent);
  545. });
  546. // }
  547. // 写入业绩单表
  548. $decInfo = CalcCache::getUserInfo($data['USER_ID'], $this->_periodNum);
  549. $sn = PerfOrder::generateSN();
  550. $insertPerfOrderData[] = [
  551. 'ID' => SnowFake::instance()->generateId(),
  552. 'SN' => $sn,
  553. 'DEC_SN' => $data['DEC_SN'],
  554. 'DEC_TYPE' => strtoupper($data['TYPE']),
  555. 'DEC_STATUS' => PerfOrder::STATUS_NORMAL,
  556. 'USER_ID' => $data['TO_USER_ID'],
  557. 'LAST_REC_USER_NAME' => $toInfo['REC_USER_NAME'],
  558. 'LAST_REC_REAL_NAME' => $toInfo['REC_REAL_NAME'],
  559. 'LAST_DEC_LV' => $toInfo['DEC_LV'],
  560. 'LAST_EMP_LV' => $toInfo['EMP_LV'],
  561. 'LAST_STATUS' => $toInfo['STATUS'],
  562. 'PV' => $data['DEC_PV'],
  563. 'DEC_AMOUNT' => $data['DEC_AMOUNT'],
  564. 'LAST_SUB_COM_ID' => $toInfo['SUB_COM_ID'],
  565. 'LAST_PROVINCE' => $toInfo['PROVINCE'],
  566. 'LAST_CITY' => $toInfo['CITY'],
  567. 'LAST_COUNTY' => $toInfo['COUNTY'],
  568. 'DEC_USER_ID' => $data['USER_ID'],
  569. 'LAST_DEC_DEC_LV' => $decInfo['DEC_LV'],
  570. 'LAST_DEC_SUB_COM_ID' => $decInfo['SUB_COM_ID'],
  571. 'LAST_DEC_PROVINCE' => $decInfo['DEC_PROVINCE'],
  572. 'LAST_DEC_CITY' => $decInfo['DEC_CITY'],
  573. 'LAST_DEC_COUNTY' => $decInfo['DEC_COUNTY'],
  574. 'PERIOD_NUM' => $this->_periodNum,
  575. 'CALC_MONTH' => $this->_calcYearMonth,
  576. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  577. 'CREATED_AT' => Date::nowTime(),
  578. 'CLOSED_AT' => 0,
  579. 'ORDER_CREATED_AT' => $data['CREATED_AT'],
  580. ];
  581. unset($data, $decInfo, $sn, $toInfo);
  582. }
  583. PerfOrder::batchInsert($insertPerfOrderData);
  584. unset($insertPerfOrderData, $allData, $snArr);
  585. return $this->loopCalcPeriodPerfByDecOrder($offset + $this->_limit);
  586. }
  587. unset($allData);
  588. return true;
  589. }
  590. /**
  591. * 周结,向上级算业绩,并计入业绩单
  592. * @param int $offset
  593. * @return bool
  594. * @throws \yii\db\Exception
  595. */
  596. public function loopCalcPeriodPerfByOrderDec($offset = 0) {
  597. // 循环获取全部报单
  598. $allData = OrderDec::findUseDbCalc()->select('CREATED_AT,ID,SN,USER_ID,ORDER_TYPE,ORDER_AMOUNT,PV,PAY_AMOUNT,PAY_PV,PERIOD_NUM,PAY_TYPE')->where("PERIOD_NUM=:PERIOD_NUM AND IS_DELETE=0 AND ORDER_TYPE=:ORDER_TYPE", [':PERIOD_NUM' => $this->_periodNum, ':ORDER_TYPE'=>'ZC'])->orderBy('ID DESC')->offset($offset)->limit($this->_limit)->asArray()->all();
  599. if ($allData) {
  600. $insertPerfOrderData = [];
  601. foreach ($allData as $data) {
  602. // 是否关停等状态不能拿业绩
  603. if (!$this->isHasPerf($data['USER_ID'])) {
  604. continue;
  605. }
  606. // 给自己增加PCS(个人消费)
  607. CalcCache::nowPeriodPerf($data['USER_ID'], $this->_periodNum, [
  608. 'PV_PCS' => $data['PAY_PV'],
  609. 'PV_PCS_ZC' => $data['PAY_PV'],
  610. ]);
  611. // 把该会员加入到能拿到业绩的会员缓存中
  612. CalcCache::addHasPerfUsers($data['USER_ID'], $this->_periodNum);
  613. //加入到报单会员中
  614. $toInfo = CalcCache::getUserInfo($data['USER_ID'], $this->_periodNum);
  615. CalcCache::addHasBDUsers($data['USER_ID'], $this->_periodNum, [
  616. 'TO_USER_ID' => $data['USER_ID'],
  617. 'USER_ID' => $toInfo['DEC_ID'],
  618. 'DEC_ID' => $toInfo['DEC_ID'],
  619. //考虑可能会移网的情况
  620. 'REC_USER_ID' => $toInfo['REC_UID'] ?? '',
  621. 'CON_USER_ID' => $toInfo['CON_UID'] ?? '',
  622. 'DEC_AMOUNT' => $data['ORDER_AMOUNT'],
  623. 'DEC_PV' => $data['PAY_PV'],
  624. ]);
  625. // 给上追加业绩
  626. $this->loopNetworkParentDo($data['USER_ID'], function ($parent) use (&$data) {
  627. // 给上级会员追加本期业绩到缓存中
  628. CalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  629. 'PV_' . $parent['LOCATION'] . 'L' => $data['PAY_PV'],
  630. 'PV_' . $parent['LOCATION'] . 'L_TOUCH' => $data['PAY_PV'],
  631. 'PV_' . $parent['LOCATION'] . 'L_' . $data['ORDER_TYPE'] => $data['PAY_PV'],
  632. ]);
  633. // 把该会员加入到能拿到业绩的会员缓存中
  634. CalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  635. unset($parent);
  636. });
  637. //给推荐关系累计增加业绩
  638. $this->loopRelationParentDo($data['USER_ID'], function ($parent) use (&$data) {
  639. // 给上级会员追加本期业绩到缓存中
  640. CalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  641. 'PV_PSS' => $data['PAY_PV'],
  642. ]);
  643. // 把该会员加入到能拿到业绩的会员缓存中
  644. CalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  645. unset($parent);
  646. });
  647. // 写入业绩单表
  648. $decInfo = CalcCache::getUserInfo($toInfo['DEC_ID'], $this->_periodNum);
  649. $sn = PerfOrder::generateSN();
  650. $insertPerfOrderData[] = [
  651. 'ID' => SnowFake::instance()->generateId(),
  652. 'SN' => $sn,
  653. 'DEC_SN' => $data['SN'],
  654. 'DEC_TYPE' => strtoupper($data['ORDER_TYPE']),
  655. 'DEC_STATUS' => PerfOrder::STATUS_NORMAL,
  656. 'USER_ID' => $data['USER_ID'],
  657. 'LAST_REC_USER_NAME' => $toInfo['REC_USER_NAME'],
  658. 'LAST_REC_REAL_NAME' => $toInfo['REC_REAL_NAME'],
  659. 'LAST_DEC_LV' => $toInfo['DEC_LV'],
  660. 'LAST_EMP_LV' => $toInfo['EMP_LV'],
  661. 'LAST_STATUS' => $toInfo['STATUS'],
  662. 'PV' => $data['PAY_PV'],
  663. 'DEC_AMOUNT' => $data['ORDER_AMOUNT'],
  664. 'LAST_SUB_COM_ID' => $toInfo['SUB_COM_ID'],
  665. 'LAST_PROVINCE' => $toInfo['PROVINCE'],
  666. 'LAST_CITY' => $toInfo['CITY'],
  667. 'LAST_COUNTY' => $toInfo['COUNTY'],
  668. 'DEC_USER_ID' => $toInfo['DEC_ID'],
  669. 'LAST_DEC_DEC_LV' => $decInfo['DEC_LV'],
  670. 'LAST_DEC_SUB_COM_ID' => $decInfo['SUB_COM_ID'],
  671. 'LAST_DEC_PROVINCE' => $decInfo['DEC_PROVINCE'],
  672. 'LAST_DEC_CITY' => $decInfo['DEC_CITY'],
  673. 'LAST_DEC_COUNTY' => $decInfo['DEC_COUNTY'],
  674. 'PERIOD_NUM' => $this->_periodNum,
  675. 'CALC_MONTH' => $this->_calcYearMonth,
  676. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  677. 'CREATED_AT' => Date::nowTime(),
  678. 'CLOSED_AT' => 0,
  679. 'ORDER_CREATED_AT' => $data['CREATED_AT']
  680. ];
  681. unset($data, $decInfo, $sn, $toInfo);
  682. }
  683. PerfOrder::batchInsert($insertPerfOrderData);
  684. unset($insertPerfOrderData, $allData, $snArr);
  685. return $this->loopCalcPeriodPerfByOrderDec($offset + $this->_limit);
  686. }
  687. unset($allData);
  688. return true;
  689. }
  690. /**
  691. * 从会员的复销订单会员计算复销业绩并加入业绩单
  692. * @param int $offset
  693. * @return bool
  694. * @throws \yii\db\Exception
  695. */
  696. public function loopCalcPerfByFXOrder(int $offset = 0) {
  697. // 循环获取全部报单
  698. $allData = Order::findUseDbCalc()->select('ID,SN,DEC_SN,USER_ID,ORDER_TYPE,ORDER_AMOUNT,PAY_AMOUNT,PAY_PV,PAY_TYPE,PERIOD_NUM,STATUS,IS_DELETE,P_CALC_MONTH,CREATED_AT')->where("PERIOD_NUM=:PERIOD_NUM AND IS_DELETE=0 AND ORDER_TYPE=:ORDER_TYPE", [':PERIOD_NUM' => $this->_periodNum, ':ORDER_TYPE'=>DeclarationForm::TYPE_FX])->orderBy('CREATED_AT DESC,ID DESC')->offset($offset)->limit($this->_limit)->asArray()->all();
  699. if ($allData) {
  700. $insertPerfOrderData = [];
  701. foreach ($allData as $data) {
  702. // 是否关停等状态不能拿业绩
  703. if (!$this->isHasPerf($data['USER_ID'])) {
  704. continue;
  705. }
  706. //如果支付方式是现金,那么实际业绩是支付PV的50%
  707. if( $data['PAY_TYPE'] === self::ORDER_PAY_TYPE_CASH ) {
  708. $orderCashAmount = $data['ORDER_AMOUNT'];
  709. //111期开始由50%改为60%-by 2020-04-30修改
  710. $payPv = $data['PAY_PV'] * $this->_sysConfig['cashReconsumeBonusPercent']['VALUE'] / 100;
  711. $cacheDataKey = 'PV_PCS_FX_CASH';
  712. }else {
  713. $orderCashAmount = 0;
  714. $payPv = $data['PAY_PV'];
  715. $cacheDataKey = 'PV_PCS_FX_POINT';
  716. }
  717. if( $payPv <= 0 ) continue;
  718. // 给自己增加PCS(个人消费)
  719. CalcCache::nowPeriodPerf($data['USER_ID'], $this->_periodNum, [
  720. 'FX_AMOUNT_CASH' => $orderCashAmount,
  721. 'PV_PCS' => $payPv,
  722. 'PV_PCS_FX' => $payPv,
  723. $cacheDataKey => $payPv,
  724. ]);
  725. // 把该会员加入到能拿到业绩的会员缓存中
  726. CalcCache::addHasPerfUsers($data['USER_ID'], $this->_periodNum);
  727. // 给上追加业绩
  728. try {
  729. $this->loopNetworkParentDo($data['USER_ID'], function ($parent) use (&$data, $payPv) {
  730. // 给上级会员追加业绩到缓存中
  731. // CalcCache::addUserPerf($parent['PARENT_UID'], $this->_periodNum, [
  732. // 'PV_'.$parent['LOCATION'].'L' => $data['DEC_PV'],
  733. // ]);
  734. // 给上级会员追加本期业绩到缓存中
  735. CalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  736. 'PV_' . $parent['LOCATION'] . 'L' => $payPv,
  737. 'PV_' . $parent['LOCATION'] . 'L_TOUCH' => $payPv,
  738. 'PV_' . $parent['LOCATION'] . 'L_FX' => $payPv,
  739. ]);
  740. // 把该会员加入到能拿到业绩的会员缓存中
  741. CalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  742. });
  743. } catch(\Exception $e) {
  744. file_put_contents('loopNetworkParentDo_error.txt', var_export([
  745. 'USER_ID' => $data['USER_ID'],
  746. '_periodNum' => $this->_periodNum,
  747. 'error' => $e->getMessage()
  748. ],true));
  749. }
  750. //给推荐关系累计增加业绩
  751. $this->loopRelationParentDo($data['USER_ID'], function ($parent) use ($data, $payPv) {
  752. // 给上级会员追加业绩到缓存中
  753. // CalcCache::addUserPerf($parent['PARENT_UID'], $this->_periodNum, [
  754. // 'PV_'.$parent['LOCATION'].'L' => $data['DEC_PV'],
  755. // ]);
  756. // 给上级会员追加本期业绩到缓存中
  757. CalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  758. 'PV_PSS' => $payPv,
  759. ]);
  760. // 把该会员加入到能拿到业绩的会员缓存中
  761. CalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  762. });
  763. // }
  764. // 写入业绩单表
  765. $baseInfo = CalcCache::getUserInfo($data['USER_ID'], $this->_periodNum);
  766. $sn = PerfOrder::generateSN();
  767. $insertPerfOrderData[] = [
  768. 'ID' => SnowFake::instance()->generateId(),
  769. 'SN' => $sn,
  770. 'DEC_SN' => null,
  771. 'DEC_TYPE' => 'FX',
  772. 'DEC_STATUS' => PerfOrder::STATUS_NORMAL,
  773. 'USER_ID' => $data['USER_ID'],
  774. 'LAST_REC_USER_NAME' => $baseInfo['REC_USER_NAME'],
  775. 'LAST_REC_REAL_NAME' => $baseInfo['REC_REAL_NAME'],
  776. 'LAST_DEC_LV' => $baseInfo['DEC_LV'],
  777. 'LAST_EMP_LV' => $baseInfo['EMP_LV'],
  778. 'LAST_STATUS' => $baseInfo['STATUS'],
  779. 'PV' => $payPv,
  780. 'DEC_AMOUNT' => $data['PAY_AMOUNT'],
  781. 'LAST_SUB_COM_ID' => $baseInfo['SUB_COM_ID'],
  782. 'LAST_PROVINCE' => $baseInfo['PROVINCE'],
  783. 'LAST_CITY' => $baseInfo['CITY'],
  784. 'LAST_COUNTY' => $baseInfo['COUNTY'],
  785. 'DEC_USER_ID' => $data['USER_ID'],
  786. 'LAST_DEC_DEC_LV' => $baseInfo['DEC_LV'],
  787. 'LAST_DEC_SUB_COM_ID' => $baseInfo['SUB_COM_ID'],
  788. 'LAST_DEC_PROVINCE' => $baseInfo['PROVINCE'],
  789. 'LAST_DEC_CITY' => $baseInfo['CITY'],
  790. 'LAST_DEC_COUNTY' => $baseInfo['COUNTY'],
  791. 'PERIOD_NUM' => $this->_periodNum,
  792. 'CALC_MONTH' => $this->_calcYearMonth,
  793. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  794. 'CREATED_AT' => Date::nowTime(),
  795. 'CLOSED_AT' => 0,
  796. 'ORDER_CREATED_AT' => $data['CREATED_AT']
  797. ];
  798. unset($data, $baseInfo, $sn, $orderCashAmount, $payPv, $cacheDataKey);
  799. }
  800. PerfOrder::batchInsert($insertPerfOrderData);
  801. unset($insertPerfOrderData, $allData, $snArr);
  802. return $this->loopCalcPerfByFXOrder($offset + $this->_limit);
  803. }
  804. unset($allData);
  805. return true;
  806. }
  807. /**
  808. * 从会员的商城复销订单会员计算复销业绩并加入业绩单
  809. * @param int $offset
  810. * @return bool
  811. * @throws \yii\db\Exception
  812. */
  813. public function loopCalcPerfByShopFXOrder(int $offset = 0) {
  814. // 循环获取全部报单
  815. $allData = OrderShop::findUseDbCalc()->select('ID,SN,DEC_SN,USER_ID,ORDER_TYPE,ORDER_AMOUNT,PAY_AMOUNT,PAY_PV,PAY_TYPE,PERIOD_NUM,STATUS,IS_DELETE,P_CALC_MONTH,CREATED_AT')->where("PERIOD_NUM=:PERIOD_NUM AND IS_DELETE=0 AND ORDER_TYPE=:ORDER_TYPE", [':PERIOD_NUM' => $this->_periodNum, ':ORDER_TYPE'=>DeclarationForm::TYPE_FX])->orderBy('CREATED_AT DESC,ID DESC')->offset($offset)->limit($this->_limit)->asArray()->all();
  816. if ($allData) {
  817. $insertPerfOrderData = [];
  818. foreach ($allData as $data) {
  819. // 是否关停等状态不能拿业绩
  820. if (!$this->isHasPerf($data['USER_ID'])) {
  821. continue;
  822. }
  823. //如果支付方式是现金,那么实际业绩是支付PV的50%
  824. if( $data['PAY_TYPE'] === self::ORDER_PAY_TYPE_CASH ) {
  825. $orderCashAmount = $data['ORDER_AMOUNT'];
  826. //111期开始由50%改为60%-by 2020-04-30修改
  827. $payPv = $data['PAY_PV'] * $this->_sysConfig['cashReconsumeBonusPercent']['VALUE'] / 100;
  828. $cacheDataKey = 'PV_PCS_FX_CASH';
  829. }else {
  830. $orderCashAmount = 0;
  831. $payPv = $data['PAY_PV'];
  832. $cacheDataKey = 'PV_PCS_FX_POINT';
  833. }
  834. if( $payPv <= 0 ) continue;
  835. // 给自己增加PCS(个人消费)
  836. CalcCache::nowPeriodPerf($data['USER_ID'], $this->_periodNum, [
  837. 'FX_AMOUNT_CASH' => $orderCashAmount,
  838. 'PV_PCS' => $payPv,
  839. 'PV_PCS_FX' => $payPv,
  840. $cacheDataKey => $payPv,
  841. ]);
  842. // 把该会员加入到能拿到业绩的会员缓存中
  843. CalcCache::addHasPerfUsers($data['USER_ID'], $this->_periodNum);
  844. // 给上追加业绩
  845. $this->loopNetworkParentDo($data['USER_ID'], function ($parent) use (&$data, $payPv) {
  846. // 给上级会员追加业绩到缓存中
  847. // CalcCache::addUserPerf($parent['PARENT_UID'], $this->_periodNum, [
  848. // 'PV_'.$parent['LOCATION'].'L' => $data['DEC_PV'],
  849. // ]);
  850. // 给上级会员追加本期业绩到缓存中
  851. CalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  852. 'PV_' . $parent['LOCATION'] . 'L' => $payPv,
  853. 'PV_' . $parent['LOCATION'] . 'L_TOUCH' => $payPv,
  854. 'PV_' . $parent['LOCATION'] . 'L_FX' => $payPv,
  855. ]);
  856. // 把该会员加入到能拿到业绩的会员缓存中
  857. CalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  858. });
  859. //给推荐关系累计增加业绩
  860. $this->loopRelationParentDo($data['USER_ID'], function ($parent) use ($data, $payPv) {
  861. // 给上级会员追加业绩到缓存中
  862. // CalcCache::addUserPerf($parent['PARENT_UID'], $this->_periodNum, [
  863. // 'PV_'.$parent['LOCATION'].'L' => $data['DEC_PV'],
  864. // ]);
  865. // 给上级会员追加本期业绩到缓存中
  866. CalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  867. 'PV_PSS' => $payPv,
  868. ]);
  869. // 把该会员加入到能拿到业绩的会员缓存中
  870. CalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  871. });
  872. // }
  873. // 写入业绩单表
  874. $baseInfo = CalcCache::getUserInfo($data['USER_ID'], $this->_periodNum);
  875. $sn = PerfOrder::generateSN();
  876. $insertPerfOrderData[] = [
  877. 'ID' => SnowFake::instance()->generateId(),
  878. 'SN' => $sn,
  879. 'DEC_SN' => null,
  880. 'DEC_TYPE' => 'FX',
  881. 'DEC_STATUS' => PerfOrder::STATUS_NORMAL,
  882. 'USER_ID' => $data['USER_ID'],
  883. 'LAST_REC_USER_NAME' => $baseInfo['REC_USER_NAME'],
  884. 'LAST_REC_REAL_NAME' => $baseInfo['REC_REAL_NAME'],
  885. 'LAST_DEC_LV' => $baseInfo['DEC_LV'],
  886. 'LAST_EMP_LV' => $baseInfo['EMP_LV'],
  887. 'LAST_STATUS' => $baseInfo['STATUS'],
  888. 'PV' => $payPv,
  889. 'DEC_AMOUNT' => $data['PAY_AMOUNT'],
  890. 'LAST_SUB_COM_ID' => $baseInfo['SUB_COM_ID'],
  891. 'LAST_PROVINCE' => $baseInfo['PROVINCE'],
  892. 'LAST_CITY' => $baseInfo['CITY'],
  893. 'LAST_COUNTY' => $baseInfo['COUNTY'],
  894. 'DEC_USER_ID' => $data['USER_ID'],
  895. 'LAST_DEC_DEC_LV' => $baseInfo['DEC_LV'],
  896. 'LAST_DEC_SUB_COM_ID' => $baseInfo['SUB_COM_ID'],
  897. 'LAST_DEC_PROVINCE' => $baseInfo['PROVINCE'],
  898. 'LAST_DEC_CITY' => $baseInfo['CITY'],
  899. 'LAST_DEC_COUNTY' => $baseInfo['COUNTY'],
  900. 'PERIOD_NUM' => $this->_periodNum,
  901. 'CALC_MONTH' => $this->_calcYearMonth,
  902. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  903. 'CREATED_AT' => Date::nowTime(),
  904. 'CLOSED_AT' => 0,
  905. 'ORDER_CREATED_AT' => $data['CREATED_AT']
  906. ];
  907. unset($data, $baseInfo, $sn, $orderCashAmount, $payPv, $cacheDataKey);
  908. }
  909. PerfOrder::batchInsert($insertPerfOrderData);
  910. unset($insertPerfOrderData, $allData, $snArr);
  911. return $this->loopCalcPerfByShopFXOrder($offset + $this->_limit);
  912. }
  913. unset($allData);
  914. return true;
  915. }
  916. /**
  917. * 达标复销订单
  918. * @param int $offset
  919. * @return bool
  920. * @throws \yii\db\Exception
  921. */
  922. public function loopCalcPerfByStandardFXOrder(int $offset = 0) {
  923. if (!$this->_isCalcMonth) {
  924. return true;
  925. }
  926. $allData = OrderStandard::findUseDbCalc()->select('ID,SN,DEC_SN,USER_ID,ORDER_TYPE,ORDER_AMOUNT,PAY_AMOUNT,PAY_PV,PAY_TYPE,PERIOD_NUM,STATUS,IS_DELETE,CREATED_AT')->where("CALC_MONTH=:CALC_MONTH", [':CALC_MONTH' => $this->_calcYearMonth])->orderBy('ID DESC')->offset($offset)->limit($this->_limit)->asArray()->all();
  927. if ($allData) {
  928. $insertPerfOrderData = [];
  929. foreach ($allData as $data) {
  930. // 是否关停等状态不能拿业绩
  931. if (!$this->isHasPerf($data['USER_ID'])) {
  932. continue;
  933. }
  934. //如果支付方式是现金,那么实际业绩是支付PV的50%
  935. if( $data['PAY_TYPE'] === self::ORDER_PAY_TYPE_CASH ) {
  936. $orderCashAmount = $data['ORDER_AMOUNT'];
  937. // $payPv = $data['PAY_PV'] * $this->_sysConfig['cashReconsumeBonusPercent']['VALUE'] / 100;
  938. }else {
  939. $orderCashAmount = 0;
  940. // $payPv = $data['PAY_PV'];
  941. }
  942. if( $orderCashAmount <= 0 ) continue;
  943. // 给自己增加个人业绩
  944. CalcCache::nowStandardMonthPerf($data['USER_ID'], $this->_periodNum, [
  945. 'AMOUNT_PCS' => $orderCashAmount,
  946. ]);
  947. // 把该会员加入到能拿到业绩的会员缓存中
  948. CalcCache::addHasStandardMonthPerfUsers($data['USER_ID'], $this->_periodNum);
  949. //只有无聘级业绩才向上累加
  950. $baseInfo = CalcCache::getUserInfo($data['USER_ID'], $this->_periodNum);
  951. if( $baseInfo['EMP_LV'] != EmployLevel::NO_LEVEL_ID ) {
  952. continue;
  953. }
  954. //给推荐团队添加团队业绩 注:如果些订单特别多的情况,可以分2步。先只计算个人业绩、然后根据个人业绩再计算团队业绩。
  955. //给推荐关系累计增加业绩
  956. $this->loopRelationParentDo($data['USER_ID'], function ($parent) use ($data, $orderCashAmount) {
  957. // 给上级会员追加本期业绩到缓存中
  958. CalcCache::nowStandardMonthPerf($parent['PARENT_UID'], $this->_periodNum, [
  959. 'AMOUNT_PSS' => $orderCashAmount,
  960. ]);
  961. // 把该会员加入到能拿到业绩的会员缓存中
  962. CalcCache::addHasStandardMonthPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  963. //获取parent聘级
  964. $parentUserInfo = CalcCache::getUserInfo($parent['PARENT_UID'], $this->_periodNum);
  965. if( $parentUserInfo['EMP_LV'] != EmployLevel::NO_LEVEL_ID ) {//如果碰到有聘级的业绩就不在向上累加了
  966. return self::LOOP_FINISH;
  967. }
  968. });
  969. unset($data, $baseInfo, $orderCashAmount);
  970. }
  971. unset($insertPerfOrderData, $allData, $snArr);
  972. return $this->loopCalcPerfByStandardFXOrder($offset + $this->_limit);
  973. }
  974. unset($allData);
  975. return true;
  976. }
  977. /**
  978. * 计算月业绩表相关的数据并写入数据库
  979. * @param int $offset
  980. * @return bool
  981. * @throws Exception
  982. * @throws \yii\db\Exception
  983. */
  984. public function loopCalcMonthPerfTableData(int $offset = 0) {
  985. if (!$this->_isCalcMonth) {
  986. return true;
  987. }
  988. echo sprintf("时间:[%s]月业绩,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  989. // 从缓存列表里面从底层往上倒序获取会员
  990. // $allData = PerfPeriod::findUseDbCalc()->from(PerfPeriod::tableName() . ' AS PP')->select('PP.USER_ID AS USER_ID, SUM(PP.PV_PCS) AS PV_PCS_SUM,SUM(PP.PV_PSS) AS PV_PSS_SUM,SUM(PP.PV_1L) AS PV_1L_SUM,SUM(PP.PV_2L) AS PV_2L_SUM,SUM(PP.PV_3L) AS PV_3L_SUM,SUM(PP.PV_4L) AS PV_4L_SUM,SUM(PP.PV_5L) AS PV_5L_SUM, SUM(PM.PV_1L_TOTAL) AS PV_1L_TOTAL, SUM(PM.PV_2L_TOTAL) AS PV_2L_TOTAL, SUM(PM.PV_3L_TOTAL) AS PV_3L_TOTAL, SUM(PM.PV_4L_TOTAL) AS PV_4L_TOTAL,SUM(PM.PV_5L_TOTAL) AS PV_5L_TOTAL, SUM(PM.PV_PSS_TOTAL) AS PV_PSS_TOTAL')->leftJoin(PerfMonth::tableName() . ' AS PM', 'PM.USER_ID=PP.USER_ID AND PM.CALC_MONTH=:LAST_CALC_MONTH', ['LAST_CALC_MONTH'=>$this->_lastCalcYearMonth])->where('PP.CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])->groupBy('PP.USER_ID')->orderBy('PP.USER_ID DESC')->offset($offset)->limit($this->_limit)->asArray()->all();
  991. $allData = PerfPeriod::findUseDbCalc()->select('USER_ID, SUM(FX_AMOUNT_CASH) AS FX_AMOUNT_CASH_SUM,SUM(PV_PCS) AS PV_PCS_SUM,SUM(PV_PCS_FX) AS PV_PCS_FX_SUM,SUM(PV_PSS) AS PV_PSS_SUM,SUM(PV_1L) AS PV_1L_SUM,SUM(PV_2L) AS PV_2L_SUM,SUM(PV_3L) AS PV_3L_SUM,SUM(PV_4L) AS PV_4L_SUM,SUM(PV_5L) AS PV_5L_SUM,SUM(PV_1L_ZC) AS PV_1L_ZC_SUM,SUM(PV_2L_ZC) AS PV_2L_ZC_SUM,SUM(PV_3L_ZC) AS PV_3L_ZC_SUM,SUM(PV_4L_ZC) AS PV_4L_ZC_SUM,SUM(PV_5L_ZC) AS PV_5L_ZC_SUM')->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])->groupBy('USER_ID')->orderBy('USER_ID DESC')->offset($offset)->limit($this->_limit)->asArray()->all();
  992. if ($allData) {
  993. // 月度业绩表
  994. foreach ($allData as $everyData) {
  995. $userId = $everyData['USER_ID'];
  996. // $lastMonthData = PerfMonth::find()->select('PV_1L_TOTAL, PV_2L_TOTAL, PV_3L_TOTAL, PV_4L_TOTAL, PV_5L_TOTAL, PV_PSS_TOTAL')->where('USER_ID=:USER_ID AND CALC_MONTH=:LAST_CALC_MONTH', [
  997. // 'USER_ID'=>$userId,
  998. // 'LAST_CALC_MONTH'=>$this->_lastCalcYearMonth,
  999. // ])->asArray()->one();
  1000. //往期业绩
  1001. $userLastPerf = CalcCache::userPerf($userId, $this->_periodNum);
  1002. //本期业绩
  1003. $periodPerf = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
  1004. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1005. $nowMonthPerf = [
  1006. 'USER_ID' => $userId,
  1007. 'FX_AMOUNT_CASH' => $everyData['FX_AMOUNT_CASH_SUM'],
  1008. 'PV_PCS' => $everyData['PV_PCS_SUM'],
  1009. 'PV_PCS_FX' => $everyData['PV_PCS_FX_SUM'],
  1010. 'PV_PSS' => $everyData['PV_PSS_SUM'],
  1011. 'PV_1L' => $everyData['PV_1L_SUM'],
  1012. 'PV_2L' => $everyData['PV_2L_SUM'],
  1013. 'PV_3L' => $everyData['PV_3L_SUM'],
  1014. 'PV_4L' => $everyData['PV_4L_SUM'],
  1015. 'PV_5L' => $everyData['PV_5L_SUM'],
  1016. //总数据,历史+本期。不能用上月加本月,因为上月可能没业绩,上上个月有业绩。
  1017. 'PV_1L_TOTAL' => $periodPerf['PV_1L'] + $userLastPerf['PV_1L'],
  1018. 'PV_2L_TOTAL' => $periodPerf['PV_2L'] + $userLastPerf['PV_2L'],
  1019. 'PV_3L_TOTAL' => $periodPerf['PV_3L'] + $userLastPerf['PV_3L'],
  1020. 'PV_4L_TOTAL' => $periodPerf['PV_4L'] + $userLastPerf['PV_4L'],
  1021. 'PV_5L_TOTAL' => $periodPerf['PV_5L'] + $userLastPerf['PV_5L'],
  1022. 'PV_PSS_TOTAL' => $periodPerf['PV_PSS'] + $userLastPerf['PV_PSS_TOTAL'],
  1023. ];
  1024. // 把会员的月业绩写入缓存中,以便下面的奖金计算从缓冲中获取数据效率高
  1025. CalcCache::addHasMonthPerfUsers($userId, $this->_periodNum);
  1026. CalcCache::nowMonthPerf($userId, $this->_periodNum, $nowMonthPerf);
  1027. unset($userId, $everyData, $nowMonthPerf, $lastMonthData, $userBaseInfo);
  1028. }
  1029. unset($allData);
  1030. $this->loopCalcMonthPerfTableData($offset + $this->_limit);
  1031. }
  1032. unset($allData);
  1033. return true;
  1034. }
  1035. /**
  1036. * 计算聘级
  1037. * @param int $offset
  1038. * @return bool
  1039. * @throws Exception
  1040. * @throws \yii\db\Exception
  1041. */
  1042. public function loopCalcEmpLevel(int $offset = 0) {
  1043. if (!$this->_isCalcMonth) {
  1044. return true;
  1045. }
  1046. echo sprintf("时间:[%s]计算聘级,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1047. // 从缓存列表里面从底层往上倒序获取会员
  1048. $allData = CalcCache::getUsers($this->_periodNum, $offset, $this->_limit);
  1049. if ($allData) {
  1050. // 月度业绩表
  1051. foreach ($allData as $userId) {
  1052. $userInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1053. $nowMonthPerf = CalcCache::nowMonthPerf($userId, $this->_periodNum);
  1054. $empLevel = $this->checkEmpLevel($userId, $nowMonthPerf, $userInfo['EMP_LV']);
  1055. //根据用户的级别判断 能否得到级别积分
  1056. if( $empLevel['LEVEL_SCORE'] > 0 ) {
  1057. CalcCache::nowMonthScore($userId, $this->_periodNum, [
  1058. 'LEVEL_SCORE' => $empLevel['LEVEL_SCORE'],
  1059. ]);
  1060. CalcCache::addHasScoreUsers($userId, $this->_periodNum);
  1061. }
  1062. //更新月业绩的聘级和用户信息中的聘级
  1063. CalcCache::nowMonthPerf($userId, $this->_periodNum, [
  1064. 'EMP_LEVEL' => $empLevel['ID']
  1065. ]);
  1066. //为业绩单更新结算时聘级
  1067. // PerfOrder::updateAll(['LAST_EMP_LV'=>$empLevel['ID']],'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',[':USER_ID'=>$userId,':PERIOD_NUM'=>$this->_periodNum]);
  1068. $userEmpLevel = $this->_empLevelConfig[$userInfo['EMP_LV']];
  1069. $userEmpLevelSort = $userEmpLevel['SORT'] ?? EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'];
  1070. //不降级
  1071. if( $empLevel['SORT'] <= $userEmpLevelSort ) continue;
  1072. $userInfo['EMP_LV'] = $empLevel['ID'];
  1073. CalcCache::setUserInfo($userId, $this->_periodNum, $userInfo);
  1074. //可以判断用户是否升级,能否得到升级积分
  1075. //因为默认是不降级,直接获得升级积分
  1076. $upgradeScore = 0;
  1077. foreach ($this->_empLevelConfig as $everyEmpLevel) {
  1078. if( $everyEmpLevel['SORT'] <= $userEmpLevelSort ) continue;
  1079. if( $everyEmpLevel['SORT'] > $empLevel['SORT'] ) continue;
  1080. $upgradeScore += $everyEmpLevel['UPGRADE_SCORE'];
  1081. unset($everyEmpLevel);
  1082. }
  1083. if( $upgradeScore > 0 ) {
  1084. CalcCache::nowMonthScore($userId, $this->_periodNum, [
  1085. 'UPGRADE_SCORE' => $upgradeScore
  1086. ]);
  1087. CalcCache::addHasScoreUsers($userId, $this->_periodNum);
  1088. }
  1089. unset($userId, $empLevel, $nowMonthPerf, $userInfo, $userEmpLevel, $userEmpLevelSort, $upgradeScore);
  1090. }
  1091. unset($allData);
  1092. $this->loopCalcEmpLevel($offset + $this->_limit);
  1093. }
  1094. unset($allData);
  1095. return true;
  1096. }
  1097. /**
  1098. * 计算公司总业绩
  1099. * @return bool
  1100. * @throws Exception
  1101. */
  1102. public function calcPerfCompany() {
  1103. if (!$this->_isCalcMonth) {
  1104. return true;
  1105. }
  1106. $perfCompany = PerfCompany::findOne(['CALC_MONTH' => $this->_calcYearMonth]);
  1107. $db = \Yii::$app->db;
  1108. $transaction = $db->beginTransaction();
  1109. try {
  1110. if (!$perfCompany) {
  1111. $perfCompany = new PerfCompany();
  1112. }
  1113. $perfCompany->PV = $this->_companyMonthPerf;
  1114. $perfCompany->CREATED_AT = Date::nowTime();
  1115. $perfCompany->CALC_YEAR = $this->_calcYear;
  1116. $perfCompany->CALC_MONTH = $this->_calcYearMonth;
  1117. if (!$perfCompany->save()) {
  1118. throw new Exception(Form::formatErrorsForApi($perfCompany->getErrors()));
  1119. }
  1120. $transaction->commit();
  1121. } catch (Exception $e) {
  1122. $transaction->rollBack();
  1123. throw new Exception($e->getMessage());
  1124. }
  1125. unset($perfCompany);
  1126. return true;
  1127. }
  1128. /**
  1129. * 循环有业绩会员,并入库
  1130. * @param int $offset
  1131. * @return bool
  1132. * @throws \yii\db\Exception
  1133. */
  1134. public function loopWriteNowPerf($offset = 0) {
  1135. echo sprintf("时间:[%s]缓存本期业绩数据入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1136. // 从缓存列表里面从底层往上倒序获取会员
  1137. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  1138. if($allData){
  1139. $insertDataPeriodPerf = [];
  1140. foreach($allData as $userId){
  1141. $insertDataPeriodPerf[] = $this->nowPeriodPerfData($userId);
  1142. unset($userId);
  1143. }
  1144. PerfPeriod::batchInsert($insertDataPeriodPerf);
  1145. unset($insertDataPeriodPerf, $allData);
  1146. return $this->loopWriteNowPerf($offset + $this->_limit);
  1147. }
  1148. unset($allData);
  1149. return true;
  1150. }
  1151. /**
  1152. * 循环有月业绩会员,并入库
  1153. * @param int $offset
  1154. * @return bool
  1155. * @throws \yii\db\Exception
  1156. */
  1157. public function loopWriteMonthPerf($offset = 0) {
  1158. if(!$this->_isCalcMonth){
  1159. return true;
  1160. }
  1161. echo sprintf("时间:[%s]缓存本月业绩数据入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1162. // 从缓存列表里面从底层往上倒序获取会员
  1163. $allData = CalcCache::getHasMonthPerfUsers($this->_periodNum, $offset, $this->_limit);
  1164. if($allData){
  1165. $insertDataMonthPerf = [];
  1166. foreach($allData as $userId){
  1167. $insertDataMonthPerf[] = $this->nowMonthPerfData($userId);
  1168. unset($userId);
  1169. }
  1170. PerfMonth::batchInsert($insertDataMonthPerf);
  1171. unset($insertDataMonthPerf, $allData);
  1172. return $this->loopWriteMonthPerf($offset + $this->_limit);
  1173. }
  1174. unset($allData);
  1175. return true;
  1176. }
  1177. /**
  1178. * 店服务奖,循环结算月所有数据,并计算小组业绩
  1179. * @param int $offset
  1180. * @return bool
  1181. * @throws \yii\db\Exception
  1182. */
  1183. public function storePerf($offset = 0) {
  1184. if(!$this->_isCalcMonth){
  1185. return true;
  1186. }
  1187. echo sprintf("时间:[%s]店服务奖业绩计算,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1188. $allData = PerfOrder::findUseDbCalc()
  1189. ->select('ID,USER_ID, SUM(PV) AS MONTH_PV, CALC_MONTH')
  1190. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  1191. ->groupBy('USER_ID')
  1192. ->orderBy('USER_ID DESC')
  1193. ->offset($offset)
  1194. ->limit($this->_limit)
  1195. ->asArray()
  1196. ->all();
  1197. if ($allData) {
  1198. foreach ($allData as $everyData) {
  1199. $userId = $everyData['USER_ID'];
  1200. $pv = $everyData['MONTH_PV'];
  1201. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1202. // 记录有业绩单的用户
  1203. StorePerfLog::addOrUpdate(
  1204. $this->_periodNum,
  1205. $this->_calcYearMonth,
  1206. $userId,
  1207. [
  1208. 'USER_ID' => $userId,
  1209. 'IS_STUDIO' => $userBaseInfo['IS_STUDIO'],
  1210. 'MONTH_PV' => $pv,
  1211. 'PERIOD_NUM' => $this->_periodNum,
  1212. 'CALC_MONTH' => $this->_calcYearMonth,
  1213. 'CREATE_AT' => time()
  1214. ],
  1215. $pv
  1216. );
  1217. // 判断此用户是否是店铺,如果是店铺则累计此业绩到自己身上
  1218. if ($userBaseInfo['IS_STUDIO'] == 1) {
  1219. CalcCache::addHasPerfUsers($userId, $this->_periodNum);
  1220. CalcCache::nowPeriodPerf($userId, $this->_periodNum, [
  1221. 'STORE_PV_GRAND' => $pv,
  1222. ]);
  1223. StorePerfLog::stParendStudio($this->_periodNum,$this->_calcYearMonth, [$userId], $userId);
  1224. } else {
  1225. $allUser = [];
  1226. $studioUid = '';
  1227. // 如果此用户不是店铺,继续上找到店铺并累加上去PV
  1228. $this->loopRelationParentDo($userId, function ($parent) use (&$everyData,&$allUser,&$studioUid) {
  1229. $parentUser = CalcCache::getUserInfo($parent['PARENT_UID'], $this->_periodNum);
  1230. StorePerfLog::addOrUpdate(
  1231. $this->_periodNum,
  1232. $this->_calcYearMonth,
  1233. $parent['PARENT_UID'],
  1234. [
  1235. 'USER_ID' => $parent['PARENT_UID'],
  1236. 'IS_STUDIO' => $parentUser['IS_STUDIO'],
  1237. 'MONTH_PV' => 0,
  1238. 'PERIOD_NUM' => $this->_periodNum,
  1239. 'CALC_MONTH' => $this->_calcYearMonth,
  1240. 'CREATE_AT' => time()
  1241. ],
  1242. $everyData['MONTH_PV']
  1243. );
  1244. array_push($allUser, $parent['PARENT_UID']);
  1245. if ($parentUser['IS_STUDIO']) {
  1246. CalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  1247. 'STORE_PV_GRAND' => $everyData['MONTH_PV']
  1248. ]);
  1249. CalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  1250. $studioUid = $parent['PARENT_UID'];
  1251. return self::LOOP_FINISH;
  1252. }
  1253. unset($parent);
  1254. });
  1255. array_push($allUser, $userId);
  1256. StorePerfLog::stParendStudio($this->_periodNum,$this->_calcYearMonth, $allUser, $studioUid);
  1257. }
  1258. }
  1259. unset($allData);
  1260. return $this->storePerf($offset + $this->_limit);
  1261. }
  1262. unset($allData);
  1263. return true;
  1264. }
  1265. /**
  1266. * 循环达标业绩会员,并入库
  1267. * @param int $offset
  1268. * @return bool
  1269. * @throws \yii\db\Exception
  1270. */
  1271. public function loopWriteStandardPerf($offset = 0) {
  1272. if(!$this->_isCalcMonth){
  1273. return true;
  1274. }
  1275. echo sprintf("时间:[%s]缓存达标业绩数据入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1276. // 从缓存列表里面从底层往上倒序获取会员
  1277. $allData = CalcCache::getHasStandardMonthPerfUsers($this->_periodNum, $offset, $this->_limit);
  1278. if($allData){
  1279. $insertDataStandardPerf = [];
  1280. foreach($allData as $userId) {
  1281. $data = CalcCache::nowStandardMonthPerf($userId, $this->_periodNum);
  1282. $baseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1283. $insertDataStandardPerf[] = [
  1284. 'ID' => SnowFake::instance()->generateId(),
  1285. 'USER_ID' => $userId,
  1286. 'LAST_DEC_LV' => $baseInfo['DEC_LV'],
  1287. 'LAST_EMP_LV' => $baseInfo['EMP_LV'],
  1288. 'LAST_STATUS' => $baseInfo['STATUS'],
  1289. 'AMOUNT_PCS' => $data['AMOUNT_PCS'],
  1290. 'AMOUNT_PSS' => $data['AMOUNT_PSS'],
  1291. 'CALC_MONTH' => $this->_calcYearMonth,
  1292. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1293. 'CREATED_AT' => Date::nowTime(),
  1294. ];
  1295. unset($data, $baseInfo, $userId);
  1296. }
  1297. PerfStandard::batchInsert($insertDataStandardPerf);
  1298. unset($insertDataStandardPerf, $allData);
  1299. return $this->loopWriteStandardPerf($offset + $this->_limit);
  1300. }
  1301. unset($allData);
  1302. return true;
  1303. }
  1304. /**
  1305. * 本期业绩数据
  1306. * @param $userId
  1307. * @return array
  1308. */
  1309. public function nowPeriodPerfData($userId){
  1310. $data = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
  1311. $baseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1312. $result = [
  1313. 'ID' => SnowFake::instance()->generateId(),
  1314. 'USER_ID' => $userId,
  1315. 'LAST_DEC_LV' => $baseInfo['DEC_LV'],
  1316. 'LAST_EMP_LV' => $baseInfo['EMP_LV'],
  1317. 'LAST_STATUS' => $baseInfo['STATUS'],
  1318. 'FX_AMOUNT_CASH' => $data['FX_AMOUNT_CASH'],
  1319. 'PV_PCS' => $data['PV_PCS'],
  1320. 'PV_PSS' => $data['PV_PSS'],
  1321. 'PV_PCS_ZC' => $data['PV_PCS_ZC'],
  1322. 'PV_PCS_YH' => $data['PV_PCS_YH'],
  1323. 'PV_PCS_ZG' => $data['PV_PCS_ZG'],
  1324. 'PV_PCS_LS' => $data['PV_PCS_LS'],
  1325. 'PV_PCS_FX' => $data['PV_PCS_FX'],
  1326. 'PV_PCS_FX_CASH' => $data['PV_PCS_FX_CASH'],
  1327. 'PV_PCS_FX_POINT' => $data['PV_PCS_FX_POINT'],
  1328. 'PV_1L' => $data['PV_1L'],
  1329. 'PV_1L_TOUCH' => $data['PV_1L_TOUCH'],
  1330. 'PV_1L_ZC' => $data['PV_1L_ZC'],
  1331. 'PV_1L_YH' => $data['PV_1L_YH'],
  1332. 'PV_1L_ZG' => $data['PV_1L_ZG'],
  1333. 'PV_1L_LS' => $data['PV_1L_LS'],
  1334. 'PV_1L_FX' => $data['PV_1L_FX'],
  1335. 'PV_2L' => $data['PV_2L'],
  1336. 'PV_2L_TOUCH' => $data['PV_2L_TOUCH'],
  1337. 'PV_2L_ZC' => $data['PV_2L_ZC'],
  1338. 'PV_2L_YH' => $data['PV_2L_YH'],
  1339. 'PV_2L_ZG' => $data['PV_2L_ZG'],
  1340. 'PV_2L_LS' => $data['PV_2L_LS'],
  1341. 'PV_2L_FX' => $data['PV_2L_FX'],
  1342. 'PV_3L' => $data['PV_3L'],
  1343. 'PV_3L_TOUCH' => $data['PV_3L_TOUCH'],
  1344. 'PV_3L_ZC' => $data['PV_3L_ZC'],
  1345. 'PV_3L_YH' => $data['PV_3L_YH'],
  1346. 'PV_3L_ZG' => $data['PV_3L_ZG'],
  1347. 'PV_3L_LS' => $data['PV_3L_LS'],
  1348. 'PV_3L_FX' => $data['PV_3L_FX'],
  1349. 'PV_4L' => $data['PV_4L'],
  1350. 'PV_4L_TOUCH' => $data['PV_4L_TOUCH'],
  1351. 'PV_4L_ZC' => $data['PV_4L_ZC'],
  1352. 'PV_4L_YH' => $data['PV_4L_YH'],
  1353. 'PV_4L_ZG' => $data['PV_4L_ZG'],
  1354. 'PV_4L_LS' => $data['PV_4L_LS'],
  1355. 'PV_4L_FX' => $data['PV_4L_FX'],
  1356. 'PV_5L' => $data['PV_5L'],
  1357. 'PV_5L_TOUCH' => $data['PV_5L_TOUCH'],
  1358. 'PV_5L_ZC' => $data['PV_5L_ZC'],
  1359. 'PV_5L_YH' => $data['PV_5L_YH'],
  1360. 'PV_5L_ZG' => $data['PV_5L_ZG'],
  1361. 'PV_5L_LS' => $data['PV_5L_LS'],
  1362. 'PV_5L_FX' => $data['PV_5L_FX'],
  1363. 'PV_LS_TOUCH' => $data['PV_LS_TOUCH'],
  1364. 'SURPLUS_1L' => $data['SURPLUS_1L'],
  1365. 'SURPLUS_2L' => $data['SURPLUS_2L'],
  1366. 'SURPLUS_3L' => $data['SURPLUS_3L'],
  1367. 'SURPLUS_4L' => $data['SURPLUS_4L'],
  1368. 'SURPLUS_5L' => $data['SURPLUS_5L'],
  1369. 'SURPLUS_LS' => $data['SURPLUS_LS'],
  1370. 'PERIOD_NUM' => $this->_periodNum,
  1371. 'CALC_MONTH' => $this->_calcYearMonth,
  1372. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1373. 'CREATED_AT' => Date::nowTime(),
  1374. ];
  1375. unset($data);
  1376. return $result;
  1377. }
  1378. /**
  1379. * 本月业绩
  1380. * @param $userId
  1381. * @return array
  1382. */
  1383. public function nowMonthPerfData($userId){
  1384. $data = CalcCache::nowMonthPerf($userId, $this->_periodNum);
  1385. $baseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1386. $result = [
  1387. 'ID' => SnowFake::instance()->generateId(),
  1388. 'USER_ID' => $userId,
  1389. 'LAST_DEC_LV' => $baseInfo['DEC_LV'],
  1390. 'LAST_EMP_LV' => $data['EMP_LEVEL'],
  1391. 'LAST_STATUS' => $baseInfo['STATUS'],
  1392. 'FX_AMOUNT_CASH' => $data['FX_AMOUNT_CASH'],
  1393. 'PV_PCS' => $data['PV_PCS'],
  1394. 'PV_PCS_FX' => $data['PV_PCS_FX'],
  1395. 'PV_PSS' => $data['PV_PSS'],
  1396. 'PV_1L' => $data['PV_1L'],
  1397. 'PV_2L' => $data['PV_2L'],
  1398. 'PV_3L' => $data['PV_3L'],
  1399. 'PV_4L' => $data['PV_4L'],
  1400. 'PV_5L' => $data['PV_5L'],
  1401. 'VIP_PV_1L_ZC' => $data['VIP_PV_1L_ZC'],
  1402. 'VIP_PV_2L_ZC' => $data['VIP_PV_2L_ZC'],
  1403. 'VIP_PV_3L_ZC' => $data['VIP_PV_3L_ZC'],
  1404. 'VIP_PV_4L_ZC' => $data['VIP_PV_4L_ZC'],
  1405. 'VIP_PV_5L_ZC' => $data['VIP_PV_5L_ZC'],
  1406. 'PV_1L_TOTAL' => $data['PV_1L_TOTAL'],
  1407. 'PV_2L_TOTAL' => $data['PV_2L_TOTAL'],
  1408. 'PV_3L_TOTAL' => $data['PV_3L_TOTAL'],
  1409. 'PV_4L_TOTAL' => $data['PV_4L_TOTAL'],
  1410. 'PV_5L_TOTAL' => $data['PV_5L_TOTAL'],
  1411. 'PV_PSS_TOTAL' => $data['PV_PSS_TOTAL'],
  1412. 'CF_PERCENT' => $data['CF_PERCENT'],
  1413. 'LX_PERCENT' => $data['LX_PERCENT'],
  1414. 'FX_STATUS' => $data['FX_STATUS'],
  1415. 'CALC_MONTH' => $this->_calcYearMonth,
  1416. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1417. 'CREATED_AT' => Date::nowTime(),
  1418. ];
  1419. unset($data);
  1420. return $result;
  1421. }
  1422. /**
  1423. * 循环父级并执行回调函数
  1424. * @param $userId
  1425. * @param callable $callbackFunc
  1426. * @param int $offset
  1427. * @return bool
  1428. */
  1429. public function loopNetworkParentDo($userId, callable $callbackFunc, int $offset = 0) {
  1430. $allParents = Cache::getAllNetworkParents($userId);
  1431. $allData = array_slice($allParents, $offset, $this->_limit);
  1432. unset($allParents);
  1433. if ($allData) {
  1434. foreach ($allData as $data) {
  1435. $funcResult = $callbackFunc($data);
  1436. if ($funcResult === self::LOOP_FINISH) {
  1437. return true;
  1438. } elseif ($funcResult === self::LOOP_CONTINUE) {
  1439. continue;
  1440. }
  1441. unset($data, $funcResult);
  1442. }
  1443. unset($allData);
  1444. return $this->loopNetworkParentDo($userId, $callbackFunc, $offset + $this->_limit);
  1445. }
  1446. return true;
  1447. }
  1448. /**
  1449. * 循环推荐网络的父级
  1450. * @param $userId
  1451. * @param callable $callbackFunc
  1452. * @param int $offset
  1453. * @return bool
  1454. */
  1455. public function loopRelationParentDo($userId, callable $callbackFunc, int $offset = 0) {
  1456. $allParents = Cache::getAllRelationParents($userId);
  1457. $allData = array_slice($allParents, $offset, $this->_limit);
  1458. unset($allParents);
  1459. if ($allData) {
  1460. foreach ($allData as $data) {
  1461. $funcResult = $callbackFunc($data);
  1462. if ($funcResult === self::LOOP_FINISH) {
  1463. return true;
  1464. } elseif ($funcResult === self::LOOP_CONTINUE) {
  1465. continue;
  1466. }
  1467. unset($data, $funcResult);
  1468. }
  1469. unset($allData);
  1470. return $this->loopRelationParentDo($userId, $callbackFunc, $offset + $this->_limit);
  1471. }
  1472. return true;
  1473. }
  1474. /**
  1475. * ==== 聘级计算开始 ====
  1476. */
  1477. /**
  1478. * 查看会员聘级
  1479. * @param $userId
  1480. * @param $nowMonthPerf
  1481. * @param $userEmpLevelId
  1482. * @return bool|mixed|\yii\db\ActiveRecord
  1483. */
  1484. public function checkEmpLevel($userId, $nowMonthPerf, $userEmpLevelId) {
  1485. $empLevel = $this->_empLevelConfig;
  1486. $userEmpLevel = $empLevel[$userEmpLevelId];
  1487. $userEmpLevelSort = $userEmpLevel['SORT'] ?? EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'];
  1488. $childEmpLevelNumArr = CalcCache::hasEmpLevelNum($userId, $this->_periodNum);
  1489. // 判断主任到首席总监
  1490. $resultLevel = $this->isEmpLevelOther($empLevel, $childEmpLevelNumArr, $userEmpLevelSort);
  1491. unset($empLevel, $childEmpLevelNumArr);
  1492. if( empty($resultLevel) ) {
  1493. //不降级
  1494. if ( $userEmpLevelSort < EmployLevel::EMP_LEVEL_SORT['JX_ZR_LEVEL'] ) {
  1495. //主任
  1496. if ($checkLevel = $this->isEmpLevel1($userId, $nowMonthPerf)) {
  1497. $resultLevel = $checkLevel;
  1498. } else {// 无聘级会员
  1499. $resultLevel = EmployLevel::getLevelFromSort(EmployLevel::EMP_LEVEL_SORT['NO_LEVEL']);
  1500. }
  1501. }else {
  1502. $resultLevel = $userEmpLevel;
  1503. }
  1504. }
  1505. unset($userEmpLevel, $userEmpLevelSort);
  1506. // 获取到级别以后,给上级的相应人数中追加数量
  1507. $parentRecUserId = $userId;
  1508. $this->loopRelationParentDo($userId, function ($parent) use ($resultLevel, &$parentRecUserId) {
  1509. if( !$parentRecUserId ) return self::LOOP_FINISH;
  1510. //判断$parentRecUserId是否为$parent['PARENT_UID']的直推
  1511. $parentUid = $parent['PARENT_UID'];
  1512. $toInfo = CalcCache::getUserInfo($parentRecUserId, $this->_periodNum);
  1513. if( !isset($toInfo['REC_UID']) ) {
  1514. echo $parentRecUserId . PHP_EOL;
  1515. }
  1516. if( isset($toInfo['REC_UID']) && $parentUid !== $toInfo['REC_UID'] ) {
  1517. $parentUid = $toInfo['REC_UID'];
  1518. }
  1519. unset($toInfo);
  1520. if( !$parentUid ) self::LOOP_FINISH;
  1521. CalcCache::hasEmpLevelNum($parentUid, $this->_periodNum, [$parentRecUserId => [$resultLevel['ID'] => 1]]);
  1522. //每次记录上次的USER_ID
  1523. $parentRecUserId = $parentUid;
  1524. });
  1525. return $resultLevel;
  1526. }
  1527. /**
  1528. * 是否达到主任
  1529. * @param $userId
  1530. * @param $nowMonthPerf
  1531. * @return bool|mixed|\yii\db\ActiveRecord
  1532. * @throws \yii\db\Exception
  1533. */
  1534. public function isEmpLevel1($userId, $nowMonthPerf) {
  1535. $level1Option = EmployLevel::getLevelFromSort(EmployLevel::EMP_LEVEL_SORT['JX_ZR_LEVEL']);
  1536. //条件去除最大部门,其它部门累计50万
  1537. if( $nowMonthPerf['PV_PSS_TOTAL'] < $level1Option['OTHER_DEPART_PERF'] ) {//本身业绩就小于50万
  1538. unset($level1Option);
  1539. return false;
  1540. }
  1541. $oneDeepRelation = CalcCache::getChildrenOneDeepFromRedis($userId, $this->_periodNum);
  1542. if ( count($oneDeepRelation) <= 1 ) {//只有一个区就不判断了
  1543. unset($oneDeepRelation, $level1Option);
  1544. return false;
  1545. }
  1546. $maxPvPSS = 0;
  1547. $childPvPssTotalSum = 0;
  1548. foreach ($oneDeepRelation as $childData) {
  1549. //往期业绩
  1550. $userLastPerf = CalcCache::userPerf($childData['USER_ID'], $this->_periodNum);
  1551. //本期业绩
  1552. $periodPerf = CalcCache::nowPeriodPerf($childData['USER_ID'], $this->_periodNum);
  1553. $pvPcsTotal = $userLastPerf['PV_PCS_ZC'] + $userLastPerf['PV_PCS_FX'] + $periodPerf['PV_PCS'];
  1554. $pvPssTotal = $userLastPerf['PV_PSS_TOTAL'] + $periodPerf['PV_PSS'];
  1555. $childPvPssTotal = $pvPcsTotal + $pvPssTotal;
  1556. unset($userLastPerf, $periodPerf, $pvPcsTotal, $pvPssTotal);
  1557. $childPvPssTotalSum += $childPvPssTotal;
  1558. if( $childPvPssTotal >= $maxPvPSS ) {
  1559. $maxPvPSS = $childPvPssTotal;
  1560. }
  1561. unset($childData, $childPvPssTotal);
  1562. }
  1563. unset($oneDeepRelation);
  1564. if( $childPvPssTotalSum - $maxPvPSS >= $level1Option['OTHER_DEPART_PERF'] ) {
  1565. unset($maxPvPSS, $childPvPssTotalSum);
  1566. return $level1Option;
  1567. }else {
  1568. unset($maxPvPSS, $childPvPssTotalSum, $level1Option);
  1569. return false;
  1570. }
  1571. }
  1572. /**
  1573. * 是否达到其他级别
  1574. * @param $empLevel
  1575. * @param $childEmpLevelNumArr
  1576. * @param $userEmpLevelSort
  1577. * @return bool|mixed|\yii\db\ActiveRecord
  1578. */
  1579. public function isEmpLevelOther($empLevel, $childEmpLevelNumArr, $userEmpLevelSort) {
  1580. $resultLevel = [];
  1581. foreach ($empLevel as $level) {
  1582. if ($level['SORT'] < EmployLevel::EMP_LEVEL_SORT['JX_ZR_LEVEL']) continue;
  1583. //不降级
  1584. if ($level['SORT'] <= $userEmpLevelSort) continue;
  1585. // 级别要求有几个区
  1586. $locationNum = $level['LOCATION_NUM'];
  1587. // 级别要求这几个区分别存在级别会员的数量
  1588. $minEmpNum = $level['MIN_EMPLOY_NUM'];
  1589. $minEmpLevel = $level['MIN_EMPLOY_LEVEL'];
  1590. if( !$minEmpLevel || !isset($empLevel[$minEmpLevel]) || $minEmpNum < 1 ) continue;
  1591. $minEmpLevelSort = $empLevel[$minEmpLevel]['SORT'];
  1592. $resultEmpLevelNumArr = array_filter($childEmpLevelNumArr, function ($item, $departUserId) use ($empLevel, $minEmpLevelSort, $minEmpNum) {
  1593. $tempEmpLevelNum = 0;
  1594. foreach ($item as $levelIndex => $levelNum) {
  1595. if ($empLevel[$levelIndex]['SORT'] >= $minEmpLevelSort && $levelNum >= $minEmpNum) {
  1596. $tempEmpLevelNum += 1;
  1597. }
  1598. }
  1599. return $tempEmpLevelNum >= 1;
  1600. }, ARRAY_FILTER_USE_BOTH);
  1601. if (count($resultEmpLevelNumArr) >= $locationNum) {
  1602. if( $level['SORT'] == EmployLevel::EMP_LEVEL_SORT['SHX_ZJ_LEVEL'] ) {
  1603. //首席总监还要满足一个条件、任意部门1个高级总监
  1604. $gjZjLevelNumArr = array_filter($childEmpLevelNumArr, function ($item, $departUserId) use ($empLevel) {
  1605. $tempEmpLevelNum = 0;
  1606. foreach ($item as $levelIndex => $levelNum) {
  1607. if ($empLevel[$levelIndex]['SORT'] >= EmployLevel::EMP_LEVEL_SORT['GJ_ZJ_LEVEL'] && $levelNum >= 1) {
  1608. $tempEmpLevelNum += 1;
  1609. }
  1610. }
  1611. return $tempEmpLevelNum >= 1;
  1612. }, ARRAY_FILTER_USE_BOTH);
  1613. if( count($gjZjLevelNumArr) >= 1 ) {
  1614. $resultLevel = $level;
  1615. }
  1616. unset($gjZjLevelNumArr);
  1617. }else {
  1618. $resultLevel = $level;
  1619. }
  1620. }
  1621. unset($level, $minEmpLevel, $minEmpLevelSort, $resultEmpLevelNumArr);
  1622. }
  1623. unset($empLevel, $childEmpLevelNumArr, $userEmpLevelSort);
  1624. return $resultLevel;
  1625. }
  1626. /**
  1627. * 是否可拿业绩(即注销、关停、停发状态)
  1628. * @param $userId
  1629. * @return bool
  1630. */
  1631. public function isHasPerf($userId) {
  1632. //@todo 所有人都有业绩
  1633. return true;
  1634. }
  1635. /**
  1636. * ==== 聘级计算结束 ====
  1637. */
  1638. /**
  1639. * 更新百分比并发送
  1640. * @param $percent
  1641. */
  1642. private function _updatePercent($percent) {
  1643. // 把数据写入数据库中
  1644. Period::updateAll(['PERF_PERCENT' => $percent], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  1645. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin($percent, ['MODEL' => 'PERIOD', 'ID' => $this->_periodId, 'FIELD' => 'PERF_PERCENT']);
  1646. }
  1647. }