MemberCoinInfo.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * 平台币使用记录信息
  4. */
  5. namespace app\common\model;
  6. use think\Env;
  7. use think\Exception;
  8. use think\Model;
  9. use think\Db;
  10. class MemberCoinInfo extends Model
  11. {
  12. protected $table = 'cy_member_coin_info';
  13. protected $dateFormat = false;
  14. /**
  15. * 待支付订单,交易取消释放平台币
  16. *
  17. * @param $userid
  18. * @param $orderid
  19. * @param $status 支付状态:0=待支付,1=已支付,2=已取消, 3=超时取消
  20. *
  21. * @return bool|void
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. * @throws \think\exception\DbException
  25. */
  26. public function releaseCoin($userid, $orderid = '', $status = 2)
  27. {
  28. if($orderid){
  29. $cache = sprintf('MemberCoinInfo:releaseCoin:order:%s:%s',$userid,$orderid);
  30. }else{
  31. $cache = sprintf('MemberCoinInfo:releaseCoin:user:%s',$userid);
  32. }
  33. if(!requestDuplicateCheck($cache,10)){
  34. return true;
  35. }
  36. $payModel = new Pay();
  37. if ($orderid) {
  38. $payList = $payModel->field('gameid,real_ptb,real_coin,orderid,id,coupon_amount,coupon_member_id')->where(['orderid' => $orderid, 'userid' => $userid, 'status' => 0])->limit(1)->select();
  39. } else {
  40. //下单已经超过30分钟的待支付订单
  41. $payList = $payModel->field('gameid,real_ptb,real_coin,orderid,id,coupon_amount,coupon_member_id')
  42. ->where(['create_time' => ['<=', NOW_TIMESTAMP - (60 * 30)], 'userid' => $userid, 'status' => 0])
  43. ->whereRaw('(real_ptb>0 or real_coin>0 or coupon_amount>0)')
  44. ->where(['create_time'=>['>', NOW_TIMESTAMP - 86400*7]])
  45. ->select();
  46. }
  47. //订单信息不为空时
  48. if (!empty($payList)) {
  49. foreach ($payList as $key => $value) {
  50. $memberInfo = Db::table('cy_members')->where(['id' => $userid])->find();
  51. $gameInfo = Db::table('cy_game')->where(['id' => $value['gameid']])->find();
  52. if ($memberInfo && $gameInfo) {
  53. // 启动事务
  54. Db::startTrans();
  55. try {
  56. //包含平台币
  57. if ($value['real_coin'] > 0) {
  58. $members = Db::table('cy_members')->field('id,amount')->where(['id' => $userid])->find();
  59. $coinData = array();
  60. $coinData['amount'] = Db::raw("amount+" . $value['real_coin']);
  61. $coinData['update_time'] = time();
  62. $result = Db::table('cy_members')->where(['id' => $members['id']])->update($coinData);
  63. if (!$result) {
  64. throw new Exception("用户游戏平台币账户金额变动失败");
  65. }
  66. $amount = priceFormat($members['amount'] + $value['real_coin']);
  67. $coinData = [
  68. 'userid' => $members['id'],
  69. 'orderid' => $value['orderid'] . "_T",
  70. 'start_amount' => $members['amount'],
  71. 'amount' => $value['real_coin'],
  72. 'result_amount' => $amount,
  73. 'type' => 3,
  74. 'remarks' => '平台币未付款退回',
  75. 'create_time' => NOW_TIMESTAMP
  76. ];
  77. if (!$this->insertGetId($coinData)) {
  78. throw new Exception("订单消费释放状态更改失败,订单保存失败");
  79. }
  80. }
  81. //专属币处理
  82. if ($value['real_ptb'] > 0) {
  83. $userCoinInfo = Db::table('nw_member_zscoin')->field('id,userid,username,game_id,amount,status')->where(['userid' => $userid, 'game_id' => $value['gameid']])->find();
  84. if (!empty($userCoinInfo)) {
  85. $coinData = array();
  86. $coinData['amount'] = Db::raw("amount+" . $value['real_ptb']);
  87. $coinData['update_time'] = time();
  88. $result = Db::table('nw_member_zscoin')->where(['id' => $userCoinInfo['id'], 'userid' => $userCoinInfo['userid'], 'game_id' => $userCoinInfo['game_id']])->update($coinData);
  89. if (!$result) {
  90. throw new Exception("用户游戏平台币账户金额变动失败");
  91. }
  92. $coinDetData = array();
  93. $coinDetData['userid'] = $userid;
  94. $coinDetData['username'] = $memberInfo['username'];
  95. $coinDetData['game_id'] = $value['gameid'];
  96. $coinDetData['type'] = 4;
  97. $coinDetData['prev_amount'] = 0;
  98. $coinDetData['change_amount'] = $value['real_ptb'];
  99. $coinDetData['after_amount'] = $coinDetData['prev_amount'] + $value['real_ptb'];
  100. $coinDetData['out_orderid'] = $value['orderid'];
  101. $coinDetData['create_time'] = time();
  102. $coinDetData['update_time'] = time();
  103. $insertDetId = Db::table('nw_member_zscoin_det')->insertGetId($coinDetData);
  104. if (!$insertDetId) {
  105. throw new Exception("添加用户游戏平台币变动明细失败");
  106. }
  107. } else {
  108. $coinData = array();
  109. $coinData['userid'] = $userid;
  110. $coinData['username'] = $memberInfo['username'];
  111. $coinData['game_id'] = $value['gameid'];
  112. $coinData['amount'] = $value['real_ptb'];
  113. $coinData['status'] = 1;
  114. $coinData['create_time'] = time();
  115. $coinData['update_time'] = time();
  116. $result = Db::table('nw_member_zscoin')->insertGetId($coinData);
  117. if (!$result) {
  118. throw new Exception("添加用户游戏平台币账户失败");
  119. }
  120. $coinDetData = array();
  121. $coinDetData['userid'] = $userid;
  122. $coinDetData['username'] = $memberInfo['username'];
  123. $coinDetData['game_id'] = $value['gameid'];
  124. $coinDetData['type'] = 4;
  125. $coinDetData['prev_amount'] = 0;
  126. $coinDetData['change_amount'] = $value['real_ptb'];
  127. $coinDetData['after_amount'] = $coinDetData['prev_amount'] + $value['real_ptb'];
  128. $coinDetData['out_orderid'] = $value['orderid'];
  129. $coinDetData['create_time'] = time();
  130. $coinDetData['update_time'] = time();
  131. $insertDetId = Db::table('nw_member_zscoin_det')->insertGetId($coinDetData);
  132. if (!$insertDetId) {
  133. throw new Exception("添加用户游戏平台币变动明细失败");
  134. }
  135. }
  136. }
  137. //代金券处理
  138. if ($value['coupon_member_id'] > 0 && $value['coupon_amount'] > 0) {
  139. $coupon = model('common/CouponMember')->updateCouponIsUseById($value['coupon_member_id'], 1);
  140. if (!$coupon) {
  141. throw new Exception("订单消费释放状态更改失败");
  142. }
  143. log_message('支付代金券取消支付:订单号'.$value['id'].'代金券id'.$value['coupon_member_id'] , 'log', LOG_PATH . 'MemberCoinInfo/releaseCoin/');
  144. }
  145. //更新订单状态
  146. $updPayResult = Db::table('cy_pay')->where(['id' => $value['id'], 'userid' => $userid, 'status' => 0])->update(['status' => $status, 'coupon_member_id'=>0]);
  147. if (!$updPayResult) {
  148. throw new Exception("订单消费释放状态更改失败");
  149. }
  150. //更新订单通知详情状态
  151. $updPayResult = Db::table('cy_paycpinfo')->where(['orderid' => $value['orderid'], 'payflag' => 0])->update(['payflag' => $status]);
  152. if (!$updPayResult) {
  153. throw new Exception("订单通知详情处理失败");
  154. }
  155. // 提交事务
  156. Db::commit();
  157. if ($orderid) {
  158. return true;
  159. }
  160. } catch (\Exception $e) {
  161. // 回滚事务
  162. Db::rollback();
  163. $template = "订单 " . $value['orderid'] . " 平台币消费释放存在异常:" . $e->getMessage()." - file: ".$e->getFile().":".$e->getLine() . "。请及时核实,时间:" . date('Y-m-d H:i:s');
  164. $ddurl = Env::get('dingtalk.warning_url');
  165. curlDD($template, $ddurl, true);
  166. if ($orderid) {
  167. return false;
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. public function getCreateTimeAttr($value)
  175. {
  176. return date('Y-m-d H:i:s', $value);
  177. }
  178. }