<?php

namespace app\admin\service\member;

use app\model\Family;
use app\model\FamilyMember;

class FamilyService
{
    public static function list($page, $pageSize, $keywords)
    {
        $rows = Family::when($keywords != '', function ($query) use ($keywords) {
            $query->where('family_name', 'like', '%' . $keywords . '%');
        });
        // 总条数
        $total = $rows->count('family_id');
        $rows = $rows->with([
            'familyMember' => function ($query) {
                $query->select('family_member_id', 'join_family_id', 'family_member_name','family_member_remark','family_member_addtimes');
            }
        ])->select('family_id', 'join_family_creator_member_id','family_city', 'family_name', 'family_remark', 'family_addtimes')
            ->orderBy('family_addtimes', 'DESC')
            ->forPage($page, $pageSize)
            ->get()
            ->toArray();
        foreach ($rows as &$row){
            $row['family_addtimes'] = date('Y-m-d H:i:s',strtotime($row['family_addtimes']));
        }

        return json_success('', compact('rows', 'page', 'pageSize', 'total'));
    }

    /**
     * @Desc 家庭列表
     * @Author Gorden
     * @Date 2024/3/6 10:56
     *
     * @param $memberId
     * @return \support\Response
     */
    public static function selectList($memberId)
    {
        $list = Family::with('familyMember')
            ->where('join_family_creator_member_id', $memberId)
            ->get()
            ->toArray();

        return json_success('', $list);
    }

    /**
     * @Desc 家庭详情
     * @Author Gorden
     * @Date 2024/3/6 11:23
     *
     * @param $familyId
     * @return \support\Response
     */
    public static function selectInfo($familyId)
    {
        $family = Family::with('familyMember')
            ->where('family_id', $familyId)
            ->first();
        $family = $family ? $family->toArray() : [];

        return json_success('', $family);
    }
}