$phone = $this->input_data['phone'];
// 成員數
$member_count = $user_customer_village->where(['house_id' => $house_id])->count();
if ($role === RoleEnum::HOST) { // 業主
// 業主必須傳手機號
if (!$phone) {
$this->json->err('請填寫手機號');
}
if (!Verify::checkMobile($phone)) {
$this->json->err('請填寫正確的手機號');
}
} else { // 成員
if (!$phone) {
// 自動生成手機號
$host_info = $user_customer_village->where(['house_id' => $house_id, 'role' => RoleEnum::HOST])->find();
if (!$host_info) {
$this->json->err('當前房屋未設置業主');
}
$host_id = $host_info['customer_id'];
$user_customer_info = $user_customer->where(['id' => $host_id])->find();
if (!$user_customer_info) {
$this->json->err('業主對應用戶不存在');
}
if ($id) {
$member_ids = $user_customer_village->where(['house_id' => $house_id])->order('role asc,id asc')->getField('id',true);
$member_ids = array_flip($member_ids);
$pos_val = $member_ids[$id];
// 獲取當前成員在家庭中的位置
$phone = $user_customer_info['phone'] . '-' . ($pos_val + 1);
} else {
$phone = $user_customer_info['phone'] . '-' . ($member_count + 1);
}
}
}
$member_ids = $user_customer_village->where(['house_id' => $house_id])->order('role asc,id asc')->getField('id',true);
$member_ids = array_flip($member_ids);
$pos_val = $member_ids[$id];
// 獲取當前成員在家庭中的位置
$phone = $user_customer_info['phone'] . '-' . ($pos_val + 1);