Balance.php 33 KB

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