Balance.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/10/30
  6. * Time: 下午3:19
  7. */
  8. namespace common\helpers\user;
  9. use common\components\ActiveRecord;
  10. use common\helpers\Cache;
  11. use common\helpers\Date;
  12. use common\helpers\Form;
  13. use common\libs\lock\RedisLock;
  14. use common\models\FlowBonus;
  15. use common\models\FlowCF;
  16. use common\models\FlowLX;
  17. use common\models\FlowReconsumePoints;
  18. use common\models\FlowZonePoints;
  19. use common\models\InvoiceFlow;
  20. use common\models\Period;
  21. use common\models\DeclarationLevel;
  22. use common\models\DecRole;
  23. use common\models\FlowExchangePoints;
  24. use common\models\UserPeriodPoints;
  25. use common\models\UserWallet;
  26. use common\models\UserBonus;
  27. use common\models\UserInfo;
  28. use common\models\UserPeriodExchangePoints;
  29. use yii\base\Exception;
  30. use yii\db\Expression;
  31. class Balance {
  32. const INCR_REDUCE = 0; // 减少
  33. const INCR_ADD = 1; // 增加
  34. const INCR_FREEZE = 2; // 冻结
  35. const INCR_UNFREEZE = 3; // 解冻
  36. const BONUS_BALANCE_LOCK_KEY = 'Bonus';
  37. const RECONSUME_POINTS_BALANCE_LOCK_KEY = 'reconsumePoints';
  38. const EXCHANGE_POINTS_BALANCE_LOCK_KEY = 'exchangePoints';
  39. const CF_BALANCE_LOCK_KEY = 'CF';
  40. const LX_BALANCE_LOCK_KEY = 'LX';
  41. const INVOICE_BALANCE_LOCK_KEY = 'Invoice';
  42. const TRAVEL_POINTS_BALANCE_LOCK_KEY = 'travelPoints';
  43. const CAR_POINTS_BALANCE_LOCK_KEY = 'carPoints';
  44. const HOUSE_POINTS_BALANCE_LOCK_KEY = 'housePoints';
  45. const BALANCE_TYPE = [
  46. 'exchange' => [
  47. 'id' => 'exchange',
  48. 'title' => '兑换点数',
  49. 'attr' => 'EXCHANGE_POINTS',
  50. 'pv' => false,
  51. ],
  52. 'travel_points' => [
  53. 'id' => 'travel_points',
  54. 'title' => '旅游积分',
  55. 'attr' => 'TRAVEL_POINTS',
  56. 'pv' => false,
  57. ],
  58. 'car_points' => [
  59. 'id' => 'car_points',
  60. 'title' => '名车积分',
  61. 'attr' => 'CAR_POINTS',
  62. 'pv' => false,
  63. ],
  64. 'house_points' => [
  65. 'id' => 'house_points',
  66. 'title' => '豪宅积分',
  67. 'attr' => 'HOUSE_POINTS',
  68. 'pv' => false,
  69. ],
  70. ];
  71. /**
  72. * 发票流水
  73. * @param $userId
  74. * @param $amount
  75. * @param $params
  76. * @return bool
  77. * @throws Exception
  78. * @throws \yii\db\Exception
  79. */
  80. public static function changeInvoice($userId, $amount, $params) {
  81. if ($amount == 0) return true;
  82. $period = Period::instance();
  83. if (!isset($params['PERIOD_NUM'])) {
  84. $periodNum = $period->getNowPeriodNum();
  85. } else {
  86. $periodNum = $params['PERIOD_NUM'];
  87. }
  88. $calcYearMonth = $period->getYearMonth($periodNum);
  89. // redis加锁(防止并发余额数值不准确出错)
  90. $lockKey = self::INVOICE_BALANCE_LOCK_KEY . $userId;
  91. if (RedisLock::instance()->lock($lockKey)) {
  92. $userInfo = UserInfo::findOne(['USER_ID' => $userId]);
  93. $totals = $userInfo->INVOICE_BALANCE + $amount;
  94. $userInfo->INVOICE_BALANCE = $totals;
  95. if (!$userInfo->save()) {
  96. throw new \Exception(Form::formatErrorsForApi($userInfo->getErrors()));
  97. }
  98. //记录流水
  99. $baseInfo = Info::baseInfoZh($userId);
  100. $flowInsertData = [
  101. 'USER_ID' => $userId,
  102. 'REAL_NAME' => $baseInfo['REAL_NAME'],
  103. 'DEC_LV' => $baseInfo['DEC_LV'],
  104. 'EMP_LV' => $baseInfo['EMP_LV'],
  105. 'MOBILE' => $baseInfo['MOBILE'],
  106. 'REG_TYPE' => $userInfo['REG_TYPE'],
  107. 'REG_NAME' => $userInfo['REG_NAME'],
  108. 'CREDIT_CODE' => $userInfo['CREDIT_CODE'],
  109. 'SALE_NAME' => $params['SALE_NAME'] ?? null,
  110. 'TAXPAYER_NUMBER' => $params['TAXPAYER_NUMBER'] ?? null,
  111. 'INVOICE_SN' => $params['INVOICE_SN'] ?? null,
  112. 'INVOICE_ACCOUNT' => $params['INVOICE_ACCOUNT'] ?? null,
  113. 'TAX_ACCOUNT' => $params['TAX_ACCOUNT'] ?? null,
  114. 'OUTED_AT' => $params['OUTED_AT'] ?? 0,
  115. 'AMOUNT' => abs($amount),
  116. 'TOTAL' => $totals,
  117. 'WITHDRAW_SN' => $params['WITHDRAW_SN'] ?? null,
  118. 'IS_INCR' => $amount > 0 ? self::INCR_ADD : self::INCR_REDUCE,
  119. 'PERIOD_NUM' => $periodNum,
  120. 'CALC_MONTH' => $calcYearMonth,
  121. 'REMARK' => $params['REMARK'] ?? null,
  122. 'CREATE_ADMIN' => $params['CREATE_ADMIN'],
  123. 'CREATE_REMARK' => $params['CREATE_REMARK'] ?? null,
  124. 'CREATE_TIME' => $params['CREATE_TIME'],
  125. 'AUDIT_ADMIN' => $params['AUDIT_ADMIN'],
  126. 'AUDIT_REMARK' => $params['AUDIT_REMARK'] ?? null,
  127. 'AUDIT_TIME' => $params['AUDIT_TIME'],
  128. 'P_MONTH' => Date::ociToDate(),
  129. 'CREATED_AT' => $params['TIME'] ?? Date::nowTime(),
  130. ];
  131. InvoiceFlow::insertOne($flowInsertData);
  132. unset($flowInsertData);
  133. RedisLock::instance()->unlock($lockKey);
  134. } else {
  135. throw new Exception('流水产生错误');
  136. }
  137. return true;
  138. }
  139. /**
  140. * 获取当前可用余额
  141. * @param $userId
  142. * @return int|mixed
  143. */
  144. public static function getAvailableBalance($userId) {
  145. $oneData = UserBonus::find()->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->asArray()->one();
  146. if ($oneData) {
  147. return $oneData['BONUS'] - $oneData['BONUS_FREEZE'];
  148. } else {
  149. return 0;
  150. }
  151. }
  152. /**
  153. * 获取当前车房养老奖余额
  154. * @param $userId
  155. * @return int|mixed
  156. */
  157. public static function getBalanceCF($userId) {
  158. $oneData = UserBonus::find()->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->asArray()->one();
  159. if ($oneData) {
  160. return $oneData['CF'];
  161. } else {
  162. return 0;
  163. }
  164. }
  165. /**
  166. * 获取当前复消积分余额
  167. * @param $userId
  168. * @return int|mixed
  169. */
  170. public static function getBalanceReconsumePoints($userId) {
  171. $oneData = UserBonus::find()->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->asArray()->one();
  172. if ($oneData) {
  173. return $oneData['RECONSUME_POINTS'];
  174. } else {
  175. return 0;
  176. }
  177. }
  178. /**
  179. * 获取当前兑换积分余额
  180. * @param $userId
  181. * @return int|mixed
  182. */
  183. public static function getBalanceExchangePoints($userId) {
  184. $oneData = UserBonus::find()->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->asArray()->one();
  185. if ($oneData) {
  186. return $oneData['EXCHANGE_POINTS'];
  187. } else {
  188. return 0;
  189. }
  190. }
  191. /**
  192. * 获取当前车房养老奖余额
  193. * @param $userId
  194. * @return int|mixed
  195. */
  196. public static function getBalanceLX($userId) {
  197. $oneData = UserBonus::find()->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->asArray()->one();
  198. if ($oneData) {
  199. return $oneData['LX'];
  200. } else {
  201. return 0;
  202. }
  203. }
  204. /**
  205. * 查询会员账户余额.
  206. * @param $userId
  207. * @param $payType
  208. * @return int|mixed
  209. */
  210. public static function getAccountBalance($userId, $payType) {
  211. $oneData = UserBonus::find()->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->asArray()->one();
  212. return !$oneData ? 0 : $oneData[Balance::BALANCE_TYPE[$payType]['attr']] ?? 0;
  213. }
  214. /**
  215. * 改变会员的余额
  216. * @param $userId
  217. * @param $type
  218. * @param $amount
  219. * @param array $params
  220. * @param bool $allowMinus
  221. * @return bool
  222. * @throws Exception
  223. * @throws \yii\db\Exception
  224. */
  225. public static function changeUserBonus($userId, $type, $amount, $params = [], $allowMinus = false) {
  226. if (array_key_exists($type, UserBonus::TYPE)) {
  227. $type = strtoupper($type);
  228. }
  229. if ($amount == 0) return true;
  230. $period = Period::instance();
  231. if (!isset($params['PERIOD_NUM'])) {
  232. $periodNum = $period->getNowPeriodNum();
  233. } else {
  234. $periodNum = $params['PERIOD_NUM'];
  235. }
  236. $calcYearMonth = $period->getYearMonth($periodNum);
  237. // redis加锁(防止并发余额数值不准确出错)
  238. switch ($type) {
  239. case 'BONUS':
  240. $lockKey = self::BONUS_BALANCE_LOCK_KEY . $userId;
  241. break;
  242. case 'RECONSUME_POINTS':
  243. $lockKey = self::RECONSUME_POINTS_BALANCE_LOCK_KEY . $userId;
  244. break;
  245. case 'EXCHANGE_POINTS':
  246. $lockKey = self::EXCHANGE_POINTS_BALANCE_LOCK_KEY . $userId;
  247. break;
  248. case 'CF':
  249. $lockKey = self::CF_BALANCE_LOCK_KEY . $userId;
  250. break;
  251. case 'LX':
  252. $lockKey = self::LX_BALANCE_LOCK_KEY . $userId;
  253. break;
  254. case 'TRAVEL_POINTS':
  255. $lockKey = self::TRAVEL_POINTS_BALANCE_LOCK_KEY . $userId;
  256. break;
  257. case 'CAR_POINTS':
  258. $lockKey = self::CAR_POINTS_BALANCE_LOCK_KEY . $userId;
  259. break;
  260. case 'HOUSE_POINTS':
  261. $lockKey = self::HOUSE_POINTS_BALANCE_LOCK_KEY . $userId;
  262. break;
  263. default:
  264. throw new Exception('流水类型错误');
  265. }
  266. if (RedisLock::instance()->lock($lockKey)) {
  267. // 改变发奖
  268. $paramData = [];
  269. $oneUserBonusModel = UserBonus::findOne(['USER_ID' => $userId]);
  270. if ($oneUserBonusModel) {
  271. $paramData[$type] = new Expression($type.' + '.$amount);
  272. $oneUserBonusModel->$type += $amount;
  273. if ($oneUserBonusModel->$type < 0) {
  274. RedisLock::instance()->unlock($lockKey);
  275. throw new Exception('金额不足');
  276. }
  277. if (isset($params['QY'])) {
  278. $paramData['QY_TOTAL'] = new Expression('QY_TOTAL + '.$params['QY']);
  279. //$oneUserBonusModel->QY_TOTAL += $params['QY'];
  280. }
  281. if (isset($params['FW'])) {
  282. $paramData['FW_TOTAL'] = new Expression('FW_TOTAL + '.$params['FW']);
  283. }
  284. if (isset($params['YC'])) {
  285. $paramData['YC_TOTAL'] = new Expression('YC_TOTAL + '.$params['YC']);
  286. //$oneUserBonusModel->YC_TOTAL += $params['YC'];
  287. }
  288. if (isset($params['VIP'])) {
  289. $paramData['VIP_TOTAL'] = new Expression('VIP_TOTAL + '.$params['VIP']);
  290. }
  291. if (isset($params['BD'])) {
  292. $paramData['BD_TOTAL'] = new Expression('BD_TOTAL + '.$params['BD']);
  293. //$oneUserBonusModel->BD_TOTAL += $params['BD'];
  294. }
  295. if (isset($params['TG'])) {
  296. $paramData['TG_TOTAL'] = new Expression('TG_TOTAL + '.$params['TG']);
  297. //$oneUserBonusModel->TG_TOTAL += $params['TG'];
  298. }
  299. if (isset($params['YJ'])) {
  300. $paramData['YJ_TOTAL'] = new Expression('YJ_TOTAL + '.$params['YJ']);
  301. //$oneUserBonusModel->YJ_TOTAL += $params['YJ'];
  302. }
  303. if (isset($params['GX'])) {
  304. $paramData['GX_TOTAL'] = new Expression('GX_TOTAL + '.$params['GX']);
  305. //$oneUserBonusModel->GX_TOTAL += $params['GX'];
  306. }
  307. if (isset($params['GL'])) {
  308. $paramData['GL_TOTAL'] = new Expression('GL_TOTAL + '.$params['GL']);
  309. //$oneUserBonusModel->GL_TOTAL += $params['GL'];
  310. }
  311. if (isset($params['BS'])) {
  312. $paramData['BS_TOTAL'] = new Expression('BS_TOTAL + '.$params['BS']);
  313. }
  314. if (isset($params['BS_MNT'])) {
  315. $paramData['BS_MNT_TOTAL'] = new Expression('BS_MNT_TOTAL + '.$params['BS_MNT']);
  316. }
  317. if (isset($params['BS_ABBR'])) {
  318. $paramData['BS_ABBR_TOTAL'] = new Expression('BS_ABBR_TOTAL + '.$params['BS_ABBR']);
  319. }
  320. if (isset($params['TRAVEL_POINTS'])) {
  321. $paramData['TRAVEL_POINTS_TOTAL'] = new Expression('TRAVEL_POINTS_TOTAL + ' . $params['TRAVEL_POINTS']);
  322. }
  323. if (isset($params['CAR_POINTS'])) {
  324. $paramData['CAR_POINTS_TOTAL'] = new Expression('CAR_POINTS_TOTAL + '.$params['CAR_POINTS']);
  325. }
  326. if (isset($params['HOUSE_POINTS'])) {
  327. $paramData['HOUSE_POINTS_TOTAL'] = new Expression('HOUSE_POINTS_TOTAL + '.$params['HOUSE_POINTS']);
  328. }
  329. if (isset($params['RECONSUME_POINTS_TOTAL'])) {
  330. $paramData['RECONSUME_POINTS_TOTAL'] = new Expression('RECONSUME_POINTS_TOTAL + '.$params['RECONSUME_POINTS_TOTAL']);
  331. //$oneUserBonusModel->RECONSUME_POINTS_TOTAL += $params['RECONSUME_POINTS_TOTAL'];
  332. }
  333. if (isset($params['EXCHANGE_POINTS_TOTAL'])) {
  334. $paramData['EXCHANGE_POINTS_TOTAL'] = new Expression('EXCHANGE_POINTS_TOTAL + '.$params['EXCHANGE_POINTS_TOTAL']);
  335. //$oneUserBonusModel->RECONSUME_POINTS_TOTAL += $params['RECONSUME_POINTS_TOTAL'];
  336. }
  337. if (isset($params['MANAGE_TAX'])) {
  338. $paramData['MANAGE_TAX'] = new Expression('MANAGE_TAX + '.$params['MANAGE_TAX']);
  339. //$oneUserBonusModel->MANAGE_TAX += $params['MANAGE_TAX'];
  340. }
  341. if (isset($params['ORI_QY'])) {
  342. $paramData['ORI_QY_TOTAL'] = new Expression('ORI_QY_TOTAL + '.$params['ORI_QY']);
  343. //$oneUserBonusModel->ORI_QY_TOTAL += $params['ORI_QY'];
  344. }
  345. // if (isset($params['ORI_FW'])) {
  346. // $paramData['ORI_FW_TOTAL'] = new Expression('ORI_FW_TOTAL + '.$params['ORI_FW']);
  347. // }
  348. if (isset($params['ORI_YC'])) {
  349. $paramData['ORI_YC_TOTAL'] = new Expression('ORI_YC_TOTAL + '.$params['ORI_YC']);
  350. //$oneUserBonusModel->ORI_YC_TOTAL += $params['ORI_YC'];
  351. }
  352. if (isset($params['ORI_VIP'])) {
  353. $paramData['ORI_VIP_TOTAL'] = new Expression('ORI_VIP_TOTAL + '.$params['ORI_VIP']);
  354. }
  355. if (isset($params['ORI_VIP'])) {
  356. $paramData['ORI_VIP_TOTAL'] = new Expression('ORI_VIP_TOTAL + '.$params['ORI_VIP']);
  357. }
  358. if (isset($params['ORI_STANDARD'])) {
  359. $paramData['ORI_STANDARD_TOTAL'] = new Expression('ORI_STANDARD_TOTAL + '.$params['ORI_STANDARD']);
  360. }
  361. if (isset($params['ORI_TG'])) {
  362. $paramData['ORI_TG_TOTAL'] = new Expression('ORI_TG_TOTAL + '.$params['ORI_TG']);
  363. //$oneUserBonusModel->ORI_TG_TOTAL += $params['ORI_TG'];
  364. }
  365. if (isset($params['ORI_YJ'])) {
  366. $paramData['ORI_YJ_TOTAL'] = new Expression('ORI_YJ_TOTAL + '.$params['ORI_YJ']);
  367. //$oneUserBonusModel->ORI_YJ_TOTAL += $params['ORI_YJ'];
  368. }
  369. if (isset($params['ORI_GX'])) {
  370. $paramData['ORI_GX_TOTAL'] = new Expression('ORI_GX_TOTAL + '.$params['ORI_GX']);
  371. //$oneUserBonusModel->ORI_GX_TOTAL += $params['ORI_GX'];
  372. }
  373. if (isset($params['ORI_GL'])) {
  374. $paramData['ORI_GL_TOTAL'] = new Expression('ORI_GL_TOTAL + '.$params['ORI_GL']);
  375. //$oneUserBonusModel->ORI_GL_TOTAL += $params['ORI_GL'];
  376. }
  377. if (isset($params['ORI_BS'])) {
  378. $paramData['ORI_BS_TOTAL'] = new Expression('ORI_BS_TOTAL + '.$params['ORI_BS']);
  379. //$oneUserBonusModel->ORI_GL_TOTAL += $params['ORI_GL'];
  380. }
  381. if (isset($params['ORI_BS_MNT'])) {
  382. $paramData['BS_MNT_ORI_TOTAL'] = new Expression('BS_MNT_ORI_TOTAL + '.$params['ORI_BS_MNT']);
  383. //$oneUserBonusModel->ORI_GL_TOTAL += $params['ORI_GL'];
  384. }
  385. if (isset($params['ORI_BS_ABBR'])) {
  386. $paramData['BS_ABBR_ORI_TOTAL'] = new Expression('BS_ABBR_ORI_TOTAL + '.$params['ORI_BS_ABBR']);
  387. //$oneUserBonusModel->ORI_GL_TOTAL += $params['ORI_GL'];
  388. }
  389. if (isset($params['BONUS_TOTAL'])) {
  390. $paramData['BONUS_TOTAL'] = new Expression('BONUS_TOTAL + '.$params['BONUS_TOTAL']);
  391. //$oneUserBonusModel->BONUS_TOTAL += $params['BONUS_TOTAL'];
  392. }
  393. UserBonus::updateAll($paramData, 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  394. } else {
  395. $paramData = [
  396. 'USER_ID'=>$userId,
  397. $type=>$amount,
  398. 'CREATED_AT'=>Date::nowTime()
  399. ];
  400. if (isset($params['QY'])) {
  401. // $paramData['QY_TOTAL'] = new Expression('QY_TOTAL + '.$params['QY']);
  402. $paramData['QY_TOTAL'] = $params['QY'];
  403. }
  404. if (isset($params['FW'])) {
  405. // $paramData['FW_TOTAL'] = new Expression('FW_TOTAL + '.$params['FW']);
  406. $paramData['FW_TOTAL'] = $params['FW'];
  407. }
  408. if (isset($params['YC'])) {
  409. // $paramData['YC_TOTAL'] = new Expression('YC_TOTAL + '.$params['YC']);
  410. $paramData['YC_TOTAL'] = $params['YC'];
  411. }
  412. if (isset($params['VIP'])) {
  413. // $paramData['VIP_TOTAL'] = new Expression('VIP_TOTAL + '.$params['VIP']);
  414. $paramData['VIP_TOTAL'] = $params['VIP'];
  415. }
  416. if (isset($params['BD'])) {
  417. // $paramData['BD_TOTAL'] = new Expression('BD_TOTAL + '.$params['BD']);
  418. $paramData['BD_TOTAL'] = $params['BD'];
  419. //$oneUserBonusModel->BD_TOTAL += $params['BD'];
  420. }
  421. if (isset($params['TG'])) {
  422. // $paramData['TG_TOTAL'] = new Expression('TG_TOTAL + '.$params['TG']);
  423. $paramData['TG_TOTAL'] = $params['TG'];
  424. }
  425. if (isset($params['YJ'])) {
  426. // $paramData['YJ_TOTAL'] = new Expression('YJ_TOTAL + '.$params['YJ']);
  427. $paramData['YJ_TOTAL'] = $params['YJ'];
  428. }
  429. if (isset($params['GX'])) {
  430. // $paramData['GX_TOTAL'] = new Expression('GX_TOTAL + '.$params['GX']);
  431. $paramData['GX_TOTAL'] = $params['GX'];
  432. }
  433. if (isset($params['GL'])) {
  434. // $paramData['GL_TOTAL'] = new Expression('GL_TOTAL + '.$params['GL']);
  435. $paramData['GL_TOTAL'] = $params['GL'];
  436. }
  437. if (isset($params['BS'])) {
  438. $paramData['BS_TOTAL'] = $params['BS'];
  439. //$oneUserBonusModel->GL_TOTAL += $params['GL'];
  440. }
  441. if (isset($params['BS_MNT'])) {
  442. $paramData['BS_MNT_TOTAL'] = $params['BS_MNT'];
  443. }
  444. if (isset($params['BS_ABBR'])) {
  445. $paramData['BS_ABBR_TOTAL'] = $params['BS_ABBR'];
  446. }
  447. if (isset($params['TRAVEL_POINTS'])) {
  448. $paramData['TRAVEL_POINTS_TOTAL'] = $params['TRAVEL_POINTS'];
  449. }
  450. if (isset($params['CAR_POINTS'])) {
  451. $paramData['CAR_POINTS_TOTAL'] = $params['CAR_POINTS'];
  452. }
  453. if (isset($params['HOUSE_POINTS'])) {
  454. $paramData['HOUSE_POINTS_TOTAL'] = $params['HOUSE_POINTS'];
  455. }
  456. if (isset($params['RECONSUME_POINTS_TOTAL'])) {
  457. // $paramData['RECONSUME_POINTS_TOTAL'] = new Expression('RECONSUME_POINTS_TOTAL + '.$params['RECONSUME_POINTS_TOTAL']);
  458. $paramData['RECONSUME_POINTS_TOTAL'] = $params['RECONSUME_POINTS_TOTAL'];
  459. }
  460. if (isset($params['MANAGE_TAX'])) {
  461. // $paramData['MANAGE_TAX'] = new Expression('MANAGE_TAX + '.$params['MANAGE_TAX']);
  462. $paramData['MANAGE_TAX'] = $params['MANAGE_TAX'];
  463. }
  464. if (isset($params['ORI_QY'])) {
  465. // $paramData['ORI_QY_TOTAL'] = new Expression('ORI_QY_TOTAL + '.$params['ORI_QY']);
  466. $paramData['ORI_QY_TOTAL'] = $params['ORI_QY'];
  467. }
  468. // if (isset($params['ORI_FW'])) {
  469. //// $paramData['ORI_FW_TOTAL'] = new Expression('ORI_FW_TOTAL + '.$params['ORI_FW']);
  470. // $paramData['ORI_FW_TOTAL'] = $params['ORI_FW'];
  471. // }
  472. if (isset($params['ORI_YC'])) {
  473. // $paramData['ORI_YC_TOTAL'] = new Expression('ORI_YC_TOTAL + '.$params['ORI_YC']);
  474. $paramData['ORI_YC_TOTAL'] = $params['ORI_YC'];
  475. }
  476. if (isset($params['ORI_VIP'])) {
  477. // $paramData['ORI_VIP_TOTAL'] = new Expression('ORI_VIP_TOTAL + '.$params['ORI_VIP']);
  478. $paramData['ORI_VIP_TOTAL'] = $params['ORI_VIP'];
  479. }
  480. if (isset($params['ORI_STANDARD'])) {
  481. $paramData['ORI_STANDARD_TOTAL'] = $params['ORI_STANDARD'];
  482. }
  483. if (isset($params['ORI_BD'])) {
  484. // $paramData['ORI_BD_TOTAL'] = new Expression('ORI_BD_TOTAL + '.$params['ORI_BD']);
  485. $paramData['ORI_BD_TOTAL'] = $params['ORI_BD'];
  486. }
  487. if (isset($params['ORI_TG'])) {
  488. // $paramData['ORI_TG_TOTAL'] = new Expression('ORI_TG_TOTAL + '.$params['ORI_TG']);
  489. $paramData['ORI_TG_TOTAL'] = $params['ORI_TG'];
  490. }
  491. if (isset($params['ORI_YJ'])) {
  492. // $paramData['ORI_YJ_TOTAL'] = new Expression('ORI_YJ_TOTAL + '.$params['ORI_YJ']);
  493. $paramData['ORI_YJ_TOTAL'] = $params['ORI_YJ'];
  494. }
  495. if (isset($params['ORI_GX'])) {
  496. // $paramData['ORI_GX_TOTAL'] = new Expression('ORI_GX_TOTAL + '.$params['ORI_GX']);
  497. $paramData['ORI_GX_TOTAL'] = $params['ORI_GX'];
  498. }
  499. if (isset($params['ORI_GL'])) {
  500. // $paramData['ORI_GL_TOTAL'] = new Expression('ORI_GL_TOTAL + '.$params['ORI_GL']);
  501. $paramData['ORI_GL_TOTAL'] = $params['ORI_GL'];
  502. }
  503. if (isset($params['ORI_BS'])) {
  504. $paramData['ORI_BS_TOTAL'] = $params['ORI_BS'];
  505. //$oneUserBonusModel->ORI_GL_TOTAL += $params['ORI_GL'];
  506. }
  507. if (isset($params['ORI_BS_MNT'])) {
  508. $paramData['BS_MNT_ORI_TOTAL'] = $params['ORI_BS_MNT'];
  509. }
  510. if (isset($params['ORI_BS_ABBR'])) {
  511. $paramData['BS_ABBR_ORI_TOTAL'] = $params['ORI_BS_ABBR'];
  512. }
  513. if (isset($params['BONUS_TOTAL'])) {
  514. // $paramData['BONUS_TOTAL'] = new Expression('BONUS_TOTAL + '.$params['BONUS_TOTAL']);
  515. $paramData['BONUS_TOTAL'] = $params['BONUS_TOTAL'];
  516. }
  517. UserBonus::insertOne($paramData);
  518. }
  519. unset($oneUserBonusModel,$paramData);
  520. // 获取发放完成的奖金信息
  521. $oneUserBonus = UserBonus::find()->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->asArray()->one();
  522. $userInfo = Info::getLastInfo($userId);
  523. // 记录流水
  524. $flowInsertData = [
  525. 'USER_ID' => $userId,
  526. 'LAST_DEC_LV' => $userInfo['DEC_LV'],
  527. 'LAST_EMP_LV' => $userInfo['EMP_LV'],
  528. 'LAST_STATUS' => $userInfo['STATUS'],
  529. 'CALC_ID' => $params['CALC_ID'] ?? null,
  530. 'AMOUNT' => $amount,
  531. 'TOTAL' => $oneUserBonus[$type],
  532. 'IS_INCR' => $amount > 0 ? FlowBonus::INCR_ADD : FlowBonus::INCR_REDUCE,
  533. 'REMARK' => $params['REMARK'] ?? null,
  534. 'REMARK_IS_SHOW' => $params['REMARK_IS_SHOW'] ?? 1,
  535. 'PERIOD_NUM' => $params['PERIOD_NUM'] ?? $periodNum,
  536. 'CALC_MONTH' => $calcYearMonth,
  537. 'P_MONTH' => Date::ociToDate(),
  538. 'CREATED_AT' => $params['TIME'] ?? Date::nowTime(),
  539. 'ADMIN_NAME' => $params['ADMIN_NAME'] ?? 'system',
  540. 'DEAL_TYPE_ID' => $params['DEAL_TYPE_ID'] ?? '',
  541. 'DEAL_TYPE_IS_PRESET' => $params['DEAL_TYPE_IS_PRESET'] ?? 1,
  542. 'TRANSFER_SN' => $params['TRANSFER_SN'] ?? '',
  543. 'SORT' => $params['SORT'] ?? 0,
  544. ];
  545. unset($userInfo, $oneUserBonus);
  546. if (strtolower($type) == 'reconsume_points' || strtolower($type) == 'cf' || strtolower($type) == 'lx'
  547. || strtolower($type) == 'exchange_points'
  548. ) {
  549. unset($flowInsertData['CALC_ID']);
  550. unset($flowInsertData['TRANSFER_SN']);
  551. unset($flowInsertData['SORT']);
  552. }
  553. if (strtolower($type) == 'bonus') {
  554. FlowBonus::insertOne($flowInsertData);
  555. } elseif (strtolower($type) == 'reconsume_points') {
  556. //记录和扣除期数的积分
  557. if( $amount > 0 ) {
  558. self::addPeriodReconsumePoints($userId, $periodNum, $amount);
  559. }else {
  560. self::deductPeriodReconsumePoints($userId, abs($amount));
  561. }
  562. FlowReconsumePoints::insertOne($flowInsertData);
  563. } elseif (strtolower($type) == 'exchange_points') {
  564. //记录和扣除期数的积分
  565. if( $amount > 0 ) {
  566. self::addPeriodExchangePoints($userId, $periodNum, $amount);
  567. }else {
  568. self::deductPeriodExchangePoints($userId, abs($amount));
  569. }
  570. FlowExchangePoints::insertOne($flowInsertData);
  571. } elseif (strtolower($type) == 'cf') {
  572. FlowCF::insertOne($flowInsertData);
  573. } elseif (strtolower($type) == 'lx') {
  574. FlowLX::insertOne($flowInsertData);
  575. } elseif (in_array(strtolower($type), ['travel_points', 'car_points', 'house_points'])) {
  576. // 这3个奖流水在一个表,需要加二级标识码
  577. $flowInsertData['WALLET_TYPE'] = strtolower($type);
  578. FlowZonePoints::insertOne($flowInsertData);
  579. }
  580. unset($flowInsertData);
  581. RedisLock::instance()->unlock($lockKey);
  582. } else {
  583. throw new Exception('流水产生错误');
  584. }
  585. return true;
  586. }
  587. /**
  588. * 添加对应期数的复消积分
  589. * @param $userId
  590. * @param $periodNum
  591. * @param $amount
  592. * @throws \yii\db\Exception
  593. * @return boolean
  594. */
  595. public static function addPeriodReconsumePoints($userId, $periodNum, $amount) {
  596. if($amount <= 0) return false;
  597. $exists = UserPeriodPoints::find()->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  598. 'USER_ID' => $userId,
  599. 'PERIOD_NUM' => $periodNum,
  600. ])->asArray()->exists();
  601. if( $exists ) {
  602. UserPeriodPoints::updateAllCounters([
  603. 'RECONSUME_POINTS' => $amount,
  604. 'REMAINDER_POINTS' => $amount,
  605. ], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  606. 'USER_ID' => $userId,
  607. 'PERIOD_NUM' => $periodNum,
  608. ]);
  609. }else {
  610. UserPeriodPoints::insertOne([
  611. 'USER_ID' => $userId,
  612. 'PERIOD_NUM' => $periodNum,
  613. 'RECONSUME_POINTS' => $amount,
  614. 'REMAINDER_POINTS' => $amount,
  615. 'EXPIRED' => 0,
  616. 'CREATED_AT' => Date::nowTime()
  617. ]);
  618. }
  619. return true;
  620. }
  621. /**
  622. * 添加对应期数的兑换积分
  623. * @param $userId
  624. * @param $periodNum
  625. * @param $amount
  626. * @throws \yii\db\Exception
  627. * @return boolean
  628. */
  629. public static function addPeriodExchangePoints($userId, $periodNum, $amount) {
  630. if($amount <= 0) return false;
  631. $exists = UserPeriodExchangePoints::find()->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  632. 'USER_ID' => $userId,
  633. 'PERIOD_NUM' => $periodNum,
  634. ])->asArray()->exists();
  635. if( $exists ) {
  636. UserPeriodExchangePoints::updateAllCounters([
  637. 'EXCHANGE_POINTS' => $amount,
  638. 'REMAINDER_POINTS' => $amount,
  639. ], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  640. 'USER_ID' => $userId,
  641. 'PERIOD_NUM' => $periodNum,
  642. ]);
  643. }else {
  644. UserPeriodExchangePoints::insertOne([
  645. 'USER_ID' => $userId,
  646. 'PERIOD_NUM' => $periodNum,
  647. 'EXCHANGE_POINTS' => $amount,
  648. 'REMAINDER_POINTS' => $amount,
  649. 'EXPIRED' => 0,
  650. 'CREATED_AT' => Date::nowTime()
  651. ]);
  652. }
  653. return true;
  654. }
  655. /**
  656. * 减少
  657. * @param $userId
  658. * @param $amount
  659. * @return bool
  660. */
  661. public static function deductPeriodReconsumePoints($userId, $amount) {
  662. if( $amount <= 0 ) return false;
  663. $avalidList = UserPeriodPoints::find()->where('USER_ID=:USER_ID AND EXPIRED=:EXPIRED AND REMAINDER_POINTS>0', [
  664. 'USER_ID' => $userId,
  665. 'EXPIRED'=>0
  666. ])->orderBy('PERIOD_NUM ASC')->asArray()->all();
  667. if( !$avalidList ) return false;
  668. foreach ($avalidList as $everyData) {
  669. if( $amount <= 0 ) break;
  670. $remainderPoints = floatval($everyData['REMAINDER_POINTS']);
  671. if( $amount >= $remainderPoints ) {
  672. UserPeriodPoints::updateAllCounters([
  673. 'REMAINDER_POINTS' => (-1) * $remainderPoints
  674. ], 'ID=:ID', ['ID'=>$everyData['ID']]);
  675. $amount -= $remainderPoints;
  676. }else {
  677. UserPeriodPoints::updateAllCounters([
  678. 'REMAINDER_POINTS' => (-1) * $amount
  679. ], 'ID=:ID', ['ID'=>$everyData['ID']]);
  680. $amount = 0;
  681. }
  682. unset($everyData, $remainderPoints);
  683. }
  684. if( $amount > 0 ) return false;
  685. return true;
  686. }
  687. /**
  688. * 减少
  689. * @param $userId
  690. * @param $amount
  691. * @return bool
  692. */
  693. public static function deductPeriodExchangePoints($userId, $amount) {
  694. if( $amount <= 0 ) return false;
  695. $avalidList = UserPeriodExchangePoints::find()->where('USER_ID=:USER_ID AND EXPIRED=:EXPIRED AND REMAINDER_POINTS>0', [
  696. 'USER_ID' => $userId,
  697. 'EXPIRED'=>0
  698. ])->orderBy('PERIOD_NUM ASC')->asArray()->all();
  699. if( !$avalidList ) return false;
  700. foreach ($avalidList as $everyData) {
  701. if( $amount <= 0 ) break;
  702. $remainderPoints = floatval($everyData['REMAINDER_POINTS']);
  703. if( $amount >= $remainderPoints ) {
  704. UserPeriodExchangePoints::updateAllCounters([
  705. 'REMAINDER_POINTS' => (-1) * $remainderPoints
  706. ], 'ID=:ID', ['ID'=>$everyData['ID']]);
  707. $amount -= $remainderPoints;
  708. }else {
  709. UserPeriodExchangePoints::updateAllCounters([
  710. 'REMAINDER_POINTS' => (-1) * $amount
  711. ], 'ID=:ID', ['ID'=>$everyData['ID']]);
  712. $amount = 0;
  713. }
  714. unset($everyData, $remainderPoints);
  715. }
  716. if( $amount > 0 ) return false;
  717. return true;
  718. }
  719. /**
  720. * 冻结用户余额
  721. * @param $userId
  722. * @param $amount
  723. * @param null $remark
  724. * @param null $time
  725. * @return bool
  726. * @throws Exception
  727. * @throws \yii\db\Exception
  728. */
  729. public static function freezeUserBonus($userId, $amount, $remark = null, $time = null) {
  730. return self::changeFreezeUserBonus($userId, $amount, ['REMARK' => $remark, 'TIME' => $time]);
  731. }
  732. /**
  733. * 解冻用户余额
  734. * @param $userId
  735. * @param $amount
  736. * @param null $remark
  737. * @param null $time
  738. * @return bool
  739. * @throws Exception
  740. * @throws \yii\db\Exception
  741. */
  742. public static function unfreezeUserBonus($userId, $amount, $remark = null, $time = null) {
  743. return self::changeFreezeUserBonus($userId, -$amount, ['REMARK' => $remark, 'TIME' => $time]);
  744. }
  745. /**
  746. * @param $userId
  747. * @param $amount
  748. * @param $params
  749. * [
  750. * 'REMARK' => '备注',
  751. * 'PERIOD_NUM' => 100,
  752. * ]
  753. * @return bool
  754. * @throws Exception
  755. * @throws \yii\db\Exception
  756. */
  757. public static function changeFreezeUserBonus($userId, $amount, $params) {
  758. if ($amount == 0) return true;
  759. $period = Period::instance();
  760. if (!isset($params['PERIOD_NUM'])) {
  761. $periodNum = $period->getNowPeriodNum();
  762. } else {
  763. $periodNum = $params['PERIOD_NUM'];
  764. }
  765. $calcYearMonth = $period->getYearMonth($periodNum);
  766. // 改变冻结
  767. $oneUserBonusModel = UserBonus::findOne(['USER_ID' => $userId]);
  768. if ($oneUserBonusModel) {
  769. $oneUserBonusModel->BONUS_FREEZE = $oneUserBonusModel->BONUS_FREEZE + $amount;
  770. } else {
  771. $oneUserBonusModel = new UserBonus();
  772. $oneUserBonusModel->USER_ID = $userId;
  773. $oneUserBonusModel->BONUS_FREEZE = $amount;
  774. }
  775. if (!$oneUserBonusModel->save()) {
  776. throw new Exception(Form::formatErrorsForApi($oneUserBonusModel->getErrors()));
  777. }
  778. unset($oneUserBonusModel);
  779. // 流水
  780. // 获取发放完成的奖金信息
  781. $oneUserBonus = UserBonus::find()->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->asArray()->one();
  782. // 记录流水
  783. $flowInsertData = [
  784. 'USER_ID' => $userId,
  785. 'AMOUNT' => abs($amount),
  786. 'TOTAL' => $oneUserBonus['BONUS'],
  787. 'IS_INCR' => $amount > 0 ? FlowBonus::INCR_FREEZE : FlowBonus::INCR_UNFREEZE,
  788. 'REMARK' => $params['REMARK'] ?? null,
  789. 'PERIOD_NUM' => $params['PERIOD_NUM'] ?? $periodNum,
  790. 'CALC_MONTH' => $calcYearMonth,
  791. 'P_MONTH' => Date::ociToDate(),
  792. 'CREATED_AT' => $params['TIME'] ?? Date::nowTime(),
  793. ];
  794. FlowBonus::insertOne($flowInsertData);
  795. unset($flowInsertData);
  796. return true;
  797. }
  798. /**
  799. * 清空会员奖金有流水
  800. * @param $userId
  801. * @param array $params
  802. * @throws Exception
  803. * @throws \yii\db\Exception
  804. */
  805. public static function clearAllBonus($userId, $params = []) {
  806. // 先查找会员的全部余额
  807. $userBonus = UserBonus::findOne(['USER_ID' => $userId]);
  808. // 如果没有会员余额数据,新建余额数据
  809. if (!$userBonus) {
  810. UserBonus::insertOne(['USER_ID' => $userId, 'CREATED_AT' => Date::nowTime()]);
  811. } else {
  812. $period = Period::instance();
  813. foreach (\Yii::$app->params['bonusWalletType'] as $type) {
  814. $field = strtoupper($type['name']);
  815. if ($userBonus[$field]<=0) continue;
  816. $userInfo = Info::getLastInfo($userId);
  817. $flowInsertData = [
  818. 'USER_ID' => $userId,
  819. 'LAST_DEC_LV' => $userInfo['DEC_LV'],
  820. 'LAST_EMP_LV' => $userInfo['EMP_LV'],
  821. 'LAST_STATUS' => $userInfo['STATUS'],
  822. 'CALC_ID' => $params['CALC_ID'] ?? null,
  823. 'AMOUNT' => -$userBonus[$field],
  824. 'TOTAL' => 0,
  825. 'IS_INCR' => FlowBonus::INCR_REDUCE,
  826. 'REMARK' => $params['REMARK'] ?? null,
  827. 'REMARK_IS_SHOW' => $params['REMARK_IS_SHOW'] ?? 1,
  828. 'PERIOD_NUM' => $params['PERIOD_NUM'] ?? $period->getNowPeriodNum(),
  829. 'CALC_MONTH' => $period->getNowYearMonth(),
  830. 'P_MONTH' => Date::ociToDate(),
  831. 'CREATED_AT' => $params['TIME'] ?? Date::nowTime(),
  832. 'ADMIN_NAME' => $params['ADMIN_NAME'] ?? 'system',
  833. 'DEAL_TYPE_ID' => $params['DEAL_TYPE_ID'] ?? '',
  834. 'DEAL_TYPE_IS_PRESET' => $params['DEAL_TYPE_IS_PRESET'] ?? 1,
  835. 'TRANSFER_SN' => $params['TRANSFER_SN'] ?? '',
  836. 'SORT' => $params['SORT'] ?? 0,
  837. ];
  838. // 流水
  839. if (strtolower($field) == 'bonus') FlowBonus::insertOne($flowInsertData);
  840. elseif (strtolower($field) == 'cf') {
  841. unset($flowInsertData['CALC_ID']);
  842. unset($flowInsertData['SORT']);
  843. unset($flowInsertData['TRANSFER_SN']);
  844. FlowCF::insertOne($flowInsertData);
  845. } elseif (strtolower($field) == 'lx') {
  846. unset($flowInsertData['CALC_ID']);
  847. unset($flowInsertData['SORT']);
  848. unset($flowInsertData['TRANSFER_SN']);
  849. FlowLX::insertOne($flowInsertData);
  850. }
  851. }
  852. // 清空
  853. $userBonus->BONUS = 0;
  854. $userBonus->CF = 0;
  855. $userBonus->LX = 0;
  856. if (!$userBonus->save()) {
  857. throw new Exception(Form::formatErrorsForApi($userBonus->getErrors()));
  858. }
  859. }
  860. FlowBonus::updateAll(['DELETED' => 1, 'DELETED_AT' => Date::nowTime()], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  861. FlowCF::updateAll(['DELETED' => 1, 'DELETED_AT' => Date::nowTime()], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  862. FlowLX::updateAll(['DELETED' => 1, 'DELETED_AT' => Date::nowTime()], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  863. }
  864. /**
  865. * 是否存在奖金余额
  866. * @param $userId
  867. * @return bool
  868. */
  869. public static function hasBonus($userId) {
  870. $userBonus = UserBonus::findOne(['USER_ID' => $userId]);
  871. if (!$userBonus) {
  872. return false;
  873. }
  874. foreach (\Yii::$app->params['bonusWalletType'] as $type) {
  875. $field = strtoupper($type['name']);
  876. if (isset($userBonus[$field]) && $userBonus[$field] > 0) {
  877. return true;
  878. }
  879. }
  880. return false;
  881. }
  882. /**
  883. * 获取金额用于日志
  884. * @param $userId
  885. * @return array
  886. */
  887. public static function getLogData($userId){
  888. $userWallet = UserWallet::findOne(['USER_ID' => $userId]);
  889. $cash = !empty($userWallet) ? $userWallet['CASH'] : '';
  890. $userName = Info::getUserNameByUserId($userId);
  891. $data = [];
  892. $data[$userId]['label'] = $userName.'余额';
  893. $data[$userId]['value'] = '奖金'.self::getAvailableBalance($userId).',现金'.$cash;
  894. return $data;
  895. }
  896. }