BonusSend.php 43 KB

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