TeamGroup.php 1.8 KB
<?php

namespace app\api\model;

use app\api\controller\Pay;
use think\Model;
use think\Db;

class TeamGroup extends Model
{
    public function selectData($where, $limit)
    {
        $where['status'] = 1;
        $where['grouptime'] = ['>',time()];
        $data = $this
            ->where($where)
            ->limit($limit)
            ->order('createtime desc')
            ->field('id team_group_id,user_avatar,user_nickname,grouptime,group_num ')
            ->select();
        return $data;
    }

    public function pastDue($ids){
        Db::startTrans();
        $orderModel = new Order();
        $pay = new Pay();
        $userModel = new User();
        try {
            //修改拼团表状态
            $res1 = $this->where(['id'=>['in',$ids]])->update(['status'=>3]);
            //修改订单表拼团状态
            $res2 = $orderModel->where(['team_group_id'=>['in',$ids]])->update(['team_group_status'=>3]);
            $orderList = $orderModel->where(['team_group_id'=>['in',$ids]])->field('id,order_num,pay_total,pay_type,user_id')->select();

            //退款
            foreach ($orderList as $k => $orderInfo){
                if ($orderInfo['pay_type'] == 1){
                    //微信退款
                    $res3 = $pay->refund($orderInfo['order_num'],$orderInfo['pay_total']);
                }else{
                    //余额退款
                    $res3 = $userModel->refund($orderInfo['user_id'],$orderInfo['pay_total'],$orderInfo['id']);
                }
                if (!$res3) return false;
            }
            if ($res1 && $res2){
                Db::commit();
                return true;
            }else{
                return false;
            }
        } catch (Exception $e) {
            Db::rollback();
            return false;
        }

    }
}