BonusSend.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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. ]);
  323. // $this->_teamworkBonus($data['USER_ID'], $periodAmount, $key);
  324. }
  325. // 旅游奖
  326. if ($data['BONUS_TRAVEL'] > 0) {
  327. Balance::changeUserBonus($data['USER_ID'], 'travel_points', $data['BONUS_TRAVEL'], [
  328. 'CALC_ID' => $data['ID'],
  329. 'REMARK' => 'From ' . $periodNum . '期',
  330. 'PERIOD_NUM' => $periodNum,
  331. 'TRAVEL' => $data['BONUS_TRAVEL'],
  332. 'DEAL_TYPE_ID' => DealType::TRAVEL_SEND,
  333. 'SORT' => $key * 10,
  334. ]);
  335. }
  336. // 名车奖
  337. if ($data['BONUS_CAR'] > 0) {
  338. Balance::changeUserBonus($data['USER_ID'], 'car_points', $data['BONUS_CAR'], [
  339. 'CALC_ID' => $data['ID'],
  340. 'REMARK' => 'From ' . $periodNum . '期',
  341. 'PERIOD_NUM' => $periodNum,
  342. 'HOUSE' => $data['BONUS_CAR'],
  343. 'DEAL_TYPE_ID' => DealType::CAR_SEND,
  344. 'SORT' => $key * 10,
  345. ]);
  346. }
  347. // 豪宅奖
  348. if ($data['BONUS_HOUSE'] > 0) {
  349. Balance::changeUserBonus($data['USER_ID'], 'house_points', $data['BONUS_HOUSE'], [
  350. 'CALC_ID' => $data['ID'],
  351. 'REMARK' => 'From ' . $periodNum . '期',
  352. 'PERIOD_NUM' => $periodNum,
  353. 'HOUSE' => $data['BONUS_HOUSE'],
  354. 'DEAL_TYPE_ID' => DealType::HOUSE_SEND,
  355. 'SORT' => $key * 10,
  356. ]);
  357. }
  358. //发放重消积分
  359. if ($data['RECONSUME_POINTS'] > 0) {
  360. Balance::changeUserBonus($data['USER_ID'], 'reconsume_points', $data['RECONSUME_POINTS'], [
  361. 'CALC_ID' => $data['ID'],
  362. 'REMARK' => 'From ' . $periodNum . '期',
  363. 'PERIOD_NUM' => $periodNum,
  364. 'RECONSUME_POINTS' => $data['RECONSUME_POINTS'],
  365. 'DEAL_TYPE_ID' => DealType::RECONSUME_POINTS_SEND,
  366. ]);
  367. }
  368. //发放兑换积分
  369. if ($data['EXCHANGE_POINTS'] > 0) {
  370. Balance::changeUserBonus($data['USER_ID'], 'exchange_points', $data['EXCHANGE_POINTS'], [
  371. 'CALC_ID' => $data['ID'],
  372. 'REMARK' => 'From ' . $periodNum . '期',
  373. 'PERIOD_NUM' => $periodNum,
  374. 'EXCHANGE_POINTS' => $data['EXCHANGE_POINTS'],
  375. 'DEAL_TYPE_ID' => DealType::EXCHANGE_POINTS_SEND,
  376. ]);
  377. }
  378. // 把记录标记为已发放状态
  379. CalcBonus::updateAll(['IS_SENT' => 1, 'SENT_AT' => Date::nowTime()], 'ID=:ID', [':ID' => $data['ID']]);
  380. unset($periodAmount, $key, $data);
  381. }
  382. $transaction->commit();
  383. } catch (Exception $e) {
  384. $transaction->rollBack();
  385. $this->addError('sendBonus', '奖金发放失败,原因:' . $e->getMessage());
  386. return false;
  387. }
  388. unset($allData, $transaction);
  389. $page++;
  390. return $this->sendBonusLoop($page);
  391. }
  392. unset($allData);
  393. return true;
  394. }
  395. /**
  396. * 蓝星奖金(即新的管理奖),更新会员聘级
  397. * @param int $offset
  398. * @return bool
  399. * @throws \yii\db\Exception
  400. */
  401. public function updateEmpLevel(int $offset = 0) {
  402. if ($this->_isCalcMonth) {
  403. $empLv = EmployLevel::getIdConvertLevelSortCache();
  404. $allData = CalcBonusBS::findUseDbCalc()
  405. ->yearMonth($this->_calcYearMonth)
  406. ->where(
  407. 'CALC_MONTH=:CALC_MONTH AND PERIOD_NUM=:PERIOD_NUM',
  408. [
  409. ':CALC_MONTH' => $this->_calcYearMonth,
  410. ':PERIOD_NUM' =>$this->_periodNum
  411. ]
  412. )
  413. ->orderBy('CREATED_AT DESC')
  414. ->groupBy('USER_ID')
  415. ->offset($offset)
  416. ->limit($this->_limit)
  417. ->all();
  418. //@todo 用户级别不变则不更新
  419. $defaultEmpLv = EmployLevel::getDefaultLevelId();
  420. if ($allData) {
  421. $transaction = Yii::$app->db->beginTransaction();
  422. try {
  423. foreach ($allData as $data) {
  424. //@todo 用户级别不变则不更新
  425. if( $data['LEVEL_ID'] === $defaultEmpLv ) continue;
  426. $nowBsEmpLv = $data['LEVEL_ID']; // 当前蓝星奖计算(即管理奖) 的等级
  427. $user = CalcCache::getUserInfo($data['USER_ID'], $this->_periodNum);
  428. $userEmpLv = $user['EMP_LV']; // 用户的历史最高聘级
  429. $userEmpLvSort = $empLv[$userEmpLv]; // 历史最高聘级的 级别值
  430. $nowBsEmpLvSort = $empLv[$nowBsEmpLv]; // 当前蓝星计算的聘级 级别值
  431. if ($nowBsEmpLvSort > $userEmpLvSort) {
  432. // 如果当前期的级别值大于历史最高级别,则更新用户表里的最高聘级
  433. User::updateAll(['EMP_LV' => $data['LEVEL_ID']], 'ID=:USER_ID', [':USER_ID' => $data['USER_ID']]);
  434. User::deleteBaseInfoFromRedis($data['USER_ID']);
  435. unset($data);
  436. } else {
  437. continue;
  438. }
  439. }
  440. $transaction->commit();
  441. } catch (Exception $e) {
  442. $transaction->rollBack();
  443. $this->addError('updateEmpLevel', '更新聘级失败,原因:' . $e->getMessage());
  444. return false;
  445. }
  446. unset($transaction, $allData, $defaultEmpLv);
  447. return $this->updateEmpLevel($offset + $this->_limit);
  448. }
  449. unset($allData);
  450. }
  451. return true;
  452. }
  453. // /**
  454. // * 更新会员聘级
  455. // * @param int $offset
  456. // * @return bool
  457. // * @throws \yii\db\Exception
  458. // */
  459. // public function updateEmpLevel(int $offset = 0) {
  460. // if ($this->_isCalcMonth) {
  461. // $allData = PerfMonth::findUseDbCalc()->yearMonth($this->_calcYearMonth)->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])->offset($offset)->limit($this->_limit)->all();
  462. // //@todo 用户级别不变则不更新
  463. // $defaultEmpLv = EmployLevel::getDefaultLevelId();
  464. // if ($allData) {
  465. // $transaction = Yii::$app->db->beginTransaction();
  466. // try {
  467. // foreach ($allData as $data) {
  468. // //@todo 用户级别不变则不更新
  469. // if( $data['LAST_EMP_LV'] === $defaultEmpLv ) continue;
  470. // User::updateAll(['EMP_LV' => $data['LAST_EMP_LV']], 'ID=:USER_ID', [':USER_ID' => $data['USER_ID']]);
  471. // User::deleteBaseInfoFromRedis($data['USER_ID']);
  472. // unset($data);
  473. // }
  474. // $transaction->commit();
  475. // } catch (Exception $e) {
  476. // $transaction->rollBack();
  477. // $this->addError('updateEmpLevel', '更新聘级失败,原因:' . $e->getMessage());
  478. // return false;
  479. // }
  480. // unset($transaction, $allData, $defaultEmpLv);
  481. // return $this->updateEmpLevel($offset + $this->_limit);
  482. // }
  483. // unset($allData);
  484. // }
  485. // return true;
  486. // }
  487. // 更活跃会员,将is_send改成1
  488. public function updateActiveUser() {
  489. try {
  490. $ret = PerfActiveUser::updateAll(
  491. ['IS_SENT' => 1],
  492. 'PERIOD_NUM=:PERIOD_NUM AND IS_SENT=:IS_SENT',
  493. ['IS_SENT'=>0, 'PERIOD_NUM'=>$this->_periodNum]
  494. );
  495. return $ret;
  496. } catch(Exception $e) {
  497. $this->addError('updateActiveUser', '更新活跃会员业绩期处理状态失败,原因:' . $e->getMessage());
  498. return false;
  499. }
  500. }
  501. /**
  502. * 更新会员的累计业绩
  503. * @param int $offset
  504. * @return bool
  505. * @throws \yii\db\Exception
  506. */
  507. public function updateUserPerf(int $offset = 0) {
  508. $allData = PerfPeriod::findUseDbCalc()->yearMonth($this->_calcYearMonth)->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])->orderBy('ID ASC')->offset($offset)->limit($this->_limit)->all();
  509. if ($allData) {
  510. $transaction = Yii::$app->db->beginTransaction();
  511. try {
  512. foreach ($allData as $data) {
  513. $isUpdate = false;
  514. if (UserPerf::findUseDbCalc()->where('USER_ID=:USER_ID', [':USER_ID' => $data['USER_ID']])->exists()) {
  515. // 判断本期是否已经更新过业绩
  516. 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()) {
  517. $isUpdate = true;
  518. // 更新业绩
  519. UserPerf::updateAll([
  520. 'PV_PCS_ZC' => new Expression('PV_PCS_ZC+' . $data['PV_PCS_ZC']),
  521. 'PV_PCS_YH' => new Expression('PV_PCS_YH+' . $data['PV_PCS_YH']),
  522. 'PV_PCS_ZG' => new Expression('PV_PCS_ZG+' . $data['PV_PCS_ZG']),
  523. 'PV_PCS_LS' => new Expression('PV_PCS_LS+' . $data['PV_PCS_LS']),
  524. 'PV_PCS_FX' => new Expression('PV_PCS_FX+' . $data['PV_PCS_FX']),
  525. 'PV_PSS' => new Expression('PV_PSS+' . $data['PV_PSS']),
  526. 'PV_PSS_TOTAL' => new Expression('PV_PSS_TOTAL+' . $data['PV_PSS']),
  527. 'PV_1L' => new Expression('PV_1L+' . $data['PV_1L']),
  528. 'PV_2L' => new Expression('PV_2L+' . $data['PV_2L']),
  529. 'PV_3L' => new Expression('PV_3L+' . $data['PV_3L']),
  530. 'PV_4L' => new Expression('PV_4L+' . $data['PV_4L']),
  531. 'PV_5L' => new Expression('PV_5L+' . $data['PV_5L']),
  532. 'SURPLUS_1L' => $data['SURPLUS_1L'],
  533. 'SURPLUS_2L' => $data['SURPLUS_2L'],
  534. 'SURPLUS_3L' => $data['SURPLUS_3L'],
  535. 'SURPLUS_4L' => $data['SURPLUS_4L'],
  536. 'SURPLUS_5L' => $data['SURPLUS_5L'],
  537. 'SURPLUS_1L_ZC' => $data['SURPLUS_1L_ZC'],
  538. 'SURPLUS_2L_ZC' => $data['SURPLUS_2L_ZC'],
  539. 'SURPLUS_3L_ZC' => $data['SURPLUS_3L_ZC'],
  540. 'SURPLUS_4L_ZC' => $data['SURPLUS_4L_ZC'],
  541. 'SURPLUS_5L_ZC' => $data['SURPLUS_5L_ZC'],
  542. 'SURPLUS_1L_FX' => $data['SURPLUS_1L_FX'],
  543. 'SURPLUS_2L_FX' => $data['SURPLUS_2L_FX'],
  544. 'SURPLUS_3L_FX' => $data['SURPLUS_3L_FX'],
  545. 'SURPLUS_4L_FX' => $data['SURPLUS_4L_FX'],
  546. 'SURPLUS_5L_FX' => $data['SURPLUS_5L_FX'],
  547. ], 'USER_ID=:USER_ID', [':USER_ID' => $data['USER_ID']]);
  548. }
  549. } else {
  550. $isUpdate = true;
  551. UserPerf::insertOne([
  552. 'USER_ID' => $data['USER_ID'],
  553. 'PV_PCS_ZC' => $data['PV_PCS_ZC'],
  554. 'PV_PCS_YH' => $data['PV_PCS_YH'],
  555. 'PV_PCS_ZG' => $data['PV_PCS_ZG'],
  556. 'PV_PCS_LS' => $data['PV_PCS_LS'],
  557. 'PV_PCS_FX' => $data['PV_PCS_FX'],
  558. 'PV_PSS' => $data['PV_PSS'],
  559. 'PV_PSS_TOTAL' => $data['PV_PSS'],
  560. 'PV_1L' => $data['PV_1L'],
  561. 'PV_2L' => $data['PV_2L'],
  562. 'PV_3L' => $data['PV_3L'],
  563. 'PV_4L' => $data['PV_4L'],
  564. 'PV_5L' => $data['PV_5L'],
  565. 'SURPLUS_1L' => $data['SURPLUS_1L'],
  566. 'SURPLUS_2L' => $data['SURPLUS_2L'],
  567. 'SURPLUS_3L' => $data['SURPLUS_3L'],
  568. 'SURPLUS_4L' => $data['SURPLUS_4L'],
  569. 'SURPLUS_5L' => $data['SURPLUS_5L'],
  570. 'SURPLUS_1L_ZC' => $data['SURPLUS_1L_ZC'],
  571. 'SURPLUS_2L_ZC' => $data['SURPLUS_2L_ZC'],
  572. 'SURPLUS_3L_ZC' => $data['SURPLUS_3L_ZC'],
  573. 'SURPLUS_4L_ZC' => $data['SURPLUS_4L_ZC'],
  574. 'SURPLUS_5L_ZC' => $data['SURPLUS_5L_ZC'],
  575. 'SURPLUS_1L_FX' => $data['SURPLUS_1L_FX'],
  576. 'SURPLUS_2L_FX' => $data['SURPLUS_2L_FX'],
  577. 'SURPLUS_3L_FX' => $data['SURPLUS_3L_FX'],
  578. 'SURPLUS_4L_FX' => $data['SURPLUS_4L_FX'],
  579. 'SURPLUS_5L_FX' => $data['SURPLUS_5L_FX'],
  580. 'CREATED_AT' => Date::nowTime(),
  581. ]);
  582. }
  583. if ($isUpdate) {
  584. // 变为已更新
  585. 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()]);
  586. }
  587. unset($data, $isUpdate);
  588. }
  589. $transaction->commit();
  590. } catch (Exception $e) {
  591. $transaction->rollBack();
  592. $this->addError('updateUserPerf', '更新会员业绩失败,原因:' . $e->getMessage());
  593. return false;
  594. }
  595. unset($allData, $transaction);
  596. return $this->updateUserPerf($offset + $this->_limit);
  597. }
  598. unset($allData);
  599. return true;
  600. }
  601. /**
  602. * 更新会员的月剩余业绩
  603. * @param int $offset
  604. * @return bool
  605. * @throws \yii\db\Exception
  606. */
  607. public function updateUserPerfMonth(int $offset = 0) {
  608. // 月结,如果不是月结点,则直接退出
  609. if (!$this->_isCalcMonth) {
  610. return true;
  611. }
  612. $allData = PerfMonth::findUseDbCalc()->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])->orderBy('ID ASC')->offset($offset)->limit($this->_limit)->all();
  613. if ($allData) {
  614. $transaction = Yii::$app->db->beginTransaction();
  615. try {
  616. foreach ($allData as $data) {
  617. $isUpdate = false;
  618. if (UserPerf::findUseDbCalc()->where('USER_ID=:USER_ID', [':USER_ID' => $data['USER_ID']])->exists()) {
  619. // 判断本期是否已经更新过业绩
  620. if (!UserPerfMonthUpdate::findUseDbCalc()->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [':USER_ID' => $data['USER_ID'], ':CALC_MONTH' => $this->_calcYearMonth])->exists()) {
  621. $isUpdate = true;
  622. // 更新业绩
  623. UserPerf::updateAll([
  624. 'VIP_SURPLUS_1L_ZC' => $data['VIP_SURPLUS_1L_ZC'],
  625. 'VIP_SURPLUS_2L_ZC' => $data['VIP_SURPLUS_2L_ZC'],
  626. 'VIP_SURPLUS_3L_ZC' => $data['VIP_SURPLUS_3L_ZC'],
  627. 'VIP_SURPLUS_4L_ZC' => $data['VIP_SURPLUS_4L_ZC'],
  628. 'VIP_SURPLUS_5L_ZC' => $data['VIP_SURPLUS_5L_ZC']
  629. ], 'USER_ID=:USER_ID', [':USER_ID' => $data['USER_ID']]);
  630. }
  631. } else {
  632. $isUpdate = true;
  633. UserPerf::insertOne([
  634. 'USER_ID' => $data['USER_ID'],
  635. 'VIP_SURPLUS_1L_ZC' => $data['VIP_SURPLUS_1L_ZC'],
  636. 'VIP_SURPLUS_2L_ZC' => $data['VIP_SURPLUS_2L_ZC'],
  637. 'VIP_SURPLUS_3L_ZC' => $data['VIP_SURPLUS_3L_ZC'],
  638. 'VIP_SURPLUS_4L_ZC' => $data['VIP_SURPLUS_4L_ZC'],
  639. 'VIP_SURPLUS_5L_ZC' => $data['VIP_SURPLUS_5L_ZC'],
  640. 'CREATED_AT' => Date::nowTime(),
  641. ]);
  642. }
  643. if ($isUpdate) {
  644. // 变为已更新
  645. UserPerfMonthUpdate::insertOne(['USER_ID' => $data['USER_ID'], 'CALC_MONTH' => $this->_calcYearMonth, 'CREATED_AT' => Date::nowTime()]);
  646. }
  647. unset($data, $isUpdate);
  648. }
  649. $transaction->commit();
  650. } catch (Exception $e) {
  651. $transaction->rollBack();
  652. $this->addError('updateUserPerf', '更新会员月业绩失败,原因:' . $e->getMessage());
  653. return false;
  654. }
  655. unset($allData, $transaction);
  656. return $this->updateUserPerfMonth($offset + $this->_limit);
  657. }
  658. unset($allData);
  659. return true;
  660. }
  661. /**
  662. * 检测复消积分是否过期
  663. * @return bool
  664. * @throws \yii\db\Exception
  665. */
  666. public function checkReconsumePointsExpired(int $offset = 0) {
  667. $periodNum = $this->_periodNum;
  668. //一期为7天,那么365天为52.14即53期
  669. $expiredPeriodNum = $periodNum - 53;
  670. if( $expiredPeriodNum <= 0 ) return true;
  671. //查询需要过期的期数
  672. $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();
  673. if( $allData ) {
  674. $transaction = Yii::$app->db->beginTransaction();
  675. try{
  676. //扣除钱包的复消积分
  677. foreach ($allData as $everyData) {
  678. //过期
  679. UserPeriodPoints::updateAll([
  680. 'REMAINDER_POINTS' => 0,
  681. 'EXPIRED' => 1,
  682. 'EXPIRED_PERIOD' => $periodNum,
  683. 'EXPIRED_AT' => Date::nowTime(),
  684. ], 'ID=:ID', ['ID'=>$everyData['ID']]);
  685. if( !isset($everyData['REMAINDER_POINTS']) || !$everyData['REMAINDER_POINTS'] ) continue;
  686. UserBonus::updateAllCounters([
  687. 'RECONSUME_POINTS' => (-1) * $everyData['REMAINDER_POINTS'],
  688. ], 'USER_ID=:USER_ID', $everyData['USER_ID']);
  689. unset($everyData);
  690. }
  691. unset($periodNum, $expiredPeriodNum, $allData);
  692. $transaction->commit();
  693. }catch (\Exception $e){
  694. $transaction->rollBack();
  695. $this->addError('checkReconsumePointsExpired', '检测过期在复消积分失败,原因:' . $e->getMessage());
  696. return false;
  697. }
  698. return $this->checkReconsumePointsExpired($offset + $this->_limit);
  699. }
  700. unset($allData);
  701. return true;
  702. }
  703. /**
  704. * 更新会员上次报单级别
  705. * @return bool
  706. */
  707. public function updateUserDevLv() {
  708. $transaction = Yii::$app->dbShop->beginTransaction();
  709. try {
  710. \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();
  711. $transaction->commit();
  712. } catch (Exception $e) {
  713. $transaction->rollBack();
  714. $this->addError('updateUserDevLv', '更新会员上次报单级别失败,原因:' . $e->getMessage());
  715. return false;
  716. }
  717. return true;
  718. }
  719. /**
  720. * 给商城会员增加货款
  721. * @param $userId
  722. * @param $amount
  723. * @param $remark
  724. * @throws Exception
  725. */
  726. private function _shopAddPaymentForGoods($userId, $amount, $remark) {
  727. $db = \Yii::$app->dbShop;
  728. $transaction = $db->beginTransaction();
  729. try {
  730. ActiveRecord::batchUpdate(['PAYMENT_FOR_GOODS' => new Expression('PAYMENT_FOR_GOODS+' . abs($amount))], 'USER_ID=:USER_ID', [':USER_ID' => $userId], '{{%USER_WALLET}}', 'dbShop');
  731. // 增加流水
  732. $flowInsertData[] = [
  733. 'USER_ID' => $userId,
  734. 'USER_NAME' => Cache::getUserBaseInfo($userId)['USER_NAME'],
  735. 'DEC_LV' => Cache::getUserBaseInfo($userId)['DEC_LV'],
  736. 'ORDER_SN' => null,
  737. 'AMOUNT' => $amount,
  738. 'CREATED_AT' => Date::nowTime(),
  739. 'PERIOD_AT' => $this->_periodNum,
  740. 'IS_INCR' => 1,
  741. 'REMARK' => $remark,
  742. 'PARTITION_DATE' => Date::ociToDate(),
  743. 'WALLET_TYPE' => 'payment_for_goods',
  744. 'FROM_TYPE' => 'incr',
  745. ];
  746. ActiveRecord::batchInsert($flowInsertData, '{{%FLOW_WALLET}}', 'dbShop');
  747. $transaction->commit();
  748. } catch (Exception $e) {
  749. $transaction->rollBack();
  750. throw new Exception($e->getMessage());
  751. }
  752. }
  753. /**
  754. * 更新百分比并发送
  755. * @param $percent
  756. */
  757. private function _updatePercent($percent) {
  758. // 把数据写入数据库中
  759. Period::updateAll(['SENT_PERCENT' => $percent], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  760. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin($percent, ['MODEL' => 'PERIOD', 'ID' => $this->_periodId, 'FIELD' => 'SENT_PERCENT']);
  761. }
  762. /**
  763. * 更新最高聘级
  764. * @param $user_id
  765. * @return bool
  766. * @throws Exception
  767. */
  768. private function _updateHighestEmpLv($user_id) {
  769. $empLv = Info::getEmpLv($user_id);
  770. $highEmpLv = Info::getHighEmpLv($user_id);
  771. $empLvSort = EmployLevel::getSortById($empLv);
  772. $empLvHighSort = EmployLevel::getSortById($highEmpLv);
  773. if ($empLvHighYear = YearHighestEmpLv::findOneAsArray('USER_ID=:USER_ID AND YEAR=:YEAR', [':USER_ID' => $user_id, ':YEAR' => $this->_calcYear], 'HIGHEST_EMP_LV_SORT')) {
  774. if ($empLvSort > $empLvHighYear['HIGHEST_EMP_LV_SORT']) {
  775. 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]);
  776. }
  777. } else {
  778. $yearHighest = new YearHighestEmpLv();
  779. $yearHighest->USER_ID = $user_id;
  780. $yearHighest->YEAR = $this->_calcYear;
  781. $yearHighest->HIGHEST_EMP_LV = $empLv;
  782. $yearHighest->HIGHEST_EMP_LV_SORT = $empLvSort;
  783. $yearHighest->HIGHEST_EMP_LV_PERIOD = $this->_periodNum;
  784. $yearHighest->CREATED_AT = Date::nowTime();
  785. if (!$yearHighest->save()) {
  786. throw new \yii\db\Exception(Form::formatErrorsForApi($yearHighest->getErrors()));
  787. }
  788. }
  789. if ($empLvSort > $empLvHighSort) {
  790. UserInfo::updateAll(['HIGHEST_EMP_LV' => $empLv, 'HIGHEST_EMP_LV_PERIOD' => $this->_periodNum], 'USER_ID=:USER_ID', [':USER_ID' => $user_id]);
  791. //发送历史最高聘级短信
  792. if($this->_sysConfig['smsEmpOpen']['VALUE']){
  793. if(in_array($empLvSort,explode(",",$this->_sysConfig['smsEmp']['VALUE']))){
  794. $realName = Info::getUserRealNameByUserId($user_id);
  795. $empLvDate = Date::convert();
  796. $empLvName = EmployLevel::getNameById($empLv);
  797. $content = str_replace(['{%REAL_NAME%}', '{%EMP_LV_DATE%}', '{%EMP_LV%}'], [$realName, $empLvDate, $empLvName], $this->_sysConfig['smsContent']['VALUE']);
  798. $params = [
  799. 'mobiles' => Info::getUserMobileByUserId($user_id),
  800. 'content' => $content,
  801. ];
  802. SmsApi::instance()->clearError();
  803. SmsApi::instance()->goSend($params);
  804. }
  805. }
  806. return true;
  807. }
  808. return false;
  809. }
  810. /**
  811. * 点位合作奖金
  812. * @param $userId
  813. * @param $amount
  814. * @param null $type
  815. * @return bool
  816. * @throws Exception
  817. * @throws \yii\db\Exception
  818. */
  819. private function _teamworkBonus($userId, $amount, $key) {
  820. 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;
  821. $fromUserInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  822. foreach ($teamwork as $value) {
  823. $bonus = Tool::formatPrice($amount * $value['DIVIDE_PERCENT'] * 0.01);
  824. if ($bonus <= 0) continue;
  825. $toUserInfo = CalcCache::getUserInfo($value['USER_ID'], $this->_periodNum);
  826. 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'] . '%']);
  827. 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'] . '%']);
  828. }
  829. }
  830. /**
  831. * 发送短信
  832. * @param int $offset
  833. * @return bool
  834. * @throws \yii\db\Exception
  835. */
  836. public function sendSmsLoop(int $offset = 0) {
  837. $allData = UserInfo::findUseDbCalc()->select('USER_ID,ALLOW_RECONSUME_SMS_TO')->where('ALLOW_RECONSUME_SMS=1')->offset($offset)->limit($this->_limit)->all();
  838. if ($allData) {
  839. $smsWallet = explode(",", $this->_sysConfig['smsWallet']['VALUE']);
  840. $smsFee = $this->_sysConfig['smsFee']['VALUE'];
  841. //获取剩余月份
  842. $period = Period::instance();
  843. $year = $period->getYear($this->_periodNum);
  844. $monthLeft = Period::getMonthLeft($this->_periodNum);
  845. $smsFee = Tool::formatPrice($smsFee * $monthLeft);
  846. $to = Date::yearEnd();
  847. $transaction = Yii::$app->db->beginTransaction();
  848. try {
  849. foreach ($allData as $data) {
  850. $userId = $data['USER_ID'];
  851. //过期续费
  852. if ($data['ALLOW_RECONSUME_SMS_TO'] < Date::nowTime()) {
  853. foreach ($smsWallet as $item) {
  854. //1奖金钱包2现金钱包
  855. if ($item == 1) {
  856. //看余额是否充足
  857. if (Balance::getAvailableBalance($userId) < $smsFee){
  858. UserInfo::updateAll(['ALLOW_RECONSUME_SMS' => 0], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  859. continue;
  860. };
  861. Balance::changeUserBonus($userId, 'bonus', -abs($smsFee), ['DEAL_TYPE_ID' => DealType::SMS, 'REMARK' => $year . '年复销提醒短信服务费'], false);
  862. UserInfo::updateAll(['ALLOW_RECONSUME_SMS_TO' => $to], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  863. $data['ALLOW_RECONSUME_SMS_TO'] = $to;
  864. break;
  865. } elseif ($item == 2) {
  866. throw new Exception('不存在此方式');
  867. break;
  868. }
  869. }
  870. }
  871. if ($data['ALLOW_RECONSUME_SMS_TO'] > Date::nowTime() && $mobile = Info::getUserMobileByUserId($userId)) {
  872. $realName = Info::getUserRealNameByUserId($userId);
  873. $reconsumPool = Reconsume::getUserReconsumePool($userId);
  874. $lastRechargeDate = $reconsumPool['toRechargeDate'];
  875. $isPass = $reconsumPool['isPass'] == 1 ? '合格' : '不合格';
  876. $month = $period->getNowMonth();
  877. $content = str_replace(['{%REAL_NAME%}', '{%LAST_RECHARGE_DATE%}', '{%MONTH%}', '{%IS_PASS%}'], [$realName, $lastRechargeDate, $month, $isPass], $this->_sysConfig['smsContent']['VALUE']);
  878. //todo 发短信函数 待测试
  879. $params = [
  880. 'mobiles' => $mobile,
  881. 'content' => $content,
  882. ];
  883. SmsApi::instance()->clearError();
  884. SmsApi::instance()->goSend($params);
  885. }
  886. unset($userId,$mobile,$realName,$reconsumPool,$lastRechargeDate,$isPass,$month,$content);
  887. }
  888. $transaction->commit();
  889. } catch (Exception $e) {
  890. $transaction->rollBack();
  891. return false;
  892. }
  893. unset($data);
  894. return $this->sendSmsLoop($offset + $this->_limit);
  895. }
  896. unset($allData);
  897. return true;
  898. }
  899. }