PerfCalc.php 80 KB

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