Balance.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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['ST'])) {
  267. $paramData['STORE_TOTAL'] = new Expression('STORE_TOTAL + '.$params['ST']);
  268. }
  269. if (isset($params['RECONSUME_POINTS_TOTAL'])) {
  270. $paramData['RECONSUME_POINTS_TOTAL'] = new Expression('RECONSUME_POINTS_TOTAL + '.$params['RECONSUME_POINTS_TOTAL']);
  271. //$oneUserBonusModel->RECONSUME_POINTS_TOTAL += $params['RECONSUME_POINTS_TOTAL'];
  272. }
  273. if (isset($params['EXCHANGE_POINTS_TOTAL'])) {
  274. $paramData['EXCHANGE_POINTS_TOTAL'] = new Expression('EXCHANGE_POINTS_TOTAL + '.$params['EXCHANGE_POINTS_TOTAL']);
  275. //$oneUserBonusModel->RECONSUME_POINTS_TOTAL += $params['RECONSUME_POINTS_TOTAL'];
  276. }
  277. if (isset($params['MANAGE_TAX'])) {
  278. $paramData['MANAGE_TAX'] = new Expression('MANAGE_TAX + '.$params['MANAGE_TAX']);
  279. //$oneUserBonusModel->MANAGE_TAX += $params['MANAGE_TAX'];
  280. }
  281. if (isset($params['ORI_QY'])) {
  282. $paramData['ORI_QY_TOTAL'] = new Expression('ORI_QY_TOTAL + '.$params['ORI_QY']);
  283. //$oneUserBonusModel->ORI_QY_TOTAL += $params['ORI_QY'];
  284. }
  285. // if (isset($params['ORI_FW'])) {
  286. // $paramData['ORI_FW_TOTAL'] = new Expression('ORI_FW_TOTAL + '.$params['ORI_FW']);
  287. // }
  288. if (isset($params['ORI_YC'])) {
  289. $paramData['ORI_YC_TOTAL'] = new Expression('ORI_YC_TOTAL + '.$params['ORI_YC']);
  290. //$oneUserBonusModel->ORI_YC_TOTAL += $params['ORI_YC'];
  291. }
  292. if (isset($params['ORI_VIP'])) {
  293. $paramData['ORI_VIP_TOTAL'] = new Expression('ORI_VIP_TOTAL + '.$params['ORI_VIP']);
  294. }
  295. if (isset($params['ORI_VIP'])) {
  296. $paramData['ORI_VIP_TOTAL'] = new Expression('ORI_VIP_TOTAL + '.$params['ORI_VIP']);
  297. }
  298. if (isset($params['ORI_STANDARD'])) {
  299. $paramData['ORI_STANDARD_TOTAL'] = new Expression('ORI_STANDARD_TOTAL + '.$params['ORI_STANDARD']);
  300. }
  301. if (isset($params['ORI_TG'])) {
  302. $paramData['ORI_TG_TOTAL'] = new Expression('ORI_TG_TOTAL + '.$params['ORI_TG']);
  303. //$oneUserBonusModel->ORI_TG_TOTAL += $params['ORI_TG'];
  304. }
  305. if (isset($params['ORI_YJ'])) {
  306. $paramData['ORI_YJ_TOTAL'] = new Expression('ORI_YJ_TOTAL + '.$params['ORI_YJ']);
  307. //$oneUserBonusModel->ORI_YJ_TOTAL += $params['ORI_YJ'];
  308. }
  309. if (isset($params['ORI_GX'])) {
  310. $paramData['ORI_GX_TOTAL'] = new Expression('ORI_GX_TOTAL + '.$params['ORI_GX']);
  311. //$oneUserBonusModel->ORI_GX_TOTAL += $params['ORI_GX'];
  312. }
  313. if (isset($params['ORI_GL'])) {
  314. $paramData['ORI_GL_TOTAL'] = new Expression('ORI_GL_TOTAL + '.$params['ORI_GL']);
  315. //$oneUserBonusModel->ORI_GL_TOTAL += $params['ORI_GL'];
  316. }
  317. if (isset($params['ORI_BS'])) {
  318. $paramData['ORI_BS_TOTAL'] = new Expression('ORI_BS_TOTAL + '.$params['ORI_BS']);
  319. //$oneUserBonusModel->ORI_GL_TOTAL += $params['ORI_GL'];
  320. }
  321. if (isset($params['ORI_ST'])) {
  322. $paramData['ORI_STORE_TOTAL'] = new Expression('ORI_STORE_TOTAL + '.$params['ORI_ST']);
  323. }
  324. if (isset($params['BONUS_TOTAL'])) {
  325. $paramData['BONUS_TOTAL'] = new Expression('BONUS_TOTAL + '.$params['BONUS_TOTAL']);
  326. //$oneUserBonusModel->BONUS_TOTAL += $params['BONUS_TOTAL'];
  327. }
  328. UserBonus::updateAll($paramData, 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  329. } else {
  330. $paramData = [
  331. 'USER_ID'=>$userId,
  332. $type=>$amount,
  333. 'CREATED_AT'=>Date::nowTime()
  334. ];
  335. if (isset($params['QY'])) {
  336. // $paramData['QY_TOTAL'] = new Expression('QY_TOTAL + '.$params['QY']);
  337. $paramData['QY_TOTAL'] = $params['QY'];
  338. }
  339. if (isset($params['FW'])) {
  340. // $paramData['FW_TOTAL'] = new Expression('FW_TOTAL + '.$params['FW']);
  341. $paramData['FW_TOTAL'] = $params['FW'];
  342. }
  343. if (isset($params['YC'])) {
  344. // $paramData['YC_TOTAL'] = new Expression('YC_TOTAL + '.$params['YC']);
  345. $paramData['YC_TOTAL'] = $params['YC'];
  346. }
  347. if (isset($params['VIP'])) {
  348. // $paramData['VIP_TOTAL'] = new Expression('VIP_TOTAL + '.$params['VIP']);
  349. $paramData['VIP_TOTAL'] = $params['VIP'];
  350. }
  351. if (isset($params['BD'])) {
  352. // $paramData['BD_TOTAL'] = new Expression('BD_TOTAL + '.$params['BD']);
  353. $paramData['BD_TOTAL'] = $params['BD'];
  354. //$oneUserBonusModel->BD_TOTAL += $params['BD'];
  355. }
  356. if (isset($params['TG'])) {
  357. // $paramData['TG_TOTAL'] = new Expression('TG_TOTAL + '.$params['TG']);
  358. $paramData['TG_TOTAL'] = $params['TG'];
  359. }
  360. if (isset($params['YJ'])) {
  361. // $paramData['YJ_TOTAL'] = new Expression('YJ_TOTAL + '.$params['YJ']);
  362. $paramData['YJ_TOTAL'] = $params['YJ'];
  363. }
  364. if (isset($params['GX'])) {
  365. // $paramData['GX_TOTAL'] = new Expression('GX_TOTAL + '.$params['GX']);
  366. $paramData['GX_TOTAL'] = $params['GX'];
  367. }
  368. if (isset($params['GL'])) {
  369. // $paramData['GL_TOTAL'] = new Expression('GL_TOTAL + '.$params['GL']);
  370. $paramData['GL_TOTAL'] = $params['GL'];
  371. }
  372. if (isset($params['BS'])) {
  373. $paramData['BS_TOTAL'] = $params['BS'];
  374. //$oneUserBonusModel->GL_TOTAL += $params['GL'];
  375. }
  376. if (isset($params['RECONSUME_POINTS_TOTAL'])) {
  377. // $paramData['RECONSUME_POINTS_TOTAL'] = new Expression('RECONSUME_POINTS_TOTAL + '.$params['RECONSUME_POINTS_TOTAL']);
  378. $paramData['RECONSUME_POINTS_TOTAL'] = $params['RECONSUME_POINTS_TOTAL'];
  379. }
  380. if (isset($params['MANAGE_TAX'])) {
  381. // $paramData['MANAGE_TAX'] = new Expression('MANAGE_TAX + '.$params['MANAGE_TAX']);
  382. $paramData['MANAGE_TAX'] = $params['MANAGE_TAX'];
  383. }
  384. if (isset($params['ORI_QY'])) {
  385. // $paramData['ORI_QY_TOTAL'] = new Expression('ORI_QY_TOTAL + '.$params['ORI_QY']);
  386. $paramData['ORI_QY_TOTAL'] = $params['ORI_QY'];
  387. }
  388. // if (isset($params['ORI_FW'])) {
  389. //// $paramData['ORI_FW_TOTAL'] = new Expression('ORI_FW_TOTAL + '.$params['ORI_FW']);
  390. // $paramData['ORI_FW_TOTAL'] = $params['ORI_FW'];
  391. // }
  392. if (isset($params['ORI_YC'])) {
  393. // $paramData['ORI_YC_TOTAL'] = new Expression('ORI_YC_TOTAL + '.$params['ORI_YC']);
  394. $paramData['ORI_YC_TOTAL'] = $params['ORI_YC'];
  395. }
  396. if (isset($params['ORI_VIP'])) {
  397. // $paramData['ORI_VIP_TOTAL'] = new Expression('ORI_VIP_TOTAL + '.$params['ORI_VIP']);
  398. $paramData['ORI_VIP_TOTAL'] = $params['ORI_VIP'];
  399. }
  400. if (isset($params['ORI_STANDARD'])) {
  401. $paramData['ORI_STANDARD_TOTAL'] = $params['ORI_STANDARD'];
  402. }
  403. if (isset($params['ORI_BD'])) {
  404. // $paramData['ORI_BD_TOTAL'] = new Expression('ORI_BD_TOTAL + '.$params['ORI_BD']);
  405. $paramData['ORI_BD_TOTAL'] = $params['ORI_BD'];
  406. }
  407. if (isset($params['ORI_TG'])) {
  408. // $paramData['ORI_TG_TOTAL'] = new Expression('ORI_TG_TOTAL + '.$params['ORI_TG']);
  409. $paramData['ORI_TG_TOTAL'] = $params['ORI_TG'];
  410. }
  411. if (isset($params['ORI_YJ'])) {
  412. // $paramData['ORI_YJ_TOTAL'] = new Expression('ORI_YJ_TOTAL + '.$params['ORI_YJ']);
  413. $paramData['ORI_YJ_TOTAL'] = $params['ORI_YJ'];
  414. }
  415. if (isset($params['ORI_GX'])) {
  416. // $paramData['ORI_GX_TOTAL'] = new Expression('ORI_GX_TOTAL + '.$params['ORI_GX']);
  417. $paramData['ORI_GX_TOTAL'] = $params['ORI_GX'];
  418. }
  419. if (isset($params['ORI_GL'])) {
  420. // $paramData['ORI_GL_TOTAL'] = new Expression('ORI_GL_TOTAL + '.$params['ORI_GL']);
  421. $paramData['ORI_GL_TOTAL'] = $params['ORI_GL'];
  422. }
  423. if (isset($params['ORI_BS'])) {
  424. $paramData['ORI_BS_TOTAL'] = $params['ORI_BS'];
  425. //$oneUserBonusModel->ORI_GL_TOTAL += $params['ORI_GL'];
  426. }
  427. if (isset($params['BONUS_TOTAL'])) {
  428. // $paramData['BONUS_TOTAL'] = new Expression('BONUS_TOTAL + '.$params['BONUS_TOTAL']);
  429. $paramData['BONUS_TOTAL'] = $params['BONUS_TOTAL'];
  430. }
  431. UserBonus::insertOne($paramData);
  432. }
  433. unset($oneUserBonusModel,$paramData);
  434. // 获取发放完成的奖金信息
  435. $oneUserBonus = UserBonus::find()->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->asArray()->one();
  436. $userInfo = Info::getLastInfo($userId);
  437. // 记录流水
  438. $flowInsertData = [
  439. 'USER_ID' => $userId,
  440. 'LAST_DEC_LV' => $userInfo['DEC_LV'],
  441. 'LAST_EMP_LV' => $userInfo['EMP_LV'],
  442. 'LAST_STATUS' => $userInfo['STATUS'],
  443. 'CALC_ID' => $params['CALC_ID'] ?? null,
  444. 'AMOUNT' => $amount,
  445. 'TOTAL' => $oneUserBonus[$type],
  446. 'IS_INCR' => $amount > 0 ? FlowBonus::INCR_ADD : FlowBonus::INCR_REDUCE,
  447. 'REMARK' => $params['REMARK'] ?? null,
  448. 'REMARK_IS_SHOW' => $params['REMARK_IS_SHOW'] ?? 1,
  449. 'PERIOD_NUM' => $params['PERIOD_NUM'] ?? $periodNum,
  450. 'CALC_MONTH' => $calcYearMonth,
  451. 'P_MONTH' => Date::ociToDate(),
  452. 'CREATED_AT' => $params['TIME'] ?? Date::nowTime(),
  453. 'ADMIN_NAME' => $params['ADMIN_NAME'] ?? 'system',
  454. 'DEAL_TYPE_ID' => $params['DEAL_TYPE_ID'] ?? '',
  455. 'DEAL_TYPE_IS_PRESET' => $params['DEAL_TYPE_IS_PRESET'] ?? 1,
  456. 'TRANSFER_SN' => $params['TRANSFER_SN'] ?? '',
  457. 'SORT' => $params['SORT'] ?? 0,
  458. ];
  459. unset($userInfo, $oneUserBonus);
  460. if (strtolower($type) == 'reconsume_points' || strtolower($type) == 'cf' || strtolower($type) == 'lx'
  461. || strtolower($type) == 'exchange_points'
  462. ) {
  463. unset($flowInsertData['CALC_ID']);
  464. unset($flowInsertData['TRANSFER_SN']);
  465. unset($flowInsertData['SORT']);
  466. }
  467. if (strtolower($type) == 'bonus') {
  468. FlowBonus::insertOne($flowInsertData);
  469. } elseif (strtolower($type) == 'reconsume_points') {
  470. //记录和扣除期数的积分
  471. if( $amount > 0 ) {
  472. self::addPeriodReconsumePoints($userId, $periodNum, $amount);
  473. }else {
  474. self::deductPeriodReconsumePoints($userId, abs($amount));
  475. }
  476. FlowReconsumePoints::insertOne($flowInsertData);
  477. } elseif (strtolower($type) == 'exchange_points') {
  478. //记录和扣除期数的积分
  479. if( $amount > 0 ) {
  480. self::addPeriodExchangePoints($userId, $periodNum, $amount);
  481. }else {
  482. self::deductPeriodExchangePoints($userId, abs($amount));
  483. }
  484. FlowExchangePoints::insertOne($flowInsertData);
  485. } elseif (strtolower($type) == 'cf') {
  486. FlowCF::insertOne($flowInsertData);
  487. } elseif (strtolower($type) == 'lx') {
  488. FlowLX::insertOne($flowInsertData);
  489. }
  490. unset($flowInsertData);
  491. RedisLock::instance()->unlock($lockKey);
  492. } else {
  493. throw new Exception('流水产生错误');
  494. }
  495. return true;
  496. }
  497. /**
  498. * 添加对应期数的复消积分
  499. * @param $userId
  500. * @param $periodNum
  501. * @param $amount
  502. * @throws \yii\db\Exception
  503. * @return boolean
  504. */
  505. public static function addPeriodReconsumePoints($userId, $periodNum, $amount) {
  506. if($amount <= 0) return false;
  507. $exists = UserPeriodPoints::find()->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  508. 'USER_ID' => $userId,
  509. 'PERIOD_NUM' => $periodNum,
  510. ])->asArray()->exists();
  511. if( $exists ) {
  512. UserPeriodPoints::updateAllCounters([
  513. 'RECONSUME_POINTS' => $amount,
  514. 'REMAINDER_POINTS' => $amount,
  515. ], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  516. 'USER_ID' => $userId,
  517. 'PERIOD_NUM' => $periodNum,
  518. ]);
  519. }else {
  520. UserPeriodPoints::insertOne([
  521. 'USER_ID' => $userId,
  522. 'PERIOD_NUM' => $periodNum,
  523. 'RECONSUME_POINTS' => $amount,
  524. 'REMAINDER_POINTS' => $amount,
  525. 'EXPIRED' => 0,
  526. 'CREATED_AT' => Date::nowTime()
  527. ]);
  528. }
  529. return true;
  530. }
  531. /**
  532. * 添加对应期数的兑换积分
  533. * @param $userId
  534. * @param $periodNum
  535. * @param $amount
  536. * @throws \yii\db\Exception
  537. * @return boolean
  538. */
  539. public static function addPeriodExchangePoints($userId, $periodNum, $amount) {
  540. if($amount <= 0) return false;
  541. $exists = UserPeriodExchangePoints::find()->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  542. 'USER_ID' => $userId,
  543. 'PERIOD_NUM' => $periodNum,
  544. ])->asArray()->exists();
  545. if( $exists ) {
  546. UserPeriodExchangePoints::updateAllCounters([
  547. 'EXCHANGE_POINTS' => $amount,
  548. 'REMAINDER_POINTS' => $amount,
  549. ], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  550. 'USER_ID' => $userId,
  551. 'PERIOD_NUM' => $periodNum,
  552. ]);
  553. }else {
  554. UserPeriodExchangePoints::insertOne([
  555. 'USER_ID' => $userId,
  556. 'PERIOD_NUM' => $periodNum,
  557. 'EXCHANGE_POINTS' => $amount,
  558. 'REMAINDER_POINTS' => $amount,
  559. 'EXPIRED' => 0,
  560. 'CREATED_AT' => Date::nowTime()
  561. ]);
  562. }
  563. return true;
  564. }
  565. /**
  566. * 减少
  567. * @param $userId
  568. * @param $amount
  569. * @return bool
  570. */
  571. public static function deductPeriodReconsumePoints($userId, $amount) {
  572. if( $amount <= 0 ) return false;
  573. $avalidList = UserPeriodPoints::find()->where('USER_ID=:USER_ID AND EXPIRED=:EXPIRED AND REMAINDER_POINTS>0', [
  574. 'USER_ID' => $userId,
  575. 'EXPIRED'=>0
  576. ])->orderBy('PERIOD_NUM ASC')->asArray()->all();
  577. if( !$avalidList ) return false;
  578. foreach ($avalidList as $everyData) {
  579. if( $amount <= 0 ) break;
  580. $remainderPoints = floatval($everyData['REMAINDER_POINTS']);
  581. if( $amount >= $remainderPoints ) {
  582. UserPeriodPoints::updateAllCounters([
  583. 'REMAINDER_POINTS' => (-1) * $remainderPoints
  584. ], 'ID=:ID', ['ID'=>$everyData['ID']]);
  585. $amount -= $remainderPoints;
  586. }else {
  587. UserPeriodPoints::updateAllCounters([
  588. 'REMAINDER_POINTS' => (-1) * $amount
  589. ], 'ID=:ID', ['ID'=>$everyData['ID']]);
  590. $amount = 0;
  591. }
  592. unset($everyData, $remainderPoints);
  593. }
  594. if( $amount > 0 ) return false;
  595. return true;
  596. }
  597. /**
  598. * 减少
  599. * @param $userId
  600. * @param $amount
  601. * @return bool
  602. */
  603. public static function deductPeriodExchangePoints($userId, $amount) {
  604. if( $amount <= 0 ) return false;
  605. $avalidList = UserPeriodExchangePoints::find()->where('USER_ID=:USER_ID AND EXPIRED=:EXPIRED AND REMAINDER_POINTS>0', [
  606. 'USER_ID' => $userId,
  607. 'EXPIRED'=>0
  608. ])->orderBy('PERIOD_NUM ASC')->asArray()->all();
  609. if( !$avalidList ) return false;
  610. foreach ($avalidList as $everyData) {
  611. if( $amount <= 0 ) break;
  612. $remainderPoints = floatval($everyData['REMAINDER_POINTS']);
  613. if( $amount >= $remainderPoints ) {
  614. UserPeriodExchangePoints::updateAllCounters([
  615. 'REMAINDER_POINTS' => (-1) * $remainderPoints
  616. ], 'ID=:ID', ['ID'=>$everyData['ID']]);
  617. $amount -= $remainderPoints;
  618. }else {
  619. UserPeriodExchangePoints::updateAllCounters([
  620. 'REMAINDER_POINTS' => (-1) * $amount
  621. ], 'ID=:ID', ['ID'=>$everyData['ID']]);
  622. $amount = 0;
  623. }
  624. unset($everyData, $remainderPoints);
  625. }
  626. if( $amount > 0 ) return false;
  627. return true;
  628. }
  629. /**
  630. * 冻结用户余额
  631. * @param $userId
  632. * @param $amount
  633. * @param null $remark
  634. * @param null $time
  635. * @return bool
  636. * @throws Exception
  637. * @throws \yii\db\Exception
  638. */
  639. public static function freezeUserBonus($userId, $amount, $remark = null, $time = null) {
  640. return self::changeFreezeUserBonus($userId, $amount, ['REMARK' => $remark, 'TIME' => $time]);
  641. }
  642. /**
  643. * 解冻用户余额
  644. * @param $userId
  645. * @param $amount
  646. * @param null $remark
  647. * @param null $time
  648. * @return bool
  649. * @throws Exception
  650. * @throws \yii\db\Exception
  651. */
  652. public static function unfreezeUserBonus($userId, $amount, $remark = null, $time = null) {
  653. return self::changeFreezeUserBonus($userId, -$amount, ['REMARK' => $remark, 'TIME' => $time]);
  654. }
  655. /**
  656. * @param $userId
  657. * @param $amount
  658. * @param $params
  659. * [
  660. * 'REMARK' => '备注',
  661. * 'PERIOD_NUM' => 100,
  662. * ]
  663. * @return bool
  664. * @throws Exception
  665. * @throws \yii\db\Exception
  666. */
  667. public static function changeFreezeUserBonus($userId, $amount, $params) {
  668. if ($amount == 0) return true;
  669. $period = Period::instance();
  670. if (!isset($params['PERIOD_NUM'])) {
  671. $periodNum = $period->getNowPeriodNum();
  672. } else {
  673. $periodNum = $params['PERIOD_NUM'];
  674. }
  675. $calcYearMonth = $period->getYearMonth($periodNum);
  676. // 改变冻结
  677. $oneUserBonusModel = UserBonus::findOne(['USER_ID' => $userId]);
  678. if ($oneUserBonusModel) {
  679. $oneUserBonusModel->BONUS_FREEZE = $oneUserBonusModel->BONUS_FREEZE + $amount;
  680. } else {
  681. $oneUserBonusModel = new UserBonus();
  682. $oneUserBonusModel->USER_ID = $userId;
  683. $oneUserBonusModel->BONUS_FREEZE = $amount;
  684. }
  685. if (!$oneUserBonusModel->save()) {
  686. throw new Exception(Form::formatErrorsForApi($oneUserBonusModel->getErrors()));
  687. }
  688. unset($oneUserBonusModel);
  689. // 流水
  690. // 获取发放完成的奖金信息
  691. $oneUserBonus = UserBonus::find()->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->asArray()->one();
  692. // 记录流水
  693. $flowInsertData = [
  694. 'USER_ID' => $userId,
  695. 'AMOUNT' => abs($amount),
  696. 'TOTAL' => $oneUserBonus['BONUS'],
  697. 'IS_INCR' => $amount > 0 ? FlowBonus::INCR_FREEZE : FlowBonus::INCR_UNFREEZE,
  698. 'REMARK' => $params['REMARK'] ?? null,
  699. 'PERIOD_NUM' => $params['PERIOD_NUM'] ?? $periodNum,
  700. 'CALC_MONTH' => $calcYearMonth,
  701. 'P_MONTH' => Date::ociToDate(),
  702. 'CREATED_AT' => $params['TIME'] ?? Date::nowTime(),
  703. ];
  704. FlowBonus::insertOne($flowInsertData);
  705. unset($flowInsertData);
  706. return true;
  707. }
  708. /**
  709. * 清空会员奖金有流水
  710. * @param $userId
  711. * @param array $params
  712. * @throws Exception
  713. * @throws \yii\db\Exception
  714. */
  715. public static function clearAllBonus($userId, $params = []) {
  716. // 先查找会员的全部余额
  717. $userBonus = UserBonus::findOne(['USER_ID' => $userId]);
  718. // 如果没有会员余额数据,新建余额数据
  719. if (!$userBonus) {
  720. UserBonus::insertOne(['USER_ID' => $userId, 'CREATED_AT' => Date::nowTime()]);
  721. } else {
  722. $period = Period::instance();
  723. foreach (\Yii::$app->params['bonusWalletType'] as $type) {
  724. $field = strtoupper($type['name']);
  725. if ($userBonus[$field]<=0) continue;
  726. $userInfo = Info::getLastInfo($userId);
  727. $flowInsertData = [
  728. 'USER_ID' => $userId,
  729. 'LAST_DEC_LV' => $userInfo['DEC_LV'],
  730. 'LAST_EMP_LV' => $userInfo['EMP_LV'],
  731. 'LAST_STATUS' => $userInfo['STATUS'],
  732. 'CALC_ID' => $params['CALC_ID'] ?? null,
  733. 'AMOUNT' => -$userBonus[$field],
  734. 'TOTAL' => 0,
  735. 'IS_INCR' => FlowBonus::INCR_REDUCE,
  736. 'REMARK' => $params['REMARK'] ?? null,
  737. 'REMARK_IS_SHOW' => $params['REMARK_IS_SHOW'] ?? 1,
  738. 'PERIOD_NUM' => $params['PERIOD_NUM'] ?? $period->getNowPeriodNum(),
  739. 'CALC_MONTH' => $period->getNowYearMonth(),
  740. 'P_MONTH' => Date::ociToDate(),
  741. 'CREATED_AT' => $params['TIME'] ?? Date::nowTime(),
  742. 'ADMIN_NAME' => $params['ADMIN_NAME'] ?? 'system',
  743. 'DEAL_TYPE_ID' => $params['DEAL_TYPE_ID'] ?? '',
  744. 'DEAL_TYPE_IS_PRESET' => $params['DEAL_TYPE_IS_PRESET'] ?? 1,
  745. 'TRANSFER_SN' => $params['TRANSFER_SN'] ?? '',
  746. 'SORT' => $params['SORT'] ?? 0,
  747. ];
  748. // 流水
  749. if (strtolower($field) == 'bonus') FlowBonus::insertOne($flowInsertData);
  750. elseif (strtolower($field) == 'cf') {
  751. unset($flowInsertData['CALC_ID']);
  752. unset($flowInsertData['SORT']);
  753. unset($flowInsertData['TRANSFER_SN']);
  754. FlowCF::insertOne($flowInsertData);
  755. } elseif (strtolower($field) == 'lx') {
  756. unset($flowInsertData['CALC_ID']);
  757. unset($flowInsertData['SORT']);
  758. unset($flowInsertData['TRANSFER_SN']);
  759. FlowLX::insertOne($flowInsertData);
  760. }
  761. }
  762. // 清空
  763. $userBonus->BONUS = 0;
  764. $userBonus->CF = 0;
  765. $userBonus->LX = 0;
  766. if (!$userBonus->save()) {
  767. throw new Exception(Form::formatErrorsForApi($userBonus->getErrors()));
  768. }
  769. }
  770. FlowBonus::updateAll(['DELETED' => 1, 'DELETED_AT' => Date::nowTime()], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  771. FlowCF::updateAll(['DELETED' => 1, 'DELETED_AT' => Date::nowTime()], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  772. FlowLX::updateAll(['DELETED' => 1, 'DELETED_AT' => Date::nowTime()], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  773. }
  774. /**
  775. * 是否存在奖金余额
  776. * @param $userId
  777. * @return bool
  778. */
  779. public static function hasBonus($userId) {
  780. $userBonus = UserBonus::findOne(['USER_ID' => $userId]);
  781. if (!$userBonus) {
  782. return false;
  783. }
  784. foreach (\Yii::$app->params['bonusWalletType'] as $type) {
  785. $field = strtoupper($type['name']);
  786. if (isset($userBonus[$field]) && $userBonus[$field] > 0) {
  787. return true;
  788. }
  789. }
  790. return false;
  791. }
  792. /**
  793. * 获取金额用于日志
  794. * @param $userId
  795. * @return array
  796. */
  797. public static function getLogData($userId){
  798. $userWallet = UserWallet::findOne(['USER_ID' => $userId]);
  799. $cash = !empty($userWallet) ? $userWallet['CASH'] : '';
  800. $userName = Info::getUserNameByUserId($userId);
  801. $data = [];
  802. $data[$userId]['label'] = $userName.'余额';
  803. $data[$userId]['value'] = '奖金'.self::getAvailableBalance($userId).',现金'.$cash;
  804. return $data;
  805. }
  806. }