MemWallet.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. /**
  3. * MemWallet.php UTF-8
  4. * 玩家钱包
  5. *
  6. * @date : 2018/5/18 22:14
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\member;
  13. use huo\controller\common\Base;
  14. use huo\controller\wallet\GmCache;
  15. use huo\controller\wallet\WalletRequest;
  16. use huo\model\finance\GmBackModel;
  17. use huo\model\finance\GmChargeModel;
  18. use huo\model\finance\GmOrderModel;
  19. use huo\model\finance\PtbBackModel;
  20. use huo\model\finance\PtbChargeModel;
  21. use huo\model\finance\PtbOrderModel;
  22. use huolib\constant\OrderConst;
  23. use huolib\constant\SettleConst;
  24. use huolib\constant\WalletConst;
  25. use huolib\status\CommonStatus;
  26. use huolib\status\MemberStatus;
  27. use huolib\status\SettleStatus;
  28. use huolib\tool\StrUtils;
  29. class MemWallet extends Base {
  30. /**
  31. * 更新渠道平台币
  32. *
  33. * @param int $mem_id
  34. * @param float $ptb_cnt
  35. * @param int $type
  36. *
  37. * @return int
  38. */
  39. public function updatePtb($mem_id, $ptb_cnt, $type = SettleConst::SETTLE_WALLET_NO) {
  40. $_mc_class = MemCache::ins();
  41. $_me_data = $_mc_class->getMeInfoById($mem_id);
  42. if (empty($_me_data)) {
  43. return SettleStatus::REMAIN_NOT_ENOUGH;
  44. }
  45. $_total = $_me_data['ptb_total'];
  46. $_remain = $_me_data['ptb_cnt'];
  47. switch ($type) {
  48. case SettleConst::SETTLE_WALLET_DEDUCT:
  49. $_remain -= $ptb_cnt;
  50. break;
  51. case SettleConst::SETTLE_WALLET_ADD:
  52. $_remain += $ptb_cnt;
  53. $_total += $ptb_cnt;
  54. break;
  55. default:
  56. }
  57. if (StrUtils::compareNumber($_remain, 0) < 0) {
  58. return SettleStatus::REMAIN_NOT_ENOUGH;
  59. }
  60. $_me_data['ptb_total'] = $_total;
  61. $_me_data['ptb_cnt'] = $_remain;
  62. $_rs = $_mc_class->updateMeCache($mem_id, $_me_data);
  63. if (false == $_rs) {
  64. return SettleStatus::INNER_ERROR;
  65. } else {
  66. return SettleStatus::NO_ERROR;
  67. }
  68. }
  69. /**
  70. * 更新玩家游戏币
  71. *
  72. * @param int $mem_id
  73. * @param int $app_id
  74. * @param float $gm_cnt
  75. * @param int $type
  76. *
  77. * @return int
  78. */
  79. public function updateGm($mem_id, $app_id, $gm_cnt, $type = SettleConst::SETTLE_WALLET_NO) {
  80. $_gmc_class = GmCache::ins();
  81. $_gm_data = $_gmc_class->getInfoByMemGame($mem_id, $app_id);
  82. if (empty($_gm_data)) {
  83. return SettleStatus::REMAIN_NOT_ENOUGH;
  84. }
  85. $_total = $_gm_data['total'];
  86. $_remain = $_gm_data['remain'];
  87. switch ($type) {
  88. case SettleConst::SETTLE_WALLET_DEDUCT:
  89. $_remain -= $gm_cnt;
  90. break;
  91. case SettleConst::SETTLE_WALLET_ADD:
  92. $_remain += $gm_cnt;
  93. $_total += $gm_cnt;
  94. break;
  95. default:
  96. }
  97. if (StrUtils::compareNumber($_remain, 0) < 0) {
  98. return SettleStatus::REMAIN_NOT_ENOUGH;
  99. }
  100. $_gm_data['total'] = $_total;
  101. $_gm_data['remain'] = $_remain;
  102. $_rs = $_gmc_class->updateGm($mem_id, $app_id, $_gm_data);
  103. if (false == $_rs) {
  104. return SettleStatus::INNER_ERROR;
  105. } else {
  106. return SettleStatus::NO_ERROR;
  107. }
  108. }
  109. /**
  110. * 玩家平台币充值订单
  111. *
  112. * @param WalletRequest $wr
  113. *
  114. * @return int
  115. */
  116. public function createPcOrder(WalletRequest $wr) {
  117. /* 更新玩家平台币 */
  118. if (OrderConst::PAY_STATUS_SUC == $wr->getStatus()) {
  119. $_rs = $this->updatePtb($wr->getMemId(), $wr->getPtbCnt(), SettleConst::SETTLE_WALLET_ADD);
  120. if (SettleStatus::NO_ERROR != $_rs) {
  121. return $_rs;
  122. }
  123. }
  124. /* 插入玩家平台币收入订单 */
  125. $_pc_data = $wr->getPtbChargeData();
  126. $_rs = (new PtbChargeModel())->createOrder($_pc_data);
  127. if (false == $_rs) {
  128. return SettleStatus::ORDER_CREATE_FAIL;
  129. }
  130. return SettleStatus::NO_ERROR;
  131. }
  132. /**
  133. * 玩家平台币消费订单
  134. *
  135. * @param WalletRequest $wr
  136. *
  137. * @return int
  138. */
  139. public function createPoOrder(WalletRequest $wr) {
  140. /* 更新玩家平台币 */
  141. if (OrderConst::PAY_STATUS_SUC == $wr->getStatus()) {
  142. /* 更新玩家平台币 */
  143. $_rs = $this->updatePtb($wr->getMemId(), $wr->getCostPtbCnt(), SettleConst::SETTLE_WALLET_DEDUCT);
  144. if (SettleStatus::NO_ERROR != $_rs) {
  145. return $_rs;
  146. }
  147. }
  148. /* 插入玩家平台币消费订单 */
  149. $_po_data = $wr->getPtbOrderData();
  150. $_po_data['status'] = OrderConst::PAY_STATUS_SUC;
  151. $_rs = (new PtbOrderModel())->createOrder($_po_data);
  152. if (false == $_rs) {
  153. return SettleStatus::ORDER_CREATE_FAIL;
  154. }
  155. return SettleStatus::NO_ERROR;
  156. }
  157. /**
  158. * 玩家游戏币充值订单
  159. *
  160. * @param WalletRequest $wr
  161. *
  162. * @return int
  163. */
  164. public function createGmcOrder(WalletRequest $wr) {
  165. /* 更新玩家游戏币 */
  166. if (OrderConst::PAY_STATUS_SUC == $wr->getStatus()) {
  167. /* 更新玩家游戏币 */
  168. $_rs = $this->updateGm($wr->getMemId(),$wr->getAppId(), $wr->getGmCnt(), SettleConst::SETTLE_WALLET_ADD);
  169. if (SettleStatus::NO_ERROR != $_rs) {
  170. return $_rs;
  171. }
  172. }
  173. /* 插入玩家游戏充值订单 */
  174. $_gmc_data = $wr->getGmChargeData();
  175. $_rs = (new GmChargeModel())->createOrder($_gmc_data);
  176. if (false == $_rs) {
  177. return SettleStatus::ORDER_CREATE_FAIL;
  178. }
  179. return SettleStatus::NO_ERROR;
  180. }
  181. /**
  182. * 玩家游戏币消费订单
  183. *
  184. * @param WalletRequest $wr
  185. *
  186. * @return int
  187. */
  188. public function createGmoOrder(WalletRequest $wr) {
  189. /* 更新玩家游戏币 */
  190. if (OrderConst::PAY_STATUS_SUC == $wr->getStatus()) {
  191. /* 更新玩家游戏币 */
  192. $_rs = $this->updateGm($wr->getMemId(), $wr->getAppId(), $wr->getCostGmCnt(), SettleConst::SETTLE_WALLET_DEDUCT);
  193. if (SettleStatus::NO_ERROR != $_rs) {
  194. return $_rs;
  195. }
  196. }
  197. /* 插入玩家平台币消费订单 */
  198. $_gmo_data = $wr->getGmOrderData();
  199. $_gmo_data['status'] = OrderConst::PAY_STATUS_SUC;
  200. $_rs = (new GmOrderModel())->createOrder($_gmo_data);
  201. if (false == $_rs) {
  202. return SettleStatus::ORDER_CREATE_FAIL;
  203. }
  204. return SettleStatus::NO_ERROR;
  205. }
  206. /**
  207. * 玩家平台币扣除
  208. *
  209. * @param WalletRequest $wr
  210. *
  211. * @return array|bool|int
  212. */
  213. private function createDeductOrder(WalletRequest $wr) {
  214. /* 更新玩家平台币 */
  215. if (OrderConst::PAY_STATUS_SUC == $wr->getStatus()) {
  216. /* 更新玩家平台币 */
  217. $_rs = $this->updatePtb($wr->getMemId(), $wr->getBackPtbCnt(), SettleConst::SETTLE_WALLET_DEDUCT);
  218. if (SettleStatus::NO_ERROR != $_rs) {
  219. return $_rs;
  220. }
  221. }
  222. /* 插入玩家平台币扣除订单 */
  223. $_deduct_data = $wr->getPtbPlayerDeductData();
  224. $_rs = (new PtbBackModel())->createOrder($_deduct_data);
  225. if (false == $_rs) {
  226. return SettleStatus::ORDER_CREATE_FAIL;
  227. }
  228. return SettleStatus::NO_ERROR;
  229. }
  230. /**
  231. * 玩家游戏币扣除
  232. *
  233. * @param WalletRequest $wr
  234. *
  235. * @return array|bool|int
  236. */
  237. private function createDeductGmOrder(WalletRequest $wr) {
  238. /* 更新玩家游戏币 */
  239. if (OrderConst::PAY_STATUS_SUC == $wr->getStatus()) {
  240. /* 更新玩家平台币 */
  241. $_rs = $this->updateGm($wr->getMemId(), $wr->getAppId(), $wr->getBackGmCnt(), SettleConst::SETTLE_WALLET_DEDUCT);
  242. if (SettleStatus::NO_ERROR != $_rs) {
  243. return $_rs;
  244. }
  245. }
  246. /* 插入玩家游戏币扣除订单 */
  247. $_deduct_data = $wr->getGmPlayerDeductData();
  248. $_rs = (new GmBackModel())->createOrder($_deduct_data);
  249. if (false == $_rs) {
  250. return SettleStatus::ORDER_CREATE_FAIL;
  251. }
  252. return SettleStatus::NO_ERROR;
  253. }
  254. /**
  255. * 官方给玩家发放平台币
  256. *
  257. * @param $mem_id
  258. * @param $ptb_cnt
  259. * @param $remark
  260. *
  261. * @return array
  262. */
  263. public function gainMemPtb($mem_id, $ptb_cnt, $remark) {
  264. $_mc_class = MemCache::ins();
  265. $_mem_data = $_mc_class->getInfoById($mem_id);
  266. $_me_data = $_mc_class->getMeInfoById($mem_id);
  267. if (empty($_mem_data) || empty($_me_data)) {
  268. $_code = MemberStatus::UID_INVALID;
  269. return $this->huoError($_code, MemberStatus::getMsg($_code));
  270. }
  271. $_wr_class = new WalletRequest();
  272. $_order_id = StrUtils::genOrderId(
  273. $_mem_data['agent_id'], $_mem_data['agent_id'], $mem_id, WalletConst::WALLET_ORDER_PREFIX_O2M
  274. );
  275. $_wr_class->setMemId($mem_id);
  276. $_wr_class->setOrderId($_order_id);
  277. $_wr_class->setType(WalletConst::WALLET_FROM_GIVE);
  278. $_wr_class->setPtbCnt($ptb_cnt);
  279. $_wr_class->setStatus(OrderConst::PAY_STATUS_SUC);
  280. $_wr_class->setRemark($remark);
  281. $_rs = $this->createPcOrder($_wr_class);
  282. if (SettleStatus::NO_ERROR != $_rs) {
  283. return $this->huoError($_rs, SettleStatus::getMsg($_rs));
  284. }
  285. $_code = CommonStatus::NO_ERROR;
  286. return $this->huoError($_code, CommonStatus::getMsg($_code));
  287. }
  288. /**
  289. * 官方从玩家账户中扣回平台币
  290. *
  291. * @param $mem_id
  292. * @param $order_ptb_cnt
  293. * @param $ptb_cnt
  294. * @param $remark
  295. * @param null $order_id
  296. *
  297. * @return array
  298. */
  299. public function deductMemPtb($mem_id, $order_ptb_cnt, $ptb_cnt, $remark, $order_id = null) {
  300. $_mc_class = MemCache::ins();
  301. $_mem_data = $_mc_class->getInfoById($mem_id);
  302. $_me_data = $_mc_class->getMeInfoById($mem_id);
  303. if (empty($_mem_data) || empty($_me_data)) {
  304. $_code = MemberStatus::UID_INVALID;
  305. return $this->huoError($_code, MemberStatus::getMsg($_code));
  306. }
  307. $_wr_class = new WalletRequest();
  308. $_wr_class->setMemId($mem_id);
  309. $_wr_class->setPtbCnt($order_ptb_cnt);
  310. $_wr_class->setBackPtbCnt($ptb_cnt);
  311. $_wr_class->setStatus(OrderConst::PAY_STATUS_SUC);
  312. $_wr_class->setRemark($remark);
  313. if (!is_null($order_id)) {
  314. $_wr_class->setOrderId($order_id);
  315. }
  316. $_rs = (new MemWallet())->createDeductOrder($_wr_class);
  317. if (SettleStatus::NO_ERROR != $_rs) {
  318. return $this->huoError($_rs, SettleStatus::getMsg($_rs));
  319. }
  320. $_code = CommonStatus::NO_ERROR;
  321. return $this->huoError($_code, CommonStatus::getMsg($_code));
  322. }
  323. /**
  324. * 官方给玩家发放游戏币
  325. *
  326. * @param $mem_id
  327. * @param $app_id
  328. * @param $gm_cnt
  329. * @param $remark
  330. *
  331. * @return array
  332. */
  333. public function gainMemGm($mem_id, $app_id, $gm_cnt, $remark) {
  334. $_mc_class = MemCache::ins();
  335. $_mem_data = $_mc_class->getInfoById($mem_id);
  336. $_gmc_class = GmCache::ins();
  337. $_gm_data = $_gmc_class->getInfoByMemGame($mem_id, $app_id);
  338. if (empty($_mem_data) || empty($_gm_data)) {
  339. $_code = MemberStatus::UID_INVALID;
  340. return $this->huoError($_code, MemberStatus::getMsg($_code));
  341. }
  342. $_wr_class = new WalletRequest();
  343. $_order_id = StrUtils::genOrderId(
  344. $_mem_data['agent_id'], $_mem_data['agent_id'], $mem_id, WalletConst::WALLET_ORDER_PREFIX_O2GM
  345. );
  346. $_wr_class->setMemId($mem_id);
  347. $_wr_class->setAppId($app_id);
  348. $_wr_class->setOrderId($_order_id);
  349. $_wr_class->setType(WalletConst::WALLET_FROM_GIVE);
  350. $_wr_class->setGmCnt($gm_cnt);
  351. $_wr_class->setStatus(OrderConst::PAY_STATUS_SUC);
  352. $_wr_class->setRemark($remark);
  353. $_rs = $this->createGmcOrder($_wr_class);
  354. if (SettleStatus::NO_ERROR != $_rs) {
  355. return $this->huoError($_rs, SettleStatus::getMsg($_rs));
  356. }
  357. $_code = CommonStatus::NO_ERROR;
  358. return $this->huoError($_code, CommonStatus::getMsg($_code));
  359. }
  360. /**
  361. * 官方从玩家账户中扣回游戏币
  362. *
  363. * @param $mem_id
  364. * @param $app_id
  365. * @param $order_gm_cnt
  366. * @param $gm_cnt
  367. * @param $remark
  368. * @param null $order_id
  369. *
  370. * @return array
  371. */
  372. public function deductMemGm($mem_id, $app_id, $order_gm_cnt, $gm_cnt, $remark, $order_id = null) {
  373. $_mc_class = MemCache::ins();
  374. $_mem_data = $_mc_class->getInfoById($mem_id);
  375. $_gmc_class = GmCache::ins();
  376. $_gm_data = $_gmc_class->getInfoByMemGame($mem_id, $app_id);
  377. if (empty($_mem_data) || empty($_gm_data)) {
  378. $_code = MemberStatus::UID_INVALID;
  379. return $this->huoError($_code, MemberStatus::getMsg($_code));
  380. }
  381. $_wr_class = new WalletRequest();
  382. $_wr_class->setMemId($mem_id);
  383. $_wr_class->setAppId($app_id);
  384. $_wr_class->setGmCnt($order_gm_cnt);
  385. $_wr_class->setBackGmCnt($gm_cnt);
  386. $_wr_class->setStatus(OrderConst::PAY_STATUS_SUC);
  387. $_wr_class->setRemark($remark);
  388. if (!is_null($order_id)) {
  389. $_wr_class->setOrderId($order_id);
  390. }
  391. $_rs = (new MemWallet())->createDeductGmOrder($_wr_class);
  392. if (SettleStatus::NO_ERROR != $_rs) {
  393. return $this->huoError($_rs, SettleStatus::getMsg($_rs));
  394. }
  395. $_code = CommonStatus::NO_ERROR;
  396. return $this->huoError($_code, CommonStatus::getMsg($_code));
  397. }
  398. }