PerfCalc.php 80 KB

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