Balance.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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['LAST_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. 'ORDER_SN' => $params['ORDER_SN'] ?? '',
  459. ];
  460. unset($userInfo, $oneUserBonus);
  461. if (strtolower($type) == 'reconsume_points' || strtolower($type) == 'cf' || strtolower($type) == 'lx'
  462. || strtolower($type) == 'exchange_points'
  463. ) {
  464. unset($flowInsertData['CALC_ID']);
  465. unset($flowInsertData['TRANSFER_SN']);
  466. unset($flowInsertData['SORT']);
  467. }
  468. if (strtolower($type) == 'bonus') {
  469. FlowBonus::insertOne($flowInsertData);
  470. } elseif (strtolower($type) == 'reconsume_points') {
  471. //记录和扣除期数的积分
  472. if( $amount > 0 ) {
  473. self::addPeriodReconsumePoints($userId, $periodNum, $amount);
  474. }else {
  475. self::deductPeriodReconsumePoints($userId, abs($amount));
  476. }
  477. FlowReconsumePoints::insertOne($flowInsertData);
  478. } elseif (strtolower($type) == 'exchange_points') {
  479. //记录和扣除期数的积分
  480. if( $amount > 0 ) {
  481. self::addPeriodExchangePoints($userId, $periodNum, $amount);
  482. }else {
  483. self::deductPeriodExchangePoints($userId, abs($amount));
  484. }
  485. FlowExchangePoints::insertOne($flowInsertData);
  486. } elseif (strtolower($type) == 'cf') {
  487. FlowCF::insertOne($flowInsertData);
  488. } elseif (strtolower($type) == 'lx') {
  489. FlowLX::insertOne($flowInsertData);
  490. }
  491. unset($flowInsertData);
  492. RedisLock::instance()->unlock($lockKey);
  493. } else {
  494. throw new Exception('流水产生错误');
  495. }
  496. return true;
  497. }
  498. /**
  499. * 添加对应期数的复消积分
  500. * @param $userId
  501. * @param $periodNum
  502. * @param $amount
  503. * @throws \yii\db\Exception
  504. * @return boolean
  505. */
  506. public static function addPeriodReconsumePoints($userId, $periodNum, $amount) {
  507. if($amount <= 0) return false;
  508. $exists = UserPeriodPoints::find()->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  509. 'USER_ID' => $userId,
  510. 'PERIOD_NUM' => $periodNum,
  511. ])->asArray()->exists();
  512. if( $exists ) {
  513. UserPeriodPoints::updateAllCounters([
  514. 'RECONSUME_POINTS' => $amount,
  515. 'REMAINDER_POINTS' => $amount,
  516. ], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  517. 'USER_ID' => $userId,
  518. 'PERIOD_NUM' => $periodNum,
  519. ]);
  520. }else {
  521. UserPeriodPoints::insertOne([
  522. 'USER_ID' => $userId,
  523. 'PERIOD_NUM' => $periodNum,
  524. 'RECONSUME_POINTS' => $amount,
  525. 'REMAINDER_POINTS' => $amount,
  526. 'EXPIRED' => 0,
  527. 'CREATED_AT' => Date::nowTime()
  528. ]);
  529. }
  530. return true;
  531. }
  532. /**
  533. * 添加对应期数的兑换积分
  534. * @param $userId
  535. * @param $periodNum
  536. * @param $amount
  537. * @throws \yii\db\Exception
  538. * @return boolean
  539. */
  540. public static function addPeriodExchangePoints($userId, $periodNum, $amount) {
  541. if($amount <= 0) return false;
  542. $exists = UserPeriodExchangePoints::find()->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  543. 'USER_ID' => $userId,
  544. 'PERIOD_NUM' => $periodNum,
  545. ])->asArray()->exists();
  546. if( $exists ) {
  547. UserPeriodExchangePoints::updateAllCounters([
  548. 'EXCHANGE_POINTS' => $amount,
  549. 'REMAINDER_POINTS' => $amount,
  550. ], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  551. 'USER_ID' => $userId,
  552. 'PERIOD_NUM' => $periodNum,
  553. ]);
  554. }else {
  555. UserPeriodExchangePoints::insertOne([
  556. 'USER_ID' => $userId,
  557. 'PERIOD_NUM' => $periodNum,
  558. 'EXCHANGE_POINTS' => $amount,
  559. 'REMAINDER_POINTS' => $amount,
  560. 'EXPIRED' => 0,
  561. 'CREATED_AT' => Date::nowTime()
  562. ]);
  563. }
  564. return true;
  565. }
  566. /**
  567. * 减少
  568. * @param $userId
  569. * @param $amount
  570. * @return bool
  571. */
  572. public static function deductPeriodReconsumePoints($userId, $amount) {
  573. if( $amount <= 0 ) return false;
  574. $avalidList = UserPeriodPoints::find()->where('USER_ID=:USER_ID AND EXPIRED=:EXPIRED AND REMAINDER_POINTS>0', [
  575. 'USER_ID' => $userId,
  576. 'EXPIRED'=>0
  577. ])->orderBy('PERIOD_NUM ASC')->asArray()->all();
  578. if( !$avalidList ) return false;
  579. foreach ($avalidList as $everyData) {
  580. if( $amount <= 0 ) break;
  581. $remainderPoints = floatval($everyData['REMAINDER_POINTS']);
  582. if( $amount >= $remainderPoints ) {
  583. UserPeriodPoints::updateAllCounters([
  584. 'REMAINDER_POINTS' => (-1) * $remainderPoints
  585. ], 'ID=:ID', ['ID'=>$everyData['ID']]);
  586. $amount -= $remainderPoints;
  587. }else {
  588. UserPeriodPoints::updateAllCounters([
  589. 'REMAINDER_POINTS' => (-1) * $amount
  590. ], 'ID=:ID', ['ID'=>$everyData['ID']]);
  591. $amount = 0;
  592. }
  593. unset($everyData, $remainderPoints);
  594. }
  595. if( $amount > 0 ) return false;
  596. return true;
  597. }
  598. /**
  599. * 减少
  600. * @param $userId
  601. * @param $amount
  602. * @return bool
  603. */
  604. public static function deductPeriodExchangePoints($userId, $amount) {
  605. if( $amount <= 0 ) return false;
  606. $avalidList = UserPeriodExchangePoints::find()->where('USER_ID=:USER_ID AND EXPIRED=:EXPIRED AND REMAINDER_POINTS>0', [
  607. 'USER_ID' => $userId,
  608. 'EXPIRED'=>0
  609. ])->orderBy('PERIOD_NUM ASC')->asArray()->all();
  610. if( !$avalidList ) return false;
  611. foreach ($avalidList as $everyData) {
  612. if( $amount <= 0 ) break;
  613. $remainderPoints = floatval($everyData['REMAINDER_POINTS']);
  614. if( $amount >= $remainderPoints ) {
  615. UserPeriodExchangePoints::updateAllCounters([
  616. 'REMAINDER_POINTS' => (-1) * $remainderPoints
  617. ], 'ID=:ID', ['ID'=>$everyData['ID']]);
  618. $amount -= $remainderPoints;
  619. }else {
  620. UserPeriodExchangePoints::updateAllCounters([
  621. 'REMAINDER_POINTS' => (-1) * $amount
  622. ], 'ID=:ID', ['ID'=>$everyData['ID']]);
  623. $amount = 0;
  624. }
  625. unset($everyData, $remainderPoints);
  626. }
  627. if( $amount > 0 ) return false;
  628. return true;
  629. }
  630. /**
  631. * 冻结用户余额
  632. * @param $userId
  633. * @param $amount
  634. * @param null $remark
  635. * @param null $time
  636. * @return bool
  637. * @throws Exception
  638. * @throws \yii\db\Exception
  639. */
  640. public static function freezeUserBonus($userId, $amount, $remark = null, $time = null) {
  641. return self::changeFreezeUserBonus($userId, $amount, ['REMARK' => $remark, 'TIME' => $time]);
  642. }
  643. /**
  644. * 解冻用户余额
  645. * @param $userId
  646. * @param $amount
  647. * @param null $remark
  648. * @param null $time
  649. * @return bool
  650. * @throws Exception
  651. * @throws \yii\db\Exception
  652. */
  653. public static function unfreezeUserBonus($userId, $amount, $remark = null, $time = null) {
  654. return self::changeFreezeUserBonus($userId, -$amount, ['REMARK' => $remark, 'TIME' => $time]);
  655. }
  656. /**
  657. * @param $userId
  658. * @param $amount
  659. * @param $params
  660. * [
  661. * 'REMARK' => '备注',
  662. * 'PERIOD_NUM' => 100,
  663. * ]
  664. * @return bool
  665. * @throws Exception
  666. * @throws \yii\db\Exception
  667. */
  668. public static function changeFreezeUserBonus($userId, $amount, $params) {
  669. if ($amount == 0) return true;
  670. $period = Period::instance();
  671. if (!isset($params['PERIOD_NUM'])) {
  672. $periodNum = $period->getNowPeriodNum();
  673. } else {
  674. $periodNum = $params['PERIOD_NUM'];
  675. }
  676. $calcYearMonth = $period->getYearMonth($periodNum);
  677. // 改变冻结
  678. $oneUserBonusModel = UserBonus::findOne(['USER_ID' => $userId]);
  679. if ($oneUserBonusModel) {
  680. $oneUserBonusModel->BONUS_FREEZE = $oneUserBonusModel->BONUS_FREEZE + $amount;
  681. } else {
  682. $oneUserBonusModel = new UserBonus();
  683. $oneUserBonusModel->USER_ID = $userId;
  684. $oneUserBonusModel->BONUS_FREEZE = $amount;
  685. }
  686. if (!$oneUserBonusModel->save()) {
  687. throw new Exception(Form::formatErrorsForApi($oneUserBonusModel->getErrors()));
  688. }
  689. unset($oneUserBonusModel);
  690. // 流水
  691. // 获取发放完成的奖金信息
  692. $oneUserBonus = UserBonus::find()->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->asArray()->one();
  693. // 记录流水
  694. $flowInsertData = [
  695. 'USER_ID' => $userId,
  696. 'AMOUNT' => abs($amount),
  697. 'TOTAL' => $oneUserBonus['BONUS'],
  698. 'IS_INCR' => $amount > 0 ? FlowBonus::INCR_FREEZE : FlowBonus::INCR_UNFREEZE,
  699. 'REMARK' => $params['REMARK'] ?? null,
  700. 'PERIOD_NUM' => $params['PERIOD_NUM'] ?? $periodNum,
  701. 'CALC_MONTH' => $calcYearMonth,
  702. 'P_MONTH' => Date::ociToDate(),
  703. 'CREATED_AT' => $params['TIME'] ?? Date::nowTime(),
  704. ];
  705. FlowBonus::insertOne($flowInsertData);
  706. unset($flowInsertData);
  707. return true;
  708. }
  709. /**
  710. * 清空会员奖金有流水
  711. * @param $userId
  712. * @param array $params
  713. * @throws Exception
  714. * @throws \yii\db\Exception
  715. */
  716. public static function clearAllBonus($userId, $params = []) {
  717. // 先查找会员的全部余额
  718. $userBonus = UserBonus::findOne(['USER_ID' => $userId]);
  719. // 如果没有会员余额数据,新建余额数据
  720. if (!$userBonus) {
  721. UserBonus::insertOne(['USER_ID' => $userId, 'CREATED_AT' => Date::nowTime()]);
  722. } else {
  723. $period = Period::instance();
  724. foreach (\Yii::$app->params['bonusWalletType'] as $type) {
  725. $field = strtoupper($type['name']);
  726. if ($userBonus[$field]<=0) continue;
  727. $userInfo = Info::getLastInfo($userId);
  728. $flowInsertData = [
  729. 'USER_ID' => $userId,
  730. 'LAST_DEC_LV' => $userInfo['DEC_LV'],
  731. 'LAST_EMP_LV' => $userInfo['EMP_LV'],
  732. 'LAST_STATUS' => $userInfo['STATUS'],
  733. 'CALC_ID' => $params['CALC_ID'] ?? null,
  734. 'AMOUNT' => -$userBonus[$field],
  735. 'TOTAL' => 0,
  736. 'IS_INCR' => FlowBonus::INCR_REDUCE,
  737. 'REMARK' => $params['REMARK'] ?? null,
  738. 'REMARK_IS_SHOW' => $params['REMARK_IS_SHOW'] ?? 1,
  739. 'PERIOD_NUM' => $params['PERIOD_NUM'] ?? $period->getNowPeriodNum(),
  740. 'CALC_MONTH' => $period->getNowYearMonth(),
  741. 'P_MONTH' => Date::ociToDate(),
  742. 'CREATED_AT' => $params['TIME'] ?? Date::nowTime(),
  743. 'ADMIN_NAME' => $params['ADMIN_NAME'] ?? 'system',
  744. 'DEAL_TYPE_ID' => $params['DEAL_TYPE_ID'] ?? '',
  745. 'DEAL_TYPE_IS_PRESET' => $params['DEAL_TYPE_IS_PRESET'] ?? 1,
  746. 'TRANSFER_SN' => $params['TRANSFER_SN'] ?? '',
  747. 'SORT' => $params['SORT'] ?? 0,
  748. ];
  749. // 流水
  750. if (strtolower($field) == 'bonus') FlowBonus::insertOne($flowInsertData);
  751. elseif (strtolower($field) == 'cf') {
  752. unset($flowInsertData['CALC_ID']);
  753. unset($flowInsertData['SORT']);
  754. unset($flowInsertData['TRANSFER_SN']);
  755. FlowCF::insertOne($flowInsertData);
  756. } elseif (strtolower($field) == 'lx') {
  757. unset($flowInsertData['CALC_ID']);
  758. unset($flowInsertData['SORT']);
  759. unset($flowInsertData['TRANSFER_SN']);
  760. FlowLX::insertOne($flowInsertData);
  761. }
  762. }
  763. // 清空
  764. $userBonus->BONUS = 0;
  765. $userBonus->CF = 0;
  766. $userBonus->LX = 0;
  767. if (!$userBonus->save()) {
  768. throw new Exception(Form::formatErrorsForApi($userBonus->getErrors()));
  769. }
  770. }
  771. FlowBonus::updateAll(['DELETED' => 1, 'DELETED_AT' => Date::nowTime()], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  772. FlowCF::updateAll(['DELETED' => 1, 'DELETED_AT' => Date::nowTime()], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  773. FlowLX::updateAll(['DELETED' => 1, 'DELETED_AT' => Date::nowTime()], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  774. }
  775. /**
  776. * 是否存在奖金余额
  777. * @param $userId
  778. * @return bool
  779. */
  780. public static function hasBonus($userId) {
  781. $userBonus = UserBonus::findOne(['USER_ID' => $userId]);
  782. if (!$userBonus) {
  783. return false;
  784. }
  785. foreach (\Yii::$app->params['bonusWalletType'] as $type) {
  786. $field = strtoupper($type['name']);
  787. if (isset($userBonus[$field]) && $userBonus[$field] > 0) {
  788. return true;
  789. }
  790. }
  791. return false;
  792. }
  793. /**
  794. * 获取金额用于日志
  795. * @param $userId
  796. * @return array
  797. */
  798. public static function getLogData($userId){
  799. $userWallet = UserWallet::findOne(['USER_ID' => $userId]);
  800. $cash = !empty($userWallet) ? $userWallet['CASH'] : '';
  801. $userName = Info::getUserNameByUserId($userId);
  802. $data = [];
  803. $data[$userId]['label'] = $userName.'余额';
  804. $data[$userId]['value'] = '奖金'.self::getAvailableBalance($userId).',现金'.$cash;
  805. return $data;
  806. }
  807. }