MemIntegral.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * MemIntegral.php UTF-8
  4. * 玩家积分
  5. *
  6. * @date : 2018/5/5 14:00
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\controller\integral;
  13. use huo\controller\agent\AgentWallet;
  14. use huo\controller\common\Base;
  15. use huo\controller\common\CommonFunc;
  16. use huo\controller\game\GameCache;
  17. use huo\controller\member\MemCache;
  18. use huo\logic\shop\ItgLogic;
  19. use huo\model\agent\AgentOrderModel;
  20. use huo\model\integral\MemItgLogModel;
  21. use huo\model\user\UserModel;
  22. use huolib\constant\IaConst;
  23. use huolib\constant\MemItgConst;
  24. use huolib\constant\SettleConst;
  25. use huolib\constant\WalletConst;
  26. use huolib\status\CommonStatus;
  27. use huolib\status\IntegralStatus;
  28. use huolib\status\SettleStatus;
  29. use huolib\tool\StrUtils;
  30. use huomp\controller\member\MemberOut;
  31. use huoMpMsg\controller\OaOut;
  32. use think\Log;
  33. class MemIntegral extends Base {
  34. /**
  35. * 获取玩家积分列表
  36. *
  37. *
  38. * @param array $where
  39. * @param string $page
  40. * @param int $mem_id
  41. *
  42. * @return array
  43. */
  44. public function getItgList($where = [], $page = '1,10', $mem_id) {
  45. $_order = '-create_time';
  46. $where['mem_id'] = $mem_id;
  47. $_data = (new ItgLogic())->getItgList($where, $page, $_order);
  48. return $this->retSucMsg(CommonStatus::NO_ERROR, $_data);
  49. }
  50. /**
  51. * 设置玩家积分
  52. *
  53. * @param int $mem_id 玩家ID
  54. * @param int $integral 获得的积分
  55. * @param int $type 积分变化类型
  56. * @param int $ia_id 积分活动ID
  57. * @param int $sub_ia_id 子活动ID
  58. * @param string $link_table 关联的表
  59. * @param int $link_id 关联表ID
  60. * @param int $app_id 盒子ID
  61. *
  62. * @param string $ia_name 活动名称,当不是积分任务中的任务改变积分时必传
  63. *
  64. * @return int
  65. */
  66. public function setMemItg(
  67. $mem_id, $integral, $type, $ia_id = 0, $sub_ia_id = 0, $link_table = '', $link_id = 0, $ia_name = '',
  68. $app_id = 0
  69. ) {
  70. if ($integral < 0) {
  71. return IntegralStatus::ITG_MEM_TYPE_ERROR;
  72. }
  73. $_mc_class = MemCache::ins();
  74. $_me_data = $_mc_class->getMeInfoById($mem_id);
  75. if (empty($_me_data)) {
  76. return IntegralStatus::ITG_MEM_NOT_EXISTS;
  77. }
  78. $_itg = $integral;
  79. switch ($type) {
  80. case MemItgConst::MEM_ITG_ADD:
  81. case MemItgConst::MEM_ITG_OFFICE_GIVE:
  82. $_me_data['integral_total'] += $integral;
  83. $_me_data['my_integral'] += $integral;
  84. break;
  85. case MemItgConst::MEM_ITG_DEDUCT:
  86. case MemItgConst::MEM_ITG_OFFICE_DEDUCT:
  87. if ($integral > $_me_data['my_integral']) {
  88. /* 玩家积分不足 */
  89. return IntegralStatus::ITG_NOT_ENOUGH;
  90. }
  91. $_me_data['my_integral'] -= $integral;
  92. $_itg = 0 - $integral;
  93. break;
  94. default:
  95. return IntegralStatus::ITG_MEM_TYPE_ERROR;
  96. }
  97. $_rs = $_mc_class->updateMeCache($mem_id, $_me_data);
  98. if (false == $_rs) {
  99. return IntegralStatus::INNER_ERROR;
  100. }
  101. $_ia_data = IaCache::ins()->getIa($ia_id);
  102. /* 插入积分Log */
  103. $_log_data['mem_id'] = $mem_id;
  104. $_log_data['itg_type'] = $type;
  105. $_log_data['integral_total'] = $_me_data['integral_total'];
  106. $_log_data['my_integral'] = $_me_data['my_integral'];
  107. $_log_data['integral'] = $integral;
  108. $_log_data['ia_id'] = $ia_id;
  109. $_log_data['ia_name'] = empty($ia_name) ? $_ia_data['ia_name'] : $ia_name;
  110. $_log_data['sub_ia_id'] = $sub_ia_id;
  111. $_log_data['link_table'] = $link_table;
  112. $_log_data['link_id'] = $link_id;
  113. $_log_data['app_id'] = $app_id;
  114. if (IaConst::IA_PLAY_3 == $ia_id) {
  115. $_play_data = (new MemberOut())->getPlayCnt($mem_id);
  116. $_pay_cnt = empty($_play_data['cnt']) ? 1 : $_play_data['cnt'] + 1;
  117. $_log_data['ia_name'] .= $_pay_cnt;
  118. }
  119. $_rs = (new MemItgLogModel())->addMemIaLog($_log_data);
  120. if (false == $_rs) {
  121. return IntegralStatus::ITG_IA_FAIL;
  122. }
  123. if (!empty($ia_id)) {
  124. $_rs = MemIaCache::ins()->updateMemIa($ia_id, $mem_id, $_itg);
  125. if (false == $_rs) {
  126. return IntegralStatus::ITG_IA_FAIL;
  127. }
  128. }
  129. return IntegralStatus::NO_ERROR;
  130. }
  131. /**
  132. * 增加玩家积分
  133. *
  134. * @param int $mem_id 玩家ID
  135. * @param int $integral 获得的积分
  136. * @param int $ia_id 积分活动ID
  137. * @param int $sub_ia_id 子活动ID
  138. * @param string $link_table 关联的表
  139. * @param int $link_id 关联表ID
  140. * @param string $ia_name 活动名称,当不是积分任务中的任务改变积分时必传
  141. * @param int $app_id 应用ID 添加到哪个盒子
  142. * @param bool $is_rp 是否转红包
  143. *
  144. * @return int
  145. */
  146. public function addMemItg(
  147. $mem_id, $integral, $ia_id = 0, $sub_ia_id = 0, $link_table = '', $link_id = 0, $ia_name = '',
  148. $app_id = 0, $is_rp = false
  149. ) {
  150. $_rs = $this->setMemItg(
  151. $mem_id, $integral, MemItgConst::MEM_ITG_ADD, $ia_id, $sub_ia_id, $link_table, $link_id, $ia_name, $app_id
  152. );
  153. if (IntegralStatus::NO_ERROR != $_rs) {
  154. return $_rs;
  155. }
  156. $_is_rp_box = GameCache::ins()->isRpBox($app_id);
  157. if ($_is_rp_box != true && $is_rp == true) {
  158. $_is_rp_box = true;
  159. }
  160. if (true == $_is_rp_box) {
  161. /* 红包盒子 直接转换所有积分为红包 */
  162. $_ia_data = IaCache::ins()->getIa($ia_id);
  163. $ia_name = empty($ia_name) ? $_ia_data['ia_name'] : $ia_name;
  164. $this->setMemItg(
  165. $mem_id, $integral, MemItgConst::MEM_ITG_DEDUCT, $ia_id, $sub_ia_id, $link_table, $link_id,
  166. $ia_name.'--转换红包'
  167. );
  168. return $this->addWalletByItg($mem_id, $integral, $app_id, $ia_name);
  169. }
  170. // (new OaOut())->sendGoldChangeMsg($mem_id, $app_id, $integral);
  171. return IntegralStatus::NO_ERROR;
  172. }
  173. /**
  174. * 通过积分转化为钱包余额
  175. *
  176. * @param int $mem_id 玩家ID
  177. * @param int $integral 获得的积分
  178. * @param int $app_id 应用ID 添加到哪个盒子
  179. * @param string $remark 备注信息
  180. *
  181. * @return int
  182. */
  183. public function addWalletByItg($mem_id, $integral, $app_id = 0, $remark = '') {
  184. /* 红包版本直接转化为红包余额 */
  185. $_agent_id = (new UserModel())->getIdByMemId($mem_id);
  186. if (!empty($_agent_id)) {
  187. $_gold_rmb_rate = CommonFunc::getGoldRmbRate();
  188. $_amount = StrUtils::formatNumber($integral / $_gold_rmb_rate);
  189. $_rs = (new AgentWallet())->updateWallet($_agent_id, $_amount, SettleConst::SETTLE_WALLET_ADD);
  190. if (SettleStatus::NO_ERROR == $_rs) {
  191. /* 添加到agentOrder 渠道余额记录*/
  192. $_agent_id = (new UserModel())->getIdByMemId($mem_id);
  193. $_ao_data = [
  194. 'order_id' => StrUtils::genOrderId(
  195. $_agent_id, $_agent_id, $mem_id, WalletConst::WALLET_ORDER_PREFIX_MP
  196. ),
  197. 'agent_id' => $_agent_id,
  198. 'agent_gain' => $_amount,
  199. 'status' => 2,//成功
  200. 'remark' => $remark
  201. ];
  202. (new AgentOrderModel())->createOrder($_ao_data);
  203. (new OaOut())->sendMoneyChangeMsg($mem_id, $app_id, $_amount);
  204. }
  205. return $_rs;
  206. } else {
  207. Log::write(
  208. "func=".__FUNCTION__."&class=".__CLASS__."&mem_id=".$mem_id."&integral=".$integral,
  209. LOG::ERROR
  210. );
  211. return IntegralStatus::NO_ERROR;
  212. }
  213. }
  214. }