BonusSend.php 43 KB

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