BonusSend.php 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/3/18
  6. * Time: 上午11:06
  7. */
  8. namespace common\helpers\bonus;
  9. use common\components\ActiveRecord;
  10. use common\helpers\DataBak;
  11. use common\helpers\Form;
  12. use common\helpers\LoggerTool;
  13. use common\helpers\Tool;
  14. use common\helpers\user\Balance;
  15. use common\helpers\user\Info;
  16. use common\helpers\user\Reconsume;
  17. use common\helpers\user\Status;
  18. use common\libs\api\sms\SmsApi;
  19. use common\libs\swoole\Process;
  20. use common\models\DealType;
  21. use common\models\FlowRemainPv;
  22. use common\models\PerfPeriod;
  23. use common\models\DecOrder;
  24. use common\models\EmployLevel;
  25. use common\models\StarCrownLevel;
  26. use common\models\UserBonus;
  27. use common\models\UserPerfMonthUpdate;
  28. use common\models\UserPeriodPoints;
  29. use common\models\UserWallet;
  30. use common\models\UserInfo;
  31. use common\models\UserPerf;
  32. use common\models\UserPerfUpdate;
  33. use common\models\UserTeamwork;
  34. use common\models\YearHighestEmpLv;
  35. use common\models\Order;
  36. use common\models\RemainPv;
  37. use common\models\forms\OrderForm;
  38. use yii\base\BaseObject;
  39. use yii\base\StaticInstanceTrait;
  40. use common\helpers\Cache;
  41. use common\helpers\Date;
  42. use common\models\CalcBonus;
  43. use common\models\CalcBonusBD;
  44. use common\models\CalcBonusBS;
  45. use common\models\CalcBonusGX;
  46. use common\models\CalcBonusQY;
  47. use common\models\CalcBonusTG;
  48. use common\models\FlowBonus;
  49. use common\models\PerfActiveUser;
  50. use common\models\PerfMonth;
  51. use common\models\User;
  52. use Yii;
  53. use common\models\Period;
  54. use yii\base\Exception;
  55. use yii\db\Expression;
  56. class BonusSend extends BaseObject {
  57. use StaticInstanceTrait;
  58. private $_limit = 1000;
  59. private $_appUserId = null;
  60. private $_sysConfig = [];
  61. private $_decLevelConfig = [];
  62. private $_empLevelConfig = [];
  63. private $_decRoleConfig = [];
  64. private $_errors = [];
  65. private $_periodNum = 0;
  66. private $_periodId;
  67. private $_isCalcMonth = 0;
  68. private $_isCalcYear = 0;
  69. private $_calcYear;
  70. private $_calcMonth;
  71. private $_calcYearMonth;
  72. private $_lastCalcYear;
  73. private $_lastCalcMonth;
  74. private $_lastCalcYearMonth;
  75. private $_workerId;
  76. private $_workerNum;
  77. public function init(int $periodNum = 0, $appUserId = null, $workerId = null, $workerNum = null) {
  78. parent::init();
  79. $this->_sysConfig = Cache::getSystemConfig();
  80. $this->_decLevelConfig = Cache::getDecLevelConfig();
  81. $this->_empLevelConfig = Cache::getEmpLevelConfig();
  82. $this->_decRoleConfig = CalcCache::getDecRoleConfig($this->_periodNum);
  83. $this->_periodNum = $periodNum;
  84. $this->_appUserId = $appUserId;
  85. $this->_workerId = $workerId;
  86. $this->_workerNum = $workerNum;
  87. }
  88. /**
  89. * 设置期数
  90. * @param int $periodNum
  91. * @return int
  92. */
  93. public function setPeriodNum(int $periodNum) {
  94. return $this->_periodNum = $periodNum;
  95. }
  96. /**
  97. * 获取期数
  98. * @return int
  99. */
  100. public function getPeriodNum() {
  101. return $this->_periodNum;
  102. }
  103. /**
  104. * 加入错误错误
  105. * @param $attr
  106. * @param $error
  107. */
  108. public function addError($attr, $error) {
  109. $this->_errors[$attr][] = $error;
  110. }
  111. /**
  112. * 获取错误信息
  113. * @return array
  114. */
  115. public function getErrors() {
  116. return $this->_errors;
  117. }
  118. /**
  119. * 挂网时处理虚假订单
  120. *
  121. */
  122. public function putFakeOrder() {
  123. echo('开始处理-假订单' . PHP_EOL);
  124. $fakeOrder= Order::find()->where(['PERIOD_NUM'=>$this->_periodNum, 'IS_AUTO'=>'1'])->asArray()->all();
  125. // print_r($fakeOrder);exit;
  126. foreach($fakeOrder as $fOrder){
  127. $oRemainPv=RemainPv::findOne(["USER_ID"=>$fOrder['USER_ID']]);
  128. $transactionRemain = \Yii::$app->db->beginTransaction();
  129. try{
  130. $flowRemainPvModel = new FlowRemainPv();
  131. $flowRemainPvModel->ID = $this->_generateSn();
  132. $flowRemainPvModel->USER_ID = $fOrder['USER_ID'];
  133. $flowRemainPvModel->REMAIN_PV_FLOW = -30;
  134. $flowRemainPvModel->REMAIN_PV_TOTAL = $oRemainPv->REMAIN_PV - 30;
  135. $flowRemainPvModel->PERIOD_NUM = $this->_periodNum;
  136. $flowRemainPvModel->UPDATED_AT = Date::nowTime();
  137. $flowRemainPvModel->ORDER_SN = $fOrder['SN'];
  138. if(!$flowRemainPvModel->save()){
  139. $this->addErrors($flowRemainPvModel->getErrors());
  140. return false;
  141. }
  142. $oRemainPv->updateCounters(['REMAIN_PV'=>-30]);
  143. $transactionRemain->commit();
  144. } catch (Exception $e) {
  145. $transactionRemain->rollBack();
  146. $this->addError('add', $e->getMessage());
  147. return null;
  148. }
  149. }
  150. echo('假订单处理完' . PHP_EOL);
  151. return true; // $flowRemainPvModel;
  152. }
  153. /**
  154. * 生成流水号
  155. * @return string
  156. */
  157. private function _generateSn() {
  158. return Date::today('Ymd') . $this->_random(10, 1);
  159. }
  160. /**
  161. * 生成随机数
  162. * @param $length
  163. * @param int $numeric
  164. * @return string
  165. */
  166. private function _random($length, $numeric = 0) {
  167. $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
  168. $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
  169. $hash = '';
  170. $max = strlen($seed) - 1;
  171. for ($i = 0; $i < $length; $i++) {
  172. $hash .= $seed[mt_rand(0, $max)];
  173. }
  174. return $hash;
  175. }
  176. /**
  177. * 进行奖金发放步骤
  178. * @return bool
  179. */
  180. public function sendStep() {
  181. try {
  182. $t1 = microtime(true);
  183. // 初始化
  184. $this->initTask();
  185. echo('挂网开始');
  186. $this->putFakeOrder();
  187. // 先把有remainPv的订单处理一下,将remainPv加入到remain_pv及流水表
  188. echo('处理当期REMAIN PV ' . date('Y-m-d H:i:s', time()) . PHP_EOL);
  189. $this->_calcRemainPv();
  190. $t2 = microtime(true);
  191. echo('初始化完成,当前期数【' . $this->_periodNum . '】,耗时:' . round($t2 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  192. // 改变状态
  193. $this->setSendStatus('start');
  194. $t3 = microtime(true);
  195. echo('改变状态完成,耗时:' . round($t3 - $t2, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  196. $this->_updatePercent(20);
  197. //Yii::$app->db->close();
  198. //Yii::$app->dbShop->close();
  199. // 更新StarDirectory
  200. $this->updateEmpLevel();
  201. $this->_updatePercent(40);
  202. $t4 = microtime(true);
  203. echo('更新聘级完成,耗时:' . round($t4 - $t3, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  204. // 更新StarCrown
  205. $this->updateCrownLevel();
  206. $this->_updatePercent(50);
  207. $t41 = microtime(true);
  208. echo('更新星级完成,耗时:' . round($t41 - $t4, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  209. // 发放奖金
  210. $this->sendBonusLoop();
  211. $this->_updatePercent(60);
  212. $t5 = microtime(true);
  213. echo('发放奖金完成,耗时:' . round($t5 - $t4, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  214. // 更新会员上次报单级别
  215. // $this->updateUserDevLv();
  216. // $this->_updatePercent(80);
  217. $t6 = microtime(true);
  218. // echo('更新会员上次报单级别完成,耗时:' . round($t6 - $t5, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  219. // 更新会员累计业绩
  220. $this->updateUserPerf();
  221. $this->_updatePercent(80);
  222. $t7 = microtime(true);
  223. echo('更新会员累计业绩完成,耗时:' . round($t7 - $t6, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  224. $this->updateUserPerfMonth();
  225. $this->_updatePercent(90);
  226. $t8 = microtime(true);
  227. echo('更新会员累计月业绩完成,耗时:' . round($t8 - $t7, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  228. // 更新活跃用户状态,更新为已处理
  229. // $this->updateActiveUser();
  230. // $this->_updatePercent(95);
  231. // $t9 = microtime(true);
  232. // echo('更新会员累计月业绩完成,耗时:' . round($t9 - $t8, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  233. // 开启子进程去完成下面的循环发放和循环改聘级和循环更新累计业绩
  234. /*$process = new Process('sendBonus', 3);
  235. $process->run(function($workId, $pmid){
  236. $this->processStep($workId);
  237. });
  238. while (true){
  239. if($process->isFinish()) break;
  240. }
  241. unset($process);
  242. $this->_updatePercent(90);
  243. $t4 = microtime(true);
  244. echo('所有子进程的任务完成,耗时:'.round($t4 - $t3, 3).',内存使用:'.(round(memory_get_usage()/1024/1024, 3)).'MB'.PHP_EOL);*/
  245. echo('全部奖金发放完成,耗时:'.round($t7 - $t1, 3).',内存使用:'.(round(memory_get_usage()/1024/1024, 3)).'MB'.PHP_EOL);
  246. $this->_updatePercent(100);
  247. } catch (\Exception $e) {
  248. LoggerTool::error('sendBonus' . sprintf('File【%s】, Line【%s】, Msg【%s】', $e->getFile(), $e->getLine(), $e->getMessage()));
  249. $this->addError('sendBonus', sprintf('%s',$e->getMessage()));
  250. return false;
  251. }
  252. if (count($this->_errors) > 0) {
  253. return false;
  254. }
  255. return true;
  256. }
  257. /**
  258. * 处理order表中有remain_pv的订单
  259. * 将结果写入到remainPv相关表中
  260. *
  261. */
  262. private function _calcRemainPv(){
  263. $orders = Order::find()->where('PERIOD_NUM=:PERIOD_NUM AND REMAIN_PV>0',[':PERIOD_NUM'=>$this->_periodNum])->asArray()->all();
  264. foreach($orders as $order){
  265. $oRemainPv = RemainPv::find()->where(['USER_ID' => $order['USER_ID']])->one();
  266. $transactionRemain = \Yii::$app->db->beginTransaction();
  267. try{
  268. $flowRemainPvModel = new FlowRemainPv();
  269. $flowRemainPvModel->ID = $this->_generateSn();
  270. $flowRemainPvModel->USER_ID = $order['USER_ID'];
  271. $flowRemainPvModel->REMAIN_PV_FLOW = $order['REMAIN_PV'];
  272. $flowRemainPvModel->REMAIN_PV_TOTAL = $oRemainPv['REMAIN_PV'] + $order['REMAIN_PV'];
  273. $flowRemainPvModel->PERIOD_NUM = $this->_periodNum;
  274. $flowRemainPvModel->UPDATED_AT = Date::nowTime();
  275. $flowRemainPvModel->ORDER_SN = $order['SN'];
  276. if(!$flowRemainPvModel->save()){
  277. $this->addErrors($flowRemainPvModel->getErrors());
  278. return false;
  279. }
  280. $oRemainPv = RemainPv::find()->where(['USER_ID' => $order['USER_ID']])->one();
  281. if($oRemainPv){
  282. $oRemainPv->updateCounters(['REMAIN_PV'=>$order['REMAIN_PV']]);
  283. }else{
  284. $remainPvModel = new RemainPv();
  285. $remainPvModel->ID = $this->_generateSn();
  286. $remainPvModel->USER_ID = $order['USER_ID'];
  287. $remainPvModel->UPDATED_AT = Date::nowTime();
  288. $remainPvModel->REMAIN_PV = $order['REMAIN_PV'];
  289. $remainPvModel->STATUS = 1;
  290. if(!$remainPvModel->save()){
  291. $this->addErrors($remainPvModel->getErrors());
  292. return false;
  293. }
  294. }
  295. $transactionRemain->commit();
  296. } catch (Exception $e) {
  297. $transactionRemain->rollBack();
  298. $this->addError('add', $e->getMessage());
  299. return null;
  300. }
  301. }
  302. return null;
  303. }
  304. /**
  305. * 需要多进程执行的任务
  306. * @param $workId
  307. * @throws \yii\db\Exception
  308. */
  309. public function processStep($workId = null) {
  310. if ($workId == 1 || $workId === null) {
  311. // 发放奖金
  312. $this->sendBonusLoop();
  313. }
  314. if ($workId == 2 || $workId === null) {
  315. // 更新聘级
  316. $this->updateEmpLevel();
  317. }
  318. if ($workId == 3 || $workId === null) {
  319. // 更新会员累计业绩
  320. $this->updateUserPerf();
  321. }
  322. Yii::$app->db->close();
  323. Yii::$app->dbShop->close();
  324. }
  325. /**
  326. * 初始化发放任务
  327. * @throws Exception
  328. */
  329. public function initTask() {
  330. $this->_errors = [];
  331. $periodNum = $this->_periodNum;
  332. // 获取传入期数的相关信息
  333. $periodObj = Period::instance();
  334. $periodDataArr = $periodObj->setPeriodNum($periodNum);
  335. $this->_periodId = $periodDataArr['ID'];
  336. $this->_isCalcMonth = $periodObj->isCalcMonth($periodNum);
  337. $this->_calcYear = $periodObj->getYear($periodNum);
  338. $this->_calcMonth = $periodObj->getMonth($periodNum);
  339. $this->_calcYearMonth = $periodObj->getYearMonth($periodNum);
  340. $lastYearMonthArr = $periodObj->getLastMonth($periodNum);
  341. $this->_lastCalcYear = $lastYearMonthArr['year'];
  342. $this->_lastCalcMonth = $lastYearMonthArr['month'];
  343. $this->_lastCalcYearMonth = $lastYearMonthArr['yearMonth'];
  344. }
  345. /**
  346. * 设置结算状态
  347. * @param $type
  348. * start|end|fail
  349. */
  350. public function setSendStatus($type) {
  351. Yii::$app->db->close();
  352. if ($type == 'start') {
  353. Period::updateAll(['IS_SENDING' => 1, 'SEND_STARTED_AT' => Date::nowTime()], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  354. } elseif ($type == 'end') {
  355. Period::updateAll(['IS_SENDING' => 0, 'IS_SENT' => Period::SEND_FINISH, 'SENT_AT' => Date::nowTime()], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  356. } elseif ($type == 'fail') {
  357. Period::updateAll(['IS_SENDING' => 0, 'IS_SENT' => Period::SEND_FAIL, 'SENT_AT' => 0], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  358. }
  359. }
  360. /**
  361. * 结束计算任务
  362. * @throws \yii\db\Exception
  363. */
  364. public function endTask() {
  365. CalcCache::clearAll($this->_periodNum);
  366. $this->setSendStatus('end');
  367. //@todo 先不备份数据
  368. // DataBak::backup($this->_periodNum);
  369. //发送复销短信
  370. // if ($this->_isCalcMonth && $this->_sysConfig['smsOpen']['VALUE']) {
  371. // $this->sendSmsLoop();
  372. // }
  373. }
  374. /**
  375. * 出现错误
  376. */
  377. public function errorTask() {
  378. $this->setSendStatus('fail');
  379. }
  380. /**
  381. * 发放奖金
  382. * @param int $page
  383. * @return bool
  384. * @throws \yii\db\Exception
  385. */
  386. public function sendBonusLoop($page=1) {
  387. echo sprintf("时间:[%s]数据库发奖,当前page为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()), $page);
  388. $periodNum = $this->_periodNum;
  389. // 从奖金结算表中获取当期未发放的所有数据
  390. $allData = CalcBonus::findUseDbCalc()
  391. ->yearMonth($this->_calcYearMonth)
  392. ->where(
  393. '(IS_SENT=0 OR IS_SENT=2) AND CALC_MONTH=:CALC_MONTH AND PERIOD_NUM=:PERIOD_NUM',
  394. [':CALC_MONTH' => $this->_calcYearMonth, ':PERIOD_NUM' => $periodNum]
  395. )
  396. ->limit($this->_limit)
  397. ->asArray()
  398. ->all();
  399. if ($allData) {
  400. $transaction = Yii::$app->db->beginTransaction();
  401. try {
  402. foreach ($allData as $key => $data) {
  403. // 期奖金
  404. // $periodAmount = $data['BONUS_QY'] + $data['BONUS_YC'] + $data['BONUS_XF'] + $data['BONUS_BD'] + $data['BONUS_TG'] + $data['BONUS_YJ'] + $data['BONUS_GX'] + $data['BONUS_GL'];
  405. $periodAmount = $data['BONUS_REAL'];
  406. // 获取本期结算的管理员
  407. // $period = Period::findOneAsArray(['PERIOD_NUM'=>$periodNum]);
  408. // $updateAminId = $period['CALC_ADMIN_ID'];
  409. if ($periodAmount > 0) {
  410. Balance::changeUserBonus($data['USER_ID'], 'bonus', $periodAmount, [
  411. 'CALC_ID' => $data['ID'],
  412. 'REMARK' => 'From Period ' . $periodNum,
  413. 'PERIOD_NUM' => $periodNum,
  414. 'QY' => $data['BONUS_QY'],
  415. 'FW' => $data['BONUS_FW'],
  416. 'YC' => $data['BONUS_YC'],
  417. 'VIP' => $data['BONUS_VIP'],
  418. 'BD' => $data['BONUS_BD'],
  419. 'TG' => $data['BONUS_TG'],
  420. 'YJ' => $data['BONUS_YJ'],
  421. 'GX' => $data['BONUS_GX'],
  422. 'GL' => $data['BONUS_GL'],
  423. 'BS' => $data['BONUS_BS'],
  424. 'BS_MNT' => $data['BONUS_BS_MNT'],
  425. 'BS_ABBR' => $data['BONUS_BS_ABBR'],
  426. 'ORI_QY' => $data['ORI_BONUS_QY'],
  427. 'ORI_YC' => $data['ORI_BONUS_YC'],
  428. 'ORI_VIP' => $data['ORI_BONUS_VIP'],
  429. 'ORI_STANDARD' => $data['ORI_BONUS_STANDARD'],
  430. 'ORI_BD' => $data['ORI_BONUS_BD'],
  431. 'ORI_TG' => $data['ORI_BONUS_TG'],
  432. 'ORI_YJ' => $data['ORI_BONUS_YJ'],
  433. 'ORI_GX' => $data['ORI_BONUS_GX'],
  434. 'ORI_GL' => $data['ORI_BONUS_GL'],
  435. 'ORI_BS' => $data['ORI_BONUS_BS'],
  436. 'ORI_BS_MNT' => $data['ORI_BONUS_BS_MNT'],
  437. 'ORI_BS_ABBR' => $data['ORI_BONUS_BS_ABBR'],
  438. 'RECONSUME_POINTS_TOTAL' => $data['RECONSUME_POINTS'],
  439. 'EXCHANGE_POINTS_TOTAL' => $data['EXCHANGE_POINTS'],
  440. 'MANAGE_TAX' => $data['MANAGE_TAX'],
  441. 'BONUS_TOTAL' => $data['BONUS_TOTAL'],
  442. 'DEAL_TYPE_ID' => DealType::BONUS_SEND,
  443. 'SORT' => $key * 10,
  444. 'BONUS_ISSUE' => true,
  445. ]);
  446. // $this->_teamworkBonus($data['USER_ID'], $periodAmount, $key);
  447. }
  448. // 旅游奖
  449. if ($data['BONUS_TOURISM'] > 0) {
  450. Balance::changeUserBonus($data['USER_ID'], 'tourism_points', $data['BONUS_TOURISM'], [
  451. 'CALC_ID' => $data['ID'],
  452. 'REMARK' => 'From Period ' . $periodNum,
  453. 'PERIOD_NUM' => $periodNum,
  454. 'TOURISM_POINTS' => $data['BONUS_TOURISM'],
  455. 'DEAL_TYPE_ID' => DealType::TOURISM_SEND,
  456. 'SORT' => $key * 10,
  457. 'BONUS_ISSUE' => true,
  458. ]);
  459. }
  460. // 车奖
  461. if ($data['BONUS_GARAGE'] > 0) {
  462. Balance::changeUserBonus($data['USER_ID'], 'garage_points', $data['BONUS_GARAGE'], [
  463. 'CALC_ID' => $data['ID'],
  464. 'REMARK' => 'From Period ' . $periodNum,
  465. 'PERIOD_NUM' => $periodNum,
  466. 'GARAGE_POINTS' => $data['BONUS_GARAGE'],
  467. 'DEAL_TYPE_ID' => DealType::GARAGE_SEND,
  468. 'SORT' => $key * 10,
  469. 'BONUS_ISSUE' => true,
  470. ]);
  471. }
  472. // 房奖
  473. if ($data['BONUS_VILLA'] > 0) {
  474. Balance::changeUserBonus($data['USER_ID'], 'villa_points', $data['BONUS_VILLA'], [
  475. 'CALC_ID' => $data['ID'],
  476. 'REMARK' => 'From Period ' . $periodNum,
  477. 'PERIOD_NUM' => $periodNum,
  478. 'VILLA_POINTS' => $data['BONUS_VILLA'],
  479. 'DEAL_TYPE_ID' => DealType::VILLA_SEND,
  480. 'SORT' => $key * 10,
  481. 'BONUS_ISSUE' => true,
  482. ]);
  483. }
  484. //发放重消积分
  485. // if ($data['RECONSUME_POINTS'] > 0) {
  486. // Balance::changeUserBonus($data['USER_ID'], 'reconsume_points', $data['RECONSUME_POINTS'], [
  487. // 'CALC_ID' => $data['ID'],
  488. // 'REMARK' => 'From ' . $periodNum . '期',
  489. // 'PERIOD_NUM' => $periodNum,
  490. // 'RECONSUME_POINTS' => $data['RECONSUME_POINTS'],
  491. // 'DEAL_TYPE_ID' => DealType::RECONSUME_POINTS_SEND,
  492. // ]);
  493. // }
  494. //
  495. // //发放兑换积分
  496. // if ($data['EXCHANGE_POINTS'] > 0) {
  497. // Balance::changeUserBonus($data['USER_ID'], 'exchange_points', $data['EXCHANGE_POINTS'], [
  498. // 'CALC_ID' => $data['ID'],
  499. // 'REMARK' => 'From ' . $periodNum . '期',
  500. // 'PERIOD_NUM' => $periodNum,
  501. // 'EXCHANGE_POINTS' => $data['EXCHANGE_POINTS'],
  502. // 'DEAL_TYPE_ID' => DealType::EXCHANGE_POINTS_SEND,
  503. // 'BONUS_ISSUE' => true,
  504. // ]);
  505. // }
  506. // 把记录标记为已发放状态
  507. CalcBonus::updateAll(['IS_SENT' => 1, 'SENT_AT' => Date::nowTime()], 'ID=:ID', [':ID' => $data['ID']]);
  508. unset($periodAmount, $key, $data);
  509. }
  510. $transaction->commit();
  511. } catch (Exception $e) {
  512. $transaction->rollBack();
  513. $this->addError('sendBonus', '奖金发放失败,原因:' . $e->getFile() . ' ' . $e->getLine() . ' ' . $e->getMessage());
  514. return false;
  515. }
  516. unset($allData, $transaction);
  517. $page++;
  518. return $this->sendBonusLoop($page);
  519. }
  520. unset($allData);
  521. return true;
  522. }
  523. /**
  524. * 蓝星奖金(即新的管理奖),更新会员聘级
  525. * @param int $offset
  526. * @return bool
  527. * @throws \yii\db\Exception
  528. */
  529. public function updateEmpLevel(int $offset = 0) {
  530. if ($this->_isCalcMonth) {
  531. $empLv = EmployLevel::getIdConvertLevelSortCache();
  532. $allData = CalcBonusBS::findUseDbCalc()
  533. ->yearMonth($this->_calcYearMonth)
  534. ->where(
  535. 'CALC_MONTH=:CALC_MONTH AND PERIOD_NUM=:PERIOD_NUM',
  536. [
  537. ':CALC_MONTH' => $this->_calcYearMonth,
  538. ':PERIOD_NUM' =>$this->_periodNum
  539. ]
  540. )
  541. ->orderBy('CREATED_AT DESC')
  542. ->offset($offset)
  543. ->limit($this->_limit)
  544. ->all();
  545. //@todo 用户级别不变则不更新
  546. $defaultEmpLv = EmployLevel::getDefaultLevelId();
  547. if ($allData) {
  548. $transaction = Yii::$app->db->beginTransaction();
  549. try {
  550. foreach ($allData as $data) {
  551. //@todo 用户级别不变则不更新
  552. if( $data['LAST_EMP_LV'] === $defaultEmpLv ) continue;
  553. $nowBsEmpLv = $data['LAST_EMP_LV']; // 当前蓝星奖计算(即管理奖) 的等级
  554. $user = CalcCache::getUserInfo($data['USER_ID'], $this->_periodNum);
  555. $userEmpLv = $user['EMP_LV']; // 用户的历史最高聘级
  556. $userEmpLvSort = $empLv[$userEmpLv]; // 历史最高聘级的 级别值
  557. $nowBsEmpLvSort = $empLv[$nowBsEmpLv]; // 当前蓝星计算的聘级 级别值
  558. if ($nowBsEmpLvSort > $userEmpLvSort) {
  559. // 如果当前期的级别值大于历史最高级别,则更新用户表里的最高聘级
  560. User::updateAll(['EMP_LV' => $nowBsEmpLv], 'ID=:USER_ID', [':USER_ID' => $data['USER_ID']]);
  561. User::deleteBaseInfoFromRedis($data['USER_ID']);
  562. }
  563. // 更新最新级别
  564. User::updateAll([
  565. 'LAST_EMP_LV' => $nowBsEmpLv,
  566. 'LAST_EMP_LV_UPDATED_AT' => time(),
  567. 'LAST_EMP_LV_UPDATED_PERIOD' => $this->_periodNum
  568. ], 'ID=:USER_ID', [':USER_ID' => $data['USER_ID']]);
  569. User::deleteBaseInfoFromRedis($data['USER_ID']);
  570. unset($data);
  571. }
  572. $transaction->commit();
  573. } catch (Exception $e) {
  574. $transaction->rollBack();
  575. $this->addError('updateEmpLevel', '更新聘级失败,原因:' . $e->getMessage());
  576. return false;
  577. }
  578. unset($transaction, $allData, $defaultEmpLv);
  579. return $this->updateEmpLevel($offset + $this->_limit);
  580. }
  581. unset($allData);
  582. // 刷新会员EmpLv为0
  583. User::updateAll([
  584. 'LAST_EMP_LV' => '',
  585. 'LAST_EMP_LV_UPDATED_AT' => time(),
  586. 'LAST_EMP_LV_UPDATED_PERIOD' => $this->_periodNum,
  587. ], 'LAST_EMP_LV_UPDATED_PERIOD < :PERIOD_NUM AND LAST_EMP_LV <> ""', [':PERIOD_NUM' => $this->_periodNum]);
  588. User::deleteAllBaseInfoFromRedis();
  589. }
  590. return true;
  591. }
  592. /**
  593. * 更新用户星级
  594. * @param int $offset
  595. * @return bool
  596. */
  597. public function updateCrownLevel(int $offset = 0)
  598. {
  599. $starCrownLv = StarCrownLevel::getIdConvertLevelSortCache();
  600. $allData = CalcBonusQY::findUseDbCalc()
  601. ->yearMonth($this->_calcYearMonth)
  602. ->where(
  603. 'CALC_MONTH=:CALC_MONTH AND PERIOD_NUM=:PERIOD_NUM',
  604. [
  605. ':CALC_MONTH' => $this->_calcYearMonth,
  606. ':PERIOD_NUM' =>$this->_periodNum
  607. ]
  608. )
  609. ->orderBy('CREATED_AT DESC')
  610. ->groupBy('USER_ID')
  611. ->offset($offset)
  612. ->limit($this->_limit)
  613. ->all();
  614. $defaultCrownLv = StarCrownLevel::getDefaultLevelId();
  615. if ($allData) {
  616. $transaction = Yii::$app->db->beginTransaction();
  617. try {
  618. foreach ($allData as $data) {
  619. // 默认级别不更新
  620. if( $data['LAST_CROWN_LV'] === $defaultCrownLv ) continue;
  621. $modernCrownLv = $data['LAST_CROWN_LV']; // 本期计算出的最新级别
  622. $user = CalcCache::getUserInfo($data['USER_ID'], $this->_periodNum);
  623. $originCrownLv = $user['CROWN_LV']; // 用户的历史最高crown级别
  624. $originCrownLvSort = $starCrownLv[$originCrownLv]; // 历史最高crown级别值
  625. $modernCrownLvSort = $starCrownLv[$modernCrownLv]; // 当前计算的crown级别值
  626. if ($modernCrownLvSort > $originCrownLvSort) {
  627. $columns = [
  628. 'CROWN_LV' => $data['LAST_CROWN_LV'],
  629. 'LAST_CROWN_LV' => $data['LAST_CROWN_LV'],
  630. 'LAST_CROWN_LV_UPDATED_AT' => time(),
  631. 'LAST_CROWN_LV_UPDATED_PERIOD' => $this->_periodNum,
  632. ];
  633. } else {
  634. $columns = [
  635. 'LAST_CROWN_LV' => $data['LAST_CROWN_LV'],
  636. 'LAST_CROWN_LV_UPDATED_AT' => time(),
  637. 'LAST_CROWN_LV_UPDATED_PERIOD' => $this->_periodNum,
  638. ];
  639. }
  640. User::updateAll($columns, 'ID = :USER_ID', [':USER_ID' => $data['USER_ID']]);
  641. User::deleteBaseInfoFromRedis($data['USER_ID']);
  642. unset($data);
  643. }
  644. $transaction->commit();
  645. } catch (Exception $e) {
  646. $transaction->rollBack();
  647. $this->addError('updateStarCrownLevel', '更新StartCrown失败,原因:' . $e->getMessage());
  648. return false;
  649. }
  650. unset($transaction, $allData);
  651. return $this->updateCrownLevel($offset + $this->_limit);
  652. }
  653. // 刷新会员CrownLv为0
  654. User::updateAll([
  655. 'LAST_CROWN_LV' => '',
  656. 'LAST_CROWN_LV_UPDATED_AT' => time(),
  657. 'LAST_CROWN_LV_UPDATED_PERIOD' => $this->_periodNum,
  658. ], 'LAST_CROWN_LV_UPDATED_PERIOD < :PERIOD_NUM AND LAST_CROWN_LV <> ""' , [':PERIOD_NUM' => $this->_periodNum]);
  659. User::deleteAllBaseInfoFromRedis();
  660. return true;
  661. }
  662. // /**
  663. // * 更新会员聘级
  664. // * @param int $offset
  665. // * @return bool
  666. // * @throws \yii\db\Exception
  667. // */
  668. // public function updateEmpLevel(int $offset = 0) {
  669. // if ($this->_isCalcMonth) {
  670. // $allData = PerfMonth::findUseDbCalc()->yearMonth($this->_calcYearMonth)->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])->offset($offset)->limit($this->_limit)->all();
  671. // //@todo 用户级别不变则不更新
  672. // $defaultEmpLv = EmployLevel::getDefaultLevelId();
  673. // if ($allData) {
  674. // $transaction = Yii::$app->db->beginTransaction();
  675. // try {
  676. // foreach ($allData as $data) {
  677. // //@todo 用户级别不变则不更新
  678. // if( $data['LAST_EMP_LV'] === $defaultEmpLv ) continue;
  679. // User::updateAll(['EMP_LV' => $data['LAST_EMP_LV']], 'ID=:USER_ID', [':USER_ID' => $data['USER_ID']]);
  680. // User::deleteBaseInfoFromRedis($data['USER_ID']);
  681. // unset($data);
  682. // }
  683. // $transaction->commit();
  684. // } catch (Exception $e) {
  685. // $transaction->rollBack();
  686. // $this->addError('updateEmpLevel', '更新聘级失败,原因:' . $e->getMessage());
  687. // return false;
  688. // }
  689. // unset($transaction, $allData, $defaultEmpLv);
  690. // return $this->updateEmpLevel($offset + $this->_limit);
  691. // }
  692. // unset($allData);
  693. // }
  694. // return true;
  695. // }
  696. // 更活跃会员,将is_send改成1
  697. public function updateActiveUser() {
  698. try {
  699. $ret = PerfActiveUser::updateAll(
  700. ['IS_SENT' => 1],
  701. 'PERIOD_NUM=:PERIOD_NUM AND IS_SENT=:IS_SENT',
  702. ['IS_SENT'=>0, 'PERIOD_NUM'=>$this->_periodNum]
  703. );
  704. return $ret;
  705. } catch(Exception $e) {
  706. $this->addError('updateActiveUser', '更新活跃会员业绩期处理状态失败,原因:' . $e->getMessage());
  707. return false;
  708. }
  709. }
  710. /**
  711. * 更新会员的累计业绩
  712. * @param int $offset
  713. * @return bool
  714. * @throws \yii\db\Exception
  715. */
  716. public function updateUserPerf(int $offset = 0) {
  717. $allData = PerfPeriod::findUseDbCalc()->yearMonth($this->_calcYearMonth)->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])->orderBy('ID ASC')->offset($offset)->limit($this->_limit)->all();
  718. if ($allData) {
  719. $transaction = Yii::$app->db->beginTransaction();
  720. try {
  721. foreach ($allData as $data) {
  722. $isUpdate = false;
  723. if (UserPerf::findUseDbCalc()->where('USER_ID=:USER_ID', [':USER_ID' => $data['USER_ID']])->exists()) {
  724. // 判断本期是否已经更新过业绩
  725. if (!UserPerfUpdate::findUseDbCalc()->yearMonth($this->_calcYearMonth)->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [':USER_ID' => $data['USER_ID'], ':PERIOD_NUM' => $this->_periodNum])->exists()) {
  726. $isUpdate = true;
  727. // 更新业绩
  728. UserPerf::updateAll([
  729. 'PV_PCS_ZC' => new Expression('PV_PCS_ZC+' . $data['PV_PCS_ZC']),
  730. 'PV_PCS_YH' => new Expression('PV_PCS_YH+' . $data['PV_PCS_YH']),
  731. 'PV_PCS_ZG' => new Expression('PV_PCS_ZG+' . $data['PV_PCS_ZG']),
  732. 'PV_PCS_LS' => new Expression('PV_PCS_LS+' . $data['PV_PCS_LS']),
  733. 'PV_PCS_FX' => new Expression('PV_PCS_FX+' . $data['PV_PCS_FX']),
  734. 'PV_PSS' => new Expression('PV_PSS+' . $data['PV_PSS']),
  735. 'PV_PSS_TOTAL' => new Expression('PV_PSS_TOTAL+' . $data['PV_PSS']),
  736. 'PV_1L' => new Expression('PV_1L+' . $data['PV_1L']),
  737. 'PV_2L' => new Expression('PV_2L+' . $data['PV_2L']),
  738. 'PV_3L' => new Expression('PV_3L+' . $data['PV_3L']),
  739. 'PV_4L' => new Expression('PV_4L+' . $data['PV_4L']),
  740. 'PV_5L' => new Expression('PV_5L+' . $data['PV_5L']),
  741. 'SURPLUS_1L' => $data['SURPLUS_1L'],
  742. 'SURPLUS_2L' => $data['SURPLUS_2L'],
  743. 'SURPLUS_3L' => $data['SURPLUS_3L'],
  744. 'SURPLUS_4L' => $data['SURPLUS_4L'],
  745. 'SURPLUS_5L' => $data['SURPLUS_5L'],
  746. 'SURPLUS_1L_ZC' => $data['SURPLUS_1L_ZC'],
  747. 'SURPLUS_2L_ZC' => $data['SURPLUS_2L_ZC'],
  748. 'SURPLUS_3L_ZC' => $data['SURPLUS_3L_ZC'],
  749. 'SURPLUS_4L_ZC' => $data['SURPLUS_4L_ZC'],
  750. 'SURPLUS_5L_ZC' => $data['SURPLUS_5L_ZC'],
  751. 'SURPLUS_1L_FX' => $data['SURPLUS_1L_FX'],
  752. 'SURPLUS_2L_FX' => $data['SURPLUS_2L_FX'],
  753. 'SURPLUS_3L_FX' => $data['SURPLUS_3L_FX'],
  754. 'SURPLUS_4L_FX' => $data['SURPLUS_4L_FX'],
  755. 'SURPLUS_5L_FX' => $data['SURPLUS_5L_FX'],
  756. ], 'USER_ID=:USER_ID', [':USER_ID' => $data['USER_ID']]);
  757. }
  758. } else {
  759. $isUpdate = true;
  760. UserPerf::insertOne([
  761. 'USER_ID' => $data['USER_ID'],
  762. 'PV_PCS_ZC' => $data['PV_PCS_ZC'],
  763. 'PV_PCS_YH' => $data['PV_PCS_YH'],
  764. 'PV_PCS_ZG' => $data['PV_PCS_ZG'],
  765. 'PV_PCS_LS' => $data['PV_PCS_LS'],
  766. 'PV_PCS_FX' => $data['PV_PCS_FX'],
  767. 'PV_PSS' => $data['PV_PSS'],
  768. 'PV_PSS_TOTAL' => $data['PV_PSS'],
  769. 'PV_1L' => $data['PV_1L'],
  770. 'PV_2L' => $data['PV_2L'],
  771. 'PV_3L' => $data['PV_3L'],
  772. 'PV_4L' => $data['PV_4L'],
  773. 'PV_5L' => $data['PV_5L'],
  774. 'SURPLUS_1L' => $data['SURPLUS_1L'],
  775. 'SURPLUS_2L' => $data['SURPLUS_2L'],
  776. 'SURPLUS_3L' => $data['SURPLUS_3L'],
  777. 'SURPLUS_4L' => $data['SURPLUS_4L'],
  778. 'SURPLUS_5L' => $data['SURPLUS_5L'],
  779. 'SURPLUS_1L_ZC' => $data['SURPLUS_1L_ZC'],
  780. 'SURPLUS_2L_ZC' => $data['SURPLUS_2L_ZC'],
  781. 'SURPLUS_3L_ZC' => $data['SURPLUS_3L_ZC'],
  782. 'SURPLUS_4L_ZC' => $data['SURPLUS_4L_ZC'],
  783. 'SURPLUS_5L_ZC' => $data['SURPLUS_5L_ZC'],
  784. 'SURPLUS_1L_FX' => $data['SURPLUS_1L_FX'],
  785. 'SURPLUS_2L_FX' => $data['SURPLUS_2L_FX'],
  786. 'SURPLUS_3L_FX' => $data['SURPLUS_3L_FX'],
  787. 'SURPLUS_4L_FX' => $data['SURPLUS_4L_FX'],
  788. 'SURPLUS_5L_FX' => $data['SURPLUS_5L_FX'],
  789. 'CREATED_AT' => Date::nowTime(),
  790. ]);
  791. }
  792. if ($isUpdate) {
  793. // 变为已更新
  794. UserPerfUpdate::insertOne(['USER_ID' => $data['USER_ID'], 'PERIOD_NUM' => $this->_periodNum, 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH), 'CREATED_AT' => Date::nowTime()]);
  795. }
  796. unset($data, $isUpdate);
  797. }
  798. $transaction->commit();
  799. } catch (Exception $e) {
  800. $transaction->rollBack();
  801. $this->addError('updateUserPerf', '更新会员业绩失败,原因:' . $e->getMessage());
  802. return false;
  803. }
  804. unset($allData, $transaction);
  805. return $this->updateUserPerf($offset + $this->_limit);
  806. }
  807. unset($allData);
  808. return true;
  809. }
  810. /**
  811. * 更新会员的月剩余业绩
  812. * @param int $offset
  813. * @return bool
  814. * @throws \yii\db\Exception
  815. */
  816. public function updateUserPerfMonth(int $offset = 0) {
  817. // 月结,如果不是月结点,则直接退出
  818. if (!$this->_isCalcMonth) {
  819. return true;
  820. }
  821. $allData = PerfMonth::findUseDbCalc()->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])->orderBy('ID ASC')->offset($offset)->limit($this->_limit)->all();
  822. if ($allData) {
  823. $transaction = Yii::$app->db->beginTransaction();
  824. try {
  825. foreach ($allData as $data) {
  826. $isUpdate = false;
  827. if (UserPerf::findUseDbCalc()->where('USER_ID=:USER_ID', [':USER_ID' => $data['USER_ID']])->exists()) {
  828. // 判断本期是否已经更新过业绩
  829. if (!UserPerfMonthUpdate::findUseDbCalc()->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [':USER_ID' => $data['USER_ID'], ':CALC_MONTH' => $this->_calcYearMonth])->exists()) {
  830. $isUpdate = true;
  831. // 更新业绩
  832. UserPerf::updateAll([
  833. 'VIP_SURPLUS_1L_ZC' => $data['VIP_SURPLUS_1L_ZC'],
  834. 'VIP_SURPLUS_2L_ZC' => $data['VIP_SURPLUS_2L_ZC'],
  835. 'VIP_SURPLUS_3L_ZC' => $data['VIP_SURPLUS_3L_ZC'],
  836. 'VIP_SURPLUS_4L_ZC' => $data['VIP_SURPLUS_4L_ZC'],
  837. 'VIP_SURPLUS_5L_ZC' => $data['VIP_SURPLUS_5L_ZC']
  838. ], 'USER_ID=:USER_ID', [':USER_ID' => $data['USER_ID']]);
  839. }
  840. } else {
  841. $isUpdate = true;
  842. UserPerf::insertOne([
  843. 'USER_ID' => $data['USER_ID'],
  844. 'VIP_SURPLUS_1L_ZC' => $data['VIP_SURPLUS_1L_ZC'],
  845. 'VIP_SURPLUS_2L_ZC' => $data['VIP_SURPLUS_2L_ZC'],
  846. 'VIP_SURPLUS_3L_ZC' => $data['VIP_SURPLUS_3L_ZC'],
  847. 'VIP_SURPLUS_4L_ZC' => $data['VIP_SURPLUS_4L_ZC'],
  848. 'VIP_SURPLUS_5L_ZC' => $data['VIP_SURPLUS_5L_ZC'],
  849. 'CREATED_AT' => Date::nowTime(),
  850. ]);
  851. }
  852. if ($isUpdate) {
  853. // 变为已更新
  854. UserPerfMonthUpdate::insertOne(['USER_ID' => $data['USER_ID'], 'CALC_MONTH' => $this->_calcYearMonth, 'CREATED_AT' => Date::nowTime()]);
  855. }
  856. unset($data, $isUpdate);
  857. }
  858. $transaction->commit();
  859. } catch (Exception $e) {
  860. $transaction->rollBack();
  861. $this->addError('updateUserPerf', '更新会员月业绩失败,原因:' . $e->getMessage());
  862. return false;
  863. }
  864. unset($allData, $transaction);
  865. return $this->updateUserPerfMonth($offset + $this->_limit);
  866. }
  867. unset($allData);
  868. return true;
  869. }
  870. /**
  871. * 检测复消积分是否过期
  872. * @return bool
  873. * @throws \yii\db\Exception
  874. */
  875. public function checkReconsumePointsExpired(int $offset = 0) {
  876. $periodNum = $this->_periodNum;
  877. //一期为7天,那么365天为52.14即53期
  878. $expiredPeriodNum = $periodNum - 53;
  879. if( $expiredPeriodNum <= 0 ) return true;
  880. //查询需要过期的期数
  881. $allData = UserPeriodPoints::find()->select('ID,USER_ID,REMAINDER_POINTS')->where('PERIOD_NUM<=:PERIOD_NUM AND EXPIRED=0', ['PERIOD_NUM'=>$expiredPeriodNum])->orderBy('ID ASC')->offset($offset)->limit($this->_limit)->asArray()->all();
  882. if( $allData ) {
  883. $transaction = Yii::$app->db->beginTransaction();
  884. try{
  885. //扣除钱包的复消积分
  886. foreach ($allData as $everyData) {
  887. //过期
  888. UserPeriodPoints::updateAll([
  889. 'REMAINDER_POINTS' => 0,
  890. 'EXPIRED' => 1,
  891. 'EXPIRED_PERIOD' => $periodNum,
  892. 'EXPIRED_AT' => Date::nowTime(),
  893. ], 'ID=:ID', ['ID'=>$everyData['ID']]);
  894. if( !isset($everyData['REMAINDER_POINTS']) || !$everyData['REMAINDER_POINTS'] ) continue;
  895. UserBonus::updateAllCounters([
  896. 'RECONSUME_POINTS' => (-1) * $everyData['REMAINDER_POINTS'],
  897. ], 'USER_ID=:USER_ID', $everyData['USER_ID']);
  898. unset($everyData);
  899. }
  900. unset($periodNum, $expiredPeriodNum, $allData);
  901. $transaction->commit();
  902. }catch (\Exception $e){
  903. $transaction->rollBack();
  904. $this->addError('checkReconsumePointsExpired', '检测过期在复消积分失败,原因:' . $e->getMessage());
  905. return false;
  906. }
  907. return $this->checkReconsumePointsExpired($offset + $this->_limit);
  908. }
  909. unset($allData);
  910. return true;
  911. }
  912. /**
  913. * 更新会员上次报单级别
  914. * @return bool
  915. */
  916. public function updateUserDevLv() {
  917. $transaction = Yii::$app->dbShop->beginTransaction();
  918. try {
  919. \Yii::$app->dbShop->createCommand()->update(User::tableName(), ['LAST_DEC_LV' => new Expression('DEC_LV'), 'LAST_DEC_LV_UPDATED_AT' => Date::nowTime(), 'LAST_DEC_LV_UPDATED_PERIOD' => $this->_periodNum], 'LAST_DEC_LV!=DEC_LV')->execute();
  920. $transaction->commit();
  921. } catch (Exception $e) {
  922. $transaction->rollBack();
  923. $this->addError('updateUserDevLv', '更新会员上次报单级别失败,原因:' . $e->getMessage());
  924. return false;
  925. }
  926. return true;
  927. }
  928. /**
  929. * 给商城会员增加货款
  930. * @param $userId
  931. * @param $amount
  932. * @param $remark
  933. * @throws Exception
  934. */
  935. private function _shopAddPaymentForGoods($userId, $amount, $remark) {
  936. $db = \Yii::$app->dbShop;
  937. $transaction = $db->beginTransaction();
  938. try {
  939. ActiveRecord::batchUpdate(['PAYMENT_FOR_GOODS' => new Expression('PAYMENT_FOR_GOODS+' . abs($amount))], 'USER_ID=:USER_ID', [':USER_ID' => $userId], '{{%USER_WALLET}}', 'dbShop');
  940. // 增加流水
  941. $flowInsertData[] = [
  942. 'USER_ID' => $userId,
  943. 'USER_NAME' => Cache::getUserBaseInfo($userId)['USER_NAME'],
  944. 'DEC_LV' => Cache::getUserBaseInfo($userId)['DEC_LV'],
  945. 'ORDER_SN' => null,
  946. 'AMOUNT' => $amount,
  947. 'CREATED_AT' => Date::nowTime(),
  948. 'PERIOD_AT' => $this->_periodNum,
  949. 'IS_INCR' => 1,
  950. 'REMARK' => $remark,
  951. 'PARTITION_DATE' => Date::ociToDate(),
  952. 'WALLET_TYPE' => 'payment_for_goods',
  953. 'FROM_TYPE' => 'incr',
  954. ];
  955. ActiveRecord::batchInsert($flowInsertData, '{{%FLOW_WALLET}}', 'dbShop');
  956. $transaction->commit();
  957. } catch (Exception $e) {
  958. $transaction->rollBack();
  959. throw new Exception($e->getMessage());
  960. }
  961. }
  962. /**
  963. * 更新百分比并发送
  964. * @param $percent
  965. */
  966. private function _updatePercent($percent) {
  967. // 把数据写入数据库中
  968. Period::updateAll(['SENT_PERCENT' => $percent], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  969. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin($percent, ['MODEL' => 'PERIOD', 'ID' => $this->_periodId, 'FIELD' => 'SENT_PERCENT']);
  970. }
  971. /**
  972. * 更新最高聘级
  973. * @param $user_id
  974. * @return bool
  975. * @throws Exception
  976. */
  977. private function _updateHighestEmpLv($user_id) {
  978. $empLv = Info::getEmpLv($user_id);
  979. $highEmpLv = Info::getHighEmpLv($user_id);
  980. $empLvSort = EmployLevel::getSortById($empLv);
  981. $empLvHighSort = EmployLevel::getSortById($highEmpLv);
  982. if ($empLvHighYear = YearHighestEmpLv::findOneAsArray('USER_ID=:USER_ID AND YEAR=:YEAR', [':USER_ID' => $user_id, ':YEAR' => $this->_calcYear], 'HIGHEST_EMP_LV_SORT')) {
  983. if ($empLvSort > $empLvHighYear['HIGHEST_EMP_LV_SORT']) {
  984. YearHighestEmpLv::updateAll(['HIGHEST_EMP_LV' => $empLv, 'HIGHEST_EMP_LV_SORT' => $empLvSort, 'HIGHEST_EMP_LV_PERIOD' => $this->_periodNum, 'UPDATED_AT' => Date::nowTime()], 'USER_ID=:USER_ID AND YEAR=:YEAR', [':USER_ID' => $user_id, ':YEAR' => $this->_calcYear]);
  985. }
  986. } else {
  987. $yearHighest = new YearHighestEmpLv();
  988. $yearHighest->USER_ID = $user_id;
  989. $yearHighest->YEAR = $this->_calcYear;
  990. $yearHighest->HIGHEST_EMP_LV = $empLv;
  991. $yearHighest->HIGHEST_EMP_LV_SORT = $empLvSort;
  992. $yearHighest->HIGHEST_EMP_LV_PERIOD = $this->_periodNum;
  993. $yearHighest->CREATED_AT = Date::nowTime();
  994. if (!$yearHighest->save()) {
  995. throw new \yii\db\Exception(Form::formatErrorsForApi($yearHighest->getErrors()));
  996. }
  997. }
  998. if ($empLvSort > $empLvHighSort) {
  999. UserInfo::updateAll(['HIGHEST_EMP_LV' => $empLv, 'HIGHEST_EMP_LV_PERIOD' => $this->_periodNum], 'USER_ID=:USER_ID', [':USER_ID' => $user_id]);
  1000. //发送历史最高聘级短信
  1001. if($this->_sysConfig['smsEmpOpen']['VALUE']){
  1002. if(in_array($empLvSort,explode(",",$this->_sysConfig['smsEmp']['VALUE']))){
  1003. $realName = Info::getUserRealNameByUserId($user_id);
  1004. $empLvDate = Date::convert();
  1005. $empLvName = EmployLevel::getNameById($empLv);
  1006. $content = str_replace(['{%REAL_NAME%}', '{%EMP_LV_DATE%}', '{%EMP_LV%}'], [$realName, $empLvDate, $empLvName], $this->_sysConfig['smsContent']['VALUE']);
  1007. $params = [
  1008. 'mobiles' => Info::getUserMobileByUserId($user_id),
  1009. 'content' => $content,
  1010. ];
  1011. SmsApi::instance()->clearError();
  1012. SmsApi::instance()->goSend($params);
  1013. }
  1014. }
  1015. return true;
  1016. }
  1017. return false;
  1018. }
  1019. /**
  1020. * 点位合作奖金
  1021. * @param $userId
  1022. * @param $amount
  1023. * @param null $type
  1024. * @return bool
  1025. * @throws Exception
  1026. * @throws \yii\db\Exception
  1027. */
  1028. private function _teamworkBonus($userId, $amount, $key) {
  1029. if (!$teamwork = UserTeamwork::findAllAsArray('USER_ID!=:MAIN_UID AND MAIN_UID=:MAIN_UID AND IS_DEL=0', [':MAIN_UID' => $userId], 'USER_ID,DIVIDE_PERCENT')) return false;
  1030. $fromUserInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1031. foreach ($teamwork as $value) {
  1032. $bonus = Tool::formatPrice($amount * $value['DIVIDE_PERCENT'] * 0.01);
  1033. if ($bonus <= 0) continue;
  1034. $toUserInfo = CalcCache::getUserInfo($value['USER_ID'], $this->_periodNum);
  1035. Balance::changeUserBonus($userId, 'bonus', -abs($bonus), ['SORT' => $key*10+1, 'DEAL_TYPE_ID' => DealType::TEAMWORK_TRANSFER_OUT, 'REMARK' => 'To:' . $toUserInfo['USER_NAME'] . ' Per:' . $value['DIVIDE_PERCENT'] . '%']);
  1036. Balance::changeUserBonus($value['USER_ID'], 'bonus', abs($bonus), ['SORT' => $key*10+2, 'DEAL_TYPE_ID' => DealType::TEAMWORK_TRANSFER_IN, 'REMARK' => 'From: ' . $fromUserInfo['USER_NAME'] . ' Per:' . $value['DIVIDE_PERCENT'] . '%']);
  1037. }
  1038. }
  1039. /**
  1040. * 发送短信
  1041. * @param int $offset
  1042. * @return bool
  1043. * @throws \yii\db\Exception
  1044. */
  1045. public function sendSmsLoop(int $offset = 0) {
  1046. $allData = UserInfo::findUseDbCalc()->select('USER_ID,ALLOW_RECONSUME_SMS_TO')->where('ALLOW_RECONSUME_SMS=1')->offset($offset)->limit($this->_limit)->all();
  1047. if ($allData) {
  1048. $smsWallet = explode(",", $this->_sysConfig['smsWallet']['VALUE']);
  1049. $smsFee = $this->_sysConfig['smsFee']['VALUE'];
  1050. //获取剩余月份
  1051. $period = Period::instance();
  1052. $year = $period->getYear($this->_periodNum);
  1053. $monthLeft = Period::getMonthLeft($this->_periodNum);
  1054. $smsFee = Tool::formatPrice($smsFee * $monthLeft);
  1055. $to = Date::yearEnd();
  1056. $transaction = Yii::$app->db->beginTransaction();
  1057. try {
  1058. foreach ($allData as $data) {
  1059. $userId = $data['USER_ID'];
  1060. //过期续费
  1061. if ($data['ALLOW_RECONSUME_SMS_TO'] < Date::nowTime()) {
  1062. foreach ($smsWallet as $item) {
  1063. //1奖金钱包2现金钱包
  1064. if ($item == 1) {
  1065. //看余额是否充足
  1066. if (Balance::getAvailableBalance($userId) < $smsFee){
  1067. UserInfo::updateAll(['ALLOW_RECONSUME_SMS' => 0], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  1068. continue;
  1069. };
  1070. Balance::changeUserBonus($userId, 'bonus', -abs($smsFee), ['DEAL_TYPE_ID' => DealType::SMS, 'REMARK' => $year . '年复销提醒短信服务费'], false);
  1071. UserInfo::updateAll(['ALLOW_RECONSUME_SMS_TO' => $to], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  1072. $data['ALLOW_RECONSUME_SMS_TO'] = $to;
  1073. break;
  1074. } elseif ($item == 2) {
  1075. throw new Exception('不存在此方式');
  1076. break;
  1077. }
  1078. }
  1079. }
  1080. if ($data['ALLOW_RECONSUME_SMS_TO'] > Date::nowTime() && $mobile = Info::getUserMobileByUserId($userId)) {
  1081. $realName = Info::getUserRealNameByUserId($userId);
  1082. $reconsumPool = Reconsume::getUserReconsumePool($userId);
  1083. $lastRechargeDate = $reconsumPool['toRechargeDate'];
  1084. $isPass = $reconsumPool['isPass'] == 1 ? '合格' : '不合格';
  1085. $month = $period->getNowMonth();
  1086. $content = str_replace(['{%REAL_NAME%}', '{%LAST_RECHARGE_DATE%}', '{%MONTH%}', '{%IS_PASS%}'], [$realName, $lastRechargeDate, $month, $isPass], $this->_sysConfig['smsContent']['VALUE']);
  1087. //todo 发短信函数 待测试
  1088. $params = [
  1089. 'mobiles' => $mobile,
  1090. 'content' => $content,
  1091. ];
  1092. SmsApi::instance()->clearError();
  1093. SmsApi::instance()->goSend($params);
  1094. }
  1095. unset($userId,$mobile,$realName,$reconsumPool,$lastRechargeDate,$isPass,$month,$content);
  1096. }
  1097. $transaction->commit();
  1098. } catch (Exception $e) {
  1099. $transaction->rollBack();
  1100. return false;
  1101. }
  1102. unset($data);
  1103. return $this->sendSmsLoop($offset + $this->_limit);
  1104. }
  1105. unset($allData);
  1106. return true;
  1107. }
  1108. }