<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic\group_activity;


use think\facade\Queue;
use app\common\logic\BaseLogic;
use app\common\model\user\User;
use app\common\model\equity\EquityConfig;
use app\common\model\group_activity\GroupOrder;
use app\common\model\group_activity\GroupActivity;
use app\common\model\group_activity\GroupUserOrder;

/**
 * 拼团订单
 * Class GroupOrderLogic
 * @package app\adminapi\logic\group_activity
 */
class GroupOrderLogic extends BaseLogic
{
    /**
     * @notes 获取拼团订单详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2025/03/13 10:31
     */
    public static function detail($params): array
    {
        $detail = GroupOrder::findOrEmpty($params['id'])->toArray();
        if ($detail) {
            //团长信息
            $detail['user'] = User::where('id', $detail['user_id'])->field('id,mobile')->findOrEmpty()->toArray();
            $detail['activity'] = GroupActivity::where('id', $detail['group_activity_id'])->findOrEmpty()->toArray();
            $detail['goods'] = EquityConfig::where('id', $detail['equity_id'])->findOrEmpty()->toArray();
            $detail['users'] = GroupUserOrder::alias('a')->leftJoin('user b','a.user_id=b.id')
                                ->where('group_order_id', $detail['id'])
                                ->field('a.*,b.mobile')
                                ->select()->toArray();
            $detail['end_time'] = date('Y-m-d H:i:s',$detail['end_time']);
        }
        return $detail;
    }
    
    /**
     * @notes 生成服务工单
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function changeWorkStatus($params) 
    {
        $detail = GroupOrder::findOrEmpty($params['id']);
        if (!$detail->isEmpty() && $detail->work_status == 0 && $detail->status == 1) {
            $detail->work_status = 1;
            $detail->save();
        }
        return $detail->toArray();
    }
}