// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\common\model; use app\api\service\User as UserService; use app\common\service\Order as OrderService; use cores\BaseModel; /** * 解锁生源用户订单模型类 * Class UnlockOrder * @package app\common\model */ class UnlockOrder extends BaseModel { protected $globalScope = ['']; // 定义表名 protected $name = 'unlock_order'; // 定义主键 protected $pk = 'order_id'; /** * 关联会员记录表 * @return \think\model\relation\BelongsTo */ public function user() { $module = self::getCalledModule(); return $this->belongsTo("app\\{$module}\\model\\User"); } /** * 付款时间 * @param $value * @return array */ public function getPayTimeAttr($value) { return format_time($value); } /** * 获取订单详情 * @param $where * @return array|null|\think\Model */ public static function detail($where) { return static::get($where); } /** * 获取老师已解锁的学校ID * @param $userId * @return array */ public static function getSchoolsByUser($userId) { return self::where(['user_id' => $userId, 'status' => 2]) ->column('school_id'); } public function createOrder($params, int $lockType = 1) { // 验证解锁操作 $schoolId = isset($params['school_id']) ? intval($params['school_id']) : 0; $userId = isset($params['user_id']) ? intval($params['user_id']) : 0; if ($schoolId <= 0) { $this->error = '学校参数错误'; return false; } // 验证解锁类型 $userIds = $lockType == 1 ? [$userId] : []; if ($lockType == 1 && $userId <= 0) { $this->error = '请选择解锁用户'; return false; } // 全解 $userInfo = UserService::getCurrentLoginUser(true); if ($lockType == 2) { $dayIds = \app\api\model\UserInfo::getLockedIds($userInfo['user_id']); $ids = UnlockOrder::getLockedUsersBySchool($userInfo['user_id'], $schoolId); $ids = $dayIds ? array_merge($dayIds, $ids) : []; $userIds = \app\api\model\User::getSourceUserIds($schoolId, $ids); } if (empty($userIds)) { $this->error = '该生源学校暂无可解锁生源'; return false; } $trade = \app\store\model\Setting::getItem('trade'); $price = isset($trade['locked']['locked_cost']) ? floatval($trade['locked']['locked_cost']) : 0; if ($price <= 0) { $this->error = '请先后台设置解锁费用'; return false; } // 订单基础信息 $num = count($userIds); $totalPrice = $num * $price; $data = [ 'user_id' => UserService::getCurrentLoginUserId(), 'order_no' => 'LC' . OrderService::createOrderNo(), 'agent_id' => isset($userInfo['info']['agent_id']) ? $userInfo['info']['agent_id'] : 0, 'school_id' => $schoolId, 'num' => $num, 'price' => $price, 'total_price' => $totalPrice, 'create_time' => time(), 'update_time' => time(), 'status' => 1, ]; $orderId = self::insertGetId($data); if (!$orderId) { $this->error = '解锁订单提交失败'; return false; } // 解锁用户数据 $datas = []; foreach ($userIds as $id) { $datas[] = [ 'order_id' => $orderId, 'user_id' => $id, 'update_time' => time(), ]; } $unlockUser = new UnlockUser; if ($datas && $unlockUser->insertAll($datas)) { $data['order_id'] = $orderId; return $data; } $this->error = '解锁订单提交失败'; return false; } /** * 保存订单数据,并返回 * @param $data * @return bool */ public function saveOrder($data) { return self::save($data); } /** * 验证支付订单 * @param $order * @return bool * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function checkPay($order) { $orderId = isset($order['order_id'])? $order['order_id'] : 0; if($orderId<=0){ $this->error = '订单参数错误'; return false; } $orderStatus = isset($order['status'])? $order['status'] : 0; if($orderStatus == 2){ $this->error = '订单已支付'; return false; } if($orderStatus != 1){ $this->error = '订单未确认或状态不可支付'; return false; } return true; } /** * 获取用户解锁订单详情(含关联数据) * @param int $orderId 订单ID * @return Order|array|null * @throws BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public static function getUserOrderDetail(int $orderId) { // 查询订单记录 $order = static::getDetail($orderId); // 该订单是否允许申请售后 $order['isAllowRefund'] = 1; return $order; } /** * 获取用户解锁订单详情(仅订单记录) * @param int $orderId * @param array $with * @return Order|array|null */ public static function getDetail(int $orderId, array $with = []) { // 查询订单记录 $order = static::detail($orderId, $with); empty($order) && throwError('订单不存在'); return $order; } /** * 获取老师该学校已经解锁的用户 * @param $userId * @param $schoolId * @return mixed */ public static function getLockedUsersBySchool($userId, $schoolId) { return self::alias('a') ->leftJoin('unlock_users u', 'u.order_id=a.order_id') ->where(['a.user_id' => $userId, 'a.school_id' => $schoolId, 'a.status' => 2]) ->column('u.user_id'); } }