Balance.php 30 KB

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