Balance.php 34 KB

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