PerfCalc.php 80 KB

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