| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- /**
- * 平台币使用记录信息
- */
- namespace app\common\model;
- use think\Env;
- use think\Exception;
- use think\Model;
- use think\Db;
- class MemberCoinInfo extends Model
- {
- protected $table = 'cy_member_coin_info';
- protected $dateFormat = false;
- /**
- * 待支付订单,交易取消释放平台币
- *
- * @param $userid
- * @param $orderid
- * @param $status 支付状态:0=待支付,1=已支付,2=已取消, 3=超时取消
- *
- * @return bool|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function releaseCoin($userid, $orderid = '', $status = 2)
- {
- if($orderid){
- $cache = sprintf('MemberCoinInfo:releaseCoin:order:%s:%s',$userid,$orderid);
- }else{
- $cache = sprintf('MemberCoinInfo:releaseCoin:user:%s',$userid);
- }
- if(!requestDuplicateCheck($cache,10)){
- return true;
- }
- $payModel = new Pay();
- if ($orderid) {
- $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();
- } else {
- //下单已经超过30分钟的待支付订单
- $payList = $payModel->field('gameid,real_ptb,real_coin,orderid,id,coupon_amount,coupon_member_id')
- ->where(['create_time' => ['<=', NOW_TIMESTAMP - (60 * 30)], 'userid' => $userid, 'status' => 0])
- ->whereRaw('(real_ptb>0 or real_coin>0 or coupon_amount>0)')
- ->where(['create_time'=>['>', NOW_TIMESTAMP - 86400*7]])
- ->select();
- }
- //订单信息不为空时
- if (!empty($payList)) {
- foreach ($payList as $key => $value) {
- $memberInfo = Db::table('cy_members')->where(['id' => $userid])->find();
- $gameInfo = Db::table('cy_game')->where(['id' => $value['gameid']])->find();
- if ($memberInfo && $gameInfo) {
- // 启动事务
- Db::startTrans();
- try {
- //包含平台币
- if ($value['real_coin'] > 0) {
- $members = Db::table('cy_members')->field('id,amount')->where(['id' => $userid])->find();
- $coinData = array();
- $coinData['amount'] = Db::raw("amount+" . $value['real_coin']);
- $coinData['update_time'] = time();
- $result = Db::table('cy_members')->where(['id' => $members['id']])->update($coinData);
- if (!$result) {
- throw new Exception("用户游戏平台币账户金额变动失败");
- }
- $amount = priceFormat($members['amount'] + $value['real_coin']);
- $coinData = [
- 'userid' => $members['id'],
- 'orderid' => $value['orderid'] . "_T",
- 'start_amount' => $members['amount'],
- 'amount' => $value['real_coin'],
- 'result_amount' => $amount,
- 'type' => 3,
- 'remarks' => '平台币未付款退回',
- 'create_time' => NOW_TIMESTAMP
- ];
- if (!$this->insertGetId($coinData)) {
- throw new Exception("订单消费释放状态更改失败,订单保存失败");
- }
- }
- //专属币处理
- if ($value['real_ptb'] > 0) {
- $userCoinInfo = Db::table('nw_member_zscoin')->field('id,userid,username,game_id,amount,status')->where(['userid' => $userid, 'game_id' => $value['gameid']])->find();
- if (!empty($userCoinInfo)) {
- $coinData = array();
- $coinData['amount'] = Db::raw("amount+" . $value['real_ptb']);
- $coinData['update_time'] = time();
- $result = Db::table('nw_member_zscoin')->where(['id' => $userCoinInfo['id'], 'userid' => $userCoinInfo['userid'], 'game_id' => $userCoinInfo['game_id']])->update($coinData);
- if (!$result) {
- throw new Exception("用户游戏平台币账户金额变动失败");
- }
- $coinDetData = array();
- $coinDetData['userid'] = $userid;
- $coinDetData['username'] = $memberInfo['username'];
- $coinDetData['game_id'] = $value['gameid'];
- $coinDetData['type'] = 4;
- $coinDetData['prev_amount'] = 0;
- $coinDetData['change_amount'] = $value['real_ptb'];
- $coinDetData['after_amount'] = $coinDetData['prev_amount'] + $value['real_ptb'];
- $coinDetData['out_orderid'] = $value['orderid'];
- $coinDetData['create_time'] = time();
- $coinDetData['update_time'] = time();
- $insertDetId = Db::table('nw_member_zscoin_det')->insertGetId($coinDetData);
- if (!$insertDetId) {
- throw new Exception("添加用户游戏平台币变动明细失败");
- }
- } else {
- $coinData = array();
- $coinData['userid'] = $userid;
- $coinData['username'] = $memberInfo['username'];
- $coinData['game_id'] = $value['gameid'];
- $coinData['amount'] = $value['real_ptb'];
- $coinData['status'] = 1;
- $coinData['create_time'] = time();
- $coinData['update_time'] = time();
- $result = Db::table('nw_member_zscoin')->insertGetId($coinData);
- if (!$result) {
- throw new Exception("添加用户游戏平台币账户失败");
- }
- $coinDetData = array();
- $coinDetData['userid'] = $userid;
- $coinDetData['username'] = $memberInfo['username'];
- $coinDetData['game_id'] = $value['gameid'];
- $coinDetData['type'] = 4;
- $coinDetData['prev_amount'] = 0;
- $coinDetData['change_amount'] = $value['real_ptb'];
- $coinDetData['after_amount'] = $coinDetData['prev_amount'] + $value['real_ptb'];
- $coinDetData['out_orderid'] = $value['orderid'];
- $coinDetData['create_time'] = time();
- $coinDetData['update_time'] = time();
- $insertDetId = Db::table('nw_member_zscoin_det')->insertGetId($coinDetData);
- if (!$insertDetId) {
- throw new Exception("添加用户游戏平台币变动明细失败");
- }
- }
- }
- //代金券处理
- if ($value['coupon_member_id'] > 0 && $value['coupon_amount'] > 0) {
- $coupon = model('common/CouponMember')->updateCouponIsUseById($value['coupon_member_id'], 1);
- if (!$coupon) {
- throw new Exception("订单消费释放状态更改失败");
- }
- log_message('支付代金券取消支付:订单号'.$value['id'].'代金券id'.$value['coupon_member_id'] , 'log', LOG_PATH . 'MemberCoinInfo/releaseCoin/');
- }
- //更新订单状态
- $updPayResult = Db::table('cy_pay')->where(['id' => $value['id'], 'userid' => $userid, 'status' => 0])->update(['status' => $status, 'coupon_member_id'=>0]);
- if (!$updPayResult) {
- throw new Exception("订单消费释放状态更改失败");
- }
- //更新订单通知详情状态
- $updPayResult = Db::table('cy_paycpinfo')->where(['orderid' => $value['orderid'], 'payflag' => 0])->update(['payflag' => $status]);
- if (!$updPayResult) {
- throw new Exception("订单通知详情处理失败");
- }
- // 提交事务
- Db::commit();
- if ($orderid) {
- return true;
- }
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollback();
- $template = "订单 " . $value['orderid'] . " 平台币消费释放存在异常:" . $e->getMessage()." - file: ".$e->getFile().":".$e->getLine() . "。请及时核实,时间:" . date('Y-m-d H:i:s');
- $ddurl = Env::get('dingtalk.warning_url');
- curlDD($template, $ddurl, true);
- if ($orderid) {
- return false;
- }
- }
- }
- }
- }
- }
- public function getCreateTimeAttr($value)
- {
- return date('Y-m-d H:i:s', $value);
- }
- }
|