BonusSend.php 48 KB

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